How to use the @ethersproject/hdnode.HDNode.fromMnemonic function in @ethersproject/hdnode

To help you get started, we’ve selected a few @ethersproject/hdnode examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ethers-io / ethers.js / packages / json-wallets / src.ts / keystore.ts View on Github external
};

        // Version 0.1 x-ethers metadata must contain an encrypted mnemonic phrase
        if (searchPath(data, "x-ethers/version") === "0.1") {
            const mnemonicCiphertext = looseArrayify(searchPath(data, "x-ethers/mnemonicCiphertext"));
            const mnemonicIv = looseArrayify(searchPath(data, "x-ethers/mnemonicCounter"));

            const mnemonicCounter = new aes.Counter(mnemonicIv);
            const mnemonicAesCtr = new aes.ModeOfOperation.ctr(mnemonicKey, mnemonicCounter);

            const path = searchPath(data, "x-ethers/path") || defaultPath;

            const entropy = arrayify(mnemonicAesCtr.decrypt(mnemonicCiphertext));
            const mnemonic = entropyToMnemonic(entropy);

            const node = HDNode.fromMnemonic(mnemonic).derivePath(path);

            if (node.privateKey != account.privateKey) {
                throw new Error("mnemonic mismatch");
            }

            account.mnemonic = node.mnemonic;
            account.path = node.path;
        }

        return new KeystoreAccount(account);
    }
github ethers-io / ethers.js / packages / json-wallets / lib.esm / keystore.js View on Github external
}
                const account = {
                    _isKeystoreAccount: true,
                    address: address,
                    privateKey: hexlify(privateKey)
                };
                // Version 0.1 x-ethers metadata must contain an encrypted mnemonic phrase
                if (searchPath(data, "x-ethers/version") === "0.1") {
                    const mnemonicCiphertext = looseArrayify(searchPath(data, "x-ethers/mnemonicCiphertext"));
                    const mnemonicIv = looseArrayify(searchPath(data, "x-ethers/mnemonicCounter"));
                    const mnemonicCounter = new aes.Counter(mnemonicIv);
                    const mnemonicAesCtr = new aes.ModeOfOperation.ctr(mnemonicKey, mnemonicCounter);
                    const path = searchPath(data, "x-ethers/path") || defaultPath;
                    const entropy = arrayify(mnemonicAesCtr.decrypt(mnemonicCiphertext));
                    const mnemonic = entropyToMnemonic(entropy);
                    const node = HDNode.fromMnemonic(mnemonic).derivePath(path);
                    if (node.privateKey != account.privateKey) {
                        throw new Error("mnemonic mismatch");
                    }
                    account.mnemonic = node.mnemonic;
                    account.path = node.path;
                }
                return new KeystoreAccount(account);
            });
        };
