Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

Bytom移动端钱包SDK开发基础

yuan081608
2394 0 0
比原项目仓库:
% b8 [# b; S) L$ XGithub地址:https://github.com/Bytom/bytom
) W4 N; a/ z" A+ T9 L# g' Z" |Gitee地址:https://gitee.com/BytomBlockchain/bytom2 X* ?" k0 P1 c2 G/ x
Bytom-Mobile-Wallet-SDK 是从bytom源码中抽离出的钱包层代码,并且对钱包层代码进行了改造。使用gomobile可以将代码 编译成Android和iOS平台可用的SDK,使用编译后的Android和iOS钱包SDK可以在移动端实现创建bytom密钥、账户、地址和交易签名功能。6 E- A, i" Q7 W( R5 X0 t
Bytom-Mobile-Wallet-SDK源码简介7 C: r) P7 n0 n
SDK源码放在项目的sdk文件夹中,android和ios文件夹是使用SDK的demo项目,bind.go 中首字母大写可以外部调用的函数会作为提供给Android和iOS调用的API。bytom创建的密钥对会存储在磁盘单独的文件中,而且对私钥进行了加密,账户地址数据是存储在go实现的leveldb中,所以Android和iOS平台也需要提供数据存储的路径。
  n! U/ M' p/ w# z7 Mfunc InitWallet(storagePath string) {
7 F* q+ [) H; q: n    hsm := pseudohsm.New(storagePath)
# ^! t7 S0 d7 F) v2 y    walletDB := db.NewDB("wallet", "leveldb", storagePath)" R9 b: Z0 S! W, T+ m$ M5 V" k4 z
    accounts := account.NewManager(walletDB)
