比原项目仓库:android
Github地址:https://github.com/Bytom/bytomios
Gitee地址:https://gitee.com/BytomBlockchain/bytomgit
Bytom-Mobile-Wallet-SDK 是从bytom源码中抽离出的钱包层代码,而且对钱包层代码进行了改造。使用gomobile能够将代码 编译成Android和iOS平台可用的SDK,使用编译后的Android和iOS钱包SDK能够在移动端实现建立bytom密钥、帐户、地址和交易签名功能。github
SDK源码放在项目的sdk文件夹中,android和ios文件夹是使用SDK的demo项目,bind.go 中首字母大写能够外部调用的函数会做为提供给Android和iOS调用的API。bytom建立的密钥对会存储在磁盘单独的文件中,并且对私钥进行了加密,帐户地址数据是存储在go实现的leveldb中,因此Android和iOS平台也须要提供数据存储的路径。golang
func InitWallet(storagePath string) { hsm := pseudohsm.New(storagePath) walletDB := db.NewDB("wallet", "leveldb", storagePath) accounts := account.NewManager(walletDB) assets := asset.NewRegistry(walletDB) wallet := aWallet.NewWallet(walletDB, accounts, assets, hsm) api = aApi.API{Wallet: wallet} }
Android和iOS平台调用其余钱包API的以前须要先调用InitWallet这个API,参数是磁盘上的绝对路径,InitWallet会对整个钱包进行一个初始化, 其中最重要是初始化leveldb的存储。其余的CreateKey、CreateAccount、CreateAccountReceiver是建立密钥、帐户、地址等API,RestoreWallet API可以对钱包全部帐户地址资产进行备份导出json格式的数据。shell
SDK代码的编译首先须要正确的安装golang和gomobile,golang须要1.7以上版本。
Android平台须要安装JDK、Android SDK、Android NDK,而且须要将Android SDK的platform-tools、ndk-bundle 添加到PATH系统环境变量中。iOS平台编译环境配置相对比较简单只须要安装Xcode就能够了。
Clone项目到本地$GOPATH/src下:json
git clone https://github.com/Bytom-Community/Bytom-Mobile-Wallet-SDK $GOPATH/src/github.com/bytom-community/mobile
gomobile init -ndk ~/path/to/your/ndk cd $GOPATH/src/github.com/bytom-community/mobile gomobile bind -target=android github.com/bytom-community/mobile/sdk/
若是须要减少SDK的体积给gomobile bind指令加上-ldflags=-s参数:api
gomobile bind -target=android -ldflags=-s github.com/bytom-community/mobile/sdk/
执行指令后会在mobile文件夹生成wallet.aar和wallet-sources.jar文件。app
cd $GOPATH/src/github.com/bytom-community/mobile gomobile bind -target=ios github.com/bytom-community/mobile/sdk/
若是须要减少SDK的体积给gomobile bind指令加上-ldflags=-w参数:ide
$ gomobile bind -target=ios -ldflags=-w github.com/bytom-community/mobile/sdk/
执行指令后会在mobile文件夹生成wallet.framework文件。
因为gomobile如今没有支持bitcode,因此生成的iOS SDK也不支持bitcode。
拷贝wallet.aar和wallet-sources.ja到Android项目的app的libs文件夹下,并在app module中的build.gradle文件中添加:
android { repositories { flatDir { dirs 'libs' } } } dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') implementation(name: 'wallet', ext: 'aar') }
sync project后能够在Android项目中对SDK的API进行调用:
package io.bytom.community; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.TextView; import wallet.Wallet; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView keyTextView = (TextView) findViewById(R.id.key_textview); String storagePath = getFilesDir().toString(); Log.d("storagePath", storagePath); Wallet.initWallet(storagePath); String keyResult = Wallet.createKey("Marshall", "123456"); Log.d("keyResult", keyResult); keyTextView.setText(keyResult); } }
经过项目target的Linked frameworks and libraries把wallet.framework添加到项目,能够在iOS项目中对SDK的API进行调用:
#import "ViewController.h" #import "Wallet/Wallet.h" // Gomobile bind generated framework @interface ViewController () @end @implementation ViewController @synthesize textLabel; - (void)loadView { [super loadView]; NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; WalletInitWallet(docPath); textLabel.text = WalletCreateKey(@"kevin",@"123456"); } @end