@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.
package/lib/index.cjs.js CHANGED
@@ -149,12 +149,14 @@ const PUBLIC_KEY_LENGTH = 32;
149
149
 
150
150
  function isPublicKeyData(value) {
151
151
  return value._bn !== undefined;
152
- }
152
+ } // local counter used by PublicKey.unique()
153
+
154
+
155
+ let uniquePublicKeyCounter = 1;
153
156
  /**
154
157
  * A public key
155
158
  */
156
159
 
157
-
158
160
  class PublicKey extends Struct {
159
161
  /** @internal */
160
162
 
@@ -187,6 +189,16 @@ class PublicKey extends Struct {
187
189
  }
188
190
  }
189
191
  }
192
+ /**
193
+ * Returns a unique PublicKey for tests and benchmarks using acounter
194
+ */
195
+
196
+
197
+ static unique() {
198
+ const key = new PublicKey(uniquePublicKeyCounter);
199
+ uniquePublicKeyCounter += 1;
200
+ return new PublicKey(key.toBuffer());
201
+ }
190
202
  /**
191
203
  * Default public key value. (All zeros)
192
204
  */
@@ -443,6 +455,71 @@ Object.defineProperty(TransactionExpiredTimeoutError.prototype, 'name', {
443
455
  value: 'TransactionExpiredTimeoutError'
444
456
  });
445
457
 
458
+ class MessageAccountKeys {
459
+ constructor(staticAccountKeys, accountKeysFromLookups) {
460
+ this.staticAccountKeys = void 0;
461
+ this.accountKeysFromLookups = void 0;
462
+ this.staticAccountKeys = staticAccountKeys;
463
+ this.accountKeysFromLookups = accountKeysFromLookups;
464
+ }
465
+
466
+ keySegments() {
467
+ const keySegments = [this.staticAccountKeys];
468
+
469
+ if (this.accountKeysFromLookups) {
470
+ keySegments.push(this.accountKeysFromLookups.writable);
471
+ keySegments.push(this.accountKeysFromLookups.readonly);
472
+ }
473
+
474
+ return keySegments;
475
+ }
476
+
477
+ get(index) {
478
+ for (const keySegment of this.keySegments()) {
479
+ if (index < keySegment.length) {
480
+ return keySegment[index];
481
+ } else {
482
+ index -= keySegment.length;
483
+ }
484
+ }
485
+
486
+ return;
487
+ }
488
+
489
+ get length() {
490
+ return this.keySegments().flat().length;
491
+ }
492
+
493
+ compileInstructions(instructions) {
494
+ // Bail early if any account indexes would overflow a u8
495
+ const U8_MAX = 255;
496
+
497
+ if (this.length > U8_MAX + 1) {
498
+ throw new Error('Account index overflow encountered during compilation');
499
+ }
500
+
501
+ const keyIndexMap = new Map();
502
+ this.keySegments().flat().forEach((key, index) => {
503
+ keyIndexMap.set(key.toBase58(), index);
504
+ });
505
+
506
+ const findKeyIndex = key => {
507
+ const keyIndex = keyIndexMap.get(key.toBase58());
508
+ if (keyIndex === undefined) throw new Error('Encountered an unknown instruction account key during compilation');
509
+ return keyIndex;
510
+ };
511
+
512
+ return instructions.map(instruction => {
513
+ return {
514
+ programIdIndex: findKeyIndex(instruction.programId),
515
+ accountKeyIndexes: instruction.keys.map(meta => findKeyIndex(meta.pubkey)),
516
+ data: instruction.data
517
+ };
518
+ });
519
+ }
520
+
521
+ }
522
+
446
523
  /**
447
524
  * Layout for a public key
448
525
  */
@@ -773,6 +850,115 @@ function assert (condition, message) {
773
850
  }
774
851
  }
775
852
 
