MiniWallet only supports the EOS network. With MiniWallet, developers can do some special actions without pulling up TokenPocket. This way can provide a better user experience.
TPManager.getInstance().initSDK(this);
//Set the mainnet and nodes
TPManager.getInstance().setBlockChain(this, NetTypeEnum.EOS_MAINNET, "http://openapi.eos.ren");
//Set the miniwallet address
TPManager.getInstance().setAppPluginNode(this, "http://eosinfo.mytokenpocket.vip");
//Set password to protect miniwallet data, here //using 12345 as an example
TPManager.getInstance().setSeed(this, "123456");
Authorization This step will customize the permission group and link the operations that need to use miniwallet to the permission group
AuthorizePerm authorizePerm = new AuthorizePerm();
//set account for the permission
authorizePerm.setAccount("ljxlzdh54321");
authorizePerm.setPermExisted(false);
//permission name
authorizePerm.setPerm("testtransfer");
//actions to be linked
LinkAction linkAction = new LinkAction();
//Here we take tpt transfer as an example. link this action to testtransfer permission
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) {
//The hash of this operation will be returned after the operation is successful,
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();
}
});
After the authorization is successful, you can use the key corresponding to the custom permission group to sign related operations
Attention should be paid to this step: the permission of authorization should be filled with the name of the defined permission group.
Send transaction
Before sending a transaction, check whether the permission in the transaction data has been linked first. If not, replace the permission in the authorization of the transaction data built in the previous step with active. then pull up TokenPocket to finish this transaction. If the link has been successful, you do not need to pull up the wallet and complete the operation directly in the Dapp.
private void pushTx() {
final Transaction transaction = getTxData();
//First, determine whether the operation permission is already in the account throught the interface. You can refer to account authorization about the process of this link.
TPManager.getInstance().isPermLinkAction(this, transaction, new TPListener() {
@Override
public void onSuccess(String data) {
//If this operation has been authorized by miniwallet, modify the parameters of the operation to the permission group defined by the developer. In the demo instance,Our custom permission group is testtransfer, and details can be found in Authorization AuthActivity.java
replacePerm(transaction, transaction.getPerm());
}
@Override
public void onError(String data) {
//If the permission group of the current operation is not linked to the account, the miniwallet cannot complete the current operation, and still needs to pull up the wallet to authorize.
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());
//After replacing the permission of the operation with a custom permission group, the operation can be completed directly in the miniwallet without pulling up the tp wallet.
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();
}
});
}
Method description
initSDK() initializes the SDK
isPermLinkAction() Checks if the permission is linked
Permission link failed, please check if the account has enough resources
After linking successfully, we still need to pull up TokenPocket. First, ensure the operating parameters are configured correctly. In addition, for some operations involving the security of user assets, even if the configuration is correct, it is still necessary to pull up TokenPocket to confirm.