@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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solana/web3.js",
3
- "version": "1.44.2",
3
+ "version": "1.44.4",
4
4
  "description": "Solana Javascript API",
5
5
  "keywords": [
6
6
  "api",
@@ -35,26 +35,6 @@
35
35
  "/lib",
36
36
  "/src"
37
37
  ],
38
- "scripts": {
39
- "build": "npm run clean; cross-env NODE_ENV=production rollup -c; npm run type:gen",
40
- "build:fixtures": "set -ex; ./test/fixtures/noop-program/build.sh",
41
- "clean": "rimraf ./coverage ./lib",
42
- "codecov": "set -ex; npm run test:cover; cat ./coverage/lcov.info | codecov",
43
- "dev": "cross-env NODE_ENV=development rollup -c",
44
- "doc": "set -ex; typedoc --treatWarningsAsErrors",
45
- "type:gen": "./scripts/typegen.sh",
46
- "lint": "set -ex; npm run pretty; eslint . --ext .js,.ts",
47
- "lint:fix": "npm run pretty:fix && eslint . --fix --ext .js,.ts",
48
- "type:check": "tsc -p tsconfig.json --noEmit",
49
- "ok": "run-s lint test doc type:check",
50
- "pretty": "prettier --check '{,{src,test}/**/}*.{j,t}s'",
51
- "pretty:fix": "prettier --write '{,{src,test}/**/}*.{j,t}s'",
52
- "re": "semantic-release --repository-url git@github.com:solana-labs/solana-web3.js.git",
53
- "test": "cross-env TS_NODE_COMPILER_OPTIONS='{ \"module\": \"commonjs\" }' ts-mocha --require esm './test/**/*.test.ts'",
54
- "test:cover": "nyc --reporter=lcov npm run test",
55
- "test:live": "TEST_LIVE=1 npm run test",
56
- "test:live-with-test-validator": "start-server-and-test 'solana-test-validator --reset --quiet' http://localhost:8899/health test:live"
57
- },
58
38
  "dependencies": {
59
39
  "@babel/runtime": "^7.12.5",
60
40
  "@ethersproject/sha2": "^5.5.0",
@@ -138,5 +118,25 @@
138
118
  },
139
119
  "engines": {
140
120
  "node": ">=12.20.0"
121
+ },
122
+ "scripts": {
123
+ "build": "npm run clean; cross-env NODE_ENV=production rollup -c; npm run type:gen",
124
+ "build:fixtures": "set -ex; ./test/fixtures/noop-program/build.sh",
125
+ "clean": "rimraf ./coverage ./lib",
126
+ "codecov": "set -ex; npm run test:cover; cat ./coverage/lcov.info | codecov",
127
+ "dev": "cross-env NODE_ENV=development rollup -c",
128
+ "doc": "set -ex; typedoc --treatWarningsAsErrors",
129
+ "type:gen": "./scripts/typegen.sh",
130
+ "lint": "set -ex; npm run pretty; eslint . --ext .js,.ts",
131
+ "lint:fix": "npm run pretty:fix && eslint . --fix --ext .js,.ts",
132
+ "type:check": "tsc -p tsconfig.json --noEmit",
133
+ "ok": "run-s lint test doc type:check",
134
+ "pretty": "prettier --check '{,{src,test}/**/}*.{j,t}s'",
135
+ "pretty:fix": "prettier --write '{,{src,test}/**/}*.{j,t}s'",
136
+ "re": "semantic-release --repository-url git@github.com:solana-labs/solana-web3.js.git",
137
+ "test": "cross-env TS_NODE_COMPILER_OPTIONS='{ \"module\": \"commonjs\" }' ts-mocha --require esm './test/**/*.test.ts'",
138
+ "test:cover": "nyc --reporter=lcov npm run test",
139
+ "test:live": "TEST_LIVE=1 npm run test",
140
+ "test:live-with-test-validator": "start-server-and-test 'solana-test-validator --reset --quiet' http://localhost:8899/health test:live"
141
141
  }
142
- }
142
+ }
package/src/connection.ts CHANGED
@@ -191,9 +191,9 @@ type Subscription = BaseSubscription &
191
191
  StatefulSubscription &
192
192
  DistributiveOmit<SubscriptionConfig, 'callback'>;
193
193
 
194
- type RpcRequest = (methodName: string, args: Array<any>) => any;
194
+ type RpcRequest = (methodName: string, args: Array<any>) => Promise<any>;
195
195
 
196
- type RpcBatchRequest = (requests: RpcParams[]) => any;
196
+ type RpcBatchRequest = (requests: RpcParams[]) => Promise<any[]>;
197
197
 
