主页 > imtoken钱包官网下载2.0 > 区块链-以太坊转账离线签名
区块链-以太坊转账离线签名
先说一下整体流程。
首先,钱包在本地对转账信息进行离线签名,然后通过以太坊JSON-RPC方式发送给以太坊节点,这里以太坊和其他ERC20代币的签名是不同的。
转账 ETH 转账签名
/**
* ETH 转账离线签名
* @param to 转入的钱包地址
* @param nonce 以太坊nonce
* @param gasPrice gasPrice
* @param gasLimit gasLimit
* @param amount 转账的eth数量
* @param wallet 钱包对象
* @param password 密码
* @return 签名data
*/
public String signedEthTransactionData(String to,
BigInteger nonce,
BigInteger gasPrice,
BigInteger gasLimit,
BigDecimal amount,
HLWallet wallet,
String password) throws Exception {
// 把十进制的转换成ETH的Wei, 1ETH = 10^18 Wei
BigDecimal amountInWei = Convert.toWei(amount.toString(), Convert.Unit.ETHER);
RawTransaction rawTransaction = RawTransaction.createEtherTransaction(nonce, gasPrice, gasLimit, to, amountInWei.toBigInteger());
return signData(rawTransaction,wallet,password);
}
private String signData(RawTransaction rawTransaction,
HLWallet wallet,
String password) throws Exception {
Credentials credentials = Credentials.create(LWallet.decrypt(password, wallet.walletFile));
byte[] signMessage = TransactionEncoder.signMessage(rawTransaction, ChainId.MAINNET, credentials);
return Numeric.toHexString(signMessage);
}
随机数
为了防止交易重放攻击,每笔交易都必须有一个nonce随机数。 每个账户的 nonce 从 0 开始。 处理完nonce为0的交易后,依次处理nonce为1的交易并添加。 将处理 1 笔交易。 下面是nonce使用的几个规则:
我们可以通过以太坊JSON-RPC方式获取nonce
ERC-20代币转账签名
ERC-20 代币不同于以太坊转账,需要智能合约。
下面是手动拼接的方式
public String signedContractTransactionData(String contractAddress,
String to,
BigInteger nonce,
BigInteger gasPrice,
BigInteger gasLimit,
BigDecimal amount,
BigDecimal decimal,
HLWallet wallet,
String password) throws Exception {
//因为每个代币可以规定自己的小数位, 所以实际的转账值=数值 * 10^小数位
BigDecimal realValue = amount.multiply(decimal);
//0xa9059cbb代表某个代币的转账方法hex(transfer) + 对方的转账地址hex + 转账的值的hex
String data = Params.Abi.transfer + // 0xa9059cbb
Numeric.toHexStringNoPrefixZeroPadded(Numeric.toBigInt(to), 64) +
Numeric.toHexStringNoPrefixZeroPadded(realValue.toBigInteger(), 64);
RawTransaction rawTransaction = RawTransaction.createTransaction(
nonce,
gasPrice,
gasLimit,
contractAddress,
data);
return signData(rawTransaction, wallet, password);
}
此时区块链钱包是以太坊钱包的是,我们需要通过签名时的数据发送转账的指令信息。 令牌传输中包含三个数据组件。
首先是固定标头 0xa9059cbb
然后转账到钱包地址
然后是转账金额,以wei为单位
这里我们解释一下为什么固定的header是0xa9059cbb。 因为是函数原型transfer(address, uint256)的MethodId,相当于以太坊用来区分指令的标识。 具体的转换方法是
// transfer -> 0xa9059cbb
String transfer = "transfer(address,uint256)";
byte[] bytes = transfer.getBytes();
byte[] bytes1 = org.web3j.crypto.Hash.sha3(bytes);
String hex = Numeric.toHexString(bytes1, 0, 4, true);
ShadowLog.i("transfer", hex);
然后转入地址和转入金额,我们转成十六进制,然后在左边加上0到64位。 三个参数串拼接在一起,形成最终签名使用的数据。 那么有人会问了,其他的命令参数也是这样拼接的吗? 当然不是,这里涉及到以太坊合约ABI,感兴趣的童鞋可以看看。 这里因为转入地址的类型地址和转账金额的类型uint256是静态的,所以就是上面说的拼接方式。 如果是动态的,那就不一样了。 详情请参考ABI文档。
这里我们提供另一种web3j现有的封装实现,无需关心内部参数是如何拼接的。 (推荐的)
public String signContractTransaction(String contractAddress,
String to,
BigInteger nonce,
BigInteger gasPrice,
BigInteger gasLimit,
BigDecimal amount,
BigDecimal decimal,
HLWallet wallet,
String password) throws IOException, CipherException {
BigDecimal realValue = amount.multiply(decimal);
Function function = new Function("transfer",
Arrays.asList(new Address(to), new Uint256(realValue.toBigInteger())),
Collections.emptyList());
String data = FunctionEncoder.encode(function);
RawTransaction rawTransaction = RawTransaction.createTransaction(
nonce,
gasPrice,
gasLimit,
contractAddress,
data);
return signData(rawTransaction, wallet, password);
}
发送请求
签名后通过以太坊的JSON-RPC发送出去。
以太坊浏览器上ETH转账的最终转账结果链接,可以在页面下方查看Input Data
0x3078
以太坊浏览器链接上代币转账的最终转账结果区块链钱包是以太坊钱包的是,Input Data为
Function: transfer(address _to, uint256 _value)
MethodID: 0xa9059cbb
[0]: 000000000000000000000000b7bb6c45800f4531cc1581637868373a06367b48
[1]: 000000000000000000000000000000000000000000000002d1a51c7e00500000
常问问题
Q:为什么调用sendRawTransaction接口后在以太坊浏览器或其他钱包中查不到转账记录?
A:接口调用后,会返回一个字符串hash,我们通常称之为txHash。 这个时候结果还不知道,因为以太坊可能因为拥堵或者其他原因不确定,所以找不到是正常的。 在app中,一种做法是发送请求后主动通知relay中继,在relay中继维护txHash状态,在processing状态呈现给用户。
友链:
参考: