Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

js-legacy: Fix decoding when optional authority is not provided #70

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions clients/js-legacy/src/extensions/transferFee/instructions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from '../../errors.js';
import { addSigners } from '../../instructions/internal.js';
import { TokenInstruction } from '../../instructions/types.js';
import { COptionPublicKeyLayout } from '../../serialization.js';
import { COptionPublicKeyLayout, getSetOfPossibleSpans } from '../../serialization.js';

export enum TransferFeeInstruction {
InitializeTransferFeeConfig = 0,
Expand Down Expand Up @@ -45,6 +45,8 @@ export const initializeTransferFeeConfigInstructionData = struct<InitializeTrans
u64('maximumFee'),
]);

const ALLOWED_SPANS = getSetOfPossibleSpans(initializeTransferFeeConfigInstructionData);

/**
* Construct an InitializeTransferFeeConfig instruction
*
Expand Down Expand Up @@ -115,8 +117,7 @@ export function decodeInitializeTransferFeeConfigInstruction(
programId: PublicKey,
): DecodedInitializeTransferFeeConfigInstruction {
if (!instruction.programId.equals(programId)) throw new TokenInvalidInstructionProgramError();
if (instruction.data.length !== initializeTransferFeeConfigInstructionData.span)
throw new TokenInvalidInstructionDataError();
if (!ALLOWED_SPANS.has(instruction.data.length)) throw new TokenInvalidInstructionDataError();

const {
keys: { mint },
Expand Down
6 changes: 4 additions & 2 deletions clients/js-legacy/src/instructions/initializeMint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
TokenInvalidInstructionTypeError,
} from '../errors.js';
import { TokenInstruction } from './types.js';
import { COptionPublicKeyLayout } from '../serialization.js';
import { COptionPublicKeyLayout, getSetOfPossibleSpans } from '../serialization.js';

/** TODO: docs */
export interface InitializeMintInstructionData {
Expand All @@ -28,6 +28,8 @@ export const initializeMintInstructionData = struct<InitializeMintInstructionDat
new COptionPublicKeyLayout('freezeAuthority'),
]);

const ALLOWED_SPANS = getSetOfPossibleSpans(initializeMintInstructionData);

/**
* Construct an InitializeMint instruction
*
Expand Down Expand Up @@ -93,7 +95,7 @@ export function decodeInitializeMintInstruction(
programId = TOKEN_PROGRAM_ID,
): DecodedInitializeMintInstruction {
if (!instruction.programId.equals(programId)) throw new TokenInvalidInstructionProgramError();
if (instruction.data.length !== initializeMintInstructionData.span) throw new TokenInvalidInstructionDataError();
if (!ALLOWED_SPANS.has(instruction.data.length)) throw new TokenInvalidInstructionDataError();

const {
keys: { mint, rent },
Expand Down
6 changes: 4 additions & 2 deletions clients/js-legacy/src/instructions/initializeMint2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
TokenInvalidInstructionTypeError,
} from '../errors.js';
import { TokenInstruction } from './types.js';
import { COptionPublicKeyLayout } from '../serialization.js';
import { COptionPublicKeyLayout, getSetOfPossibleSpans } from '../serialization.js';

/** TODO: docs */
export interface InitializeMint2InstructionData {
Expand All @@ -28,6 +28,8 @@ export const initializeMint2InstructionData = struct<InitializeMint2InstructionD
new COptionPublicKeyLayout('freezeAuthority'),
]);

const ALLOWED_SPANS = getSetOfPossibleSpans(initializeMint2InstructionData);

/**
* Construct an InitializeMint2 instruction
*
Expand Down Expand Up @@ -89,7 +91,7 @@ export function decodeInitializeMint2Instruction(
programId = TOKEN_PROGRAM_ID,
): DecodedInitializeMint2Instruction {
if (!instruction.programId.equals(programId)) throw new TokenInvalidInstructionProgramError();
if (instruction.data.length !== initializeMint2InstructionData.span) throw new TokenInvalidInstructionDataError();
if (!ALLOWED_SPANS.has(instruction.data.length)) throw new TokenInvalidInstructionDataError();

const {
keys: { mint },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
TokenUnsupportedInstructionError,
} from '../errors.js';
import { TokenInstruction } from './types.js';
import { COptionPublicKeyLayout } from '../serialization.js';
import { COptionPublicKeyLayout, getSetOfPossibleSpans } from '../serialization.js';

/** TODO: docs */
export interface InitializeMintCloseAuthorityInstructionData {
Expand All @@ -24,6 +24,8 @@ export const initializeMintCloseAuthorityInstructionData = struct<InitializeMint
new COptionPublicKeyLayout('closeAuthority'),
]);

