@solana/web3.js 1.13.0 → 1.13.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.cjs.js CHANGED
@@ -469,6 +469,36 @@ function encodeLength(bytes, len) {
469
469
  }
470
470
  }
471
471
 
472
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
473
+ /**
474
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
475
+ */
476
+
477
+ function guardedShift(byteArray) {
478
+ if (byteArray.length === 0) {
479
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
480
+ }
481
+
482
+ return byteArray.shift();
483
+ }
484
+ /**
485
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
486
+ * the array.
487
+ */
488
+
489
+ function guardedSplice(byteArray, ...args) {
490
+ var _args$;
491
+
492
+ const [start] = args;
493
+
494
+ if (args.length === 2 // Implies that `deleteCount` was supplied
495
+ ? start + ((_args$ = args[1]) !== null && _args$ !== void 0 ? _args$ : 0) > byteArray.length : start >= byteArray.length) {
496
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
497
+ }
498
+
499
+ return byteArray.splice(...args);
500
+ }
501
+
472
502
  /**
473
503
  * The message header, identifying signed and read-only account
474
504
  */
@@ -545,32 +575,28 @@ class Message {
545
575
  static from(buffer$1) {
546
576
  // Slice up wire data
547
577
  let byteArray = [...buffer$1];
548
- const numRequiredSignatures = byteArray.shift();
549
- const numReadonlySignedAccounts = byteArray.shift();
550
- const numReadonlyUnsignedAccounts = byteArray.shift();
578
+ const numRequiredSignatures = guardedShift(byteArray);
579
+ const numReadonlySignedAccounts = guardedShift(byteArray);
580
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
551
581
  const accountCount = decodeLength(byteArray);
552
582
  let accountKeys = [];
553
583
 
554
584
  for (let i = 0; i < accountCount; i++) {
555
- const account = byteArray.slice(0, PUBKEY_LENGTH);
556
- byteArray = byteArray.slice(PUBKEY_LENGTH);
585
+ const account = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
557
586
  accountKeys.push(bs58__default['default'].encode(buffer.Buffer.from(account)));
558
587
  }
559
588
 
560
- const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
561
- byteArray = byteArray.slice(PUBKEY_LENGTH);
589
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
562
590
  const instructionCount = decodeLength(byteArray);
563
591
  let instructions = [];
564
592
 
565
593
  for (let i = 0; i < instructionCount; i++) {
566
- const programIdIndex = byteArray.shift();
594
+ const programIdIndex = guardedShift(byteArray);
567
595
  const accountCount = decodeLength(byteArray);
568
- const accounts = byteArray.slice(0, accountCount);
569
- byteArray = byteArray.slice(accountCount);
596
+ const accounts = guardedSplice(byteArray, 0, accountCount);
570
597
  const dataLength = decodeLength(byteArray);
571
- const dataSlice = byteArray.slice(0, dataLength);
598
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
572
599
  const data = bs58__default['default'].encode(buffer.Buffer.from(dataSlice));
573
- byteArray = byteArray.slice(dataLength);
574
600
  instructions.push({
575
601
  programIdIndex,
576
602
  accounts,
@@ -593,6 +619,10 @@ class Message {
593
619
 
594
620
  }
595
621
 
622
+ /**
623
+ * Transaction signature as base-58 encoded string
624
+ */
625
+
596
626
  /**
597
627
  * Default (empty) signature
598
628
  *
@@ -1176,8 +1206,7 @@ class Transaction {
1176
1206
  let signatures = [];
1177
1207
 
1178
1208
  for (let i = 0; i < signatureCount; i++) {
1179
- const signature = byteArray.slice(0, SIGNATURE_LENGTH);
1180
- byteArray = byteArray.slice(SIGNATURE_LENGTH);
1209
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH);
1181
1210
  signatures.push(bs58__default['default'].encode(buffer.Buffer.from(signature)));
1182
1211
  }
1183
1212
 
@@ -6213,10 +6242,8 @@ class ValidatorInfo {
6213
6242
  const configKeys = [];
6214
6243
 
6215
6244
  for (let i = 0; i < 2; i++) {
6216
- const publicKey = new PublicKey(byteArray.slice(0, PUBKEY_LENGTH));
6217
- byteArray = byteArray.slice(PUBKEY_LENGTH);
6218
- const isSigner = byteArray.slice(0, 1)[0] === 1;
6219
- byteArray = byteArray.slice(1);
6245
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBKEY_LENGTH));
6246
+ const isSigner = guardedShift(byteArray) === 1;
6220
6247
  configKeys.push({
6221
6248
  publicKey,
6222
6249
  isSigner