@solana/web3.js 1.26.0 → 1.26.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.
package/lib/index.esm.js CHANGED
@@ -439,6 +439,36 @@ function encodeLength(bytes, len) {
439
439
  }
440
440
  }
441
441
 
442
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
443
+ /**
444
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
445
+ */
446
+
447
+ function guardedShift(byteArray) {
448
+ if (byteArray.length === 0) {
449
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
450
+ }
451
+
452
+ return byteArray.shift();
453
+ }
454
+ /**
455
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
456
+ * the array.
457
+ */
458
+
459
+ function guardedSplice(byteArray, ...args) {
460
+ var _args$;
461
+
462
+ const [start] = args;
463
+
464
+ if (args.length === 2 // Implies that `deleteCount` was supplied
465
+ ? start + ((_args$ = args[1]) !== null && _args$ !== void 0 ? _args$ : 0) > byteArray.length : start >= byteArray.length) {
466
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
467
+ }
468
+
469
+ return byteArray.splice(...args);
470
+ }
471
+
442
472
  /**
443
473
  * The message header, identifying signed and read-only account
444
474
  */
@@ -523,32 +553,28 @@ class Message {
523
553
  static from(buffer) {
524
554
  // Slice up wire data
525
555
  let byteArray = [...buffer];
526
- const numRequiredSignatures = byteArray.shift();
527
- const numReadonlySignedAccounts = byteArray.shift();
528
- const numReadonlyUnsignedAccounts = byteArray.shift();
556
+ const numRequiredSignatures = guardedShift(byteArray);
557
+ const numReadonlySignedAccounts = guardedShift(byteArray);
558
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
529
559
  const accountCount = decodeLength(byteArray);
530
560
  let accountKeys = [];
531
561
 
532
562
  for (let i = 0; i < accountCount; i++) {
533
- const account = byteArray.slice(0, PUBKEY_LENGTH);
534
- byteArray = byteArray.slice(PUBKEY_LENGTH);
563
+ const account = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
535
564
  accountKeys.push(bs58.encode(Buffer.from(account)));
536
565
  }
537
566
 
538
- const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
539
- byteArray = byteArray.slice(PUBKEY_LENGTH);
567
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
540
568
  const instructionCount = decodeLength(byteArray);
541
569
  let instructions = [];
542
570
 
543
571
  for (let i = 0; i < instructionCount; i++) {
544
- const programIdIndex = byteArray.shift();
572
+ const programIdIndex = guardedShift(byteArray);
545
573
  const accountCount = decodeLength(byteArray);
546
- const accounts = byteArray.slice(0, accountCount);
547
- byteArray = byteArray.slice(accountCount);
574
+ const accounts = guardedSplice(byteArray, 0, accountCount);
548
575
  const dataLength = decodeLength(byteArray);
549
- const dataSlice = byteArray.slice(0, dataLength);
576
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
550
577
  const data = bs58.encode(Buffer.from(dataSlice));
551
- byteArray = byteArray.slice(dataLength);
552
578
  instructions.push({
553
579
  programIdIndex,
554
580
  accounts,
@@ -577,6 +603,10 @@ function assert (condition, message) {
577
603
  }
578
604
  }
579
605
 
606
+ /**
607
+ * Transaction signature as base-58 encoded string
608
+ */
609
+
580
610
  /**
581
611
  * Default (empty) signature
582
612
  *
@@ -1170,8 +1200,7 @@ class Transaction {
1170
1200
  let signatures = [];
1171
1201
 
1172
1202
  for (let i = 0; i < signatureCount; i++) {
1173
- const signature = byteArray.slice(0, SIGNATURE_LENGTH);
1174
- byteArray = byteArray.slice(SIGNATURE_LENGTH);
1203
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH);
1175
1204
  signatures.push(bs58.encode(Buffer.from(signature)));
1176
1205
  }
1177
1206
 
@@ -6584,10 +6613,8 @@ class ValidatorInfo {
6584
6613
  const configKeys = [];
6585
6614
 
6586
6615
  for (let i = 0; i < 2; i++) {
6587
- const publicKey = new PublicKey(byteArray.slice(0, PUBKEY_LENGTH));
6588
- byteArray = byteArray.slice(PUBKEY_LENGTH);
6589
- const isSigner = byteArray.slice(0, 1)[0] === 1;
6590
- byteArray = byteArray.slice(1);
6616
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBKEY_LENGTH));
6617
+ const isSigner = guardedShift(byteArray) === 1;
6591
6618
  configKeys.push({
6592
6619
  publicKey,
6593
6620
  isSigner