@solana/web3.js 1.74.0 → 1.76.0

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.
@@ -1,10 +1,7 @@
1
1
  'use strict';
2
2
 
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
3
  var buffer = require('buffer');
6
- var sha512 = require('@noble/hashes/sha512');
7
- var ed25519 = require('@noble/ed25519');
4
+ var ed25519 = require('@noble/curves/ed25519');
8
5
  var BN = require('bn.js');
9
6
  var bs58 = require('bs58');
10
7
  var sha256 = require('@noble/hashes/sha256');
@@ -16,13 +13,12 @@ var RpcClient = require('jayson/lib/client/browser');
16
13
  var RpcWebSocketCommonClient = require('rpc-websockets/dist/lib/client');
17
14
  var createRpc = require('rpc-websockets/dist/lib/client/websocket.browser');
18
15
  var sha3 = require('@noble/hashes/sha3');
19
- var hmac = require('@noble/hashes/hmac');
20
- var secp256k1 = require('@noble/secp256k1');
16
+ var secp256k1 = require('@noble/curves/secp256k1');
21
17
 
22
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
18
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }
23
19
 
24
- function _interopNamespace(e) {
25
- if (e && e.__esModule) return e;
20
+ function _interopNamespaceCompat(e) {
21
+ if (e && typeof e === 'object' && 'default' in e) return e;
26
22
  var n = Object.create(null);
27
23
  if (e) {
28
24
  Object.keys(e).forEach(function (k) {
@@ -35,18 +31,16 @@ function _interopNamespace(e) {
35
31
  }
36
32
  });
37
33
  }
38
- n["default"] = e;
34
+ n.default = e;
39
35
  return Object.freeze(n);
40
36
  }
41
37
 
42
- var ed25519__namespace = /*#__PURE__*/_interopNamespace(ed25519);
43
- var BN__default = /*#__PURE__*/_interopDefaultLegacy(BN);
44
- var bs58__default = /*#__PURE__*/_interopDefaultLegacy(bs58);
45
- var BufferLayout__namespace = /*#__PURE__*/_interopNamespace(BufferLayout);
46
- var RpcClient__default = /*#__PURE__*/_interopDefaultLegacy(RpcClient);
47
- var RpcWebSocketCommonClient__default = /*#__PURE__*/_interopDefaultLegacy(RpcWebSocketCommonClient);
48
- var createRpc__default = /*#__PURE__*/_interopDefaultLegacy(createRpc);
49
- var secp256k1__namespace = /*#__PURE__*/_interopNamespace(secp256k1);
38
+ var BN__default = /*#__PURE__*/_interopDefaultCompat(BN);
39
+ var bs58__default = /*#__PURE__*/_interopDefaultCompat(bs58);
40
+ var BufferLayout__namespace = /*#__PURE__*/_interopNamespaceCompat(BufferLayout);
41
+ var RpcClient__default = /*#__PURE__*/_interopDefaultCompat(RpcClient);
42
+ var RpcWebSocketCommonClient__default = /*#__PURE__*/_interopDefaultCompat(RpcWebSocketCommonClient);
43
+ var createRpc__default = /*#__PURE__*/_interopDefaultCompat(createRpc);
50
44
 
51
45
  /**
52
46
  * A 64 byte secret key, the first 32 bytes of which is the
@@ -54,10 +48,9 @@ var secp256k1__namespace = /*#__PURE__*/_interopNamespace(secp256k1);
54
48
  * Read more: https://blog.mozilla.org/warner/2011/11/29/ed25519-keys/
55
49
  */
56
50
 
57
- ed25519__namespace.utils.sha512Sync = (...m) => sha512.sha512(ed25519__namespace.utils.concatBytes(...m));
58
- const generatePrivateKey = ed25519__namespace.utils.randomPrivateKey;
51
+ const generatePrivateKey = ed25519.ed25519.utils.randomPrivateKey;
59
52
  const generateKeypair = () => {
60
- const privateScalar = ed25519__namespace.utils.randomPrivateKey();
53
+ const privateScalar = ed25519.ed25519.utils.randomPrivateKey();
61
54
  const publicKey = getPublicKey(privateScalar);
62
55
  const secretKey = new Uint8Array(64);
63
56
  secretKey.set(privateScalar);
@@ -67,17 +60,17 @@ const generateKeypair = () => {
67
60
  secretKey
68
61
  };
69
62
  };
70
- const getPublicKey = ed25519__namespace.sync.getPublicKey;
63
+ const getPublicKey = ed25519.ed25519.getPublicKey;
71
64
  function isOnCurve(publicKey) {
72
65
  try {
73
- ed25519__namespace.Point.fromHex(publicKey, true /* strict */);
66
+ ed25519.ed25519.ExtendedPoint.fromHex(publicKey);
74
67
  return true;
75
68
  } catch {
76
69
  return false;
77
70
  }
78
71
  }
79
- const sign = (message, secretKey) => ed25519__namespace.sync.sign(message, secretKey.slice(0, 32));
80
- const verify = ed25519__namespace.sync.verify;
72
+ const sign = (message, secretKey) => ed25519.ed25519.sign(message, secretKey.slice(0, 32));
73
+ const verify = ed25519.ed25519.verify;
81
74
 
82
75
  const toBuffer = arr => {
83
76
  if (buffer.Buffer.isBuffer(arr)) {
@@ -163,13 +156,13 @@ class PublicKey extends Struct {
163
156
  } else {
164
157
  if (typeof value === 'string') {
165
158
  // assume base 58 encoding by default
166
- const decoded = bs58__default["default"].decode(value);
159
+ const decoded = bs58__default.default.decode(value);
167
160
  if (decoded.length != PUBLIC_KEY_LENGTH) {
168
161
  throw new Error(`Invalid public key input`);
169
162
  }
170
- this._bn = new BN__default["default"](decoded);
163
+ this._bn = new BN__default.default(decoded);
171
164
  } else {
172
- this._bn = new BN__default["default"](value);
165
+ this._bn = new BN__default.default(value);
173
166
  }
174
167
  if (this._bn.byteLength() > PUBLIC_KEY_LENGTH) {
175
168
  throw new Error(`Invalid public key input`);
@@ -202,7 +195,7 @@ class PublicKey extends Struct {
202
195
  * Return the base-58 representation of the public key
203
196
  */
204
197
  toBase58() {
205
- return bs58__default["default"].encode(this.toBytes());
198
+ return bs58__default.default.encode(this.toBytes());
206
199
  }
207
200
  toJSON() {
208
201
  return this.toBase58();
@@ -634,8 +627,8 @@ class CompiledKeys {
634
627
  getOrInsertDefault(ix.programId).isInvoked = true;
635
628
  for (const accountMeta of ix.keys) {
636
629
  const keyMeta = getOrInsertDefault(accountMeta.pubkey);
637
- keyMeta.isSigner || (keyMeta.isSigner = accountMeta.isSigner);
638
- keyMeta.isWritable || (keyMeta.isWritable = accountMeta.isWritable);
630
+ keyMeta.isSigner ||= accountMeta.isSigner;
631
+ keyMeta.isWritable ||= accountMeta.isWritable;
639
632
  }
640
633
  }
641
634
  return new CompiledKeys(payer, keyMetaMap);
@@ -734,7 +727,7 @@ class Message {
734
727
  return this.instructions.map(ix => ({
735
728
  programIdIndex: ix.programIdIndex,
736
729
  accountKeyIndexes: ix.accounts,
737
- data: bs58__default["default"].decode(ix.data)
730
+ data: bs58__default.default.decode(ix.data)
738
731
  }));
739
732
  }
740
733
  get addressTableLookups() {
@@ -750,7 +743,7 @@ class Message {
750
743
  const instructions = accountKeys.compileInstructions(args.instructions).map(ix => ({
751
744
  programIdIndex: ix.programIdIndex,
752
745
  accounts: ix.accountKeyIndexes,
753
- data: bs58__default["default"].encode(ix.data)
746
+ data: bs58__default.default.encode(ix.data)
754
747
  }));
755
748
  return new Message({
756
749
  header,
@@ -792,7 +785,7 @@ class Message {
792
785
  accounts,
793
786
  programIdIndex
794
787
  } = instruction;
795
- const data = Array.from(bs58__default["default"].decode(instruction.data));
788
+ const data = Array.from(bs58__default.default.decode(instruction.data));
796
789
  let keyIndicesCount = [];
797
790
  encodeLength(keyIndicesCount, accounts.length);
798
791
  let dataCount = [];
@@ -823,7 +816,7 @@ class Message {
823
816
  numReadonlyUnsignedAccounts: buffer.Buffer.from([this.header.numReadonlyUnsignedAccounts]),
824
817
  keyCount: buffer.Buffer.from(keyCount),
825
818
  keys: this.accountKeys.map(key => toBuffer(key.toBytes())),
826
- recentBlockhash: bs58__default["default"].decode(this.recentBlockhash)
819
+ recentBlockhash: bs58__default.default.decode(this.recentBlockhash)
827
820
  };
828
821
  let signData = buffer.Buffer.alloc(2048);
829
822
  const length = signDataLayout.encode(transaction, signData);
@@ -861,7 +854,7 @@ class Message {
861
854
  byteArray = byteArray.slice(accountCount);
862
855
  const dataLength = decodeLength(byteArray);
863
856
  const dataSlice = byteArray.slice(0, dataLength);
864
- const data = bs58__default["default"].encode(buffer.Buffer.from(dataSlice));
857
+ const data = bs58__default.default.encode(buffer.Buffer.from(dataSlice));
865
858
  byteArray = byteArray.slice(dataLength);
866
859
  instructions.push({
867
860
  programIdIndex,
@@ -875,7 +868,7 @@ class Message {
875
868
  numReadonlySignedAccounts,
876
869
  numReadonlyUnsignedAccounts
877
870
  },
878
- recentBlockhash: bs58__default["default"].encode(buffer.Buffer.from(recentBlockhash)),
871
+ recentBlockhash: bs58__default.default.encode(buffer.Buffer.from(recentBlockhash)),
879
872
  accountKeys,
880
873
  instructions
881
874
  };
@@ -1019,7 +1012,7 @@ class MessageV0 {
1019
1012
  header: this.header,
1020
1013
  staticAccountKeysLength: new Uint8Array(encodedStaticAccountKeysLength),
1021
1014
  staticAccountKeys: this.staticAccountKeys.map(key => key.toBytes()),
1022
- recentBlockhash: bs58__default["default"].decode(this.recentBlockhash),
1015
+ recentBlockhash: bs58__default.default.decode(this.recentBlockhash),
1023
1016
  instructionsLength: new Uint8Array(encodedInstructionsLength),
1024
1017
  serializedInstructions,
1025
1018
  addressTableLookupsLength: new Uint8Array(encodedAddressTableLookupsLength),
@@ -1082,7 +1075,7 @@ class MessageV0 {
1082
1075
  for (let i = 0; i < staticAccountKeysLength; i++) {
1083
1076
  staticAccountKeys.push(new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH)));
1084
1077
  }
1085
- const recentBlockhash = bs58__default["default"].encode(byteArray.splice(0, PUBLIC_KEY_LENGTH));
1078
+ const recentBlockhash = bs58__default.default.encode(byteArray.splice(0, PUBLIC_KEY_LENGTH));
1086
1079
  const instructionCount = decodeLength(byteArray);
1087
1080
  const compiledInstructions = [];
1088
1081
  for (let i = 0; i < instructionCount; i++) {
@@ -1488,7 +1481,7 @@ class Transaction {
1488
1481
  return {
1489
1482
  programIdIndex: accountKeys.indexOf(programId.toString()),
1490
1483
  accounts: instruction.keys.map(meta => accountKeys.indexOf(meta.pubkey.toString())),
1491
- data: bs58__default["default"].encode(data)
1484
+ data: bs58__default.default.encode(data)
1492
1485
  };
1493
1486
  });
1494
1487
  compiledInstructions.forEach(instruction => {
@@ -1781,7 +1774,7 @@ class Transaction {
1781
1774
  for (let i = 0; i < signatureCount; i++) {
1782
1775
  const signature = byteArray.slice(0, SIGNATURE_LENGTH_IN_BYTES);
1783
1776
  byteArray = byteArray.slice(SIGNATURE_LENGTH_IN_BYTES);
1784
- signatures.push(bs58__default["default"].encode(buffer.Buffer.from(signature)));
1777
+ signatures.push(bs58__default.default.encode(buffer.Buffer.from(signature)));
1785
1778
  }
1786
1779
  return Transaction.populate(Message.from(byteArray), signatures);
1787
1780
  }
@@ -1797,7 +1790,7 @@ class Transaction {
1797
1790
  }
1798
1791
  signatures.forEach((signature, index) => {
1799
1792
  const sigPubkeyPair = {
1800
- signature: signature == bs58__default["default"].encode(DEFAULT_SIGNATURE) ? null : bs58__default["default"].decode(signature),
1793
+ signature: signature == bs58__default.default.encode(DEFAULT_SIGNATURE) ? null : bs58__default.default.decode(signature),
1801
1794
  publicKey: message.accountKeys[index]
1802
1795
  };
1803
1796
  transaction.signatures.push(sigPubkeyPair);
@@ -1814,7 +1807,7 @@ class Transaction {
1814
1807
  transaction.instructions.push(new TransactionInstruction({
1815
1808
  keys,
1816
1809
  programId: message.accountKeys[instruction.programIdIndex],
1817
- data: bs58__default["default"].decode(instruction.data)
1810
+ data: bs58__default.default.decode(instruction.data)
1818
1811
  }));
1819
1812
  });
1820
1813
  transaction._message = message;
@@ -3239,10 +3232,10 @@ class SolanaJSONRPCError extends Error {
3239
3232
 
3240
3233
  var fetchImpl = globalThis.fetch;
3241
3234
 
3242
- class RpcWebSocketClient extends RpcWebSocketCommonClient__default["default"] {
3235
+ class RpcWebSocketClient extends RpcWebSocketCommonClient__default.default {
3243
3236
  constructor(address, options, generate_request_id) {
3244
3237
  const webSocketFactory = url => {
3245
- const rpc = createRpc__default["default"](url, {
3238
+ const rpc = createRpc__default.default(url, {
3246
3239
  autoconnect: true,
3247
3240
  max_reconnects: 5,
3248
3241
  reconnect: true,
@@ -3496,7 +3489,7 @@ function versionedMessageFromResponse(version, response) {
3496
3489
  compiledInstructions: response.instructions.map(ix => ({
3497
3490
  programIdIndex: ix.programIdIndex,
3498
3491
  accountKeyIndexes: ix.accounts,
3499
- data: bs58__default["default"].decode(ix.data)
3492
+ data: bs58__default.default.decode(ix.data)
3500
3493
  })),
3501
3494
  addressTableLookups: response.addressTableLookups
3502
3495
  });
@@ -3536,6 +3529,13 @@ const GetInflationRewardResult = jsonRpcResult(superstruct.array(superstruct.nul
3536
3529
  postBalance: superstruct.number(),
3537
3530
  commission: superstruct.optional(superstruct.nullable(superstruct.number()))
3538
3531
  }))));
3532
+ /**
3533
+ * Expected JSON RPC response for the "getRecentPrioritizationFees" message
3534
+ */
3535
+ const GetRecentPrioritizationFeesResult = superstruct.array(superstruct.type({
3536
+ slot: superstruct.number(),
3537
+ prioritizationFee: superstruct.number()
3538
+ }));
3539
3539
  /**
3540
3540
  * Expected JSON RPC response for the "getInflationRate" message
3541
3541
  */
@@ -3650,7 +3650,7 @@ function createRpcClient(url, httpHeaders, customFetch, fetchMiddleware, disable
3650
3650
  return await fetch(...modifiedFetchArgs);
3651
3651
  };
3652
3652
  }
3653
- const clientBrowser = new RpcClient__default["default"](async (request, callback) => {
3653
+ const clientBrowser = new RpcClient__default.default(async (request, callback) => {
3654
3654
  const options = {
3655
3655
  method: 'POST',
3656
3656
  body: request,
@@ -3737,6 +3737,11 @@ const GetInflationGovernorRpcResult = jsonRpcResult(GetInflationGovernorResult);
3737
3737
  */
3738
3738
  const GetInflationRateRpcResult = jsonRpcResult(GetInflationRateResult);
3739
3739
 
3740
+ /**
3741
+ * Expected JSON RPC response for the "getRecentPrioritizationFees" message
3742
+ */
3743
+ const GetRecentPrioritizationFeesRpcResult = jsonRpcResult(GetRecentPrioritizationFeesResult);
3744
+
3740
3745
  /**
3741
3746
  * Expected JSON RPC response for the "getEpochInfo" message
3742
3747
  */
@@ -4964,7 +4969,7 @@ class Connection {
4964
4969
  }
4965
4970
  let decodedSignature;
4966
4971
  try {
4967
- decodedSignature = bs58__default["default"].decode(rawSignature);
4972
+ decodedSignature = bs58__default.default.decode(rawSignature);
4968
4973
  } catch (err) {
4969
4974
  throw new Error('signature must be base58 encoded: ' + rawSignature);
4970
4975
  }
@@ -5634,6 +5639,19 @@ class Connection {
5634
5639
  return res.result;
5635
5640
  }
5636
5641
 
5642
+ /**
5643
+ * Fetch a list of prioritization fees from recent blocks.
5644
+ */
5645
+ async getRecentPrioritizationFees(config) {
5646
+ const accounts = config?.lockedWritableAccounts?.map(key => key.toBase58());
5647
+ const args = this._buildArgs(accounts?.length ? [accounts] : []);
5648
+ const unsafeRes = await this._rpcRequest('getRecentPrioritizationFees', args);
5649
+ const res = superstruct.create(unsafeRes, GetRecentPrioritizationFeesRpcResult);
5650
+ if ('error' in res) {
5651
+ throw new SolanaJSONRPCError(res.error, 'failed to get recent prioritization fees');
5652
+ }
5653
+ return res.result;
5654
+ }
5637
5655
  /**
5638
5656
  * Fetch a recent blockhash from the cluster
5639
5657
  * @return {Promise<{blockhash: Blockhash, feeCalculator: FeeCalculator}>}
@@ -6644,12 +6662,11 @@ class Connection {
6644
6662
  * @internal
6645
6663
  */
6646
6664
  _onSubscriptionStateChange(clientSubscriptionId, callback) {
6647
- var _this$_subscriptionSt;
6648
6665
  const hash = this._subscriptionHashByClientSubscriptionId[clientSubscriptionId];
6649
6666
  if (hash == null) {
6650
6667
  return () => {};
6651
6668
  }
6652
- const stateChangeCallbacks = (_this$_subscriptionSt = this._subscriptionStateChangeCallbacksByHash)[hash] || (_this$_subscriptionSt[hash] = new Set());
6669
+ const stateChangeCallbacks = this._subscriptionStateChangeCallbacksByHash[hash] ||= new Set();
6653
6670
  stateChangeCallbacks.add(callback);
6654
6671
  return () => {
6655
6672
  stateChangeCallbacks.delete(callback);
@@ -7877,19 +7894,12 @@ class Ed25519Program {
7877
7894
  }
7878
7895
  Ed25519Program.programId = new PublicKey('Ed25519SigVerify111111111111111111111111111');
7879
7896
 
7880
- // Supply a synchronous hashing algorithm to make this
7881
- // library interoperable with the synchronous APIs in web3.js.
7882
- secp256k1__namespace.utils.hmacSha256Sync = (key, ...msgs) => {
7883
- const h = hmac.hmac.create(sha256.sha256, key);
7884
- msgs.forEach(msg => h.update(msg));
7885
- return h.digest();
7897
+ const ecdsaSign = (msgHash, privKey) => {
7898
+ const signature = secp256k1.secp256k1.sign(msgHash, privKey);
7899
+ return [signature.toCompactRawBytes(), signature.recovery];
7886
7900
  };
7887
- const ecdsaSign = (msgHash, privKey) => secp256k1__namespace.signSync(msgHash, privKey, {
7888
- der: false,
7889
- recovered: true
7890
- });
7891
- secp256k1__namespace.utils.isValidPrivateKey;
7892
- const publicKeyCreate = secp256k1__namespace.getPublicKey;
7901
+ secp256k1.secp256k1.utils.isValidPrivateKey;
7902
+ const publicKeyCreate = secp256k1.secp256k1.getPublicKey;
7893
7903
 
7894
7904
  const PRIVATE_KEY_BYTES = 32;
7895
7905
  const ETHEREUM_ADDRESS_BYTES = 20;