Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
};
// 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);
}
}
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);
});
};
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 = {};
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 {
static fromMnemonic(mnemonic, path, wordlist) {
if (!path) {
path = defaultPath;
}
return new Wallet(HDNode.fromMnemonic(mnemonic, null, wordlist).derivePath(path));
}
}
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);
}
static fromMnemonic(mnemonic: string, path?: string, wordlist?: Wordlist): Wallet {
if (!path) { path = defaultPath; }
return new Wallet(HDNode.fromMnemonic(mnemonic, null, wordlist).derivePath(path));
}
}
private async getHDNode(withPassword: boolean = true): Promise {
return HDNode.fromMnemonic(this.mnemonicPhrase, withPassword ? this.password : undefined);
}
}
private async getHDNode(withPassword: boolean = true): Promise {
return HDNode.fromMnemonic(this.mnemonicPhrase, withPassword ? this.password : undefined);
}
}