跳至主要內容
本頁面使用機器翻譯自英語,可能包含錯誤或不清楚的語言。如需最準確的信息,請參閱英文原文。由於更新頻繁,部分內容可能與英文原文有出入。請加入我們在 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

讓這個頁面變得更好