853
+ class CompiledKeys {
854
+ constructor(payer, keyMetaMap) {
855
+ this.payer = void 0;
856
+ this.keyMetaMap = void 0;
857
+ this.payer = payer;
858
+ this.keyMetaMap = keyMetaMap;
859
+ }
860
+
861
+ static compile(instructions, payer) {
862
+ const keyMetaMap = new Map();
863
+
864
+ const getOrInsertDefault = pubkey => {
865
+ const address = pubkey.toBase58();
866
+ let keyMeta = keyMetaMap.get(address);
867
+
868
+ if (keyMeta === undefined) {
869
+ keyMeta = {
870
+ isSigner: false,
871
+ isWritable: false,
872
+ isInvoked: false
873
+ };
874
+ keyMetaMap.set(address, keyMeta);
875
+ }
876
+
877
+ return keyMeta;
878
+ };
879
+
880
+ const payerKeyMeta = getOrInsertDefault(payer);
881
+ payerKeyMeta.isSigner = true;
882
+ payerKeyMeta.isWritable = true;
883
+
884
+ for (const ix of instructions) {
885
+ getOrInsertDefault(ix.programId).isInvoked = true;
886
+
887
+ for (const accountMeta of ix.keys) {
888
+ const keyMeta = getOrInsertDefault(accountMeta.pubkey);
889
+ keyMeta.isSigner || (keyMeta.isSigner = accountMeta.isSigner);
890
+ keyMeta.isWritable || (keyMeta.isWritable = accountMeta.isWritable);
891
+ }
892
+ }
893
+
894
+ return new CompiledKeys(payer, keyMetaMap);
895
+ }
896
+
897
+ getMessageComponents() {
898
+ const mapEntries = [...this.keyMetaMap.entries()];
899
+ assert(mapEntries.length <= 256, 'Max static account keys length exceeded');
900
+ const writableSigners = mapEntries.filter(([, meta]) => meta.isSigner && meta.isWritable);
901
+ const readonlySigners = mapEntries.filter(([, meta]) => meta.isSigner && !meta.isWritable);
902
+ const writableNonSigners = mapEntries.filter(([, meta]) => !meta.isSigner && meta.isWritable);
903
+ const readonlyNonSigners = mapEntries.filter(([, meta]) => !meta.isSigner && !meta.isWritable);
904
+ const header = {
905
+ numRequiredSignatures: writableSigners.length + readonlySigners.length,
906
+ numReadonlySignedAccounts: readonlySigners.length,
907
+ numReadonlyUnsignedAccounts: readonlyNonSigners.length
908
+ }; // sanity checks
909
+
910
+ {
911
+ assert(writableSigners.length > 0, 'Expected at least one writable signer key');
912
+ const [payerAddress] = writableSigners[0];
913
+ assert(payerAddress === this.payer.toBase58(), 'Expected first writable signer key to be the fee payer');
914
+ }
915
+ 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))];
916
+ return [header, staticAccountKeys];
917
+ }
918
+
919
+ extractTableLookup(lookupTable) {
920
+ const [writableIndexes, drainedWritableKeys] = this.drainKeysFoundInLookupTable(lookupTable.state.addresses, keyMeta => !keyMeta.isSigner && !keyMeta.isInvoked && keyMeta.isWritable);
921
+ const [readonlyIndexes, drainedReadonlyKeys] = this.drainKeysFoundInLookupTable(lookupTable.state.addresses, keyMeta => !keyMeta.isSigner && !keyMeta.isInvoked && !keyMeta.isWritable); // Don't extract lookup if no keys were found
922
+
923
+ if (writableIndexes.length === 0 && readonlyIndexes.length === 0) {
924
+ return;
925
+ }
926
+
927
+ return [{
928
+ accountKey: lookupTable.key,
929
+ writableIndexes,
930
+ readonlyIndexes
931
+ }, {
932
+ writable: drainedWritableKeys,
933
+ readonly: drainedReadonlyKeys
934
+ }];
935
+ }
936
+ /** @internal */
937
+
938
+
939
+ drainKeysFoundInLookupTable(lookupTableEntries, keyMetaFilter) {
940
+ const lookupTableIndexes = new Array();
941
+ const drainedKeys = new Array();
942
+
943
+ for (const [address, keyMeta] of this.keyMetaMap.entries()) {
944
+ if (keyMetaFilter(keyMeta)) {
945
+ const key = new PublicKey(address);
946
+ const lookupTableIndex = lookupTableEntries.findIndex(entry => entry.equals(key));
947
+
948
+ if (lookupTableIndex >= 0) {
949
+ assert(lookupTableIndex < 256, 'Max lookup table index exceeded');
950
+ lookupTableIndexes.push(lookupTableIndex);
951
+ drainedKeys.push(key);
952
+ this.keyMetaMap.delete(address);
953
+ }
954
+ }
955
+ }
956
+
957
+ return [lookupTableIndexes, drainedKeys];
958
+ }
959
+
960
+ }
961
+
776
962
  /**
777
963
  * Message constructor arguments
778
964
  */
