前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Bytom移动端钱包SDK开发基础

Bytom移动端钱包SDK开发基础

原创
作者头像
比原链Bytom
发布2018-09-10 08:01:14
9540
发布2018-09-10 08:01:14
举报
文章被收录于专栏:比原链比原链

比原项目仓库:

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平台也需要提供数据存储的路径。

代码语言:txt
复制
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下:

代码语言:txt
复制
 git clone https://github.com/Bytom-Community/Bytom-Mobile-Wallet-SDK $GOPATH/src/github.com/bytom-community/mobile

Android

代码语言:txt
复制
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参数:

代码语言:txt
复制
gomobile bind -target=android -ldflags=-s github.com/bytom-community/mobile/sdk/

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

iOS

代码语言:txt
复制
cd $GOPATH/src/github.com/bytom-community/mobile
gomobile bind -target=ios github.com/bytom-community/mobile/sdk/

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

代码语言:txt
复制
$ 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文件中添加:

代码语言:txt
复制
android {
    repositories {
        flatDir { dirs 'libs' }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation(name: 'wallet', ext: 'aar')
}

sync project后可以在Android项目中对SDK的API进行调用:

代码语言:txt
复制
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进行调用:

代码语言:txt
复制
#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

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Bytom-Mobile-Wallet-SDK源码简介
  • Bytom-Mobile-Wallet-SDK的编译
    • Android
      • iOS
      • Bytom-Mobile-Wallet-SDK的使用
        • Android
          • iOS
          相关产品与服务
          对象存储
          对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档