@solana/web3.js 1.56.2 → 1.57.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.
@@ -143,12 +143,14 @@ const PUBLIC_KEY_LENGTH = 32;
143
143
 
144
144
  function isPublicKeyData(value) {
145
145
  return value._bn !== undefined;
146
- }
146
+ } // local counter used by PublicKey.unique()
147
+
148
+
149
+ let uniquePublicKeyCounter = 1;
147
150
  /**
148
151
  * A public key
149
152
  */
150
153
 
151
-
152
154
  class PublicKey extends Struct {
153
155
  /** @internal */
154
156
 
@@ -181,6 +183,16 @@ class PublicKey extends Struct {
181
183
  }
182
184
  }
183
185
  }
186
+ /**
187
+ * Returns a unique PublicKey for tests and benchmarks using acounter
188
+ */
189
+
190
+
191
+ static unique() {
192
+ const key = new PublicKey(uniquePublicKeyCounter);
193
+ uniquePublicKeyCounter += 1;
194
+ return new PublicKey(key.toBuffer());
195
+ }
184
196
  /**
185
197
  * Default public key value. (All zeros)
186
198
  */
@@ -437,6 +449,71 @@ Object.defineProperty(TransactionExpiredTimeoutError.prototype, 'name', {
437
449
  value: 'TransactionExpiredTimeoutError'
438
450
  });
439
451
 
452
+ class MessageAccountKeys {
453
+ constructor(staticAccountKeys, accountKeysFromLookups) {
454
+ this.staticAccountKeys = void 0;
455
+ this.accountKeysFromLookups = void 0;
456
+ this.staticAccountKeys = staticAccountKeys;
457
+ this.accountKeysFromLookups = accountKeysFromLookups;
458
+ }
459
+
460
+ keySegments() {
461
+ const keySegments = [this.staticAccountKeys];
462
+
463
+ if (this.accountKeysFromLookups) {
464
+ keySegments.push(this.accountKeysFromLookups.writable);
465
+ keySegments.push(this.accountKeysFromLookups.readonly);
466
+ }
467
+
468
+ return keySegments;
469
+ }
470
+
471
+ get(index) {
472
+ for (const keySegment of this.keySegments()) {
473
+ if (index < keySegment.length) {
474
+ return keySegment[index];
475
+ } else {
476
+ index -= keySegment.length;
477
+ }
478
+ }
479
+
480
+ return;
481
+ }
482
+
483
+ get length() {
484
+ return this.keySegments().flat().length;
485
+ }
486
+
487
+ compileInstructions(instructions) {
488
+ // Bail early if any account indexes would overflow a u8
489
+ const U8_MAX = 255;
490
+
491
+ if (this.length > U8_MAX + 1) {
492
+ throw new Error('Account index overflow encountered during compilation');
493
+ }
494
+
495
+ const keyIndexMap = new Map();
496
+ this.keySegments().flat().forEach((key, index) => {
497
+ keyIndexMap.set(key.toBase58(), index);
498
+ });
499
+
500
+ const findKeyIndex = key => {
501
+ const keyIndex = keyIndexMap.get(key.toBase58());
502
+ if (keyIndex === undefined) throw new Error('Encountered an unknown instruction account key during compilation');
503
+ return keyIndex;
504
+ };
505
+
506
+ return instructions.map(instruction => {
507
+ return {
508
+ programIdIndex: findKeyIndex(instruction.programId),
509
+ accountKeyIndexes: instruction.keys.map(meta => findKeyIndex(meta.pubkey)),
510
+ data: instruction.data
511
+ };
512
+ });
513
+ }
514
+
515
+ }
516
+
440
517
  /**
441
518
  * Layout for a public key
442
519
  */
@@ -733,6 +810,115 @@ function assert (condition, message) {
733
810
  }
734
811
  }
735
812
 
