@solana/web3.js 1.56.3 → 1.57.1

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.
@@ -112,12 +112,14 @@ const PUBLIC_KEY_LENGTH = 32;
112
112
 
113
113
  function isPublicKeyData(value) {
114
114
  return value._bn !== undefined;
115
- }
115
+ } // local counter used by PublicKey.unique()
116
+
117
+
118
+ let uniquePublicKeyCounter = 1;
116
119
  /**
117
120
  * A public key
118
121
  */
119
122
 
120
-
121
123
  class PublicKey extends Struct {
122
124
  /** @internal */
123
125
 
@@ -150,6 +152,16 @@ class PublicKey extends Struct {
150
152
  }
151
153
  }
152
154
  }
155
+ /**
156
+ * Returns a unique PublicKey for tests and benchmarks using acounter
157
+ */
158
+
159
+
160
+ static unique() {
161
+ const key = new PublicKey(uniquePublicKeyCounter);
162
+ uniquePublicKeyCounter += 1;
163
+ return new PublicKey(key.toBuffer());
164
+ }
153
165
  /**
154
166
  * Default public key value. (All zeros)
155
167
  */
@@ -406,6 +418,71 @@ Object.defineProperty(TransactionExpiredTimeoutError.prototype, 'name', {
406
418
  value: 'TransactionExpiredTimeoutError'
407
419
  });
408
420
 
421
+ class MessageAccountKeys {
422
+ constructor(staticAccountKeys, accountKeysFromLookups) {
423
+ this.staticAccountKeys = void 0;
424
+ this.accountKeysFromLookups = void 0;
425
+ this.staticAccountKeys = staticAccountKeys;
426
+ this.accountKeysFromLookups = accountKeysFromLookups;
427
+ }
428
+
429
+ keySegments() {
430
+ const keySegments = [this.staticAccountKeys];
431
+
432
+ if (this.accountKeysFromLookups) {
433
+ keySegments.push(this.accountKeysFromLookups.writable);
434
+ keySegments.push(this.accountKeysFromLookups.readonly);
435
+ }
436
+
437
+ return keySegments;
438
+ }
439
+
440
+ get(index) {
441
+ for (const keySegment of this.keySegments()) {
442
+ if (index < keySegment.length) {
443
+ return keySegment[index];
444
+ } else {
445
+ index -= keySegment.length;
446
+ }
447
+ }
448
+
449
+ return;
450
+ }
451
+
452
+ get length() {
453
+ return this.keySegments().flat().length;
454
+ }
455
+
456
+ compileInstructions(instructions) {
457
+ // Bail early if any account indexes would overflow a u8
458
+ const U8_MAX = 255;
459
+
460
+ if (this.length > U8_MAX + 1) {
461
+ throw new Error('Account index overflow encountered during compilation');
462
+ }
463
+
464
+ const keyIndexMap = new Map();
465
+ this.keySegments().flat().forEach((key, index) => {
466
+ keyIndexMap.set(key.toBase58(), index);
467
+ });
468
+
469
+ const findKeyIndex = key => {
470
+ const keyIndex = keyIndexMap.get(key.toBase58());
471
+ if (keyIndex === undefined) throw new Error('Encountered an unknown instruction account key during compilation');
472
+ return keyIndex;
473
+ };
474
+
475
+ return instructions.map(instruction => {
476
+ return {
477
+ programIdIndex: findKeyIndex(instruction.programId),
478
+ accountKeyIndexes: instruction.keys.map(meta => findKeyIndex(meta.pubkey)),
479
+ data: instruction.data
480
+ };
481
+ });
482
+ }
483
+
484
+ }
485
+
409
486
  /**
410
487
  * Layout for a public key
411
488
  */
@@ -736,6 +813,115 @@ function assert (condition, message) {
736
813
  }
737
814
  }
738
815
 
