Skip to main content

Role-based Account Key

AccountKeyRoleBased represents a role-based key. If an account has an AccountKeyRoleBased object and the transaction type is one except account update, the validation process is done according to each roles like below:

Import necessary classes from the Web3j and kaia libraries(web3j-ext).

Create a Web3j instance with the specified BAOBAB_URL

Also, you can change the default provider. For example, using the alchemy provider.

Load different role-based credentials from theirs key pairs

Get the first credential address

Define any message to sign

Set the block number to be the latest block in the kaia network

Sign the message with all 3 role-based credentials in order and get the signature outputs as string

Recover the addresses from 3 message responses and compare them to the original from address

Shut down the Web3j instance

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