Skip to content

Commit

Permalink
Fix seed phrase so it works with masterAccount
Browse files Browse the repository at this point in the history
  • Loading branch information
mehtaphysical committed May 6, 2021
1 parent 21893e6 commit ef6cbcd
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2,327 deletions.
20 changes: 11 additions & 9 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 { 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 };
};
Loading

0 comments on commit ef6cbcd

Please sign in to comment.