Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

Bytom移动端钱包SDK开发基础

yuan081608
2351 0 0
比原项目仓库:
5 B# F0 P+ S3 F+ K% ]- EGithub地址:https://github.com/Bytom/bytom/ w& E0 g+ S( e( A
Gitee地址:https://gitee.com/BytomBlockchain/bytom4 Z+ r) {3 K7 o: B
Bytom-Mobile-Wallet-SDK 是从bytom源码中抽离出的钱包层代码,并且对钱包层代码进行了改造。使用gomobile可以将代码 编译成Android和iOS平台可用的SDK,使用编译后的Android和iOS钱包SDK可以在移动端实现创建bytom密钥、账户、地址和交易签名功能。; B/ B1 K# `8 ?
Bytom-Mobile-Wallet-SDK源码简介" B" N6 u; R, }, Q* h
SDK源码放在项目的sdk文件夹中,android和ios文件夹是使用SDK的demo项目,bind.go 中首字母大写可以外部调用的函数会作为提供给Android和iOS调用的API。bytom创建的密钥对会存储在磁盘单独的文件中,而且对私钥进行了加密,账户地址数据是存储在go实现的leveldb中,所以Android和iOS平台也需要提供数据存储的路径。
" O7 ^1 V8 H6 ]- kfunc InitWallet(storagePath string) {- [1 h* k: S, g: G& q% s
    hsm := pseudohsm.New(storagePath)
+ u( m$ y6 u; v4 v8 ]    walletDB := db.NewDB("wallet", "leveldb", storagePath)
, l; Q0 b, Z7 s+ V% f+ H5 O    accounts := account.NewManager(walletDB)
  t* \+ \3 L" F( z8 G4 S    assets := asset.NewRegistry(walletDB); R+ J) R- q- S, z8 R8 z7 Q
    wallet := aWallet.NewWallet(walletDB, accounts, assets, hsm)
