From 6b579bb7c4cca9f0ff24d613e21eab76b9be9fe2 Mon Sep 17 00:00:00 2001 From: Vladimir Grichina Date: Mon, 23 Dec 2019 13:11:19 -0800 Subject: [PATCH 1/4] Extract create_account command into separate file --- bin/near-cli.js | 28 +--------------------- commands/create-account.js | 49 ++++++++++++++++++++++++++++++++++++++ index.js | 16 ------------- 3 files changed, 50 insertions(+), 43 deletions(-) create mode 100644 commands/create-account.js diff --git a/bin/near-cli.js b/bin/near-cli.js index 77673cb8..a074d83c 100644 --- a/bin/near-cli.js +++ b/bin/near-cli.js @@ -3,32 +3,6 @@ const main = require('../'); const exitOnError = require('../utils/exit-on-error'); // For account: -const createAccount = { - command: 'create_account ', - desc: 'create a new developer account', - builder: (yargs) => yargs - .option('accountId', { - desc: 'Unique identifier for the newly created account', - type: 'string', - required: true - }) - .option('masterAccount', { - desc: 'Account used to create requested account.', - type: 'string', - required: true - }) - .option('publicKey', { - desc: 'Public key to initialize the account with', - type: 'string', - required: false - }) - .option('initialBalance', { - desc: 'Number of tokens to transfer to newly created account', - type: 'string', - default: '10' - }), - handler: exitOnError(main.createAccount) -}; const login = { command: 'login', @@ -207,7 +181,7 @@ yargs // eslint-disable-line desc: 'Unique identifier for the account', type: 'string', }) - .command(createAccount) + .command(require('../commands/create-account')) .command(viewAccount) .command(deleteAccount) .command(keys) diff --git a/commands/create-account.js b/commands/create-account.js new file mode 100644 index 00000000..637fe88f --- /dev/null +++ b/commands/create-account.js @@ -0,0 +1,49 @@ + +const exitOnError = require('../utils/exit-on-error'); +const connect = require('../utils/connect'); +const { KeyPair } = require('nearlib'); + +module.exports = { + command: 'create_account ', + desc: 'create a new developer account', + builder: (yargs) => yargs + .option('accountId', { + desc: 'Unique identifier for the newly created account', + type: 'string', + required: true + }) + .option('masterAccount', { + desc: 'Account used to create requested account.', + type: 'string', + required: true + }) + .option('publicKey', { + desc: 'Public key to initialize the account with', + type: 'string', + required: false + }) + .option('initialBalance', { + desc: 'Number of tokens to transfer to newly created account', + type: 'string', + default: '10' + }), + handler: exitOnError(createAccount) +}; + +async function createAccount(options) { + let near = await connect(options); + let keyPair; + let publicKey; + if (options.publicKey) { + publicKey = options.publicKey; + } else { + keyPair = await KeyPair.fromRandom('ed25519'); + publicKey = keyPair.getPublicKey(); + } + // TODO: Needs to pass initialBalance converted from NEAR tokens + await near.createAccount(options.accountId, publicKey); + if (keyPair) { + await near.connection.signer.keyStore.setKey(options.networkId, options.accountId, keyPair); + } + console.log(`Account ${options.accountId} for network "${options.networkId}" was created.`); +}; \ No newline at end of file diff --git a/index.js b/index.js index 443cee82..abaef73a 100644 --- a/index.js +++ b/index.js @@ -59,22 +59,6 @@ exports.callViewFunction = async function(options) { }; // For account: -exports.createAccount = async function(options) { - let near = await connect(options); - let keyPair; - let publicKey; - if (options.publicKey) { - publicKey = options.publicKey; - } else { - keyPair = await KeyPair.fromRandom('ed25519'); - publicKey = keyPair.getPublicKey(); - } - await near.createAccount(options.accountId, publicKey); - if (keyPair) { - await near.connection.signer.keyStore.setKey(options.networkId, options.accountId, keyPair); - } - console.log(`Account ${options.accountId} for network "${options.networkId}" was created.`); -}; exports.login = async function(options) { if (!options.walletUrl) { From 9139d0c6a83f7f38c700550397d681290869b5d2 Mon Sep 17 00:00:00 2001 From: Vladimir Grichina Date: Mon, 23 Dec 2019 14:12:43 -0800 Subject: [PATCH 2/4] Convert --initialBalance from NEAR tokens to integer units This also changes default value to 0.1 NEAR --- commands/create-account.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/commands/create-account.js b/commands/create-account.js index 637fe88f..85243057 100644 --- a/commands/create-account.js +++ b/commands/create-account.js @@ -1,7 +1,7 @@ const exitOnError = require('../utils/exit-on-error'); const connect = require('../utils/connect'); -const { KeyPair } = require('nearlib'); +const { KeyPair, utils } = require('nearlib'); module.exports = { command: 'create_account ', @@ -25,12 +25,14 @@ module.exports = { .option('initialBalance', { desc: 'Number of tokens to transfer to newly created account', type: 'string', - default: '10' + default: '0.1' }), handler: exitOnError(createAccount) }; async function createAccount(options) { + options.initialBalance = utils.format.parseNearAmount(options.initialBalance); + // NOTE: initialBalance is passed as part of config here let near = await connect(options); let keyPair; let publicKey; @@ -40,7 +42,6 @@ async function createAccount(options) { keyPair = await KeyPair.fromRandom('ed25519'); publicKey = keyPair.getPublicKey(); } - // TODO: Needs to pass initialBalance converted from NEAR tokens await near.createAccount(options.accountId, publicKey); if (keyPair) { await near.connection.signer.keyStore.setKey(options.networkId, options.accountId, keyPair); From a8cb47b3f8cfa11a5ab706d39cf0aab898af1fd8 Mon Sep 17 00:00:00 2001 From: Vladimir Grichina Date: Mon, 23 Dec 2019 14:14:50 -0800 Subject: [PATCH 3/4] yarn fix --- commands/create-account.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/create-account.js b/commands/create-account.js index 85243057..ca6f4fbb 100644 --- a/commands/create-account.js +++ b/commands/create-account.js @@ -47,4 +47,4 @@ async function createAccount(options) { await near.connection.signer.keyStore.setKey(options.networkId, options.accountId, keyPair); } console.log(`Account ${options.accountId} for network "${options.networkId}" was created.`); -}; \ No newline at end of file +} \ No newline at end of file From a54493e4a2e30d00b828b263167b7b7ba0e4a1fd Mon Sep 17 00:00:00 2001 From: Vladimir Grichina Date: Mon, 23 Dec 2019 14:59:48 -0800 Subject: [PATCH 4/4] Update expected amount in account creation test --- test/test_account_operations.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_account_operations.sh b/test/test_account_operations.sh index 6d9864e5..78639a2f 100755 --- a/test/test_account_operations.sh +++ b/test/test_account_operations.sh @@ -11,7 +11,7 @@ echo Create account echo Get account state RESULT=$(../bin/near state $testaccount | strip-ansi) echo $RESULT -EXPECTED=".+Account $testaccount.+amount:.+'10'.+ " +EXPECTED=".+Account $testaccount.+amount:.+'100000000000000000000000'.+ " if [[ ! "$RESULT" =~ $EXPECTED ]]; then echo FAILURE Unexpected output from near view exit 1