How to use @ethersproject/hdnode - 10 common examples

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 / json-wallets / lib / keystore.js View on Github external
}
                                }
                                account = {
                                    _isKeystoreAccount: true,
                                    address: address,
                                    privateKey: bytes_1.hexlify(privateKey)
                                };
                                // Version 0.1 x-ethers metadata must contain an encrypted mnemonic phrase
                                if (utils_1.searchPath(data, "x-ethers/version") === "0.1") {
                                    mnemonicCiphertext = utils_1.looseArrayify(utils_1.searchPath(data, "x-ethers/mnemonicCiphertext"));
                                    mnemonicIv = utils_1.looseArrayify(utils_1.searchPath(data, "x-ethers/mnemonicCounter"));
                                    mnemonicCounter = new aes_js_1.default.Counter(mnemonicIv);
                                    mnemonicAesCtr = new aes_js_1.default.ModeOfOperation.ctr(mnemonicKey, mnemonicCounter);
                                    path = utils_1.searchPath(data, "x-ethers/path") || hdnode_1.defaultPath;
                                    entropy = bytes_1.arrayify(mnemonicAesCtr.decrypt(mnemonicCiphertext));
                                    mnemonic = hdnode_1.entropyToMnemonic(entropy);
                                    node = hdnode_1.HDNode.fromMnemonic(mnemonic).derivePath(path);
                                    if (node.privateKey != account.privateKey) {
                                        throw new Error("mnemonic mismatch");
                                    }
                                    account.mnemonic = node.mnemonic;
                                    account.path = node.path;
                                }
                                return [2 /*return*/, new KeystoreAccount(account)];
                            });
                        });
github ethers-io / ethers.js / packages / json-wallets / src.ts / keystore.ts View on Github external
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 / 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 / lib / keystore.js View on Github external
if (address_1.getAddress(check) !== address) {
                                        throw new Error("address mismatch");
                                    }
                                }
                                account = {
                                    _isKeystoreAccount: true,
                                    address: address,
                                    privateKey: bytes_1.hexlify(privateKey)
                                };
                                // Version 0.1 x-ethers metadata must contain an encrypted mnemonic phrase
                                if (utils_1.searchPath(data, "x-ethers/version") === "0.1") {
                                    mnemonicCiphertext = utils_1.looseArrayify(utils_1.searchPath(data, "x-ethers/mnemonicCiphertext"));
                                    mnemonicIv = utils_1.looseArrayify(utils_1.searchPath(data, "x-ethers/mnemonicCounter"));
                                    mnemonicCounter = new aes_js_1.default.Counter(mnemonicIv);
                                    mnemonicAesCtr = new aes_js_1.default.ModeOfOperation.ctr(mnemonicKey, mnemonicCounter);
                                    path = utils_1.searchPath(data, "x-ethers/path") || hdnode_1.defaultPath;
                                    entropy = bytes_1.arrayify(mnemonicAesCtr.decrypt(mnemonicCiphertext));
                                    mnemonic = hdnode_1.entropyToMnemonic(entropy);
                                    node = hdnode_1.HDNode.fromMnemonic(mnemonic).derivePath(path);
                                    if (node.privateKey != account.privateKey) {
                                        throw new Error("mnemonic mismatch");
                                    }
                                    account.mnemonic = node.mnemonic;
                                    account.path = node.path;
                                }
                                return [2 /*return*/, new KeystoreAccount(account)];
                            });
                        });
github ethers-io / ethers.js / packages / json-wallets / lib / keystore.js View on Github external
// the options are optional, so adjust the call as needed
    if (typeof (options) === "function" && !progressCallback) {
        progressCallback = options;
        options = {};
    }
    if (!options) {
        options = {};
    }
    var privateKey = bytes_1.arrayify(account.privateKey);
    var passwordBytes = utils_1.getPassword(password);
    var entropy = null;
    var path = account.path;
    if (account.mnemonic) {
        entropy = bytes_1.arrayify(hdnode_1.mnemonicToEntropy(account.mnemonic));
        if (!path) {
            path = hdnode_1.defaultPath;
        }
    }
    var client = options.client;
    if (!client) {
        client = "ethers.js";
    }
    // Check/generate the salt
    var salt = null;
    if (options.salt) {
        salt = bytes_1.arrayify(options.salt);
    }
    else {
        salt = random_1.randomBytes(32);
        ;
    }
    // Override initialization vector
github ethers-io / ethers.js / packages / json-wallets / lib / keystore.js View on Github external
function encrypt(account, password, options, progressCallback) {
    try {
        if (address_1.getAddress(account.address) !== transactions_1.computeAddress(account.privateKey)) {
            throw new Error("address/privateKey mismatch");
        }
        if (account.mnemonic != null) {
            var node = hdnode_1.HDNode.fromMnemonic(account.mnemonic).derivePath(account.path || hdnode_1.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 / json-wallets / lib / keystore.js View on Github external
}
                                account = {
                                    _isKeystoreAccount: true,
                                    address: address,
                                    privateKey: bytes_1.hexlify(privateKey)
                                };
                                // Version 0.1 x-ethers metadata must contain an encrypted mnemonic phrase
                                if (utils_1.searchPath(data, "x-ethers/version") === "0.1") {
                                    mnemonicCiphertext = utils_1.looseArrayify(utils_1.searchPath(data, "x-ethers/mnemonicCiphertext"));
                                    mnemonicIv = utils_1.looseArrayify(utils_1.searchPath(data, "x-ethers/mnemonicCounter"));
                                    mnemonicCounter = new aes_js_1.default.Counter(mnemonicIv);
                                    mnemonicAesCtr = new aes_js_1.default.ModeOfOperation.ctr(mnemonicKey, mnemonicCounter);
                                    path = utils_1.searchPath(data, "x-ethers/path") || hdnode_1.defaultPath;
                                    entropy = bytes_1.arrayify(mnemonicAesCtr.decrypt(mnemonicCiphertext));
                                    mnemonic = hdnode_1.entropyToMnemonic(entropy);
                                    node = hdnode_1.HDNode.fromMnemonic(mnemonic).derivePath(path);
                                    if (node.privateKey != account.privateKey) {
                                        throw new Error("mnemonic mismatch");
                                    }
                                    account.mnemonic = node.mnemonic;
                                    account.path = node.path;
                                }
                                return [2 /*return*/, new KeystoreAccount(account)];
                            });
                        });