Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add restore-account-contract command #2561

Merged
merged 4 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions packages/utilities/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ The uitilites package provides various wallet related command line utilities.

_Click on a command for more information and examples._

| Command | Description |
| ----------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------- |
| [`disable-2fa`](#disable-2fa) | disable 2fa using the provided seedphrase |
| Command | Description |
| ------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------- |
| [`disable-2fa`](#disable-2fa) | disable 2fa using the provided seedphrase |
| [`restore-account-contract`](#restore-account-contract)| restore account contract to a given `blockHash` |


## Disable 2fa
Expand All @@ -21,4 +22,15 @@ This script allows the user to disable NEAR Wallet 2fa using the provided seedph

```shell=
NEAR_WALLET_ENV=mainnet node index.js disable-2fa --accountId="testAccount.near" --seedPhrase='lorem ipsum dolor sit amet consectetur adipiscing elit nunc efficitur est'
```
---
## Restore account contract

This script restores account code back to a given `blockHash` by deploying a contract. It pulls contract code from the given archival node
* arguments: `accountId` `seedPhrase` `nodeUrl` `blockHash`

### Usage

```shell=
NEAR_WALLET_ENV=testnet node index.js restore-account-contract --accountId='testAccount.testnet' --seedPhrase='lorem ipsum dolor sit amet consectetur adipiscing elit nunc efficitur est' --blockHash='9c5tk9kdTzgAgejgmnLCRu9yJ1Jp1LjFqtgB4SHEzzeB' --nodeUrl='https://sample-archival-testnet-node-url.com/'
```
57 changes: 57 additions & 0 deletions packages/utilities/commands/restoreAccountContract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const { InMemoryKeyStore } = require("near-api-js/lib/key_stores");
const { parseSeedPhrase } = require("near-seed-phrase");
const { Connection, KeyPair } = require("near-api-js");
const { Account } = require("near-api-js/lib/account");

module.exports = {
command: `restore-account-contract`,
builder: (yargs) =>
yargs
.option("accountId", {
desc: "accountId to disable the 2fa on",
type: "string",
required: true,
})
.option("seedPhrase", {
desc: "seedPhrase for the accountId",
type: "string",
required: true,
})
.option("nodeUrl", {
desc: "Url for the archival rpc node to pull the code from",
type: "string",
required: true,
})
.option("blockHash", {
desc: `block hash to pull code from and deploy to the account`,
type: "string",
required: true,
}),
handler: restoreAccountContract,
};

async function restoreAccountContract({ accountId, seedPhrase, nodeUrl, blockHash }) {
const networkId = process.env.NEAR_WALLET_ENV || "mainnet";

const keyStore = new InMemoryKeyStore();
await keyStore.setKey(networkId, accountId, KeyPair.fromString(parseSeedPhrase(seedPhrase).secretKey));

const connection = Connection.fromConfig({
networkId,
provider: {
type: "JsonRpcProvider",
args: { url: nodeUrl },
},
signer: { type: "InMemorySigner", keyStore },
});

const { code_base64 } = await connection.provider.query({
request_type: "view_code",
account_id: accountId,
blockId: blockHash,
finality: 'final'
});

const account = new Account(connection, accountId);
await account.deployContract(new Uint8Array(Buffer.from(code_base64, 'base64')));
}
2 changes: 2 additions & 0 deletions packages/utilities/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const yargs = require("yargs/yargs");
const { hideBin } = require("yargs/helpers");
const disable2fa = require("./commands/disable2fa");
const restoreAccountContract = require("./commands/restoreAccountContract");

yargs(hideBin(process.argv))
.command(disable2fa)
.command(restoreAccountContract)
.parse();