Skip to content

pmkoo/CoboSDK-Android

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CoboSDK-Android

CoboSDK帮助开发者通过Cobo钱包进行ethereum交易的签名和发送。 DApp开发者可以使用CoboSDK获取用户的ethereum账户地址,完成消息的签名和校验,发起交易、调用智能合约以及广播签名后的交易数据。从而省去了开发者自行实现用户私钥管理和钱包功能的工作。

其他语言:English

接入CoboSDK

接入CoboSDK需要如下几个步骤:

添加依赖

打开build.gradle文件,在dependencies中添加:

implementation 'com.cobo:sdk:1.0'

初始化CoboSDK

调用CoboSDK的initialize方法对SDK进行初始化:

CoboSDK.getInstance().initialize(this);

接收返回结果

  1. 添加CBEntryActivity

在你的应用package下新建一个名为coboapi的package,并在coboapi包下新建一个名为CBEntryActivity的Activity。

添加Entry Activity

在AndroidManifest.xml文件中为CBEntryActivity加上android:exported="true"android:launchMode="singleTask"属性,例如:

<activity
    android:name=".coboapi.CBEntryActivity"
    android:exported="true"
    android:launchMode="singleTask" />
  1. 实现CBEntryActivityonCreate方法
@Override  
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cbentry);

    Intent intent = getIntent();
    CoboSDK.getInstance().handleIntent(intent);
    finish();
}

使用SDK

签名消息

final String message = "Hello Cobo!";
CoboSDK.getInstance().signMessage(message, new SignMessageAction.ResultHandler() {
    @Override
    public void onResult(SignMessageResult result) {
        Log.i("DEMO", "result: " + result.address);
        boolean match = CoboSDK.getInstance().verifyMessage(result.address, result.signature, message);
        Log.i("DEMO", "match: " + match);
    }

    @Override
    public void onError(int code, String message) {
        Log.e("DEMO", "error: " + code + " message: " + message);
    }
}, this);

发送交易

BigInteger gasPrice = new BigInteger("1000000000"); // 1 Gwei
BigInteger gasLimit = new BigInteger("21000");
BigInteger value = new BigInteger("1000000000000000000"); // 1 ETH
String from = <replace with address in sign message result>
String to = <replace with address to transfer to>
RawTransaction tx = RawTransaction.createTransaction(null, gasPrice, gasLimit, to, value, null);
CoboSDK.getInstance().sendTransaction(tx, from, new SignTransactionAction.ResultHandler() {
    @Override
    public void onResult(SignTransactionResult result) {
        Log.i("DEMO", "result: " + result.hash);
    }

    @Override
    public void onError(int code, String message) {
        Log.e("DEMO", "error: " + code + " message: " + message);
    }
}, this);

更多功能使用请参考Example工程。

Packages

 
 
 

Languages

  • Java 100.0%