-
Notifications
You must be signed in to change notification settings - Fork 91
/
seed-phrase.js
25 lines (20 loc) · 1.1 KB
/
seed-phrase.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
const { parseSeedPhrase } = require('near-seed-phrase');
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, 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 { 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 };
};