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 2d9246c
Show file tree
Hide file tree
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
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 10 additions & 10 deletions web3.js/test/publickey.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ describe('PublicKey', function () {
'SeedPubey1111111111111111111111111111111111',
);

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

programAddress = await PublicKey.createProgramAddress(
programAddress = PublicKey.createProgramAddress(
[Buffer.from('☉', 'utf8')],
programId,
);
Expand All @@ -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,
);
Expand All @@ -159,7 +159,7 @@ describe('PublicKey', function () {
),
).to.be.true;

programAddress = await PublicKey.createProgramAddress(
programAddress = PublicKey.createProgramAddress(
[publicKey.toBuffer()],
programId,
);
Expand All @@ -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(
PublicKey.createProgramAddress(
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
{
Expand All @@ -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'),
Expand All @@ -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,
);
Expand Down

0 comments on commit 2d9246c

Please sign in to comment.