@@ -795,6 +981,41 @@ class MessageV0 {
795
981
  return 0;
796
982
  }
797
983
 
984
+ static compile(args) {
985
+ const compiledKeys = CompiledKeys.compile(args.instructions, args.payerKey);
986
+ const addressTableLookups = new Array();
987
+ const accountKeysFromLookups = {
988
+ writable: new Array(),
989
+ readonly: new Array()
990
+ };
991
+ const lookupTableAccounts = args.addressLookupTableAccounts || [];
992
+
993
+ for (const lookupTable of lookupTableAccounts) {
994
+ const extractResult = compiledKeys.extractTableLookup(lookupTable);
995
+
996
+ if (extractResult !== undefined) {
997
+ const [addressTableLookup, {
998
+ writable,
999
+ readonly
1000
+ }] = extractResult;
1001
+ addressTableLookups.push(addressTableLookup);
1002
+ accountKeysFromLookups.writable.push(...writable);
1003
+ accountKeysFromLookups.readonly.push(...readonly);
1004
+ }
1005
+ }
1006
+
1007
+ const [header, staticAccountKeys] = compiledKeys.getMessageComponents();
1008
+ const accountKeys = new MessageAccountKeys(staticAccountKeys, accountKeysFromLookups);
1009
+ const compiledInstructions = accountKeys.compileInstructions(args.instructions);
1010
+ return new MessageV0({
1011
+ header,
1012
+ staticAccountKeys,
1013
+ recentBlockhash: args.recentBlockhash,
1014
+ compiledInstructions,
1015
+ addressTableLookups
1016
+ });
1017
+ }
1018
+
798
1019
  serialize() {
799
1020
  const encodedStaticAccountKeysLength = Array();
800
1021
  encodeLength(encodedStaticAccountKeysLength, this.staticAccountKeys.length);
@@ -4191,7 +4412,7 @@ const LogsNotificationResult = superstruct.type({
4191
4412
 
4192
4413
  /** @internal */
4193
4414
  const COMMON_HTTP_HEADERS = {
4194
- 'solana-client': `js/${(_process$env$npm_pack = "1.56.3") !== null && _process$env$npm_pack !== void 0 ? _process$env$npm_pack : 'UNKNOWN'}`
4415
+ 'solana-client': `js/${(_process$env$npm_pack = "1.57.1") !== null && _process$env$npm_pack !== void 0 ? _process$env$npm_pack : 'UNKNOWN'}`
4195
4416
  };
4196
4417
  /**
4197
4418
  * A connection to a fullnode JSON RPC endpoint
@@ -9204,6 +9425,7 @@ exports.Loader = Loader;
9204
9425
  exports.Lockup = Lockup;
9205
9426
  exports.MAX_SEED_LENGTH = MAX_SEED_LENGTH;
9206
9427
  exports.Message = Message;
9428
+ exports.MessageAccountKeys = MessageAccountKeys;
9207
9429
  exports.MessageV0 = MessageV0;
9208
9430
  exports.NONCE_ACCOUNT_LENGTH = NONCE_ACCOUNT_LENGTH;
9209
9431
  exports.NonceAccount = NonceAccount;