8.3 Android-SDK

适用范围

该文档适用于Android cocos 钱包开发.
SDK适用于Android4.0 (API Level 14)及以上版本,SDK目前测试版编译版本选择27.
注意:
Android P(版本27以上) 对网络请求http限制,SDK中有使用http请求

类库引用说明

1.将bcx_sdk.aar复制到项目目录下,在Project Structure里选择New Moudule(注意不是添加Module dependency), 选择Import JAR/arr Package,点击Next,选择arr文件所在的路径, 点击Finish,选择Module dependency里将bcx_sdkMoudle添加到项目里即可
这种方式不会将第三方依赖编译进aar文件,需要添加以下依赖:

// implenment websocket
    implementation 'com.neovisionaries:nv-websocket-client:1.30'
    // implenment bitcoinj
    implementation 'org.bitcoinj:bitcoinj-core:0.14.7'
    implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
    implementation group: "org.tukaani", name: "xz", version: "1.6"
    implementation 'com.squareup.okhttp3:okhttp:3.12.1'
    // spongycastle
    implementation 'com.madgag.spongycastle:core:1.58.0.0'
    implementation 'com.madgag.spongycastle:prov:1.58.0.0'
    implementation 'com.madgag.spongycastle:pkix:1.54.0.0'
    implementation 'com.madgag.spongycastle:pg:1.54.0.0'
    // fasterxml
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.9.7'

2.Maven 方式引入(暂不支持)

//正常依赖
implementation 'com.cocos.android:cocos-sdk:1.0.0'

//关闭全部依赖传递-方法1
implementation 'com.cocos.android:cocos-sdk:[email protected]'

//关闭全部依赖传递-方法2
implementation('com.cocos.android:cocos-sdk:1.0.0') {
        transitive = false
}

注意:

1.避免创建同名数据库:cocos_bcx_android_sdk.db;
2.ERROR:INSTALL_FAILED_NO_MATCHING_ABIS

解决方案
在app模块的build.gradle android下添加以下代码:

packagingOptions {
        exclude 'lib/x86_64/darwin/libscrypt.dylib'
        exclude 'lib/x86_64/freebsd/libscrypt.so'
        exclude 'lib/x86_64/linux/libscrypt.so'
    }

SDK 初始化 (调用其他接口前必须初始化项目 ,否则会报空指针错误)

List<String> mListNode = Arrays.asList("ws://47.93.62.96:8050", "ws://39.96.33.61:8080", "ws://39.96.29.40:8050", "ws://39.106.126.54:8050");
       String[] faucetUrl = {"http://47.93.62.96:3000/api/v1/accounts"};
       String chainId = "53b98adf376459cc29e5672075ed0c0b1672ea7dce42b0b1fe5e021c02bda640";
       String coreAsset = "COCOS";
       boolean isOpenLog = true;
       CocosBcxApiWrapper.getBcxInstance().init(this, chainId, mListNode, faucetUrl, coreAsset, isOpenLog,
               new IBcxCallBack() {
                   @Override
                   public void onReceiveValue(String value) {
                       Log.i("initBcxSdk", value);
                 }
               });

SDK API 使用说明

回调数据为统一string 类型;
API 调用对象为单列对象;

下面给出API调用示例:

/**
    * account model  create account
    *
    * @param strAccountName accountName
    * @param strPassword    Password
    * @param isAutoLogin    true :   log in, false:just register
    * @param callBack
    */           
CocosBcxApiWrapper.getBcxInstance().create_password_account("*****", "*****", true, new IBcxCallBack() {
               @Override
               public void onReceiveValue(String value) {
                   Log.i("createaccwithpassword", value);
                   ToastUtils.showShort(value);
               }
           });

状态码

状态码说明

