我试图为以太坊混合BitcoinJ
和Web3j
创建确定性钱包。确定性密钥使用BitcoinJ
生成,以及使用Web3j
生成以太坊凭证。但是使用Web3j
凭据生成的地址不符合预期。
String seedCode = "yard impulse luxury drive today throw farm pepper survey wreck glass federal";
// BitcoinJ
DeterministicSeed seed = new DeterministicSeed(seedCode, null, "", 1409478661L);
DeterministicKeyChain chain = DeterministicKeyChain.builder().seed(seed).build();
DeterministicKey key = chain.getKey(KeyPurpose.RECEIVE_FUNDS);
BigInteger privKey = key.getPrivKey();
// Web3j
Credentials credentials = Credentials.create(privKey.toString(16));
System.out.println("Address: " + credentials.getAddress());
输出:0x2c4186d0422d0462a48c92cd559cbc30f528855b
预计:0x72445fcFdEB1Fff79496D7Ce66089d663Ff90E26
代码中的误解在哪里?
答案 0 :(得分:5)
默认情况下,bitcoinj
使用路径 m/0'/0
作为密钥链。大多数以太坊解决方案使用 BIP44 规范中的 m/44'/60'/0'/0
。这就是为什么与其他以太坊工具相比,结果并不像预期的那样。
以下代码修复问题":
String seedCode = "yard impulse luxury drive today throw farm pepper survey wreck glass federal";
// BitcoinJ
DeterministicSeed seed = new DeterministicSeed(seedCode, null, "", 1409478661L);
DeterministicKeyChain chain = DeterministicKeyChain.builder().seed(seed).build();
List<ChildNumber> keyPath = HDUtils.parsePath("M/44H/60H/0H/0/0");
DeterministicKey key = chain.getKeyByPath(keyPath, true);
BigInteger privKey = key.getPrivKey();
// Web3j
Credentials credentials = Credentials.create(privKey.toString(16));
System.out.println(credentials.getAddress());