solana/web3.js/src/instruction.ts

50 lines
1.3 KiB
TypeScript
Raw Normal View History

import {Buffer} from 'buffer';
import * as BufferLayout from 'buffer-layout';
import * as Layout from './layout';
/**
* @typedef {Object} InstructionType
* @property (index} The Instruction index (from solana upstream program)
* @property (BufferLayout} The BufferLayout to use to build data
2021-03-15 11:01:35 +08:00
* @internal
*/
2021-03-15 11:01:35 +08:00
export type InstructionType = {
index: number;
layout: typeof BufferLayout;
};
/**
* Populate a buffer of instruction data using an InstructionType
2021-03-15 11:01:35 +08:00
* @internal
*/
2021-03-15 11:01:35 +08:00
export function encodeData(type: InstructionType, fields?: any): Buffer {
const allocLength =
type.layout.span >= 0 ? type.layout.span : Layout.getAlloc(type, fields);
const data = Buffer.alloc(allocLength);
const layoutFields = Object.assign({instruction: type.index}, fields);
type.layout.encode(layoutFields, data);
return data;
}
/**
* Decode instruction data buffer using an InstructionType
2021-03-15 11:01:35 +08:00
* @internal
*/
2021-03-15 11:01:35 +08:00
export function decodeData(type: InstructionType, buffer: Buffer): any {
let data;
try {
data = type.layout.decode(buffer);
} catch (err) {
throw new Error('invalid instruction; ' + err);
}
if (data.instruction !== type.index) {
throw new Error(
`invalid instruction; instruction index mismatch ${data.instruction} != ${type.index}`,
);
}
return data;
}