@solana/web3.js 1.21.0 → 1.21.1

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.
@@ -436,6 +436,36 @@ function encodeLength(bytes, len) {
436
436
  }
437
437
  }
438
438
 
439
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
440
+ /**
441
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
442
+ */
443
+
444
+ function guardedShift(byteArray) {
445
+ if (byteArray.length === 0) {
446
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
447
+ }
448
+
449
+ return byteArray.shift();
450
+ }
451
+ /**
452
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
453
+ * the array.
454
+ */
455
+
456
+ function guardedSplice(byteArray, ...args) {
457
+ var _args$;
458
+
459
+ const [start] = args;
460
+
461
+ if (args.length === 2 // Implies that `deleteCount` was supplied
462
+ ? start + ((_args$ = args[1]) !== null && _args$ !== void 0 ? _args$ : 0) > byteArray.length : start >= byteArray.length) {
463
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
464
+ }
465
+
466
+ return byteArray.splice(...args);
467
+ }
468
+
439
469
  /**
440
470
  * The message header, identifying signed and read-only account
441
471
  */
@@ -520,32 +550,28 @@ class Message {
520
550
  static from(buffer) {
521
551
  // Slice up wire data
522
552
  let byteArray = [...buffer];
523
- const numRequiredSignatures = byteArray.shift();
524
- const numReadonlySignedAccounts = byteArray.shift();
525
- const numReadonlyUnsignedAccounts = byteArray.shift();
553
+ const numRequiredSignatures = guardedShift(byteArray);
554
+ const numReadonlySignedAccounts = guardedShift(byteArray);
555
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
526
556
  const accountCount = decodeLength(byteArray);
527
557
  let accountKeys = [];
528
558
 
529
559
  for (let i = 0; i < accountCount; i++) {
530
- const account = byteArray.slice(0, PUBKEY_LENGTH);
531
- byteArray = byteArray.slice(PUBKEY_LENGTH);
560
+ const account = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
532
561
  accountKeys.push(bs58.encode(Buffer.from(account)));
533
562
  }
534
563
 
535
- const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
536
- byteArray = byteArray.slice(PUBKEY_LENGTH);
564
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
537
565
  const instructionCount = decodeLength(byteArray);
538
566
  let instructions = [];
539
567
 
540
568
  for (let i = 0; i < instructionCount; i++) {
541
- const programIdIndex = byteArray.shift();
569
+ const programIdIndex = guardedShift(byteArray);
542
570
  const accountCount = decodeLength(byteArray);
543
- const accounts = byteArray.slice(0, accountCount);
544
- byteArray = byteArray.slice(accountCount);
571
+ const accounts = guardedSplice(byteArray, 0, accountCount);
545
572
  const dataLength = decodeLength(byteArray);
546
- const dataSlice = byteArray.slice(0, dataLength);
573
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
547
574
  const data = bs58.encode(Buffer.from(dataSlice));
548
- byteArray = byteArray.slice(dataLength);
549
575
  instructions.push({
550
576
  programIdIndex,
551
577
  accounts,
@@ -574,6 +600,10 @@ function assert (condition, message) {
574
600
  }
575
601
  }
576
602
 
603
+ /**
604
+ * Transaction signature as base-58 encoded string
605
+ */
606
+
577
607
  /**
578
608
  * Default (empty) signature
579
609
  *
@@ -1167,8 +1197,7 @@ class Transaction {
1167
1197
  let signatures = [];
1168
1198
 
1169
1199
  for (let i = 0; i < signatureCount; i++) {
1170
- const signature = byteArray.slice(0, SIGNATURE_LENGTH);
1171
- byteArray = byteArray.slice(SIGNATURE_LENGTH);
1200
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH);
1172
1201
  signatures.push(bs58.encode(Buffer.from(signature)));
1173
1202
  }
1174
1203
 
@@ -6457,10 +6486,8 @@ class ValidatorInfo {
6457
6486
  const configKeys = [];
6458
6487
 
6459
6488
  for (let i = 0; i < 2; i++) {
6460
- const publicKey = new PublicKey(byteArray.slice(0, PUBKEY_LENGTH));
6461
- byteArray = byteArray.slice(PUBKEY_LENGTH);
6462
- const isSigner = byteArray.slice(0, 1)[0] === 1;
6463
- byteArray = byteArray.slice(1);
6489
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBKEY_LENGTH));
6490
+ const isSigner = guardedShift(byteArray) === 1;
6464
6491
  configKeys.push({
6465
6492
  publicKey,
6466
6493
  isSigner