816
+ class CompiledKeys {
817
+ constructor(payer, keyMetaMap) {
818
+ this.payer = void 0;
819
+ this.keyMetaMap = void 0;
820
+ this.payer = payer;
821
+ this.keyMetaMap = keyMetaMap;
822
+ }
823
+
824
+ static compile(instructions, payer) {
825
+ const keyMetaMap = new Map();
826
+
827
+ const getOrInsertDefault = pubkey => {
828
+ const address = pubkey.toBase58();
829
+ let keyMeta = keyMetaMap.get(address);
830
+
831
+ if (keyMeta === undefined) {
832
+ keyMeta = {
833
+ isSigner: false,
834
+ isWritable: false,
835
+ isInvoked: false
836
+ };
837
+ keyMetaMap.set(address, keyMeta);
838
+ }
839
+
840
+ return keyMeta;
841
+ };
842
+
843
+ const payerKeyMeta = getOrInsertDefault(payer);
844
+ payerKeyMeta.isSigner = true;
845
+ payerKeyMeta.isWritable = true;
846
+
847
+ for (const ix of instructions) {
848
+ getOrInsertDefault(ix.programId).isInvoked = true;
849
+
850
+ for (const accountMeta of ix.keys) {
851
+ const keyMeta = getOrInsertDefault(accountMeta.pubkey);
852
+ keyMeta.isSigner || (keyMeta.isSigner = accountMeta.isSigner);
853
+ keyMeta.isWritable || (keyMeta.isWritable = accountMeta.isWritable);
854
+ }
855
+ }
856
+
857
+ return new CompiledKeys(payer, keyMetaMap);
858
+ }
859
+
860
+ getMessageComponents() {
861
+ const mapEntries = [...this.keyMetaMap.entries()];
862
+ assert(mapEntries.length <= 256, 'Max static account keys length exceeded');
863
+ const writableSigners = mapEntries.filter(([, meta]) => meta.isSigner && meta.isWritable);
864
+ const readonlySigners = mapEntries.filter(([, meta]) => meta.isSigner && !meta.isWritable);
865
+ const writableNonSigners = mapEntries.filter(([, meta]) => !meta.isSigner && meta.isWritable);
866
+ const readonlyNonSigners = mapEntries.filter(([, meta]) => !meta.isSigner && !meta.isWritable);
867
+ const header = {
868
+ numRequiredSignatures: writableSigners.length + readonlySigners.length,
869
+ numReadonlySignedAccounts: readonlySigners.length,
870
+ numReadonlyUnsignedAccounts: readonlyNonSigners.length
871
+ }; // sanity checks
872
+
873
+ {
874
+ assert(writableSigners.length > 0, 'Expected at least one writable signer key');
875
+ const [payerAddress] = writableSigners[0];
876
+ assert(payerAddress === this.payer.toBase58(), 'Expected first writable signer key to be the fee payer');
877
+ }
878
+ const staticAccountKeys = [...writableSigners.map(([address]) => new PublicKey(address)), ...readonlySigners.map(([address]) => new PublicKey(address)), ...writableNonSigners.map(([address]) => new PublicKey(address)), ...readonlyNonSigners.map(([address]) => new PublicKey(address))];
879
+ return [header, staticAccountKeys];
880
+ }
881
+
882
+ extractTableLookup(lookupTable) {
883
+ const [writableIndexes, drainedWritableKeys] = this.drainKeysFoundInLookupTable(lookupTable.state.addresses, keyMeta => !keyMeta.isSigner && !keyMeta.isInvoked && keyMeta.isWritable);
884
+ const [readonlyIndexes, drainedReadonlyKeys] = this.drainKeysFoundInLookupTable(lookupTable.state.addresses, keyMeta => !keyMeta.isSigner && !keyMeta.isInvoked && !keyMeta.isWritable); // Don't extract lookup if no keys were found
885
+
886
+ if (writableIndexes.length === 0 && readonlyIndexes.length === 0) {
887
+ return;
888
+ }
889
+
890
+ return [{
891
+ accountKey: lookupTable.key,
892
+ writableIndexes,
893
+ readonlyIndexes
894
+ }, {
895
+ writable: drainedWritableKeys,
896
+ readonly: drainedReadonlyKeys
897
+ }];
898
+ }
899
+ /** @internal */
900
+
901
+
902
+ drainKeysFoundInLookupTable(lookupTableEntries, keyMetaFilter) {
903
+ const lookupTableIndexes = new Array();
904
+ const drainedKeys = new Array();
905
+
906
+ for (const [address, keyMeta] of this.keyMetaMap.entries()) {
907
+ if (keyMetaFilter(keyMeta)) {
908
+ const key = new PublicKey(address);
909
+ const lookupTableIndex = lookupTableEntries.findIndex(entry => entry.equals(key));
910
+
911
+ if (lookupTableIndex >= 0) {
912
+ assert(lookupTableIndex < 256, 'Max lookup table index exceeded');
913
+ lookupTableIndexes.push(lookupTableIndex);
914
+ drainedKeys.push(key);
915
+ this.keyMetaMap.delete(address);
916
+ }
917
+ }
918
+ }
919
+
920
+ return [lookupTableIndexes, drainedKeys];
921
+ }
922
+
923
+ }
924
+
739
925
  /**
740
926
  * Message constructor arguments
741
927
  */
