@solana/web3.js 1.7.1 → 1.7.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
@@ -416,6 +416,36 @@ function encodeLength(bytes, len) {
416
416
  }
417
417
  }
418
418
 
419
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
420
+ /**
421
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
422
+ */
423
+
424
+ function guardedShift(byteArray) {
425
+ if (byteArray.length === 0) {
426
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
427
+ }
428
+
429
+ return byteArray.shift();
430
+ }
431
+ /**
432
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
433
+ * the array.
434
+ */
435
+
436
+ function guardedSplice(byteArray, ...args) {
437
+ var _args$;
438
+
439
+ const [start] = args;
440
+
441
+ if (args.length === 2 // Implies that `deleteCount` was supplied
442
+ ? start + ((_args$ = args[1]) !== null && _args$ !== void 0 ? _args$ : 0) > byteArray.length : start >= byteArray.length) {
443
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
444
+ }
445
+
446
+ return byteArray.splice(...args);
447
+ }
448
+
419
449
  /**
420
450
  * The message header, identifying signed and read-only account
421
451
  */
@@ -500,32 +530,28 @@ class Message {
500
530
  static from(buffer$1) {
501
531
  // Slice up wire data
502
532
  let byteArray = [...buffer$1];
503
- const numRequiredSignatures = byteArray.shift();
504
- const numReadonlySignedAccounts = byteArray.shift();
505
- const numReadonlyUnsignedAccounts = byteArray.shift();
533
+ const numRequiredSignatures = guardedShift(byteArray);
534
+ const numReadonlySignedAccounts = guardedShift(byteArray);
535
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
506
536
  const accountCount = decodeLength(byteArray);
507
537
  let accountKeys = [];
508
538
 
509
539
  for (let i = 0; i < accountCount; i++) {
510
- const account = byteArray.slice(0, PUBKEY_LENGTH);
511
- byteArray = byteArray.slice(PUBKEY_LENGTH);
540
+ const account = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
512
541
  accountKeys.push(bs58__default['default'].encode(buffer.Buffer.from(account)));
513
542
  }
514
543
 
515
- const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
516
- byteArray = byteArray.slice(PUBKEY_LENGTH);
544
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
517
545
  const instructionCount = decodeLength(byteArray);
518
546
  let instructions = [];
519
547
 
520
548
  for (let i = 0; i < instructionCount; i++) {
521
- const programIdIndex = byteArray.shift();
549
+ const programIdIndex = guardedShift(byteArray);
522
550
  const accountCount = decodeLength(byteArray);
523
- const accounts = byteArray.slice(0, accountCount);
524
- byteArray = byteArray.slice(accountCount);
551
+ const accounts = guardedSplice(byteArray, 0, accountCount);
525
552
  const dataLength = decodeLength(byteArray);
526
- const dataSlice = byteArray.slice(0, dataLength);
553
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
527
554
  const data = bs58__default['default'].encode(buffer.Buffer.from(dataSlice));
528
- byteArray = byteArray.slice(dataLength);
529
555
  instructions.push({
530
556
  programIdIndex,
531
557
  accounts,
@@ -1145,8 +1171,7 @@ class Transaction {
1145
1171
  let signatures = [];
1146
1172
 
1147
1173
  for (let i = 0; i < signatureCount; i++) {
1148
- const signature = byteArray.slice(0, SIGNATURE_LENGTH);
1149
- byteArray = byteArray.slice(SIGNATURE_LENGTH);
1174
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH);
1150
1175
  signatures.push(bs58__default['default'].encode(buffer.Buffer.from(signature)));
1151
1176
  }
1152
1177
 
@@ -5998,10 +6023,8 @@ class ValidatorInfo {
5998
6023
  const configKeys = [];
5999
6024
 
6000
6025
  for (let i = 0; i < 2; i++) {
6001
- const publicKey = new PublicKey(byteArray.slice(0, PUBKEY_LENGTH));
6002
- byteArray = byteArray.slice(PUBKEY_LENGTH);
6003
- const isSigner = byteArray.slice(0, 1)[0] === 1;
6004
- byteArray = byteArray.slice(1);
6026
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBKEY_LENGTH));
6027
+ const isSigner = guardedShift(byteArray) === 1;
6005
6028
  configKeys.push({
6006
6029
  publicKey,
6007
6030
  isSigner