codemessage说明
300Chain sync error, please check your system clock链同步错误,请检查您的系统时钟
301RPC connection failed. Please check your network连接RPC失败,请检查你的网络
1操作成功
0failed操作失败,返回错误状态描述不固定,可直接提示res.message或统一提示为操作失败
101Parameter is missing参数缺失
1011Parameter error参数错误
102The network is busy, please check your network connection网络繁忙,请检查你的网络连接
103Please enter the correct account name(/^a-z{4,63}/$)请输入正确的账户名(正则/^a-z{4,63}/$)
104XX not foundXX 不存在
105wrong password密码错误
106The account is already unlocked账户已经处于解锁状态
107Please import the private key请先导入私钥
108User name or password error (please confirm that your account is registered in account mode, and the account registered in wallet mode cannot be logged in using account mode)用户名或密码错误(请确认你的账户是通过账户模式注册的,钱包模式注册的账户不能使用账户模式登录)
109Please enter the correct private key请输入正确的私钥
110The private key has no account information该私钥没有对应的账户信息
111Please login first请先登录
112Must have owner permission to change the password, please confirm that you imported the ownerPrivateKey必须拥有owner权限才可以进行密码修改,请确认你导入的是ownerPrivateKey
113Please enter the correct original/temporary password请输入正确的原始密码/临时密码
114Account is locked or not logged in.帐户被锁定或未登录
115There is no asset XX on block chain区块链上不存在资产 XX
116Account receivable does not exist收款方账户不存在
117The current asset precision is configured as X ,and the decimal cannot exceed X当前资产精度配置为 X ,小数点不能超过 X
118Encrypt memo failed备注加密失败
119Expiry of the transaction交易过期
120Error fetching account record获取帐户记录错误
121block and transaction information cannot be found查询不到相关区块及交易信息
122Parameter blockOrTXID is incorrect参数blockOrTXID不正确
123Parameter account can not be empty参数account不能为空
124Receivables account name can not be empty收款方账户名不能为空
125Users do not own XX assets用户未拥有 XX 资产
127No reward available没有可领取的奖励
129Parameter ‘memo’ can not be emptymemo不能为空
130Please enter the correct contract name(/^a-z{4,63}$/)请输入正确的合约名称(正则/^a-z{4,63}$/)
131Parameter ‘worldView’ can not be empty世界观名称不能为空
133Parameter ‘toAccount’ can not be emptytoAccount不能为空
135Please check parameter data type请检查参数数据类型
136Parameter ‘orderId’ can not be emptyorderId不能为空
137Parameter ‘NHAssetHashOrIds’ can not be emptyNHAssetHashOrIds不能为空
138Parameter ‘url’ can not be empty接入点地址不能为空
139Node address must start with ws:// or wss://节点地址必须以 ws:// 或 wss:// 开头
140API server node address already existsAPI服务器节点地址已经存在
141Please check the data in parameter NHAssets请检查参数NHAssets中的数据
142Please check the data type of parameter NHAssets请检查参数NHAssets的数据类型
144Your current batch creation / deletion / transfer number is X , and batch operations can not exceed X您当前批量 创建/删除/转移 NH资产数量为 X ,批量操作数量不能超过 X
145XX contract not foundXX 合约不存在
146The account does not contain information about the contract账户没有该合约相关的信息
147NHAsset do not exist非同质资产不存在
148Request timeout, please try to unlock the account or login the account请求超时,请尝试解锁账户或登录账户
149This wallet has already been imported此私钥已导入过钱包
150Key import error导入私钥失败
151File saving is not supported您的浏览器不支持文件保存
152Invalid backup to download conversion无效的备份下载转换
153Please unlock your wallet first请先解锁钱包
154Please restore your wallet first请先恢复你的钱包
155Your browser may not support wallet file recovery浏览器不支持钱包文件恢复
156The wallet has been imported. Do not repeat import该钱包已经导入,请勿重复导入
157Can’t delete wallet, does not exist in index请求超时,请尝试解锁账户或登录账户
158Imported Wallet core assets can not be XX , and it should be XX导入的钱包核心资产不能为 XX ,应为 XX
159Account exists账户已存在
160You are not the creator of the Asset XX .你不是该资产的创建者
161Orders do not exist订单不存在
162The asset already exists资产已存在
163The wallet already exists. Please try importing the private key钱包已经存在,请尝试导入私钥
164worldViews do not exist世界观不存在
165There is no wallet account information on the chain链上没有该钱包账户信息
166The Wallet Chain ID does not match the current chain configuration information. The chain ID of the wallet is: XX该钱包链id与当前链配置信息不匹配,该钱包的链id为: XXX
167The current contract version ID was not found当前合约版本id没有找到 X
168This subscription does not exist当前没有订阅此项
169MethodMethod does not exist does not existAPI方法不存在