813
+ class CompiledKeys {
814
+ constructor(payer, keyMetaMap) {
815
+ this.payer = void 0;
816
+ this.keyMetaMap = void 0;
817
+ this.payer = payer;
818
+ this.keyMetaMap = keyMetaMap;
819
+ }
820
+
821
+ static compile(instructions, payer) {
822
+ const keyMetaMap = new Map();
823
+
824
+ const getOrInsertDefault = pubkey => {
825
+ const address = pubkey.toBase58();
826
+ let keyMeta = keyMetaMap.get(address);
827
+
828
+ if (keyMeta === undefined) {
829
+ keyMeta = {
830
+ isSigner: false,
831
+ isWritable: false,
832
+ isInvoked: false
833
+ };
834
+ keyMetaMap.set(address, keyMeta);
835
+ }
836
+
837
+ return keyMeta;
838
+ };
839
+
840
+ const payerKeyMeta = getOrInsertDefault(payer);
841
+ payerKeyMeta.isSigner = true;
842
+ payerKeyMeta.isWritable = true;
843
+
844
+ for (const ix of instructions) {
845
+ getOrInsertDefault(ix.programId).isInvoked = true;
846
+
847
+ for (const accountMeta of ix.keys) {
848
+ const keyMeta = getOrInsertDefault(accountMeta.pubkey);
849
+ keyMeta.isSigner || (keyMeta.isSigner = accountMeta.isSigner);
850
+ keyMeta.isWritable || (keyMeta.isWritable = accountMeta.isWritable);
851
+ }
852
+ }
853
+
854
+ return new CompiledKeys(payer, keyMetaMap);
855
+ }
856
+
857
+ getMessageComponents() {
858
+ const mapEntries = [...this.keyMetaMap.entries()];
859
+ assert(mapEntries.length <= 256, 'Max static account keys length exceeded');
860
+ const writableSigners = mapEntries.filter(([, meta]) => meta.isSigner && meta.isWritable);
861
+ const readonlySigners = mapEntries.filter(([, meta]) => meta.isSigner && !meta.isWritable);
862
+ const writableNonSigners = mapEntries.filter(([, meta]) => !meta.isSigner && meta.isWritable);
863
+ const readonlyNonSigners = mapEntries.filter(([, meta]) => !meta.isSigner && !meta.isWritable);
864
+ const header = {
865
+ numRequiredSignatures: writableSigners.length + readonlySigners.length,
866
+ numReadonlySignedAccounts: readonlySigners.length,
867
+ numReadonlyUnsignedAccounts: readonlyNonSigners.length
868
+ }; // sanity checks
869
+
870
+ {
871
+ assert(writableSigners.length > 0, 'Expected at least one writable signer key');
872
+ const [payerAddress] = writableSigners[0];
873
+ assert(payerAddress === this.payer.toBase58(), 'Expected first writable signer key to be the fee payer');
874
+ }
875
+ 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))];
876
+ return [header, staticAccountKeys];
877
+ }
878
+
879
+ extractTableLookup(lookupTable) {
880
+ const [writableIndexes, drainedWritableKeys] = this.drainKeysFoundInLookupTable(lookupTable.state.addresses, keyMeta => !keyMeta.isSigner && !keyMeta.isInvoked && keyMeta.isWritable);
881
+ const [readonlyIndexes, drainedReadonlyKeys] = this.drainKeysFoundInLookupTable(lookupTable.state.addresses, keyMeta => !keyMeta.isSigner && !keyMeta.isInvoked && !keyMeta.isWritable); // Don't extract lookup if no keys were found
882
+
883
+ if (writableIndexes.length === 0 && readonlyIndexes.length === 0) {
884
+ return;
885
+ }
886
+
887
+ return [{
888
+ accountKey: lookupTable.key,
889
+ writableIndexes,
890
+ readonlyIndexes
891
+ }, {
892
+ writable: drainedWritableKeys,
893
+ readonly: drainedReadonlyKeys
894
+ }];
895
+ }
896
+ /** @internal */
897
+
898
+
899
+ drainKeysFoundInLookupTable(lookupTableEntries, keyMetaFilter) {
900
+ const lookupTableIndexes = new Array();
901
+ const drainedKeys = new Array();
902
+
903
+ for (const [address, keyMeta] of this.keyMetaMap.entries()) {
904
+ if (keyMetaFilter(keyMeta)) {
905
+ const key = new PublicKey(address);
906
+ const lookupTableIndex = lookupTableEntries.findIndex(entry => entry.equals(key));
907
+
908
+ if (lookupTableIndex >= 0) {
909
+ assert(lookupTableIndex < 256, 'Max lookup table index exceeded');
910
+ lookupTableIndexes.push(lookupTableIndex);
911
+ drainedKeys.push(key);
912
+ this.keyMetaMap.delete(address);
913
+ }
914
+ }
915
+ }
916
+
917
+ return [lookupTableIndexes, drainedKeys];
918
+ }
919
+
920
+ }
921
+
736
922
  /**
737
923
  * Message constructor arguments
738
924
  */
@@ -755,6 +941,41 @@ class MessageV0 {
755
941
  return 0;
756
942
  }
757
943
 
944
+ static compile(args) {
945
+ const compiledKeys = CompiledKeys.compile(args.instructions, args.payerKey);
946
+ const addressTableLookups = new Array();
947
+ const accountKeysFromLookups = {
948
+ writable: new Array(),
949
+ readonly: new Array()
950
+ };
951
+ const lookupTableAccounts = args.addressLookupTableAccounts || [];
952
+
953
+ for (const lookupTable of lookupTableAccounts) {
954
+ const extractResult = compiledKeys.extractTableLookup(lookupTable);
955
+
956
+ if (extractResult !== undefined) {
957
+ const [addressTableLookup, {
958
+ writable,
959
+ readonly
960
+ }] = extractResult;
961
+ addressTableLookups.push(addressTableLookup);
962
+ accountKeysFromLookups.writable.push(...writable);
963
+ accountKeysFromLookups.readonly.push(...readonly);
964
+ }
965
+ }
966
+
967
+ const [header, staticAccountKeys] = compiledKeys.getMessageComponents();
968
+ const accountKeys = new MessageAccountKeys(staticAccountKeys, accountKeysFromLookups);
969
+ const compiledInstructions = accountKeys.compileInstructions(args.instructions);
970
+ return new MessageV0({
971
+ header,
972
+ staticAccountKeys,
973
+ recentBlockhash: args.recentBlockhash,
974
+ compiledInstructions,
975
+ addressTableLookups
976
+ });
977
+ }
978
+
758
979
  serialize() {
759
980
  const encodedStaticAccountKeysLength = Array();
760
981
  encodeLength(encodedStaticAccountKeysLength, this.staticAccountKeys.length);
@@ -9107,6 +9328,7 @@ exports.Loader = Loader;
9107
9328
  exports.Lockup = Lockup;
9108
9329
  exports.MAX_SEED_LENGTH = MAX_SEED_LENGTH;
9109
9330
  exports.Message = Message;
9331
+ exports.MessageAccountKeys = MessageAccountKeys;
9110
9332
  exports.MessageV0 = MessageV0;
9111
9333
  exports.NONCE_ACCOUNT_LENGTH = NONCE_ACCOUNT_LENGTH;
9112
9334
  exports.NonceAccount = NonceAccount;