如何使用 Java 为以太坊生成确定性密钥?

How to generate deterministic keys for Ethereum using Java?

我正在尝试为以太坊混合 BitcoinJWeb3j 创建一个确定性钱包。使用 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 作为密钥链。大多数以太坊解决方案使用 m/44'/60'/0'/0 来自 BIP44 规范。这就是为什么与其他以太坊工具相比结果不如预期的原因。

下面的代码修复了 "the problem":

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());