Skip to content

Commit

Permalink
Merge pull request #739 from near/fix/seed-phrase-create-account
Browse files Browse the repository at this point in the history
Fix seed phrase so it works with masterAccount
  • Loading branch information
mehtaphysical authored May 11, 2021
2 parents 6d7d59e + 389e021 commit 6222bbd
Show file tree
Hide file tree
Showing 15 changed files with 429 additions and 386 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.DS_Store
node_modules
tmp-project
neardev/*
package-lock.json
.idea
3 changes: 2 additions & 1 deletion commands/create-account.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,11 @@ async function createAccount(options) {
keyRootPath = near.connection.signer.keyStore.keyStores[0].keyDir;
}
keyFilePath = `${keyRootPath}/${options.networkId}/${options.accountId}.json`;
console.log(`Saving key to '${keyFilePath}'`);
await near.connection.signer.keyStore.setKey(options.networkId, options.accountId, keyPair);
}

// Create account
console.log(`Saving key to '${keyFilePath}'`);
try {
const response = await near.createAccount(options.accountId, publicKey);
inspectResponse.prettyPrintResponse(response, options);
Expand Down
22 changes: 12 additions & 10 deletions middleware/seed-phrase.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
const { parseSeedPhrase } = require('near-seed-phrase');
const { utils: { KeyPair }, InMemorySigner } = require('near-api-js');
const { InMemoryKeyStore } = require('near-api-js/lib/key_stores');
const { utils: { KeyPair } } = require('near-api-js');
const { InMemoryKeyStore, MergeKeyStore } = require('near-api-js/lib/key_stores');

const implicitAccountId = require('../utils/implicit-accountid');

// near ... --seedPhrase="phrase" --seedPath="m/44'/397'/0'"
// near generate-key --seedPhrase="phrase"
module.exports = async function useSeedPhrase({ seedPhrase, seedPath, publicKey, accountId, networkId }, yargs) {
module.exports = async function useSeedPhrase({ seedPhrase, seedPath, keyStore, accountId, masterAccount, networkId }, yargs) {
if (!seedPhrase) {
return;
}
if (yargs.usingLedger) {
throw new Error('Can not use both --useLedgerKey and --seedPhrase at the same time');
}
const result = parseSeedPhrase(seedPhrase, seedPath);
publicKey = result.publicKey;
let keyStore = new InMemoryKeyStore();
accountId = accountId || implicitAccountId(publicKey);
await keyStore.setKey(networkId, accountId, KeyPair.fromString(result.secretKey));
let signer = new InMemorySigner(keyStore);
return { signer, publicKey, accountId };
const { publicKey, secretKey } = parseSeedPhrase(seedPhrase, seedPath);

const seedPhraseKeystore = new InMemoryKeyStore();
const seedPhraseAccountId = masterAccount ? masterAccount : accountId || implicitAccountId(publicKey);

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

return { keyStore, accountId };
};
1 change: 1 addition & 0 deletions neardev/dev-account
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dev-1620395864376.test.near
1 change: 1 addition & 0 deletions neardev/dev-account.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CONTRACT_NAME=dev-1620395864376.test.near
1 change: 1 addition & 0 deletions neardev/shared-test-staging/test.near.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"account_id":"test.near","private_key":"ed25519:2wyRcSwSuHtRVmkMCGjPwnzZmQLeXLzLLyED1NDMt4BjnKgQL6tF85yBx6Jr26D2dUNeC716RBoTxntVHsegogYw"}
1 change: 1 addition & 0 deletions neardev/shared-test/test.near.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"account_id":"test.near","private_key":"ed25519:2wyRcSwSuHtRVmkMCGjPwnzZmQLeXLzLLyED1NDMt4BjnKgQL6tF85yBx6Jr26D2dUNeC716RBoTxntVHsegogYw"}
16 changes: 14 additions & 2 deletions test/index.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,28 @@ export NODE_ENV=${NODE_ENV:-test}
OVERALL_RESULT=0
mkdir ~/.near-config
echo '{"trackingEnabled":false}' > ~/.near-config/settings.json

SUCCESS=""
FAIL=""

for test in ./test/test_*; do
echo ""
echo "Running $test"
"$test"
if [ $? -ne 0 ]; then
echo "$test FAIL"
echo -e "$test \033[0;31m FAIL \033[0m"
FAIL="$FAIL$test \033[0;31m FAIL \033[0m\n"
OVERALL_RESULT=1
else
echo "$test SUCCESS"
echo -e "$test \033[0;32m SUCCESS \033[0m"
SUCCESS="$SUCCESS$test \033[0;32m SUCCESS \033[0m\n"
fi
done

echo ""
echo "Results:"
echo ""
echo -e $SUCCESS
echo -e $FAIL

exit $OVERALL_RESULT
Binary file added test/res/guest_book.wasm
Binary file not shown.
17 changes: 12 additions & 5 deletions test/test_account_creation.sh
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
#!/bin/bash
set -x

timestamp=$(date +%s)
testaccount=testaccount$timestamp.test.near
testaccount=testaccount$timestamp$RANDOM.test.near

ERROR=$(./bin/near create-account $testaccount --masterAccount test.far 2>&1 >/dev/null)
echo $ERROR
EXPECTED_ERROR=".+New account doesn't share the same top-level account.+ "
if [[ ! "$ERROR" =~ $EXPECTED_ERROR ]]; then
echo FAILURE Unexpected output creating account with different master account
exit 1
fi

ERROR=$(./bin/near create-account tooshortfortla --masterAccount test.far 2>&1 >/dev/null)
echo $ERROR
EXPECTED_ERROR=".+Top-level accounts must be at least.+ "
if [[ ! "$ERROR" =~ $EXPECTED_ERROR ]]; then
echo FAILURE Unexpected output when creating a short top-level account
exit 1
fi

echo Create account with seed phrase
./bin/near create-account $testaccount --publicKey="Au4RC7YHfZuhxK8DnXDrizCSEXXc9mc9teVm4Nbtv6Tj" > /dev/null
RESULT=$(./bin/near create-account create-me.$testaccount --masterAccount=$testaccount --seedPhrase="cross teach omit obvious riot echo parrot member memory wide obscure ball" --initialBalance=1 -v | ./node_modules/.bin/strip-ansi)
EXPECTED=".+Account create-me.$testaccount for network .+ was created.+"
if [[ ! "$RESULT" =~ $EXPECTED ]]; then
echo FAILURE \"Create account with seed phrase\" Unexpected output from create-account
exit 1
fi

./bin/near delete $testaccount test.near > /dev/null
16 changes: 7 additions & 9 deletions test/test_account_operations.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
#!/bin/bash
set -ex
rm -rf tmp-project
yarn create near-app tmp-project
cd tmp-project
set -e

timestamp=$(date +%s)
testaccount=testaccount$timestamp.test.near
testaccount=testaccount$timestamp$RANDOM.test.near

echo Create account
../bin/near create-account $testaccount
./bin/near create-account $testaccount

echo Get account state
RESULT=$(../bin/near state $testaccount -v | ../node_modules/.bin/strip-ansi)
echo $RESULT
RESULT=$(./bin/near state $testaccount -v | ./node_modules/.bin/strip-ansi)
EXPECTED=".+Account $testaccount.+amount:.+'100000000000000000000000000'.+ "
if [[ ! "$RESULT" =~ $EXPECTED ]]; then
echo FAILURE Unexpected output from near view
exit 1
fi

../bin/near delete $testaccount test.near
./bin/near delete $testaccount test.near
30 changes: 11 additions & 19 deletions test/test_contract.sh
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
#!/bin/bash
set -ex
rm -rf tmp-project

yarn create near-app tmp-project

cd tmp-project
set -e

timestamp=$(date +%s)
testaccount=testaccount$timestamp.test.near
../bin/near create-account $testaccount

echo Building contract
yarn install
yarn build:contract
testaccount=testaccount$timestamp$RANDOM.test.near
./bin/near create-account $testaccount

echo Deploying contract
../bin/near deploy --accountId=$testaccount --wasmFile=out/main.wasm
./bin/near deploy --accountId=$testaccount --wasmFile=./test/res/guest_book.wasm

echo Deploying contract to temporary accountId
# TODO: Specify helperUrl in project template
yes | ../bin/near dev-deploy
yes | ./bin/near dev-deploy ./test/res/guest_book.wasm > /dev/null

echo Calling functions
../bin/near call $testaccount setGreeting '{"message":"TEST"}' --accountId=test.near
./bin/near call $testaccount addMessage '{"text":"TEST"}' --accountId=test.near > /dev/null

RESULT=$(../bin/near view $testaccount getGreeting '{"accountId":"test.near"}' --accountId=test.near -v)
RESULT=$(./bin/near view $testaccount getMessages '{}' --accountId=test.near -v)
TEXT=$RESULT
EXPECTED='TEST'
if [[ ! $TEXT =~ .*$EXPECTED.* ]]; then
Expand All @@ -33,12 +24,13 @@ if [[ ! $TEXT =~ .*$EXPECTED.* ]]; then
fi

# base64-encoded '{"message":"BASE64ROCKS"}'
../bin/near call $testaccount setGreeting --base64 'eyJtZXNzYWdlIjoiQkFTRTY0Uk9DS1MifQ==' --accountId=test.near
./bin/near call $testaccount addMessage --base64 'eyJ0ZXh0IjoiVEVTVCJ9' --accountId=test.near > /dev/null

RESULT=$(../bin/near view $testaccount getGreeting '{"accountId":"test.near"}' --accountId=test.near -v)
RESULT=$(./bin/near view $testaccount getMessages '{}' --accountId=test.near -v)
# TODO: Refactor asserts
TEXT=$RESULT
EXPECTED='BASE64ROCKS'
echo $RESULT
EXPECTED="[ { premium: false, sender: 'test.near', text: 'TEST' }, { premium: false, sender: 'test.near', text: 'TEST' } ]"
if [[ ! $TEXT =~ .*$EXPECTED.* ]]; then
echo FAILURE Unexpected output from near call: $RESULT
exit 1
Expand Down
28 changes: 12 additions & 16 deletions test/test_deploy_init_contract.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
#!/bin/bash
set -ex
rm -rf tmp-project
# create-near-app only used to get access to test.near key file
yarn create near-app tmp-project
cd tmp-project
set -e

timestamp=$(date +%s)
testaccount=testaccount$timestamp.test.near
testaccount=testaccount$timestamp$RANDOM.test.near

echo Creating account
../bin/near create-account $testaccount
./bin/near create-account $testaccount > /dev/null

echo Deploying contract without init method
../bin/near deploy --accountId $testaccount --wasmFile ../test/res/fungible_token.wasm
ERROR=$(../bin/near view $testaccount get_balance '{"owner_id": "test.near"}' -v 2>&1 >/dev/null | ../node_modules/.bin/strip-ansi)
echo $ERROR
./bin/near deploy --accountId $testaccount --wasmFile ./test/res/fungible_token.wasm > /dev/null
ERROR=$(./bin/near view $testaccount get_balance '{"owner_id": "test.near"}' -v 2>&1 >/dev/null | ./node_modules/.bin/strip-ansi)
EXPECTED_ERROR=".+Fun token should be initialized before usage+"
if [[ ! "$ERROR" =~ $EXPECTED_ERROR ]]; then
echo FAILURE Expected message requiring initialization of contract
Expand All @@ -22,11 +19,10 @@ else
fi

# Delete account, remake, redeploy
../bin/near delete $testaccount test.near
../bin/near create-account $testaccount
../bin/near deploy --accountId $testaccount --wasmFile ../test/res/fungible_token.wasm --initFunction new --initArgs '{"owner_id": "test.near", "total_supply": "1000000"}'
RESULT=$(../bin/near view $testaccount get_balance '{"owner_id": "test.near"}' -v | ../node_modules/.bin/strip-ansi)
echo $RESULT
./bin/near delete $testaccount test.near > /dev/null
./bin/near create-account $testaccount > /dev/null
./bin/near deploy --accountId $testaccount --wasmFile ./test/res/fungible_token.wasm --initFunction new --initArgs '{"owner_id": "test.near", "total_supply": "1000000"}' > /dev/null
RESULT=$(./bin/near view $testaccount get_balance '{"owner_id": "test.near"}' -v | ./node_modules/.bin/strip-ansi)
if [[ $RESULT -ne 1000000 ]]; then
echo FAILURE Expected balance sent in initialization args
exit 1
Expand All @@ -35,4 +31,4 @@ else
fi

# Clean up by deleting account, sending back to test.near
../bin/near delete $testaccount test.near
./bin/near delete $testaccount test.near > /dev/null
11 changes: 3 additions & 8 deletions test/test_generate_key.sh
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@

#!/bin/bash
set -ex
set -e

KEY_FILE=~/.near-credentials/$NODE_ENV/generate-key-test.json
rm -f "$KEY_FILE"
echo "Testing generating-key: new key"

echo "Testing generating-key: new key"
RESULT=$(./bin/near generate-key generate-key-test --networkId $NODE_ENV -v)
echo $RESULT

if [[ ! -f "${KEY_FILE}" ]]; then
echo "FAILURE Key file doesn't exist"
exit 1
fi

EXPECTED=".*Key pair with ed25519:.+ public key.*"
if [[ ! "$RESULT" =~ $EXPECTED ]]; then
echo FAILURE Unexpected output from near generate-key
exit 1
fi

echo "Testing generating-key: key for account already exists"

RESULT2=$(./bin/near generate-key generate-key-test --networkId $NODE_ENV -v)
echo $RESULT2

EXPECTED2=".*Account has existing key pair with ed25519:.+ public key.*"
if [[ ! "$RESULT2" =~ $EXPECTED2 ]]; then
echo FAILURE Unexpected output from near generate-key when key already exists
Expand Down
Loading

0 comments on commit 6222bbd

Please sign in to comment.