& |3 n* ?6 A3 D9 [( E1 R8 a: i' f- V    assets := asset.NewRegistry(walletDB)
6 Y* z$ Y8 u2 _% {+ R3 ^/ e2 H    wallet := aWallet.NewWallet(walletDB, accounts, assets, hsm)8 M4 h2 t8 x8 F8 H
    api = aApi.API{Wallet: wallet}+ b0 i1 ~4 |* ?
}
- {9 z% X* W  V( C9 X4 |Android和iOS平台调用其他钱包API的之前需要先调用InitWallet这个API,参数是磁盘上的绝对路径,InitWallet会对整个钱包进行一个初始化, 其中最重要是初始化leveldb的存储。其他的CreateKey、CreateAccount、CreateAccountReceiver是创建密钥、账户、地址等API,RestoreWallet API能够对钱包所有账户地址资产进行备份导出json格式的数据。
* `$ S5 Z& u* W4 U% s& IBytom-Mobile-Wallet-SDK的编译
" o* u% `) h1 eSDK代码的编译首先需要正确的安装golang和gomobile,golang需要1.7以上版本。
6 Z8 o' C. d" ^Android平台需要安装JDK、Android SDK、Android NDK,并且需要将Android SDK的platform-tools、ndk-bundle 添加到PATH系统环境变量中。iOS平台编译环境配置相对比较简单只需要安装Xcode就可以了。/ M% t' L3 T( j6 {" ]
Clone项目到本地$GOPATH/src下:
8 E, ], l, R0 g git clone https://github.com/Bytom-Community/Bytom-Mobile-Wallet-SDK $GOPATH/src/github.com/bytom-community/mobile5 [* L5 J, _0 f8 \6 T' ]! f' c+ t
Android
; p9 l4 Z* t: Z/ B* v1 p8 k+ E# p  Wgomobile init -ndk ~/path/to/your/ndk
0 F, B# T% @! scd $GOPATH/src/github.com/bytom-community/mobile
- j7 d2 W& b/ s$ ]" U% Y. Ngomobile bind -target=android github.com/bytom-community/mobile/sdk/+ z1 S/ Y) [! T8 d
如果需要减小SDK的体积给gomobile bind指令加上-ldflags=-s参数:
, ?* ]' L: Z9 j( f9 jgomobile bind -target=android -ldflags=-s github.com/bytom-community/mobile/sdk/# e1 |% Q, X% V6 w2 Y
执行指令后会在mobile文件夹生成wallet.aar和wallet-sources.jar文件。
( N* k. }) @/ i) p5 @* Z& @/ [iOS) U/ g. \, [/ d& v4 E: n
cd $GOPATH/src/github.com/bytom-community/mobile! ]/ `0 f& {7 n* C
gomobile bind -target=ios github.com/bytom-community/mobile/sdk/
4 |( O* z3 d+ F* G. J+ ?如果需要减小SDK的体积给gomobile bind指令加上-ldflags=-w参数:
$ K* ~' }8 h* y$ gomobile bind -target=ios -ldflags=-w github.com/bytom-community/mobile/sdk/
3 A- k; ~' D: ]0 f* c$ ]- J7 Q执行指令后会在mobile文件夹生成wallet.framework文件。: S! w' T) T5 n( ]7 W  `% _7 ^) y3 `5 S6 }
由于gomobile现在没有支持bitcode,所以生成的iOS SDK也不支持bitcode。
" I% y+ T  i$ b' XBytom-Mobile-Wallet-SDK的使用
/ g" D  n$ b4 QAndroid
+ a& x" P# r/ H) S5 ~+ j& H) d拷贝wallet.aar和wallet-sources.ja到Android项目的app的libs文件夹下,并在app module中的build.gradle文件中添加:# S/ v/ j/ K4 F: X1 x
android {
8 ]! b- y+ i2 ?3 B7 F; e. {# R    repositories {! d" V7 H5 D5 U+ h$ q
        flatDir { dirs 'libs' }
( i2 I" m) V1 q    }
7 a' N  F; E4 o2 S}
' \) |- O4 m% E7 f) |/ T* N5 qdependencies {
& r( |; @6 X# S/ V& B; K    implementation fileTree(include: ['*.jar'], dir: 'libs')) n2 D' l* J  ]8 F
    implementation(name: 'wallet', ext: 'aar')! i6 L( _5 o. b! N* e% Y$ ]: o5 D" }" H
}  l/ L" \1 s% `0 U
sync project后可以在Android项目中对SDK的API进行调用:
. |. q6 B0 r. S, Qpackage io.bytom.community;% }* l, X5 ~# _
import android.app.Activity;
, K% m- s3 b  Z% Nimport android.os.Bundle;
7 B8 [% ^) w' G- mimport android.util.Log;
3 i% y. _7 L2 q5 S- M( O+ iimport android.widget.TextView;
+ @* _7 q. F& eimport wallet.Wallet;3 E1 V1 G  f- _. d# D, ^# [
public class MainActivity extends Activity {
9 a9 B/ m6 C* t7 f2 E! o    @Override: ~9 t' u% U% K' x7 Q5 ]& R1 d
    protected void onCreate(Bundle savedInstanceState) {
+ Q: D9 T9 c4 ^5 G4 r        super.onCreate(savedInstanceState);
9 Z* u' l' x& w* t5 E( y% D        setContentView(R.layout.activity_main);# D. J* ]4 O' o: k) ~1 t- |
        TextView keyTextView = (TextView) findViewById(R.id.key_textview);
( H. \7 T$ j2 S1 w, o" ~  g6 V        String storagePath = getFilesDir().toString();9 r& \/ l% M. P, n' v
        Log.d("storagePath", storagePath);
6 p0 `! @  j2 D3 l! G        Wallet.initWallet(storagePath);6 ?7 o3 r& }1 l+ O& h' ^
        String keyResult = Wallet.createKey("Marshall", "123456");$ ]; o  F% ^% k" d' Z
        Log.d("keyResult", keyResult);
7 V; K+ `8 ^7 f) r+ U; t        keyTextView.setText(keyResult);+ N9 l6 q  s/ {7 A% g
    }
+ K4 @0 ]% X* a; v  D3 I# a}
8 P. ^1 H8 s. qiOS- U. }! W+ O; v+ c1 o4 l
通过项目target的Linked frameworks and libraries把wallet.framework添加到项目,可以在iOS项目中对SDK的API进行调用:" i/ m4 [% w6 q" b. o) j
#import "ViewController.h"
) `* ]. j3 t3 T/ N1 E1 c" f2 e$ Z# T#import "Wallet/Wallet.h"  // Gomobile bind generated framework
% N" a- S9 h8 v9 \@interface ViewController ()5 l9 g; U' L6 f  \9 Q( S
@end) J2 k- n* L- V7 t5 C
@implementation ViewController3 y( s7 z6 j& x2 ?# W4 z
@synthesize textLabel;
' {9 h' z- |: W, U, z- (void)loadView {/ y* T% C; b# ]" r8 u
    [super loadView];
* }- G% H7 S1 k7 c5 \- |0 z, r    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
  V/ G: z0 V$ g8 ]9 k, ]% \4 T    WalletInitWallet(docPath);6 U, z3 u7 t' M% v
    textLabel.text = WalletCreateKey(@"kevin",@"123456");3 a# X7 Y; O4 A, d, A
}- Z. E$ _% }! j2 H3 n# |9 \
@end
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

yuan081608 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    2