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

公共賬戶密鑰

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

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

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

定義接收方的地址

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

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

使用提供程序定義Web3 實例

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

創建具有 type:TxType.ValueTransfer 類型

用發件人的錢包簽署交易

簽署的交易發送到 kaia 網絡,並打印收據

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

SignTxWithPubkeyExample.js

const { Web3, TxType, toPeb } = require("@kaiachain/web3js-ext");
// Using senderPriv == senderNewPriv to execute this example repeatedly.
// But you might want to register a different private key.
const senderAddr = "0xfb60ded0ae96fe04eed6450aead860aa9d57128e";
const senderPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";
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 = {
type: TxType.ValueTransfer,
to: receiverAddr,
value: toPeb("0.01", "KLAY"),
from: senderAddr,
};
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 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

讓這個頁面變得更好