@exodus/solana-lib 1.7.5 → 1.8.0

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.
@@ -197,11 +197,11 @@ export const SYSTEM_INSTRUCTION_LAYOUTS = Object.freeze({
197
197
  /**
198
198
  * System Instruction class
199
199
  */
200
- export class SystemInstruction {
200
+ export const SystemInstruction = {
201
201
  /**
202
202
  * Decode a system instruction and retrieve the instruction type.
203
203
  */
204
- static decodeInstructionType(instruction) {
204
+ decodeInstructionType(instruction) {
205
205
  this.checkProgramId(instruction.programId)
206
206
 
207
207
  const instructionTypeLayout = BufferLayout.u32('instruction')
@@ -219,12 +219,12 @@ export class SystemInstruction {
219
219
  }
220
220
 
221
221
  return type
222
- }
222
+ },
223
223
 
224
224
  /**
225
225
  * Decode a create account system instruction and retrieve the instruction params.
226
226
  */
227
- static decodeCreateAccount(instruction) {
227
+ decodeCreateAccount(instruction) {
228
228
  this.checkProgramId(instruction.programId)
229
229
  this.checkKeyLength(instruction.keys, 2)
230
230
 
@@ -240,12 +240,12 @@ export class SystemInstruction {
240
240
  space,
241
241
  programId: new PublicKey(programId),
242
242
  }
243
- }
243
+ },
244
244
 
245
245
  /**
246
246
  * Decode a transfer system instruction and retrieve the instruction params.
247
247
  */
248
- static decodeTransfer(instruction) {
248
+ decodeTransfer(instruction) {
249
249
  this.checkProgramId(instruction.programId)
250
250
  this.checkKeyLength(instruction.keys, 2)
251
251
 
@@ -256,12 +256,12 @@ export class SystemInstruction {
256
256
  toPubkey: instruction.keys[1].pubkey,
257
257
  lamports,
258
258
  }
259
- }
259
+ },
260
260
 
261
261
  /**
262
262
  * Decode an allocate system instruction and retrieve the instruction params.
263
263
  */
264
- static decodeAllocate(instruction) {
264
+ decodeAllocate(instruction) {
265
265
  this.checkProgramId(instruction.programId)
266
266
  this.checkKeyLength(instruction.keys, 1)
267
267
 
@@ -271,12 +271,12 @@ export class SystemInstruction {
271
271
  accountPubkey: instruction.keys[0].pubkey,
272
272
  space,
273
273
  }
274
- }
274
+ },
275
275
 
276
276
  /**
277
277
  * Decode an allocate with seed system instruction and retrieve the instruction params.
278
278
  */
279
- static decodeAllocateWithSeed(instruction) {
279
+ decodeAllocateWithSeed(instruction) {
280
280
  this.checkProgramId(instruction.programId)
281
281
  this.checkKeyLength(instruction.keys, 1)
282
282
 
@@ -292,12 +292,12 @@ export class SystemInstruction {
292
292
  space,
293
293
  programId: new PublicKey(programId),
294
294
  }
295
- }
295
+ },
296
296
 
297
297
  /**
298
298
  * Decode an assign system instruction and retrieve the instruction params.
299
299
  */
300
- static decodeAssign(instruction) {
300
+ decodeAssign(instruction) {
301
301
  this.checkProgramId(instruction.programId)
302
302
  this.checkKeyLength(instruction.keys, 1)
303
303
 
@@ -307,12 +307,12 @@ export class SystemInstruction {
307
307
  accountPubkey: instruction.keys[0].pubkey,
308
308
  programId: new PublicKey(programId),
309
309
  }
310
- }
310
+ },
311
311
 
312
312
  /**
313
313
  * Decode an assign with seed system instruction and retrieve the instruction params.
314
314
  */
315
- static decodeAssignWithSeed(instruction) {
315
+ decodeAssignWithSeed(instruction) {
316
316
  this.checkProgramId(instruction.programId)
317
317
  this.checkKeyLength(instruction.keys, 1)
318
318
 
@@ -327,12 +327,12 @@ export class SystemInstruction {
327
327
  seed,
328
328
  programId: new PublicKey(programId),
329
329
  }
330
- }
330
+ },
331
331
 
332
332
  /**
333
333
  * Decode a create account with seed system instruction and retrieve the instruction params.
334
334
  */
335
- static decodeCreateWithSeed(instruction) {
335
+ decodeCreateWithSeed(instruction) {
336
336
  this.checkProgramId(instruction.programId)
337
337
  this.checkKeyLength(instruction.keys, 2)
338
338
 
@@ -350,12 +350,12 @@ export class SystemInstruction {
350
350
  space,
351
351
  programId: new PublicKey(programId),
352
352
  }
353
- }
353
+ },
354
354
 
355
355
  /**
356
356
  * Decode a nonce initialize system instruction and retrieve the instruction params.
357
357
  */
358
- static decodeNonceInitialize(instruction) {
358
+ decodeNonceInitialize(instruction) {
359
359
  this.checkProgramId(instruction.programId)
360
360
  this.checkKeyLength(instruction.keys, 3)
361
361
 
@@ -368,12 +368,12 @@ export class SystemInstruction {
368
368
  noncePubkey: instruction.keys[0].pubkey,
369
369
  authorizedPubkey: new PublicKey(authorized),
370
370
  }
371
- }
371
+ },
372
372
 
373
373
  /**
374
374
  * Decode a nonce advance system instruction and retrieve the instruction params.
375
375
  */
376
- static decodeNonceAdvance(instruction) {
376
+ decodeNonceAdvance(instruction) {
377
377
  this.checkProgramId(instruction.programId)
378
378
  this.checkKeyLength(instruction.keys, 3)
379
379
 
@@ -383,12 +383,12 @@ export class SystemInstruction {
383
383
  noncePubkey: instruction.keys[0].pubkey,
384
384
  authorizedPubkey: instruction.keys[2].pubkey,
385
385
  }
386
- }
386
+ },
387
387
 
388
388
  /**
389
389
  * Decode a nonce withdraw system instruction and retrieve the instruction params.
390
390
  */
391
- static decodeNonceWithdraw(instruction) {
391
+ decodeNonceWithdraw(instruction) {
392
392
  this.checkProgramId(instruction.programId)
393
393
  this.checkKeyLength(instruction.keys, 5)
394
394
 
@@ -403,12 +403,12 @@ export class SystemInstruction {
403
403
  authorizedPubkey: instruction.keys[4].pubkey,
404
404
  lamports,
405
405
  }
406
- }
406
+ },
407
407
 
408
408
  /**
409
409
  * Decode a nonce authorize system instruction and retrieve the instruction params.
410
410
  */
411
- static decodeNonceAuthorize(instruction) {
411
+ decodeNonceAuthorize(instruction) {
412
412
  this.checkProgramId(instruction.programId)
413
413
  this.checkKeyLength(instruction.keys, 2)
414
414
 
@@ -422,44 +422,44 @@ export class SystemInstruction {
422
422
  authorizedPubkey: instruction.keys[1].pubkey,
423
423
  newAuthorizedPubkey: new PublicKey(authorized),
424
424
  }
425
- }
425
+ },
426
426
 
427
427
  /**
428
428
  * @private
429
429
  */
430
- static checkProgramId(programId) {
430
+ checkProgramId(programId) {
431
431
  if (!programId.equals(SystemProgram.programId)) {
432
432
  throw new Error('invalid instruction; programId is not SystemProgram')
433
433
  }
434
- }
434
+ },
435
435
 
436
436
  /**
437
437
  * @private
438
438
  */
439
- static checkKeyLength(keys, expectedLength) {
439
+ checkKeyLength(keys, expectedLength) {
440
440
  if (keys.length < expectedLength) {
441
441
  throw new Error(
442
442
  `invalid instruction; found ${keys.length} keys, expected at least ${expectedLength}`
443
443
  )
444
444
  }
445
- }
445
+ },
446
446
  }
447
447
 
448
448
  /**
449
449
  * Factory class for transactions to interact with the System program
450
450
  */
451
- export class SystemProgram {
451
+ export const SystemProgram = {
452
452
  /**
453
453
  * Public key that identifies the System program
454
454
  */
455
- static get programId() {
455
+ get programId() {
456
456
  return new PublicKey('11111111111111111111111111111111')
457
- }
457
+ },
458
458
 
459
459
  /**
460
460
  * Generate a transaction instruction that creates a new account
461
461
  */
462
- static createAccount(params) {
462
+ createAccount(params) {
463
463
  const type = SYSTEM_INSTRUCTION_LAYOUTS.Create
464
464
  const data = encodeData(type, {
465
465
  lamports: params.lamports,
@@ -475,12 +475,12 @@ export class SystemProgram {
475
475
  programId: this.programId,
476
476
  data,
477
477
  })
478
- }
478
+ },
479
479
 
480
480
  /**
481
481
  * Generate a transaction instruction that transfers lamports from one account to another
482
482
  */
483
- static transfer(params) {
483
+ transfer(params) {
484
484
  const type = SYSTEM_INSTRUCTION_LAYOUTS.Transfer
485
485
  const data = encodeData(type, { lamports: params.lamports })
486
486
 
@@ -492,12 +492,12 @@ export class SystemProgram {
492
492
  programId: this.programId,
493
493
  data,
494
494
  })
495
- }
495
+ },
496
496
 
497
497
  /**
498
498
  * Generate a transaction instruction that assigns an account to a program
499
499
  */
500
- static assign(params) {
500
+ assign(params) {
501
501
  let data
502
502
  if (params.basePubkey) {
503
503
  const type = SYSTEM_INSTRUCTION_LAYOUTS.AssignWithSeed
@@ -516,13 +516,13 @@ export class SystemProgram {
516
516
  programId: this.programId,
517
517
  data,
518
518
  })
519
- }
519
+ },
520
520
 
521
521
  /**
522
522
  * Generate a transaction instruction that creates a new account at
523
523
  * an address generated with `from`, a seed, and programId
524
524
  */
525
- static createAccountWithSeed(params) {
525
+ createAccountWithSeed(params) {
526
526
  const type = SYSTEM_INSTRUCTION_LAYOUTS.CreateWithSeed
527
527
  const data = encodeData(type, {
528
528
  base: params.basePubkey.toBuffer(),
@@ -540,12 +540,12 @@ export class SystemProgram {
540
540
  programId: this.programId,
541
541
  data,
542
542
  })
543
- }
543
+ },
544
544
 
545
545
  /**
546
546
  * Generate a transaction that creates a new Nonce account
547
547
  */
548
- static createNonceAccount(params) {
548
+ createNonceAccount(params) {
549
549
  const transaction = new Transaction()
550
550
  if (params.basePubkey && params.seed) {
551
551
  transaction.add(
@@ -578,12 +578,12 @@ export class SystemProgram {
578
578
 
579
579
  transaction.add(this.nonceInitialize(initParams))
580
580
  return transaction
581
- }
581
+ },
582
582
 
583
583
  /**
584
584
  * Generate an instruction to initialize a Nonce account
585
585
  */
586
- static nonceInitialize(params) {
586
+ nonceInitialize(params) {
587
587
  const type = SYSTEM_INSTRUCTION_LAYOUTS.InitializeNonceAccount
588
588
  const data = encodeData(type, {
589
589
  authorized: params.authorizedPubkey.toBuffer(),
@@ -602,12 +602,12 @@ export class SystemProgram {
602
602
  data,
603
603
  }
604
604
  return new TransactionInstruction(instructionData)
605
- }
605
+ },
606
606
 
607
607
  /**
608
608
  * Generate an instruction to advance the nonce in a Nonce account
609
609
  */
610
- static nonceAdvance(params) {
610
+ nonceAdvance(params) {
611
611
  const type = SYSTEM_INSTRUCTION_LAYOUTS.AdvanceNonceAccount
612
612
  const data = encodeData(type)
613
613
  const instructionData = {
@@ -624,12 +624,12 @@ export class SystemProgram {
624
624
  data,
625
625
  }
626
626
  return new TransactionInstruction(instructionData)
627
- }
627
+ },
628
628
 
629
629
  /**
630
630
  * Generate a transaction instruction that withdraws lamports from a Nonce account
631
631
  */
632
- static nonceWithdraw(params) {
632
+ nonceWithdraw(params) {
633
633
  const type = SYSTEM_INSTRUCTION_LAYOUTS.WithdrawNonceAccount
634
634
  const data = encodeData(type, { lamports: params.lamports })
635
635
 
@@ -652,13 +652,13 @@ export class SystemProgram {
652
652
  programId: this.programId,
653
653
  data,
654
654
  })
655
- }
655
+ },
656
656
 
657
657
  /**
658
658
  * Generate a transaction instruction that authorizes a new PublicKey as the authority
659
659
  * on a Nonce account.
660
660
  */
661
- static nonceAuthorize(params) {
661
+ nonceAuthorize(params) {
662
662
  const type = SYSTEM_INSTRUCTION_LAYOUTS.AuthorizeNonceAccount
663
663
  const data = encodeData(type, {
664
664
  authorized: params.newAuthorizedPubkey.toBuffer(),
@@ -672,12 +672,12 @@ export class SystemProgram {
672
672
  programId: this.programId,
673
673
  data,
674
674
  })
675
- }
675
+ },
676
676
 
677
677
  /**
678
678
  * Generate a transaction instruction that allocates space in an account without funding
679
679
  */
680
- static allocate(params) {
680
+ allocate(params) {
681
681
  let data
682
682
  if (params.basePubkey) {
683
683
  const type = SYSTEM_INSTRUCTION_LAYOUTS.AllocateWithSeed
@@ -699,5 +699,5 @@ export class SystemProgram {
699
699
  programId: this.programId,
700
700
  data,
701
701
  })
702
- }
702
+ },
703
703
  }
@@ -5,7 +5,8 @@ import nacl from 'tweetnacl'
5
5
  import bs58 from 'bs58'
6
6
 
7
7
  import { Message } from './message'
8
- import { PublicKey } from './index'
8
+ import { PublicKey } from './publickey'
9
+ import { PACKET_DATA_SIZE } from './constants'
9
10
  import * as shortvec from './utils/shortvec-encoding'
10
11
 
11
12
  /**
@@ -18,16 +19,6 @@ import * as shortvec from './utils/shortvec-encoding'
18
19
  * Signatures are 64 bytes in length
19
20
  */
20
21
  const DEFAULT_SIGNATURE = Buffer.alloc(64).fill(0)
21
-
22
- /**
23
- * Maximum over-the-wire size of a Transaction
24
- *
25
- * 1280 is IPv6 minimum MTU
26
- * 40 bytes is the size of the IPv6 header
27
- * 8 bytes is the size of the fragment header
28
- */
29
- export const PACKET_DATA_SIZE = 1280 - 40 - 8
30
-
31
22
  const SIGNATURE_LENGTH = 64
32
23
 
33
24
  /**
@@ -119,6 +110,7 @@ export class Transaction {
119
110
  if (this.signatures.length > 0) {
120
111
  return this.signatures[0].signature
121
112
  }
113
+
122
114
  return null
123
115
  }
124
116
 
@@ -134,6 +126,7 @@ export class Transaction {
134
126
  if (this.signatures.length > 0) {
135
127
  return this.signatures[0].publicKey
136
128
  }
129
+
137
130
  return null
138
131
  }
139
132
 
@@ -159,6 +152,7 @@ export class Transaction {
159
152
  constructor(opts) {
160
153
  opts && Object.assign(this, opts)
161
154
  }
155
+
162
156
  /**
163
157
  * Add one or more instructions to this Transaction
164
158
  */
@@ -169,7 +163,7 @@ export class Transaction {
169
163
 
170
164
  items.forEach((item) => {
171
165
  if ('instructions' in item) {
172
- this.instructions = this.instructions.concat(item.instructions)
166
+ this.instructions = [...this.instructions, ...item.instructions]
173
167
  } else if ('data' in item && 'programId' in item && 'keys' in item) {
174
168
  this.instructions.push(item)
175
169
  } else {
@@ -178,6 +172,7 @@ export class Transaction {
178
172
  })
179
173
  return this
180
174
  }
175
+
181
176
  /**
182
177
  * Compile transaction data
183
178
  */
@@ -187,12 +182,13 @@ export class Transaction {
187
182
  this.recentBlockhash = nonceInfo.nonce
188
183
  this.instructions.unshift(nonceInfo.nonceInstruction)
189
184
  }
185
+
190
186
  const { recentBlockhash } = this
191
187
  if (!recentBlockhash) {
192
188
  throw new Error('Transaction recentBlockhash required')
193
189
  }
194
190
 
195
- if (this.instructions.length < 1) {
191
+ if (this.instructions.length === 0) {
196
192
  throw new Error('No instructions provided')
197
193
  }
198
194
 
@@ -228,11 +224,12 @@ export class Transaction {
228
224
  })
229
225
 
230
226
  // Sort. Prioritizing first by signer, then by writable
231
- accountMetas.sort(function(x, y) {
227
+ accountMetas.sort(function (x, y) {
232
228
  if (x.isSigner !== y.isSigner) {
233
229
  // Signers always come before non-signers
234
230
  return x.isSigner ? -1 : 1
235
231
  }
232
+
236
233
  if (x.isWritable !== y.isWritable) {
237
234
  // Writable accounts always come before read-only accounts
238
235
  return x.isWritable ? -1 : 1
@@ -323,7 +320,7 @@ export class Transaction {
323
320
  }
324
321
  })
325
322
 
326
- const accountKeys = signedKeys.concat(unsignedKeys)
323
+ const accountKeys = [...signedKeys, ...unsignedKeys]
327
324
  const instructions = this.instructions.map((instruction) => {
328
325
  const { data, programId } = instruction
329
326
  return {
@@ -349,12 +346,14 @@ export class Transaction {
349
346
  instructions,
350
347
  })
351
348
  }
349
+
352
350
  /**
353
351
  * Get a buffer of the Transaction data that need to be covered by signatures
354
352
  */
355
353
  serializeMessage() {
356
354
  return this.compileMessage().serialize()
357
355
  }
356
+
358
357
  /**
359
358
  * Specify the public keys which will be used to sign the Transaction.
360
359
  * The first signer will be used as the transaction fee payer account.
@@ -372,13 +371,14 @@ export class Transaction {
372
371
  const key = publicKey.toString()
373
372
  if (seen.has(key)) {
374
373
  return false
375
- } else {
376
- seen.add(key)
377
- return true
378
374
  }
375
+
376
+ seen.add(key)
377
+ return true
379
378
  })
380
379
  .map((publicKey) => ({ signature: null, publicKey }))
381
380
  }
381
+
382
382
  /**
383
383
  * Sign the Transaction with the specified accounts. Multiple signatures may
384
384
  * be applied to a Transaction. The first signature is considered "primary"
@@ -402,10 +402,10 @@ export class Transaction {
402
402
  const key = signer.publicKey.toString()
403
403
  if (seen.has(key)) {
404
404
  return false
405
- } else {
406
- seen.add(key)
407
- return true
408
405
  }
406
+
407
+ seen.add(key)
408
+ return true
409
409
  })
410
410
  .map((signer) => ({
411
411
  signature: null,
@@ -414,6 +414,7 @@ export class Transaction {
414
414
 
415
415
  this.partialSign(...signers)
416
416
  }
417
+
417
418
  /**
418
419
  * Partially sign a transaction with the specified accounts. All accounts must
419
420
  * correspond to a public key that was previously provided to `setSigners`.
@@ -426,7 +427,7 @@ export class Transaction {
426
427
  }
427
428
 
428
429
  const message = this.compileMessage()
429
- this.signatures.sort(function(x, y) {
430
+ this.signatures.sort(function (x, y) {
430
431
  const xIndex = message.findSignerIndex(x.publicKey)
431
432
  const yIndex = message.findSignerIndex(y.publicKey)
432
433
  return xIndex < yIndex ? -1 : 1
@@ -438,6 +439,7 @@ export class Transaction {
438
439
  this.addSignature(signer.publicKey, signature)
439
440
  })
440
441
  }
442
+
441
443
  /**
442
444
  * Add an externally created signature to a transaction. The public key
443
445
  * must correspond to a public key that was previously provided to `setSigners`.
@@ -452,12 +454,14 @@ export class Transaction {
452
454
 
453
455
  this.signatures[index].signature = Buffer.from(signature)
454
456
  }
457
+
455
458
  /**
456
459
  * Verify signatures of a complete, signed Transaction
457
460
  */
458
461
  verifySignatures() {
459
462
  return this._verifySignatures(this.serializeMessage(), true)
460
463
  }
464
+
461
465
  /**
462
466
  * @private
463
467
  */
@@ -473,8 +477,10 @@ export class Transaction {
473
477
  }
474
478
  }
475
479
  }
480
+
476
481
  return true
477
482
  }
483
+
478
484
  /**
479
485
  * Serialize the Transaction in the wire format.
480
486
  */
@@ -491,6 +497,7 @@ export class Transaction {
491
497
 
492
498
  return this._serialize(signData)
493
499
  }
500
+
494
501
  /**
495
502
  * @private
496
503
  */
@@ -515,6 +522,7 @@ export class Transaction {
515
522
  )
516
523
  return wireTransaction
517
524
  }
525
+
518
526
  /**
519
527
  * Deprecated method
520
528
  * @private
@@ -523,6 +531,7 @@ export class Transaction {
523
531
  invariant(this.instructions.length === 1)
524
532
  return this.instructions[0].keys.map((keyObj) => keyObj.pubkey)
525
533
  }
534
+
526
535
  /**
527
536
  * Deprecated method
528
537
  * @private
@@ -531,6 +540,7 @@ export class Transaction {
531
540
  invariant(this.instructions.length === 1)
532
541
  return this.instructions[0].programId
533
542
  }
543
+
534
544
  /**
535
545
  * Deprecated method
536
546
  * @private
@@ -539,6 +549,7 @@ export class Transaction {
539
549
  invariant(this.instructions.length === 1)
540
550
  return this.instructions[0].data
541
551
  }
552
+
542
553
  /**
543
554
  * Parse a wire transaction into a Transaction object.
544
555
  */
@@ -547,7 +558,7 @@ export class Transaction {
547
558
  let byteArray = [...buffer]
548
559
 
549
560
  const signatureCount = shortvec.decodeLength(byteArray)
550
- let signatures = []
561
+ const signatures = []
551
562
  for (let i = 0; i < signatureCount; i++) {
552
563
  const signature = byteArray.slice(0, SIGNATURE_LENGTH)
553
564
  byteArray = byteArray.slice(SIGNATURE_LENGTH)
@@ -556,6 +567,7 @@ export class Transaction {
556
567
 
557
568
  return Transaction.populate(Message.from(byteArray), signatures)
558
569
  }
570
+
559
571
  /**
560
572
  * Populate Transaction object from message and signatures
561
573
  */
@@ -2,13 +2,14 @@ export function decodeLength(bytes) {
2
2
  let len = 0
3
3
  let size = 0
4
4
  for (;;) {
5
- let elem = bytes.shift()
5
+ const elem = bytes.shift()
6
6
  len |= (elem & 0x7f) << (size * 7)
7
7
  size += 1
8
8
  if ((elem & 0x80) === 0) {
9
9
  break
10
10
  }
11
11
  }
12
+
12
13
  return len
13
14
  }
14
15
 
@@ -1,9 +1,11 @@
1
1
  export const toBuffer = (arr) => {
2
2
  if (arr instanceof Buffer) {
3
3
  return arr
4
- } else if (arr instanceof Uint8Array) {
4
+ }
5
+
6
+ if (arr instanceof Uint8Array) {
5
7
  return Buffer.from(arr.buffer, arr.byteOffset, arr.byteLength)
6
- } else {
7
- return Buffer.from(arr)
8
8
  }
9
+
10
+ return Buffer.from(arr)
9
11
  }
@@ -1,3 +0,0 @@
1
- /**
2
- * @typedef {string} Blockhash
3
- */