Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
fix: add seedphrase to create-account
Browse files Browse the repository at this point in the history
  • Loading branch information
gagdiez committed Feb 29, 2024
1 parent b21460a commit 1f59164
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ near login
> Creates an account using an existing account or a faucet service to pay for the account's creation and initial balance.
- arguments: `accountId`
- options: `--initialBalance`, `--useFaucet`, `--useAccount`, `--signWithLedger`, `--ledgerPath`, `--useLedgerPK`, `--PkLedgerPath`
- options: `--initialBalance`, `--useFaucet`, `--useAccount`, `--seedPhrase`, `--publicKey`, `--signWithLedger`, `--ledgerPath`, `--useLedgerPK`, `--PkLedgerPath`

**Examples:**:

Expand Down
12 changes: 12 additions & 0 deletions commands/account/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { assertCredentials, storeCredentials } = require('../../utils/credentials
const { DEFAULT_NETWORK } = require('../../config');
const chalk = require('chalk');
const { getPublicKeyForPath } = require('../../utils/ledger');
const { parseSeedPhrase } = require('near-seed-phrase');

module.exports = {
command: 'create-account <new-account-id>',
Expand Down Expand Up @@ -34,6 +35,11 @@ module.exports = {
type: 'string',
required: false
})
.option('seedPhrase', {
desc: 'seedPhrase from which to derive the account\'s publicKey',
type: 'string',
required: false
})
.option('signWithLedger', {
alias: ['useLedgerKey'],
desc: 'Use Ledger for signing',
Expand Down Expand Up @@ -105,6 +111,12 @@ async function create(options) {
let keyPair;
let publicKey = options.useLedgerPK ? await getPublicKeyForPath(options.PkLedgerPath) : options.publicKey;

if (options.seedPhrase) {
const parsed = parseSeedPhrase(options.seedPhrase);
keyPair = KeyPair.fromString(parsed.secretKey);
publicKey = keyPair.getPublicKey();
}

if (!publicKey) {
// If no public key is specified, create a random one
keyPair = KeyPair.fromRandom('ed25519');
Expand Down
14 changes: 14 additions & 0 deletions test/test_account_creation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ testaccount1=test-ac-${timestamp}-1.testnet
testaccount2=test-ac-${timestamp}-2.testnet
testaccount3=test-ac-${timestamp}-3.testnet
testaccount4=test-ac-${timestamp}-4.testnet
testaccount5=test-ac-${timestamp}-5.testnet
zerobalance=test-ac-${timestamp}-z.testnet
withbalance=test-ac-${timestamp}-y.testnet

Expand All @@ -14,6 +15,19 @@ tla=${timestamp}${timestamp}${timestamp}12
# Can create a pre-funded account
./bin/near create-account $testaccount1 --useFaucet

# Can create a pre-funded account with a seedPhrase
./bin/near create-account $testaccount5 --useFaucet --seedPhrase "crisp clump stay mean dynamic become fashion mail bike disorder chronic sight"

RESULT=$(./bin/near list-keys $testaccount5)
EXPECTED=".*GPnL8k4MV1hLccB5rkNiihVAEEmQX3BTDJnmW1T7ZDXG.*"
if [[ ! "$RESULT" =~ $EXPECTED ]]; then
echo FAILURE Unexpected output from near list-keys
echo Got: $RESULT
echo Expected: $EXPECTED
exit 1
fi


# Can create an account with a given public key & balance
./bin/near create-account sub.$testaccount1 --accountId $testaccount1 --publicKey "78MziB9aTNsu19MHHVrfWy762S5mAqXgCB6Vgvrv9uGV" --initialBalance 0.01

Expand Down

0 comments on commit 1f59164

Please sign in to comment.