feat: getProgramAddress takes bytes rather than strings (#10837)

This commit is contained in:
Jack May
2020-06-29 17:05:05 -07:00
committed by GitHub
parent d9b389f510
commit 0579581f8c
3 changed files with 28 additions and 6 deletions

View File

@ -25,6 +25,10 @@ declare module '@solana/web3.js' {
seed: string, seed: string,
programId: PublicKey, programId: PublicKey,
): Promise<PublicKey>; ): Promise<PublicKey>;
static createProgramAddress(
seeds: Array<Buffer | Uint8Array>,
programId: PublicKey,
): Promise<PublicKey>;
equals(publickey: PublicKey): boolean; equals(publickey: PublicKey): boolean;
toBase58(): string; toBase58(): string;
toBuffer(): Buffer; toBuffer(): Buffer;

View File

@ -98,7 +98,7 @@ export class PublicKey {
* Derive a program address from seeds and a program ID. * Derive a program address from seeds and a program ID.
*/ */
static async createProgramAddress( static async createProgramAddress(
seeds: Array<string>, seeds: Array<Buffer | Uint8Array>,
programId: PublicKey, programId: PublicKey,
): Promise<PublicKey> { ): Promise<PublicKey> {
let buffer = Buffer.alloc(0); let buffer = Buffer.alloc(0);

View File

@ -243,21 +243,32 @@ test('createProgramAddress', async () => {
const programId = new PublicKey( const programId = new PublicKey(
'BPFLoader1111111111111111111111111111111111', 'BPFLoader1111111111111111111111111111111111',
); );
const publicKey = new PublicKey(
'SeedPubey1111111111111111111111111111111111',
);
let programAddress = await PublicKey.createProgramAddress([''], programId); let programAddress = await PublicKey.createProgramAddress(
[Buffer.from('', 'utf8')],
programId,
);
expect( expect(
programAddress.equals( programAddress.equals(
new PublicKey('CsdSsqp6Upkh2qajhZMBM8xT4GAyDNSmcV37g4pN8rsc'), new PublicKey('CsdSsqp6Upkh2qajhZMBM8xT4GAyDNSmcV37g4pN8rsc'),
), ),
).toBe(true); ).toBe(true);
programAddress = await PublicKey.createProgramAddress(['☉'], programId);
programAddress = await PublicKey.createProgramAddress(
[Buffer.from('☉', 'utf8')],
programId,
);
expect( expect(
programAddress.equals( programAddress.equals(
new PublicKey('A8mYnN8Pfx7Nn6f8RoQgsPNtAGAWmmKSBCDfyDvE6sXF'), new PublicKey('A8mYnN8Pfx7Nn6f8RoQgsPNtAGAWmmKSBCDfyDvE6sXF'),
), ),
).toBe(true); ).toBe(true);
programAddress = await PublicKey.createProgramAddress( programAddress = await PublicKey.createProgramAddress(
['Talking', 'Squirrels'], [Buffer.from('Talking', 'utf8'), Buffer.from('Squirrels', 'utf8')],
programId, programId,
); );
expect( expect(
@ -265,12 +276,19 @@ test('createProgramAddress', async () => {
new PublicKey('CawYq8Rmj4JRR992wVnGEFUjMEkmtmcFgEL4iS1qPczu'), new PublicKey('CawYq8Rmj4JRR992wVnGEFUjMEkmtmcFgEL4iS1qPczu'),
), ),
).toBe(true); ).toBe(true);
programAddress = await PublicKey.createProgramAddress( programAddress = await PublicKey.createProgramAddress(
['Talking', 'Squirrels'], [publicKey.toBuffer()],
programId, programId,
); );
expect(
programAddress.equals(
new PublicKey('4ak7qJacCKMAGP8xJtDkg2VYZh5QKExa71ijMDjZGQyb'),
),
).toBe(true);
const programAddress2 = await PublicKey.createProgramAddress( const programAddress2 = await PublicKey.createProgramAddress(
['Talking'], [Buffer.from('Talking', 'utf8')],
programId, programId,
); );
expect(programAddress.equals(programAddress2)).toBe(false); expect(programAddress.equals(programAddress2)).toBe(false);