fix: createProgramAddress now throws on an invalid seed length

This commit is contained in:
Michael Vines 2020-10-13 13:40:38 -07:00
parent f8d338c9cb
commit e84a91d417
5 changed files with 19 additions and 2 deletions

1
web3.js/module.d.ts vendored
View File

@ -3,6 +3,7 @@ declare module '@solana/web3.js' {
import * as BufferLayout from 'buffer-layout';
// === src/publickey.js ===
export const MAX_SEED_LENGTH: number;
export type PublicKeyNonce = [PublicKey, number];
export class PublicKey {
constructor(value: number | string | Buffer | Uint8Array | Array<number>);

View File

@ -16,6 +16,7 @@ import {PublicKey} from './src/publickey';
declare module '@solana/web3.js' {
// === src/publickey.js ===
declare export var MAX_SEED_LENGTH: number;
declare export type PublicKeyNonce = [PublicKey, number];
declare export class PublicKey {
constructor(

View File

@ -6,7 +6,7 @@ export {Connection} from './connection';
export {Loader} from './loader';
export {Message} from './message';
export {NonceAccount, NONCE_ACCOUNT_LENGTH} from './nonce-account';
export {PublicKey} from './publickey';
export {MAX_SEED_LENGTH, PublicKey} from './publickey';
export {
STAKE_CONFIG_ID,
Authorized,

View File

@ -10,6 +10,11 @@ let naclLowLevel = nacl.lowlevel;
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
*/
@ -97,6 +102,9 @@ export class PublicKey {
): Promise<PublicKey> {
let buffer = Buffer.alloc(0);
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([

View File

@ -1,7 +1,7 @@
// @flow
import BN from 'bn.js';
import {PublicKey} from '../src/publickey';
import {PublicKey, MAX_SEED_LENGTH} from '../src/publickey';
test('invalid', () => {
expect(() => {
@ -279,6 +279,13 @@ test('createProgramAddress', async () => {
);
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
{
let seeds = [