Skip to content
This repository was archived by the owner on Oct 4, 2024. It is now read-only.
Open
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
30 changes: 27 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ near generate-key example.testnet --seedPhrase="cow moon right send now cool den

#### 1) add a `full access` key

- arguments: `accountId` `publicKey`
- arguments: `accountId` `--access-key`

**Example:**

Expand All @@ -402,7 +402,7 @@ near add-key example-acct.testnet Cxg2wgFYrdLTEkMu6j5D6aEZqTb3kXbmJygS48ZKbo1S

#### 2) add a `function access` key

- arguments: `accountId` `publicKey` `--contract-id`
- arguments: `accountId` `--access-key` `--contract-id`
- options: `--method-names` `--allowance`

> `accountId` is the account you are adding the key to
Expand All @@ -418,7 +418,7 @@ near add-key example-acct.testnet Cxg2wgFYrdLTEkMu6j5D6aEZqTb3kXbmJygS48ZKbo1S
**Example:**

```bash
near add-key example-acct.testnet GkMNfc92fwM1AmwH1MTjF4b7UZuceamsq96XPkHsQ9vi --contract-id example-contract.testnet --method-names example_method --allowance 30000000000
near add-key example-acct.testnet --access-key GkMNfc92fwM1AmwH1MTjF4b7UZuceamsq96XPkHsQ9vi --contract-id example-contract.testnet --method-names example_method --allowance 30000000000
```

<details>
Expand All @@ -433,6 +433,30 @@ near add-key example-acct.testnet GkMNfc92fwM1AmwH1MTjF4b7UZuceamsq96XPkHsQ9vi -
</p>
</details>


#### 3) add a `full access` key based on a seed phrase

- arguments: `accountId` `--seedPhrase="[your seed phrase]"`

**Example:**

```bash
near add-key example-acct.testnet --seedPhrase="cow moon right send now cool dense quark pretty see light after"
```

<details>
<summary><strong>Example Response</strong></summary>
<p>

Adding seed phrase as full access key = "cow moon right send now cool dense quark pretty see light after" to example-acct.testnet.
Transaction Id EwU1ooEvkR42HvGoJHu5ou3xLYT3JcgQwFV3fAwevGJg
To see the transaction in the transaction explorer, please open this url in your browser
https://explorer.testnet.near.org/transactions/EwU1ooEvkR42HvGoJHu5ou3xLYT3JcgQwFV3fAwevGJg

</p>
</details>


---

### `near delete-key`
Expand Down
26 changes: 23 additions & 3 deletions commands/add-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ const { utils } = require('near-api-js');
const checkCredentials = require('../utils/check-credentials');

module.exports = {
command: 'add-key <account-id> <access-key>',
command: 'add-key <account-id>',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed there is another syntax to possibly use the existing arg as an optional parameter

Suggested change
command: 'add-key <account-id>',
command: 'add-key <account-id> [access-key]',

example in the generate-key command

This would allow to not break the current input of the command (and thus the existing docs). But I let maintainers decide for the best solution :)

Copy link
Author

@tituszban tituszban Aug 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's a pretty good idea. Other thoughts?

desc: 'Add an access key to given account',
builder: (yargs) => yargs
.option('access-key', {
desc: 'Public key to add (base58 encoded)',
type: 'string',
required: true,
required: false,
})
.option('contract-id', {
desc: 'Limit access key to given contract (if not provided - will create full access key)',
Expand All @@ -33,9 +33,29 @@ module.exports = {

async function addAccessKey(options) {
await checkCredentials(options.accountId, options.networkId, options.keyStore);
console.log(`Adding ${options.contractId ? 'function call access' : 'full access'} key = ${options.accessKey} to ${options.accountId}.`);
const near = await connect(options);
const account = await near.account(options.accountId);

if(options.seedPhrase){
if(options.contractId){
console.log("Seed phrase key must be a full access key");
return;
}
console.log(`Adding seed phrase as full access key to ${options.accountId}`);

const result = await account.addKey(options.seedPhrasePublicKey);
inspectResponse.prettyPrintResponse(result, options);

return;
}

if(!options.accessKey){
console.log("access-key to be added must be specified");
return;
}

console.log(`Adding ${options.contractId ? 'function call access' : 'full access'} key = ${options.accessKey} to ${options.accountId}.`);

const allowance = utils.format.parseNearAmount(options.allowance);
const result = await account.addKey(options.accessKey, options.contractId, options.methodNames, allowance);
inspectResponse.prettyPrintResponse(result, options);
Expand Down
4 changes: 2 additions & 2 deletions middleware/seed-phrase.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ module.exports = async function useSeedPhrase({ seedPhrase, seedPath, keyStore,
const seedPhraseKeystore = new InMemoryKeyStore();
const seedPhraseAccountId = masterAccount ? masterAccount : accountId || implicitAccountId(publicKey);

await keyStore.setKey(networkId, seedPhraseAccountId, KeyPair.fromString(secretKey));
await seedPhraseKeystore.setKey(networkId, seedPhraseAccountId, KeyPair.fromString(secretKey));
if(keyStore instanceof MergeKeyStore) keyStore.keyStores.push(seedPhraseKeystore);

return { keyStore, accountId };
return { keyStore, accountId, seedPhrasePublicKey: publicKey };
};