java 调试filecoin,实现地址生成,签名交易

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"))

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xuxizhou1994

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值