github ethers-io / ethers.js / packages / json-wallets / src.ts / keystore.ts View on Github external
export function encrypt(account: ExternallyOwnedAccount, password: Bytes | string, options?: EncryptOptions, progressCallback?: ProgressCallback): Promise {

    try {
        if (getAddress(account.address) !== computeAddress(account.privateKey)) {
            throw new Error("address/privateKey mismatch");
        }

        if (account.mnemonic != null){
            const node = HDNode.fromMnemonic(account.mnemonic).derivePath(account.path || defaultPath);

            if (node.privateKey != account.privateKey) {
                throw new Error("mnemonic mismatch");
            }
        } else if (account.path != null) {
            throw new Error("cannot specify path without mnemonic");
        }

    } catch (e) {
        return Promise.reject(e);
    }

    // the options are optional, so adjust the call as needed
    if (typeof(options) === "function" && !progressCallback) {
        progressCallback = options;
        options = {};
github ethers-io / ethers.js / packages / wallet / src.ts / index.ts View on Github external
if (isAccount(privateKey)) {
            const signingKey = new SigningKey(privateKey.privateKey);
            defineReadOnly(this, "_signingKey", () => signingKey);
            defineReadOnly(this, "address", computeAddress(this.publicKey));

            if (this.address !== getAddress(privateKey.address)) {
                logger.throwArgumentError("privateKey/address mismatch", "privateKey", "[REDCACTED]");
            }

            if (privateKey.mnemonic != null) {
                const mnemonic = privateKey.mnemonic;
                const path = privateKey.path || defaultPath;
                defineReadOnly(this, "_mnemonic", () => mnemonic);
                defineReadOnly(this, "path", privateKey.path);
                const node = HDNode.fromMnemonic(mnemonic).derivePath(path);
                if (computeAddress(node.privateKey) !== this.address) {
                    logger.throwArgumentError("mnemonic/address mismatch", "privateKey", "[REDCACTED]");
                }
            } else {
                defineReadOnly(this, "_mnemonic", (): string => null);
                defineReadOnly(this, "path", null);
            }


        } else {
            if (SigningKey.isSigningKey(privateKey)) {
                if (privateKey.curve !== "secp256k1") {
                    logger.throwArgumentError("unsupported curve; must be secp256k1", "privateKey", "[REDACTED]");
                }
                defineReadOnly(this, "_signingKey", () => privateKey);
            } else {
github ethers-io / ethers.js / packages / wallet / lib.esm / index.js View on Github external
static fromMnemonic(mnemonic, path, wordlist) {
        if (!path) {
            path = defaultPath;
        }
        return new Wallet(HDNode.fromMnemonic(mnemonic, null, wordlist).derivePath(path));
    }
}
github ethers-io / ethers.js / packages / wallet / lib.esm / index.js View on Github external
constructor(privateKey, provider) {
        logger.checkNew(new.target, Wallet);
        super();
        if (isAccount(privateKey)) {
            const signingKey = new SigningKey(privateKey.privateKey);
            defineReadOnly(this, "_signingKey", () => signingKey);
            defineReadOnly(this, "address", computeAddress(this.publicKey));
            if (this.address !== getAddress(privateKey.address)) {
                logger.throwArgumentError("privateKey/address mismatch", "privateKey", "[REDCACTED]");
            }
            if (privateKey.mnemonic != null) {
                const mnemonic = privateKey.mnemonic;
                const path = privateKey.path || defaultPath;
                defineReadOnly(this, "_mnemonic", () => mnemonic);
                defineReadOnly(this, "path", privateKey.path);
                const node = HDNode.fromMnemonic(mnemonic).derivePath(path);
                if (computeAddress(node.privateKey) !== this.address) {
                    logger.throwArgumentError("mnemonic/address mismatch", "privateKey", "[REDCACTED]");
                }
            }
            else {
                defineReadOnly(this, "_mnemonic", () => null);
                defineReadOnly(this, "path", null);
            }
        }
        else {
            if (SigningKey.isSigningKey(privateKey)) {
                if (privateKey.curve !== "secp256k1") {
                    logger.throwArgumentError("unsupported curve; must be secp256k1", "privateKey", "[REDACTED]");
                }
                defineReadOnly(this, "_signingKey", () => privateKey);
            }
github ethers-io / ethers.js / packages / wallet / src.ts / index.ts View on Github external
static fromMnemonic(mnemonic: string, path?: string, wordlist?: Wordlist): Wallet {
        if (!path) { path = defaultPath; }
        return new Wallet(HDNode.fromMnemonic(mnemonic, null, wordlist).derivePath(path));
    }
}
github Mrtenz / FindETH / packages / web / src / wallets / MnemonicPhrase.ts View on Github external
private async getHDNode(withPassword: boolean = true): Promise {
    return HDNode.fromMnemonic(this.mnemonicPhrase, withPassword ? this.password : undefined);
  }
}
github Mrtenz / FindETH / src / wallets / MnemonicPhrase.ts View on Github external
private async getHDNode(withPassword: boolean = true): Promise {
    return HDNode.fromMnemonic(this.mnemonicPhrase, withPassword ? this.password : undefined);
  }
}