Skip to content

Commit

Permalink
fix: remove async for createProgramAddress and findProgramAddress (#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
yangli-io committed Feb 16, 2022
1 parent 7939fdc commit 0a15f9e
Showing 2 changed files with 15 additions and 15 deletions.
10 changes: 5 additions & 5 deletions web3.js/src/publickey.ts
Original file line number Diff line number Diff line change
@@ -143,10 +143,10 @@ export class PublicKey extends Struct {
* Derive a program address from seeds and a program ID.
*/
/* eslint-disable require-await */
static async createProgramAddress(
static createProgramAddress(
seeds: Array<Buffer | Uint8Array>,
programId: PublicKey,
): Promise<PublicKey> {
): PublicKey {
let buffer = Buffer.alloc(0);
seeds.forEach(function (seed) {
if (seed.length > MAX_SEED_LENGTH) {
@@ -174,16 +174,16 @@ export class PublicKey extends Struct {
* iterates a nonce until it finds one that when combined with the seeds
* results in a valid program address.
*/
static async findProgramAddress(
static findProgramAddress(
seeds: Array<Buffer | Uint8Array>,
programId: PublicKey,
): Promise<[PublicKey, number]> {
): [PublicKey, number] {
let nonce = 255;
let address;
while (nonce != 0) {
try {
const seedsWithNonce = seeds.concat(Buffer.from([nonce]));
address = await this.createProgramAddress(seedsWithNonce, programId);
address = this.createProgramAddress(seedsWithNonce, programId);
} catch (err) {
if (err instanceof TypeError) {
throw err;
20 changes: 10 additions & 10 deletions web3.js/test/publickey.test.ts
Original file line number Diff line number Diff line change
@@ -121,15 +121,15 @@ describe('PublicKey', function () {
).to.be.true;
});

it('createProgramAddress', async () => {
it('createProgramAddress', () => {
const programId = new PublicKey(
'BPFLoader1111111111111111111111111111111111',
);
const publicKey = new PublicKey(
'SeedPubey1111111111111111111111111111111111',
);

let programAddress = await PublicKey.createProgramAddress(
let programAddress = PublicKey.createProgramAddress(
[Buffer.from('', 'utf8'), Buffer.from([1])],
programId,
);
@@ -139,7 +139,7 @@ describe('PublicKey', function () {
),
).to.be.true;

programAddress = await PublicKey.createProgramAddress(
programAddress = PublicKey.createProgramAddress(
[Buffer.from('☉', 'utf8')],
programId,
);
@@ -149,7 +149,7 @@ describe('PublicKey', function () {
),
).to.be.true;

programAddress = await PublicKey.createProgramAddress(
programAddress = PublicKey.createProgramAddress(
[Buffer.from('Talking', 'utf8'), Buffer.from('Squirrels', 'utf8')],
programId,
);
@@ -159,7 +159,7 @@ describe('PublicKey', function () {
),
).to.be.true;

programAddress = await PublicKey.createProgramAddress(
programAddress = PublicKey.createProgramAddress(
[publicKey.toBuffer()],
programId,
);
@@ -169,18 +169,18 @@ describe('PublicKey', function () {
),
).to.be.true;

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

await expect(
expect(() =>
PublicKey.createProgramAddress(
[Buffer.alloc(MAX_SEED_LENGTH + 1)],
programId,
),
).to.be.rejectedWith('Max seed length exceeded');
).to.be.throw('Max seed length exceeded');

// https://github.com/solana-labs/solana/issues/11950
{
@@ -193,7 +193,7 @@ describe('PublicKey', function () {
let programId = new PublicKey(
'4ckmDgGdxQoPDLUkDT3vHgSAkzA3QRdNq5ywwY4sUSJn',
);
programAddress = await PublicKey.createProgramAddress(seeds, programId);
programAddress = PublicKey.createProgramAddress(seeds, programId);
expect(
programAddress.equals(
new PublicKey('12rqwuEgBYiGhBrDJStCiqEtzQpTTiZbh7teNVLuYcFA'),
@@ -206,7 +206,7 @@ describe('PublicKey', function () {
const programId = new PublicKey(
'BPFLoader1111111111111111111111111111111111',
);
let [programAddress, nonce] = await PublicKey.findProgramAddress(
let [programAddress, nonce] = PublicKey.findProgramAddress(
[Buffer.from('', 'utf8')],
programId,
);

0 comments on commit 0a15f9e

Please sign in to comment.