Bytom 移动端钱包 SDK 开发基础

2018-09-10 08:00:17 +08:00
 Bytom

比原项目仓库:

Github 地址:https://github.com/Bytom/bytom

Gitee 地址:https://gitee.com/BytomBlockchain/bytom

Bytom-Mobile-Wallet-SDK 是从 bytom 源码中抽离出的钱包层代码,并且对钱包层代码进行了改造。使用gomobile可以将代码 编译成 Android 和 iOS 平台可用的 SDK,使用编译后的 Android 和 iOS 钱包 SDK 可以在移动端实现创建 bytom 密钥、账户、地址和交易签名功能。

Bytom-Mobile-Wallet-SDK 源码简介

SDK 源码放在项目的 sdk 文件夹中,android 和 ios 文件夹是使用 SDK 的 demo 项目,bind.go 中首字母大写可以外部调用的函数会作为提供给 Android 和 iOS 调用的 API。bytom 创建的密钥对会存储在磁盘单独的文件中,而且对私钥进行了加密,账户地址数据是存储在 go 实现的 leveldb 中,所以 Android 和 iOS 平台也需要提供数据存储的路径。

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 格式的数据。

Bytom-Mobile-Wallet-SDK 的编译

SDK 代码的编译首先需要正确的安装 golang 和 gomobile,golang 需要 1.7 以上版本。
Android 平台需要安装 JDK、Android SDK、Android NDK,并且需要将 Android SDK 的 platform-tools、ndk-bundle 添加到 PATH 系统环境变量中。iOS 平台编译环境配置相对比较简单只需要安装 Xcode 就可以了。
Clone 项目到本地$GOPATH/src 下:

 git clone https://github.com/Bytom-Community/Bytom-Mobile-Wallet-SDK $GOPATH/src/github.com/bytom-community/mobile

Android

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 参数:

gomobile bind -target=android -ldflags=-s github.com/bytom-community/mobile/sdk/

执行指令后会在 mobile 文件夹生成 wallet.aar 和 wallet-sources.jar 文件。

iOS

cd $GOPATH/src/github.com/bytom-community/mobile
gomobile bind -target=ios github.com/bytom-community/mobile/sdk/

如果需要减小 SDK 的体积给 gomobile bind 指令加上-ldflags=-w 参数:

$ gomobile bind -target=ios -ldflags=-w github.com/bytom-community/mobile/sdk/

执行指令后会在 mobile 文件夹生成 wallet.framework 文件。
由于 gomobile 现在没有支持 bitcode,所以生成的 iOS SDK 也不支持 bitcode。

Bytom-Mobile-Wallet-SDK 的使用

Android

拷贝 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);
    }
}

iOS

通过项目 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
1336 次点击
所在节点    区块链
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/487735

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX