@solana/web3.js 1.9.0 → 1.9.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/README.md CHANGED
@@ -69,7 +69,7 @@ console.log(solanaWeb3);
69
69
 
70
70
  ### ES6
71
71
  ```js
72
- import solanaWeb3 from '@solana/web3.js';
72
+ import * as solanaWeb3 from '@solana/web3.js';
73
73
  console.log(solanaWeb3);
74
74
  ```
75
75
 
@@ -1264,6 +1264,36 @@ function encodeLength(bytes, len) {
1264
1264
  }
1265
1265
  }
1266
1266
 
1267
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
1268
+ /**
1269
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
1270
+ */
1271
+
1272
+ function guardedShift(byteArray) {
1273
+ if (byteArray.length === 0) {
1274
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
1275
+ }
1276
+
1277
+ return byteArray.shift();
1278
+ }
1279
+ /**
1280
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
1281
+ * the array.
1282
+ */
1283
+
1284
+ function guardedSplice(byteArray, ...args) {
1285
+ var _args$;
1286
+
1287
+ const [start] = args;
1288
+
1289
+ if (args.length === 2 // Implies that `deleteCount` was supplied
1290
+ ? start + ((_args$ = args[1]) !== null && _args$ !== void 0 ? _args$ : 0) > byteArray.length : start >= byteArray.length) {
1291
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
1292
+ }
1293
+
1294
+ return byteArray.splice(...args);
1295
+ }
1296
+
1267
1297
  /**
1268
1298
  * The message header, identifying signed and read-only account
1269
1299
  */
@@ -1340,32 +1370,28 @@ class Message {
1340
1370
  static from(buffer) {
1341
1371
  // Slice up wire data
1342
1372
  let byteArray = [...buffer];
1343
- const numRequiredSignatures = byteArray.shift();
1344
- const numReadonlySignedAccounts = byteArray.shift();
1345
- const numReadonlyUnsignedAccounts = byteArray.shift();
1373
+ const numRequiredSignatures = guardedShift(byteArray);
1374
+ const numReadonlySignedAccounts = guardedShift(byteArray);
1375
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
1346
1376
  const accountCount = decodeLength(byteArray);
1347
1377
  let accountKeys = [];
1348
1378
 
1349
1379
  for (let i = 0; i < accountCount; i++) {
1350
- const account = byteArray.slice(0, PUBKEY_LENGTH);
1351
- byteArray = byteArray.slice(PUBKEY_LENGTH);
1380
+ const account = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
1352
1381
  accountKeys.push(bs58.encode(Buffer.from(account)));
1353
1382
  }
1354
1383
 
1355
- const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
1356
- byteArray = byteArray.slice(PUBKEY_LENGTH);
1384
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
1357
1385
  const instructionCount = decodeLength(byteArray);
1358
1386
  let instructions = [];
1359
1387
 
1360
1388
  for (let i = 0; i < instructionCount; i++) {
1361
- const programIdIndex = byteArray.shift();
1389
+ const programIdIndex = guardedShift(byteArray);
1362
1390
  const accountCount = decodeLength(byteArray);
1363
- const accounts = byteArray.slice(0, accountCount);
1364
- byteArray = byteArray.slice(accountCount);
1391
+ const accounts = guardedSplice(byteArray, 0, accountCount);
1365
1392
  const dataLength = decodeLength(byteArray);
1366
- const dataSlice = byteArray.slice(0, dataLength);
1393
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
1367
1394
  const data = bs58.encode(Buffer.from(dataSlice));
1368
- byteArray = byteArray.slice(dataLength);
1369
1395
  instructions.push({
1370
1396
  programIdIndex,
1371
1397
  accounts,
@@ -1975,8 +2001,7 @@ class Transaction {
1975
2001
  let signatures = [];
1976
2002
 
1977
2003
  for (let i = 0; i < signatureCount; i++) {
1978
- const signature = byteArray.slice(0, SIGNATURE_LENGTH);
1979
- byteArray = byteArray.slice(SIGNATURE_LENGTH);
2004
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH);
1980
2005
  signatures.push(bs58.encode(Buffer.from(signature)));
1981
2006
  }
1982
2007
 
@@ -7975,10 +8000,8 @@ class ValidatorInfo {
7975
8000
  const configKeys = [];
7976
8001
 
7977
8002
  for (let i = 0; i < 2; i++) {
7978
- const publicKey = new PublicKey(byteArray.slice(0, PUBKEY_LENGTH));
7979
- byteArray = byteArray.slice(PUBKEY_LENGTH);
7980
- const isSigner = byteArray.slice(0, 1)[0] === 1;
7981
- byteArray = byteArray.slice(1);
8003
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBKEY_LENGTH));
8004
+ const isSigner = guardedShift(byteArray) === 1;
7982
8005
  configKeys.push({
7983
8006
  publicKey,
7984
8007
  isSigner