@solana/web3.js 0.0.0-next

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +158 -0
  3. package/lib/index.browser.cjs.js +10062 -0
  4. package/lib/index.browser.cjs.js.map +1 -0
  5. package/lib/index.browser.esm.js +9976 -0
  6. package/lib/index.browser.esm.js.map +1 -0
  7. package/lib/index.cjs.js +9568 -0
  8. package/lib/index.cjs.js.map +1 -0
  9. package/lib/index.d.ts +3311 -0
  10. package/lib/index.esm.js +9479 -0
  11. package/lib/index.esm.js.map +1 -0
  12. package/lib/index.iife.js +30136 -0
  13. package/lib/index.iife.js.map +1 -0
  14. package/lib/index.iife.min.js +40 -0
  15. package/lib/index.iife.min.js.map +1 -0
  16. package/package.json +140 -0
  17. package/src/account.ts +46 -0
  18. package/src/agent-manager.ts +44 -0
  19. package/src/blockhash.ts +4 -0
  20. package/src/bpf-loader-deprecated.ts +5 -0
  21. package/src/bpf-loader.ts +45 -0
  22. package/src/connection.ts +4935 -0
  23. package/src/ed25519-program.ts +157 -0
  24. package/src/epoch-schedule.ts +102 -0
  25. package/src/errors.ts +9 -0
  26. package/src/fee-calculator.ts +16 -0
  27. package/src/index.ts +31 -0
  28. package/src/instruction.ts +58 -0
  29. package/src/keypair.ts +98 -0
  30. package/src/layout.ts +147 -0
  31. package/src/loader.ts +236 -0
  32. package/src/message.ts +271 -0
  33. package/src/nonce-account.ts +78 -0
  34. package/src/publickey.ts +296 -0
  35. package/src/secp256k1-program.ts +229 -0
  36. package/src/stake-program.ts +923 -0
  37. package/src/system-program.ts +1007 -0
  38. package/src/sysvar.ts +37 -0
  39. package/src/timing.ts +23 -0
  40. package/src/transaction.ts +808 -0
  41. package/src/util/assert.ts +8 -0
  42. package/src/util/borsh-schema.ts +38 -0
  43. package/src/util/cluster.ts +31 -0
  44. package/src/util/promise-timeout.ts +14 -0
  45. package/src/util/send-and-confirm-raw-transaction.ts +46 -0
  46. package/src/util/send-and-confirm-transaction.ts +50 -0
  47. package/src/util/shortvec-encoding.ts +28 -0
  48. package/src/util/sleep.ts +4 -0
  49. package/src/util/to-buffer.ts +11 -0
  50. package/src/util/url.ts +18 -0
  51. package/src/validator-info.ts +106 -0
  52. package/src/vote-account.ts +236 -0
  53. package/src/vote-program.ts +413 -0
