feat: remove hex public key decoding
This commit is contained in:
committed by
Justin Starry
parent
30dbe257cf
commit
74bf0d8d3b
@ -21,17 +21,12 @@ export class PublicKey {
|
|||||||
*/
|
*/
|
||||||
constructor(value: number | string | Buffer | Uint8Array | Array<number>) {
|
constructor(value: number | string | Buffer | Uint8Array | Array<number>) {
|
||||||
if (typeof value === 'string') {
|
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
|
// assume base 58 encoding by default
|
||||||
const decoded = bs58.decode(value);
|
const decoded = bs58.decode(value);
|
||||||
if (decoded.length != 32) {
|
if (decoded.length != 32) {
|
||||||
throw new Error(`Invalid public key input`);
|
throw new Error(`Invalid public key input`);
|
||||||
}
|
}
|
||||||
this._bn = new BN(decoded);
|
this._bn = new BN(decoded);
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
this._bn = new BN(value);
|
this._bn = new BN(value);
|
||||||
}
|
}
|
||||||
@ -90,7 +85,7 @@ export class PublicKey {
|
|||||||
programId.toBuffer(),
|
programId.toBuffer(),
|
||||||
]);
|
]);
|
||||||
const hash = await sha256(new Uint8Array(buffer));
|
const hash = await sha256(new Uint8Array(buffer));
|
||||||
return new PublicKey('0x' + hash);
|
return new PublicKey(Buffer.from(hash, 'hex'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -46,6 +46,12 @@ test('invalid', () => {
|
|||||||
);
|
);
|
||||||
}).toThrow();
|
}).toThrow();
|
||||||
|
|
||||||
|
expect(() => {
|
||||||
|
new PublicKey(
|
||||||
|
'0x300000000000000000000000000000000000000000000000000000000000000',
|
||||||
|
);
|
||||||
|
}).toThrow();
|
||||||
|
|
||||||
expect(() => {
|
expect(() => {
|
||||||
new PublicKey(
|
new PublicKey(
|
||||||
'135693854574979916511997248057056142015550763280047535983739356259273198796800000',
|
'135693854574979916511997248057056142015550763280047535983739356259273198796800000',
|
||||||
@ -92,21 +98,15 @@ test('equals', () => {
|
|||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
]);
|
]);
|
||||||
const hexKey = new PublicKey(
|
const base58Key = new PublicKey(
|
||||||
'0x300000000000000000000000000000000000000000000000000000000000000',
|
|
||||||
);
|
|
||||||
const base56Key = new PublicKey(
|
|
||||||
'CiDwVBFgWV9E5MvXWoLgnEgn2hK7rJikbvfWavzAQz3',
|
'CiDwVBFgWV9E5MvXWoLgnEgn2hK7rJikbvfWavzAQz3',
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(arrayKey.equals(hexKey)).toBe(true);
|
expect(arrayKey.equals(base58Key)).toBe(true);
|
||||||
expect(arrayKey.equals(base56Key)).toBe(true);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('toBase58', () => {
|
test('toBase58', () => {
|
||||||
const key = new PublicKey(
|
const key = new PublicKey('CiDwVBFgWV9E5MvXWoLgnEgn2hK7rJikbvfWavzAQz3');
|
||||||
'0x300000000000000000000000000000000000000000000000000000000000000',
|
|
||||||
);
|
|
||||||
expect(key.toBase58()).toBe('CiDwVBFgWV9E5MvXWoLgnEgn2hK7rJikbvfWavzAQz3');
|
expect(key.toBase58()).toBe('CiDwVBFgWV9E5MvXWoLgnEgn2hK7rJikbvfWavzAQz3');
|
||||||
expect(key.toString()).toBe('CiDwVBFgWV9E5MvXWoLgnEgn2hK7rJikbvfWavzAQz3');
|
expect(key.toString()).toBe('CiDwVBFgWV9E5MvXWoLgnEgn2hK7rJikbvfWavzAQz3');
|
||||||
|
|
||||||
@ -155,25 +155,17 @@ test('toBase58', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('toBuffer', () => {
|
test('toBuffer', () => {
|
||||||
const key = new PublicKey(
|
const key = new PublicKey('CiDwVBFgWV9E5MvXWoLgnEgn2hK7rJikbvfWavzAQz3');
|
||||||
'0x300000000000000000000000000000000000000000000000000000000000000',
|
|
||||||
);
|
|
||||||
expect(key.toBuffer()).toHaveLength(32);
|
expect(key.toBuffer()).toHaveLength(32);
|
||||||
expect(key.toBase58()).toBe('CiDwVBFgWV9E5MvXWoLgnEgn2hK7rJikbvfWavzAQz3');
|
expect(key.toBase58()).toBe('CiDwVBFgWV9E5MvXWoLgnEgn2hK7rJikbvfWavzAQz3');
|
||||||
|
|
||||||
const key2 = new PublicKey(
|
const key2 = new PublicKey('11111111111111111111111111111111');
|
||||||
'0x000000000000000000000000000000000000000000000000000000000000000',
|
|
||||||
);
|
|
||||||
expect(key2.toBuffer()).toHaveLength(32);
|
expect(key2.toBuffer()).toHaveLength(32);
|
||||||
expect(key2.toBase58()).toBe('11111111111111111111111111111111');
|
expect(key2.toBase58()).toBe('11111111111111111111111111111111');
|
||||||
|
|
||||||
const key3 = new PublicKey(0);
|
const key3 = new PublicKey(0);
|
||||||
expect(key3.toBuffer()).toHaveLength(32);
|
expect(key3.toBuffer()).toHaveLength(32);
|
||||||
expect(key3.toBase58()).toBe('11111111111111111111111111111111');
|
expect(key3.toBase58()).toBe('11111111111111111111111111111111');
|
||||||
|
|
||||||
const key4 = new PublicKey('0x0');
|
|
||||||
expect(key4.toBuffer()).toHaveLength(32);
|
|
||||||
expect(key4.toBase58()).toBe('11111111111111111111111111111111');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('equals (II)', () => {
|
test('equals (II)', () => {
|
||||||
|
@ -264,7 +264,7 @@ test('live staking actions', async () => {
|
|||||||
fromPubkey: from.publicKey,
|
fromPubkey: from.publicKey,
|
||||||
stakePubkey: newStakeAccount.publicKey,
|
stakePubkey: newStakeAccount.publicKey,
|
||||||
authorized: new Authorized(authorized.publicKey, authorized.publicKey),
|
authorized: new Authorized(authorized.publicKey, authorized.publicKey),
|
||||||
lockup: new Lockup(0, 0, new PublicKey('0x00')),
|
lockup: new Lockup(0, 0, new PublicKey(0)),
|
||||||
lamports: minimumAmount + 42,
|
lamports: minimumAmount + 42,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -303,7 +303,7 @@ test('live staking actions', async () => {
|
|||||||
basePubkey: from.publicKey,
|
basePubkey: from.publicKey,
|
||||||
seed,
|
seed,
|
||||||
authorized: new Authorized(authorized.publicKey, authorized.publicKey),
|
authorized: new Authorized(authorized.publicKey, authorized.publicKey),
|
||||||
lockup: new Lockup(0, 0, new PublicKey('0x00')),
|
lockup: new Lockup(0, 0, new PublicKey(0)),
|
||||||
lamports: 3 * minimumAmount + 42,
|
lamports: 3 * minimumAmount + 42,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user