Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

Bytom移动端钱包SDK开发基础

yuan081608
2340 0 0
比原项目仓库:3 C% B# r& i4 I5 @% f2 P: @
Github地址:https://github.com/Bytom/bytom2 _1 d5 M$ Q& n+ o0 e. \% `
Gitee地址:https://gitee.com/BytomBlockchain/bytom
/ J9 X. X! @4 U& U' @, qBytom-Mobile-Wallet-SDK 是从bytom源码中抽离出的钱包层代码,并且对钱包层代码进行了改造。使用gomobile可以将代码 编译成Android和iOS平台可用的SDK,使用编译后的Android和iOS钱包SDK可以在移动端实现创建bytom密钥、账户、地址和交易签名功能。
& M, o+ ?1 d* e  @2 g% lBytom-Mobile-Wallet-SDK源码简介. A2 K, G% J" l: H1 S, a/ L" h
SDK源码放在项目的sdk文件夹中,android和ios文件夹是使用SDK的demo项目,bind.go 中首字母大写可以外部调用的函数会作为提供给Android和iOS调用的API。bytom创建的密钥对会存储在磁盘单独的文件中,而且对私钥进行了加密,账户地址数据是存储在go实现的leveldb中,所以Android和iOS平台也需要提供数据存储的路径。
8 U% x4 Q+ j6 p0 P- `func InitWallet(storagePath string) {
& D; w9 X; X: {, B    hsm := pseudohsm.New(storagePath)
/ n% ^: a0 a! u  {& \4 B. T' V    walletDB := db.NewDB("wallet", "leveldb", storagePath)
/ \0 K, A( k- y    accounts := account.NewManager(walletDB)9 j- C) L) K# y2 g$ v8 [  K
    assets := asset.NewRegistry(walletDB): q7 u: N$ p! h: e
    wallet := aWallet.NewWallet(walletDB, accounts, assets, hsm)
- w* ^! _' X/ B    api = aApi.API{Wallet: wallet}* @3 L- t' ?+ N5 [2 N
}$ B+ O1 c& K" C& _
Android和iOS平台调用其他钱包API的之前需要先调用InitWallet这个API,参数是磁盘上的绝对路径,InitWallet会对整个钱包进行一个初始化, 其中最重要是初始化leveldb的存储。其他的CreateKey、CreateAccount、CreateAccountReceiver是创建密钥、账户、地址等API,RestoreWallet API能够对钱包所有账户地址资产进行备份导出json格式的数据。4 ?8 h% |4 p2 ~
Bytom-Mobile-Wallet-SDK的编译9 ]6 Z1 W2 z# n
SDK代码的编译首先需要正确的安装golang和gomobile,golang需要1.7以上版本。+ T4 i, h0 R  q' i
Android平台需要安装JDK、Android SDK、Android NDK,并且需要将Android SDK的platform-tools、ndk-bundle 添加到PATH系统环境变量中。iOS平台编译环境配置相对比较简单只需要安装Xcode就可以了。
( S5 o. q8 _8 y1 ~& eClone项目到本地$GOPATH/src下:) j' v$ s0 C- S" j. _
git clone https://github.com/Bytom-Community/Bytom-Mobile-Wallet-SDK $GOPATH/src/github.com/bytom-community/mobile& e9 N* ~5 N( D% a$ a4 K9 K
Android
* j, ?+ C- ^2 ?% r+ {gomobile init -ndk ~/path/to/your/ndk  g5 n2 F6 u! n3 J) a1 T, _
cd $GOPATH/src/github.com/bytom-community/mobile
4 X$ z2 _( I0 H, sgomobile bind -target=android github.com/bytom-community/mobile/sdk/. {0 O  V- H8 m
如果需要减小SDK的体积给gomobile bind指令加上-ldflags=-s参数:
' {" k' H$ n3 U( k- r2 Q4 Sgomobile bind -target=android -ldflags=-s github.com/bytom-community/mobile/sdk/8 E1 D  H/ m$ ?4 b: h/ e0 d
执行指令后会在mobile文件夹生成wallet.aar和wallet-sources.jar文件。& _& K' G3 r7 G: Q% [7 S
iOS
6 D& D- B  H3 kcd $GOPATH/src/github.com/bytom-community/mobile
' \) u: H2 ]5 b5 M6 G4 ogomobile bind -target=ios github.com/bytom-community/mobile/sdk/
! e  n* X& j6 A3 c# p如果需要减小SDK的体积给gomobile bind指令加上-ldflags=-w参数:
/ T1 \6 ^5 J2 [$ i5 }! b8 X$ gomobile bind -target=ios -ldflags=-w github.com/bytom-community/mobile/sdk/3 C/ ^# u- q+ A4 ?2 Q
执行指令后会在mobile文件夹生成wallet.framework文件。$ P: \$ I: a) Z+ e2 p7 Z) n
由于gomobile现在没有支持bitcode,所以生成的iOS SDK也不支持bitcode。
" c! l' V# N# d. h) JBytom-Mobile-Wallet-SDK的使用
& [+ g4 I# Y7 y6 uAndroid
6 j- Y0 K, Z; A拷贝wallet.aar和wallet-sources.ja到Android项目的app的libs文件夹下,并在app module中的build.gradle文件中添加:
) O2 p7 i' Q+ |, O. Wandroid {
; B/ x+ P' }7 g7 q: a* O* }: C    repositories {
& C  i( @9 ]5 D( P$ u4 L# s" ?+ ~1 b        flatDir { dirs 'libs' }; H5 x* d  d% F  Q( w4 u5 C
    }
5 {2 h+ x/ R0 J! |2 Y}+ x* r" [3 R# V$ g  G) [
dependencies {, \( k7 W5 Q3 b# W3 b
    implementation fileTree(include: ['*.jar'], dir: 'libs')
9 K$ t, m# F- N8 n$ H, ~2 }& i$ a$ C: N    implementation(name: 'wallet', ext: 'aar')
" Q, l& g3 f' Q' i3 m}# D0 B* E1 @$ Z+ W7 d, \6 @% u& F
sync project后可以在Android项目中对SDK的API进行调用:4 @+ J# }' k7 t" \
package io.bytom.community;
. Y: r8 \! P# k( s& |import android.app.Activity;& r3 i) I/ i% R( {7 [& R
import android.os.Bundle;! y8 f7 b) V/ Q4 s3 |  ~# ^0 n1 w
import android.util.Log;; R. y, \. ]2 I6 H. r& F( N  q+ `/ g" t
import android.widget.TextView;
; q& p) U1 U/ j) H! ~import wallet.Wallet;: X, p8 O" v6 Y/ Z0 F* [5 j; _/ m! H
public class MainActivity extends Activity {
5 s# x9 X6 ~3 E2 G    @Override  U1 u, U& t$ [7 B' r9 z
    protected void onCreate(Bundle savedInstanceState) {+ b; G/ `4 A6 ]
        super.onCreate(savedInstanceState);
0 N3 e' T) j  V! T/ a% N        setContentView(R.layout.activity_main);$ @, V+ T% t) n& K6 T3 j
        TextView keyTextView = (TextView) findViewById(R.id.key_textview);
5 e! L3 F4 v% q' y: A+ B        String storagePath = getFilesDir().toString();
! u' `& }& i/ R, c( G/ g) y        Log.d("storagePath", storagePath);' ~0 b& {% ^; D/ K, d5 {  R
        Wallet.initWallet(storagePath);) A0 ]! L. f  C1 h
        String keyResult = Wallet.createKey("Marshall", "123456");
