Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

Bytom移动端钱包SDK开发基础

yuan081608
2338 0 0
比原项目仓库:% H/ V* ~- f6 `6 F$ l
Github地址:https://github.com/Bytom/bytom
$ l. E8 P; s% c& x" g" }! yGitee地址:https://gitee.com/BytomBlockchain/bytom
# J' T' L! R' x0 C, |: S8 i1 ^Bytom-Mobile-Wallet-SDK 是从bytom源码中抽离出的钱包层代码,并且对钱包层代码进行了改造。使用gomobile可以将代码 编译成Android和iOS平台可用的SDK,使用编译后的Android和iOS钱包SDK可以在移动端实现创建bytom密钥、账户、地址和交易签名功能。
+ ]8 u& j- g" kBytom-Mobile-Wallet-SDK源码简介8 [: v; N: R/ K% g% \1 c
SDK源码放在项目的sdk文件夹中,android和ios文件夹是使用SDK的demo项目,bind.go 中首字母大写可以外部调用的函数会作为提供给Android和iOS调用的API。bytom创建的密钥对会存储在磁盘单独的文件中,而且对私钥进行了加密,账户地址数据是存储在go实现的leveldb中,所以Android和iOS平台也需要提供数据存储的路径。
% L5 X2 ?* q4 zfunc InitWallet(storagePath string) {
7 ~! s; d% `2 r3 R! t, ~    hsm := pseudohsm.New(storagePath)( h; d6 K0 `$ C" e' g9 s' N
    walletDB := db.NewDB("wallet", "leveldb", storagePath)
* R4 m* e$ C6 w# t    accounts := account.NewManager(walletDB)
9 r1 O" T4 w9 \    assets := asset.NewRegistry(walletDB)' \+ T; {/ L  W: K
    wallet := aWallet.NewWallet(walletDB, accounts, assets, hsm)6 l, T& M0 C0 [/ P0 G, r
    api = aApi.API{Wallet: wallet}
. ^! K% m! ^! X# f$ U9 }/ F+ k}
3 |6 s5 b1 b- e& F$ k3 `Android和iOS平台调用其他钱包API的之前需要先调用InitWallet这个API,参数是磁盘上的绝对路径,InitWallet会对整个钱包进行一个初始化, 其中最重要是初始化leveldb的存储。其他的CreateKey、CreateAccount、CreateAccountReceiver是创建密钥、账户、地址等API,RestoreWallet API能够对钱包所有账户地址资产进行备份导出json格式的数据。
, }9 y$ G( X! q1 }0 jBytom-Mobile-Wallet-SDK的编译
" K5 w4 }& c) \' f- o% [3 q* X( d, FSDK代码的编译首先需要正确的安装golang和gomobile,golang需要1.7以上版本。
! C8 Y5 s& x4 h5 iAndroid平台需要安装JDK、Android SDK、Android NDK,并且需要将Android SDK的platform-tools、ndk-bundle 添加到PATH系统环境变量中。iOS平台编译环境配置相对比较简单只需要安装Xcode就可以了。
; ?! U6 E5 ~: K- j. v- b1 p3 K+ CClone项目到本地$GOPATH/src下:
8 l: B5 X; L; z git clone https://github.com/Bytom-Community/Bytom-Mobile-Wallet-SDK $GOPATH/src/github.com/bytom-community/mobile" ~2 W! l* A& `& l' ?" r( X
Android
( ], [7 a8 B6 }# G% Tgomobile init -ndk ~/path/to/your/ndk9 i6 x0 a- \8 Y3 y7 n1 v% i4 q
cd $GOPATH/src/github.com/bytom-community/mobile
: p* I- d, j/ f7 S$ `gomobile bind -target=android github.com/bytom-community/mobile/sdk/) D' T: D' s, Q+ }, U2 T! _2 n
如果需要减小SDK的体积给gomobile bind指令加上-ldflags=-s参数:
+ w6 ~* z# H+ ~gomobile bind -target=android -ldflags=-s github.com/bytom-community/mobile/sdk/
+ o7 H; k4 w5 S  H执行指令后会在mobile文件夹生成wallet.aar和wallet-sources.jar文件。# i0 Q6 \) O; H8 \) s  M/ D
iOS
3 q) y, |. x5 y8 k0 a- Rcd $GOPATH/src/github.com/bytom-community/mobile
2 I' Q' D/ V& u* m: {gomobile bind -target=ios github.com/bytom-community/mobile/sdk/
0 T. s; d5 G4 [; o' y. M$ N5 `如果需要减小SDK的体积给gomobile bind指令加上-ldflags=-w参数:
6 C$ m$ r! Y! y+ v8 L+ E; E$ gomobile bind -target=ios -ldflags=-w github.com/bytom-community/mobile/sdk/
' t- W; k- O7 E执行指令后会在mobile文件夹生成wallet.framework文件。
8 d: ^; y5 ~; g7 n由于gomobile现在没有支持bitcode,所以生成的iOS SDK也不支持bitcode。
9 N  y. X; n$ s' q, M+ mBytom-Mobile-Wallet-SDK的使用
# N- ?0 _* e. m* p6 I0 c+ XAndroid% t. W0 I4 u4 p7 t( w5 a" s
拷贝wallet.aar和wallet-sources.ja到Android项目的app的libs文件夹下,并在app module中的build.gradle文件中添加:
. L* Z" i% o5 P0 v- Vandroid {, k* @* Q* B& t. @/ f' y: S
    repositories {
' E- ^8 U0 l; l& R6 i1 m  M1 U        flatDir { dirs 'libs' }
% B: t6 T# ?8 E) L5 [    }2 L! ]' ~3 g, U0 `% N
}4 A# I9 a- E& ^. M' T) u' f
dependencies {5 I3 Z; q  w* G! X( P
    implementation fileTree(include: ['*.jar'], dir: 'libs'): L7 {( J4 G1 o- r
    implementation(name: 'wallet', ext: 'aar')( S7 W5 @/ r8 w  g/ q
}! O/ _# f9 D8 h1 w
sync project后可以在Android项目中对SDK的API进行调用:
% z9 C' \% [8 _5 w1 a6 ]* {package io.bytom.community;
$ E( r% {5 V# w! B; Cimport android.app.Activity;; P0 v6 v& d$ F# ?4 b5 O
import android.os.Bundle;0 R6 P$ f& R9 X5 G
import android.util.Log;
  k9 U1 W, k6 X; M/ i* R+ C7 _2 Iimport android.widget.TextView;: F6 X" t+ a: m$ i+ w, A; s
import wallet.Wallet;% L9 t" I; M* W: V) S, G
public class MainActivity extends Activity {3 }$ N) |$ j; H+ d! q! Q% g
    @Override
- L, z. u, G* d( \# S. E5 S0 m- ?    protected void onCreate(Bundle savedInstanceState) {+ u3 i+ N( P. K, U: H. K
        super.onCreate(savedInstanceState);
) [# O9 F0 o: M3 ~% \' k- G        setContentView(R.layout.activity_main);2 ^, R3 G) ~$ d. P  Y
        TextView keyTextView = (TextView) findViewById(R.id.key_textview);
  L/ ^7 Y' ?; D7 n$ I, Z        String storagePath = getFilesDir().toString();
1 ?/ j4 a/ I: W! H( A; |        Log.d("storagePath", storagePath);
# l) o) K* T, E" \6 r        Wallet.initWallet(storagePath);
0 ^  V# e9 K  R        String keyResult = Wallet.createKey("Marshall", "123456");2 N0 V8 `5 X! Y' e8 O
        Log.d("keyResult", keyResult);
. _  ?$ A# N" ?6 i# D        keyTextView.setText(keyResult);- x! k  Z2 y' `
    }- _, \( E* J+ }! t4 |* m2 h
}
' U+ a4 F. S! P+ H; v) miOS
5 i0 k$ Z0 A( M+ J通过项目target的Linked frameworks and libraries把wallet.framework添加到项目,可以在iOS项目中对SDK的API进行调用:6 r+ ]% L9 C7 h( P  N0 c
#import "ViewController.h"
/ E! |. y' }- R1 _0 S#import "Wallet/Wallet.h"  // Gomobile bind generated framework; S+ y2 g0 ^4 Q, D
@interface ViewController ()( K. m+ m! d: j4 E9 M  j
@end0 y1 Q. W2 r2 ^3 Z
@implementation ViewController
* Z0 t: V( K- V& l% W- V@synthesize textLabel;
6 B; W- v: ~" O, c$ J+ A  X- (void)loadView {: B; Y$ h( D4 Y: ^
    [super loadView];# ]1 y2 y- W! R$ g) D% B* D* R
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
: W0 o' U9 A; G, {. X/ g) o% ~    WalletInitWallet(docPath);
8 ~( u& I) `9 a6 v    textLabel.text = WalletCreateKey(@"kevin",@"123456");
! z0 m  y7 q# k0 {0 T5 V}, U. q' S3 g( f+ u
@end
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

yuan081608 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    2