From f4761584f7f4ba4246fc44ddc71c92dc83d1ec7c Mon Sep 17 00:00:00 2001 From: yangli-io Date: Wed, 16 Feb 2022 18:29:26 +0800 Subject: [PATCH 1/2] fix: remove async for createProgramAddress and findProgramAddress (#23184) make sync --- web3.js/src/publickey.ts | 33 ++++++++++++++++++++++++++++----- web3.js/test/publickey.test.ts | 2 +- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/web3.js/src/publickey.ts b/web3.js/src/publickey.ts index cc8b2f8efa98ca..7aa030361294c3 100644 --- a/web3.js/src/publickey.ts +++ b/web3.js/src/publickey.ts @@ -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 createProgramAddressSync( seeds: Array, programId: PublicKey, - ): Promise { + ): PublicKey { let buffer = Buffer.alloc(0); seeds.forEach(function (seed) { if (seed.length > MAX_SEED_LENGTH) { @@ -167,6 +167,18 @@ export class PublicKey extends Struct { return new PublicKey(publicKeyBytes); } + /** + * Async version of createProgramAddressSync + * For backwards compatibility + */ + /* eslint-disable require-await */ + static async createProgramAddress( + seeds: Array, + programId: PublicKey, + ): Promise { + return this.createProgramAddressSync(seeds, programId); + } + /** * Find a valid program address * @@ -174,16 +186,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 findProgramAddressSync( seeds: Array, 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.createProgramAddressSync(seedsWithNonce, programId); } catch (err) { if (err instanceof TypeError) { throw err; @@ -196,6 +208,17 @@ export class PublicKey extends Struct { throw new Error(`Unable to find a viable program address nonce`); } + /** + * Async version of findProgramAddressSync + * For backwards compatibility + */ + static async findProgramAddress( + seeds: Array, + programId: PublicKey, + ): Promise<[PublicKey, number]> { + return this.findProgramAddressSync(seeds, programId); + } + /** * Check that a pubkey is on the ed25519 curve. */ diff --git a/web3.js/test/publickey.test.ts b/web3.js/test/publickey.test.ts index d6a725c4f53e70..ac9132199e7ed9 100644 --- a/web3.js/test/publickey.test.ts +++ b/web3.js/test/publickey.test.ts @@ -246,4 +246,4 @@ describe('PublicKey', function () { const decoded = PublicKey.decodeUnchecked(encoded); expect(decoded.equals(publicKey)).to.be.true; }); -}); +}); \ No newline at end of file From 8260d8d57ec3ec473e548f2c21edf9e68319cc2c Mon Sep 17 00:00:00 2001 From: yangli-io Date: Thu, 17 Feb 2022 17:35:12 +0800 Subject: [PATCH 2/2] test: add test to ensure backwards compatibility --- web3.js/src/publickey.ts | 2 +- web3.js/test/publickey.test.ts | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/web3.js/src/publickey.ts b/web3.js/src/publickey.ts index 7aa030361294c3..e8be3de8000bfa 100644 --- a/web3.js/src/publickey.ts +++ b/web3.js/src/publickey.ts @@ -212,7 +212,7 @@ export class PublicKey extends Struct { * Async version of findProgramAddressSync * For backwards compatibility */ - static async findProgramAddress( + static async findProgramAddress( seeds: Array, programId: PublicKey, ): Promise<[PublicKey, number]> { diff --git a/web3.js/test/publickey.test.ts b/web3.js/test/publickey.test.ts index ac9132199e7ed9..b6766b8463eae7 100644 --- a/web3.js/test/publickey.test.ts +++ b/web3.js/test/publickey.test.ts @@ -200,6 +200,12 @@ describe('PublicKey', function () { ), ).to.be.true; } + + // Should work in promise mode, for backwards compatibility + PublicKey.createProgramAddress( + [Buffer.from('', 'utf8'), Buffer.from([1])], + programId, + ).then(); }); it('findProgramAddress', async () => { @@ -218,6 +224,9 @@ describe('PublicKey', function () { ), ), ).to.be.true; + + // Should work in promise mode, for backwards compatibility + PublicKey.findProgramAddress([Buffer.from('', 'utf8')], programId).then(); }); it('isOnCurve', () => { @@ -246,4 +255,4 @@ describe('PublicKey', function () { const decoded = PublicKey.decodeUnchecked(encoded); expect(decoded.equals(publicKey)).to.be.true; }); -}); \ No newline at end of file +});