V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Bytom
V2EX  ›  区块链

Bytom 移动端钱包 SDK 开发基础

  •  
  •   Bytom · 2018-09-10 08:00:17 +08:00 · 1290 次点击
    这是一个创建于 2049 天前的主题,其中的信息可能已经有所发展或是发生改变。

    比原项目仓库:

    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
    
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2945 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 129ms · UTC 08:35 · PVG 16:35 · LAX 01:35 · JFK 04:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.