如何在创建账户后为账户添加一个密钥,例如,为多协议合同,使用near shell、near repl或near-api-js?

问题描述 投票:0回答:1

最近,我创建了一个账户,并部署了一个需要2个确认(签名)的multisig合同,但我只有一个完整的访问密钥。我无法对合同进行任何操作,包括将确认次数设置为1... 我如何使用near shell、near repl或near-api-js添加密钥?

near create_account msig --masterAccount account.testnet --initialBalance 50
near deploy --accountId msig --wasmFile ./contract/res/multisig.wasm
near call msig new '{"num_confirmations":2}' --account_id msig

这时,multisig已经被部署并实例化了,用 num_confirmations = 2但只有一把钥匙 完全访问账户密钥。

我如何添加另一个密钥,以便我可以确认multisig请求?

security blockchain smartcontracts nearprotocol
1个回答
0
投票

首先,我必须生成一个新的密钥添加到账户中。我打算把钥匙命名为 msig.lak1为我的账户添加一个新的有限访问密钥(LAK)。msig:

near generate-key msig.lak1

复制生成的公钥。

以供参考和以后签署tss,比如 confirm 在multisig合约上,secretKey可以在这里找到。code ~/.near-credentials/default/msig.lak1.json

现在我需要把这个加到账户上 msig.

鉴于 near shell 不提供为账户添加新密钥的功能,我将不得不使用 near repl。

启动 near repl 与账户ID msig 我的Multisig合约账户只有一个完整的访问密钥。

near repl --accountId msig
>

更新: (感谢@vlad-grichina评论)

await account.addKey("PUBLIC KEY FROM GENERATE KEY", contractName, 'confirm')

由于这是一个单一动作的TX。

上一个: (如果捆绑行动)

现在我将创建一个 addKey 交易,并使用 msig 帐户。

const contractName = "msig"
const result = account.signAndSendTransaction('msig', [
    nearAPI.transactions.addKey(nearAPI.utils.PublicKey.from("PUBLIC KEY FROM GENERATE KEY"), nearAPI.transactions.functionCallAccessKey(contractName, ['confirm'], null)),
])

我可以用近似shell的方式验证密钥是否被添加。

near keys msig

现在我可以用秘钥来确认一个multisig合同请求:

// in my JS app
await window.keyStore.setKey(
    NETWORK_ID, contractName,
    // from ~/.near-credentials/default/msig.lak1.json
    nearApi.KeyPair.fromString(secretKey)
)
const contract = new nearApi.Contract(window.contractAccount, contractName, {
    changeMethods: ['confirm'],
    sender: contractName
})
...
contract.confirm({ request_id: X }) // confirms request 

我还可以在explorer中查看添加访问密钥是否成功:https:/explorer.testnet.near.orgaccountsmsig.add access key image from explorer

© www.soinside.com 2019 - 2024. All rights reserved.