跳至主要内容
本页面使用机器翻译自英语,可能包含错误或不清楚的语言。如需最准确的信息,请参阅英文原文。由于更新频繁,部分内容可能与英文原文有出入。请加入我们在 Crowdin 上的努力,帮助我们改进本页面的翻译。 (Crowdin translation page, Contributing guide)

传统账户密钥

从 Web3j 和 kaia 库(web3j-ext)中导入必要的类。

使用指定的 BAOBAB_URL 创建 Web3j 实例

此外,您还可以更改默认提供商。 例如,使用alchemy提供商。

从私人密钥创建证书

燃气价格gas 限值设置

发件人地址设置为已加载凭据的地址

从网络中获取chain ID

接收器地址设置为任何有效地址

获取发件人地址的nonce

设置要传输的值

将交易类型设置为 VALUE_TRANSFER

为价值转移创建原始交易

签署交易

签署的交易发送至 kaia 网络

从签名交易中恢复发件人地址,并将其与发件人地址进行比较

关闭 Web3j 实例

SignTxWithLegacyExample.java

package org.web3j.example.accountKey;
import org.web3j.tx.response.PollingTransactionReceiptProcessor;
import org.web3j.tx.response.TransactionReceiptProcessor;
import org.web3j.example.keySample;
import java.io.IOException;
import java.math.BigInteger;
import org.web3j.crypto.KlayCredentials;
import org.web3j.crypto.KlayRawTransaction;
import org.web3j.crypto.KlayTransactionEncoder;
import org.web3j.crypto.transaction.type.TxType;
import org.web3j.crypto.transaction.type.TxType.Type;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.response.EthChainId;
import org.web3j.protocol.http.HttpService;
import org.web3j.protocol.kaia.Web3j;
import org.web3j.protocol.kaia.core.method.response.KlayRecoverFromTransactionResponse;
import org.web3j.utils.Numeric;
import org.web3j.protocol.core.methods.response.EthSendTransaction;
/**
*
*/
public class SignTxWithLegacyExample implements keySample {
/**
*
*/
public static void run() throws Exception {
Web3j web3j = Web3j.build(new HttpService(keySample.BAOBAB_URL));
KlayCredentials credentials1 = KlayCredentials.create(keySample.LEGACY_KEY_privkey);
BigInteger GAS_PRICE = BigInteger.valueOf(50000000000L);
BigInteger GAS_LIMIT = BigInteger.valueOf(6721950);
String from = credentials1.getAddress();
EthChainId EthchainId = web3j.ethChainId().send();
long chainId = EthchainId.getChainId().longValue();
String to = "0x000000000000000000000000000000000000dead";
BigInteger nonce = web3j.ethGetTransactionCount(from, DefaultBlockParameterName.LATEST).send()
.getTransactionCount();
BigInteger value = BigInteger.valueOf(100);
TxType.Type type = Type.VALUE_TRANSFER;
KlayRawTransaction raw = KlayRawTransaction.createTransaction(
type,
nonce,
GAS_PRICE,
GAS_LIMIT,
to,
value,
from);
byte[] signedMessage = KlayTransactionEncoder.signMessage(raw, chainId, credentials1);
String hexValue = Numeric.toHexString(signedMessage);
EthSendTransaction transactionResponse = web3j.ethSendRawTransaction(hexValue).send();
System.out.println("TxHash : \n " + transactionResponse.getResult());
String blockNumber = "latest";
KlayRecoverFromTransactionResponse response = web3j.klayRecoverFromTransaction(hexValue, blockNumber).send();
System.out.println("Original address : " + from);
System.out.println("Result address : " + response.getResult());
web3j.shutdown();
}
}

output

❯ java SignTxWithLegacyExample.java
TxHash :
0x957734684be8f79a21cef4de1842709b84c92e3920d656165ddb951981987b5a
Original address : 0xa2a8854b1802d8cd5de631e690817c253d6a9153
Result address : 0xa2a8854b1802d8cd5de631e690817c253d6a9153

让这个页面变得更好