@solana/web3.js 1.20.1 → 1.20.3

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.cjs.js CHANGED
@@ -10,8 +10,6 @@ var bs58 = require('bs58');
10
10
  var cryptoHash = require('crypto-hash');
11
11
  var borsh = require('borsh');
12
12
  var BufferLayout = require('@solana/buffer-layout');
13
- var invariant = require('assert');
14
- var url = require('url');
15
13
  var fetch = require('node-fetch');
16
14
  var superstruct = require('superstruct');
17
15
  var rpcWebsockets = require('rpc-websockets');
@@ -49,7 +47,6 @@ var nacl__namespace = /*#__PURE__*/_interopNamespace(nacl);
49
47
  var BN__default = /*#__PURE__*/_interopDefaultLegacy(BN);
50
48
  var bs58__default = /*#__PURE__*/_interopDefaultLegacy(bs58);
51
49
  var BufferLayout__namespace = /*#__PURE__*/_interopNamespace(BufferLayout);
52
- var invariant__default = /*#__PURE__*/_interopDefaultLegacy(invariant);
53
50
  var fetch__default = /*#__PURE__*/_interopDefaultLegacy(fetch);
54
51
  var RpcClient__default = /*#__PURE__*/_interopDefaultLegacy(RpcClient);
55
52
  var http__default = /*#__PURE__*/_interopDefaultLegacy(http);
@@ -479,6 +476,36 @@ function encodeLength(bytes, len) {
479
476
  }
480
477
  }
481
478
 
479
+ const END_OF_BUFFER_ERROR_MESSAGE = 'Reached end of buffer unexpectedly';
480
+ /**
481
+ * Delegates to `Array#shift`, but throws if the array is zero-length.
482
+ */
483
+
484
+ function guardedShift(byteArray) {
485
+ if (byteArray.length === 0) {
486
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
487
+ }
488
+
489
+ return byteArray.shift();
490
+ }
491
+ /**
492
+ * Delegates to `Array#splice`, but throws if the section being spliced out extends past the end of
493
+ * the array.
494
+ */
495
+
496
+ function guardedSplice(byteArray, ...args) {
497
+ var _args$;
498
+
499
+ const [start] = args;
500
+
501
+ if (args.length === 2 // Implies that `deleteCount` was supplied
502
+ ? start + ((_args$ = args[1]) !== null && _args$ !== void 0 ? _args$ : 0) > byteArray.length : start >= byteArray.length) {
503
+ throw new Error(END_OF_BUFFER_ERROR_MESSAGE);
504
+ }
505
+
506
+ return byteArray.splice(...args);
507
+ }
508
+
482
509
  /**
483
510
  * The message header, identifying signed and read-only account
484
511
  */
