本頁面使用機器翻譯 自英語,可能包含錯誤或不清楚的語言。如需最準確的信息,請參閱英文原文。由於更新頻繁,部分內容可能與英文原文有出入。請加入我們在 Crowdin 上的努力,幫助我們改進本頁面的翻譯。 (Crowdin translation page, Contributing guide)
基於角色的賬戶密鑰
AccountKeyRoleBased 表示基於角色的密鑰。 如果賬戶有一個基於賬戶密鑰角色的對象,且交易類型為除賬戶更新外的,那麼驗證過程將根據每個角色進行,如下所示:
從 Web3j 和 kaia 庫(web3j-ext
)中導入必要的類。
使用指定的 BAOBAB_URL 創建 Web3j 實例
此外,您還可以更改默認提供商。 例如,使用alchemy提供商。
用私人密鑰創建憑證,私人密鑰是交易角色密鑰
燃氣價格和gas 限值設置
將發件人地址設置為已加載憑據的地址
從網絡中獲取chain ID
將接收器地址設置為任何有效地址
獲取發件人地址的nonce值
設置要傳輸的值
將交易類型設置為 VALUE_TRANSFER
為價值轉移創建原始交易
簽署交易
將簽署的交易發送至 kaia 網絡
從簽名交易中恢復發件人地址,並將其與發件人地址進行比較
關閉 Web3j 實例
SignTxWithRoleBasedExample.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 SignTxWithRoleBasedExample implements keySample { /** * */ public static void run() throws Exception { Web3j web3j = Web3j.build(new HttpService(keySample.BAOBAB_URL)); KlayCredentials credentials1 = KlayCredentials.create(keySample.ROLEBASED_KEY_transactionkey, keySample.ROLEBASED_KEY_address); 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 SignTxWithRoleBasedExample.javaTxHash : 0x75b1fbfcaeb0c3fa7a0738e628a010b85b6514394ed08d25df4b055aef7d0e58Original address : 0x5bd2fb3c21564c023a4a735935a2b7a238c4cceaResult address : 0x5bd2fb3c21564c023a4a735935a2b7a238c4ccea