跳至主要内容
本页面使用机器翻译自英语,可能包含错误或不清楚的语言。如需最准确的信息,请参阅英文原文。由于更新频繁,部分内容可能与英文原文有出入。请加入我们在 Crowdin 上的努力,帮助我们改进本页面的翻译。 (Crowdin translation page, Contributing guide)

基于角色的账户密钥

AccountKeyRoleBased 表示基于角色的密钥。 如果账户有一个基于账户密钥角色的对象,且交易类型为除账户更新外的,那么验证过程将根据每个角色进行,如下所示:

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

定义要更改的发件人地址、发件人私人密钥和新的基于角色的私人密钥

使用指定的 kairos 测试网 URL 设置提供程序。 web3js 中的提供者是访问区块链数据的只读抽象。

此外,您还可以将提供商 URL 从 kairos 更改为 quicknode

使用提供程序定义Web3 实例

使用更新者角色私钥和提供者创建发送者钱包

根据新的role-based私钥计算public keys

在** keys字段中使用计算出的基于role-based public keys**声明一个事务

发件人的钱包签署交易

向区块链发送已签名的 tx。 它将返回交易的收据

AccountUpdateWithRoleBased.js

const { Web3, TxType, AccountKeyType, getPublicKeyFromPrivate } = require("@kaiachain/web3js-ext");
const senderAddr = "0x334b4d3c775c45c59de54e9f0408cba25a1aece7";
const senderRoleTransactionPriv = "0xc9668ccd35fc20587aa37a48838b48ccc13cf14dd74c8999dd6a480212d5f7ac";
const senderRoleAccountUpdatePriv = "0x9ba8cb8f60044058a9e6f815c5c42d3a216f47044c61a1750b6d29ddc7f34bda";
const senderRoleFeePayerPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";
const provider = new Web3.providers.HttpProvider("https://public-en-kairos.node.kaia.io");
const web3 = new Web3(provider);
const updaterAccount = web3.eth.accounts.privateKeyToAccount(senderRoleAccountUpdatePriv);
async function main() {
const pub1 = getPublicKeyFromPrivate(senderRoleTransactionPriv);
const pub2 = getPublicKeyFromPrivate(senderRoleAccountUpdatePriv);
const pub3 = getPublicKeyFromPrivate(senderRoleFeePayerPriv);
console.log({ pub1, pub2, pub3 });
const tx = {
type: TxType.AccountUpdate,
from: senderAddr,
gasLimit: 100_000,
key: {
type: AccountKeyType.RoleBased,
keys: [
{ type: AccountKeyType.Public, key: pub1 }, // RoleTransaction
{ type: AccountKeyType.Public, key: pub2 }, // RoleAccountUpdate
{ type: AccountKeyType.Public, key: pub3 }, // RoleFeePayer
]
}
};
const signResult = await updaterAccount.signTransaction(tx);
console.log("signedTx", signResult.transactionHash);
const receipt = await web3.eth.sendSignedTransaction(signResult.rawTransaction);
console.log("receipt", receipt);
}
main().catch(console.error);

output

❯ js AccountUpdateWithRoleBased.js
{
pub1: '0x03f26489914098c5da51f0f646e3000da4d6197217df082b4f7ce1530f0a0cbf2a',
pub2: '0x0263021199702b9fefca617bdcb2a9ed4a810dfa8d270d4e804a1e778450e63ec3',
pub3: '0x03dc9dccbd788c00fa98f7f4082f2f714e799bc0c29d63f04d48b54fe6250453cd'
}
sentTx 0xcb8e1fc03088f2a00d44c31ce1c5f4913d3cf11403d60014a245ae44aa4c2a2f
receipt {
to: '0x5bD2fb3c21564C023A4A735935a2B7A238C4cCEA',
from: '0x5bD2fb3c21564C023A4A735935a2B7A238C4cCEA',
contractAddress: null,
transactionIndex: 1,
gasUsed: BigNumber { _hex: '0x013c68', _isBigNumber: true },
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
blockHash: '0xb9145a53ef85bc4b375de828d9c3387cadc6bcd8f81a8d8eaf4f0331a2d7a5c0',
transactionHash: '0xcb8e1fc03088f2a00d44c31ce1c5f4913d3cf11403d60014a245ae44aa4c2a2f',
logs: [],
blockNumber: 152203576,
confirmations: 2,
cumulativeGasUsed: BigNumber { _hex: '0x0377d6', _isBigNumber: true },
effectiveGasPrice: BigNumber { _hex: '0x05d21dba00', _isBigNumber: true },
status: 1,
type: 0,
byzantium: true
}

让这个页面变得更好