@solana/web3.js 1.50.1 → 1.50.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solana/web3.js",
3
- "version": "1.50.1",
3
+ "version": "1.50.2",
4
4
  "description": "Solana Javascript API",
5
5
  "keywords": [
6
6
  "api",
@@ -36,26 +36,6 @@
36
36
  "/lib",
37
37
  "/src"
38
38
  ],
39
- "scripts": {
40
- "build": "npm run clean; cross-env NODE_ENV=production rollup -c; npm run type:gen",
41
- "build:fixtures": "set -ex; ./test/fixtures/noop-program/build.sh",
42
- "clean": "rimraf ./coverage ./lib",
43
- "codecov": "set -ex; npm run test:cover; cat ./coverage/lcov.info | codecov",
44
- "dev": "cross-env NODE_ENV=development rollup -c",
45
- "doc": "set -ex; typedoc --treatWarningsAsErrors",
46
- "type:gen": "./scripts/typegen.sh",
47
- "lint": "set -ex; npm run pretty; eslint . --ext .js,.ts",
48
- "lint:fix": "npm run pretty:fix && eslint . --fix --ext .js,.ts",
49
- "type:check": "tsc -p tsconfig.json --noEmit",
50
- "ok": "run-s lint test doc type:check",
51
- "pretty": "prettier --check '{,{src,test}/**/}*.{j,t}s'",
52
- "pretty:fix": "prettier --write '{,{src,test}/**/}*.{j,t}s'",
53
- "re": "semantic-release --repository-url git@github.com:solana-labs/solana-web3.js.git",
54
- "test": "cross-env TS_NODE_COMPILER_OPTIONS='{ \"module\": \"commonjs\", \"target\": \"es2019\" }' ts-mocha --require esm './test/**/*.test.ts'",
55
- "test:cover": "nyc --reporter=lcov npm run test",
56
- "test:live": "TEST_LIVE=1 npm run test",
57
- "test:live-with-test-validator": "start-server-and-test 'solana-test-validator --reset --quiet' http://localhost:8899/health test:live"
58
- },
59
39
  "dependencies": {
60
40
  "@babel/runtime": "^7.12.5",
61
41
  "@ethersproject/sha2": "^5.5.0",
@@ -140,5 +120,25 @@
140
120
  },
141
121
  "engines": {
142
122
  "node": ">=12.20.0"
123
+ },
124
+ "scripts": {
125
+ "build": "npm run clean; cross-env NODE_ENV=production rollup -c; npm run type:gen",
126
+ "build:fixtures": "set -ex; ./test/fixtures/noop-program/build.sh",
127
+ "clean": "rimraf ./coverage ./lib",
128
+ "codecov": "set -ex; npm run test:cover; cat ./coverage/lcov.info | codecov",
129
+ "dev": "cross-env NODE_ENV=development rollup -c",
130
+ "doc": "set -ex; typedoc --treatWarningsAsErrors",
131
+ "type:gen": "./scripts/typegen.sh",
132
+ "lint": "set -ex; npm run pretty; eslint . --ext .js,.ts",
133
+ "lint:fix": "npm run pretty:fix && eslint . --fix --ext .js,.ts",
134
+ "type:check": "tsc -p tsconfig.json --noEmit",
135
+ "ok": "run-s lint test doc type:check",
136
+ "pretty": "prettier --check '{,{src,test}/**/}*.{j,t}s'",
137
+ "pretty:fix": "prettier --write '{,{src,test}/**/}*.{j,t}s'",
138
+ "re": "semantic-release --repository-url git@github.com:solana-labs/solana-web3.js.git",
139
+ "test": "cross-env TS_NODE_COMPILER_OPTIONS='{ \"module\": \"commonjs\", \"target\": \"es2019\" }' ts-mocha --require esm './test/**/*.test.ts'",
140
+ "test:cover": "nyc --reporter=lcov npm run test",
141
+ "test:live": "TEST_LIVE=1 npm run test",
142
+ "test:live-with-test-validator": "start-server-and-test 'solana-test-validator --reset --quiet' http://localhost:8899/health test:live"
143
143
  }
144
- }
144
+ }
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
@@ -808,8 +809,7 @@ export class Transaction {
808
809
  const signatureCount = shortvec.decodeLength(byteArray);
809
810
  let signatures = [];
810
811
  for (let i = 0; i < signatureCount; i++) {
811
- const signature = byteArray.slice(0, SIGNATURE_LENGTH_IN_BYTES);
812
- byteArray = byteArray.slice(SIGNATURE_LENGTH_IN_BYTES);
812
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH_IN_BYTES);
813
813
  signatures.push(bs58.encode(Buffer.from(signature)));
814
814
  }
815
815
 
@@ -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