Skip to content

Commit

Permalink
Allow private keys to Wallet to omit the 0x prefix (#1166).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Nov 20, 2020
1 parent a185e89 commit 29f6c34
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/wallet/src.ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down

0 comments on commit 29f6c34

Please sign in to comment.