@solana/web3.js 1.10.0 → 1.10.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
@@ -169,7 +169,7 @@ class PublicKey {
169
169
  let buffer$1 = buffer.Buffer.alloc(0);
170
170
  seeds.forEach(function (seed) {
171
171
  if (seed.length > MAX_SEED_LENGTH) {
172
- throw new Error("Max seed length exceeded");
172
+ throw new TypeError("Max seed length exceeded");
173
173
  }
174
174
 
175
175
  buffer$1 = buffer.Buffer.concat([buffer$1, toBuffer(seed)]);
@@ -202,6 +202,10 @@ class PublicKey {
202
202
  const seedsWithNonce = seeds.concat(buffer.Buffer.from([nonce]));
203
203
  address = await this.createProgramAddress(seedsWithNonce, programId);
204
204
  } catch (err) {
205
+ if (err instanceof TypeError) {
206
+ throw err;
207
+ }
208
+
205
209
  nonce--;
206
210
  continue;
207
211
  }
@@ -414,6 +418,36 @@ function encodeLength(bytes, len) {
414
418
  }
415
419
  }
416
420
 
421
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
422
+ /**
423
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
424
+ */
425
+
426
+ function guardedShift(byteArray) {
427
+ if (byteArray.length === 0) {
428
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
429
+ }
430
+
431
+ return byteArray.shift();
432
+ }
433
+ /**
434
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
435
+ * the array.
436
+ */
437
+
438
+ function guardedSplice(byteArray, ...args) {
439
+ var _args$;
440
+
441
+ const [start] = args;
442
+
443
+ if (args.length === 2 // Implies that `deleteCount` was supplied
444
+ ? start + ((_args$ = args[1]) !== null && _args$ !== void 0 ? _args$ : 0) > byteArray.length : start >= byteArray.length) {
445
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
446
+ }
447
+
448
+ return byteArray.splice(...args);
449
+ }
450
+
417
451
  /**
418
452
  * The message header, identifying signed and read-only account
419
453
  */
@@ -490,32 +524,28 @@ class Message {
490
524
  static from(buffer$1) {
491
525
  // Slice up wire data
492
526
  let byteArray = [...buffer$1];
493
- const numRequiredSignatures = byteArray.shift();
494
- const numReadonlySignedAccounts = byteArray.shift();
495
- const numReadonlyUnsignedAccounts = byteArray.shift();
527
+ const numRequiredSignatures = guardedShift(byteArray);
528
+ const numReadonlySignedAccounts = guardedShift(byteArray);
529
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
496
530
  const accountCount = decodeLength(byteArray);
497
531
  let accountKeys = [];
498
532
 
499
533
  for (let i = 0; i < accountCount; i++) {
500
- const account = byteArray.slice(0, PUBKEY_LENGTH);
501
- byteArray = byteArray.slice(PUBKEY_LENGTH);
534
+ const account = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
502
535
  accountKeys.push(bs58__default['default'].encode(buffer.Buffer.from(account)));
503
536
  }
504
537
 
505
- const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
506
- byteArray = byteArray.slice(PUBKEY_LENGTH);
538
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
507
539
  const instructionCount = decodeLength(byteArray);
508
540
  let instructions = [];
509
541
 
510
542
  for (let i = 0; i < instructionCount; i++) {
511
- const programIdIndex = byteArray.shift();
543
+ const programIdIndex = guardedShift(byteArray);
512
544
  const accountCount = decodeLength(byteArray);
513
- const accounts = byteArray.slice(0, accountCount);
514
- byteArray = byteArray.slice(accountCount);
545
+ const accounts = guardedSplice(byteArray, 0, accountCount);
515
546
  const dataLength = decodeLength(byteArray);
516
- const dataSlice = byteArray.slice(0, dataLength);
547
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
517
548
  const data = bs58__default['default'].encode(buffer.Buffer.from(dataSlice));
518
- byteArray = byteArray.slice(dataLength);
519
549
  instructions.push({
520
550
  programIdIndex,
521
551
  accounts,
@@ -538,6 +568,10 @@ class Message {
538
568
 
539
569
  }
540
570
 
571
+ /**
572
+ * Transaction signature as base-58 encoded string
573
+ */
574
+
541
575
  /**
542
576
  * Default (empty) signature
543
577
  *
@@ -1121,8 +1155,7 @@ class Transaction {
1121
1155
  let signatures = [];
1122
1156
 
1123
1157
  for (let i = 0; i < signatureCount; i++) {
1124
- const signature = byteArray.slice(0, SIGNATURE_LENGTH);
1125
- byteArray = byteArray.slice(SIGNATURE_LENGTH);
1158
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH);
1126
1159
  signatures.push(bs58__default['default'].encode(buffer.Buffer.from(signature)));
1127
1160
  }
1128
1161
 
@@ -6155,10 +6188,8 @@ class ValidatorInfo {
6155
6188
  const configKeys = [];
6156
6189
 
6157
6190
  for (let i = 0; i < 2; i++) {
6158
- const publicKey = new PublicKey(byteArray.slice(0, PUBKEY_LENGTH));
6159
- byteArray = byteArray.slice(PUBKEY_LENGTH);
6160
- const isSigner = byteArray.slice(0, 1)[0] === 1;
6161
- byteArray = byteArray.slice(1);
6191
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBKEY_LENGTH));
6192
+ const isSigner = guardedShift(byteArray) === 1;
6162
6193
  configKeys.push({
6163
6194
  publicKey,
6164
6195
  isSigner