Filecoin java 离线生成地址
Posted jc0803kevin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Filecoin java 离线生成地址相关的知识,希望对你有一定的参考价值。
使用DeterministicKey以路径BIP44
filecoin 的子路径为461
import cn.hutool.core.codec.Base32;
import com.google.common.collect.ImmutableList;
import org.bitcoinj.core.ECKey;
import org.bitcoinj.crypto.ChildNumber;
import org.bitcoinj.crypto.DeterministicHierarchy;
import org.bitcoinj.crypto.DeterministicKey;
import org.bitcoinj.crypto.HDKeyDerivation;
import org.web3j.crypto.MnemonicUtils;
import org.web3j.utils.Numeric;
import ove.crypto.digest.Blake2b;
public class Address {
public static final ChildNumber FIL_HARDENED = new ChildNumber(461, true);
public static String getBip44Credentials(String mnemonicWords, String passPhrase, int number) {
byte[] seed = MnemonicUtils.generateSeed(mnemonicWords, passPhrase);
DeterministicKey rootPrivateKey = HDKeyDerivation.createMasterPrivateKey(seed);
DeterministicHierarchy deterministicHierarchy = new DeterministicHierarchy(rootPrivateKey);
ImmutableList path = ImmutableList.of(new ChildNumber(44, true), FIL_HARDENED, ChildNumber.ZERO_HARDENED);
DeterministicKey fourpath = deterministicHierarchy.get(path, true, true);
DeterministicKey fourpathhd = HDKeyDerivation.deriveChildKey(fourpath, 0);
DeterministicKey fivepathhd = HDKeyDerivation.deriveChildKey(fourpathhd, number);
Blake2b.Digest blake2b1 = Blake2b.Digest.newInstance(20);
ECKey ecKey = ECKey.fromPrivate(fivepathhd.getPrivKey(),false);
String pulStr = ecKey.getPublicKeyAsHex();
System.out.println("getPublicKeyAsHex "+ecKey.getPublicKeyAsHex());
System.out.println("getPrivateKeyAsHex "+ecKey.getPrivateKeyAsHex());
byte[] bytes = Numeric.hexStringToByteArray(pulStr);
byte[] black2HashByte = blake2b1.digest(bytes);
String black2HashStr = Numeric.toHexStringNoPrefix(black2HashByte);
String black2HashSecond = "0x01"+black2HashStr;
Blake2b.Digest blake2b2 = Blake2b.Digest.newInstance(4);
byte[] checksumBytes = blake2b2.digest(Numeric.hexStringToByteArray(black2HashSecond));
byte[] addressBytes = new byte[black2HashByte.length + checksumBytes.length];
System.arraycopy(black2HashByte, 0, addressBytes, 0, black2HashByte.length);
System.arraycopy(checksumBytes, 0, addressBytes, black2HashByte.length,checksumBytes.length);
//f 正式 t 测试 1 钱包 2 合约
return "f1"+ Base32.encode(addressBytes);
}
public static String fromPub(String pubKeyAsHex){
Blake2b.Digest blake2b1 = Blake2b.Digest.newInstance(20);
byte[] bytes = Numeric.hexStringToByteArray(pubKeyAsHex);
byte[] black2HashByte = blake2b1.digest(bytes);
String black2HashStr = Numeric.toHexStringNoPrefix(black2HashByte);
String black2HashSecond = "0x01"+black2HashStr;
Blake2b.Digest blake2b2 = Blake2b.Digest.newInstance(4);
byte[] checksumBytes = blake2b2.digest(Numeric.hexStringToByteArray(black2HashSecond));
byte[] addressBytes = new byte[black2HashByte.length + checksumBytes.length];
System.arraycopy(black2HashByte, 0, addressBytes, 0, black2HashByte.length);
System.arraycopy(checksumBytes, 0, addressBytes, black2HashByte.length,checksumBytes.length);
//f 正式 t 测试 1 钱包 2 合约
return "f1"+ Base32.encode(addressBytes);
}
}
以上是关于Filecoin java 离线生成地址的主要内容,如果未能解决你的问题,请参考以下文章