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 } from "@ethersproject/abstract-signer";
|
|
|
|
import { arrayify, Bytes, BytesLike, concat, hexDataSlice, isHexString, joinSignature, SignatureLike } from "@ethersproject/bytes";
|
|
|
|
import { hashMessage } from "@ethersproject/hash";
|
|
|
|
import { defaultPath, HDNode, entropyToMnemonic } from "@ethersproject/hdnode";
|
|
|
|
import { keccak256 } from "@ethersproject/keccak256";
|
2019-06-11 05:25:46 +03:00
|
|
|
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, encryptKeystore, ProgressCallback } from "@ethersproject/json-wallets";
|
|
|
|
import { computeAddress, recoverAddress, serialize } from "@ethersproject/transactions";
|
2019-08-25 09:39:20 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Wallet extends Signer implements ExternallyOwnedAccount {
|
|
|
|
|
|
|
|
readonly address: string;
|
|
|
|
readonly provider: Provider;
|
|
|
|
|
|
|
|
readonly path: string;
|
|
|
|
|
|
|
|
// Wrapping the _signingKey and _mnemonic in a getter function prevents
|
|
|
|
// leaking the private key in console.log; still, be careful! :)
|
|
|
|
readonly _signingKey: () => SigningKey;
|
|
|
|
readonly _mnemonic: () => string;
|
|
|
|
|
|
|
|
constructor(privateKey: BytesLike | ExternallyOwnedAccount | SigningKey, provider?: Provider) {
|
2019-08-02 01:04:06 +03:00
|
|
|
logger.checkNew(new.target, Wallet);
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
super();
|
|
|
|
|
|
|
|
if (isAccount(privateKey)) {
|
2019-11-01 17:33:51 +03:00
|
|
|
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)) {
|
2019-08-02 01:04:06 +03:00
|
|
|
logger.throwArgumentError("privateKey/address mismatch", "privateKey", "[REDCACTED]");
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (privateKey.mnemonic != null) {
|
2019-11-01 17:33:51 +03:00
|
|
|
const mnemonic = privateKey.mnemonic;
|
|
|
|
const path = privateKey.path || defaultPath;
|
2019-05-15 01:25:46 +03:00
|
|
|
defineReadOnly(this, "_mnemonic", () => mnemonic);
|
|
|
|
defineReadOnly(this, "path", privateKey.path);
|
2019-11-01 17:33:51 +03:00
|
|
|
const node = HDNode.fromMnemonic(mnemonic).derivePath(path);
|
2019-05-15 01:25:46 +03:00
|
|
|
if (computeAddress(node.privateKey) !== this.address) {
|
2019-08-02 01:04:06 +03:00
|
|
|
logger.throwArgumentError("mnemonic/address mismatch", "privateKey", "[REDCACTED]");
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
defineReadOnly(this, "_mnemonic", (): string => null);
|
|
|
|
defineReadOnly(this, "path", null);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
2019-06-11 05:25:46 +03:00
|
|
|
if (SigningKey.isSigningKey(privateKey)) {
|
|
|
|
if (privateKey.curve !== "secp256k1") {
|
2019-08-02 01:04:06 +03:00
|
|
|
logger.throwArgumentError("unsupported curve; must be secp256k1", "privateKey", "[REDACTED]");
|
2019-06-11 05:25:46 +03:00
|
|
|
}
|
2019-05-15 01:25:46 +03:00
|
|
|
defineReadOnly(this, "_signingKey", () => privateKey);
|
|
|
|
} else {
|
2019-11-01 17:33:51 +03:00
|
|
|
const signingKey = new SigningKey(privateKey);
|
2019-05-15 01:25:46 +03:00
|
|
|
defineReadOnly(this, "_signingKey", () => signingKey);
|
|
|
|
}
|
|
|
|
defineReadOnly(this, "_mnemonic", (): string => null);
|
|
|
|
defineReadOnly(this, "path", null);
|
|
|
|
defineReadOnly(this, "address", computeAddress(this.publicKey));
|
|
|
|
}
|
|
|
|
|
2019-06-11 05:25:46 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
get mnemonic(): string { return this._mnemonic(); }
|
|
|
|
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) {
|
|
|
|
throw new Error("transaction from address mismatch");
|
|
|
|
}
|
|
|
|
delete tx.from;
|
|
|
|
}
|
|
|
|
|
2019-11-01 17:33:51 +03:00
|
|
|
const signature = this._signingKey().signDigest(keccak256(serialize(tx)));
|
2019-05-15 01:25:46 +03:00
|
|
|
return serialize(tx, signature);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
signMessage(message: Bytes | string): Promise<string> {
|
|
|
|
return Promise.resolve(joinSignature(this._signingKey().signDigest(hashMessage(message))));
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2019-11-01 17:33:51 +03:00
|
|
|
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 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);
|
|
|
|
}
|