@@ -0,0 +1,1007 @@
1
+ import * as BufferLayout from '@solana/buffer-layout';
2
+
3
+ import {
4
+ encodeData,
5
+ decodeData,
6
+ InstructionType,
7
+ IInstructionInputData,
8
+ } from './instruction';
9
+ import * as Layout from './layout';
10
+ import {NONCE_ACCOUNT_LENGTH} from './nonce-account';
11
+ import {PublicKey} from './publickey';
12
+ import {SYSVAR_RECENT_BLOCKHASHES_PUBKEY, SYSVAR_RENT_PUBKEY} from './sysvar';
13
+ import {Transaction, TransactionInstruction} from './transaction';
14
+ import {toBuffer} from './util/to-buffer';
15
+
16
+ /**
17
+ * Create account system transaction params
18
+ */
19
+ export type CreateAccountParams = {
20
+ /** The account that will transfer lamports to the created account */
21
+ fromPubkey: PublicKey;
22
+ /** Public key of the created account */
23
+ newAccountPubkey: PublicKey;
24
+ /** Amount of lamports to transfer to the created account */
25
+ lamports: number;
26
+ /** Amount of space in bytes to allocate to the created account */
27
+ space: number;
28
+ /** Public key of the program to assign as the owner of the created account */
29
+ programId: PublicKey;
30
+ };
31
+
32
+ /**
33
+ * Transfer system transaction params
34
+ */
35
+ export type TransferParams = {
36
+ /** Account that will transfer lamports */
37
+ fromPubkey: PublicKey;
38
+ /** Account that will receive transferred lamports */
39
+ toPubkey: PublicKey;
40
+ /** Amount of lamports to transfer */
41
+ lamports: number;
42
+ };
43
+
44
+ /**
45
+ * Assign system transaction params
46
+ */
47
+ export type AssignParams = {
48
+ /** Public key of the account which will be assigned a new owner */
49
+ accountPubkey: PublicKey;
50
+ /** Public key of the program to assign as the owner */
51
+ programId: PublicKey;
52
+ };
53
+
54
+ /**
55
+ * Create account with seed system transaction params
56
+ */
57
+ export type CreateAccountWithSeedParams = {
58
+ /** The account that will transfer lamports to the created account */
59
+ fromPubkey: PublicKey;
60
+ /** Public key of the created account. Must be pre-calculated with PublicKey.createWithSeed() */
61
+ newAccountPubkey: PublicKey;
62
+ /** Base public key to use to derive the address of the created account. Must be the same as the base key used to create `newAccountPubkey` */
63
+ basePubkey: PublicKey;
64
+ /** Seed to use to derive the address of the created account. Must be the same as the seed used to create `newAccountPubkey` */
65
+ seed: string;
66
+ /** Amount of lamports to transfer to the created account */
67
+ lamports: number;
68
+ /** Amount of space in bytes to allocate to the created account */
69
+ space: number;
70
+ /** Public key of the program to assign as the owner of the created account */
71
+ programId: PublicKey;
72
+ };
73
+
74
+ /**
75
+ * Create nonce account system transaction params
76
+ */
77
+ export type CreateNonceAccountParams = {
78
+ /** The account that will transfer lamports to the created nonce account */
79
+ fromPubkey: PublicKey;
80
+ /** Public key of the created nonce account */
81
+ noncePubkey: PublicKey;
82
+ /** Public key to set as authority of the created nonce account */
83
+ authorizedPubkey: PublicKey;
84
+ /** Amount of lamports to transfer to the created nonce account */
85
+ lamports: number;
86
+ };
87
+
88
+ /**
89
+ * Create nonce account with seed system transaction params
90
+ */
91
+ export type CreateNonceAccountWithSeedParams = {
92
+ /** The account that will transfer lamports to the created nonce account */
93
+ fromPubkey: PublicKey;
94
+ /** Public key of the created nonce account */
95
+ noncePubkey: PublicKey;
96
+ /** Public key to set as authority of the created nonce account */
97
+ authorizedPubkey: PublicKey;
98
+ /** Amount of lamports to transfer to the created nonce account */
99
+ lamports: number;
100
+ /** Base public key to use to derive the address of the nonce account */
101
+ basePubkey: PublicKey;
102
+ /** Seed to use to derive the address of the nonce account */
103
+ seed: string;
104
+ };
105
+
106
+ /**
107
+ * Initialize nonce account system instruction params
108
+ */
109
+ export type InitializeNonceParams = {
110
+ /** Nonce account which will be initialized */
111
+ noncePubkey: PublicKey;
112
+ /** Public key to set as authority of the initialized nonce account */
113
+ authorizedPubkey: PublicKey;
114
+ };
115
+
116
+ /**
117
+ * Advance nonce account system instruction params
118
+ */
119
+ export type AdvanceNonceParams = {
120
+ /** Nonce account */
121
+ noncePubkey: PublicKey;
122
+ /** Public key of the nonce authority */
123
+ authorizedPubkey: PublicKey;
124
+ };
125
+
126
+ /**
127
+ * Withdraw nonce account system transaction params
128
+ */
129
+ export type WithdrawNonceParams = {
130
+ /** Nonce account */
131
+ noncePubkey: PublicKey;
132
+ /** Public key of the nonce authority */
133
+ authorizedPubkey: PublicKey;
134
+ /** Public key of the account which will receive the withdrawn nonce account balance */
135
+ toPubkey: PublicKey;
136
+ /** Amount of lamports to withdraw from the nonce account */
137
+ lamports: number;
138
+ };
139
+
140
+ /**
141
+ * Authorize nonce account system transaction params
142
+ */
143
+ export type AuthorizeNonceParams = {
144
+ /** Nonce account */
145
+ noncePubkey: PublicKey;
146
+ /** Public key of the current nonce authority */
147
+ authorizedPubkey: PublicKey;
148
+ /** Public key to set as the new nonce authority */
149
+ newAuthorizedPubkey: PublicKey;
150
+ };
151
+
152
+ /**
153
+ * Allocate account system transaction params
154
+ */
155
+ export type AllocateParams = {
156
+ /** Account to allocate */
157
+ accountPubkey: PublicKey;
158
+ /** Amount of space in bytes to allocate */
159
+ space: number;
160
+ };
161
+
162
+ /**
163
+ * Allocate account with seed system transaction params
164
+ */
165
+ export type AllocateWithSeedParams = {
166
+ /** Account to allocate */
167
+ accountPubkey: PublicKey;
168
+ /** Base public key to use to derive the address of the allocated account */
169
+ basePubkey: PublicKey;
170
+ /** Seed to use to derive the address of the allocated account */
171
+ seed: string;
172
+ /** Amount of space in bytes to allocate */
173
+ space: number;
174
+ /** Public key of the program to assign as the owner of the allocated account */
175
+ programId: PublicKey;
176
+ };
177
+
178
+ /**
179
+ * Assign account with seed system transaction params
180
+ */
181
+ export type AssignWithSeedParams = {
182
+ /** Public key of the account which will be assigned a new owner */
183
+ accountPubkey: PublicKey;
184
+ /** Base public key to use to derive the address of the assigned account */
185
+ basePubkey: PublicKey;
186
+ /** Seed to use to derive the address of the assigned account */
187
+ seed: string;
188
+ /** Public key of the program to assign as the owner */
189
+ programId: PublicKey;
190
+ };
191
+
192
+ /**
193
+ * Transfer with seed system transaction params
194
+ */
195
+ export type TransferWithSeedParams = {
196
+ /** Account that will transfer lamports */
197
+ fromPubkey: PublicKey;
198
+ /** Base public key to use to derive the funding account address */
199
+ basePubkey: PublicKey;
200
+ /** Account that will receive transferred lamports */
201
+ toPubkey: PublicKey;
202
+ /** Amount of lamports to transfer */
203
+ lamports: number;
204
+ /** Seed to use to derive the funding account address */
205
+ seed: string;
206
+ /** Program id to use to derive the funding account address */
207
+ programId: PublicKey;
208
+ };
209
+
210
+ /**
211
+ * System Instruction class
212
+ */
213
+ export class SystemInstruction {
214
+ /**
215
+ * @internal
216
+ */
217
+ constructor() {}
218
+
219
+ /**
220
+ * Decode a system instruction and retrieve the instruction type.
221
+ */
222
+ static decodeInstructionType(
223
+ instruction: TransactionInstruction,
224
+ ): SystemInstructionType {
225
+ this.checkProgramId(instruction.programId);
226
+
227
+ const instructionTypeLayout = BufferLayout.u32('instruction');
228
+ const typeIndex = instructionTypeLayout.decode(instruction.data);
229
+
230
+ let type: SystemInstructionType | undefined;
231
+ for (const [ixType, layout] of Object.entries(SYSTEM_INSTRUCTION_LAYOUTS)) {
232
+ if (layout.index == typeIndex) {
233
+ type = ixType as SystemInstructionType;
234
+ break;
235
+ }
236
+ }
237
+
238
+ if (!type) {
239
+ throw new Error('Instruction type incorrect; not a SystemInstruction');
240
+ }
241
+
242
+ return type;
243
+ }
244
+
245
+ /**
246
+ * Decode a create account system instruction and retrieve the instruction params.
247
+ */
248
+ static decodeCreateAccount(
249
+ instruction: TransactionInstruction,
250
+ ): CreateAccountParams {
251
+ this.checkProgramId(instruction.programId);
252
+ this.checkKeyLength(instruction.keys, 2);
253
+
254
+ const {lamports, space, programId} = decodeData(
255
+ SYSTEM_INSTRUCTION_LAYOUTS.Create,
256
+ instruction.data,
257
+ );
258
+
259
+ return {
260
+ fromPubkey: instruction.keys[0].pubkey,
261
+ newAccountPubkey: instruction.keys[1].pubkey,
262
+ lamports,
263
+ space,
264
+ programId: new PublicKey(programId),
265
+ };
266
+ }
267
+
268
+ /**
269
+ * Decode a transfer system instruction and retrieve the instruction params.
270
+ */
271
+ static decodeTransfer(instruction: TransactionInstruction): TransferParams {
272
+ this.checkProgramId(instruction.programId);
273
+ this.checkKeyLength(instruction.keys, 2);
274
+
275
+ const {lamports} = decodeData(
276
+ SYSTEM_INSTRUCTION_LAYOUTS.Transfer,
277
+ instruction.data,
278
+ );
279
+
280
+ return {
281
+ fromPubkey: instruction.keys[0].pubkey,
282
+ toPubkey: instruction.keys[1].pubkey,
283
+ lamports,
284
+ };
285
+ }
286
+
287
+ /**
288
+ * Decode a transfer with seed system instruction and retrieve the instruction params.
289
+ */
290
+ static decodeTransferWithSeed(
291
+ instruction: TransactionInstruction,
292
+ ): TransferWithSeedParams {
293
+ this.checkProgramId(instruction.programId);
294
+ this.checkKeyLength(instruction.keys, 3);
295
+
296
+ const {lamports, seed, programId} = decodeData(
297
+ SYSTEM_INSTRUCTION_LAYOUTS.TransferWithSeed,
298
+ instruction.data,
299
+ );
300
+
301
+ return {
302
+ fromPubkey: instruction.keys[0].pubkey,
303
+ basePubkey: instruction.keys[1].pubkey,
304
+ toPubkey: instruction.keys[2].pubkey,
305
+ lamports,
306
+ seed,
307
+ programId: new PublicKey(programId),
308
+ };
309
+ }
310
+
311
+ /**
312
+ * Decode an allocate system instruction and retrieve the instruction params.
313
+ */
314
+ static decodeAllocate(instruction: TransactionInstruction): AllocateParams {
315
+ this.checkProgramId(instruction.programId);
316
+ this.checkKeyLength(instruction.keys, 1);
317
+
318
+ const {space} = decodeData(
319
+ SYSTEM_INSTRUCTION_LAYOUTS.Allocate,
320
+ instruction.data,
321
+ );
322
+
323
+ return {
324
+ accountPubkey: instruction.keys[0].pubkey,
325
+ space,
326
+ };
327
+ }
328
+
329
+ /**
330
+ * Decode an allocate with seed system instruction and retrieve the instruction params.
331
+ */
332
+ static decodeAllocateWithSeed(
333
+ instruction: TransactionInstruction,
334
+ ): AllocateWithSeedParams {
335
+ this.checkProgramId(instruction.programId);
336
+ this.checkKeyLength(instruction.keys, 1);
337
+
338
+ const {base, seed, space, programId} = decodeData(
339
+ SYSTEM_INSTRUCTION_LAYOUTS.AllocateWithSeed,
340
+ instruction.data,
341
+ );
342
+
343
+ return {
344
+ accountPubkey: instruction.keys[0].pubkey,
345
+ basePubkey: new PublicKey(base),
346
+ seed,
347
+ space,
348
+ programId: new PublicKey(programId),
349
+ };
350
+ }
351
+
352
+ /**
353
+ * Decode an assign system instruction and retrieve the instruction params.
354
+ */
355
+ static decodeAssign(instruction: TransactionInstruction): AssignParams {
356
+ this.checkProgramId(instruction.programId);
357
+ this.checkKeyLength(instruction.keys, 1);
358
+
359
+ const {programId} = decodeData(
360
+ SYSTEM_INSTRUCTION_LAYOUTS.Assign,
361
+ instruction.data,
362
+ );
363
+
364
+ return {
365
+ accountPubkey: instruction.keys[0].pubkey,
366
+ programId: new PublicKey(programId),
367
+ };
368
+ }
369
+
370
+ /**
371
+ * Decode an assign with seed system instruction and retrieve the instruction params.
372
+ */
373
+ static decodeAssignWithSeed(
374
+ instruction: TransactionInstruction,
375
+ ): AssignWithSeedParams {
376
+ this.checkProgramId(instruction.programId);
377
+ this.checkKeyLength(instruction.keys, 1);
378
+
379
+ const {base, seed, programId} = decodeData(
380
+ SYSTEM_INSTRUCTION_LAYOUTS.AssignWithSeed,
381
+ instruction.data,
382
+ );
383
+
384
+ return {
385
+ accountPubkey: instruction.keys[0].pubkey,
386
+ basePubkey: new PublicKey(base),
387
+ seed,
388
+ programId: new PublicKey(programId),
389
+ };
390
+ }
391
+
392
+ /**
393
+ * Decode a create account with seed system instruction and retrieve the instruction params.
394
+ */
395
+ static decodeCreateWithSeed(
396
+ instruction: TransactionInstruction,
397
+ ): CreateAccountWithSeedParams {
398
+ this.checkProgramId(instruction.programId);
399
+ this.checkKeyLength(instruction.keys, 2);
400
+
401
+ const {base, seed, lamports, space, programId} = decodeData(
402
+ SYSTEM_INSTRUCTION_LAYOUTS.CreateWithSeed,
403
+ instruction.data,
404
+ );
405
+
406
+ return {
407
+ fromPubkey: instruction.keys[0].pubkey,
408
+ newAccountPubkey: instruction.keys[1].pubkey,
409
+ basePubkey: new PublicKey(base),
410
+ seed,
411
+ lamports,
412
+ space,
413
+ programId: new PublicKey(programId),
414
+ };
415
+ }
416
+
417
+ /**
418
+ * Decode a nonce initialize system instruction and retrieve the instruction params.
419
+ */
420
+ static decodeNonceInitialize(
421
+ instruction: TransactionInstruction,
422
+ ): InitializeNonceParams {
423
+ this.checkProgramId(instruction.programId);
424
+ this.checkKeyLength(instruction.keys, 3);
425
+
426
+ const {authorized} = decodeData(
427
+ SYSTEM_INSTRUCTION_LAYOUTS.InitializeNonceAccount,
428
+ instruction.data,
429
+ );
430
+
431
+ return {
432
+ noncePubkey: instruction.keys[0].pubkey,
433
+ authorizedPubkey: new PublicKey(authorized),
434
+ };
435
+ }
436
+
437
+ /**
438
+ * Decode a nonce advance system instruction and retrieve the instruction params.
439
+ */
440
+ static decodeNonceAdvance(
441
+ instruction: TransactionInstruction,
442
+ ): AdvanceNonceParams {
443
+ this.checkProgramId(instruction.programId);
444
+ this.checkKeyLength(instruction.keys, 3);
445
+
446
+ decodeData(
447
+ SYSTEM_INSTRUCTION_LAYOUTS.AdvanceNonceAccount,
448
+ instruction.data,
449
+ );
450
+
451
+ return {
452
+ noncePubkey: instruction.keys[0].pubkey,
453
+ authorizedPubkey: instruction.keys[2].pubkey,
454
+ };
455
+ }
456
+
457
+ /**
458
+ * Decode a nonce withdraw system instruction and retrieve the instruction params.
459
+ */
460
+ static decodeNonceWithdraw(
461
+ instruction: TransactionInstruction,
462
+ ): WithdrawNonceParams {
463
+ this.checkProgramId(instruction.programId);
464
+ this.checkKeyLength(instruction.keys, 5);
465
+
466
+ const {lamports} = decodeData(
467
+ SYSTEM_INSTRUCTION_LAYOUTS.WithdrawNonceAccount,
468
+ instruction.data,
469
+ );
470
+
471
+ return {
472
+ noncePubkey: instruction.keys[0].pubkey,
473
+ toPubkey: instruction.keys[1].pubkey,
474
+ authorizedPubkey: instruction.keys[4].pubkey,
475
+ lamports,
476
+ };
477
+ }
478
+
479
+ /**
480
+ * Decode a nonce authorize system instruction and retrieve the instruction params.
481
+ */
482
+ static decodeNonceAuthorize(
483
+ instruction: TransactionInstruction,
484
+ ): AuthorizeNonceParams {
485
+ this.checkProgramId(instruction.programId);
486
+ this.checkKeyLength(instruction.keys, 2);
487
+
488
+ const {authorized} = decodeData(
489
+ SYSTEM_INSTRUCTION_LAYOUTS.AuthorizeNonceAccount,
490
+ instruction.data,
491
+ );
492
+
493
+ return {
494
+ noncePubkey: instruction.keys[0].pubkey,
495
+ authorizedPubkey: instruction.keys[1].pubkey,
496
+ newAuthorizedPubkey: new PublicKey(authorized),
497
+ };
498
+ }
499
+
500
+ /**
501
+ * @internal
502
+ */
503
+ static checkProgramId(programId: PublicKey) {
504
+ if (!programId.equals(SystemProgram.programId)) {
505
+ throw new Error('invalid instruction; programId is not SystemProgram');
506
+ }
507
+ }
508
+
509
+ /**
510
+ * @internal
511
+ */
512
+ static checkKeyLength(keys: Array<any>, expectedLength: number) {
513
+ if (keys.length < expectedLength) {
514
+ throw new Error(
515
+ `invalid instruction; found ${keys.length} keys, expected at least ${expectedLength}`,
516
+ );
517
+ }
518
+ }
519
+ }
520
+
521
+ /**
522
+ * An enumeration of valid SystemInstructionType's
523
+ */
524
+ export type SystemInstructionType =
525
+ // FIXME
526
+ // It would be preferable for this type to be `keyof SystemInstructionInputData`
527
+ // but Typedoc does not transpile `keyof` expressions.
528
+ // See https://github.com/TypeStrong/typedoc/issues/1894
529
+ | 'AdvanceNonceAccount'
530
+ | 'Allocate'
531
+ | 'AllocateWithSeed'
532
+ | 'Assign'
533
+ | 'AssignWithSeed'
534
+ | 'AuthorizeNonceAccount'
535
+ | 'Create'
536
+ | 'CreateWithSeed'
537
+ | 'InitializeNonceAccount'
538
+ | 'Transfer'
539
+ | 'TransferWithSeed'
540
+ | 'WithdrawNonceAccount';
541
+
542
+ type SystemInstructionInputData = {
543
+ AdvanceNonceAccount: IInstructionInputData;
544
+ Allocate: IInstructionInputData & {
545
+ space: number;
546
+ };
547
+ AllocateWithSeed: IInstructionInputData & {
548
+ base: Uint8Array;
549
+ programId: Uint8Array;
550
+ seed: string;
551
+ space: number;
552
+ };
553
+ Assign: IInstructionInputData & {
554
+ programId: Uint8Array;
555
+ };
556
+ AssignWithSeed: IInstructionInputData & {
557
+ base: Uint8Array;
558
+ seed: string;
559
+ programId: Uint8Array;
560
+ };
561
+ AuthorizeNonceAccount: IInstructionInputData & {
562
+ authorized: Uint8Array;
563
+ };
564
+ Create: IInstructionInputData & {
565
+ lamports: number;
566
+ programId: Uint8Array;
567
+ space: number;
568
+ };
569
+ CreateWithSeed: IInstructionInputData & {
570
+ base: Uint8Array;
571
+ lamports: number;
572
+ programId: Uint8Array;
573
+ seed: string;
574
+ space: number;
575
+ };
576
+ InitializeNonceAccount: IInstructionInputData & {
577
+ authorized: Uint8Array;
578
+ };
579
+ Transfer: IInstructionInputData & {
580
+ lamports: number;
581
+ };
582
+ TransferWithSeed: IInstructionInputData & {
583
+ lamports: number;
584
+ programId: Uint8Array;
585
+ seed: string;
586
+ };
587
+ WithdrawNonceAccount: IInstructionInputData & {
588
+ lamports: number;
589
+ };
590
+ };
591
+
592
+ /**
593
+ * An enumeration of valid system InstructionType's
594
+ * @internal
595
+ */
596
+ export const SYSTEM_INSTRUCTION_LAYOUTS = Object.freeze<{
597
+ [Instruction in SystemInstructionType]: InstructionType<
598
+ SystemInstructionInputData[Instruction]
599
+ >;
600
+ }>({
601
+ Create: {
602
+ index: 0,
603
+ layout: BufferLayout.struct<SystemInstructionInputData['Create']>([
604
+ BufferLayout.u32('instruction'),
605
+ BufferLayout.ns64('lamports'),
606
+ BufferLayout.ns64('space'),
607
+ Layout.publicKey('programId'),
608
+ ]),
609
+ },
610
+ Assign: {
611
+ index: 1,
612
+ layout: BufferLayout.struct<SystemInstructionInputData['Assign']>([
613
+ BufferLayout.u32('instruction'),
614
+ Layout.publicKey('programId'),
615
+ ]),
616
+ },
617
+ Transfer: {
618
+ index: 2,
619
+ layout: BufferLayout.struct<SystemInstructionInputData['Transfer']>([
620
+ BufferLayout.u32('instruction'),
621
+ BufferLayout.ns64('lamports'),
622
+ ]),
623
+ },
624
+ CreateWithSeed: {
625
+ index: 3,
626
+ layout: BufferLayout.struct<SystemInstructionInputData['CreateWithSeed']>([
627
+ BufferLayout.u32('instruction'),
628
+ Layout.publicKey('base'),
629
+ Layout.rustString('seed'),
630
+ BufferLayout.ns64('lamports'),
631
+ BufferLayout.ns64('space'),
632
+ Layout.publicKey('programId'),
633
+ ]),
634
+ },
635
+ AdvanceNonceAccount: {
636
+ index: 4,
637
+ layout: BufferLayout.struct<
638
+ SystemInstructionInputData['AdvanceNonceAccount']
639
+ >([BufferLayout.u32('instruction')]),
640
+ },
641
+ WithdrawNonceAccount: {
642
+ index: 5,
643
+ layout: BufferLayout.struct<
644
+ SystemInstructionInputData['WithdrawNonceAccount']
645
+ >([BufferLayout.u32('instruction'), BufferLayout.ns64('lamports')]),
646
+ },
647
+ InitializeNonceAccount: {
648
+ index: 6,
649
+ layout: BufferLayout.struct<
650
+ SystemInstructionInputData['InitializeNonceAccount']
651
+ >([BufferLayout.u32('instruction'), Layout.publicKey('authorized')]),
652
+ },
653
+ AuthorizeNonceAccount: {
654
+ index: 7,
655
+ layout: BufferLayout.struct<
656
+ SystemInstructionInputData['AuthorizeNonceAccount']
657
+ >([BufferLayout.u32('instruction'), Layout.publicKey('authorized')]),
658
+ },
659
+ Allocate: {
660
+ index: 8,
661
+ layout: BufferLayout.struct<SystemInstructionInputData['Allocate']>([
662
+ BufferLayout.u32('instruction'),
663
+ BufferLayout.ns64('space'),
664
+ ]),
665
+ },
666
+ AllocateWithSeed: {
667
+ index: 9,
668
+ layout: BufferLayout.struct<SystemInstructionInputData['AllocateWithSeed']>(
669
+ [
670
+ BufferLayout.u32('instruction'),
671
+ Layout.publicKey('base'),
672
+ Layout.rustString('seed'),
673
+ BufferLayout.ns64('space'),
674
+ Layout.publicKey('programId'),
675
+ ],
676
+ ),
677
+ },
678
+ AssignWithSeed: {
679
+ index: 10,
680
+ layout: BufferLayout.struct<SystemInstructionInputData['AssignWithSeed']>([
681
+ BufferLayout.u32('instruction'),
682
+ Layout.publicKey('base'),
683
+ Layout.rustString('seed'),
684
+ Layout.publicKey('programId'),
685
+ ]),
686
+ },
687
+ TransferWithSeed: {
688
+ index: 11,
689
+ layout: BufferLayout.struct<SystemInstructionInputData['TransferWithSeed']>(
690
+ [
691
+ BufferLayout.u32('instruction'),
692
+ BufferLayout.ns64('lamports'),
693
+ Layout.rustString('seed'),
694
+ Layout.publicKey('programId'),
695
+ ],
696
+ ),
697
+ },
698
+ });
699
+
700
+ /**
701
+ * Factory class for transactions to interact with the System program
702
+ */
703
+ export class SystemProgram {
704
+ /**
705
+ * @internal
706
+ */
707
+ constructor() {}
708
+
709
+ /**
710
+ * Public key that identifies the System program
711
+ */
712
+ static programId: PublicKey = new PublicKey(
713
+ '11111111111111111111111111111111',
714
+ );
715
+
716
+ /**
717
+ * Generate a transaction instruction that creates a new account
718
+ */
719
+ static createAccount(params: CreateAccountParams): TransactionInstruction {
720
+ const type = SYSTEM_INSTRUCTION_LAYOUTS.Create;
721
+ const data = encodeData(type, {
722
+ lamports: params.lamports,
723
+ space: params.space,
724
+ programId: toBuffer(params.programId.toBuffer()),
725
+ });
726
+
727
+ return new TransactionInstruction({
728
+ keys: [
729
+ {pubkey: params.fromPubkey, isSigner: true, isWritable: true},
730
+ {pubkey: params.newAccountPubkey, isSigner: true, isWritable: true},
731
+ ],
732
+ programId: this.programId,
733
+ data,
734
+ });
735
+ }
736
+
737
+ /**
738
+ * Generate a transaction instruction that transfers lamports from one account to another
739
+ */
740
+ static transfer(
741
+ params: TransferParams | TransferWithSeedParams,
742
+ ): TransactionInstruction {
743
+ let data;
744
+ let keys;
745
+ if ('basePubkey' in params) {
746
+ const type = SYSTEM_INSTRUCTION_LAYOUTS.TransferWithSeed;
747
+ data = encodeData(type, {
748
+ lamports: params.lamports,
749
+ seed: params.seed,
750
+ programId: toBuffer(params.programId.toBuffer()),
751
+ });
752
+ keys = [
753
+ {pubkey: params.fromPubkey, isSigner: false, isWritable: true},
754
+ {pubkey: params.basePubkey, isSigner: true, isWritable: false},
755
+ {pubkey: params.toPubkey, isSigner: false, isWritable: true},
756
+ ];
757
+ } else {
758
+ const type = SYSTEM_INSTRUCTION_LAYOUTS.Transfer;
759
+ data = encodeData(type, {lamports: params.lamports});
760
+ keys = [
761
+ {pubkey: params.fromPubkey, isSigner: true, isWritable: true},
762
+ {pubkey: params.toPubkey, isSigner: false, isWritable: true},
763
+ ];
764
+ }
765
+
766
+ return new TransactionInstruction({
767
+ keys,
768
+ programId: this.programId,
769
+ data,
770
+ });
771
+ }
772
+
773
+ /**
774
+ * Generate a transaction instruction that assigns an account to a program
775
+ */
776
+ static assign(
777
+ params: AssignParams | AssignWithSeedParams,
778
+ ): TransactionInstruction {
779
+ let data;
780
+ let keys;
781
+ if ('basePubkey' in params) {
782
+ const type = SYSTEM_INSTRUCTION_LAYOUTS.AssignWithSeed;
783
+ data = encodeData(type, {
784
+ base: toBuffer(params.basePubkey.toBuffer()),
785
+ seed: params.seed,
786
+ programId: toBuffer(params.programId.toBuffer()),
787
+ });
788
+ keys = [
789
+ {pubkey: params.accountPubkey, isSigner: false, isWritable: true},
790
+ {pubkey: params.basePubkey, isSigner: true, isWritable: false},
791
+ ];
792
+ } else {
793
+ const type = SYSTEM_INSTRUCTION_LAYOUTS.Assign;
794
+ data = encodeData(type, {
795
+ programId: toBuffer(params.programId.toBuffer()),
796
+ });
797
+ keys = [{pubkey: params.accountPubkey, isSigner: true, isWritable: true}];
798
+ }
799
+
800
+ return new TransactionInstruction({
801
+ keys,
802
+ programId: this.programId,
803
+ data,
804
+ });
805
+ }
806
+
807
+ /**
808
+ * Generate a transaction instruction that creates a new account at
809
+ * an address generated with `from`, a seed, and programId
810
+ */
811
+ static createAccountWithSeed(
812
+ params: CreateAccountWithSeedParams,
813
+ ): TransactionInstruction {
814
+ const type = SYSTEM_INSTRUCTION_LAYOUTS.CreateWithSeed;
815
+ const data = encodeData(type, {
816
+ base: toBuffer(params.basePubkey.toBuffer()),
817
+ seed: params.seed,
818
+ lamports: params.lamports,
819
+ space: params.space,
820
+ programId: toBuffer(params.programId.toBuffer()),
821
+ });
822
+ let keys = [
823
+ {pubkey: params.fromPubkey, isSigner: true, isWritable: true},
824
+ {pubkey: params.newAccountPubkey, isSigner: false, isWritable: true},
825
+ ];
826
+ if (params.basePubkey != params.fromPubkey) {
827
+ keys.push({pubkey: params.basePubkey, isSigner: true, isWritable: false});
828
+ }
829
+
830
+ return new TransactionInstruction({
831
+ keys,
832
+ programId: this.programId,
833
+ data,
834
+ });
835
+ }
836
+
837
+ /**
838
+ * Generate a transaction that creates a new Nonce account
839
+ */
840
+ static createNonceAccount(
841
+ params: CreateNonceAccountParams | CreateNonceAccountWithSeedParams,
842
+ ): Transaction {
843
+ const transaction = new Transaction();
844
+ if ('basePubkey' in params && 'seed' in params) {
845
+ transaction.add(
846
+ SystemProgram.createAccountWithSeed({
847
+ fromPubkey: params.fromPubkey,
848
+ newAccountPubkey: params.noncePubkey,
849
+ basePubkey: params.basePubkey,
850
+ seed: params.seed,
851
+ lamports: params.lamports,
852
+ space: NONCE_ACCOUNT_LENGTH,
853
+ programId: this.programId,
854
+ }),
855
+ );
856
+ } else {
857
+ transaction.add(
858
+ SystemProgram.createAccount({
859
+ fromPubkey: params.fromPubkey,
860
+ newAccountPubkey: params.noncePubkey,
861
+ lamports: params.lamports,
862
+ space: NONCE_ACCOUNT_LENGTH,
863
+ programId: this.programId,
864
+ }),
865
+ );
866
+ }
867
+
868
+ const initParams = {
869
+ noncePubkey: params.noncePubkey,
870
+ authorizedPubkey: params.authorizedPubkey,
871
+ };
872
+
873
+ transaction.add(this.nonceInitialize(initParams));
874
+ return transaction;
875
+ }
876
+
877
+ /**
878
+ * Generate an instruction to initialize a Nonce account
879
+ */
880
+ static nonceInitialize(
881
+ params: InitializeNonceParams,
882
+ ): TransactionInstruction {
883
+ const type = SYSTEM_INSTRUCTION_LAYOUTS.InitializeNonceAccount;
884
+ const data = encodeData(type, {
885
+ authorized: toBuffer(params.authorizedPubkey.toBuffer()),
886
+ });
887
+ const instructionData = {
888
+ keys: [
889
+ {pubkey: params.noncePubkey, isSigner: false, isWritable: true},
890
+ {
891
+ pubkey: SYSVAR_RECENT_BLOCKHASHES_PUBKEY,
892
+ isSigner: false,
893
+ isWritable: false,
894
+ },
895
+ {pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false},
896
+ ],
897
+ programId: this.programId,
898
+ data,
899
+ };
900
+ return new TransactionInstruction(instructionData);
901
+ }
902
+
903
+ /**
904
+ * Generate an instruction to advance the nonce in a Nonce account
905
+ */
906
+ static nonceAdvance(params: AdvanceNonceParams): TransactionInstruction {
907
+ const type = SYSTEM_INSTRUCTION_LAYOUTS.AdvanceNonceAccount;
908
+ const data = encodeData(type);
909
+ const instructionData = {
910
+ keys: [
911
+ {pubkey: params.noncePubkey, isSigner: false, isWritable: true},
912
+ {
913
+ pubkey: SYSVAR_RECENT_BLOCKHASHES_PUBKEY,
914
+ isSigner: false,
915
+ isWritable: false,
916
+ },
917
+ {pubkey: params.authorizedPubkey, isSigner: true, isWritable: false},
918
+ ],
919
+ programId: this.programId,
920
+ data,
921
+ };
922
+ return new TransactionInstruction(instructionData);
923
+ }
924
+
925
+ /**
926
+ * Generate a transaction instruction that withdraws lamports from a Nonce account
927
+ */
928
+ static nonceWithdraw(params: WithdrawNonceParams): TransactionInstruction {
929
+ const type = SYSTEM_INSTRUCTION_LAYOUTS.WithdrawNonceAccount;
930
+ const data = encodeData(type, {lamports: params.lamports});
931
+
932
+ return new TransactionInstruction({
933
+ keys: [
934
+ {pubkey: params.noncePubkey, isSigner: false, isWritable: true},
935
+ {pubkey: params.toPubkey, isSigner: false, isWritable: true},
936
+ {
937
+ pubkey: SYSVAR_RECENT_BLOCKHASHES_PUBKEY,
938
+ isSigner: false,
939
+ isWritable: false,
940
+ },
941
+ {
942
+ pubkey: SYSVAR_RENT_PUBKEY,
943
+ isSigner: false,
944
+ isWritable: false,
945
+ },
946
+ {pubkey: params.authorizedPubkey, isSigner: true, isWritable: false},
947
+ ],
948
+ programId: this.programId,
949
+ data,
950
+ });
951
+ }
952
+
953
+ /**
954
+ * Generate a transaction instruction that authorizes a new PublicKey as the authority
955
+ * on a Nonce account.
956
+ */
957
+ static nonceAuthorize(params: AuthorizeNonceParams): TransactionInstruction {
958
+ const type = SYSTEM_INSTRUCTION_LAYOUTS.AuthorizeNonceAccount;
959
+ const data = encodeData(type, {
960
+ authorized: toBuffer(params.newAuthorizedPubkey.toBuffer()),
961
+ });
962
+
963
+ return new TransactionInstruction({
964
+ keys: [
965
+ {pubkey: params.noncePubkey, isSigner: false, isWritable: true},
966
+ {pubkey: params.authorizedPubkey, isSigner: true, isWritable: false},
967
+ ],
968
+ programId: this.programId,
969
+ data,
970
+ });
971
+ }
972
+
973
+ /**
974
+ * Generate a transaction instruction that allocates space in an account without funding
975
+ */
976
+ static allocate(
977
+ params: AllocateParams | AllocateWithSeedParams,
978
+ ): TransactionInstruction {
979
+ let data;
980
+ let keys;
981
+ if ('basePubkey' in params) {
982
+ const type = SYSTEM_INSTRUCTION_LAYOUTS.AllocateWithSeed;
983
+ data = encodeData(type, {
984
+ base: toBuffer(params.basePubkey.toBuffer()),
985
+ seed: params.seed,
986
+ space: params.space,
987
+ programId: toBuffer(params.programId.toBuffer()),
988
+ });
989
+ keys = [
990
+ {pubkey: params.accountPubkey, isSigner: false, isWritable: true},
991
+ {pubkey: params.basePubkey, isSigner: true, isWritable: false},
992
+ ];
993
+ } else {
994
+ const type = SYSTEM_INSTRUCTION_LAYOUTS.Allocate;
995
+ data = encodeData(type, {
996
+ space: params.space,
997
+ });
998
+ keys = [{pubkey: params.accountPubkey, isSigner: true, isWritable: true}];
999
+ }
1000
+
1001
+ return new TransactionInstruction({
1002
+ keys,
1003
+ programId: this.programId,
1004
+ data,
1005
+ });
1006
+ }
1007
+ }