@solana/web3.js 1.9.1 → 1.9.2

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
@@ -412,6 +412,36 @@ function encodeLength(bytes, len) {
412
412
  }
413
413
  }
414
414
 
415
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
416
+ /**
417
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
418
+ */
419
+
420
+ function guardedShift(byteArray) {
421
+ if (byteArray.length === 0) {
422
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
423
+ }
424
+
425
+ return byteArray.shift();
426
+ }
427
+ /**
428
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
429
+ * the array.
430
+ */
431
+
432
+ function guardedSplice(byteArray, ...args) {
433
+ var _args$;
434
+
435
+ const [start] = args;
436
+
437
+ if (args.length === 2 // Implies that `deleteCount` was supplied
438
+ ? start + ((_args$ = args[1]) !== null && _args$ !== void 0 ? _args$ : 0) > byteArray.length : start >= byteArray.length) {
439
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
440
+ }
441
+
442
+ return byteArray.splice(...args);
443
+ }
444
+
415
445
  /**
416
446
  * The message header, identifying signed and read-only account
417
447
  */
@@ -488,32 +518,28 @@ class Message {
488
518
  static from(buffer$1) {
489
519
  // Slice up wire data
490
520
  let byteArray = [...buffer$1];
491
- const numRequiredSignatures = byteArray.shift();
492
- const numReadonlySignedAccounts = byteArray.shift();
493
- const numReadonlyUnsignedAccounts = byteArray.shift();
521
+ const numRequiredSignatures = guardedShift(byteArray);
522
+ const numReadonlySignedAccounts = guardedShift(byteArray);
523
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
494
524
  const accountCount = decodeLength(byteArray);
495
525
  let accountKeys = [];
496
526
 
497
527
  for (let i = 0; i < accountCount; i++) {
498
- const account = byteArray.slice(0, PUBKEY_LENGTH);
499
- byteArray = byteArray.slice(PUBKEY_LENGTH);
528
+ const account = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
500
529
  accountKeys.push(bs58__default['default'].encode(buffer.Buffer.from(account)));
501
530
  }
502
531
 
503
- const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
504
- byteArray = byteArray.slice(PUBKEY_LENGTH);
532
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
505
533
  const instructionCount = decodeLength(byteArray);
506
534
  let instructions = [];
507
535
 
508
536
  for (let i = 0; i < instructionCount; i++) {
509
- const programIdIndex = byteArray.shift();
537
+ const programIdIndex = guardedShift(byteArray);
510
538
  const accountCount = decodeLength(byteArray);
511
- const accounts = byteArray.slice(0, accountCount);
512
- byteArray = byteArray.slice(accountCount);
539
+ const accounts = guardedSplice(byteArray, 0, accountCount);
513
540
  const dataLength = decodeLength(byteArray);
514
- const dataSlice = byteArray.slice(0, dataLength);
541
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
515
542
  const data = bs58__default['default'].encode(buffer.Buffer.from(dataSlice));
516
- byteArray = byteArray.slice(dataLength);
517
543
  instructions.push({
518
544
  programIdIndex,
519
545
  accounts,
@@ -1123,8 +1149,7 @@ class Transaction {
1123
1149
  let signatures = [];
1124
1150
 
1125
1151
  for (let i = 0; i < signatureCount; i++) {
1126
- const signature = byteArray.slice(0, SIGNATURE_LENGTH);
1127
- byteArray = byteArray.slice(SIGNATURE_LENGTH);
1152
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH);
1128
1153
  signatures.push(bs58__default['default'].encode(buffer.Buffer.from(signature)));
1129
1154
  }
1130
1155
 
@@ -6073,10 +6098,8 @@ class ValidatorInfo {
6073
6098
  const configKeys = [];
6074
6099
 
6075
6100
  for (let i = 0; i < 2; i++) {
6076
- const publicKey = new PublicKey(byteArray.slice(0, PUBKEY_LENGTH));
6077
- byteArray = byteArray.slice(PUBKEY_LENGTH);
6078
- const isSigner = byteArray.slice(0, 1)[0] === 1;
6079
- byteArray = byteArray.slice(1);
6101
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBKEY_LENGTH));
6102
+ const isSigner = guardedShift(byteArray) === 1;
6080
6103
  configKeys.push({
6081
6104
  publicKey,
6082
6105
  isSigner