' U# k' ^: t6 m+ c: \6 K9 I; ~    api = aApi.API{Wallet: wallet}$ p$ ^4 U, [! T# V# K9 P2 J
}
. O) [9 ?/ e7 H  [4 ?# MAndroid和iOS平台调用其他钱包API的之前需要先调用InitWallet这个API,参数是磁盘上的绝对路径,InitWallet会对整个钱包进行一个初始化, 其中最重要是初始化leveldb的存储。其他的CreateKey、CreateAccount、CreateAccountReceiver是创建密钥、账户、地址等API,RestoreWallet API能够对钱包所有账户地址资产进行备份导出json格式的数据。9 {3 j; v7 E6 Q/ f3 Y
Bytom-Mobile-Wallet-SDK的编译
4 c9 r, k2 Q$ w% V0 n0 v& [- mSDK代码的编译首先需要正确的安装golang和gomobile,golang需要1.7以上版本。; ^! N. n/ H6 O+ J4 U/ f. y7 D0 m4 c
Android平台需要安装JDK、Android SDK、Android NDK,并且需要将Android SDK的platform-tools、ndk-bundle 添加到PATH系统环境变量中。iOS平台编译环境配置相对比较简单只需要安装Xcode就可以了。5 {- X( Q2 B: f( y' r1 D! k
Clone项目到本地$GOPATH/src下:) |( o  R, J* D. V9 |$ `; I  {- ?
git clone https://github.com/Bytom-Community/Bytom-Mobile-Wallet-SDK $GOPATH/src/github.com/bytom-community/mobile' C0 e' B) g+ n0 _/ {' r! L8 E
Android
: F) J: ?3 i, x, _2 w, |gomobile init -ndk ~/path/to/your/ndk
7 @& g$ i9 K  R+ A" J; J4 m* {cd $GOPATH/src/github.com/bytom-community/mobile# x0 r/ J# Q& H9 N& ~- a) p; @
gomobile bind -target=android github.com/bytom-community/mobile/sdk/
/ [7 u  ~% k  M7 Q. v6 x如果需要减小SDK的体积给gomobile bind指令加上-ldflags=-s参数:
( ^' e8 P0 B7 J% Z9 u  ugomobile bind -target=android -ldflags=-s github.com/bytom-community/mobile/sdk/
8 q4 K- B. t% f0 k2 W执行指令后会在mobile文件夹生成wallet.aar和wallet-sources.jar文件。7 K* S! f- F7 T+ A% v* B
iOS4 z- N6 y0 z8 d, y* z
cd $GOPATH/src/github.com/bytom-community/mobile
, ~6 [; s- l9 d+ F% B+ x* d7 ogomobile bind -target=ios github.com/bytom-community/mobile/sdk/
7 R0 _9 Z+ a4 m( y如果需要减小SDK的体积给gomobile bind指令加上-ldflags=-w参数:, a( ~- |- V6 z3 u$ r
$ gomobile bind -target=ios -ldflags=-w github.com/bytom-community/mobile/sdk/+ x: W2 R( ]! S% P7 S6 m! w* Q
执行指令后会在mobile文件夹生成wallet.framework文件。
7 V; {2 X9 Z& M由于gomobile现在没有支持bitcode,所以生成的iOS SDK也不支持bitcode。' ?$ |& ]$ h5 i" ~# z( n% o# m
Bytom-Mobile-Wallet-SDK的使用- ]% d7 A+ _0 }! a+ T+ S' N" i
Android
0 q  L$ `2 \" p2 Z拷贝wallet.aar和wallet-sources.ja到Android项目的app的libs文件夹下,并在app module中的build.gradle文件中添加:3 \3 |0 O6 M: l
android {
0 {( N: {# _& u: e. [% o    repositories {8 O% e1 u- h4 x! |: R- D
        flatDir { dirs 'libs' }
  A: T2 u' `! S4 `5 L  u    }& F' \0 W; [0 O" F; B7 T- i
}
! l6 J  u, L6 U7 s5 Gdependencies {
0 S6 b6 F2 K' N  G# b( V    implementation fileTree(include: ['*.jar'], dir: 'libs')
- z% T+ w9 A1 e    implementation(name: 'wallet', ext: 'aar')
3 b9 B' W& l3 b2 t( u3 H}/ Y6 x: k6 P' e: n1 t+ _
sync project后可以在Android项目中对SDK的API进行调用:
' \( Z0 _/ }- ?* S  cpackage io.bytom.community;2 t' L1 [2 ]0 K/ u# B" F1 \7 e
import android.app.Activity;
: v) {/ S1 ^3 w" S( S. p. Simport android.os.Bundle;, c: x8 V( |: ~
import android.util.Log;8 R) H+ R- ^2 ~
import android.widget.TextView;) i, m3 _4 [4 y' _0 `$ \. m/ B2 ?
import wallet.Wallet;3 h  @$ Z0 f& u1 }' j  m6 T
public class MainActivity extends Activity {
1 V$ [! w3 N8 ^: }" T/ I    @Override
, E7 M/ R* Z. C' H, [    protected void onCreate(Bundle savedInstanceState) {+ I$ u6 ^' X. E- s& u1 B* b
        super.onCreate(savedInstanceState);/ i, N2 E( Q# k: t% B/ A! l
        setContentView(R.layout.activity_main);  p- K* {3 a% Y4 \( k7 A0 m) I2 P
        TextView keyTextView = (TextView) findViewById(R.id.key_textview);, n! N0 ]( C" F0 C. h0 U/ @$ g# e
        String storagePath = getFilesDir().toString();
  i! o: d0 p: m; u/ G) a        Log.d("storagePath", storagePath);( ~. V# K2 G9 A$ ]$ o. ?% E" k
        Wallet.initWallet(storagePath);9 B* }' [  G9 X
        String keyResult = Wallet.createKey("Marshall", "123456");8 V; a( A( H, f: l( r- O9 b
        Log.d("keyResult", keyResult);
. W. D( L1 L# H, I& }2 _9 t8 v        keyTextView.setText(keyResult);! G- U& T3 T: m% f5 _$ z: M
    }
3 S! ~% q, \% c5 n1 a2 D3 ?}
5 ]( U+ Y2 L5 p$ K" R# GiOS1 Z2 r$ v0 y1 O* A
通过项目target的Linked frameworks and libraries把wallet.framework添加到项目,可以在iOS项目中对SDK的API进行调用:4 o; t* t; N5 v: {  r8 q5 t/ t+ v6 e
#import "ViewController.h"$ E* i& Q2 ]6 G0 ^/ ]) A& r- {$ D
#import "Wallet/Wallet.h"  // Gomobile bind generated framework
$ t6 I8 Z: P3 Y) n- E@interface ViewController ()! D( y& G: j. }/ R- ?: _9 X% x0 h) ^
@end8 L, y; I" H% t4 E' n+ Q0 t& v
@implementation ViewController) J8 t1 O3 o" D) \) H$ g$ f% o
@synthesize textLabel;
) u! p/ w! @* W, c8 W8 T- }1 m- (void)loadView {7 [5 I" w, w3 x! x3 P( U
    [super loadView];
% e4 l8 B* [5 Q    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
! G' O! X8 Z8 [# w. f    WalletInitWallet(docPath);
- K/ x8 n) h4 i3 I, R( G6 A' X    textLabel.text = WalletCreateKey(@"kevin",@"123456");
( v3 k% k6 |$ |: |/ E& h}
& e5 }# |/ z6 }! l  x% p) |) |@end
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

yuan081608 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    2