Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
feat: getProgramAddress takes bytes rather than strings (#10837)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackcmay authored Jun 30, 2020
1 parent d9b389f commit 0579581
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
4 changes: 4 additions & 0 deletions web3.js/module.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ declare module '@solana/web3.js' {
seed: string,
programId: PublicKey,
): Promise<PublicKey>;
static createProgramAddress(
seeds: Array<Buffer | Uint8Array>,
programId: PublicKey,
): Promise<PublicKey>;
equals(publickey: PublicKey): boolean;
toBase58(): string;
toBuffer(): Buffer;
Expand Down
2 changes: 1 addition & 1 deletion web3.js/src/publickey.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class PublicKey {
* Derive a program address from seeds and a program ID.
*/
static async createProgramAddress(
seeds: Array<string>,
seeds: Array<Buffer | Uint8Array>,
programId: PublicKey,
): Promise<PublicKey> {
let buffer = Buffer.alloc(0);
Expand Down
28 changes: 23 additions & 5 deletions web3.js/test/publickey.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,34 +243,52 @@ test('createProgramAddress', async () => {
const programId = new PublicKey(
'BPFLoader1111111111111111111111111111111111',
);
const publicKey = new PublicKey(
'SeedPubey1111111111111111111111111111111111',
);

let programAddress = await PublicKey.createProgramAddress([''], programId);
let programAddress = await PublicKey.createProgramAddress(
[Buffer.from('', 'utf8')],
programId,
);
expect(
programAddress.equals(
new PublicKey('CsdSsqp6Upkh2qajhZMBM8xT4GAyDNSmcV37g4pN8rsc'),
),
).toBe(true);
programAddress = await PublicKey.createProgramAddress(['☉'], programId);

programAddress = await PublicKey.createProgramAddress(
[Buffer.from('☉', 'utf8')],
programId,
);
expect(
programAddress.equals(
new PublicKey('A8mYnN8Pfx7Nn6f8RoQgsPNtAGAWmmKSBCDfyDvE6sXF'),
),
).toBe(true);

programAddress = await PublicKey.createProgramAddress(
['Talking', 'Squirrels'],
[Buffer.from('Talking', 'utf8'), Buffer.from('Squirrels', 'utf8')],
programId,
);
expect(
programAddress.equals(
new PublicKey('CawYq8Rmj4JRR992wVnGEFUjMEkmtmcFgEL4iS1qPczu'),
),
).toBe(true);

programAddress = await PublicKey.createProgramAddress(
['Talking', 'Squirrels'],
[publicKey.toBuffer()],
programId,
);
expect(
programAddress.equals(
new PublicKey('4ak7qJacCKMAGP8xJtDkg2VYZh5QKExa71ijMDjZGQyb'),
),
).toBe(true);

const programAddress2 = await PublicKey.createProgramAddress(
['Talking'],
[Buffer.from('Talking', 'utf8')],
programId,
);
expect(programAddress.equals(programAddress2)).toBe(false);
Expand Down

0 comments on commit 0579581

Please sign in to comment.