1. springboot pom引入类库
<dependencies>
<dependency>
<groupId>org.bitcoinj</groupId>
<artifactId>bitcoinj-core</artifactId>
<version>0.14.7</version>
</dependency>
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>com.github.apetersson</groupId>
<artifactId>Blake2b</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.4.7</version>
</dependency>
<dependency>
<groupId>co.nstant.in</groupId>
<artifactId>cbor</artifactId>
<version>0.9</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>central</id>
<url>https://repo.maven.apache.org/maven2</url>
<layout>default</layout>
</repository>
<repository>
<id>abcd</id>
<url>http://maven.scijava.org/content/repositories/public/</url>
</repository>
</repositories>
2. 生成bip44地址:
public static final ChildNumber FIL_HARDENED = new ChildNumber(461, true);
public String getBip44Credentials(List<String> mnemonicWords, String passPhrase, int number) {
String words = Joiner.on(" ").join(mnemonicWords);
byte[] seed = MnemonicUtils.generateSeed(words, passPhrase);
DeterministicKey rootPrivateKey = HDKeyDerivation.createMasterPrivateKey(seed);
DeterministicHierarchy deterministicHierarchy = new DeterministicHierarchy(rootPrivateKey);
ImmutableList<ChildNumber> 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();
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);
}
3. 构建交易
@Override
public WalletReturnDto sendCoin(String addrfrom, String addrto, BigDecimal amount, BigDecimal mbfee, String ethContactAddress) {
BigInteger nonce = client.getNonce(addrfrom);
BigDecimal balance = client.getbalance(addrfrom);
if(balance.compareTo(amount.add(new BigDecimal("0.001")))<0){
return new WalletReturnDto(LangString.CREATE_TX_FAIL, false, 200004, "", BigDecimal.ZERO, BigDecimal.ZERO);
}
BigDecimal pow = BigDecimal.TEN.pow(6);
BigDecimal gasLimit = nonce.compareTo(BigInteger.ZERO)==0?new BigDecimal(3).multiply(pow)
:new BigDecimal(2).multiply(pow);
BigDecimal fee = new BigDecimal("0.001");
Transaction transaction = new Transaction();
transaction.setFrom(addrfrom);
transaction.setTo(addrto);
transaction.setNonce(nonce.longValue());
transaction.setValue(amount.toString());
transaction.setGasLimit(gasLimit.longValue());
transaction.setGasPremium("1000129");
transaction.setGasFeeCap("500000000");
return new WalletReturnDto(LangString.CREATE_TX_SUCCESS, true, 0, JSON.toJSONString(transaction), fee, BigDecimal.ZERO);
}
4.签名
@Override
public WalletManagerReturnDto signTransaction(String inputTransaction, String addr, List<String> mnemonicWords, String passPhrase) {
Transaction tran = new Transaction();
JSONObject jsonObject = JSONObject.parseObject(inputTransaction);
String value = jsonObject.getString("value");
BigDecimal amount = new BigDecimal(value).multiply(BigDecimal.TEN.pow(18));
tran.setFrom(jsonObject.getString("from"));
tran.setTo(jsonObject.getString("to"));
tran.setValue(amount.toBigInteger().toString());
tran.setNonce(jsonObject.getLong("nonce"));
tran.setGasLimit(jsonObject.getLong("gasLimit"));
tran.setGasFeeCap(jsonObject.getString("gasFeeCap"));
tran.setGasPremium(jsonObject.getString("gasPremium"))