198
198
  /**
199
199
  * @internal
package/src/message.ts CHANGED
@@ -8,6 +8,7 @@ import * as Layout from './layout';
8
8
  import {PACKET_DATA_SIZE} from './transaction-constants';
9
9
  import * as shortvec from './util/shortvec-encoding';
10
10
  import {toBuffer} from './util/to-buffer';
11
+ import {guardedShift, guardedSplice} from './util/guarded-array-utils';
11
12
 
12
13
  /**
13
14
  * The message header, identifying signed and read-only account
@@ -222,32 +223,28 @@ export class Message {
222
223
  // Slice up wire data
223
224
  let byteArray = [...buffer];
224
225
 
225
- const numRequiredSignatures = byteArray.shift() as number;
226
- const numReadonlySignedAccounts = byteArray.shift() as number;
227
- const numReadonlyUnsignedAccounts = byteArray.shift() as number;
226
+ const numRequiredSignatures = guardedShift(byteArray);
227
+ const numReadonlySignedAccounts = guardedShift(byteArray);
228
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
228
229
 
229
230
  const accountCount = shortvec.decodeLength(byteArray);
230
231
  let accountKeys = [];
231
232
  for (let i = 0; i < accountCount; i++) {
232
- const account = byteArray.slice(0, PUBKEY_LENGTH);
233
- byteArray = byteArray.slice(PUBKEY_LENGTH);
233
+ const account = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
234
234
  accountKeys.push(bs58.encode(Buffer.from(account)));
235
235
  }
236
236
 
237
- const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
238
- byteArray = byteArray.slice(PUBKEY_LENGTH);
237
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
239
238
 
240
239
  const instructionCount = shortvec.decodeLength(byteArray);
241
240
  let instructions: CompiledInstruction[] = [];
242
241
  for (let i = 0; i < instructionCount; i++) {
243
- const programIdIndex = byteArray.shift() as number;
242
+ const programIdIndex = guardedShift(byteArray);
244
243
  const accountCount = shortvec.decodeLength(byteArray);
245
- const accounts = byteArray.slice(0, accountCount);
246
- byteArray = byteArray.slice(accountCount);
244
+ const accounts = guardedSplice(byteArray, 0, accountCount);
247
245
  const dataLength = shortvec.decodeLength(byteArray);
248
- const dataSlice = byteArray.slice(0, dataLength);
246
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
249
247
  const data = bs58.encode(Buffer.from(dataSlice));
250
- byteArray = byteArray.slice(dataLength);
251
248
  instructions.push({
252
249
  programIdIndex,
253
250
  accounts,
@@ -15,6 +15,7 @@ import invariant from './util/assert';
15
15
  import type {Signer} from './keypair';
16
16
  import type {Blockhash} from './blockhash';
17
17
  import type {CompiledInstruction} from './message';
18
+ import {guardedSplice} from './util/guarded-array-utils';
18
19
 
19
20
  /**
20
21
  * Transaction signature as base-58 encoded string
@@ -327,17 +328,24 @@ export class Transaction {
327
328
  return this._message;
328
329
  }
329
330
 
330
- const {nonceInfo} = this;
331
- if (nonceInfo && this.instructions[0] != nonceInfo.nonceInstruction) {
332
- this.recentBlockhash = nonceInfo.nonce;
333
- this.instructions.unshift(nonceInfo.nonceInstruction);
331
+ let recentBlockhash;
332
+ let instructions: TransactionInstruction[];
333
+ if (this.nonceInfo) {
334
+ recentBlockhash = this.nonceInfo.nonce;
335
+ if (this.instructions[0] != this.nonceInfo.nonceInstruction) {
336
+ instructions = [this.nonceInfo.nonceInstruction, ...this.instructions];
337
+ } else {
338
+ instructions = this.instructions;
339
+ }
340
+ } else {
341
+ recentBlockhash = this.recentBlockhash;
342
+ instructions = this.instructions;
334
343
  }
335
- const {recentBlockhash} = this;
336
344
  if (!recentBlockhash) {
337
345
  throw new Error('Transaction recentBlockhash required');
338
346
  }
339
347
 
340
- if (this.instructions.length < 1) {
348
+ if (instructions.length < 1) {
341
349
  console.warn('No instructions provided');
342
350
  }
343
351
 
@@ -351,8 +359,8 @@ export class Transaction {
351
359
  throw new Error('Transaction fee payer required');
352
360
  }
353
361
 
354
- for (let i = 0; i < this.instructions.length; i++) {
355
- if (this.instructions[i].programId === undefined) {
362
+ for (let i = 0; i < instructions.length; i++) {
363
+ if (instructions[i].programId === undefined) {
356
364
  throw new Error(
357
365
  `Transaction instruction index ${i} has undefined program id`,
358
366
  );
@@ -361,7 +369,7 @@ export class Transaction {
361
369
 
362
370
  const programIds: string[] = [];
363
371
  const accountMetas: AccountMeta[] = [];
364
- this.instructions.forEach(instruction => {
372
+ instructions.forEach(instruction => {
365
373
  instruction.keys.forEach(accountMeta => {
366
374
  accountMetas.push({...accountMeta});
367
375
  });
@@ -471,7 +479,7 @@ export class Transaction {
471
479
  });
472
480
 
473
481
  const accountKeys = signedKeys.concat(unsignedKeys);
474
- const instructions: CompiledInstruction[] = this.instructions.map(
482
+ const compiledInstructions: CompiledInstruction[] = instructions.map(
475
483
  instruction => {
476
484
  const {data, programId} = instruction;
477
485
  return {
@@ -484,7 +492,7 @@ export class Transaction {
484
492
  },
485
493
  );
486
494
 
487
- instructions.forEach(instruction => {
495
+ compiledInstructions.forEach(instruction => {
488
496
  invariant(instruction.programIdIndex >= 0);
489
497
  instruction.accounts.forEach(keyIndex => invariant(keyIndex >= 0));
490
498
  });
@@ -497,7 +505,7 @@ export class Transaction {
497
505
  },
498
506
  accountKeys,
499
507
  recentBlockhash,
500
- instructions,
508
+ instructions: compiledInstructions,
501
509
  });
502
510
  }
503
511
 
@@ -793,8 +801,7 @@ export class Transaction {
793
801
  const signatureCount = shortvec.decodeLength(byteArray);
794
802
  let signatures = [];
795
803
  for (let i = 0; i < signatureCount; i++) {
796
- const signature = byteArray.slice(0, SIGNATURE_LENGTH_IN_BYTES);
797
- byteArray = byteArray.slice(SIGNATURE_LENGTH_IN_BYTES);
804
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH_IN_BYTES);
798
805
  signatures.push(bs58.encode(Buffer.from(signature)));
799
806
  }
800
807
 
@@ -0,0 +1,34 @@
1
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
2
+
3
+ /**
4
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
5
+ */
6
+ export function guardedShift<T>(byteArray: T[]): T {
7
+ if (byteArray.length === 0) {
8
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
9
+ }
10
+ return byteArray.shift() as T;
11
+ }
12
+
13
+ /**
14
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
15
+ * the array.
16
+ */
17
+ export function guardedSplice<T>(
18
+ byteArray: T[],
19
+ ...args:
20
+ | [start: number, deleteCount?: number]
21
+ | [start: number, deleteCount: number, ...items: T[]]
22
+ ): T[] {
23
+ const [start] = args;
24
+ if (
25
+ args.length === 2 // Implies that `deleteCount` was supplied
26
+ ? start + (args[1] ?? 0) > byteArray.length
27
+ : start >= byteArray.length
28
+ ) {
29
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
30
+ }
31
+ return byteArray.splice(
32
+ ...(args as Parameters<typeof Array.prototype.splice>),
33
+ );
34
+ }
@@ -9,6 +9,7 @@ import {
9
9
  import * as Layout from './layout';
10
10
  import * as shortvec from './util/shortvec-encoding';
11
11
  import {PublicKey} from './publickey';
12
+ import {guardedShift, guardedSplice} from './util/guarded-array-utils';
12
13
 
13
14
  export const VALIDATOR_INFO_KEY = new PublicKey(
14
15
  'Va1idator1nfo111111111111111111111111111111',
@@ -85,10 +86,10 @@ export class ValidatorInfo {
85
86
 
86
87
  const configKeys: Array<ConfigKey> = [];
87
88
  for (let i = 0; i < 2; i++) {
88
- const publicKey = new PublicKey(byteArray.slice(0, PUBKEY_LENGTH));
89
- byteArray = byteArray.slice(PUBKEY_LENGTH);
90
- const isSigner = byteArray.slice(0, 1)[0] === 1;
91
- byteArray = byteArray.slice(1);
89
+ const publicKey = new PublicKey(
90
+ guardedSplice(byteArray, 0, PUBKEY_LENGTH),
91
+ );
92
+ const isSigner = guardedShift(byteArray) === 1;
92
93
  configKeys.push({publicKey, isSigner});
93
94
  }
94
95