Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function createFromMnemonic (numberOfAccounts, mnemonic) {
if (this.length > 0) {
throw Error('This wallet has already been created!')
}
if (numberOfAccounts === undefined) {
numberOfAccounts = 1
}
if (mnemonic === undefined) {
mnemonic = bip39.generateMnemonic()
}
if (bip39.validateMnemonic(mnemonic)) {
let seed = bip39.mnemonicToSeed(mnemonic)
// contains masternode extended priv key and extended pub key
let masternode = hdkey.fromMasterSeed(seed)
for (let i = 0; i < numberOfAccounts; ++i) {
// m / purpose' / coin_type' / account' / change / address_index
let child = masternode.derive(`m/44'/60'/0'/0/${i}`)
let privkeyHex = child.privateKey.toString('hex')
var privateKey = this._accounts.privateKeyToAccount(add0x(privkeyHex)).privateKey
this.add(privateKey)
}
} else {
throw Error(`Mnemonic was not valid: ${mnemonic}`)
}
return this
}
createAddressPrivatekey() {
const t0 = performance.now();
if (this.mnemonic == '') {
this.mnemonic = bip39.generateMnemonic(); // generates string
}
const seed = bip39.mnemonicToSeed(this.mnemonic); // creates seed buffer
const root = hdkey.fromMasterSeed(seed);
// Create address for eth ...
const addrNode = root.derive(StringHelper.format('m/44\'/{0}\'/0\'/0/0', this.coinType));
const pubKey = ethUtil.privateToPublic(addrNode._privateKey);
const addr = ethUtil.publicToAddress(pubKey).toString('hex');
const address = ethUtil.toChecksumAddress(addr);
const privateKey = addrNode._privateKey.toString('hex');
this.address = address;
this.privateKey = privateKey;
this.chainId = this.network == Ethereum.Network.Mainnet ? 1 : 4;
const t1 = performance.now();
getXpub() {
if (this._xpub) {
return this._xpub; // cache hit
}
const mnemonic = this.secret;
const seed = bip39.mnemonicToSeed(mnemonic);
const root = bip32.fromSeed(seed);
const path = "m/0'";
const child = root.derivePath(path).neutered();
this._xpub = child.toBase58();
return this._xpub;
}
getXpub() {
if (this._xpub) {
return this._xpub; // cache hit
}
const mnemonic = this.secret;
const seed = bip39.mnemonicToSeed(mnemonic);
const root = bitcoin.bip32.fromSeed(seed);
const path = "m/44'/0'/0'";
const child = root.derivePath(path).neutered();
this._xpub = child.toBase58();
return this._xpub;
}
_getInternalAddressByIndex(index) {
index = index * 1; // cast to int
if (this.internal_addresses_cache[index]) return this.internal_addresses_cache[index]; // cache hit
const mnemonic = this.secret;
const seed = bip39.mnemonicToSeed(mnemonic);
const root = bip32.fromSeed(seed);
const path = "m/0'/1/" + index;
const child = root.derivePath(path);
const address = bitcoinjs.payments.p2pkh({
pubkey: child.publicKey,
}).address;
return (this.internal_addresses_cache[index] = address);
}
async init(force = false) {
if (!force && this.masterWallet !== null) return
const seed = await bip39.mnemonicToSeed(this.mnemonic)
this.masterKey = hdkey.fromMasterSeed(seed)
this.masterWallet = this.masterKey.getWallet()
const masterAddress = this.masterWallet.getChecksumAddressString()
this.accounts[masterAddress] = new Account({
txCount: await this.txCount(masterAddress),
pendingCount: 0,
wallet: this.masterWallet
})
logger.info(`Initialized master key for account ${masterAddress}`)
for (let i = 0; i < this._childrenToCreate; i++) {
const childKey = this.masterKey.derivePath(getBIP44Path(i))
const childWallet = childKey.getWallet()
const address = childWallet.getChecksumAddressString()
public static createFromMnemonicAndPath(mnemonic: string, derivationPath: string) {
const seed = bip39.mnemonicToSeed(mnemonic);
return Account.createFromSeedAndPath(seed, derivationPath);
}
generateKeypair(keySeed) {
if (typeof keySeed == "undefined" || keySeed == "") return new driver.Ed25519Keypair();
return new driver.Ed25519Keypair(bip39.mnemonicToSeed(keySeed).slice(0, 32));
}
}
const mnemonicItem = await showQuickPick(
mnemonicItems,
{ placeHolder: Constants.placeholders.selectMnemonicExtractKey, ignoreFocusOut: true },
);
const mnemonic = mnemonicItem.extended;
if (!mnemonic) {
Telemetry.sendEvent('TruffleCommands.getPrivateKeyFromMnemonic.mnemonicFileHaveNoText');
window.showErrorMessage(Constants.errorMessageStrings.MnemonicFileHaveNoText);
return;
}
try {
const buffer = await bip39.mnemonicToSeed(mnemonic);
const key = hdkey.fromMasterSeed(buffer);
const childKey = key.derive('m/44\'/60\'/0\'/0/0');
const privateKey = childKey.privateKey.toString('hex');
await vscodeEnvironment.writeToClipboard(privateKey);
window.showInformationMessage(Constants.informationMessage.privateKeyWasCopiedToClipboard);
} catch (error) {
Telemetry.sendException(error);
window.showErrorMessage(Constants.errorMessageStrings.InvalidMnemonic);
}
Telemetry.sendEvent('TruffleCommands.getPrivateKeyFromMnemonic.commandFinished');
}
}
export function decryptMnemonicToPrivKey(
phrase: string,
pass: string | undefined,
path: string,
address: string
): Buffer {
phrase = phrase.trim();
address = stripHexPrefixAndLower(address);
if (!validateMnemonic(phrase)) {
throw new Error('Invalid mnemonic');
}
const seed = mnemonicToSeed(phrase, pass);
const derived = HDkey.fromMasterSeed(seed).derive(path);
const dPrivKey = derived.privateKey;
const dAddress = privateToAddress(dPrivKey).toString('hex');
if (dAddress !== address) {
throw new Error(`Derived ${dAddress}, expected ${address}`);
}
return dPrivKey;
}