fix: remove async for createProgramAddress and findProgramAddress (#2… (#23185)
* fix: remove async for createProgramAddress and findProgramAddress (#23184) make sync * test: add test to ensure backwards compatibility
This commit is contained in:
parent
6d1b6bdd7c
commit
a6742b5838
|
@ -143,10 +143,10 @@ export class PublicKey extends Struct {
|
||||||
* Derive a program address from seeds and a program ID.
|
* Derive a program address from seeds and a program ID.
|
||||||
*/
|
*/
|
||||||
/* eslint-disable require-await */
|
/* eslint-disable require-await */
|
||||||
static async createProgramAddress(
|
static createProgramAddressSync(
|
||||||
seeds: Array<Buffer | Uint8Array>,
|
seeds: Array<Buffer | Uint8Array>,
|
||||||
programId: PublicKey,
|
programId: PublicKey,
|
||||||
): Promise<PublicKey> {
|
): PublicKey {
|
||||||
let buffer = Buffer.alloc(0);
|
let buffer = Buffer.alloc(0);
|
||||||
seeds.forEach(function (seed) {
|
seeds.forEach(function (seed) {
|
||||||
if (seed.length > MAX_SEED_LENGTH) {
|
if (seed.length > MAX_SEED_LENGTH) {
|
||||||
|
@ -167,6 +167,18 @@ export class PublicKey extends Struct {
|
||||||
return new PublicKey(publicKeyBytes);
|
return new PublicKey(publicKeyBytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Async version of createProgramAddressSync
|
||||||
|
* For backwards compatibility
|
||||||
|
*/
|
||||||
|
/* eslint-disable require-await */
|
||||||
|
static async createProgramAddress(
|
||||||
|
seeds: Array<Buffer | Uint8Array>,
|
||||||
|
programId: PublicKey,
|
||||||
|
): Promise<PublicKey> {
|
||||||
|
return this.createProgramAddressSync(seeds, programId);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find a valid program address
|
* 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
|
* iterates a nonce until it finds one that when combined with the seeds
|
||||||
* results in a valid program address.
|
* results in a valid program address.
|
||||||
*/
|
*/
|
||||||
static async findProgramAddress(
|
static findProgramAddressSync(
|
||||||
seeds: Array<Buffer | Uint8Array>,
|
seeds: Array<Buffer | Uint8Array>,
|
||||||
programId: PublicKey,
|
programId: PublicKey,
|
||||||
): Promise<[PublicKey, number]> {
|
): [PublicKey, number] {
|
||||||
let nonce = 255;
|
let nonce = 255;
|
||||||
let address;
|
let address;
|
||||||
while (nonce != 0) {
|
while (nonce != 0) {
|
||||||
try {
|
try {
|
||||||
const seedsWithNonce = seeds.concat(Buffer.from([nonce]));
|
const seedsWithNonce = seeds.concat(Buffer.from([nonce]));
|
||||||
address = await this.createProgramAddress(seedsWithNonce, programId);
|
address = this.createProgramAddressSync(seedsWithNonce, programId);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof TypeError) {
|
if (err instanceof TypeError) {
|
||||||
throw err;
|
throw err;
|
||||||
|
@ -196,6 +208,17 @@ export class PublicKey extends Struct {
|
||||||
throw new Error(`Unable to find a viable program address nonce`);
|
throw new Error(`Unable to find a viable program address nonce`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Async version of findProgramAddressSync
|
||||||
|
* For backwards compatibility
|
||||||
|
*/
|
||||||
|
static async findProgramAddress(
|
||||||
|
seeds: Array<Buffer | Uint8Array>,
|
||||||
|
programId: PublicKey,
|
||||||
|
): Promise<[PublicKey, number]> {
|
||||||
|
return this.findProgramAddressSync(seeds, programId);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check that a pubkey is on the ed25519 curve.
|
* Check that a pubkey is on the ed25519 curve.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -200,6 +200,12 @@ describe('PublicKey', function () {
|
||||||
),
|
),
|
||||||
).to.be.true;
|
).to.be.true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Should work in promise mode, for backwards compatibility
|
||||||
|
PublicKey.createProgramAddress(
|
||||||
|
[Buffer.from('', 'utf8'), Buffer.from([1])],
|
||||||
|
programId,
|
||||||
|
).then();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('findProgramAddress', async () => {
|
it('findProgramAddress', async () => {
|
||||||
|
@ -218,6 +224,9 @@ describe('PublicKey', function () {
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
).to.be.true;
|
).to.be.true;
|
||||||
|
|
||||||
|
// Should work in promise mode, for backwards compatibility
|
||||||
|
PublicKey.findProgramAddress([Buffer.from('', 'utf8')], programId).then();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('isOnCurve', () => {
|
it('isOnCurve', () => {
|
||||||
|
|
Loading…
Reference in New Issue