본문으로 건너뛰기
이 페이지는 영어로 된 기계 번역을 사용하므로 오류나 불명확한 언어가 포함될 수 있습니다. 가장 정확한 정보는 영어 원문을 참조하시기 바랍니다. 잦은 업데이트로 인해 일부 콘텐츠는 원래 영어로 되어 있을 수 있습니다. Crowdin에서 이 페이지의 번역을 개선하는 데 동참하여 도움을 주세요. (Crowdin translation page, Contributing guide)

Legacy Account Key

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 private key 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 and convert the signature to string

Recover the address and compare it to the original address

Shut down the Web3j instance

SignMsgWithLegacyExample.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 SignMsgWithLegacyExample implements keySample {
/**
*
*/
public static void run() throws Exception {
Web3j web3j = Web3j.build(new HttpService(keySample.BAOBAB_URL));
KlayCredentials credentials1 = KlayCredentials.create(keySample.LEGACY_KEY_privkey);
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 SignMsgWithLegacyExample.java
Original address : 0xa2a8854b1802d8cd5de631e690817c253d6a9153
Result address : 0xa2a8854b1802d8cd5de631e690817c253d6a9153

페이지를 개선해 주세요