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

傳統賬戶密鑰

導入**@kaiachain/web3js-ext**軟件包,在 web3 上添加 kaia 功能

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

定義接收方的地址

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

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

使用提供程序定義Web3 實例

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

創建價值轉移交易,使用 toPeb 助手將金額從 KLAY 轉換為 Peb

用發件人的錢包簽署交易

簽署的交易發送至 kaia 網絡

使用 web3.klay.recoverFromTransaction 從已簽署的事務中恢復地址

SignTxWithLegacyExample.js

const { Web3, toPeb } = require("@kaiachain/web3js-ext");
const senderAddr = "0xb2ba72e1f84b7b8cb15487a2bf20328f2cf40c25";
const senderPriv = "0xebceaca693ea3740231be94f38af6090d3aded336725d26a09b7d14e8e485e1e";
const receiverAddr = "0xc40b6909eb7085590e1c26cb3becc25368e249e9";
const provider = new Web3.providers.HttpProvider("https://public-en-kairos.node.kaia.io");
const web3 = new Web3(provider);
const senderAccount = web3.eth.accounts.privateKeyToAccount(senderPriv);
async function main() {
let tx = {
from: senderAddr,
to: receiverAddr,
value: toPeb("0.01", "KLAY"),
};
const signResult = await senderAccount.signTransaction(tx);
console.log("signedTx", signResult.transactionHash);
const receipt = await web3.eth.sendSignedTransaction(signResult.rawTransaction);
console.log("receipt", receipt);
const sig = signResult.signature;
const addr2 = await web3.klay.recoverFromTransaction(senderAddr, sig, "latest");
console.log("recoveredAddr rpc", addr2, addr2.toLowerCase() === senderAddr);
}
main().catch(console.error);

output

❯ js SignTxWithLegacyExample.js
rawTx 0x08f87e05850ba43b740082cd1494c40b6909eb7085590e1c26cb3becc25368e249e98094b2ba72e1f84b7b8cb15487a2bf20328f2cf40c25f847f8458207f5a070d2c5f2dd52d6a3b7bba65594b2c875b8ce0473e16b06b43d5c2ec9ebfcd467a048af2fc51f75961a9cebfb1685b12d99db143bbd42a76f9c50970b807e1010b9
sentTx 0xecb117338d7a0e7e9444886ebdab5d0e14fd1b02fa476fee839a2fc3b105f391
receipt {
to: '0xC40B6909EB7085590E1c26Cb3beCC25368e249E9',
from: '0xb2ba72e1f84b7B8Cb15487A2bf20328F2cF40c25',
contractAddress: null,
transactionIndex: 1,
gasUsed: BigNumber { _hex: '0x5208', _isBigNumber: true },
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
blockHash: '0x7eae10aa2fcd3c42ac1705b63ad025f972a222bb71d65feeb0000d92c2e73732',
transactionHash: '0xecb117338d7a0e7e9444886ebdab5d0e14fd1b02fa476fee839a2fc3b105f391',
logs: [],
blockNumber: 152256170,
confirmations: 3,
cumulativeGasUsed: BigNumber { _hex: '0x029636', _isBigNumber: true },
effectiveGasPrice: BigNumber { _hex: '0x05d21dba00', _isBigNumber: true },
status: 1,
type: 0,
byzantium: true
}
recoveredAddr rpc 0xb2ba72e1f84b7b8cb15487a2bf20328f2cf40c25 true

讓這個頁面變得更好