Nhảy tới nội dung
This page uses machine translation from English, which may contain errors or unclear language. For the most accurate information, please see the original English version. Some content may be in the original English due to frequent updates. Help us improve this page's translation by joining our effort on Crowdin. (Crowdin translation page, Contributing guide)

Public Account Key

AccountKeyPublic is used for accounts having one public key. If an account has an AccountKeyPublic object, the transaction validation process is done 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 a credential from the public key's key pair and get its address

Define any message to sign

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

Sign the message with using KlaySignatureData.signPrefixedMessage

Recover the address and compare it to the original address

Shut down the Web3j instance

SignMsgWithPubkeyExample.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 SignMsgWithPubkeyExample implements keySample {
/**
*
*/
public static void run() throws Exception {
Web3j web3j = Web3j.build(new HttpService(keySample.BAOBAB_URL));
KlayCredentials credentials1 = KlayCredentials.create(keySample.PUBLIC_KEY_privkey,
keySample.PUBLIC_KEY_address);
String from = credentials1.getAddress();
String message = "0xdeadbeef";
String blockNumber = "latest";
SignatureData signature = KlaySignatureData.signPrefixedMessage(message, credentials1);
String result = KlaySignatureData.getSignatureString(signature);
KlayRecoverFromMessageResponse response = web3j.klayRecoverFromMessage(from, message, result, blockNumber)
.send();
System.out.println("Original address : " + from);
System.out.println("Result address : " + response.getResult());
web3j.shutdown();
}
}

output

❯ java SignMsgWithPubkeyExample.java
Original address : 0xe15cd70a41dfb05e7214004d7d054801b2a2f06b
Result address : 0xa2a8854b1802d8cd5de631e690817c253d6a9153

Cải thiện trang này