Skip to content

Instantly share code, notes, and snippets.

@XuNeal
Created June 16, 2021 01:38
Show Gist options
  • Save XuNeal/47da3beaaf704c07eb10a373cbce3ccc to your computer and use it in GitHub Desktop.
Save XuNeal/47da3beaaf704c07eb10a373cbce3ccc to your computer and use it in GitHub Desktop.
package com.consenlabs.test.f17;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.consenlabs.test.f17.api.Api;
import com.consenlabs.test.f17.api.Params;
import com.google.protobuf.AbstractMessageLite;
import com.google.protobuf.Any;
import com.google.protobuf.InvalidProtocolBufferException;
import java.io.File;
public class MainActivity extends AppCompatActivity {
static {
// System.loadLibrary("secp256k1");
System.loadLibrary("tcx");
}
boolean isRunning = false;
Handler uiHandler = null;
long times = 0L;
TextView tvMsg = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uiHandler = new Handler(MainActivity.this.getMainLooper());
tvMsg = findViewById(R.id.tv_msg);
AppCompatButton btn = findViewById(R.id.btn_start);
btn.setOnClickListener(view -> {
isRunning = true;
new Thread(new Runnable() {
@Override
public void run() {
while (isRunning) {
times++;
String address = importFILWallet();
if (address.equals("wrong address")) {
new AlertDialog.Builder(MainActivity.this).setMessage("Find the aei PK").show();
return;
}
runOnUiThread(() -> tvMsg.setText(String.format("run times: %d\n%s", times, address)));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).run();
});
AppCompatButton stopBtn = findViewById(R.id.btn_stop);
stopBtn.setOnClickListener(view -> {
isRunning = false;
times = 0;
});
initTcx();
}
void initTcx() {
TcxApiStruct.InitTokenCoreXParam param = TcxApiStruct.InitTokenCoreXParam.newBuilder()
.setFileDir(getKeystorePath().getAbsolutePath())
.setXpubCommonKey("xpub key")
.setXpubCommonIv("xpub iv")
.setIsDebug(true)
.build();
callTcxWrap(param, "init_token_core_x");
}
String importFILWallet() {
Log.d("tcx", "------------------------------------------------");
Log.d("tcx", "run times: " + times);
try {
Params.HdStoreImportParam param = Params.HdStoreImportParam.newBuilder()
.setMnemonic("user mnemonic")
.setName("FIL Test")
.setPassword("22222222")
.setSource("MNEMONIC")
.setPasswordHint("imtoken")
.setOverwrite(true)
.build();
String walletRsp = callTcxWrap(param, "hd_store_import");
Params.WalletResult wallet = Params.WalletResult.parseFrom(NumericUtil.hexToBytes(walletRsp));
Params.KeystoreCommonDeriveParam.Derivation derivation = Params.KeystoreCommonDeriveParam.Derivation.newBuilder()
.setChainType("FILECOIN")
.setPath("m/44'/461'/0'/0/0")
.setSegWit("")
.setChainId("")
.setNetwork("MAINNET")
.setCurve("SECP256k1")
.build();
Params.KeystoreCommonDeriveParam deriveParam = Params.KeystoreCommonDeriveParam.newBuilder()
.setPassword("22222222")
.setId(wallet.getId())
.addDerivations(derivation)
.build();
String derivationRsp = callTcxWrap(deriveParam, "keystore_common_derive");
Params.AccountsResponse accountResponse = Params.AccountsResponse.parseFrom(NumericUtil.hexToBytes(derivationRsp));
String address = accountResponse.getAccounts(0).getAddress();
Log.d("tcx", String.format("address is %s", address));
return address;
} catch (InvalidProtocolBufferException e) {
e.printStackTrace();
}
return "";
}
String callTcxWrap(AbstractMessageLite param, String method) {
Any anyWrapper = Any.newBuilder()
.setTypeUrl("imtoken." + method)
.setValue(param.toByteString())
.build();
TcxApiStruct.TcxAction action = TcxApiStruct.TcxAction.newBuilder()
.setMethod(method)
.setParam(anyWrapper)
.build();
String hex = NumericUtil.bytesToHex(action.toByteArray());
return new TcxAPI().callTcxApi(hex);
}
File getKeystorePath() {
File directory = new File(this.getFilesDir(), "wallets");
if (!directory.exists()) {
directory.mkdirs();
}
return directory;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment