@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.esm.js CHANGED
@@ -378,6 +378,36 @@ function encodeLength(bytes, len) {
378
378
  }
379
379
  }
380
380
 
381
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
382
+ /**
383
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
384
+ */
385
+
386
+ function guardedShift(byteArray) {
387
+ if (byteArray.length === 0) {
388
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
389
+ }
390
+
391
+ return byteArray.shift();
392
+ }
393
+ /**
394
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
395
+ * the array.
396
+ */
397
+
398
+ function guardedSplice(byteArray, ...args) {
399
+ var _args$;
400
+
401
+ const [start] = args;
402
+
403
+ if (args.length === 2 // Implies that `deleteCount` was supplied
404
+ ? start + ((_args$ = args[1]) !== null && _args$ !== void 0 ? _args$ : 0) > byteArray.length : start >= byteArray.length) {
405
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
406
+ }
407
+
408
+ return byteArray.splice(...args);
409
+ }
410
+
381
411
  /**
382
412
  * The message header, identifying signed and read-only account
383
413
  */
@@ -462,32 +492,28 @@ class Message {
462
492
  static from(buffer) {
463
493
  // Slice up wire data
464
494
  let byteArray = [...buffer];
465
- const numRequiredSignatures = byteArray.shift();
466
- const numReadonlySignedAccounts = byteArray.shift();
467
- const numReadonlyUnsignedAccounts = byteArray.shift();
495
+ const numRequiredSignatures = guardedShift(byteArray);
496
+ const numReadonlySignedAccounts = guardedShift(byteArray);
497
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
468
498
  const accountCount = decodeLength(byteArray);
469
499
  let accountKeys = [];
470
500
 
471
501
  for (let i = 0; i < accountCount; i++) {
472
- const account = byteArray.slice(0, PUBKEY_LENGTH);
473
- byteArray = byteArray.slice(PUBKEY_LENGTH);
502
+ const account = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
474
503
  accountKeys.push(bs58.encode(Buffer.from(account)));
475
504
  }
476
505
 
477
- const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
478
- byteArray = byteArray.slice(PUBKEY_LENGTH);
506
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
479
507
  const instructionCount = decodeLength(byteArray);
480
508
  let instructions = [];
481
509
 
482
510
  for (let i = 0; i < instructionCount; i++) {
483
- const programIdIndex = byteArray.shift();
511
+ const programIdIndex = guardedShift(byteArray);
484
512
  const accountCount = decodeLength(byteArray);
485
- const accounts = byteArray.slice(0, accountCount);
486
- byteArray = byteArray.slice(accountCount);
513
+ const accounts = guardedSplice(byteArray, 0, accountCount);
487
514
  const dataLength = decodeLength(byteArray);
488
- const dataSlice = byteArray.slice(0, dataLength);
515
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
489
516
  const data = bs58.encode(Buffer.from(dataSlice));
490
- byteArray = byteArray.slice(dataLength);
491
517
  instructions.push({
492
518
  programIdIndex,
493
519
  accounts,
@@ -1107,8 +1133,7 @@ class Transaction {
1107
1133
  let signatures = [];
1108
1134
 
1109
1135
  for (let i = 0; i < signatureCount; i++) {
1110
- const signature = byteArray.slice(0, SIGNATURE_LENGTH);
1111
- byteArray = byteArray.slice(SIGNATURE_LENGTH);
1136
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH);
1112
1137
  signatures.push(bs58.encode(Buffer.from(signature)));
1113
1138
  }
1114
1139
 
@@ -5960,10 +5985,8 @@ class ValidatorInfo {
5960
5985
  const configKeys = [];
5961
5986
 
5962
5987
  for (let i = 0; i < 2; i++) {
5963
- const publicKey = new PublicKey(byteArray.slice(0, PUBKEY_LENGTH));
5964
- byteArray = byteArray.slice(PUBKEY_LENGTH);
5965
- const isSigner = byteArray.slice(0, 1)[0] === 1;
5966
- byteArray = byteArray.slice(1);
5988
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBKEY_LENGTH));
5989
+ const isSigner = guardedShift(byteArray) === 1;
5967
5990
  configKeys.push({
5968
5991
  publicKey,
5969
5992
  isSigner