以太坊3(助記詞、賬戶匯入、賬戶匯出)
阿新 • • 發佈:2018-12-31
生成助記詞
public void nem(String pwd) {
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 ();
System.out.println("mnemonic:" + mnemonic);
List mnemonicList = Arrays.asList(mnemonic.split(" "));
byte[] seed = new SeedCalculator()
.withWordsFromWordList(English.INSTANCE)
.calculateSeed(mnemonicList, pwd);
ECKeyPair ecKeyPair = ECKeyPair.create (Sha256.sha256(seed));
String privateKey = ecKeyPair.getPrivateKey().toString(16);
String publicKey = ecKeyPair.getPublicKey().toString(16);
String address = Keys.getAddress(publicKey);
System.out.println("privateKey:" + privateKey);
System.out.println("publicKey:" + publicKey);
System.out.println("address:" + address);
}
- 建立賬戶並返回助記詞
/**
* 建立賬戶
*
* @param pwd
* @return mnemonic :助記詞
* privateKey:私鑰
* publicKey:公鑰
* address:地址
* accountFilePath:賬戶檔案路徑
*/
public Map createAccount(String pwd) {
Map resultMap = new LinkedHashMap();
//String filePath = System.getProperty("user.home") + File.separator + "keystore";
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();
System.out.println("mnemonic:" + mnemonic);
List mnemonicList = Arrays.asList(mnemonic.split(" "));
byte[] seed = new SeedCalculator()
.withWordsFromWordList(English.INSTANCE)
.calculateSeed(mnemonicList, pwd);
ECKeyPair ecKeyPair = ECKeyPair.create(Sha256.sha256(seed));
String privateKey = ecKeyPair.getPrivateKey().toString(16);
String publicKey = ecKeyPair.getPublicKey().toString(16);
String address = "0x" + Keys.getAddress(publicKey);
//建立錢包地址與金鑰
String fileName = null;
try {
fileName = WalletUtils.generateWalletFile(pwd, ecKeyPair, new File(filePath), false);
} catch (Exception e) {
e.printStackTrace();
}
if (fileName == null) {
return null;
}
String accountFilePath = filePath + File.separator + fileName;
resultMap.put("mnemonic", mnemonic);
resultMap.put("privateKey", privateKey);
resultMap.put("publicKey", publicKey);
resultMap.put("address", address);
resultMap.put("accountFilePath", accountFilePath);
return resultMap;
}
- 根據助記詞匯入
/**
* 根據助記詞匯入
*
* @param pwd : 錢包密碼
* @param mnemonic :助記詞
* @return
*/
public Map importByMnemonic(String pwd, String mnemonic) {
Map resultMap = new LinkedHashMap();
List mnemonicList = Arrays.asList(mnemonic.split(" "));
byte[] seed = new SeedCalculator()
.withWordsFromWordList(English.INSTANCE)
.calculateSeed(mnemonicList, pwd);
ECKeyPair ecKeyPair = ECKeyPair.create(Sha256.sha256(seed));
String privateKey = ecKeyPair.getPrivateKey().toString(16);
String publicKey = ecKeyPair.getPublicKey().toString(16);
String address = "0x" + Keys.getAddress(publicKey);
//建立錢包地址與金鑰
String fileName = null;
try {
fileName = WalletUtils.generateWalletFile(pwd, ecKeyPair, new File(filePath), false);
} catch (Exception e) {
e.printStackTrace();
}
if (fileName == null) {
return null;
}
String accountFilePath = filePath + File.separator + fileName;
resultMap.put("mnemonic", mnemonic);
resultMap.put("privateKey", privateKey);
resultMap.put("publicKey", publicKey);
resultMap.put("address", address);
resultMap.put("accountFilePath", accountFilePath);
return resultMap;
}
匯出賬戶
/**
* 匯出賬戶
* @param walletFilePath : 賬戶完整路徑,包括檔名
* @param password : 密碼
* @return
*/
public Map export(String walletFilePath, String password) {
Map resultMap = new LinkedHashMap();
Credentials credentials = loadAccount(walletFilePath, password);
ECKeyPair ecKeyPair = credentials.getEcKeyPair();
boolean useFullScrypt = false;
WalletFile walletFile = null;
try {
if (useFullScrypt) {
walletFile = Wallet.createStandard(password, ecKeyPair);
} else {
walletFile = Wallet.createLight(password, ecKeyPair);
}
} catch (Exception e) {
e.printStackTrace();
}
String fileNameEx = getWalletFileName(walletFile);
System.out.println("walletFile:" + JSON.toJSONString(walletFile));
System.out.println("fileNameEx:" + fileNameEx);
resultMap.put("walletFile", walletFile);
resultMap.put("fileName", fileNameEx);
return resultMap;
}
maven依賴:
<!-- https://mvnrepository.com/artifact/io.github.novacrypto/BIP39 -->
<dependency>
<groupId>io.github.novacrypto</groupId>
<artifactId>BIP39</artifactId>
<version>0.1.9</version>
</dependency>
<dependency>
<groupId>io.github.novacrypto</groupId>
<artifactId>BIP32</artifactId>
<version>0.0.9</version>
</dependency>
<dependency>
<groupId>io.github.novacrypto</groupId>
<artifactId>BIP44</artifactId>
<version>0.0.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.github.novacrypto/SHA256 -->
<dependency>
<groupId>io.github.novacrypto</groupId>
<artifactId>SHA256</artifactId>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>com.lambdaworks</groupId>
<artifactId>scrypt</artifactId>
<version>1.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.github.novacrypto/ToRuntime -->
<dependency>
<groupId>io.github.novacrypto</groupId>
<artifactId>ToRuntime</artifactId>
<version>0.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.madgag.spongycastle/core -->
<dependency>
<groupId>com.madgag.spongycastle</groupId>
<artifactId>core</artifactId>
<version>1.58.0.0</version>
</dependency>
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>3.5.0</version>
</dependency>