我正在尝试为混合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
代码中的误解在哪里?
最佳答案
默认情况下,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());
关于java - 如何使用Java为以太坊生成确定性 key ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49201637/