@solana/web3.js 1.10.0 → 1.10.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.
@@ -127,7 +127,7 @@ class PublicKey {
127
127
  let buffer = Buffer.alloc(0);
128
128
  seeds.forEach(function (seed) {
129
129
  if (seed.length > MAX_SEED_LENGTH) {
130
- throw new Error("Max seed length exceeded");
130
+ throw new TypeError("Max seed length exceeded");
131
131
  }
132
132
 
133
133
  buffer = Buffer.concat([buffer, toBuffer(seed)]);
@@ -160,6 +160,10 @@ class PublicKey {
160
160
  const seedsWithNonce = seeds.concat(Buffer.from([nonce]));
161
161
  address = await this.createProgramAddress(seedsWithNonce, programId);
162
162
  } catch (err) {
163
+ if (err instanceof TypeError) {
164
+ throw err;
165
+ }
166
+
163
167
  nonce--;
164
168
  continue;
165
169
  }
@@ -1266,6 +1270,36 @@ function encodeLength(bytes, len) {
1266
1270
  }
1267
1271
  }
1268
1272
 
1273
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
1274
+ /**
1275
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
1276
+ */
1277
+
1278
+ function guardedShift(byteArray) {
1279
+ if (byteArray.length === 0) {
1280
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
1281
+ }
1282
+
1283
+ return byteArray.shift();
1284
+ }
1285
+ /**
1286
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
1287
+ * the array.
1288
+ */
1289
+
1290
+ function guardedSplice(byteArray, ...args) {
1291
+ var _args$;
1292
+
1293
+ const [start] = args;
1294
+
1295
+ if (args.length === 2 // Implies that `deleteCount` was supplied
1296
+ ? start + ((_args$ = args[1]) !== null && _args$ !== void 0 ? _args$ : 0) > byteArray.length : start >= byteArray.length) {
1297
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
1298
+ }
1299
+
1300
+ return byteArray.splice(...args);
1301
+ }
1302
+
1269
1303
  /**
1270
1304
  * The message header, identifying signed and read-only account
1271
1305
  */
@@ -1342,32 +1376,28 @@ class Message {
1342
1376
  static from(buffer) {
1343
1377
  // Slice up wire data
1344
1378
  let byteArray = [...buffer];
1345
- const numRequiredSignatures = byteArray.shift();
1346
- const numReadonlySignedAccounts = byteArray.shift();
1347
- const numReadonlyUnsignedAccounts = byteArray.shift();
1379
+ const numRequiredSignatures = guardedShift(byteArray);
1380
+ const numReadonlySignedAccounts = guardedShift(byteArray);
1381
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
1348
1382
  const accountCount = decodeLength(byteArray);
1349
1383
  let accountKeys = [];
1350
1384
 
1351
1385
  for (let i = 0; i < accountCount; i++) {
1352
- const account = byteArray.slice(0, PUBKEY_LENGTH);
1353
- byteArray = byteArray.slice(PUBKEY_LENGTH);
1386
+ const account = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
1354
1387
  accountKeys.push(bs58.encode(Buffer.from(account)));
1355
1388
  }
1356
1389
 
1357
- const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
1358
- byteArray = byteArray.slice(PUBKEY_LENGTH);
1390
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
1359
1391
  const instructionCount = decodeLength(byteArray);
1360
1392
  let instructions = [];
1361
1393
 
1362
1394
  for (let i = 0; i < instructionCount; i++) {
1363
- const programIdIndex = byteArray.shift();
1395
+ const programIdIndex = guardedShift(byteArray);
1364
1396
  const accountCount = decodeLength(byteArray);
1365
- const accounts = byteArray.slice(0, accountCount);
1366
- byteArray = byteArray.slice(accountCount);
1397
+ const accounts = guardedSplice(byteArray, 0, accountCount);
1367
1398
  const dataLength = decodeLength(byteArray);
1368
- const dataSlice = byteArray.slice(0, dataLength);
1399
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
1369
1400
  const data = bs58.encode(Buffer.from(dataSlice));
1370
- byteArray = byteArray.slice(dataLength);
1371
1401
  instructions.push({
1372
1402
  programIdIndex,
1373
1403
  accounts,
@@ -1390,6 +1420,10 @@ class Message {
1390
1420
 
1391
1421
  }
1392
1422
 
1423
+ /**
1424
+ * Transaction signature as base-58 encoded string
1425
+ */
1426
+
1393
1427
  /**
1394
1428
  * Default (empty) signature
1395
1429
  *
@@ -1973,8 +2007,7 @@ class Transaction {
1973
2007
  let signatures = [];
1974
2008
 
1975
2009
  for (let i = 0; i < signatureCount; i++) {
1976
- const signature = byteArray.slice(0, SIGNATURE_LENGTH);
1977
- byteArray = byteArray.slice(SIGNATURE_LENGTH);
2010
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH);
1978
2011
  signatures.push(bs58.encode(Buffer.from(signature)));
1979
2012
  }
1980
2013
 
@@ -8057,10 +8090,8 @@ class ValidatorInfo {
8057
8090
  const configKeys = [];
8058
8091
 
8059
8092
  for (let i = 0; i < 2; i++) {
8060
- const publicKey = new PublicKey(byteArray.slice(0, PUBKEY_LENGTH));
8061
- byteArray = byteArray.slice(PUBKEY_LENGTH);
8062
- const isSigner = byteArray.slice(0, 1)[0] === 1;
8063
- byteArray = byteArray.slice(1);
8093
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBKEY_LENGTH));
8094
+ const isSigner = guardedShift(byteArray) === 1;
8064
8095
  configKeys.push({
8065
8096
  publicKey,
8066
8097
  isSigner