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

本文实现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"));
       tran.setMethod(0L);
       tran.setParams("");

       String sign = FilecoinSign.signTransaction(tran,mnemonicWords);

       JSONObject cid = new JSONObject();
       JSONObject signer = new JSONObject();
       JSONObject message = new JSONObject();
       JSONObject callback = new JSONObject();
       JSONObject ncid = new JSONObject();

       cid.put("/","");
       ncid.put("/","");

       signer.put("Type",1);
       signer.put("Data",sign);

       message.put("To",tran.getTo());
       message.put("From",tran.getFrom());
       message.put("Nonce",tran.getNonce());
       message.put("Value",tran.getValue());
       message.put("GasLimit",tran.getGasLimit());
       message.put("GasFeeCap",tran.getGasFeeCap());
       message.put("GasPremium",tran.getGasPremium());
       message.put("Method",0);
       message.put("Params","");
       message.put("CID",cid);

       callback.put("Message",message);
       callback.put("Signature",signer);
       callback.put("CID",ncid);

       return new WalletManagerReturnDto("", true, 0, callback.toJSONString());
   }

6.交易确认
    @Override
    public int transactionConfirm(String txId) {
        String url = "https://filscoutv3api.ipfsunion.cn/message/by_cid/"+txId;
        try {
            String urlinfo = OkHttpClientUtil.httpGet(url);
            JSONObject jsonObject = JSONObject.parseObject(urlinfo);
            if (jsonObject == null) {
                return 0;
            }
 
            if (jsonObject.getString("error") != null && jsonObject.getString("error").equals("ok")) {
                JSONObject data = JSONObject.parseObject(jsonObject.getString("data"));
                if(data!=null){
                    JSONObject message = JSONObject.parseObject(data.getString("message"));
                    if(message!=null && message.getString("exit_code_name").equals("OK")) {
                        long height = message.getLong("block_height")!=null?message.getLong("block_height"):0L;
                        if(height==0){
                            return 0;
                        }
                        long head = client.getBlockHeight();
                        if(head - height > 10){
                            return 1;
                        }
                    }else{
                        return 2;
                    }
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
 
        return 0;
    }
7.一些实体类
@Data
public class SignData {

   private UnsignedInteger version;

   private ByteString from;

   private ByteString to;

   private UnsignedInteger nonce;

   private ByteString value;

   private ByteString gasFeeCap;

   private ByteString gasPremium;

   private UnsignedInteger gasLimit;

   private UnsignedInteger methodNum;

   private ByteString params; //空数组


}

8.总结 filecoin 交易费设置影响交易的确认,目前网络矿工优先打包数据存储和验证的交易,币币交易很难被打包。主流交易所和imtoken等估计是和矿池有合作,打包速度才提升。比币交易费产生收益较少,矿工不愿打包。每一个区块在调试的时候100多笔交易,通常只有几笔是币币的。参考设置imtoken或者火币一样的gaslimit gasfeecap gaspremium一样长时间没人打包。通常要等到深夜。

 

参考地址:https://www.jianshu.com/p/65cfa5e11738

                 https://filfox.info/zh/stats/gas

                 https://github.com/filecoin-project/lotus/blob/master/documentation/en/api-methods.md#GasEstimateMessageGas

                 https://www.jianshu.com/p/c0a8fdabbe78

                https://documenter.getpostman.com/view/4872192/SWLh5mUd?version=latest

               https://filscout.io/zh


0
199