fix: createProgramAddress now throws on an invalid seed length
This commit is contained in:
1
web3.js/module.d.ts
vendored
1
web3.js/module.d.ts
vendored
@ -3,6 +3,7 @@ declare module '@solana/web3.js' {
|
|||||||
import * as BufferLayout from 'buffer-layout';
|
import * as BufferLayout from 'buffer-layout';
|
||||||
|
|
||||||
// === src/publickey.js ===
|
// === src/publickey.js ===
|
||||||
|
export const MAX_SEED_LENGTH: number;
|
||||||
export type PublicKeyNonce = [PublicKey, number];
|
export type PublicKeyNonce = [PublicKey, number];
|
||||||
export class PublicKey {
|
export class PublicKey {
|
||||||
constructor(value: number | string | Buffer | Uint8Array | Array<number>);
|
constructor(value: number | string | Buffer | Uint8Array | Array<number>);
|
||||||
|
@ -16,6 +16,7 @@ import {PublicKey} from './src/publickey';
|
|||||||
|
|
||||||
declare module '@solana/web3.js' {
|
declare module '@solana/web3.js' {
|
||||||
// === src/publickey.js ===
|
// === src/publickey.js ===
|
||||||
|
declare export var MAX_SEED_LENGTH: number;
|
||||||
declare export type PublicKeyNonce = [PublicKey, number];
|
declare export type PublicKeyNonce = [PublicKey, number];
|
||||||
declare export class PublicKey {
|
declare export class PublicKey {
|
||||||
constructor(
|
constructor(
|
||||||
|
@ -6,7 +6,7 @@ export {Connection} from './connection';
|
|||||||
export {Loader} from './loader';
|
export {Loader} from './loader';
|
||||||
export {Message} from './message';
|
export {Message} from './message';
|
||||||
export {NonceAccount, NONCE_ACCOUNT_LENGTH} from './nonce-account';
|
export {NonceAccount, NONCE_ACCOUNT_LENGTH} from './nonce-account';
|
||||||
export {PublicKey} from './publickey';
|
export {MAX_SEED_LENGTH, PublicKey} from './publickey';
|
||||||
export {
|
export {
|
||||||
STAKE_CONFIG_ID,
|
STAKE_CONFIG_ID,
|
||||||
Authorized,
|
Authorized,
|
||||||
|
@ -10,6 +10,11 @@ let naclLowLevel = nacl.lowlevel;
|
|||||||
|
|
||||||
type PublicKeyNonce = [PublicKey, number]; // This type exists to workaround an esdoc parse error
|
type PublicKeyNonce = [PublicKey, number]; // This type exists to workaround an esdoc parse error
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maximum length of derived pubkey seed
|
||||||
|
*/
|
||||||
|
export const MAX_SEED_LENGTH = 32;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A public key
|
* A public key
|
||||||
*/
|
*/
|
||||||
@ -97,6 +102,9 @@ export class PublicKey {
|
|||||||
): Promise<PublicKey> {
|
): Promise<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) {
|
||||||
|
throw new Error(`Max seed length exceeded`);
|
||||||
|
}
|
||||||
buffer = Buffer.concat([buffer, Buffer.from(seed)]);
|
buffer = Buffer.concat([buffer, Buffer.from(seed)]);
|
||||||
});
|
});
|
||||||
buffer = Buffer.concat([
|
buffer = Buffer.concat([
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
// @flow
|
// @flow
|
||||||
import BN from 'bn.js';
|
import BN from 'bn.js';
|
||||||
|
|
||||||
import {PublicKey} from '../src/publickey';
|
import {PublicKey, MAX_SEED_LENGTH} from '../src/publickey';
|
||||||
|
|
||||||
test('invalid', () => {
|
test('invalid', () => {
|
||||||
expect(() => {
|
expect(() => {
|
||||||
@ -279,6 +279,13 @@ test('createProgramAddress', async () => {
|
|||||||
);
|
);
|
||||||
expect(programAddress.equals(programAddress2)).toBe(false);
|
expect(programAddress.equals(programAddress2)).toBe(false);
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
PublicKey.createProgramAddress(
|
||||||
|
[Buffer.alloc(MAX_SEED_LENGTH + 1)],
|
||||||
|
programId,
|
||||||
|
),
|
||||||
|
).rejects.toThrow('Max seed length exceeded');
|
||||||
|
|
||||||
// https://github.com/solana-labs/solana/issues/11950
|
// https://github.com/solana-labs/solana/issues/11950
|
||||||
{
|
{
|
||||||
let seeds = [
|
let seeds = [
|
||||||
|
Reference in New Issue
Block a user