2020-04-30 09:05:29 -07:00
|
|
|
import bs58 from 'bs58';
|
2021-02-01 18:53:24 -08:00
|
|
|
import {Buffer} from 'buffer';
|
2021-07-08 18:01:11 -07:00
|
|
|
import * as BufferLayout from '@solana/buffer-layout';
|
2020-04-30 09:05:29 -07:00
|
|
|
|
|
|
|
import {PublicKey} from './publickey';
|
|
|
|
import type {Blockhash} from './blockhash';
|
|
|
|
import * as Layout from './layout';
|
2022-04-27 11:41:14 -07:00
|
|
|
import {PACKET_DATA_SIZE} from './transaction-constants';
|
2020-04-30 09:05:29 -07:00
|
|
|
import * as shortvec from './util/shortvec-encoding';
|
2021-03-28 20:00:56 -07:00
|
|
|
import {toBuffer} from './util/to-buffer';
|
2020-04-30 09:05:29 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The message header, identifying signed and read-only account
|
|
|
|
*/
|
|
|
|
export type MessageHeader = {
|
2021-03-31 03:48:41 -07:00
|
|
|
/**
|
|
|
|
* The number of signatures required for this message to be considered valid. The
|
|
|
|
* signatures must match the first `numRequiredSignatures` of `accountKeys`.
|
|
|
|
*/
|
2021-03-14 20:01:35 -07:00
|
|
|
numRequiredSignatures: number;
|
2021-03-31 03:48:41 -07:00
|
|
|
/** The last `numReadonlySignedAccounts` of the signed keys are read-only accounts */
|
2021-03-14 20:01:35 -07:00
|
|
|
numReadonlySignedAccounts: number;
|
2021-03-31 03:48:41 -07:00
|
|
|
/** The last `numReadonlySignedAccounts` of the unsigned keys are read-only accounts */
|
2021-03-14 20:01:35 -07:00
|
|
|
numReadonlyUnsignedAccounts: number;
|
2020-04-30 09:05:29 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An instruction to execute by a program
|
|
|
|
*
|
2021-03-31 03:48:41 -07:00
|
|
|
* @property {number} programIdIndex
|
|
|
|
* @property {number[]} accounts
|
|
|
|
* @property {string} data
|
2020-04-30 09:05:29 -07:00
|
|
|
*/
|
|
|
|
export type CompiledInstruction = {
|
2021-03-31 03:48:41 -07:00
|
|
|
/** Index into the transaction keys array indicating the program account that executes this instruction */
|
2021-03-14 20:01:35 -07:00
|
|
|
programIdIndex: number;
|
2021-03-31 03:48:41 -07:00
|
|
|
/** Ordered indices into the transaction keys array indicating which accounts to pass to the program */
|
2021-03-14 20:01:35 -07:00
|
|
|
accounts: number[];
|
2021-03-31 03:48:41 -07:00
|
|
|
/** The program input data encoded as base 58 */
|
2021-03-14 20:01:35 -07:00
|
|
|
data: string;
|
2020-04-30 09:05:29 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Message constructor arguments
|
|
|
|
*/
|
2021-03-14 20:01:35 -07:00
|
|
|
export type MessageArgs = {
|
2021-03-31 03:48:41 -07:00
|
|
|
/** The message header, identifying signed and read-only `accountKeys` */
|
2021-03-14 20:01:35 -07:00
|
|
|
header: MessageHeader;
|
2021-03-31 03:48:41 -07:00
|
|
|
/** All the account keys used by this transaction */
|
2021-03-14 20:01:35 -07:00
|
|
|
accountKeys: string[];
|
2021-03-31 03:48:41 -07:00
|
|
|
/** The hash of a recent ledger block */
|
2021-03-14 20:01:35 -07:00
|
|
|
recentBlockhash: Blockhash;
|
2021-03-31 03:48:41 -07:00
|
|
|
/** Instructions that will be executed in sequence and committed in one atomic transaction if all succeed. */
|
2021-03-14 20:01:35 -07:00
|
|
|
instructions: CompiledInstruction[];
|
2020-04-30 09:05:29 -07:00
|
|
|
};
|
|
|
|
|
2020-08-12 15:01:39 -07:00
|
|
|
const PUBKEY_LENGTH = 32;
|
|
|
|
|
2020-04-30 09:05:29 -07:00
|
|
|
/**
|
|
|
|
* List of instructions to be processed atomically
|
|
|
|
*/
|
|
|
|
export class Message {
|
|
|
|
header: MessageHeader;
|
|
|
|
accountKeys: PublicKey[];
|
|
|
|
recentBlockhash: Blockhash;
|
|
|
|
instructions: CompiledInstruction[];
|
|
|
|
|
2021-09-16 14:10:28 -07:00
|
|
|
private indexToProgramIds: Map<number, PublicKey> = new Map<
|
|
|
|
number,
|
|
|
|
PublicKey
|
|
|
|
>();
|
|
|
|
|
2020-04-30 09:05:29 -07:00
|
|
|
constructor(args: MessageArgs) {
|
|
|
|
this.header = args.header;
|
2020-06-10 22:15:14 -07:00
|
|
|
this.accountKeys = args.accountKeys.map(account => new PublicKey(account));
|
2020-04-30 09:05:29 -07:00
|
|
|
this.recentBlockhash = args.recentBlockhash;
|
|
|
|
this.instructions = args.instructions;
|
2021-09-16 14:10:28 -07:00
|
|
|
this.instructions.forEach(ix =>
|
|
|
|
this.indexToProgramIds.set(
|
|
|
|
ix.programIdIndex,
|
|
|
|
this.accountKeys[ix.programIdIndex],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
isAccountSigner(index: number): boolean {
|
|
|
|
return index < this.header.numRequiredSignatures;
|
2020-04-30 09:05:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
isAccountWritable(index: number): boolean {
|
|
|
|
return (
|
|
|
|
index <
|
|
|
|
this.header.numRequiredSignatures -
|
|
|
|
this.header.numReadonlySignedAccounts ||
|
|
|
|
(index >= this.header.numRequiredSignatures &&
|
|
|
|
index <
|
|
|
|
this.accountKeys.length - this.header.numReadonlyUnsignedAccounts)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-09-16 14:10:28 -07:00
|
|
|
isProgramId(index: number): boolean {
|
|
|
|
return this.indexToProgramIds.has(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
programIds(): PublicKey[] {
|
|
|
|
return [...this.indexToProgramIds.values()];
|
|
|
|
}
|
|
|
|
|
|
|
|
nonProgramIds(): PublicKey[] {
|
|
|
|
return this.accountKeys.filter((_, index) => !this.isProgramId(index));
|
|
|
|
}
|
|
|
|
|
2020-04-30 09:05:29 -07:00
|
|
|
serialize(): Buffer {
|
|
|
|
const numKeys = this.accountKeys.length;
|
|
|
|
|
2021-03-14 20:01:35 -07:00
|
|
|
let keyCount: number[] = [];
|
2020-04-30 09:05:29 -07:00
|
|
|
shortvec.encodeLength(keyCount, numKeys);
|
|
|
|
|
|
|
|
const instructions = this.instructions.map(instruction => {
|
|
|
|
const {accounts, programIdIndex} = instruction;
|
2022-03-23 23:59:08 -07:00
|
|
|
const data = Array.from(bs58.decode(instruction.data));
|
2020-04-30 09:05:29 -07:00
|
|
|
|
2021-03-14 20:01:35 -07:00
|
|
|
let keyIndicesCount: number[] = [];
|
2020-04-30 09:05:29 -07:00
|
|
|
shortvec.encodeLength(keyIndicesCount, accounts.length);
|
|
|
|
|
2021-03-14 20:01:35 -07:00
|
|
|
let dataCount: number[] = [];
|
2020-04-30 09:05:29 -07:00
|
|
|
shortvec.encodeLength(dataCount, data.length);
|
|
|
|
|
|
|
|
return {
|
|
|
|
programIdIndex,
|
|
|
|
keyIndicesCount: Buffer.from(keyIndicesCount),
|
2022-03-23 23:59:08 -07:00
|
|
|
keyIndices: accounts,
|
2020-04-30 09:05:29 -07:00
|
|
|
dataLength: Buffer.from(dataCount),
|
|
|
|
data,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2021-03-14 20:01:35 -07:00
|
|
|
let instructionCount: number[] = [];
|
2020-04-30 09:05:29 -07:00
|
|
|
shortvec.encodeLength(instructionCount, instructions.length);
|
|
|
|
let instructionBuffer = Buffer.alloc(PACKET_DATA_SIZE);
|
|
|
|
Buffer.from(instructionCount).copy(instructionBuffer);
|
|
|
|
let instructionBufferLength = instructionCount.length;
|
|
|
|
|
|
|
|
instructions.forEach(instruction => {
|
2022-03-23 23:59:08 -07:00
|
|
|
const instructionLayout = BufferLayout.struct<
|
|
|
|
Readonly<{
|
|
|
|
data: number[];
|
|
|
|
dataLength: Uint8Array;
|
|
|
|
keyIndices: number[];
|
|
|
|
keyIndicesCount: Uint8Array;
|
|
|
|
programIdIndex: number;
|
|
|
|
}>
|
|
|
|
>([
|
2020-04-30 09:05:29 -07:00
|
|
|
BufferLayout.u8('programIdIndex'),
|
|
|
|
|
|
|
|
BufferLayout.blob(
|
|
|
|
instruction.keyIndicesCount.length,
|
|
|
|
'keyIndicesCount',
|
|
|
|
),
|
|
|
|
BufferLayout.seq(
|
|
|
|
BufferLayout.u8('keyIndex'),
|
|
|
|
instruction.keyIndices.length,
|
|
|
|
'keyIndices',
|
|
|
|
),
|
|
|
|
BufferLayout.blob(instruction.dataLength.length, 'dataLength'),
|
|
|
|
BufferLayout.seq(
|
|
|
|
BufferLayout.u8('userdatum'),
|
|
|
|
instruction.data.length,
|
|
|
|
'data',
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
const length = instructionLayout.encode(
|
|
|
|
instruction,
|
|
|
|
instructionBuffer,
|
|
|
|
instructionBufferLength,
|
|
|
|
);
|
|
|
|
instructionBufferLength += length;
|
|
|
|
});
|
|
|
|
instructionBuffer = instructionBuffer.slice(0, instructionBufferLength);
|
|
|
|
|
2022-03-23 23:59:08 -07:00
|
|
|
const signDataLayout = BufferLayout.struct<
|
|
|
|
Readonly<{
|
|
|
|
keyCount: Uint8Array;
|
|
|
|
keys: Uint8Array[];
|
|
|
|
numReadonlySignedAccounts: Uint8Array;
|
|
|
|
numReadonlyUnsignedAccounts: Uint8Array;
|
|
|
|
numRequiredSignatures: Uint8Array;
|
|
|
|
recentBlockhash: Uint8Array;
|
|
|
|
}>
|
|
|
|
>([
|
2020-04-30 09:05:29 -07:00
|
|
|
BufferLayout.blob(1, 'numRequiredSignatures'),
|
|
|
|
BufferLayout.blob(1, 'numReadonlySignedAccounts'),
|
|
|
|
BufferLayout.blob(1, 'numReadonlyUnsignedAccounts'),
|
|
|
|
BufferLayout.blob(keyCount.length, 'keyCount'),
|
|
|
|
BufferLayout.seq(Layout.publicKey('key'), numKeys, 'keys'),
|
|
|
|
Layout.publicKey('recentBlockhash'),
|
|
|
|
]);
|
|
|
|
|
|
|
|
const transaction = {
|
|
|
|
numRequiredSignatures: Buffer.from([this.header.numRequiredSignatures]),
|
|
|
|
numReadonlySignedAccounts: Buffer.from([
|
|
|
|
this.header.numReadonlySignedAccounts,
|
|
|
|
]),
|
|
|
|
numReadonlyUnsignedAccounts: Buffer.from([
|
|
|
|
this.header.numReadonlyUnsignedAccounts,
|
|
|
|
]),
|
|
|
|
keyCount: Buffer.from(keyCount),
|
2021-03-28 20:00:56 -07:00
|
|
|
keys: this.accountKeys.map(key => toBuffer(key.toBytes())),
|
2020-04-30 09:05:29 -07:00
|
|
|
recentBlockhash: bs58.decode(this.recentBlockhash),
|
|
|
|
};
|
|
|
|
|
|
|
|
let signData = Buffer.alloc(2048);
|
|
|
|
const length = signDataLayout.encode(transaction, signData);
|
|
|
|
instructionBuffer.copy(signData, length);
|
|
|
|
return signData.slice(0, length + instructionBuffer.length);
|
|
|
|
}
|
2020-08-12 15:01:39 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode a compiled message into a Message object.
|
|
|
|
*/
|
|
|
|
static from(buffer: Buffer | Uint8Array | Array<number>): Message {
|
|
|
|
// Slice up wire data
|
|
|
|
let byteArray = [...buffer];
|
|
|
|
|
2021-03-14 20:01:35 -07:00
|
|
|
const numRequiredSignatures = byteArray.shift() as number;
|
|
|
|
const numReadonlySignedAccounts = byteArray.shift() as number;
|
|
|
|
const numReadonlyUnsignedAccounts = byteArray.shift() as number;
|
2020-08-12 15:01:39 -07:00
|
|
|
|
|
|
|
const accountCount = shortvec.decodeLength(byteArray);
|
|
|
|
let accountKeys = [];
|
|
|
|
for (let i = 0; i < accountCount; i++) {
|
|
|
|
const account = byteArray.slice(0, PUBKEY_LENGTH);
|
|
|
|
byteArray = byteArray.slice(PUBKEY_LENGTH);
|
|
|
|
accountKeys.push(bs58.encode(Buffer.from(account)));
|
|
|
|
}
|
|
|
|
|
|
|
|
const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
|
|
|
|
byteArray = byteArray.slice(PUBKEY_LENGTH);
|
|
|
|
|
|
|
|
const instructionCount = shortvec.decodeLength(byteArray);
|
2021-03-14 20:01:35 -07:00
|
|
|
let instructions: CompiledInstruction[] = [];
|
2020-08-12 15:01:39 -07:00
|
|
|
for (let i = 0; i < instructionCount; i++) {
|
2021-03-14 20:01:35 -07:00
|
|
|
const programIdIndex = byteArray.shift() as number;
|
2020-08-12 15:01:39 -07:00
|
|
|
const accountCount = shortvec.decodeLength(byteArray);
|
2021-03-14 20:01:35 -07:00
|
|
|
const accounts = byteArray.slice(0, accountCount);
|
2020-08-12 15:01:39 -07:00
|
|
|
byteArray = byteArray.slice(accountCount);
|
|
|
|
const dataLength = shortvec.decodeLength(byteArray);
|
2021-03-14 20:01:35 -07:00
|
|
|
const dataSlice = byteArray.slice(0, dataLength);
|
|
|
|
const data = bs58.encode(Buffer.from(dataSlice));
|
2020-08-12 15:01:39 -07:00
|
|
|
byteArray = byteArray.slice(dataLength);
|
2021-03-14 20:01:35 -07:00
|
|
|
instructions.push({
|
|
|
|
programIdIndex,
|
|
|
|
accounts,
|
|
|
|
data,
|
|
|
|
});
|
2020-08-12 15:01:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const messageArgs = {
|
|
|
|
header: {
|
|
|
|
numRequiredSignatures,
|
|
|
|
numReadonlySignedAccounts,
|
|
|
|
numReadonlyUnsignedAccounts,
|
|
|
|
},
|
|
|
|
recentBlockhash: bs58.encode(Buffer.from(recentBlockhash)),
|
|
|
|
accountKeys,
|
|
|
|
instructions,
|
|
|
|
};
|
|
|
|
|
|
|
|
return new Message(messageArgs);
|
|
|
|
}
|
2020-04-30 09:05:29 -07:00
|
|
|
}
|