API 使用示例

1.钱包模式-创建账户

/**
     * wallet model  create account
     *
     * @param strAccountName accountName
     * @param strPassword    Password
     * @param isAutoLogin    true :   log in, false:just register
     * @param callBack
     */
  CocosBcxApiWrapper.getBcxInstance().create_wallet_account("testtest3", "111111", true, new IBcxCallBack() {
                @Override
                public void onReceiveValue(String value) {
                    Log.i("create_wallet_account", value);
                }
            });
返回数据1 :{"code":1,"data":{"account":{"active_key":"COCOS6BGnotPV3232ZJBvp5FgZuZ5cGPqNgqaSHnbSoaJLjaKmV8LLE","name":"testtest3","owner_key":"COCOS8F9BeMjVqBVakgJhTm2pQoMido4ZBksfHL6oQb1ZWXtpmpBJF5"}}}
返回数据 2:{"code":159,"message":account exist}
返回数据 3:{"code":102,"message":It doesn't connect to the server.}

2.转账

/**
     * transfer
     * @param password 密码 (临时密码/账户密码)
     * @param strFrom 转出账户
     * @param strTo 转入账户
     * @param strAmount 转账金额
     * @param strAssetSymbol 转账币种
     * @param strMemo 备注信息
     */
 CocosBcxApiWrapper.getBcxInstance().transfer("111111", "testtest3", "gnkhandsome1", "10", "COCOS", "testting", new IBcxCallBack() {
                @Override
                public void onReceiveValue(String value) {
                    Log.i("transfer", value);
                }
            });
###返回数据1 :{"code":1,"message":bf3c058914399136d384de714a3c57d2966e1513}
注: hash : bf3c058914399136d384de714a3c57d2966e1513
返回数据2: {"code":105,"message":wrong password}

3.导出私钥 (私通过钥导入登录的账户只能导出登陆时导入的私钥)

CocosBcxApiWrapper.getBcxInstance().export_private_key("gnkhandsome1", "123456", new IBcxCallBack() {
                            @Override
                            public void onReceiveValue(final String value) {
                                   Log.i("export_private_key", value);
                                
                            }
                        });

返回数据 (公钥(Key),私钥(Value)):

{"code":1,"data":{"COCOS6G55VgR94GZmELS4UHEf2eVggmhPRnWLTWgGiEmzuBKdvEwoAB":"5Hy7aVcZFyHa7UKURN22m9gB7xp4KS7Bo1dibWSVZZYAg6Br1bu","COCOS8Dw7QjWVFggYCvp9c8XbsXssqizN1MqkwPfSAVTQppQLhUcTC2":"5JgPmrWHevyH4ZzLkgZL3yAaddXE6phrKJYCfKyAJmhhjbmZyF7"},"message":""}

4.获取账户余额(参数1:用户ID, 参数2:获取余额的资产ID,为空则获取账户的所有资产余额信息)

List<Object> assetSymbolOrId = new ArrayList<>();
      // todo 默认币种类型
assetSymbolOrId.add("1.3.0");
CocosBcxApiWrapper.getBcxInstance().get_account_balances("1.2.76", unit, new IBcxCallBack() {
          @Override
          public void onReceiveValue(final String value) {
                  Log.i("get_account_balances", value);
          }
      });

返回数据:

{"id":1,"data":[{"amount":55362897,"asset_id":"1.3.0"}]}

合作项目

CocosWallet

开源地址

AndroidSdk

Updated 8 months ago


8.3 Android-SDK


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.