dependencies {
compile(name:'wallet-sdk-release', ext:'aar')
}
# tokenpocket sdk
-dontwarn com.tokenpocket.opensdk.**
-keep class com.tokenpocket.opensdk.**{*;}
-keep interface com.tokenpocket.opensdk.**{*;}
TPManager.getInstance().initSDK(this);
//设置主网和节点
TPManager.getInstance().setBlockChain(this, NetTypeEnum.EOS_MAINNET, "http://openapi.eos.ren");
//设置miniwallet插件地址
TPManager.getInstance().setAppPluginNode(this, "http://eosinfo.mytokenpocket.vip");
//设置一个密码,用来保护miniwallet数据,这里只是为了展示需要设置为12345
TPManager.getInstance().setSeed(this, "123456");
AuthorizePerm authorizePerm = new AuthorizePerm();
//添加权限组的账号
authorizePerm.setAccount("ljxlzdh54321");
authorizePerm.setPermExisted(false);
//要添加的权限组的名字
authorizePerm.setPerm("testtransfer");
//和权限组关联的操作,只有把操作关联到添加的权限组上面了,才能利用自定义的权限进行相应的操作
LinkAction linkAction = new LinkAction();
//这里定义EOS上TPT的转账操作关联到自定义的testtransfer权限组
linkAction.setContract("eosiotptoken");
linkAction.setAction("transfer");
linkActions.add(linkAction);
authorizePerm.setActions(linkActions);
authorizePerm.setDappIcon("https://newdex.io/static/logoicon.png");
authorizePerm.setDappName("Test");
authorizePerm.setSelectAll(true);
TPManager.getInstance().auth(AuthActivity.this, authorizePerm, new TPListener() {
@Override
public void onSuccess(String data) {
//操作成功后,会返回本次操作的hash
Toast.makeText(AuthActivity.this, data, Toast.LENGTH_SHORT).show();
}
@Override
public void onError(String data) {
Toast.makeText(AuthActivity.this, data, Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel(String data) {
Toast.makeText(AuthActivity.this, data, Toast.LENGTH_SHORT).show();
}
});
String actions = "[\n" +
" {\n" +
" \"account\": \"eosiotptoken\",\n" +
" \"name\": \"transfer\",\n" +
" \"authorization\": [\n" +
" {\n" +
" \"actor\": \"ljxlzdh54321\",\n" +
" \"permission\": \"testtransfer\"\n" +
" }\n" +
" ],\n" +
" \"data\": {\n" +
" \"from\": \"ljxlzdh54321\",\n" +
" \"memo\": \"ddd\",\n" +
" \"quantity\": \"0.0100 TPT\",\n" +
" \"to\": \"clementtes51\"\n" +
" }\n" +
" }\n" +
" ]";
发送交易前,首先检查交易数据中的权限是否已经link,如果没有link,则需要将上一步构造的交易数据中的authorization中permission替换成active,以便拉起钱包授权操作。如果已经link成功,则无需拉起钱包,直接在dapp中完成操作。
private void pushTx() {
final Transaction transaction = getTxData();
//首先通过接口判断,本次执行的操作需要的权限,是否已经在账号中,这个链接的过程可以参考账号授权
TPManager.getInstance().isPermLinkAction(this, transaction, new TPListener() {
@Override
public void onSuccess(String data) {
//如果本次操作已经通过miniwallet授权,则将操作的参数修改为开发者自己定义的权限组,在demo实例中,
//我们自定义的权限组是testtransfer,详情可参加授权AuthActivity.java
replacePerm(transaction, transaction.getPerm());
}
@Override
public void onError(String data) {
//如果当前操作的权限组没有link到账号,则miniwallet无法完成当前操作,仍然需要拉起钱包授权
replacePerm(transaction, "active");
}
@Override
public void onCancel(String data) {
}
});
}
/**
*
* @param transaction
* @param permission
*/
private void replacePerm(Transaction transaction, String permission) {
Json actions = new Json(transaction.getActions());
for (int i = 0; i < actions.getLength(); i++) {
Json authorization = actions.getObject(i, "{}").getArray("authorization", "[]")
.getObject(0, "{}");
authorization.putString("permission", permission);
}
transaction.setActions(actions.toString());
//将操作的permission替换成自定义的权限组后,就可以直接在miniwallet中完成操作,而不需要拉起tp 钱包授权
realPushTx(transaction);
}
private void realPushTx(Transaction transaction) {
TPManager.getInstance().pushTransaction(PushTxActivity.this, transaction, new TPListener() {
@Override
public void onSuccess(String data) {
Toast.makeText(PushTxActivity.this, data, Toast.LENGTH_LONG).show();
}
@Override
public void onError(String data) {
Toast.makeText(PushTxActivity.this, data, Toast.LENGTH_LONG).show();
}
@Override
public void onCancel(String data) {
Toast.makeText(PushTxActivity.this, data, Toast.LENGTH_LONG).show();
}
});
}