web3j钱包篇-Android

1:问题

导入jar包时,因为导入

implementation 'io.github.novacrypto:BIP39:0.1.9'

一直编译不通过,后来添加

 mavenCentral()

成功。

2:jar

implementation 'org.web3j:core:3.3.1-android'
implementation 'com.android.support:multidex:1.0.0'
implementation 'io.github.novacrypto:BIP39:0.1.9'

3:生成方式

 File path=getCacheDir();
//第一种方式
 ECKeyPair ecKeyPair= Keys.createEcKeyPair();
//keystore文件名
String fileName=WalletUtils.generateWalletFile(pwd,ecKeyPair,path,false);
 //获取keystore内容
File keystore=new File(path+"/"+fileName);
Log.e("+++","fileName:"+fileName+"\n PrivateKey: "+ Numeric.encodeQuantity(ecKeyPair.getPrivateKey())+"\n publickey:"+Numeric.encodeQuantity(ecKeyPair.getPublicKey()));




//第二种方式 助计词

 StringBuilder sb=new StringBuilder();
byte[] entropy=new byte[Words.TWELVE.byteLength()];
new SecureRandom().nextBytes(entropy);

new MnemonicGenerator(English.INSTANCE).createMnemonic(entropy,sb::append);
//助计词
String mnemonic=sb.toString();

byte[] seed= MnemonicUtils.generateSeed(mnemonic,pwd);
ECKeyPair ecKeyPair=ECKeyPair.create(sha256(seed));
String  fileName=WalletUtils.generateWalletFile(pwd,ecKeyPair,path,false);

Bip39Wallet bip39Wallet=new Bip39Wallet(fileName,mnemonic);

Credentials credentials=    WalletUtils.loadBip39Credentials(pwd,bip39Wallet.getMnemonic());



4:导入钱包

//密码。keystore 导入钱包
 ObjectMapper objectMapper=new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
File keystore=new File(path+"/"+fileName);
WalletFile walletFile=  objectMapper.readValue(keystore,WalletFile.class);

ECKeyPair ecKeyPair1= Wallet.decrypt(pwd,walletFile);
WalletFile generateWalletFile=Wallet.createLight(pwd,ecKeyPair);

 

你可能感兴趣的:(区块链)