7 J; c% n- D" Q  L) q        Log.d("keyResult", keyResult);" P$ N& u, H0 f
        keyTextView.setText(keyResult);+ ?) ?, N* ^8 M) ]  n: {
    }
3 H4 M1 W. ~: S+ E2 ]}* p( y% L# r6 r+ k6 F/ Q
iOS5 l7 O/ v2 x0 x2 @) y' w
通过项目target的Linked frameworks and libraries把wallet.framework添加到项目,可以在iOS项目中对SDK的API进行调用:# P: w2 H3 l' a
#import "ViewController.h"( H2 \9 X: \* ?- @  P
#import "Wallet/Wallet.h"  // Gomobile bind generated framework7 v& @; `+ k) r, K
@interface ViewController ()
% ^4 K3 y4 V/ ~9 k0 y( j@end  B( _) J0 ?' x, l, [* ^
@implementation ViewController
1 K3 {! @4 h3 {+ T! ^7 F2 d@synthesize textLabel;& O7 T8 @# }6 U0 _$ _; X
- (void)loadView {7 o3 E' M/ \' R6 D0 S( `1 X5 ^
    [super loadView];# ]$ v2 _2 i* L8 Q5 a: ?
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];/ ?. A& i# H3 w5 s8 c7 s# K! X
    WalletInitWallet(docPath);
! R( w7 M0 u- l: e) Y# ]    textLabel.text = WalletCreateKey(@"kevin",@"123456");0 s8 u: r3 G5 W9 a6 r' _
}
4 v+ G. C& f3 S) e9 G9 I/ Q@end
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

yuan081608 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    2