Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

Bytom移动端钱包SDK开发基础

yuan081608
2339 0 0
比原项目仓库:2 J$ }1 h) N4 h; V& ?4 }
Github地址:https://github.com/Bytom/bytom
  p* h. y1 k9 f* V! W% v% LGitee地址:https://gitee.com/BytomBlockchain/bytom
% x% {0 A8 m! H: TBytom-Mobile-Wallet-SDK 是从bytom源码中抽离出的钱包层代码,并且对钱包层代码进行了改造。使用gomobile可以将代码 编译成Android和iOS平台可用的SDK,使用编译后的Android和iOS钱包SDK可以在移动端实现创建bytom密钥、账户、地址和交易签名功能。. `4 @4 J& [& O$ x  y$ a9 x* L" u) F
Bytom-Mobile-Wallet-SDK源码简介: _+ t2 }" `- a# ~. G  ]4 k  a
SDK源码放在项目的sdk文件夹中,android和ios文件夹是使用SDK的demo项目,bind.go 中首字母大写可以外部调用的函数会作为提供给Android和iOS调用的API。bytom创建的密钥对会存储在磁盘单独的文件中,而且对私钥进行了加密,账户地址数据是存储在go实现的leveldb中,所以Android和iOS平台也需要提供数据存储的路径。
( T. p7 F  F3 t( p4 Jfunc InitWallet(storagePath string) {+ I5 E6 |- Z# T
    hsm := pseudohsm.New(storagePath)
+ E; @/ ?& y/ y! s5 o' j* p    walletDB := db.NewDB("wallet", "leveldb", storagePath)
' }9 R$ S. L6 w" x1 X6 `* E    accounts := account.NewManager(walletDB)1 z3 Z  c8 L8 U' c& w
    assets := asset.NewRegistry(walletDB)
1 N6 V7 [! ~( z: f* x$ z/ R    wallet := aWallet.NewWallet(walletDB, accounts, assets, hsm); |$ c# g+ z7 j% }
    api = aApi.API{Wallet: wallet}9 M0 Z9 C7 J8 V
}
1 B8 _4 B  P& |4 a. `& yAndroid和iOS平台调用其他钱包API的之前需要先调用InitWallet这个API,参数是磁盘上的绝对路径,InitWallet会对整个钱包进行一个初始化, 其中最重要是初始化leveldb的存储。其他的CreateKey、CreateAccount、CreateAccountReceiver是创建密钥、账户、地址等API,RestoreWallet API能够对钱包所有账户地址资产进行备份导出json格式的数据。% l0 B8 f( C1 `- ]) u9 o6 J
Bytom-Mobile-Wallet-SDK的编译* a, X+ M, U; b2 U! I
SDK代码的编译首先需要正确的安装golang和gomobile,golang需要1.7以上版本。
7 K! ^  w/ v7 M+ j1 |) P# U: ?! VAndroid平台需要安装JDK、Android SDK、Android NDK,并且需要将Android SDK的platform-tools、ndk-bundle 添加到PATH系统环境变量中。iOS平台编译环境配置相对比较简单只需要安装Xcode就可以了。, g! B' ?9 H. b. q
Clone项目到本地$GOPATH/src下:
' C! ?( Y2 i1 K git clone https://github.com/Bytom-Community/Bytom-Mobile-Wallet-SDK $GOPATH/src/github.com/bytom-community/mobile
: W+ h/ N7 O9 D" ?8 a1 b* }Android
, ~+ H! \4 {1 Z& l1 J5 j0 V0 Fgomobile init -ndk ~/path/to/your/ndk
1 S- a1 n( w# P/ e- Icd $GOPATH/src/github.com/bytom-community/mobile# X! V8 `- E" R9 O7 R3 y( q9 u
gomobile bind -target=android github.com/bytom-community/mobile/sdk/
% K/ D4 D: r0 W4 L如果需要减小SDK的体积给gomobile bind指令加上-ldflags=-s参数:3 r: Z- [. g. n' C" x& Q: ~
gomobile bind -target=android -ldflags=-s github.com/bytom-community/mobile/sdk/7 ]  r/ l$ n$ c" O' `- ]$ l
执行指令后会在mobile文件夹生成wallet.aar和wallet-sources.jar文件。
, ?  N/ z0 T) s( }+ hiOS. a3 h3 W% t+ S$ I! j; n
cd $GOPATH/src/github.com/bytom-community/mobile
( \% \9 O9 t" }2 m# ugomobile bind -target=ios github.com/bytom-community/mobile/sdk/
/ h: M8 b3 o7 Y- i; x1 [- a如果需要减小SDK的体积给gomobile bind指令加上-ldflags=-w参数:- I# p' p: ~4 G
$ gomobile bind -target=ios -ldflags=-w github.com/bytom-community/mobile/sdk/5 a* f- v+ @, q$ b$ E6 \. u5 x4 v
执行指令后会在mobile文件夹生成wallet.framework文件。& O# f1 e% M6 v) B/ v
由于gomobile现在没有支持bitcode,所以生成的iOS SDK也不支持bitcode。
/ ]1 f- G+ u  M7 V5 ^# bBytom-Mobile-Wallet-SDK的使用0 `6 l0 B$ m2 j$ v' V5 v$ x6 |
Android
& u+ [1 ~$ w& Z0 ]0 b4 v) e9 q3 K拷贝wallet.aar和wallet-sources.ja到Android项目的app的libs文件夹下,并在app module中的build.gradle文件中添加:
" t4 T) F3 l' Q8 B6 Vandroid {3 L: n1 Z1 Q) J3 Y1 _3 m' S
    repositories {" f# N0 q, J& x# p0 Z0 l. R
        flatDir { dirs 'libs' }
; s  V6 j7 |( p% n) `# J% g* Z    }
: S- q0 W9 y) N* c1 p}' ~9 a* s) _  k# K* {. }
dependencies {
8 i% J+ h  o) N    implementation fileTree(include: ['*.jar'], dir: 'libs')
# r$ c/ Q1 P8 m" p- V4 y$ F/ u+ E    implementation(name: 'wallet', ext: 'aar')) R& w4 o7 L# p/ Q' @( v
}
6 L! f- x% g7 Y$ v' \" K& H1 hsync project后可以在Android项目中对SDK的API进行调用:
) a/ L1 |6 o% ^! N" i! l# Epackage io.bytom.community;
% U/ c0 V/ c7 R& L8 B6 `import android.app.Activity;; {" ~' [2 c9 @: ^& v8 t
import android.os.Bundle;+ A1 m% N" X, y
import android.util.Log;
$ M5 T5 H. F: C, l- I2 himport android.widget.TextView;
" W" @1 m- ^# g9 F/ Y% ^6 v, Nimport wallet.Wallet;
( d  W! V, Y  Zpublic class MainActivity extends Activity {
) g# F, b, @: b! K& k    @Override. w4 r+ U9 V! c
    protected void onCreate(Bundle savedInstanceState) {, }' J' q" g2 `, \5 ~) o
        super.onCreate(savedInstanceState);
; n: F! v, w8 P' w- u+ b        setContentView(R.layout.activity_main);
) t+ F* d7 d, i        TextView keyTextView = (TextView) findViewById(R.id.key_textview);
9 J. L- O% @7 w$ j4 ]7 @        String storagePath = getFilesDir().toString();: U0 q3 a7 ]5 Y
        Log.d("storagePath", storagePath);/ K2 w, l0 q- [" I
        Wallet.initWallet(storagePath);% R& B3 w3 n( T% g( C
        String keyResult = Wallet.createKey("Marshall", "123456");: ?1 C- h0 w$ k% O! Y
        Log.d("keyResult", keyResult);' G7 ~4 C8 r# F& a- D
        keyTextView.setText(keyResult);
6 {4 @' Q6 V4 `5 r: S" C    }
/ i5 b" f) e( R" A2 n4 Q+ l}& K1 I3 G$ {: |
iOS
. f. W8 k" U3 X0 H4 E+ Q通过项目target的Linked frameworks and libraries把wallet.framework添加到项目,可以在iOS项目中对SDK的API进行调用:9 X9 Z% }  w' y0 L1 F. }
#import "ViewController.h"4 g5 O( J9 S  ?: b. S5 l+ u
#import "Wallet/Wallet.h"  // Gomobile bind generated framework
  L# ^4 T$ @0 _$ N5 \' ~* g@interface ViewController ()
9 x) w4 S7 H$ f" X9 \8 K3 y@end
7 N. [0 c0 Q) t9 \4 ~  ~/ ]@implementation ViewController5 `: u+ K5 ~3 S& P' _  e2 F
@synthesize textLabel;; d1 V. h* t# r  v. I/ G) O
- (void)loadView {3 ?, h4 j( u) u. }9 n9 A) G0 O
    [super loadView];
! {4 c% V; ]. b+ l% ]    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
6 m& }0 h1 h" Y+ U    WalletInitWallet(docPath);2 l% _" K. }" N8 |$ s  D
    textLabel.text = WalletCreateKey(@"kevin",@"123456");
5 }+ f+ d- K8 R% M: B0 Z  t}, X# a. P* w) X3 g7 U) L
@end
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

yuan081608 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    2