fix: remove decimal string support from PublicKey ctor

This commit is contained in:
Michael Vines 2018-10-24 07:58:25 -07:00
parent 02787df7b9
commit e50b705de3
2 changed files with 20 additions and 21 deletions

View File

@ -12,25 +12,17 @@ export class PublicKey {
/**
* Create a new PublicKey object
*/
constructor(number: number | string | Buffer | Array<number>) {
for (;;) {
if (typeof number === 'string') {
// base 58 encoded?
if (/^[1-9A-HJ-NP-Za-km-z]{43,44}$/.test(number)) {
this._bn = new BN(bs58.decode(number));
break;
}
// hexadecimal number
if (number.startsWith('0x')) {
this._bn = new BN(number.substring(2), 16);
break;
}
constructor(value: number | string | Buffer | 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
this._bn = new BN(bs58.decode(value));
}
this._bn = new BN(number);
break;
} else {
this._bn = new BN(value);
}
if (this._bn.byteLength() > 32) {

View File

@ -22,11 +22,9 @@ test('equals', () => {
3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
]);
const hexKey = new PublicKey('0x300000000000000000000000000000000000000000000000000000000000000');
const decimalKey = new PublicKey('1356938545749799165119972480570561420155507632800475359837393562592731987968');
const base56Key = new PublicKey('CiDwVBFgWV9E5MvXWoLgnEgn2hK7rJikbvfWavzAQz3');
expect(arrayKey.equals(hexKey)).toBe(true);
expect(arrayKey.equals(decimalKey)).toBe(true);
expect(arrayKey.equals(base56Key)).toBe(true);
});
@ -42,9 +40,18 @@ test('toBase58', () => {
expect(key.toBase58()).toBe('CiDwVBFgWV9E5MvXWoLgnEgn2hK7rJikbvfWavzAQz3');
expect(key.toString()).toBe('CiDwVBFgWV9E5MvXWoLgnEgn2hK7rJikbvfWavzAQz3');
const key2 = new PublicKey('123456789');
const key2 = new PublicKey('1111111111111111111111111111BukQL');
expect(key2.toBase58()).toBe('1111111111111111111111111111BukQL');
expect(key2.toString()).toBe('1111111111111111111111111111BukQL');
const key3 = new PublicKey('11111111111111111111111111111111');
expect(key3.toBase58()).toBe('11111111111111111111111111111111');
const key4 = new PublicKey([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
]);
expect(key4.toBase58()).toBe('11111111111111111111111111111111');
});
test('toBuffer', () => {