@@ -563,32 +590,28 @@ class Message {
563
590
  static from(buffer$1) {
564
591
  // Slice up wire data
565
592
  let byteArray = [...buffer$1];
566
- const numRequiredSignatures = byteArray.shift();
567
- const numReadonlySignedAccounts = byteArray.shift();
568
- const numReadonlyUnsignedAccounts = byteArray.shift();
593
+ const numRequiredSignatures = guardedShift(byteArray);
594
+ const numReadonlySignedAccounts = guardedShift(byteArray);
595
+ const numReadonlyUnsignedAccounts = guardedShift(byteArray);
569
596
  const accountCount = decodeLength(byteArray);
570
597
  let accountKeys = [];
571
598
 
572
599
  for (let i = 0; i < accountCount; i++) {
573
- const account = byteArray.slice(0, PUBKEY_LENGTH);
574
- byteArray = byteArray.slice(PUBKEY_LENGTH);
600
+ const account = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
575
601
  accountKeys.push(bs58__default['default'].encode(buffer.Buffer.from(account)));
576
602
  }
577
603
 
578
- const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
579
- byteArray = byteArray.slice(PUBKEY_LENGTH);
604
+ const recentBlockhash = guardedSplice(byteArray, 0, PUBKEY_LENGTH);
580
605
  const instructionCount = decodeLength(byteArray);
581
606
  let instructions = [];
582
607
 
583
608
  for (let i = 0; i < instructionCount; i++) {
584
- const programIdIndex = byteArray.shift();
609
+ const programIdIndex = guardedShift(byteArray);
585
610
  const accountCount = decodeLength(byteArray);
586
- const accounts = byteArray.slice(0, accountCount);
587
- byteArray = byteArray.slice(accountCount);
611
+ const accounts = guardedSplice(byteArray, 0, accountCount);
588
612
  const dataLength = decodeLength(byteArray);
589
- const dataSlice = byteArray.slice(0, dataLength);
613
+ const dataSlice = guardedSplice(byteArray, 0, dataLength);
590
614
  const data = bs58__default['default'].encode(buffer.Buffer.from(dataSlice));
591
- byteArray = byteArray.slice(dataLength);
592
615
  instructions.push({
593
616
  programIdIndex,
594
617
  accounts,
@@ -611,6 +634,16 @@ class Message {
611
634
 
612
635
  }
613
636
 
637
+ function assert (condition, message) {
638
+ if (!condition) {
639
+ throw new Error(message || 'Assertion failed');
640
+ }
641
+ }
642
+
643
+ /**
644
+ * Transaction signature as base-58 encoded string
645
+ */
646
+
614
647
  /**
615
648
  * Default (empty) signature
616
649
  *
@@ -886,8 +919,8 @@ class Transaction {
886
919
  };
887
920
  });
888
921
  instructions.forEach(instruction => {
889
- invariant__default['default'](instruction.programIdIndex >= 0);
890
- instruction.accounts.forEach(keyIndex => invariant__default['default'](keyIndex >= 0));
922
+ assert(instruction.programIdIndex >= 0);
923
+ instruction.accounts.forEach(keyIndex => assert(keyIndex >= 0));
891
924
  });
892
925
  return new Message({
893
926
  header: {
@@ -1074,7 +1107,7 @@ class Transaction {
1074
1107
 
1075
1108
 
1076
1109
  _addSignature(pubkey, signature) {
1077
- invariant__default['default'](signature.length === 64);
1110
+ assert(signature.length === 64);
1078
1111
  const index = this.signatures.findIndex(sigpair => pubkey.equals(sigpair.publicKey));
1079
1112
 
1080
1113
  if (index < 0) {
@@ -1148,18 +1181,18 @@ class Transaction {
1148
1181
  encodeLength(signatureCount, signatures.length);
1149
1182
  const transactionLength = signatureCount.length + signatures.length * 64 + signData.length;
1150
1183
  const wireTransaction = buffer.Buffer.alloc(transactionLength);
1151
- invariant__default['default'](signatures.length < 256);
1184
+ assert(signatures.length < 256);
1152
1185
  buffer.Buffer.from(signatureCount).copy(wireTransaction, 0);
1153
1186
  signatures.forEach(({
1154
1187
  signature
1155
1188
  }, index) => {
1156
1189
  if (signature !== null) {
1157
- invariant__default['default'](signature.length === 64, `signature has invalid length`);
1190
+ assert(signature.length === 64, `signature has invalid length`);
1158
1191
  buffer.Buffer.from(signature).copy(wireTransaction, signatureCount.length + index * 64);
1159
1192
  }
1160
1193
  });
1161
1194
  signData.copy(wireTransaction, signatureCount.length + signatures.length * 64);
1162
- invariant__default['default'](wireTransaction.length <= PACKET_DATA_SIZE, `Transaction too large: ${wireTransaction.length} > ${PACKET_DATA_SIZE}`);
1195
+ assert(wireTransaction.length <= PACKET_DATA_SIZE, `Transaction too large: ${wireTransaction.length} > ${PACKET_DATA_SIZE}`);
1163
1196
  return wireTransaction;
1164
1197
  }
1165
1198
  /**
@@ -1169,7 +1202,7 @@ class Transaction {
1169
1202
 
1170
1203
 
1171
1204
  get keys() {
1172
- invariant__default['default'](this.instructions.length === 1);
1205
+ assert(this.instructions.length === 1);
1173
1206
  return this.instructions[0].keys.map(keyObj => keyObj.pubkey);
1174
1207
  }
1175
1208
  /**
@@ -1179,7 +1212,7 @@ class Transaction {
1179
1212
 
1180
1213
 
1181
1214
  get programId() {
1182
- invariant__default['default'](this.instructions.length === 1);
1215
+ assert(this.instructions.length === 1);
1183
1216
  return this.instructions[0].programId;
1184
1217
  }
1185
1218
  /**
@@ -1189,7 +1222,7 @@ class Transaction {
1189
1222
 
1190
1223
 
1191
1224
  get data() {
1192
- invariant__default['default'](this.instructions.length === 1);
1225
+ assert(this.instructions.length === 1);
1193
1226
  return this.instructions[0].data;
1194
1227
  }
1195
1228
  /**
@@ -1204,8 +1237,7 @@ class Transaction {
1204
1237
  let signatures = [];
1205
1238
 
1206
1239
  for (let i = 0; i < signatureCount; i++) {
1207
- const signature = byteArray.slice(0, SIGNATURE_LENGTH);
1208
- byteArray = byteArray.slice(SIGNATURE_LENGTH);
1240
+ const signature = guardedSplice(byteArray, 0, SIGNATURE_LENGTH);
1209
1241
  signatures.push(bs58__default['default'].encode(buffer.Buffer.from(signature)));
1210
1242
  }
1211
1243
 
@@ -2488,21 +2520,21 @@ function promiseTimeout(promise, timeoutMs) {
2488
2520
  }
2489
2521
 
2490
2522
  function makeWebsocketUrl(endpoint) {
2491
- let url$1 = url.parse(endpoint);
2492
- const useHttps = url$1.protocol === 'https:';
2493
- url$1.protocol = useHttps ? 'wss:' : 'ws:';
2494
- url$1.host = ''; // Only shift the port by +1 as a convention for ws(s) only if given endpoint
2523
+ let url = new URL(endpoint);
2524
+ const useHttps = url.protocol === 'https:';
2525
+ url.protocol = useHttps ? 'wss:' : 'ws:';
2526
+ url.host = ''; // Only shift the port by +1 as a convention for ws(s) only if given endpoint
2495
2527
  // is explictly specifying the endpoint port (HTTP-based RPC), assuming
2496
2528
  // we're directly trying to connect to solana-validator's ws listening port.
2497
2529
  // When the endpoint omits the port, we're connecting to the protocol
2498
2530
  // default ports: http(80) or https(443) and it's assumed we're behind a reverse
2499
2531
  // proxy which manages WebSocket upgrade and backend port redirection.
2500
2532
 
2501
- if (url$1.port !== null) {
2502
- url$1.port = String(Number(url$1.port) + 1);
2533
+ if (url.port !== '') {
2534
+ url.port = String(Number(url.port) + 1);
2503
2535
  }
2504
2536
 
2505
- return url.format(url$1);
2537
+ return url.toString();
2506
2538
  }
2507
2539
 
2508
2540
  const PublicKeyFromString = superstruct.coerce(superstruct.instance(PublicKey), superstruct.string(), value => new PublicKey(value));
@@ -3456,8 +3488,8 @@ class Connection {
3456
3488
 
3457
3489
  _defineProperty__default['default'](this, "_slotUpdateSubscriptions", {});
3458
3490
 
3459
- let url$1 = url.parse(endpoint);
3460
- const useHttps = url$1.protocol === 'https:';
3491
+ let url = new URL(endpoint);
3492
+ const useHttps = url.protocol === 'https:';
3461
3493
  let wsEndpoint;
3462
3494
  let httpHeaders;
3463
3495
  let fetchMiddleware;
@@ -3475,7 +3507,7 @@ class Connection {
3475
3507
 
3476
3508
  this._rpcEndpoint = endpoint;
3477
3509
  this._rpcWsEndpoint = wsEndpoint || makeWebsocketUrl(endpoint);
3478
- this._rpcClient = createRpcClient(url$1.href, useHttps, httpHeaders, fetchMiddleware, disableRetryOnRateLimit);
3510
+ this._rpcClient = createRpcClient(url.toString(), useHttps, httpHeaders, fetchMiddleware, disableRetryOnRateLimit);
3479
3511
  this._rpcRequest = createRpcRequest(this._rpcClient);
3480
3512
  this._rpcBatchRequest = createRpcBatchRequest(this._rpcClient);
3481
3513
  this._rpcWebSocket = new rpcWebsockets.Client(this._rpcWsEndpoint, {
@@ -3888,7 +3920,7 @@ class Connection {
3888
3920
  throw new Error('signature must be base58 encoded: ' + signature);
3889
3921
  }
3890
3922
 
3891
- invariant__default['default'](decodedSignature.length === 64, 'signature has invalid length');
3923
+ assert(decodedSignature.length === 64, 'signature has invalid length');
3892
3924
  const start = Date.now();
3893
3925
  const subscriptionCommitment = commitment || this.commitment;
3894
3926
  let subscriptionId;
@@ -4031,7 +4063,7 @@ class Connection {
4031
4063
  context,
4032
4064
  value: values
4033
4065
  } = await this.getSignatureStatuses([signature], config);
4034
- invariant__default['default'](values.length === 1);
4066
+ assert(values.length === 1);
4035
4067
  const value = values[0];
4036
4068
  return {
4037
4069
  context,
@@ -6325,7 +6357,7 @@ class Secp256k1Program {
6325
6357
  * @param {Buffer} publicKey a 64 byte secp256k1 public key buffer
6326
6358
  */
6327
6359
  static publicKeyToEthAddress(publicKey) {
6328
- invariant__default['default'](publicKey.length === PUBLIC_KEY_BYTES, `Public key must be ${PUBLIC_KEY_BYTES} bytes but received ${publicKey.length} bytes`);
6360
+ assert(publicKey.length === PUBLIC_KEY_BYTES, `Public key must be ${PUBLIC_KEY_BYTES} bytes but received ${publicKey.length} bytes`);
6329
6361
 
6330
6362
  try {
6331
6363
  return buffer.Buffer.from(jsSha3.keccak_256.update(toBuffer(publicKey)).digest()).slice(-ETHEREUM_ADDRESS_BYTES);
@@ -6378,7 +6410,7 @@ class Secp256k1Program {
6378
6410
  ethAddress = rawAddress;
6379
6411
  }
6380
6412
 
6381
- invariant__default['default'](ethAddress.length === ETHEREUM_ADDRESS_BYTES, `Address must be ${ETHEREUM_ADDRESS_BYTES} bytes but received ${ethAddress.length} bytes`);
6413
+ assert(ethAddress.length === ETHEREUM_ADDRESS_BYTES, `Address must be ${ETHEREUM_ADDRESS_BYTES} bytes but received ${ethAddress.length} bytes`);
6382
6414
  const dataStart = 1 + SIGNATURE_OFFSETS_SERIALIZED_SIZE;
6383
6415
  const ethAddressOffset = dataStart;
6384
6416
  const signatureOffset = dataStart + ethAddress.length;
@@ -6416,7 +6448,7 @@ class Secp256k1Program {
6416
6448
  privateKey: pkey,
6417
6449
  message
6418
6450
  } = params;
6419
- invariant__default['default'](pkey.length === PRIVATE_KEY_BYTES, `Private key must be ${PRIVATE_KEY_BYTES} bytes but received ${pkey.length} bytes`);
6451
+ assert(pkey.length === PRIVATE_KEY_BYTES, `Private key must be ${PRIVATE_KEY_BYTES} bytes but received ${pkey.length} bytes`);
6420
6452
 
6421
6453
  try {
6422
6454
  const privateKey = toBuffer(pkey);
@@ -6497,10 +6529,8 @@ class ValidatorInfo {
6497
6529
  const configKeys = [];
6498
6530
 
6499
6531
  for (let i = 0; i < 2; i++) {
6500
- const publicKey = new PublicKey(byteArray.slice(0, PUBKEY_LENGTH));
6501
- byteArray = byteArray.slice(PUBKEY_LENGTH);
6502
- const isSigner = byteArray.slice(0, 1)[0] === 1;
6503
- byteArray = byteArray.slice(1);
6532
+ const publicKey = new PublicKey(guardedSplice(byteArray, 0, PUBKEY_LENGTH));
6533
+ const isSigner = guardedShift(byteArray) === 1;
6504
6534
  configKeys.push({
6505
6535
  publicKey,
6506
6536
  isSigner