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

多用戶賬戶密鑰

AccountKeyWeightedMultiSig 是一種賬戶密鑰類型,包含一個閾值和加權公鑰(WeightedPublicKeys),後者包含一個由公鑰及其權重組成的列表。

要使與 AccountKeyWeightedMultiSig 關聯的賬戶的交易有效,必須滿足以下條件: 已簽名公鑰的加權和應大於閾值。 _ 無效簽名不應包含在交易中。 * 已簽名公鑰的數量應少於加權公鑰的數量。

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

定義發件人的地址私人密鑰和其他加權多重簽名私人密鑰

定義接收方的地址

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

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

使用加權多重簽名私鑰和提供商創建 3 個不同的錢包

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

第一個錢包簽署交易,"populateTransaction "方法為交易對象添加更多參數,如gas、nonce...

第二個錢包簽署交易

第三個錢包中籤署交易,並將簽署的交易發送至 kaia 網絡

等待交易完成並打印收據

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

SignTxWithMultiSigExample.js

const { ethers } = require("ethers");
const { Wallet, TxType, AccountKeyType, parseKlay } = require("@kaiachain/ethers-ext/v5");
const senderAddr = "0x82c6a8d94993d49cfd0c1d30f0f8caa65782cc7e";
const senderPriv = "0xa32c30608667d43be2d652bede413f12a649dd1be93440878e7f712d51a6768a";
const senderNewPriv1 = "0xa32c30608667d43be2d652bede413f12a649dd1be93440878e7f712d51a6768a";
const senderNewPriv2 = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";
const senderNewPriv3 = "0xc9668ccd35fc20587aa37a48838b48ccc13cf14dd74c8999dd6a480212d5f7ac";
const recieverAddr = "0xc40b6909eb7085590e1c26cb3becc25368e249e9";
const provider = new ethers.providers.JsonRpcProvider("https://public-en-kairos.node.kaia.io");
const wallet1 = new Wallet(senderAddr, senderNewPriv1, provider);
const wallet2 = new Wallet(senderAddr, senderNewPriv2, provider);
const wallet3 = new Wallet(senderAddr, senderNewPriv3, provider);
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"),
gasLimit: 100000,
};
// The example senderAddr actually requires only 2 signature (threshold = 2),
// but we use 3 signatures to show different ways to sign a transaction.
// sign 1: First signer sign from the tx object
const populatedTx = await wallet1.populateTransaction(tx);
const rawTx1 = await wallet1.signTransaction(populatedTx);
console.log("rawTx1", rawTx1);
// sign 2: Middle signer sign from the rawTx
const rawTx2 = await wallet2.signTransaction(rawTx1);
console.log("rawTx2", rawTx2);
// sign 3: Last signer sign and send from the rawTx
const sentTx3 = await wallet3.sendTransaction(rawTx2);
console.log("sentTx3", sentTx3.hash);
const receipt = await sentTx3.wait();
console.log("receipt", receipt);
const addr = await provider.send("klay_recoverFromTransaction", [rawTx2, "latest"]);
console.log("recoveredAddr rpc", addr, addr.toLowerCase() === senderAddr.toLowerCase());
}
main().catch(console.error);

output

❯ js SignTxWithMultiSigExample.js
rawTx1 0x08f88676850ba43b7400830186a094c40b6909eb7085590e1c26cb3becc25368e249e9872386f26fc100009482c6a8d94993d49cfd0c1d30f0f8caa65782cc7ef847f8458207f6a008b987d8905dae51b856c478e6f7b49f01f5f2432a90c03d332e61d518100087a07bea2578790b7dabe9f2fe920d1f3183f48dc26d4ce752bf27964433317504e8
rawTx2 0x08f8cd76850ba43b7400830186a094c40b6909eb7085590e1c26cb3becc25368e249e9872386f26fc100009482c6a8d94993d49cfd0c1d30f0f8caa65782cc7ef88ef8458207f6a008b987d8905dae51b856c478e6f7b49f01f5f2432a90c03d332e61d518100087a07bea2578790b7dabe9f2fe920d1f3183f48dc26d4ce752bf27964433317504e8f8458207f6a052dcb9cfd9edf75ea60aa7eafce1a984209722c81a6c17f4f26d4b79adcc2f03a03ef04d7793009b90f7af7831f834ad2892678f0f10f8d6e02d6d76b3e4671a1d
sentTx3 0x1b3b4b8a177ead1602c5052d8c1145a2e9ffc53ac4ce208f4730177486726c6a
receipt {
to: '0xC40B6909EB7085590E1c26Cb3beCC25368e249E9',
from: '0x82C6a8D94993d49cfd0c1D30F0F8Caa65782cc7E',
contractAddress: null,
transactionIndex: 2,
gasUsed: BigNumber { _hex: '0xc738', _isBigNumber: true },
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
blockHash: '0x6f5fca2355230874808c4fe1b8459c6d61cf7ba22b864aeba4c3cf61d7a49b99',
transactionHash: '0x1b3b4b8a177ead1602c5052d8c1145a2e9ffc53ac4ce208f4730177486726c6a',
logs: [],
blockNumber: 152258186,
confirmations: 6,
cumulativeGasUsed: BigNumber { _hex: '0x05f70f', _isBigNumber: true },
effectiveGasPrice: BigNumber { _hex: '0x05d21dba00', _isBigNumber: true },
status: 1,
type: 0,
byzantium: true
}
recoveredAddr rpc 0x82c6a8d94993d49cfd0c1d30f0f8caa65782cc7e true

讓這個頁面變得更好