const ALLOWED_SPANS = getSetOfPossibleSpans(initializeMintCloseAuthorityInstructionData);

/**
* Construct an InitializeMintCloseAuthority instruction
*
Expand Down Expand Up @@ -80,8 +82,7 @@ export function decodeInitializeMintCloseAuthorityInstruction(
programId: PublicKey,
): DecodedInitializeMintCloseAuthorityInstruction {
if (!instruction.programId.equals(programId)) throw new TokenInvalidInstructionProgramError();
if (instruction.data.length !== initializeMintCloseAuthorityInstructionData.span)
throw new TokenInvalidInstructionDataError();
if (!ALLOWED_SPANS.has(instruction.data.length)) throw new TokenInvalidInstructionDataError();

const {
keys: { mint },
Expand Down
6 changes: 4 additions & 2 deletions clients/js-legacy/src/instructions/setAuthority.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '../errors.js';
import { addSigners } from './internal.js';
import { TokenInstruction } from './types.js';
import { COptionPublicKeyLayout } from '../serialization.js';
import { COptionPublicKeyLayout, getSetOfPossibleSpans } from '../serialization.js';

/** Authority types defined by the program */
export enum AuthorityType {
Expand Down Expand Up @@ -46,6 +46,8 @@ export const setAuthorityInstructionData = struct<SetAuthorityInstructionData>([
new COptionPublicKeyLayout('newAuthority'),
]);

const ALLOWED_SPANS = getSetOfPossibleSpans(setAuthorityInstructionData);

/**
* Construct a SetAuthority instruction
*
Expand Down Expand Up @@ -109,7 +111,7 @@ export function decodeSetAuthorityInstruction(
programId = TOKEN_PROGRAM_ID,
): DecodedSetAuthorityInstruction {
if (!instruction.programId.equals(programId)) throw new TokenInvalidInstructionProgramError();
if (instruction.data.length !== setAuthorityInstructionData.span) throw new TokenInvalidInstructionDataError();
if (!ALLOWED_SPANS.has(instruction.data.length)) throw new TokenInvalidInstructionDataError();

const {
keys: { account, currentAuthority, multiSigners },
Expand Down
34 changes: 31 additions & 3 deletions clients/js-legacy/src/serialization.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Layout } from '@solana/buffer-layout';
import { Layout, Structure } from '@solana/buffer-layout';
import { publicKey } from '@solana/buffer-layout-utils';
import type { PublicKey } from '@solana/web3.js';

Expand All @@ -10,6 +10,14 @@ export class COptionPublicKeyLayout extends Layout<PublicKey | null> {
this.publicKeyLayout = publicKey();
}

static get spanWhenNull(): number {
return 1;
}

static get spanWithValue(): number {
return 1 + publicKey().span;
}

decode(buffer: Uint8Array, offset: number = 0): PublicKey | null {
const option = buffer[offset];
if (option === 0) {
Expand All @@ -32,8 +40,28 @@ export class COptionPublicKeyLayout extends Layout<PublicKey | null> {
getSpan(buffer?: Uint8Array, offset: number = 0): number {
if (buffer) {
const option = buffer[offset];
return option === 0 ? 1 : 1 + this.publicKeyLayout.span;
return option === 0 ? COptionPublicKeyLayout.spanWhenNull : 1 + COptionPublicKeyLayout.spanWithValue;
}
return 1 + this.publicKeyLayout.span;
return 1 + COptionPublicKeyLayout.spanWithValue;
Comment on lines +43 to +45
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are incorrect since spanWithValue already includes the additional byte

}
}

function computeCombinationsOfSpans(combinations: number[], fields: Layout<any>[], index: number, partialResult: number): number[] {
if (index >= fields.length) {
combinations.push(partialResult);
return combinations;
}
if (fields[index] instanceof COptionPublicKeyLayout) {
computeCombinationsOfSpans(combinations, fields, index + 1, partialResult + COptionPublicKeyLayout.spanWhenNull);
computeCombinationsOfSpans(combinations, fields, index + 1, partialResult + COptionPublicKeyLayout.spanWithValue);
} else {
computeCombinationsOfSpans(combinations, fields, index + 1, partialResult + fields[index].span);
}
return combinations;
}

export function getSetOfPossibleSpans(struct: Structure<any>): Set<number> {
return new Set<number>(
computeCombinationsOfSpans([], struct.fields, 0, 0)
);
}