ETH 簽名
public static String signedEthTransactionData(
String privateKey, String to, BigInteger nonce, BigInteger gasPrice, BigInteger gasLimit, String value) {
// 把十進(jìn)制的轉(zhuǎn)換成ETH的Wei, 1ETH = 10^18 Wei
BigDecimal realValue = Convert.toWei(value, Convert.Unit.ETHER);
RawTransaction rawTransaction = RawTransaction.createEtherTransaction(nonce, gasPrice, gasLimit, to, realValue.toBigIntegerExact());
// 手續(xù)費(fèi)= (gasPrice * gasLimit ) / 10^18 ether
Credentials credentials = Credentials.create(privateKey);
// 使用TransactionEncoder對(duì)RawTransaction進(jìn)行簽名操作
byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
// 轉(zhuǎn)換成0x開(kāi)頭的字符串
return Numeric.toHexString(signedMessage);
}
ETH代幣簽名
public static String signedEthContractTransactionData(
String privateKey, String contractAddress, String to, BigInteger nonce, BigInteger gasPrice,
BigInteger gasLimit, Double value, Double decimal) {
// 因?yàn)槊總€(gè)代幣可以規(guī)定自己的小數(shù)位, 所以實(shí)際的轉(zhuǎn)賬值 = 數(shù)值 * 10^小數(shù)位
BigDecimal realValue = BigDecimal.valueOf(value * Math.pow(10.0, decimal));
// 0xa9059cbb代表某個(gè)代幣的轉(zhuǎn)賬方法hex(transfer) + 對(duì)方的轉(zhuǎn)賬地址hex + 轉(zhuǎn)賬的值的hex
String data = "0xa9059cbb" + Numeric.toHexStringNoPrefixZeroPadded(Numeric.toBigInt(to), 64)
+ Numeric.toHexStringNoPrefixZeroPadded(realValue.toBigInteger(), 64);
RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, gasPrice, gasLimit, contractAddress, data);
// 手續(xù)費(fèi)= (gasPrice * gasLimit ) / 10^18 ether
Credentials credentials = Credentials.create(privateKey);
// 使用TransactionEncoder對(duì)RawTransaction進(jìn)行簽名操作
byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
// 轉(zhuǎn)換成0x開(kāi)頭的字符串
return Numeric.toHexString(signedMessage);
}