@solana/web3.js 1.44.2 → 1.44.4

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.
@@ -2187,6 +2187,36 @@ function encodeLength(bytes, len) {
2187
2187
  }
2188
2188
  }
2189
2189
 
2190
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
2191
+ /**
2192
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
2193
+ */
2194
+
2195
+ function guardedShift(byteArray) {
2196
+ if (byteArray.length === 0) {
2197
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
2198
+ }
2199
+
2200
+ return byteArray.shift();
2201
+ }
2202
+ /**
2203
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
2204
+ * the array.
2205
+ */
2206
+
2207
+ function guardedSplice(byteArray, ...args) {
2208
+ var _args$;
2209
+
2210
+ const [start] = args;
2211
+
2212
+ if (args.length === 2 // Implies that `deleteCount` was supplied
2213
+ ? start + ((_args$ = args[1]) !== null && _args$ !== void 0 ? _args$ : 0) > byteArray.length : start >= byteArray.length) {
2214
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
2215
+ }
2216
+
2217
+ return byteArray.splice(...args);
2218
+ }
2219
+
2190
2220
  /**
2191
2221
  * The message header, identifying signed and read-only account
2192
2222
  */
@@ -2285,32 +2315,28 @@ class Message {
2285
2315
  static from(buffer) {
2286
2316
  // Slice up wire data
2287
2317
  let byteArray = [...buffer];
2288
- const numRequiredSignatures = byteArray.shift();
2289
- const numReadonlySignedAccounts = byteArray.shift();
2290
- const numReadonlyUnsignedAccounts = byteArray.shift();
2318
+ const numRequiredSignatures = guardedShift(byteArray);
2319
+ const numReadonlySignedAccounts = guardedShift(byteArray);
2320
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
2291
2321
  const accountCount = decodeLength(byteArray);
2292
2322
  let accountKeys = [];
2293
2323
 
2294
2324
  for (let i = 0; i < accountCount; i++) {
2295
- const account = byteArray.slice(0, PUBKEY_LENGTH);
2296
- byteArray = byteArray.slice(PUBKEY_LENGTH);
2325
+ const account = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
2297
2326
  accountKeys.push(bs58.encode(Buffer.from(account)));
2298
2327
  }
2299
2328
 
2300
- const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
2301
- byteArray = byteArray.slice(PUBKEY_LENGTH);
2329
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
2302
2330
  const instructionCount = decodeLength(byteArray);
2303
2331
  let instructions = [];
2304
2332
 
2305
2333
  for (let i = 0; i < instructionCount; i++) {
2306
- const programIdIndex = byteArray.shift();
2334
+ const programIdIndex = guardedShift(byteArray);
2307
2335
  const accountCount = decodeLength(byteArray);
2308
- const accounts = byteArray.slice(0, accountCount);
2309
- byteArray = byteArray.slice(accountCount);
2336
+ const accounts = guardedSplice(byteArray, 0, accountCount);
2310
2337
  const dataLength = decodeLength(byteArray);
2311
- const dataSlice = byteArray.slice(0, dataLength);
2338
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
2312
2339
  const data = bs58.encode(Buffer.from(dataSlice));
2313
- byteArray = byteArray.slice(dataLength);
2314
2340
  instructions.push({
2315
2341
  programIdIndex,
2316
2342
  accounts,
@@ -2339,6 +2365,10 @@ function assert (condition, message) {
2339
2365
  }
2340
2366
  }
2341
2367
 
2368
+ /**
2369
+ * Transaction signature as base-58 encoded string
2370
+ */
2371
+
2342
2372
  let TransactionStatus;
2343
2373
  /**
2344
2374
  * Default (empty) signature
@@ -2510,24 +2540,27 @@ class Transaction {
2510
2540
  return this._message;
2511
2541
  }
2512
2542
 
2513
- const {
2514
- nonceInfo
2515
- } = this;
2543
+ let recentBlockhash;
2544
+ let instructions;
2516
2545
 
2517
- if (nonceInfo && this.instructions[0] != nonceInfo.nonceInstruction) {
2518
- this.recentBlockhash = nonceInfo.nonce;
2519
- this.instructions.unshift(nonceInfo.nonceInstruction);
2520
- }
2546
+ if (this.nonceInfo) {
2547
+ recentBlockhash = this.nonceInfo.nonce;
2521
2548
 
2522
- const {
2523
- recentBlockhash
2524
- } = this;
2549
+ if (this.instructions[0] != this.nonceInfo.nonceInstruction) {
2550
+ instructions = [this.nonceInfo.nonceInstruction, ...this.instructions];
2551
+ } else {
2552
+ instructions = this.instructions;
2553
+ }
2554
+ } else {
2555
+ recentBlockhash = this.recentBlockhash;
2556
+ instructions = this.instructions;
2557
+ }
2525
2558
 
2526
2559
  if (!recentBlockhash) {
2527
2560
  throw new Error('Transaction recentBlockhash required');
2528
2561
  }
2529
2562
 
2530
- if (this.instructions.length < 1) {
2563
+ if (instructions.length < 1) {
2531
2564
  console.warn('No instructions provided');
2532
2565
  }
2533
2566
 
@@ -2542,15 +2575,15 @@ class Transaction {
2542
2575
  throw new Error('Transaction fee payer required');
2543
2576
  }
2544
2577
 
2545
- for (let i = 0; i < this.instructions.length; i++) {
2546
- if (this.instructions[i].programId === undefined) {
2578
+ for (let i = 0; i < instructions.length; i++) {
2579
+ if (instructions[i].programId === undefined) {
2547
2580
  throw new Error(`Transaction instruction index ${i} has undefined program id`);
2548
2581
  }
2549
2582
  }
2550
2583
 
2551
2584
  const programIds = [];
2552
2585
  const accountMetas = [];
2553
- this.instructions.forEach(instruction => {
2586
+ instructions.forEach(instruction => {
2554
2587
  instruction.keys.forEach(accountMeta => {
2555
2588
  accountMetas.push({ ...accountMeta
2556
2589
  });
@@ -2660,7 +2693,7 @@ class Transaction {
2660
2693
  }
2661
2694
  });
2662
2695
  const accountKeys = signedKeys.concat(unsignedKeys);
2663
- const instructions = this.instructions.map(instruction => {
2696
+ const compiledInstructions = instructions.map(instruction => {
2664
2697
  const {
2665
2698
  data,
2666
2699
  programId
@@ -2671,7 +2704,7 @@ class Transaction {
2671
2704
  data: bs58.encode(data)
2672
2705
  };
2673
2706
  });
2674
- instructions.forEach(instruction => {
2707
+ compiledInstructions.forEach(instruction => {
2675
2708
  assert(instruction.programIdIndex >= 0);
2676
2709
  instruction.accounts.forEach(keyIndex => assert(keyIndex >= 0));
2677
2710
  });
@@ -2683,7 +2716,7 @@ class Transaction {
2683
2716
  },
2684
2717
  accountKeys,
2685
2718
  recentBlockhash,
2686
- instructions
2719
+ instructions: compiledInstructions
2687
2720
  });
2688
2721
  }
2689
2722
  /**
@@ -2996,8 +3029,7 @@ class Transaction {
2996
3029
  let signatures = [];
2997
3030
 
2998
3031
  for (let i = 0; i < signatureCount; i++) {
2999
- const signature = byteArray.slice(0, SIGNATURE_LENGTH_IN_BYTES);
3000
- byteArray = byteArray.slice(SIGNATURE_LENGTH_IN_BYTES);
3032
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH_IN_BYTES);
3001
3033
  signatures.push(bs58.encode(Buffer.from(signature)));
3002
3034
  }
3003
3035
 
@@ -9257,10 +9289,8 @@ class ValidatorInfo {
9257
9289
  const configKeys = [];
9258
9290
 
9259
9291
  for (let i = 0; i < 2; i++) {
9260
- const publicKey = new PublicKey(byteArray.slice(0, PUBKEY_LENGTH));
9261
- byteArray = byteArray.slice(PUBKEY_LENGTH);
9262
- const isSigner = byteArray.slice(0, 1)[0] === 1;
9263
- byteArray = byteArray.slice(1);
9292
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBKEY_LENGTH));
9293
+ const isSigner = guardedShift(byteArray) === 1;
9264
9294
  configKeys.push({
9265
9295
  publicKey,
9266
9296
  isSigner