multisig-ui/src/utils/idl.ts

34 lines
913 B
TypeScript
Raw Permalink Normal View History

2021-04-22 22:05:29 -07:00
import { struct, Layout } from "buffer-layout";
import { rustEnum } from "@project-serum/borsh";
2021-04-22 12:16:15 -07:00
// Simplified since we only use the SetBuffer variant.
export type IdlInstruction =
| Create
| CreateBuffer
| Write
| SetBuffer
| SetAuthority;
type Create = {};
type CreateBuffer = {};
type Write = {};
type SetBuffer = {};
type SetAuthority = {};
const IDL_INSTRUCTION_LAYOUT: Layout<IdlInstruction> = rustEnum([
2021-04-22 22:05:29 -07:00
struct([], "create"),
struct([], "createBuffer"),
struct([], "write"),
struct([], "setBuffer"),
struct([], "setAuthority"),
2021-04-22 12:16:15 -07:00
]);
export function encodeInstruction(i: IdlInstruction): Buffer {
const buffer = Buffer.alloc(1000); // TODO: use a tighter buffer.
const len = IDL_INSTRUCTION_LAYOUT.encode(i, buffer);
return Buffer.concat([IDL_TAG, buffer.slice(0, len)]);
}
// Reverse for little endian.
2021-04-22 22:05:29 -07:00
export const IDL_TAG = Buffer.from("0a69e9a778bcf440", "hex").reverse();