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

Role-based Account Key

AccountKeyRoleBased 表示基于角色的密钥。 如果账户有一个** AccountKeyRoleBased对象,且交易类型为除账户更新外的**,那么验证过程将根据每个角色进行,如下所示:

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

使用指定的 BAOBAB_URL 创建 Web3j 实例

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

从他们的密钥对中加载不同的基于角色的证书

获取第一个证书地址

定义要签署的任何信息

设置区块编号为 kaia 网络中的最新区块

使用所有 3 个基于角色的凭据依次签署信息,并以字符串形式输出签名

从 3 个报文回复中恢复地址,并与原始发件人地址进行比较

关闭 Web3j 实例

SignMsgWithRoleBasedExample.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 org.web3j.crypto.KlayCredentials;
import org.web3j.crypto.KlaySignatureData;
import org.web3j.crypto.Sign.SignatureData;
import org.web3j.protocol.http.HttpService;
import org.web3j.protocol.kaia.Web3j;
import org.web3j.protocol.kaia.core.method.response.KlayRecoverFromMessageResponse;
/**
*
*/
public class SignMsgWithRoleBasedExample 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);
KlayCredentials credentials2 = KlayCredentials.create(keySample.ROLEBASED_KEY_updatekey,
keySample.ROLEBASED_KEY_address);
KlayCredentials credentials3 = KlayCredentials.create(keySample.ROLEBASED_KEY_feepayer,
keySample.ROLEBASED_KEY_address);
String from = credentials1.getAddress();
String message = "0xdeadbeef";
String blockNumber = "latest";
SignatureData signature1 = KlaySignatureData.signPrefixedMessage(message, credentials1);
String result1 = KlaySignatureData.getSignatureString(signature1);
SignatureData signature2 = KlaySignatureData.signPrefixedMessage(message, credentials2);
String result2 = KlaySignatureData.getSignatureString(signature2);
SignatureData signature3 = KlaySignatureData.signPrefixedMessage(message, credentials3);
String result3 = KlaySignatureData.getSignatureString(signature3);
KlayRecoverFromMessageResponse response1 = web3j
.klayRecoverFromMessage(from, message, result1, blockNumber)
.send();
KlayRecoverFromMessageResponse response2 = web3j
.klayRecoverFromMessage(from, message, result2, blockNumber)
.send();
KlayRecoverFromMessageResponse response3 = web3j
.klayRecoverFromMessage(from, message, result3, blockNumber)
.send();
System.out.println("Original address : " + from);
System.out.println("Result address for transaction key : " + response1.getResult());
System.out.println("Result address for update key : " + response2.getResult());
System.out.println("Result address for feepayer key : " + response3.getResult());
web3j.shutdown();
}
}

output

❯ java SignMsgWithRoleBasedExample.java
Original address : 0x5bd2fb3c21564c023a4a735935a2b7a238c4ccea
Result address for transaction key : 0xe15cd70a41dfb05e7214004d7d054801b2a2f06b
Result address for update key : 0x5bd2fb3c21564c023a4a735935a2b7a238c4ccea
Result address for feepayer key : 0xa2a8854b1802d8cd5de631e690817c253d6a9153

让这个页面变得更好