@solana/web3.js 1.3.0 → 1.3.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
@@ -365,6 +365,36 @@ function encodeLength(bytes, len) {
365
365
  }
366
366
  }
367
367
 
368
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
369
+ /**
370
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
371
+ */
372
+
373
+ function guardedShift(byteArray) {
374
+ if (byteArray.length === 0) {
375
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
376
+ }
377
+
378
+ return byteArray.shift();
379
+ }
380
+ /**
381
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
382
+ * the array.
383
+ */
384
+
385
+ function guardedSplice(byteArray, ...args) {
386
+ var _args$;
387
+
388
+ const [start] = args;
389
+
390
+ if (args.length === 2 // Implies that `deleteCount` was supplied
391
+ ? start + ((_args$ = args[1]) !== null && _args$ !== void 0 ? _args$ : 0) > byteArray.length : start >= byteArray.length) {
392
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
393
+ }
394
+
395
+ return byteArray.splice(...args);
396
+ }
397
+
368
398
  /**
369
399
  * The message header, identifying signed and read-only account
370
400
  */
@@ -449,32 +479,28 @@ class Message {
449
479
  static from(buffer) {
450
480
  // Slice up wire data
451
481
  let byteArray = [...buffer];
452
- const numRequiredSignatures = byteArray.shift();
453
- const numReadonlySignedAccounts = byteArray.shift();
454
- const numReadonlyUnsignedAccounts = byteArray.shift();
482
+ const numRequiredSignatures = guardedShift(byteArray);
483
+ const numReadonlySignedAccounts = guardedShift(byteArray);
484
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
455
485
  const accountCount = decodeLength(byteArray);
456
486
  let accountKeys = [];
457
487
 
458
488
  for (let i = 0; i < accountCount; i++) {
459
- const account = byteArray.slice(0, PUBKEY_LENGTH);
460
- byteArray = byteArray.slice(PUBKEY_LENGTH);
489
+ const account = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
461
490
  accountKeys.push(bs58.encode(Buffer.from(account)));
462
491
  }
463
492
 
464
- const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
465
- byteArray = byteArray.slice(PUBKEY_LENGTH);
493
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
466
494
  const instructionCount = decodeLength(byteArray);
467
495
  let instructions = [];
468
496
 
469
497
  for (let i = 0; i < instructionCount; i++) {
470
- const programIdIndex = byteArray.shift();
498
+ const programIdIndex = guardedShift(byteArray);
471
499
  const accountCount = decodeLength(byteArray);
472
- const accounts = byteArray.slice(0, accountCount);
473
- byteArray = byteArray.slice(accountCount);
500
+ const accounts = guardedSplice(byteArray, 0, accountCount);
474
501
  const dataLength = decodeLength(byteArray);
475
- const dataSlice = byteArray.slice(0, dataLength);
502
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
476
503
  const data = bs58.encode(Buffer.from(dataSlice));
477
- byteArray = byteArray.slice(dataLength);
478
504
  instructions.push({
479
505
  programIdIndex,
480
506
  accounts,
@@ -1094,8 +1120,7 @@ class Transaction {
1094
1120
  let signatures = [];
1095
1121
 
1096
1122
  for (let i = 0; i < signatureCount; i++) {
1097
- const signature = byteArray.slice(0, SIGNATURE_LENGTH);
1098
- byteArray = byteArray.slice(SIGNATURE_LENGTH);
1123
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH);
1099
1124
  signatures.push(bs58.encode(Buffer.from(signature)));
1100
1125
  }
1101
1126
 
@@ -5816,10 +5841,8 @@ class ValidatorInfo {
5816
5841
  const configKeys = [];
5817
5842
 
5818
5843
  for (let i = 0; i < 2; i++) {
5819
- const publicKey = new PublicKey(byteArray.slice(0, PUBKEY_LENGTH));
5820
- byteArray = byteArray.slice(PUBKEY_LENGTH);
5821
- const isSigner = byteArray.slice(0, 1)[0] === 1;
5822
- byteArray = byteArray.slice(1);
5844
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBKEY_LENGTH));
5845
+ const isSigner = guardedShift(byteArray) === 1;
5823
5846
  configKeys.push({
5824
5847
  publicKey,
5825
5848
  isSigner