@solana/web3.js 1.39.0 → 1.39.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
@@ -2169,6 +2169,36 @@ function encodeLength(bytes, len) {
2169
2169
  }
2170
2170
  }
2171
2171
 
2172
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
2173
+ /**
2174
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
2175
+ */
2176
+
2177
+ function guardedShift(byteArray) {
2178
+ if (byteArray.length === 0) {
2179
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
2180
+ }
2181
+
2182
+ return byteArray.shift();
2183
+ }
2184
+ /**
2185
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
2186
+ * the array.
2187
+ */
2188
+
2189
+ function guardedSplice(byteArray, ...args) {
2190
+ var _args$;
2191
+
2192
+ const [start] = args;
2193
+
2194
+ if (args.length === 2 // Implies that `deleteCount` was supplied
2195
+ ? start + ((_args$ = args[1]) !== null && _args$ !== void 0 ? _args$ : 0) > byteArray.length : start >= byteArray.length) {
2196
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
2197
+ }
2198
+
2199
+ return byteArray.splice(...args);
2200
+ }
2201
+
2172
2202
  /**
2173
2203
  * The message header, identifying signed and read-only account
2174
2204
  */
@@ -2267,32 +2297,28 @@ class Message {
2267
2297
  static from(buffer) {
2268
2298
  // Slice up wire data
2269
2299
  let byteArray = [...buffer];
2270
- const numRequiredSignatures = byteArray.shift();
2271
- const numReadonlySignedAccounts = byteArray.shift();
2272
- const numReadonlyUnsignedAccounts = byteArray.shift();
2300
+ const numRequiredSignatures = guardedShift(byteArray);
2301
+ const numReadonlySignedAccounts = guardedShift(byteArray);
2302
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
2273
2303
  const accountCount = decodeLength(byteArray);
2274
2304
  let accountKeys = [];
2275
2305
 
2276
2306
  for (let i = 0; i < accountCount; i++) {
2277
- const account = byteArray.slice(0, PUBKEY_LENGTH);
2278
- byteArray = byteArray.slice(PUBKEY_LENGTH);
2307
+ const account = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
2279
2308
  accountKeys.push(bs58.encode(Buffer.from(account)));
2280
2309
  }
2281
2310
 
2282
- const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
2283
- byteArray = byteArray.slice(PUBKEY_LENGTH);
2311
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
2284
2312
  const instructionCount = decodeLength(byteArray);
2285
2313
  let instructions = [];
2286
2314
 
2287
2315
  for (let i = 0; i < instructionCount; i++) {
2288
- const programIdIndex = byteArray.shift();
2316
+ const programIdIndex = guardedShift(byteArray);
2289
2317
  const accountCount = decodeLength(byteArray);
2290
- const accounts = byteArray.slice(0, accountCount);
2291
- byteArray = byteArray.slice(accountCount);
2318
+ const accounts = guardedSplice(byteArray, 0, accountCount);
2292
2319
  const dataLength = decodeLength(byteArray);
2293
- const dataSlice = byteArray.slice(0, dataLength);
2320
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
2294
2321
  const data = bs58.encode(Buffer.from(dataSlice));
2295
- byteArray = byteArray.slice(dataLength);
2296
2322
  instructions.push({
2297
2323
  programIdIndex,
2298
2324
  accounts,
@@ -2321,6 +2347,10 @@ function assert (condition, message) {
2321
2347
  }
2322
2348
  }
2323
2349
 
2350
+ /**
2351
+ * Transaction signature as base-58 encoded string
2352
+ */
2353
+
2324
2354
  /**
2325
2355
  * Default (empty) signature
2326
2356
  *
@@ -2368,6 +2398,26 @@ class TransactionInstruction {
2368
2398
  this.data = opts.data;
2369
2399
  }
2370
2400
  }
2401
+ /**
2402
+ * @internal
2403
+ */
2404
+
2405
+
2406
+ toJSON() {
2407
+ return {
2408
+ keys: this.keys.map(({
2409
+ pubkey,
2410
+ isSigner,
2411
+ isWritable
2412
+ }) => ({
2413
+ pubkey: pubkey.toJSON(),
2414
+ isSigner,
2415
+ isWritable
2416
+ })),
2417
+ programId: this.programId.toJSON(),
2418
+ data: [...this.data]
2419
+ };
2420
+ }
2371
2421
 
2372
2422
  }
2373
2423
  /**
@@ -2407,8 +2457,33 @@ class Transaction {
2407
2457
  this.instructions = [];
2408
2458
  this.recentBlockhash = void 0;
2409
2459
  this.nonceInfo = void 0;
2460
+ this._message = void 0;
2461
+ this._json = void 0;
2410
2462
  opts && Object.assign(this, opts);
2411
2463
  }
2464
+ /**
2465
+ * @internal
2466
+ */
2467
+
2468
+
2469
+ toJSON() {
2470
+ return {
2471
+ recentBlockhash: this.recentBlockhash || null,
2472
+ feePayer: this.feePayer ? this.feePayer.toJSON() : null,
2473
+ nonceInfo: this.nonceInfo ? {
2474
+ nonce: this.nonceInfo.nonce,
2475
+ nonceInstruction: this.nonceInfo.nonceInstruction.toJSON()
2476
+ } : null,
2477
+ instructions: this.instructions.map(instruction => instruction.toJSON()),
2478
+ signatures: this.signatures.map(({
2479
+ publicKey,
2480
+ signature
2481
+ }) => ({
2482
+ publicKey: publicKey.toJSON(),
2483
+ signature: signature ? [...signature] : null
2484
+ }))
2485
+ };
2486
+ }
2412
2487
  /**
2413
2488
  * Add one or more instructions to this Transaction
2414
2489
  */
@@ -2436,6 +2511,14 @@ class Transaction {
2436
2511
 
2437
2512
 
2438
2513
  compileMessage() {
2514
+ if (this._message) {
2515
+ if (JSON.stringify(this.toJSON()) !== JSON.stringify(this._json)) {
2516
+ throw new Error('Transaction mutated after being populated from Message');
2517
+ }
2518
+
2519
+ return this._message;
2520
+ }
2521
+
2439
2522
  const {
2440
2523
  nonceInfo
2441
2524
  } = this;
@@ -2915,8 +2998,7 @@ class Transaction {
2915
2998
  let signatures = [];
2916
2999
 
2917
3000
  for (let i = 0; i < signatureCount; i++) {
2918
- const signature = byteArray.slice(0, SIGNATURE_LENGTH);
2919
- byteArray = byteArray.slice(SIGNATURE_LENGTH);
3001
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH);
2920
3002
  signatures.push(bs58.encode(Buffer.from(signature)));
2921
3003
  }
2922
3004
 
@@ -2957,6 +3039,8 @@ class Transaction {
2957
3039
  data: bs58.decode(instruction.data)
2958
3040
  }));
2959
3041
  });
3042
+ transaction._message = message;
3043
+ transaction._json = transaction.toJSON();
2960
3044
  return transaction;
2961
3045
  }
2962
3046
 
@@ -6733,7 +6817,9 @@ class Connection {
6733
6817
  });
6734
6818
  transaction.instructions = transactionOrMessage.instructions;
6735
6819
  } else {
6736
- transaction = Transaction.populate(transactionOrMessage);
6820
+ transaction = Transaction.populate(transactionOrMessage); // HACK: this function relies on mutating the populated transaction
6821
+
6822
+ transaction._message = transaction._json = undefined;
6737
6823
  }
6738
6824
 
6739
6825
  if (transaction.nonceInfo && signers) {
@@ -8756,10 +8842,8 @@ class ValidatorInfo {
8756
8842
  const configKeys = [];
8757
8843
 
8758
8844
  for (let i = 0; i < 2; i++) {
8759
- const publicKey = new PublicKey(byteArray.slice(0, PUBKEY_LENGTH));
8760
- byteArray = byteArray.slice(PUBKEY_LENGTH);
8761
- const isSigner = byteArray.slice(0, 1)[0] === 1;
8762
- byteArray = byteArray.slice(1);
8845
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBKEY_LENGTH));
8846
+ const isSigner = guardedShift(byteArray) === 1;
8763
8847
  configKeys.push({
8764
8848
  publicKey,
8765
8849
  isSigner