2018-10-06 11:13:58 -07:00
|
|
|
import * as BufferLayout from 'buffer-layout';
|
2018-09-18 12:46:59 -07:00
|
|
|
|
2021-03-14 20:01:35 -07:00
|
|
|
import {encodeData, decodeData, InstructionType} from './instruction';
|
2018-10-06 11:13:58 -07:00
|
|
|
import * as Layout from './layout';
|
2020-03-05 08:44:56 -08:00
|
|
|
import {NONCE_ACCOUNT_LENGTH} from './nonce-account';
|
2019-12-23 10:46:49 -08:00
|
|
|
import {PublicKey} from './publickey';
|
2020-01-02 16:58:54 -08:00
|
|
|
import {SYSVAR_RECENT_BLOCKHASHES_PUBKEY, SYSVAR_RENT_PUBKEY} from './sysvar';
|
2019-12-23 10:46:49 -08:00
|
|
|
import {Transaction, TransactionInstruction} from './transaction';
|
2021-03-28 20:00:56 -07:00
|
|
|
import {toBuffer} from './util/to-buffer';
|
2020-03-03 00:05:50 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create account system transaction params
|
|
|
|
* @typedef {Object} CreateAccountParams
|
|
|
|
* @property {PublicKey} fromPubkey
|
|
|
|
* @property {PublicKey} newAccountPubkey
|
|
|
|
* @property {number} lamports
|
|
|
|
* @property {number} space
|
|
|
|
* @property {PublicKey} programId
|
|
|
|
*/
|
2021-03-14 20:01:35 -07:00
|
|
|
export type CreateAccountParams = {
|
|
|
|
fromPubkey: PublicKey;
|
|
|
|
newAccountPubkey: PublicKey;
|
|
|
|
lamports: number;
|
|
|
|
space: number;
|
|
|
|
programId: PublicKey;
|
|
|
|
};
|
2020-03-03 00:05:50 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Transfer system transaction params
|
|
|
|
* @typedef {Object} TransferParams
|
|
|
|
* @property {PublicKey} fromPubkey
|
|
|
|
* @property {PublicKey} toPubkey
|
|
|
|
* @property {number} lamports
|
|
|
|
*/
|
2021-03-14 20:01:35 -07:00
|
|
|
export type TransferParams = {
|
|
|
|
fromPubkey: PublicKey;
|
|
|
|
toPubkey: PublicKey;
|
|
|
|
lamports: number;
|
|
|
|
};
|
2020-03-03 00:05:50 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Assign system transaction params
|
|
|
|
* @typedef {Object} AssignParams
|
2020-05-09 03:33:36 -07:00
|
|
|
* @property {PublicKey} accountPubkey
|
2020-03-03 00:05:50 -08:00
|
|
|
* @property {PublicKey} programId
|
|
|
|
*/
|
2021-03-14 20:01:35 -07:00
|
|
|
export type AssignParams = {
|
|
|
|
accountPubkey: PublicKey;
|
|
|
|
programId: PublicKey;
|
|
|
|
};
|
2020-03-03 00:05:50 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create account with seed system transaction params
|
|
|
|
* @typedef {Object} CreateAccountWithSeedParams
|
|
|
|
* @property {PublicKey} fromPubkey
|
|
|
|
* @property {PublicKey} newAccountPubkey
|
|
|
|
* @property {PublicKey} basePubkey
|
|
|
|
* @property {string} seed
|
|
|
|
* @property {number} lamports
|
|
|
|
* @property {number} space
|
|
|
|
* @property {PublicKey} programId
|
|
|
|
*/
|
2021-03-14 20:01:35 -07:00
|
|
|
export type CreateAccountWithSeedParams = {
|
|
|
|
fromPubkey: PublicKey;
|
|
|
|
newAccountPubkey: PublicKey;
|
|
|
|
basePubkey: PublicKey;
|
|
|
|
seed: string;
|
|
|
|
lamports: number;
|
|
|
|
space: number;
|
|
|
|
programId: PublicKey;
|
|
|
|
};
|
2020-03-03 00:05:50 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create nonce account system transaction params
|
2020-05-04 22:30:55 -07:00
|
|
|
* @typedef {Object} CreateNonceAccountParams
|
2020-03-03 00:05:50 -08:00
|
|
|
* @property {PublicKey} fromPubkey
|
2020-05-04 22:30:55 -07:00
|
|
|
* @property {PublicKey} noncePubkey
|
|
|
|
* @property {PublicKey} authorizedPubkey
|
|
|
|
* @property {number} lamports
|
2020-03-03 00:05:50 -08:00
|
|
|
*/
|
2021-03-14 20:01:35 -07:00
|
|
|
export type CreateNonceAccountParams = {
|
|
|
|
fromPubkey: PublicKey;
|
|
|
|
noncePubkey: PublicKey;
|
|
|
|
authorizedPubkey: PublicKey;
|
|
|
|
lamports: number;
|
|
|
|
};
|
2020-03-03 00:05:50 -08:00
|
|
|
|
2020-05-04 22:30:55 -07:00
|
|
|
/**
|
|
|
|
* Create nonce account with seed system transaction params
|
|
|
|
* @typedef {Object} CreateNonceAccountWithSeedParams
|
|
|
|
* @property {PublicKey} fromPubkey
|
|
|
|
* @property {PublicKey} noncePubkey
|
|
|
|
* @property {PublicKey} authorizedPubkey
|
|
|
|
* @property {PublicKey} basePubkey
|
|
|
|
* @property {string} seed
|
|
|
|
* @property {number} lamports
|
|
|
|
*/
|
2021-03-14 20:01:35 -07:00
|
|
|
export type CreateNonceAccountWithSeedParams = {
|
|
|
|
fromPubkey: PublicKey;
|
|
|
|
noncePubkey: PublicKey;
|
|
|
|
authorizedPubkey: PublicKey;
|
|
|
|
lamports: number;
|
|
|
|
basePubkey: PublicKey;
|
|
|
|
seed: string;
|
|
|
|
};
|
2020-05-04 22:30:55 -07:00
|
|
|
|
2020-03-03 00:05:50 -08:00
|
|
|
/**
|
|
|
|
* Initialize nonce account system instruction params
|
|
|
|
* @typedef {Object} InitializeNonceParams
|
2021-01-20 19:07:19 -08:00
|
|
|
* @property {PublicKey} noncePubkey
|
|
|
|
* @property {PublicKey} authorizedPubkey
|
2020-03-03 00:05:50 -08:00
|
|
|
*/
|
2021-03-14 20:01:35 -07:00
|
|
|
export type InitializeNonceParams = {
|
|
|
|
noncePubkey: PublicKey;
|
|
|
|
authorizedPubkey: PublicKey;
|
|
|
|
};
|
2020-03-03 00:05:50 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Advance nonce account system instruction params
|
|
|
|
* @typedef {Object} AdvanceNonceParams
|
2021-01-20 19:07:19 -08:00
|
|
|
* @property {PublicKey} noncePubkey
|
|
|
|
* @property {PublicKey} authorizedPubkey
|
2020-03-03 00:05:50 -08:00
|
|
|
*/
|
2021-03-14 20:01:35 -07:00
|
|
|
export type AdvanceNonceParams = {
|
|
|
|
noncePubkey: PublicKey;
|
|
|
|
authorizedPubkey: PublicKey;
|
|
|
|
};
|
2020-03-03 00:05:50 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Withdraw nonce account system transaction params
|
|
|
|
* @typedef {Object} WithdrawNonceParams
|
|
|
|
* @property {PublicKey} noncePubkey
|
|
|
|
* @property {PublicKey} authorizedPubkey
|
|
|
|
* @property {PublicKey} toPubkey
|
|
|
|
* @property {number} lamports
|
|
|
|
*/
|
2021-03-14 20:01:35 -07:00
|
|
|
export type WithdrawNonceParams = {
|
|
|
|
noncePubkey: PublicKey;
|
|
|
|
authorizedPubkey: PublicKey;
|
|
|
|
toPubkey: PublicKey;
|
|
|
|
lamports: number;
|
|
|
|
};
|
2020-03-03 00:05:50 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Authorize nonce account system transaction params
|
|
|
|
* @typedef {Object} AuthorizeNonceParams
|
|
|
|
* @property {PublicKey} noncePubkey
|
|
|
|
* @property {PublicKey} authorizedPubkey
|
|
|
|
* @property {PublicKey} newAuthorizedPubkey
|
|
|
|
*/
|
2021-03-14 20:01:35 -07:00
|
|
|
export type AuthorizeNonceParams = {
|
|
|
|
noncePubkey: PublicKey;
|
|
|
|
authorizedPubkey: PublicKey;
|
|
|
|
newAuthorizedPubkey: PublicKey;
|
|
|
|
};
|
2019-09-13 17:07:13 -07:00
|
|
|
|
2020-05-09 03:33:36 -07:00
|
|
|
/**
|
|
|
|
* Allocate account system transaction params
|
|
|
|
* @typedef {Object} AllocateParams
|
|
|
|
* @property {PublicKey} accountPubkey
|
|
|
|
* @property {number} space
|
|
|
|
*/
|
2021-03-14 20:01:35 -07:00
|
|
|
export type AllocateParams = {
|
|
|
|
accountPubkey: PublicKey;
|
|
|
|
space: number;
|
|
|
|
};
|
2020-05-09 03:33:36 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocate account with seed system transaction params
|
|
|
|
* @typedef {Object} AllocateWithSeedParams
|
|
|
|
* @property {PublicKey} accountPubkey
|
|
|
|
* @property {PublicKey} basePubkey
|
|
|
|
* @property {string} seed
|
|
|
|
* @property {number} space
|
|
|
|
* @property {PublicKey} programId
|
|
|
|
*/
|
2021-03-14 20:01:35 -07:00
|
|
|
export type AllocateWithSeedParams = {
|
|
|
|
accountPubkey: PublicKey;
|
|
|
|
basePubkey: PublicKey;
|
|
|
|
seed: string;
|
|
|
|
space: number;
|
|
|
|
programId: PublicKey;
|
|
|
|
};
|
2020-05-09 03:33:36 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Assign account with seed system transaction params
|
|
|
|
* @typedef {Object} AssignWithSeedParams
|
|
|
|
* @property {PublicKey} accountPubkey
|
|
|
|
* @property {PublicKey} basePubkey
|
|
|
|
* @property {string} seed
|
|
|
|
* @property {PublicKey} programId
|
|
|
|
*/
|
2021-03-14 20:01:35 -07:00
|
|
|
export type AssignWithSeedParams = {
|
|
|
|
accountPubkey: PublicKey;
|
|
|
|
basePubkey: PublicKey;
|
|
|
|
seed: string;
|
|
|
|
programId: PublicKey;
|
|
|
|
};
|
2020-05-09 03:33:36 -07:00
|
|
|
|
2021-01-14 08:59:31 -08:00
|
|
|
/**
|
|
|
|
* Transfer with seed system transaction params
|
|
|
|
* @typedef {Object} TransferWithSeedParams
|
2021-01-20 19:07:19 -08:00
|
|
|
* @property {PublicKey} fromPubkey
|
2021-01-14 08:59:31 -08:00
|
|
|
* @property {PublicKey} basePubkey
|
2021-01-20 19:07:19 -08:00
|
|
|
* @property {PublicKey} toPubkey
|
|
|
|
* @property {number} lamports
|
2021-01-14 08:59:31 -08:00
|
|
|
* @property {string} seed
|
|
|
|
* @property {PublicKey} programId
|
|
|
|
*/
|
2021-03-14 20:01:35 -07:00
|
|
|
export type TransferWithSeedParams = {
|
|
|
|
fromPubkey: PublicKey;
|
|
|
|
basePubkey: PublicKey;
|
|
|
|
toPubkey: PublicKey;
|
|
|
|
lamports: number;
|
|
|
|
seed: string;
|
|
|
|
programId: PublicKey;
|
|
|
|
};
|
2021-01-14 08:59:31 -08:00
|
|
|
|
2019-09-13 17:07:13 -07:00
|
|
|
/**
|
|
|
|
* System Instruction class
|
|
|
|
*/
|
2020-03-03 00:05:50 -08:00
|
|
|
export class SystemInstruction {
|
2021-03-14 20:01:35 -07:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
constructor() {}
|
|
|
|
|
2019-09-13 17:07:13 -07:00
|
|
|
/**
|
2020-03-03 00:05:50 -08:00
|
|
|
* Decode a system instruction and retrieve the instruction type.
|
2019-09-13 17:07:13 -07:00
|
|
|
*/
|
2020-03-03 00:05:50 -08:00
|
|
|
static decodeInstructionType(
|
|
|
|
instruction: TransactionInstruction,
|
|
|
|
): SystemInstructionType {
|
|
|
|
this.checkProgramId(instruction.programId);
|
2019-09-13 17:07:13 -07:00
|
|
|
|
|
|
|
const instructionTypeLayout = BufferLayout.u32('instruction');
|
|
|
|
const typeIndex = instructionTypeLayout.decode(instruction.data);
|
2020-03-03 00:05:50 -08:00
|
|
|
|
2021-03-14 20:01:35 -07:00
|
|
|
let type: SystemInstructionType | undefined;
|
|
|
|
for (const [ixType, layout] of Object.entries(SYSTEM_INSTRUCTION_LAYOUTS)) {
|
|
|
|
if (layout.index == typeIndex) {
|
|
|
|
type = ixType as SystemInstructionType;
|
|
|
|
break;
|
2019-09-13 17:07:13 -07:00
|
|
|
}
|
|
|
|
}
|
2020-03-03 00:05:50 -08:00
|
|
|
|
2019-09-13 17:07:13 -07:00
|
|
|
if (!type) {
|
|
|
|
throw new Error('Instruction type incorrect; not a SystemInstruction');
|
|
|
|
}
|
2020-03-03 00:05:50 -08:00
|
|
|
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode a create account system instruction and retrieve the instruction params.
|
|
|
|
*/
|
|
|
|
static decodeCreateAccount(
|
|
|
|
instruction: TransactionInstruction,
|
|
|
|
): CreateAccountParams {
|
|
|
|
this.checkProgramId(instruction.programId);
|
|
|
|
this.checkKeyLength(instruction.keys, 2);
|
|
|
|
|
|
|
|
const {lamports, space, programId} = decodeData(
|
|
|
|
SYSTEM_INSTRUCTION_LAYOUTS.Create,
|
|
|
|
instruction.data,
|
2019-09-13 17:07:13 -07:00
|
|
|
);
|
2020-03-03 00:05:50 -08:00
|
|
|
|
|
|
|
return {
|
|
|
|
fromPubkey: instruction.keys[0].pubkey,
|
|
|
|
newAccountPubkey: instruction.keys[1].pubkey,
|
|
|
|
lamports,
|
|
|
|
space,
|
|
|
|
programId: new PublicKey(programId),
|
|
|
|
};
|
2019-09-13 17:07:13 -07:00
|
|
|
}
|
|
|
|
|
2020-02-28 02:43:10 -08:00
|
|
|
/**
|
2020-03-03 00:05:50 -08:00
|
|
|
* Decode a transfer system instruction and retrieve the instruction params.
|
2020-02-28 02:43:10 -08:00
|
|
|
*/
|
2020-03-03 00:05:50 -08:00
|
|
|
static decodeTransfer(instruction: TransactionInstruction): TransferParams {
|
|
|
|
this.checkProgramId(instruction.programId);
|
|
|
|
this.checkKeyLength(instruction.keys, 2);
|
|
|
|
|
|
|
|
const {lamports} = decodeData(
|
|
|
|
SYSTEM_INSTRUCTION_LAYOUTS.Transfer,
|
|
|
|
instruction.data,
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
fromPubkey: instruction.keys[0].pubkey,
|
|
|
|
toPubkey: instruction.keys[1].pubkey,
|
|
|
|
lamports,
|
|
|
|
};
|
2020-02-28 02:43:10 -08:00
|
|
|
}
|
|
|
|
|
2021-01-14 08:59:31 -08:00
|
|
|
/**
|
|
|
|
* Decode a transfer with seed system instruction and retrieve the instruction params.
|
|
|
|
*/
|
|
|
|
static decodeTransferWithSeed(
|
|
|
|
instruction: TransactionInstruction,
|
|
|
|
): TransferWithSeedParams {
|
|
|
|
this.checkProgramId(instruction.programId);
|
|
|
|
this.checkKeyLength(instruction.keys, 3);
|
|
|
|
|
|
|
|
const {lamports, seed, programId} = decodeData(
|
|
|
|
SYSTEM_INSTRUCTION_LAYOUTS.TransferWithSeed,
|
|
|
|
instruction.data,
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
fromPubkey: instruction.keys[0].pubkey,
|
|
|
|
basePubkey: instruction.keys[1].pubkey,
|
|
|
|
toPubkey: instruction.keys[2].pubkey,
|
|
|
|
lamports,
|
|
|
|
seed,
|
|
|
|
programId: new PublicKey(programId),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-09 03:33:36 -07:00
|
|
|
/**
|
|
|
|
* Decode an allocate system instruction and retrieve the instruction params.
|
|
|
|
*/
|
|
|
|
static decodeAllocate(instruction: TransactionInstruction): AllocateParams {
|
|
|
|
this.checkProgramId(instruction.programId);
|
|
|
|
this.checkKeyLength(instruction.keys, 1);
|
|
|
|
|
|
|
|
const {space} = decodeData(
|
|
|
|
SYSTEM_INSTRUCTION_LAYOUTS.Allocate,
|
|
|
|
instruction.data,
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
accountPubkey: instruction.keys[0].pubkey,
|
|
|
|
space,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode an allocate with seed system instruction and retrieve the instruction params.
|
|
|
|
*/
|
|
|
|
static decodeAllocateWithSeed(
|
|
|
|
instruction: TransactionInstruction,
|
|
|
|
): AllocateWithSeedParams {
|
|
|
|
this.checkProgramId(instruction.programId);
|
|
|
|
this.checkKeyLength(instruction.keys, 1);
|
|
|
|
|
|
|
|
const {base, seed, space, programId} = decodeData(
|
|
|
|
SYSTEM_INSTRUCTION_LAYOUTS.AllocateWithSeed,
|
|
|
|
instruction.data,
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
accountPubkey: instruction.keys[0].pubkey,
|
|
|
|
basePubkey: new PublicKey(base),
|
|
|
|
seed,
|
|
|
|
space,
|
|
|
|
programId: new PublicKey(programId),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-09-13 17:07:13 -07:00
|
|
|
/**
|
2020-03-03 00:05:50 -08:00
|
|
|
* Decode an assign system instruction and retrieve the instruction params.
|
2019-09-13 17:07:13 -07:00
|
|
|
*/
|
2020-03-03 00:05:50 -08:00
|
|
|
static decodeAssign(instruction: TransactionInstruction): AssignParams {
|
|
|
|
this.checkProgramId(instruction.programId);
|
|
|
|
this.checkKeyLength(instruction.keys, 1);
|
|
|
|
|
|
|
|
const {programId} = decodeData(
|
|
|
|
SYSTEM_INSTRUCTION_LAYOUTS.Assign,
|
|
|
|
instruction.data,
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
2020-05-09 03:33:36 -07:00
|
|
|
accountPubkey: instruction.keys[0].pubkey,
|
|
|
|
programId: new PublicKey(programId),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode an assign with seed system instruction and retrieve the instruction params.
|
|
|
|
*/
|
|
|
|
static decodeAssignWithSeed(
|
|
|
|
instruction: TransactionInstruction,
|
|
|
|
): AssignWithSeedParams {
|
|
|
|
this.checkProgramId(instruction.programId);
|
|
|
|
this.checkKeyLength(instruction.keys, 1);
|
|
|
|
|
|
|
|
const {base, seed, programId} = decodeData(
|
|
|
|
SYSTEM_INSTRUCTION_LAYOUTS.AssignWithSeed,
|
|
|
|
instruction.data,
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
accountPubkey: instruction.keys[0].pubkey,
|
|
|
|
basePubkey: new PublicKey(base),
|
|
|
|
seed,
|
2020-03-03 00:05:50 -08:00
|
|
|
programId: new PublicKey(programId),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode a create account with seed system instruction and retrieve the instruction params.
|
|
|
|
*/
|
|
|
|
static decodeCreateWithSeed(
|
|
|
|
instruction: TransactionInstruction,
|
|
|
|
): CreateAccountWithSeedParams {
|
|
|
|
this.checkProgramId(instruction.programId);
|
|
|
|
this.checkKeyLength(instruction.keys, 2);
|
|
|
|
|
|
|
|
const {base, seed, lamports, space, programId} = decodeData(
|
|
|
|
SYSTEM_INSTRUCTION_LAYOUTS.CreateWithSeed,
|
|
|
|
instruction.data,
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
fromPubkey: instruction.keys[0].pubkey,
|
|
|
|
newAccountPubkey: instruction.keys[1].pubkey,
|
|
|
|
basePubkey: new PublicKey(base),
|
|
|
|
seed,
|
|
|
|
lamports,
|
|
|
|
space,
|
|
|
|
programId: new PublicKey(programId),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode a nonce initialize system instruction and retrieve the instruction params.
|
|
|
|
*/
|
|
|
|
static decodeNonceInitialize(
|
|
|
|
instruction: TransactionInstruction,
|
|
|
|
): InitializeNonceParams {
|
|
|
|
this.checkProgramId(instruction.programId);
|
|
|
|
this.checkKeyLength(instruction.keys, 3);
|
|
|
|
|
|
|
|
const {authorized} = decodeData(
|
|
|
|
SYSTEM_INSTRUCTION_LAYOUTS.InitializeNonceAccount,
|
|
|
|
instruction.data,
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
noncePubkey: instruction.keys[0].pubkey,
|
|
|
|
authorizedPubkey: new PublicKey(authorized),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode a nonce advance system instruction and retrieve the instruction params.
|
|
|
|
*/
|
|
|
|
static decodeNonceAdvance(
|
|
|
|
instruction: TransactionInstruction,
|
|
|
|
): AdvanceNonceParams {
|
|
|
|
this.checkProgramId(instruction.programId);
|
|
|
|
this.checkKeyLength(instruction.keys, 3);
|
|
|
|
|
|
|
|
decodeData(
|
|
|
|
SYSTEM_INSTRUCTION_LAYOUTS.AdvanceNonceAccount,
|
|
|
|
instruction.data,
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
noncePubkey: instruction.keys[0].pubkey,
|
|
|
|
authorizedPubkey: instruction.keys[2].pubkey,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode a nonce withdraw system instruction and retrieve the instruction params.
|
|
|
|
*/
|
|
|
|
static decodeNonceWithdraw(
|
|
|
|
instruction: TransactionInstruction,
|
|
|
|
): WithdrawNonceParams {
|
|
|
|
this.checkProgramId(instruction.programId);
|
|
|
|
this.checkKeyLength(instruction.keys, 5);
|
|
|
|
|
|
|
|
const {lamports} = decodeData(
|
|
|
|
SYSTEM_INSTRUCTION_LAYOUTS.WithdrawNonceAccount,
|
|
|
|
instruction.data,
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
noncePubkey: instruction.keys[0].pubkey,
|
|
|
|
toPubkey: instruction.keys[1].pubkey,
|
|
|
|
authorizedPubkey: instruction.keys[4].pubkey,
|
|
|
|
lamports,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode a nonce authorize system instruction and retrieve the instruction params.
|
|
|
|
*/
|
|
|
|
static decodeNonceAuthorize(
|
|
|
|
instruction: TransactionInstruction,
|
|
|
|
): AuthorizeNonceParams {
|
|
|
|
this.checkProgramId(instruction.programId);
|
|
|
|
this.checkKeyLength(instruction.keys, 2);
|
|
|
|
|
|
|
|
const {authorized} = decodeData(
|
|
|
|
SYSTEM_INSTRUCTION_LAYOUTS.AuthorizeNonceAccount,
|
|
|
|
instruction.data,
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
noncePubkey: instruction.keys[0].pubkey,
|
|
|
|
authorizedPubkey: instruction.keys[1].pubkey,
|
|
|
|
newAuthorizedPubkey: new PublicKey(authorized),
|
|
|
|
};
|
2019-09-13 17:07:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-14 20:01:35 -07:00
|
|
|
* @internal
|
2019-09-13 17:07:13 -07:00
|
|
|
*/
|
2020-03-03 00:05:50 -08:00
|
|
|
static checkProgramId(programId: PublicKey) {
|
|
|
|
if (!programId.equals(SystemProgram.programId)) {
|
|
|
|
throw new Error('invalid instruction; programId is not SystemProgram');
|
2019-09-13 17:07:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-14 20:01:35 -07:00
|
|
|
* @internal
|
2019-09-13 17:07:13 -07:00
|
|
|
*/
|
2020-03-03 00:05:50 -08:00
|
|
|
static checkKeyLength(keys: Array<any>, expectedLength: number) {
|
2020-05-18 20:36:15 -07:00
|
|
|
if (keys.length < expectedLength) {
|
2020-03-03 00:05:50 -08:00
|
|
|
throw new Error(
|
2020-05-18 20:36:15 -07:00
|
|
|
`invalid instruction; found ${keys.length} keys, expected at least ${expectedLength}`,
|
2020-03-03 00:05:50 -08:00
|
|
|
);
|
2019-09-13 17:07:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-02-28 02:43:10 -08:00
|
|
|
* An enumeration of valid SystemInstructionType's
|
|
|
|
*/
|
2021-03-14 20:01:35 -07:00
|
|
|
export type SystemInstructionType =
|
|
|
|
| 'AdvanceNonceAccount'
|
|
|
|
| 'Allocate'
|
|
|
|
| 'AllocateWithSeed'
|
|
|
|
| 'Assign'
|
|
|
|
| 'AssignWithSeed'
|
|
|
|
| 'AuthorizeNonceAccount'
|
|
|
|
| 'Create'
|
|
|
|
| 'CreateWithSeed'
|
|
|
|
| 'InitializeNonceAccount'
|
|
|
|
| 'Transfer'
|
|
|
|
| 'TransferWithSeed'
|
|
|
|
| 'WithdrawNonceAccount';
|
2020-02-28 02:43:10 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An enumeration of valid system InstructionType's
|
2019-09-13 17:07:13 -07:00
|
|
|
*/
|
2021-03-14 20:01:35 -07:00
|
|
|
export const SYSTEM_INSTRUCTION_LAYOUTS: {
|
|
|
|
[type in SystemInstructionType]: InstructionType;
|
|
|
|
} = Object.freeze({
|
2019-09-16 08:46:25 -07:00
|
|
|
Create: {
|
2019-09-13 17:07:13 -07:00
|
|
|
index: 0,
|
|
|
|
layout: BufferLayout.struct([
|
|
|
|
BufferLayout.u32('instruction'),
|
|
|
|
BufferLayout.ns64('lamports'),
|
|
|
|
BufferLayout.ns64('space'),
|
|
|
|
Layout.publicKey('programId'),
|
|
|
|
]),
|
|
|
|
},
|
2019-09-16 08:46:25 -07:00
|
|
|
Assign: {
|
2019-09-13 17:07:13 -07:00
|
|
|
index: 1,
|
|
|
|
layout: BufferLayout.struct([
|
|
|
|
BufferLayout.u32('instruction'),
|
|
|
|
Layout.publicKey('programId'),
|
|
|
|
]),
|
|
|
|
},
|
2019-09-16 08:46:25 -07:00
|
|
|
Transfer: {
|
2019-09-13 17:07:13 -07:00
|
|
|
index: 2,
|
|
|
|
layout: BufferLayout.struct([
|
|
|
|
BufferLayout.u32('instruction'),
|
2020-01-02 16:58:54 -08:00
|
|
|
BufferLayout.ns64('lamports'),
|
2019-09-13 17:07:13 -07:00
|
|
|
]),
|
|
|
|
},
|
2019-12-09 19:50:01 -08:00
|
|
|
CreateWithSeed: {
|
|
|
|
index: 3,
|
|
|
|
layout: BufferLayout.struct([
|
|
|
|
BufferLayout.u32('instruction'),
|
2019-12-29 14:16:32 -08:00
|
|
|
Layout.publicKey('base'),
|
2019-12-09 19:50:01 -08:00
|
|
|
Layout.rustString('seed'),
|
|
|
|
BufferLayout.ns64('lamports'),
|
|
|
|
BufferLayout.ns64('space'),
|
|
|
|
Layout.publicKey('programId'),
|
|
|
|
]),
|
|
|
|
},
|
2020-01-13 14:31:02 -08:00
|
|
|
AdvanceNonceAccount: {
|
2020-01-02 16:58:54 -08:00
|
|
|
index: 4,
|
|
|
|
layout: BufferLayout.struct([BufferLayout.u32('instruction')]),
|
|
|
|
},
|
2020-01-13 14:31:02 -08:00
|
|
|
WithdrawNonceAccount: {
|
2020-01-02 16:58:54 -08:00
|
|
|
index: 5,
|
|
|
|
layout: BufferLayout.struct([
|
|
|
|
BufferLayout.u32('instruction'),
|
|
|
|
BufferLayout.ns64('lamports'),
|
|
|
|
]),
|
|
|
|
},
|
2020-01-13 14:31:02 -08:00
|
|
|
InitializeNonceAccount: {
|
2020-01-02 16:58:54 -08:00
|
|
|
index: 6,
|
|
|
|
layout: BufferLayout.struct([
|
|
|
|
BufferLayout.u32('instruction'),
|
|
|
|
Layout.publicKey('authorized'),
|
|
|
|
]),
|
|
|
|
},
|
2020-01-13 14:31:02 -08:00
|
|
|
AuthorizeNonceAccount: {
|
2020-01-02 16:58:54 -08:00
|
|
|
index: 7,
|
|
|
|
layout: BufferLayout.struct([
|
|
|
|
BufferLayout.u32('instruction'),
|
|
|
|
Layout.publicKey('authorized'),
|
|
|
|
]),
|
|
|
|
},
|
2020-05-09 03:33:36 -07:00
|
|
|
Allocate: {
|
|
|
|
index: 8,
|
|
|
|
layout: BufferLayout.struct([
|
|
|
|
BufferLayout.u32('instruction'),
|
|
|
|
BufferLayout.ns64('space'),
|
|
|
|
]),
|
|
|
|
},
|
|
|
|
AllocateWithSeed: {
|
|
|
|
index: 9,
|
|
|
|
layout: BufferLayout.struct([
|
|
|
|
BufferLayout.u32('instruction'),
|
|
|
|
Layout.publicKey('base'),
|
|
|
|
Layout.rustString('seed'),
|
|
|
|
BufferLayout.ns64('space'),
|
|
|
|
Layout.publicKey('programId'),
|
|
|
|
]),
|
|
|
|
},
|
|
|
|
AssignWithSeed: {
|
|
|
|
index: 10,
|
|
|
|
layout: BufferLayout.struct([
|
|
|
|
BufferLayout.u32('instruction'),
|
|
|
|
Layout.publicKey('base'),
|
|
|
|
Layout.rustString('seed'),
|
|
|
|
Layout.publicKey('programId'),
|
|
|
|
]),
|
|
|
|
},
|
2021-01-14 08:59:31 -08:00
|
|
|
TransferWithSeed: {
|
|
|
|
index: 11,
|
|
|
|
layout: BufferLayout.struct([
|
|
|
|
BufferLayout.u32('instruction'),
|
|
|
|
BufferLayout.ns64('lamports'),
|
|
|
|
Layout.rustString('seed'),
|
|
|
|
Layout.publicKey('programId'),
|
|
|
|
]),
|
|
|
|
},
|
2019-09-13 17:07:13 -07:00
|
|
|
});
|
|
|
|
|
2018-09-18 12:46:59 -07:00
|
|
|
/**
|
2018-09-20 10:10:46 -07:00
|
|
|
* Factory class for transactions to interact with the System program
|
2018-09-18 12:46:59 -07:00
|
|
|
*/
|
2018-09-20 10:10:46 -07:00
|
|
|
export class SystemProgram {
|
2021-03-14 20:01:35 -07:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
constructor() {}
|
|
|
|
|
2018-09-18 12:46:59 -07:00
|
|
|
/**
|
2018-09-20 10:10:46 -07:00
|
|
|
* Public key that identifies the System program
|
2018-09-18 12:46:59 -07:00
|
|
|
*/
|
2018-09-20 10:10:46 -07:00
|
|
|
static get programId(): PublicKey {
|
2020-04-07 17:18:21 -07:00
|
|
|
return new PublicKey('11111111111111111111111111111111');
|
2018-09-18 12:46:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-10 00:43:32 -07:00
|
|
|
* Generate a transaction instruction that creates a new account
|
2018-09-18 12:46:59 -07:00
|
|
|
*/
|
2020-09-10 00:43:32 -07:00
|
|
|
static createAccount(params: CreateAccountParams): TransactionInstruction {
|
2020-02-28 02:43:10 -08:00
|
|
|
const type = SYSTEM_INSTRUCTION_LAYOUTS.Create;
|
2019-09-13 17:07:13 -07:00
|
|
|
const data = encodeData(type, {
|
2020-03-03 00:05:50 -08:00
|
|
|
lamports: params.lamports,
|
|
|
|
space: params.space,
|
2021-03-28 20:00:56 -07:00
|
|
|
programId: toBuffer(params.programId.toBytes()),
|
2019-09-13 17:07:13 -07:00
|
|
|
});
|
2018-09-18 12:46:59 -07:00
|
|
|
|
2020-09-10 00:43:32 -07:00
|
|
|
return new TransactionInstruction({
|
2019-04-10 12:31:50 -07:00
|
|
|
keys: [
|
2020-03-03 00:05:50 -08:00
|
|
|
{pubkey: params.fromPubkey, isSigner: true, isWritable: true},
|
|
|
|
{pubkey: params.newAccountPubkey, isSigner: true, isWritable: true},
|
2019-04-10 12:31:50 -07:00
|
|
|
],
|
2020-01-02 16:58:54 -08:00
|
|
|
programId: this.programId,
|
2019-03-14 13:27:47 -07:00
|
|
|
data,
|
2018-09-18 12:46:59 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-10 00:43:32 -07:00
|
|
|
* Generate a transaction instruction that transfers lamports from one account to another
|
2018-09-18 12:46:59 -07:00
|
|
|
*/
|
2021-01-14 08:59:31 -08:00
|
|
|
static transfer(
|
|
|
|
params: TransferParams | TransferWithSeedParams,
|
|
|
|
): TransactionInstruction {
|
|
|
|
let data;
|
|
|
|
let keys;
|
2021-03-14 20:01:35 -07:00
|
|
|
if ('basePubkey' in params) {
|
2021-01-14 08:59:31 -08:00
|
|
|
const type = SYSTEM_INSTRUCTION_LAYOUTS.TransferWithSeed;
|
|
|
|
data = encodeData(type, {
|
|
|
|
lamports: params.lamports,
|
|
|
|
seed: params.seed,
|
2021-03-28 20:00:56 -07:00
|
|
|
programId: toBuffer(params.programId.toBytes()),
|
2021-01-14 08:59:31 -08:00
|
|
|
});
|
|
|
|
keys = [
|
|
|
|
{pubkey: params.fromPubkey, isSigner: false, isWritable: true},
|
|
|
|
{pubkey: params.basePubkey, isSigner: true, isWritable: false},
|
|
|
|
{pubkey: params.toPubkey, isSigner: false, isWritable: true},
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
const type = SYSTEM_INSTRUCTION_LAYOUTS.Transfer;
|
|
|
|
data = encodeData(type, {lamports: params.lamports});
|
|
|
|
keys = [
|
2020-03-03 00:05:50 -08:00
|
|
|
{pubkey: params.fromPubkey, isSigner: true, isWritable: true},
|
|
|
|
{pubkey: params.toPubkey, isSigner: false, isWritable: true},
|
2021-01-14 08:59:31 -08:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return new TransactionInstruction({
|
|
|
|
keys,
|
2020-01-02 16:58:54 -08:00
|
|
|
programId: this.programId,
|
2019-03-14 13:27:47 -07:00
|
|
|
data,
|
2018-09-18 12:46:59 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-10 00:43:32 -07:00
|
|
|
* Generate a transaction instruction that assigns an account to a program
|
2018-09-18 12:46:59 -07:00
|
|
|
*/
|
2020-09-10 00:43:32 -07:00
|
|
|
static assign(
|
|
|
|
params: AssignParams | AssignWithSeedParams,
|
|
|
|
): TransactionInstruction {
|
2020-05-09 03:33:36 -07:00
|
|
|
let data;
|
2021-01-14 08:59:31 -08:00
|
|
|
let keys;
|
2021-03-14 20:01:35 -07:00
|
|
|
if ('basePubkey' in params) {
|
2020-05-09 03:33:36 -07:00
|
|
|
const type = SYSTEM_INSTRUCTION_LAYOUTS.AssignWithSeed;
|
|
|
|
data = encodeData(type, {
|
2021-03-28 20:00:56 -07:00
|
|
|
base: toBuffer(params.basePubkey.toBytes()),
|
2020-05-09 03:33:36 -07:00
|
|
|
seed: params.seed,
|
2021-03-28 20:00:56 -07:00
|
|
|
programId: toBuffer(params.programId.toBytes()),
|
2020-05-09 03:33:36 -07:00
|
|
|
});
|
2021-01-14 08:59:31 -08:00
|
|
|
keys = [
|
|
|
|
{pubkey: params.accountPubkey, isSigner: false, isWritable: true},
|
|
|
|
{pubkey: params.basePubkey, isSigner: true, isWritable: false},
|
|
|
|
];
|
2020-05-09 03:33:36 -07:00
|
|
|
} else {
|
|
|
|
const type = SYSTEM_INSTRUCTION_LAYOUTS.Assign;
|
2021-03-28 20:00:56 -07:00
|
|
|
data = encodeData(type, {
|
|
|
|
programId: toBuffer(params.programId.toBytes()),
|
|
|
|
});
|
2021-01-14 08:59:31 -08:00
|
|
|
keys = [{pubkey: params.accountPubkey, isSigner: true, isWritable: true}];
|
2020-05-09 03:33:36 -07:00
|
|
|
}
|
2018-09-18 12:46:59 -07:00
|
|
|
|
2020-09-10 00:43:32 -07:00
|
|
|
return new TransactionInstruction({
|
2021-01-14 08:59:31 -08:00
|
|
|
keys,
|
2020-01-02 16:58:54 -08:00
|
|
|
programId: this.programId,
|
2019-03-14 13:27:47 -07:00
|
|
|
data,
|
2018-09-18 12:46:59 -07:00
|
|
|
});
|
|
|
|
}
|
2019-12-09 19:50:01 -08:00
|
|
|
|
|
|
|
/**
|
2020-09-10 00:43:32 -07:00
|
|
|
* Generate a transaction instruction that creates a new account at
|
2019-12-09 19:50:01 -08:00
|
|
|
* an address generated with `from`, a seed, and programId
|
|
|
|
*/
|
|
|
|
static createAccountWithSeed(
|
2020-03-03 00:05:50 -08:00
|
|
|
params: CreateAccountWithSeedParams,
|
2020-09-10 00:43:32 -07:00
|
|
|
): TransactionInstruction {
|
2020-02-28 02:43:10 -08:00
|
|
|
const type = SYSTEM_INSTRUCTION_LAYOUTS.CreateWithSeed;
|
2019-12-09 19:50:01 -08:00
|
|
|
const data = encodeData(type, {
|
2021-03-28 20:00:56 -07:00
|
|
|
base: toBuffer(params.basePubkey.toBytes()),
|
2020-03-03 00:05:50 -08:00
|
|
|
seed: params.seed,
|
|
|
|
lamports: params.lamports,
|
|
|
|
space: params.space,
|
2021-03-28 20:00:56 -07:00
|
|
|
programId: toBuffer(params.programId.toBytes()),
|
2019-12-09 19:50:01 -08:00
|
|
|
});
|
2021-02-25 22:47:22 -08:00
|
|
|
let keys = [
|
|
|
|
{pubkey: params.fromPubkey, isSigner: true, isWritable: true},
|
|
|
|
{pubkey: params.newAccountPubkey, isSigner: false, isWritable: true},
|
|
|
|
];
|
|
|
|
if (params.basePubkey != params.fromPubkey) {
|
|
|
|
keys.push({pubkey: params.basePubkey, isSigner: true, isWritable: false});
|
|
|
|
}
|
2019-12-09 19:50:01 -08:00
|
|
|
|
2020-09-10 00:43:32 -07:00
|
|
|
return new TransactionInstruction({
|
2021-02-25 22:47:22 -08:00
|
|
|
keys,
|
2020-01-02 16:58:54 -08:00
|
|
|
programId: this.programId,
|
|
|
|
data,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-10 00:43:32 -07:00
|
|
|
* Generate a transaction that creates a new Nonce account
|
2020-01-02 16:58:54 -08:00
|
|
|
*/
|
2020-05-04 22:30:55 -07:00
|
|
|
static createNonceAccount(
|
|
|
|
params: CreateNonceAccountParams | CreateNonceAccountWithSeedParams,
|
|
|
|
): Transaction {
|
2020-09-10 00:43:32 -07:00
|
|
|
const transaction = new Transaction();
|
2021-03-14 20:01:35 -07:00
|
|
|
if ('basePubkey' in params && 'seed' in params) {
|
2020-09-10 00:43:32 -07:00
|
|
|
transaction.add(
|
|
|
|
SystemProgram.createAccountWithSeed({
|
|
|
|
fromPubkey: params.fromPubkey,
|
|
|
|
newAccountPubkey: params.noncePubkey,
|
|
|
|
basePubkey: params.basePubkey,
|
|
|
|
seed: params.seed,
|
|
|
|
lamports: params.lamports,
|
|
|
|
space: NONCE_ACCOUNT_LENGTH,
|
|
|
|
programId: this.programId,
|
|
|
|
}),
|
|
|
|
);
|
2020-05-04 22:30:55 -07:00
|
|
|
} else {
|
2020-09-10 00:43:32 -07:00
|
|
|
transaction.add(
|
|
|
|
SystemProgram.createAccount({
|
|
|
|
fromPubkey: params.fromPubkey,
|
|
|
|
newAccountPubkey: params.noncePubkey,
|
|
|
|
lamports: params.lamports,
|
|
|
|
space: NONCE_ACCOUNT_LENGTH,
|
|
|
|
programId: this.programId,
|
|
|
|
}),
|
|
|
|
);
|
2020-05-04 22:30:55 -07:00
|
|
|
}
|
2020-01-02 16:58:54 -08:00
|
|
|
|
2020-03-03 00:05:50 -08:00
|
|
|
const initParams = {
|
|
|
|
noncePubkey: params.noncePubkey,
|
|
|
|
authorizedPubkey: params.authorizedPubkey,
|
|
|
|
};
|
|
|
|
|
|
|
|
transaction.add(this.nonceInitialize(initParams));
|
|
|
|
return transaction;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate an instruction to initialize a Nonce account
|
|
|
|
*/
|
|
|
|
static nonceInitialize(
|
|
|
|
params: InitializeNonceParams,
|
|
|
|
): TransactionInstruction {
|
2020-02-28 02:43:10 -08:00
|
|
|
const type = SYSTEM_INSTRUCTION_LAYOUTS.InitializeNonceAccount;
|
2020-01-02 16:58:54 -08:00
|
|
|
const data = encodeData(type, {
|
2021-03-28 20:00:56 -07:00
|
|
|
authorized: toBuffer(params.authorizedPubkey.toBytes()),
|
2020-01-02 16:58:54 -08:00
|
|
|
});
|
2020-03-03 00:05:50 -08:00
|
|
|
const instructionData = {
|
2020-01-02 16:58:54 -08:00
|
|
|
keys: [
|
2020-03-03 00:05:50 -08:00
|
|
|
{pubkey: params.noncePubkey, isSigner: false, isWritable: true},
|
2020-01-02 16:58:54 -08:00
|
|
|
{
|
|
|
|
pubkey: SYSVAR_RECENT_BLOCKHASHES_PUBKEY,
|
|
|
|
isSigner: false,
|
|
|
|
isWritable: false,
|
|
|
|
},
|
|
|
|
{pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false},
|
|
|
|
],
|
|
|
|
programId: this.programId,
|
|
|
|
data,
|
2020-03-03 00:05:50 -08:00
|
|
|
};
|
|
|
|
return new TransactionInstruction(instructionData);
|
2020-01-02 16:58:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate an instruction to advance the nonce in a Nonce account
|
|
|
|
*/
|
2020-03-03 00:05:50 -08:00
|
|
|
static nonceAdvance(params: AdvanceNonceParams): TransactionInstruction {
|
2020-02-28 02:43:10 -08:00
|
|
|
const type = SYSTEM_INSTRUCTION_LAYOUTS.AdvanceNonceAccount;
|
2020-01-02 16:58:54 -08:00
|
|
|
const data = encodeData(type);
|
|
|
|
const instructionData = {
|
|
|
|
keys: [
|
2020-03-03 00:05:50 -08:00
|
|
|
{pubkey: params.noncePubkey, isSigner: false, isWritable: true},
|
2020-01-02 16:58:54 -08:00
|
|
|
{
|
|
|
|
pubkey: SYSVAR_RECENT_BLOCKHASHES_PUBKEY,
|
|
|
|
isSigner: false,
|
|
|
|
isWritable: false,
|
|
|
|
},
|
2020-03-03 00:05:50 -08:00
|
|
|
{pubkey: params.authorizedPubkey, isSigner: true, isWritable: false},
|
2020-01-02 16:58:54 -08:00
|
|
|
],
|
|
|
|
programId: this.programId,
|
|
|
|
data,
|
|
|
|
};
|
|
|
|
return new TransactionInstruction(instructionData);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-10 00:43:32 -07:00
|
|
|
* Generate a transaction instruction that withdraws lamports from a Nonce account
|
2020-01-02 16:58:54 -08:00
|
|
|
*/
|
2020-09-10 00:43:32 -07:00
|
|
|
static nonceWithdraw(params: WithdrawNonceParams): TransactionInstruction {
|
2020-02-28 02:43:10 -08:00
|
|
|
const type = SYSTEM_INSTRUCTION_LAYOUTS.WithdrawNonceAccount;
|
2020-03-03 00:05:50 -08:00
|
|
|
const data = encodeData(type, {lamports: params.lamports});
|
2020-01-02 16:58:54 -08:00
|
|
|
|
2020-09-10 00:43:32 -07:00
|
|
|
return new TransactionInstruction({
|
2020-01-02 16:58:54 -08:00
|
|
|
keys: [
|
2020-03-03 00:05:50 -08:00
|
|
|
{pubkey: params.noncePubkey, isSigner: false, isWritable: true},
|
|
|
|
{pubkey: params.toPubkey, isSigner: false, isWritable: true},
|
2020-01-02 16:58:54 -08:00
|
|
|
{
|
|
|
|
pubkey: SYSVAR_RECENT_BLOCKHASHES_PUBKEY,
|
|
|
|
isSigner: false,
|
|
|
|
isWritable: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pubkey: SYSVAR_RENT_PUBKEY,
|
|
|
|
isSigner: false,
|
|
|
|
isWritable: false,
|
|
|
|
},
|
2020-03-03 00:05:50 -08:00
|
|
|
{pubkey: params.authorizedPubkey, isSigner: true, isWritable: false},
|
2020-01-02 16:58:54 -08:00
|
|
|
],
|
|
|
|
programId: this.programId,
|
|
|
|
data,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-10 00:43:32 -07:00
|
|
|
* Generate a transaction instruction that authorizes a new PublicKey as the authority
|
2020-01-02 16:58:54 -08:00
|
|
|
* on a Nonce account.
|
|
|
|
*/
|
2020-09-10 00:43:32 -07:00
|
|
|
static nonceAuthorize(params: AuthorizeNonceParams): TransactionInstruction {
|
2020-02-28 02:43:10 -08:00
|
|
|
const type = SYSTEM_INSTRUCTION_LAYOUTS.AuthorizeNonceAccount;
|
2020-01-02 16:58:54 -08:00
|
|
|
const data = encodeData(type, {
|
2021-03-28 20:00:56 -07:00
|
|
|
authorized: toBuffer(params.newAuthorizedPubkey.toBytes()),
|
2020-01-02 16:58:54 -08:00
|
|
|
});
|
|
|
|
|
2020-09-10 00:43:32 -07:00
|
|
|
return new TransactionInstruction({
|
2020-01-02 16:58:54 -08:00
|
|
|
keys: [
|
2020-03-03 00:05:50 -08:00
|
|
|
{pubkey: params.noncePubkey, isSigner: false, isWritable: true},
|
|
|
|
{pubkey: params.authorizedPubkey, isSigner: true, isWritable: false},
|
2020-01-02 16:58:54 -08:00
|
|
|
],
|
|
|
|
programId: this.programId,
|
2019-12-09 19:50:01 -08:00
|
|
|
data,
|
|
|
|
});
|
|
|
|
}
|
2020-05-09 03:33:36 -07:00
|
|
|
|
|
|
|
/**
|
2020-09-10 00:43:32 -07:00
|
|
|
* Generate a transaction instruction that allocates space in an account without funding
|
2020-05-09 03:33:36 -07:00
|
|
|
*/
|
|
|
|
static allocate(
|
|
|
|
params: AllocateParams | AllocateWithSeedParams,
|
2020-09-10 00:43:32 -07:00
|
|
|
): TransactionInstruction {
|
2020-05-09 03:33:36 -07:00
|
|
|
let data;
|
2021-01-14 08:59:31 -08:00
|
|
|
let keys;
|
2021-03-14 20:01:35 -07:00
|
|
|
if ('basePubkey' in params) {
|
2020-05-09 03:33:36 -07:00
|
|
|
const type = SYSTEM_INSTRUCTION_LAYOUTS.AllocateWithSeed;
|
|
|
|
data = encodeData(type, {
|
2021-03-28 20:00:56 -07:00
|
|
|
base: toBuffer(params.basePubkey.toBytes()),
|
2020-05-09 03:33:36 -07:00
|
|
|
seed: params.seed,
|
|
|
|
space: params.space,
|
2021-03-28 20:00:56 -07:00
|
|
|
programId: toBuffer(params.programId.toBytes()),
|
2020-05-09 03:33:36 -07:00
|
|
|
});
|
2021-01-14 08:59:31 -08:00
|
|
|
keys = [
|
|
|
|
{pubkey: params.accountPubkey, isSigner: false, isWritable: true},
|
|
|
|
{pubkey: params.basePubkey, isSigner: true, isWritable: false},
|
|
|
|
];
|
2020-05-09 03:33:36 -07:00
|
|
|
} else {
|
|
|
|
const type = SYSTEM_INSTRUCTION_LAYOUTS.Allocate;
|
|
|
|
data = encodeData(type, {
|
|
|
|
space: params.space,
|
|
|
|
});
|
2021-01-14 08:59:31 -08:00
|
|
|
keys = [{pubkey: params.accountPubkey, isSigner: true, isWritable: true}];
|
2020-05-09 03:33:36 -07:00
|
|
|
}
|
|
|
|
|
2020-09-10 00:43:32 -07:00
|
|
|
return new TransactionInstruction({
|
2021-01-14 08:59:31 -08:00
|
|
|
keys,
|
2020-05-09 03:33:36 -07:00
|
|
|
programId: this.programId,
|
|
|
|
data,
|
|
|
|
});
|
|
|
|
}
|
2018-09-18 12:46:59 -07:00
|
|
|
}
|