Allow private keys to Wallet to omit the 0x prefix (#1166).

This commit is contained in:
Richard Moore 2020-11-20 17:30:36 -05:00
parent a185e89181
commit 29f6c34343
No known key found for this signature in database
GPG Key ID: 665176BE8E9DC651

@ -76,11 +76,20 @@ export class Wallet extends Signer implements ExternallyOwnedAccount, TypedDataS
if (privateKey.curve !== "secp256k1") {
logger.throwArgumentError("unsupported curve; must be secp256k1", "privateKey", "[REDACTED]");
}
defineReadOnly(this, "_signingKey", () => privateKey);
defineReadOnly(this, "_signingKey", () => (<SigningKey>privateKey));
} 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);
defineReadOnly(this, "_signingKey", () => signingKey);
}
defineReadOnly(this, "_mnemonic", (): Mnemonic => null);
defineReadOnly(this, "address", computeAddress(this.publicKey));
}