知乎专栏 | 多维度架构 |
package cn.netkiller.ethereum.wallet; import java.io.File; import java.io.IOException; import java.security.InvalidAlgorithmParameterException; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import org.web3j.crypto.CipherException; import org.web3j.crypto.WalletUtils; public class WalletMain { public void createWallet() throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, CipherException, IOException { File file = new File("/tmp/ethereum/keystore"); String password = "passw0rd"; String fileName = WalletUtils.generateFullNewWalletFile(password, file); System.out.println(fileName); } public static void main(String[] args) { // TODO Auto-generated method stub WalletMain wallet = new WalletMain(); try { wallet.createWallet(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
运行结果
neo@MacBook-Pro ~ % mkdir -p /tmp/ethereum/keystore neo@MacBook-Pro ~ % ll /tmp/ethereum/keystore total 8 -rw-r--r-- 1 neo wheel 491B Feb 4 18:30 UTC--2018-02-04T10-30-58.476000000Z--75d01e920d6e018445dae504058ce4d968fd2a58.json neo@MacBook-Pro ~ % cat /tmp/ethereum/keystore/UTC--2018-02-04T10-30-58.476000000Z--75d01e920d6e018445dae504058ce4d968fd2a58.json {"address":"75d01e920d6e018445dae504058ce4d968fd2a58","id":"80700448-69bc-475a-aaf9-f2b836f17b13","version":3,"crypto":{"cipher":"aes-128-ctr","ciphertext":"fe86f5dbd61d15d092f9d6870e70bff7ed99a7925703ea71eef23669c8e3ec62","cipherparams":{"iv":"d058819ab660cd062080b405591ba143"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"f69c535137b08667dbac53b8001313f5b43f81fce67a5d0e94b518c97d212d14"},"mac":"c247e34760bc838c3a4c8b2da286ccc6acec244bbc13fc6cc9ce28e88a7319d5"}}
package cn.netkiller.ethereum.wallet; import java.io.File; import java.io.IOException; import java.security.InvalidAlgorithmParameterException; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import org.web3j.crypto.CipherException; import org.web3j.crypto.Credentials; import org.web3j.crypto.WalletUtils; public class WalletMain { public void walletAddress() throws IOException, CipherException { File file = new File( "/tmp/ethereum/keystore/UTC--2018-02-04T10-43-27.339000000Z--7cab470df532710d13078c5cdc0812a27f70cf51.json"); String password = "passw0rd"; Credentials credentials = WalletUtils.loadCredentials(password, file); System.out.println(credentials.getAddress()); } public static void main(String[] args) { // TODO Auto-generated method stub WalletMain wallet = new WalletMain(); try { wallet.walletAddress(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
运行结果
0x7cab470df532710d13078c5cdc0812a27f70cf51
String keystore = WalletUtils.getDefaultKeyDirectory(); System.out.println("生成keystore文件的默认目录:" + keystore); // 通过密码及keystore目录生成钱包 Bip39Wallet wallet = WalletUtils.generateBip39Wallet("yourpassword", new File(keystore)); // keystore文件名 System.out.println(wallet.getFilename()); // 12个单词的助记词 System.out.println(wallet.getMnemonic());
package cn.netkiller.example.ethereum.mnemonic; import java.security.SecureRandom; import org.web3j.crypto.MnemonicUtils; public class MnemonicUtilsTest { public MnemonicUtilsTest() { // TODO Auto-generated constructor stub } public static void main(String[] args) { // TODO Auto-generated method stub byte[] initialEntropy = new byte[16]; SecureRandom secureRandom = new SecureRandom(); secureRandom.nextBytes(initialEntropy); String mnemonic = MnemonicUtils.generateMnemonic(initialEntropy); System.out.println(mnemonic); } }
Credentials credentials = WalletUtils.loadBip39Credentials("password", "spoon crisp length scrub train scrap initial inherit airport that answer tornado"); // 钱包地址 System.out.println(credentials.getAddress()); // 公钥16进制字符串表示 System.out.println(credentials.getEcKeyPair().getPublicKey().toString(16)); // 私钥16进制字符串表示 System.out.println(credentials.getEcKeyPair().getPrivateKey().toString(16));