跳至主要內容
本頁面使用機器翻譯自英語,可能包含錯誤或不清楚的語言。如需最準確的信息,請參閱英文原文。由於更新頻繁,部分內容可能與英文原文有出入。請加入我們在 Crowdin 上的努力,幫助我們改進本頁面的翻譯。 (Crowdin translation page, Contributing guide)

Public Account Key

AccountKeyPublic 用於有一個公鑰的賬戶。 如果賬戶有一個 AccountKeyPublic 對象,交易驗證過程如下:

導入 ethers@kaiachain/ethers-ext 軟件包,在 ethers.js 上添加 kaia 功能

定義發件人的地址私人密鑰

定義接收方的地址

使用指定的 kairos 測試網 URL 設置提供程序。 以太坊中的提供者是訪問區塊鏈數據的只讀抽象。

此外,您還可以將提供商 URL 從 kairos 更改為 quicknode

私鑰提供者創建發件人錢包

使用 type.TxType.ValueTransfer 創建一個價值轉移事務,以便以後使用 klay_recoverFromTransaction 恢復:TxType.ValueTransfer",以便以後使用 "klay_recoverFromTransaction "恢復它

用發件人的錢包簽署交易,"populateTransaction "方法為交易對象添加更多參數,如gas、nonce...

簽署的交易發送至 kaia 網絡

等待交易完成並打印收據

使用 klay_recoverFromMessage 從已簽署的事務中恢復地址

SignTxWithPubkeyExample.js

const { ethers } = require("ethers");
const { Wallet, TxType, parseKlay } = require("@kaiachain/ethers-ext/v6");
const senderAddr = "0xe15cd70a41dfb05e7214004d7d054801b2a2f06b";
const senderPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";
const senderNewPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";
const recieverAddr = "0xc40b6909eb7085590e1c26cb3becc25368e249e9";
const provider = new ethers.JsonRpcProvider("https://public-en-kairos.node.kaia.io");
const newWallet = new Wallet(senderAddr, senderNewPriv, provider); // decoupled account
async function main() {
let tx = { // use Klaytn TxType to send transaction from Klaytn typed account
type: TxType.ValueTransfer,
from: senderAddr,
to: recieverAddr,
value: parseKlay("0.01"),
};
const populatedTx = await newWallet.populateTransaction(tx);
const rawTx = await newWallet.signTransaction(populatedTx);
console.log("rawTx", rawTx);
const sentTx = await newWallet.sendTransaction(tx);
console.log("sentTx", sentTx.hash);
const receipt = await sentTx.wait();
console.log("receipt", receipt);
const addr = await provider.send("klay_recoverFromTransaction", [rawTx, "latest"]);
console.log("recoveredAddr rpc", addr, addr.toLowerCase() === senderAddr.toLowerCase());
}
main().catch(console.error);

output

❯ js SignTxWithPubkeyExample.js
rawTx 0x08f8868188850ba43b740082cd1494c40b6909eb7085590e1c26cb3becc25368e249e9872386f26fc1000094e15cd70a41dfb05e7214004d7d054801b2a2f06bf847f8458207f5a05e2299e8d947e8b39d3715ba3945535ad05da364ebc1fa168b3fea675cb27f60a05adf38401f83409216fd29aa6fb5b39f7113c0a49e2bb3e2003cb60baf2df7ac
sentTx 0x751ae9e74895cd899a51b5deab2d5e72103d06468cf1701d5925784808962d7e
receipt {
to: '0xC40B6909EB7085590E1c26Cb3beCC25368e249E9',
from: '0xe15Cd70A41dfb05e7214004d7D054801b2a2f06b',
contractAddress: null,
transactionIndex: 0,
gasUsed: BigNumber { _hex: '0x5208', _isBigNumber: true },
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
blockHash: '0xc61b3f87368b60e95e061e991f2a51a238240a7e10dd746192e9f640395661e9',
transactionHash: '0x751ae9e74895cd899a51b5deab2d5e72103d06468cf1701d5925784808962d7e',
logs: [],
blockNumber: 152256785,
confirmations: 3,
cumulativeGasUsed: BigNumber { _hex: '0x5208', _isBigNumber: true },
effectiveGasPrice: BigNumber { _hex: '0x05d21dba00', _isBigNumber: true },
status: 1,
type: 0,
byzantium: true
}
recoveredAddr rpc 0xe15cd70a41dfb05e7214004d7d054801b2a2f06b true

讓這個頁面變得更好