2018-09-30 18:42:45 -07:00
|
|
|
// @flow
|
2020-08-31 15:02:21 -07:00
|
|
|
import BN from 'bn.js';
|
|
|
|
|
2020-10-13 13:40:38 -07:00
|
|
|
import {PublicKey, MAX_SEED_LENGTH} from '../src/publickey';
|
2018-09-30 18:42:45 -07:00
|
|
|
|
|
|
|
test('invalid', () => {
|
|
|
|
expect(() => {
|
|
|
|
new PublicKey([
|
2018-11-04 11:41:21 -08:00
|
|
|
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,
|
|
|
|
0,
|
2018-09-30 18:42:45 -07:00
|
|
|
]);
|
|
|
|
}).toThrow();
|
|
|
|
|
|
|
|
expect(() => {
|
2018-11-04 11:41:21 -08:00
|
|
|
new PublicKey(
|
|
|
|
'0x300000000000000000000000000000000000000000000000000000000000000000000',
|
|
|
|
);
|
2018-09-30 18:42:45 -07:00
|
|
|
}).toThrow();
|
|
|
|
|
2020-08-14 15:02:00 +08:00
|
|
|
expect(() => {
|
|
|
|
new PublicKey(
|
|
|
|
'0x300000000000000000000000000000000000000000000000000000000000000',
|
|
|
|
);
|
|
|
|
}).toThrow();
|
|
|
|
|
2018-09-30 18:42:45 -07:00
|
|
|
expect(() => {
|
2018-11-04 11:41:21 -08:00
|
|
|
new PublicKey(
|
|
|
|
'135693854574979916511997248057056142015550763280047535983739356259273198796800000',
|
|
|
|
);
|
2018-09-30 18:42:45 -07:00
|
|
|
}).toThrow();
|
2019-08-28 10:45:17 -04:00
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
new PublicKey('12345');
|
|
|
|
}).toThrow();
|
2018-09-30 18:42:45 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
test('equals', () => {
|
|
|
|
const arrayKey = new PublicKey([
|
2018-11-04 11:41:21 -08:00
|
|
|
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,
|
2018-09-30 18:42:45 -07:00
|
|
|
]);
|
2020-08-14 15:02:00 +08:00
|
|
|
const base58Key = new PublicKey(
|
2018-11-04 11:41:21 -08:00
|
|
|
'CiDwVBFgWV9E5MvXWoLgnEgn2hK7rJikbvfWavzAQz3',
|
|
|
|
);
|
2018-09-30 18:42:45 -07:00
|
|
|
|
2020-08-14 15:02:00 +08:00
|
|
|
expect(arrayKey.equals(base58Key)).toBe(true);
|
2018-09-30 18:42:45 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
test('toBase58', () => {
|
2020-08-14 15:02:00 +08:00
|
|
|
const key = new PublicKey('CiDwVBFgWV9E5MvXWoLgnEgn2hK7rJikbvfWavzAQz3');
|
2018-09-30 18:42:45 -07:00
|
|
|
expect(key.toBase58()).toBe('CiDwVBFgWV9E5MvXWoLgnEgn2hK7rJikbvfWavzAQz3');
|
2018-10-07 20:12:03 -07:00
|
|
|
expect(key.toString()).toBe('CiDwVBFgWV9E5MvXWoLgnEgn2hK7rJikbvfWavzAQz3');
|
2018-09-30 20:07:28 -07:00
|
|
|
|
2018-10-24 07:58:25 -07:00
|
|
|
const key2 = new PublicKey('1111111111111111111111111111BukQL');
|
2018-10-23 12:25:02 -07:00
|
|
|
expect(key2.toBase58()).toBe('1111111111111111111111111111BukQL');
|
|
|
|
expect(key2.toString()).toBe('1111111111111111111111111111BukQL');
|
2018-10-24 07:58:25 -07:00
|
|
|
|
|
|
|
const key3 = new PublicKey('11111111111111111111111111111111');
|
|
|
|
expect(key3.toBase58()).toBe('11111111111111111111111111111111');
|
|
|
|
|
|
|
|
const key4 = new PublicKey([
|
2018-11-04 11:41:21 -08:00
|
|
|
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,
|
2018-10-24 07:58:25 -07:00
|
|
|
]);
|
|
|
|
expect(key4.toBase58()).toBe('11111111111111111111111111111111');
|
2018-09-30 18:42:45 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
test('toBuffer', () => {
|
2020-08-14 15:02:00 +08:00
|
|
|
const key = new PublicKey('CiDwVBFgWV9E5MvXWoLgnEgn2hK7rJikbvfWavzAQz3');
|
2018-09-30 18:42:45 -07:00
|
|
|
expect(key.toBuffer()).toHaveLength(32);
|
|
|
|
expect(key.toBase58()).toBe('CiDwVBFgWV9E5MvXWoLgnEgn2hK7rJikbvfWavzAQz3');
|
|
|
|
|
2020-08-14 15:02:00 +08:00
|
|
|
const key2 = new PublicKey('11111111111111111111111111111111');
|
2018-09-30 18:42:45 -07:00
|
|
|
expect(key2.toBuffer()).toHaveLength(32);
|
|
|
|
expect(key2.toBase58()).toBe('11111111111111111111111111111111');
|
2019-08-28 10:45:17 -04:00
|
|
|
|
|
|
|
const key3 = new PublicKey(0);
|
|
|
|
expect(key3.toBuffer()).toHaveLength(32);
|
|
|
|
expect(key3.toBase58()).toBe('11111111111111111111111111111111');
|
2018-09-30 18:42:45 -07:00
|
|
|
});
|
|
|
|
|
2018-10-23 12:25:02 -07:00
|
|
|
test('equals (II)', () => {
|
|
|
|
const key1 = new PublicKey([
|
2018-11-04 11:41:21 -08:00
|
|
|
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,
|
|
|
|
1,
|
2018-10-23 12:25:02 -07:00
|
|
|
]);
|
|
|
|
const key2 = new PublicKey(key1.toBuffer());
|
|
|
|
|
|
|
|
expect(key1.equals(key2)).toBe(true);
|
|
|
|
});
|
2019-12-20 16:14:37 -07:00
|
|
|
|
2020-03-16 17:44:12 +08:00
|
|
|
test('createWithSeed', async () => {
|
|
|
|
const defaultPublicKey = new PublicKey('11111111111111111111111111111111');
|
|
|
|
const derivedKey = await PublicKey.createWithSeed(
|
2019-12-23 11:46:49 -07:00
|
|
|
defaultPublicKey,
|
|
|
|
'limber chicken: 4/45',
|
|
|
|
defaultPublicKey,
|
|
|
|
);
|
2019-12-20 16:14:37 -07:00
|
|
|
|
2019-12-23 11:46:49 -07:00
|
|
|
expect(
|
|
|
|
derivedKey.equals(
|
|
|
|
new PublicKey('9h1HyLCW5dZnBVap8C5egQ9Z6pHyjsh5MNy83iPqqRuq'),
|
|
|
|
),
|
|
|
|
).toBe(true);
|
2019-12-20 16:14:37 -07:00
|
|
|
});
|
2020-05-26 17:46:49 -07:00
|
|
|
|
|
|
|
test('createProgramAddress', async () => {
|
|
|
|
const programId = new PublicKey(
|
|
|
|
'BPFLoader1111111111111111111111111111111111',
|
|
|
|
);
|
2020-06-29 17:05:05 -07:00
|
|
|
const publicKey = new PublicKey(
|
|
|
|
'SeedPubey1111111111111111111111111111111111',
|
|
|
|
);
|
2020-05-26 17:46:49 -07:00
|
|
|
|
2020-06-29 17:05:05 -07:00
|
|
|
let programAddress = await PublicKey.createProgramAddress(
|
2020-08-06 07:10:54 -07:00
|
|
|
[Buffer.from('', 'utf8'), Buffer.from([1])],
|
2020-06-29 17:05:05 -07:00
|
|
|
programId,
|
|
|
|
);
|
2020-05-26 17:46:49 -07:00
|
|
|
expect(
|
|
|
|
programAddress.equals(
|
2020-08-06 07:10:54 -07:00
|
|
|
new PublicKey('3gF2KMe9KiC6FNVBmfg9i267aMPvK37FewCip4eGBFcT'),
|
2020-05-26 17:46:49 -07:00
|
|
|
),
|
|
|
|
).toBe(true);
|
2020-06-29 17:05:05 -07:00
|
|
|
|
|
|
|
programAddress = await PublicKey.createProgramAddress(
|
|
|
|
[Buffer.from('☉', 'utf8')],
|
|
|
|
programId,
|
|
|
|
);
|
2020-05-26 17:46:49 -07:00
|
|
|
expect(
|
|
|
|
programAddress.equals(
|
2020-08-06 07:10:54 -07:00
|
|
|
new PublicKey('7ytmC1nT1xY4RfxCV2ZgyA7UakC93do5ZdyhdF3EtPj7'),
|
2020-05-26 17:46:49 -07:00
|
|
|
),
|
|
|
|
).toBe(true);
|
2020-06-29 17:05:05 -07:00
|
|
|
|
2020-05-26 17:46:49 -07:00
|
|
|
programAddress = await PublicKey.createProgramAddress(
|
2020-06-29 17:05:05 -07:00
|
|
|
[Buffer.from('Talking', 'utf8'), Buffer.from('Squirrels', 'utf8')],
|
2020-05-26 17:46:49 -07:00
|
|
|
programId,
|
|
|
|
);
|
|
|
|
expect(
|
|
|
|
programAddress.equals(
|
2020-08-06 07:10:54 -07:00
|
|
|
new PublicKey('HwRVBufQ4haG5XSgpspwKtNd3PC9GM9m1196uJW36vds'),
|
2020-05-26 17:46:49 -07:00
|
|
|
),
|
|
|
|
).toBe(true);
|
2020-06-29 17:05:05 -07:00
|
|
|
|
2020-05-26 17:46:49 -07:00
|
|
|
programAddress = await PublicKey.createProgramAddress(
|
2020-06-29 17:05:05 -07:00
|
|
|
[publicKey.toBuffer()],
|
2020-05-26 17:46:49 -07:00
|
|
|
programId,
|
|
|
|
);
|
2020-06-29 17:05:05 -07:00
|
|
|
expect(
|
|
|
|
programAddress.equals(
|
2020-08-06 07:10:54 -07:00
|
|
|
new PublicKey('GUs5qLUfsEHkcMB9T38vjr18ypEhRuNWiePW2LoK4E3K'),
|
2020-06-29 17:05:05 -07:00
|
|
|
),
|
|
|
|
).toBe(true);
|
|
|
|
|
2020-05-26 17:46:49 -07:00
|
|
|
const programAddress2 = await PublicKey.createProgramAddress(
|
2020-06-29 17:05:05 -07:00
|
|
|
[Buffer.from('Talking', 'utf8')],
|
2020-05-26 17:46:49 -07:00
|
|
|
programId,
|
|
|
|
);
|
|
|
|
expect(programAddress.equals(programAddress2)).toBe(false);
|
2020-08-31 15:02:21 -07:00
|
|
|
|
2020-10-13 13:40:38 -07:00
|
|
|
await expect(
|
|
|
|
PublicKey.createProgramAddress(
|
|
|
|
[Buffer.alloc(MAX_SEED_LENGTH + 1)],
|
|
|
|
programId,
|
|
|
|
),
|
|
|
|
).rejects.toThrow('Max seed length exceeded');
|
|
|
|
|
2020-08-31 15:02:21 -07:00
|
|
|
// https://github.com/solana-labs/solana/issues/11950
|
|
|
|
{
|
|
|
|
let seeds = [
|
|
|
|
new PublicKey('H4snTKK9adiU15gP22ErfZYtro3aqR9BTMXiH3AwiUTQ').toBuffer(),
|
|
|
|
new BN(2).toArrayLike(Buffer, 'le', 8),
|
|
|
|
];
|
|
|
|
let programId = new PublicKey(
|
|
|
|
'4ckmDgGdxQoPDLUkDT3vHgSAkzA3QRdNq5ywwY4sUSJn',
|
|
|
|
);
|
|
|
|
programAddress = await PublicKey.createProgramAddress(seeds, programId);
|
|
|
|
expect(
|
|
|
|
programAddress.equals(
|
|
|
|
new PublicKey('12rqwuEgBYiGhBrDJStCiqEtzQpTTiZbh7teNVLuYcFA'),
|
|
|
|
),
|
|
|
|
).toBe(true);
|
|
|
|
}
|
2020-05-26 17:46:49 -07:00
|
|
|
});
|
2020-08-06 07:10:54 -07:00
|
|
|
|
|
|
|
test('findProgramAddress', async () => {
|
|
|
|
const programId = new PublicKey(
|
|
|
|
'BPFLoader1111111111111111111111111111111111',
|
|
|
|
);
|
|
|
|
let [programAddress, nonce] = await PublicKey.findProgramAddress(
|
|
|
|
[Buffer.from('', 'utf8')],
|
|
|
|
programId,
|
|
|
|
);
|
|
|
|
expect(
|
|
|
|
programAddress.equals(
|
|
|
|
await PublicKey.createProgramAddress(
|
|
|
|
[Buffer.from('', 'utf8'), Buffer.from([nonce])],
|
|
|
|
programId,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
).toBe(true);
|
|
|
|
});
|