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

5.交易广播

    @Override
    public WalletReturnDto broadcastTx(String txid, String txData) {
        String hash = "";
        if (!txData.isEmpty()) {
            hash = client.push(txData);
            if (hash==null || hash.equals("")){
                return new WalletReturnDto(LangString.GET_BROADCAST_FAIL, false, 200005, hash);
            }
        } else {
            return new WalletReturnDto(LangString.GET_BROADCAST_FAIL, false, 200005, hash);
        }
        return new WalletReturnDto(LangString.GET_BROADCAST_SUCCESS, true, 0, hash);
    }

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; //空数组
 
 
}
@Data
public class Transaction {
 
    private String to;
 
    private String from;
 
    private Long nonce;
 
    private String value;
 
    private Long gasLimit;
 
    private String gasFeeCap;
 
    private String gasPremium;
 
    private Long method;
 
    private String params;
 
    private Integer version;
 
    private String CID;
 
}
@Component
public class FilFactory {
 
    private static final BigDecimal FIL_UNIT = BigDecimal.TEN.pow(18);
 
    //获取nonce
    private final static String NONCE = "Filecoin.MpoolGetNonce";
    //获取余额
    private final static String BALANCE = "Filecoin.WalletBalance";
    //获取交易费
    private final static String VALID_ADDRESS = "Filecoin.WalletValidateAddress";
    //广播
    private final static String PUSH = "Filecoin.MpoolPush";
    //当前区块高度
    private final static String BLOCK_HEIGHT = "Filecoin.ChainHead";
 
 
    public BigDecimal getbalance(String address) {
        String[] addr = {address};
        String body = getBody(BALANCE,addr);
        if(body!=null) {
            JSONObject jsonBody = JSONObject.parseObject(body);
            if (jsonBody != null) {
                String balance = jsonBody.getString("result");
                if (balance != null) {
                    return (new BigDecimal(balance)).divide(FIL_UNIT, 8, RoundingMode.HALF_DOWN);
                }
            }
        }
        return BigDecimal.ZERO;
    }
 
    public String checkAddress(String address) {
        String[] addr = {address};
        String body = getBody(VALID_ADDRESS,addr);
        if(body!=null) {
            JSONObject jsonBody = JSONObject.parseObject(body);
            return  jsonBody!=null?jsonBody.getString("result"):null;
        }
        return null;
    }
 
    public BigInteger getNonce(String address) {
        String[] addr = {address};
        String body = getBody(NONCE,addr);
        if(body!=null) {
            JSONObject jsonBody = JSONObject.parseObject(body);
            if (jsonBody != null) {
                String balance = jsonBody.getString("result");
                if (balance != null) {
                    System.out.println("nonce=" + new BigDecimal(balance).toBigInteger());
                    return new BigDecimal(balance).toBigInteger();
                }
            }
        }
        return BigInteger.ZERO;
    }
 
    public Integer getBlockHeight() {
        List<JSONObject> jsonObjects = new ArrayList<>();
        System.out.println("height=");
        String body = getBody(BLOCK_HEIGHT,jsonObjects);
        JSONObject jsonBody = JSONObject.parseObject(body);
        if(jsonBody!=null) {
            String result = jsonBody.getString("result");
            if (result != null) {
                JSONObject object = JSONObject.parseObject(result);
                if (object != null) {
                    int height = Integer.parseInt(object.getString("Height"));
                    System.out.println("height=" + height);
                    return height;
                }
            }
        }
        return 0;
    }
 
    public String push(String message) {
        JSONObject jsonObject = JSONObject.parseObject(message);
        List<JSONObject> jsonObjects = new ArrayList<>();
        jsonObjects.add(jsonObject);
        String body = getBody(PUSH,jsonObjects);
        if(body!=null) {
            JSONObject jsonBody = JSONObject.parseObject(body);
            if(jsonBody!=null) {
                String result = jsonBody.getString("result");
                System.out.println("result=" + result);
                if (result != null) {
                    JSONObject object = JSONObject.parseObject(result);
                    if (object != null) {
                        String cid = object.getString("/");
                        System.out.println("cid=" + cid);
                        return cid;
                    }
                }
            }
        }
        return "";
    }
 
    public String getBody(String method,List<JSONObject> object) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("jsonrpc", "2.0");
        jsonObject.put("method", method);
        jsonObject.put("params", object);
        jsonObject.put("id", 1);
        String json = jsonObject.toJSONString();
        HttpRequest httpRequest = HttpRequest.post(RpcConstant.FIL_RPC_URL).headerMap(getHeaderMap(), true);
        //httpRequest.setHttpProxy("127.0.0.1",7890);
        return httpRequest.body(json).execute().body();
    }
 
    public String getBody(String method,String[] params) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("jsonrpc", "2.0");
        jsonObject.put("method", method);
        jsonObject.put("params", params);
        jsonObject.put("id", 1);
        String json = jsonObject.toJSONString();
        return HttpRequest.post(RpcConstant.FIL_RPC_URL).headerMap(getHeaderMap(), true).body(json).execute().body();
    }
 
    public Map<String,String> getHeaderMap() {
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Content-Type", "application/json");
        headerMap.put("charset", "UTF-8");
        headerMap.put("Authorization", "Bearer " + RpcConstant.FIL_RPC_PASSWORD);
        return headerMap;
    }

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