ethers.js/packages/wallet/src.ts/index.ts

203 lines
8.4 KiB
TypeScript
Raw Normal View History

2019-05-15 01:25:46 +03:00
"use strict";
import { getAddress } from "@ethersproject/address";
import { Provider, TransactionRequest } from "@ethersproject/abstract-provider";
import { ExternallyOwnedAccount, Signer, TypedDataDomain, TypedDataField, TypedDataSigner } from "@ethersproject/abstract-signer";
2019-05-15 01:25:46 +03:00
import { arrayify, Bytes, BytesLike, concat, hexDataSlice, isHexString, joinSignature, SignatureLike } from "@ethersproject/bytes";
import { hashMessage, _TypedDataEncoder } from "@ethersproject/hash";
2020-01-19 05:09:02 +03:00
import { defaultPath, HDNode, entropyToMnemonic, Mnemonic } from "@ethersproject/hdnode";
2019-05-15 01:25:46 +03:00
import { keccak256 } from "@ethersproject/keccak256";
import { defineReadOnly, resolveProperties } from "@ethersproject/properties";
2019-05-15 01:25:46 +03:00
import { randomBytes } from "@ethersproject/random";
import { SigningKey } from "@ethersproject/signing-key";
import { decryptJsonWallet, decryptJsonWalletSync, encryptKeystore, ProgressCallback } from "@ethersproject/json-wallets";
2020-02-07 02:21:34 +03:00
import { computeAddress, recoverAddress, serialize, UnsignedTransaction } from "@ethersproject/transactions";
import { Wordlist } from "@ethersproject/wordlists";
2019-05-15 01:25:46 +03:00
2019-08-02 01:04:06 +03:00
import { Logger } from "@ethersproject/logger";
import { version } from "./_version";
const logger = new Logger(version);
2019-05-15 01:25:46 +03:00
function isAccount(value: any): value is ExternallyOwnedAccount {
return (value != null && isHexString(value.privateKey, 32) && value.address != null);
}
2020-01-19 05:09:02 +03:00
function hasMnemonic(value: any): value is { mnemonic: Mnemonic } {
const mnemonic = value.mnemonic;
return (mnemonic && mnemonic.phrase);
}
export class Wallet extends Signer implements ExternallyOwnedAccount, TypedDataSigner {
2019-05-15 01:25:46 +03:00
readonly address: string;
readonly provider: Provider;
// Wrapping the _signingKey and _mnemonic in a getter function prevents
// leaking the private key in console.log; still, be careful! :)
readonly _signingKey: () => SigningKey;
2020-01-19 05:09:02 +03:00
readonly _mnemonic: () => Mnemonic;
2019-05-15 01:25:46 +03:00
constructor(privateKey: BytesLike | ExternallyOwnedAccount | SigningKey, provider?: Provider) {
super();
if (isAccount(privateKey)) {
const signingKey = new SigningKey(privateKey.privateKey);
2019-05-15 01:25:46 +03:00
defineReadOnly(this, "_signingKey", () => signingKey);
defineReadOnly(this, "address", computeAddress(this.publicKey));
if (this.address !== getAddress(privateKey.address)) {
logger.throwArgumentError("privateKey/address mismatch", "privateKey", "[REDACTED]");
2019-05-15 01:25:46 +03:00
}
2020-01-19 05:09:02 +03:00
if (hasMnemonic(privateKey)) {
const srcMnemonic = privateKey.mnemonic;
defineReadOnly(this, "_mnemonic", () => (
{
phrase: srcMnemonic.phrase,
path: srcMnemonic.path || defaultPath,
locale: srcMnemonic.locale || "en"
}
));
const mnemonic = this.mnemonic;
const node = HDNode.fromMnemonic(mnemonic.phrase, null, mnemonic.locale).derivePath(mnemonic.path);
2019-05-15 01:25:46 +03:00
if (computeAddress(node.privateKey) !== this.address) {
logger.throwArgumentError("mnemonic/address mismatch", "privateKey", "[REDACTED]");
2019-05-15 01:25:46 +03:00
}
} else {
2020-01-19 05:09:02 +03:00
defineReadOnly(this, "_mnemonic", (): Mnemonic => null);
2019-05-15 01:25:46 +03:00
}
} else {
if (SigningKey.isSigningKey(privateKey)) {
2020-07-13 13:48:33 +03:00
/* istanbul ignore if */
if (privateKey.curve !== "secp256k1") {
2019-08-02 01:04:06 +03:00
logger.throwArgumentError("unsupported curve; must be secp256k1", "privateKey", "[REDACTED]");
}
defineReadOnly(this, "_signingKey", () => (<SigningKey>privateKey));
2019-05-15 01:25:46 +03:00
} else {
// A lot of common tools do not prefix private keys with a 0x (see: #1166)
if (typeof(privateKey) === "string") {
if (privateKey.match(/^[0-9a-f]*$/i) && privateKey.length === 64) {
privateKey = "0x" + privateKey;
}
}
const signingKey = new SigningKey(privateKey);
2019-05-15 01:25:46 +03:00
defineReadOnly(this, "_signingKey", () => signingKey);
}
2020-01-19 05:09:02 +03:00
defineReadOnly(this, "_mnemonic", (): Mnemonic => null);
2019-05-15 01:25:46 +03:00
defineReadOnly(this, "address", computeAddress(this.publicKey));
}
2020-07-13 13:48:33 +03:00
/* istanbul ignore if */
if (provider && !Provider.isProvider(provider)) {
2019-08-02 01:04:06 +03:00
logger.throwArgumentError("invalid provider", "provider", provider);
2019-05-15 01:25:46 +03:00
}
defineReadOnly(this, "provider", provider || null);
}
2020-01-19 05:09:02 +03:00
get mnemonic(): Mnemonic { return this._mnemonic(); }
2019-05-15 01:25:46 +03:00
get privateKey(): string { return this._signingKey().privateKey; }
get publicKey(): string { return this._signingKey().publicKey; }
getAddress(): Promise<string> {
return Promise.resolve(this.address);
}
connect(provider: Provider): Wallet {
return new Wallet(this, provider);
}
signTransaction(transaction: TransactionRequest): Promise<string> {
return resolveProperties(transaction).then((tx) => {
if (tx.from != null) {
if (getAddress(tx.from) !== this.address) {
2020-07-13 13:48:33 +03:00
logger.throwArgumentError("transaction from address mismatch", "transaction.from", transaction.from);
2019-05-15 01:25:46 +03:00
}
delete tx.from;
}
2020-02-07 02:21:34 +03:00
const signature = this._signingKey().signDigest(keccak256(serialize(<UnsignedTransaction>tx)));
return serialize(<UnsignedTransaction>tx, signature);
2019-05-15 01:25:46 +03:00
});
}
async signMessage(message: Bytes | string): Promise<string> {
return joinSignature(this._signingKey().signDigest(hashMessage(message)));
}
async _signTypedData(domain: TypedDataDomain, types: Record<string, Array<TypedDataField>>, value: Record<string, any>): Promise<string> {
// Populate any ENS names
const populated = await _TypedDataEncoder.resolveNames(domain, types, value, (name: string) => {
if (this.provider == null) {
logger.throwError("cannot resolve ENS names without a provider", Logger.errors.UNSUPPORTED_OPERATION, {
2020-11-15 01:42:36 +03:00
operation: "resolveName",
value: name
});
}
return this.provider.resolveName(name);
});
return joinSignature(this._signingKey().signDigest(_TypedDataEncoder.hash(populated.domain, types, populated.value)));
2019-05-15 01:25:46 +03:00
}
encrypt(password: Bytes | string, options?: any, progressCallback?: ProgressCallback): Promise<string> {
if (typeof(options) === "function" && !progressCallback) {
progressCallback = options;
options = {};
}
if (progressCallback && typeof(progressCallback) !== "function") {
throw new Error("invalid callback");
}
if (!options) { options = {}; }
return encryptKeystore(this, password, options, progressCallback);
}
/**
* Static methods to create Wallet instances.
*/
static createRandom(options?: any): Wallet {
let entropy: Uint8Array = randomBytes(16);
if (!options) { options = { }; }
if (options.extraEntropy) {
entropy = arrayify(hexDataSlice(keccak256(concat([ entropy, options.extraEntropy ])), 0, 16));
}
const mnemonic = entropyToMnemonic(entropy, options.locale);
2019-05-15 01:25:46 +03:00
return Wallet.fromMnemonic(mnemonic, options.path, options.locale);
}
static fromEncryptedJson(json: string, password: Bytes | string, progressCallback?: ProgressCallback): Promise<Wallet> {
return decryptJsonWallet(json, password, progressCallback).then((account) => {
return new Wallet(account);
});
}
static fromEncryptedJsonSync(json: string, password: Bytes | string): Wallet {
return new Wallet(decryptJsonWalletSync(json, password));
}
2019-05-15 01:25:46 +03:00
static fromMnemonic(mnemonic: string, path?: string, wordlist?: Wordlist): Wallet {
if (!path) { path = defaultPath; }
return new Wallet(HDNode.fromMnemonic(mnemonic, null, wordlist).derivePath(path));
}
}
export function verifyMessage(message: Bytes | string, signature: SignatureLike): string {
return recoverAddress(hashMessage(message), signature);
}
export function verifyTypedData(domain: TypedDataDomain, types: Record<string, Array<TypedDataField>>, value: Record<string, any>, signature: SignatureLike): string {
return recoverAddress(_TypedDataEncoder.hash(domain, types, value), signature);
}