feat: remove hex public key decoding

This commit is contained in:
Justin Starry
2020-08-14 15:02:00 +08:00
committed by Justin Starry
parent 30dbe257cf
commit 74bf0d8d3b
3 changed files with 19 additions and 32 deletions

View File

@ -21,17 +21,12 @@ export class PublicKey {
*/
constructor(value: number | string | Buffer | Uint8Array | Array<number>) {
if (typeof value === 'string') {
// hexadecimal number
if (value.startsWith('0x')) {
this._bn = new BN(value.substring(2), 16);
} else {
// assume base 58 encoding by default
const decoded = bs58.decode(value);
if (decoded.length != 32) {
throw new Error(`Invalid public key input`);
}
this._bn = new BN(decoded);
// assume base 58 encoding by default
const decoded = bs58.decode(value);
if (decoded.length != 32) {
throw new Error(`Invalid public key input`);
}
this._bn = new BN(decoded);
} else {
this._bn = new BN(value);
}
@ -90,7 +85,7 @@ export class PublicKey {
programId.toBuffer(),
]);
const hash = await sha256(new Uint8Array(buffer));
return new PublicKey('0x' + hash);
return new PublicKey(Buffer.from(hash, 'hex'));
}
/**