@solana/web3.js 1.28.0 → 1.28.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.
@@ -439,6 +439,36 @@ function encodeLength(bytes, len) {
439
439
  }
440
440
  }
441
441
 
442
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
443
+ /**
444
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
445
+ */
446
+
447
+ function guardedShift(byteArray) {
448
+ if (byteArray.length === 0) {
449
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
450
+ }
451
+
452
+ return byteArray.shift();
453
+ }
454
+ /**
455
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
456
+ * the array.
457
+ */
458
+
459
+ function guardedSplice(byteArray, ...args) {
460
+ var _args$;
461
+
462
+ const [start] = args;
463
+
464
+ if (args.length === 2 // Implies that `deleteCount` was supplied
465
+ ? start + ((_args$ = args[1]) !== null && _args$ !== void 0 ? _args$ : 0) > byteArray.length : start >= byteArray.length) {
466
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
467
+ }
468
+
469
+ return byteArray.splice(...args);
470
+ }
471
+
442
472
  /**
443
473
  * The message header, identifying signed and read-only account
444
474
  */
@@ -542,32 +572,28 @@ class Message {
542
572
  static from(buffer) {
543
573
  // Slice up wire data
544
574
  let byteArray = [...buffer];
545
- const numRequiredSignatures = byteArray.shift();
546
- const numReadonlySignedAccounts = byteArray.shift();
547
- const numReadonlyUnsignedAccounts = byteArray.shift();
575
+ const numRequiredSignatures = guardedShift(byteArray);
576
+ const numReadonlySignedAccounts = guardedShift(byteArray);
577
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
548
578
  const accountCount = decodeLength(byteArray);
549
579
  let accountKeys = [];
550
580
 
551
581
  for (let i = 0; i < accountCount; i++) {
552
- const account = byteArray.slice(0, PUBKEY_LENGTH);
553
- byteArray = byteArray.slice(PUBKEY_LENGTH);
582
+ const account = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
554
583
  accountKeys.push(bs58.encode(Buffer.from(account)));
555
584
  }
556
585
 
557
- const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
558
- byteArray = byteArray.slice(PUBKEY_LENGTH);
586
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
559
587
  const instructionCount = decodeLength(byteArray);
560
588
  let instructions = [];
561
589
 
562
590
  for (let i = 0; i < instructionCount; i++) {
563
- const programIdIndex = byteArray.shift();
591
+ const programIdIndex = guardedShift(byteArray);
564
592
  const accountCount = decodeLength(byteArray);
565
- const accounts = byteArray.slice(0, accountCount);
566
- byteArray = byteArray.slice(accountCount);
593
+ const accounts = guardedSplice(byteArray, 0, accountCount);
567
594
  const dataLength = decodeLength(byteArray);
568
- const dataSlice = byteArray.slice(0, dataLength);
595
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
569
596
  const data = bs58.encode(Buffer.from(dataSlice));
570
- byteArray = byteArray.slice(dataLength);
571
597
  instructions.push({
572
598
  programIdIndex,
573
599
  accounts,
@@ -596,6 +622,10 @@ function assert (condition, message) {
596
622
  }
597
623
  }
598
624
 
625
+ /**
626
+ * Transaction signature as base-58 encoded string
627
+ */
628
+
599
629
  /**
600
630
  * Default (empty) signature
601
631
  *
@@ -1189,8 +1219,7 @@ class Transaction {
1189
1219
  let signatures = [];
1190
1220
 
1191
1221
  for (let i = 0; i < signatureCount; i++) {
1192
- const signature = byteArray.slice(0, SIGNATURE_LENGTH);
1193
- byteArray = byteArray.slice(SIGNATURE_LENGTH);
1222
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH);
1194
1223
  signatures.push(bs58.encode(Buffer.from(signature)));
1195
1224
  }
1196
1225
 
@@ -7232,10 +7261,8 @@ class ValidatorInfo {
7232
7261
  const configKeys = [];
7233
7262
 
7234
7263
  for (let i = 0; i < 2; i++) {
7235
- const publicKey = new PublicKey(byteArray.slice(0, PUBKEY_LENGTH));
7236
- byteArray = byteArray.slice(PUBKEY_LENGTH);
7237
- const isSigner = byteArray.slice(0, 1)[0] === 1;
7238
- byteArray = byteArray.slice(1);
7264
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBKEY_LENGTH));
7265
+ const isSigner = guardedShift(byteArray) === 1;
7239
7266
  configKeys.push({
7240
7267
  publicKey,
7241
7268
  isSigner