@@ -758,6 +944,41 @@ class MessageV0 {
758
944
  return 0;
759
945
  }
760
946
 
947
+ static compile(args) {
948
+ const compiledKeys = CompiledKeys.compile(args.instructions, args.payerKey);
949
+ const addressTableLookups = new Array();
950
+ const accountKeysFromLookups = {
951
+ writable: new Array(),
952
+ readonly: new Array()
953
+ };
954
+ const lookupTableAccounts = args.addressLookupTableAccounts || [];
955
+
956
+ for (const lookupTable of lookupTableAccounts) {
957
+ const extractResult = compiledKeys.extractTableLookup(lookupTable);
958
+
959
+ if (extractResult !== undefined) {
960
+ const [addressTableLookup, {
961
+ writable,
962
+ readonly
963
+ }] = extractResult;
964
+ addressTableLookups.push(addressTableLookup);
965
+ accountKeysFromLookups.writable.push(...writable);
966
+ accountKeysFromLookups.readonly.push(...readonly);
967
+ }
968
+ }
969
+
970
+ const [header, staticAccountKeys] = compiledKeys.getMessageComponents();
971
+ const accountKeys = new MessageAccountKeys(staticAccountKeys, accountKeysFromLookups);
972
+ const compiledInstructions = accountKeys.compileInstructions(args.instructions);
973
+ return new MessageV0({
974
+ header,
975
+ staticAccountKeys,
976
+ recentBlockhash: args.recentBlockhash,
977
+ compiledInstructions,
978
+ addressTableLookups
979
+ });
980
+ }
981
+
761
982
  serialize() {
762
983
  const encodedStaticAccountKeysLength = Array();
763
984
  encodeLength(encodedStaticAccountKeysLength, this.staticAccountKeys.length);
@@ -4094,7 +4315,7 @@ const LogsNotificationResult = type({
4094
4315
 
4095
4316
  /** @internal */
4096
4317
  const COMMON_HTTP_HEADERS = {
4097
- 'solana-client': `js/${(_process$env$npm_pack = "1.56.3") !== null && _process$env$npm_pack !== void 0 ? _process$env$npm_pack : 'UNKNOWN'}`
4318
+ 'solana-client': `js/${(_process$env$npm_pack = "1.57.1") !== null && _process$env$npm_pack !== void 0 ? _process$env$npm_pack : 'UNKNOWN'}`
4098
4319
  };
4099
4320
  /**
4100
4321
  * A connection to a fullnode JSON RPC endpoint
@@ -9083,5 +9304,5 @@ async function sendAndConfirmRawTransaction(connection, rawTransaction, confirma
9083
9304
 
9084
9305
  const LAMPORTS_PER_SOL = 1000000000;
9085
9306
 
9086
- export { Account, AddressLookupTableAccount, AddressLookupTableInstruction, AddressLookupTableProgram, Authorized, BLOCKHASH_CACHE_TIMEOUT_MS, BPF_LOADER_DEPRECATED_PROGRAM_ID, BPF_LOADER_PROGRAM_ID, BpfLoader, COMPUTE_BUDGET_INSTRUCTION_LAYOUTS, ComputeBudgetInstruction, ComputeBudgetProgram, Connection, Ed25519Program, Enum, EpochSchedule, FeeCalculatorLayout, Keypair, LAMPORTS_PER_SOL, LOOKUP_TABLE_INSTRUCTION_LAYOUTS, Loader, Lockup, MAX_SEED_LENGTH, Message, MessageV0, NONCE_ACCOUNT_LENGTH, NonceAccount, PACKET_DATA_SIZE, PUBLIC_KEY_LENGTH, PublicKey, SIGNATURE_LENGTH_IN_BYTES, SOLANA_SCHEMA, STAKE_CONFIG_ID, STAKE_INSTRUCTION_LAYOUTS, SYSTEM_INSTRUCTION_LAYOUTS, SYSVAR_CLOCK_PUBKEY, SYSVAR_EPOCH_SCHEDULE_PUBKEY, SYSVAR_INSTRUCTIONS_PUBKEY, SYSVAR_RECENT_BLOCKHASHES_PUBKEY, SYSVAR_RENT_PUBKEY, SYSVAR_REWARDS_PUBKEY, SYSVAR_SLOT_HASHES_PUBKEY, SYSVAR_SLOT_HISTORY_PUBKEY, SYSVAR_STAKE_HISTORY_PUBKEY, Secp256k1Program, SendTransactionError, SolanaJSONRPCError, SolanaJSONRPCErrorCode, StakeAuthorizationLayout, StakeInstruction, StakeProgram, Struct, SystemInstruction, SystemProgram, Transaction, TransactionExpiredBlockheightExceededError, TransactionExpiredTimeoutError, TransactionInstruction, TransactionStatus, VALIDATOR_INFO_KEY, VERSION_PREFIX_MASK, VOTE_PROGRAM_ID, ValidatorInfo, VersionedMessage, VersionedTransaction, VoteAccount, VoteAuthorizationLayout, VoteInit, VoteInstruction, VoteProgram, clusterApiUrl, sendAndConfirmRawTransaction, sendAndConfirmTransaction };
9307
+ export { Account, AddressLookupTableAccount, AddressLookupTableInstruction, AddressLookupTableProgram, Authorized, BLOCKHASH_CACHE_TIMEOUT_MS, BPF_LOADER_DEPRECATED_PROGRAM_ID, BPF_LOADER_PROGRAM_ID, BpfLoader, COMPUTE_BUDGET_INSTRUCTION_LAYOUTS, ComputeBudgetInstruction, ComputeBudgetProgram, Connection, Ed25519Program, Enum, EpochSchedule, FeeCalculatorLayout, Keypair, LAMPORTS_PER_SOL, LOOKUP_TABLE_INSTRUCTION_LAYOUTS, Loader, Lockup, MAX_SEED_LENGTH, Message, MessageAccountKeys, MessageV0, NONCE_ACCOUNT_LENGTH, NonceAccount, PACKET_DATA_SIZE, PUBLIC_KEY_LENGTH, PublicKey, SIGNATURE_LENGTH_IN_BYTES, SOLANA_SCHEMA, STAKE_CONFIG_ID, STAKE_INSTRUCTION_LAYOUTS, SYSTEM_INSTRUCTION_LAYOUTS, SYSVAR_CLOCK_PUBKEY, SYSVAR_EPOCH_SCHEDULE_PUBKEY, SYSVAR_INSTRUCTIONS_PUBKEY, SYSVAR_RECENT_BLOCKHASHES_PUBKEY, SYSVAR_RENT_PUBKEY, SYSVAR_REWARDS_PUBKEY, SYSVAR_SLOT_HASHES_PUBKEY, SYSVAR_SLOT_HISTORY_PUBKEY, SYSVAR_STAKE_HISTORY_PUBKEY, Secp256k1Program, SendTransactionError, SolanaJSONRPCError, SolanaJSONRPCErrorCode, StakeAuthorizationLayout, StakeInstruction, StakeProgram, Struct, SystemInstruction, SystemProgram, Transaction, TransactionExpiredBlockheightExceededError, TransactionExpiredTimeoutError, TransactionInstruction, TransactionStatus, VALIDATOR_INFO_KEY, VERSION_PREFIX_MASK, VOTE_PROGRAM_ID, ValidatorInfo, VersionedMessage, VersionedTransaction, VoteAccount, VoteAuthorizationLayout, VoteInit, VoteInstruction, VoteProgram, clusterApiUrl, sendAndConfirmRawTransaction, sendAndConfirmTransaction };
9087
9308
  //# sourceMappingURL=index.browser.esm.js.map