@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.
package/lib/index.iife.js CHANGED
@@ -8308,12 +8308,14 @@ var solanaWeb3 = (function (exports) {
8308
8308
 
8309
8309
  function isPublicKeyData(value) {
8310
8310
  return value._bn !== undefined;
8311
- }
8311
+ } // local counter used by PublicKey.unique()
8312
+
8313
+
8314
+ let uniquePublicKeyCounter = 1;
8312
8315
  /**
8313
8316
  * A public key
8314
8317
  */
8315
8318
 
8316
-
8317
8319
  class PublicKey extends Struct$1 {
8318
8320
  /** @internal */
8319
8321
 
@@ -8346,6 +8348,16 @@ var solanaWeb3 = (function (exports) {
8346
8348
  }
8347
8349
  }
8348
8350
  }
8351
+ /**
8352
+ * Returns a unique PublicKey for tests and benchmarks using acounter
8353
+ */
8354
+
8355
+
8356
+ static unique() {
8357
+ const key = new PublicKey(uniquePublicKeyCounter);
8358
+ uniquePublicKeyCounter += 1;
8359
+ return new PublicKey(key.toBuffer());
8360
+ }
8349
8361
  /**
8350
8362
  * Default public key value. (All zeros)
8351
8363
  */
@@ -10885,6 +10897,71 @@ var solanaWeb3 = (function (exports) {
10885
10897
  value: 'TransactionExpiredTimeoutError'
10886
10898
  });
10887
10899
 
10900
+ class MessageAccountKeys {
10901
+ constructor(staticAccountKeys, accountKeysFromLookups) {
10902
+ this.staticAccountKeys = void 0;
10903
+ this.accountKeysFromLookups = void 0;
10904
+ this.staticAccountKeys = staticAccountKeys;
10905
+ this.accountKeysFromLookups = accountKeysFromLookups;
10906
+ }
10907
+
10908
+ keySegments() {
10909
+ const keySegments = [this.staticAccountKeys];
10910
+
10911
+ if (this.accountKeysFromLookups) {
10912
+ keySegments.push(this.accountKeysFromLookups.writable);
10913
+ keySegments.push(this.accountKeysFromLookups.readonly);
10914
+ }
10915
+
10916
+ return keySegments;
10917
+ }
10918
+
10919
+ get(index) {
10920
+ for (const keySegment of this.keySegments()) {
10921
+ if (index < keySegment.length) {
10922
+ return keySegment[index];
10923
+ } else {
10924
+ index -= keySegment.length;
10925
+ }
10926
+ }
10927
+
10928
+ return;
10929
+ }
10930
+
10931
+ get length() {
10932
+ return this.keySegments().flat().length;
10933
+ }
10934
+
10935
+ compileInstructions(instructions) {
10936
+ // Bail early if any account indexes would overflow a u8
10937
+ const U8_MAX = 255;
10938
+
10939
+ if (this.length > U8_MAX + 1) {
10940
+ throw new Error('Account index overflow encountered during compilation');
10941
+ }
10942
+
10943
+ const keyIndexMap = new Map();
10944
+ this.keySegments().flat().forEach((key, index) => {
10945
+ keyIndexMap.set(key.toBase58(), index);
10946
+ });
10947
+
10948
+ const findKeyIndex = key => {
10949
+ const keyIndex = keyIndexMap.get(key.toBase58());
10950
+ if (keyIndex === undefined) throw new Error('Encountered an unknown instruction account key during compilation');
10951
+ return keyIndex;
10952
+ };
10953
+
10954
+ return instructions.map(instruction => {
10955
+ return {
10956
+ programIdIndex: findKeyIndex(instruction.programId),
10957
+ accountKeyIndexes: instruction.keys.map(meta => findKeyIndex(meta.pubkey)),
10958
+ data: instruction.data
10959
+ };
10960
+ });
10961
+ }
10962
+
10963
+ }
10964
+
10888
10965
  /**
10889
10966
  * Layout for a public key
10890
10967
  */
@@ -11181,6 +11258,115 @@ var solanaWeb3 = (function (exports) {
11181
11258
  }
11182
11259
  }
11183
11260
 
11261
+ class CompiledKeys {
11262
+ constructor(payer, keyMetaMap) {
11263
+ this.payer = void 0;
11264
+ this.keyMetaMap = void 0;
11265
+ this.payer = payer;
11266
+ this.keyMetaMap = keyMetaMap;
11267
+ }
11268
+
11269
+ static compile(instructions, payer) {
11270
+ const keyMetaMap = new Map();
11271
+
11272
+ const getOrInsertDefault = pubkey => {
11273
+ const address = pubkey.toBase58();
11274
+ let keyMeta = keyMetaMap.get(address);
11275
+
11276
+ if (keyMeta === undefined) {
11277
+ keyMeta = {
11278
+ isSigner: false,
11279
+ isWritable: false,
11280
+ isInvoked: false
11281
+ };
11282
+ keyMetaMap.set(address, keyMeta);
11283
+ }
11284
+
11285
+ return keyMeta;
11286
+ };
11287
+
11288
+ const payerKeyMeta = getOrInsertDefault(payer);
11289
+ payerKeyMeta.isSigner = true;
11290
+ payerKeyMeta.isWritable = true;
11291
+
11292
+ for (const ix of instructions) {
11293
+ getOrInsertDefault(ix.programId).isInvoked = true;
11294
+
11295
+ for (const accountMeta of ix.keys) {
11296
+ const keyMeta = getOrInsertDefault(accountMeta.pubkey);
11297
+ keyMeta.isSigner || (keyMeta.isSigner = accountMeta.isSigner);
11298
+ keyMeta.isWritable || (keyMeta.isWritable = accountMeta.isWritable);
11299
+ }
11300
+ }
11301
+
11302
+ return new CompiledKeys(payer, keyMetaMap);
11303
+ }
11304
+
11305
+ getMessageComponents() {
11306
+ const mapEntries = [...this.keyMetaMap.entries()];
11307
+ assert$1(mapEntries.length <= 256, 'Max static account keys length exceeded');
11308
+ const writableSigners = mapEntries.filter(([, meta]) => meta.isSigner && meta.isWritable);
11309
+ const readonlySigners = mapEntries.filter(([, meta]) => meta.isSigner && !meta.isWritable);
11310
+ const writableNonSigners = mapEntries.filter(([, meta]) => !meta.isSigner && meta.isWritable);
11311
+ const readonlyNonSigners = mapEntries.filter(([, meta]) => !meta.isSigner && !meta.isWritable);
11312
+ const header = {
11313
+ numRequiredSignatures: writableSigners.length + readonlySigners.length,
11314
+ numReadonlySignedAccounts: readonlySigners.length,
11315
+ numReadonlyUnsignedAccounts: readonlyNonSigners.length
11316
+ }; // sanity checks
11317
+
11318
+ {
11319
+ assert$1(writableSigners.length > 0, 'Expected at least one writable signer key');
11320
+ const [payerAddress] = writableSigners[0];
11321
+ assert$1(payerAddress === this.payer.toBase58(), 'Expected first writable signer key to be the fee payer');
11322
+ }
11323
+ 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))];
11324
+ return [header, staticAccountKeys];
11325
+ }
11326
+
11327
+ extractTableLookup(lookupTable) {
11328
+ const [writableIndexes, drainedWritableKeys] = this.drainKeysFoundInLookupTable(lookupTable.state.addresses, keyMeta => !keyMeta.isSigner && !keyMeta.isInvoked && keyMeta.isWritable);
11329
+ const [readonlyIndexes, drainedReadonlyKeys] = this.drainKeysFoundInLookupTable(lookupTable.state.addresses, keyMeta => !keyMeta.isSigner && !keyMeta.isInvoked && !keyMeta.isWritable); // Don't extract lookup if no keys were found
11330
+
11331
+ if (writableIndexes.length === 0 && readonlyIndexes.length === 0) {
11332
+ return;
11333
+ }
11334
+
11335
+ return [{
11336
+ accountKey: lookupTable.key,
11337
+ writableIndexes,
11338
+ readonlyIndexes
11339
+ }, {
11340
+ writable: drainedWritableKeys,
11341
+ readonly: drainedReadonlyKeys
11342
+ }];
11343
+ }
11344
+ /** @internal */
11345
+
11346
+
11347
+ drainKeysFoundInLookupTable(lookupTableEntries, keyMetaFilter) {
11348
+ const lookupTableIndexes = new Array();
11349
+ const drainedKeys = new Array();
11350
+
11351
+ for (const [address, keyMeta] of this.keyMetaMap.entries()) {
11352
+ if (keyMetaFilter(keyMeta)) {
11353
+ const key = new PublicKey(address);
11354
+ const lookupTableIndex = lookupTableEntries.findIndex(entry => entry.equals(key));
11355
+
11356
+ if (lookupTableIndex >= 0) {
11357
+ assert$1(lookupTableIndex < 256, 'Max lookup table index exceeded');
11358
+ lookupTableIndexes.push(lookupTableIndex);
11359
+ drainedKeys.push(key);
11360
+ this.keyMetaMap.delete(address);
11361
+ }
11362
+ }
11363
+ }
11364
+
11365
+ return [lookupTableIndexes, drainedKeys];
11366
+ }
11367
+
11368
+ }
11369
+
11184
11370
  /**
11185
11371
  * Message constructor arguments
11186
11372
  */
@@ -11203,6 +11389,41 @@ var solanaWeb3 = (function (exports) {
11203
11389
  return 0;
11204
11390
  }
11205
11391
 
11392
+ static compile(args) {
11393
+ const compiledKeys = CompiledKeys.compile(args.instructions, args.payerKey);
11394
+ const addressTableLookups = new Array();
11395
+ const accountKeysFromLookups = {
11396
+ writable: new Array(),
11397
+ readonly: new Array()
11398
+ };
11399
+ const lookupTableAccounts = args.addressLookupTableAccounts || [];
11400
+
11401
+ for (const lookupTable of lookupTableAccounts) {
11402
+ const extractResult = compiledKeys.extractTableLookup(lookupTable);
11403
+
11404
+ if (extractResult !== undefined) {
11405
+ const [addressTableLookup, {
11406
+ writable,
11407
+ readonly
11408
+ }] = extractResult;
11409
+ addressTableLookups.push(addressTableLookup);
11410
+ accountKeysFromLookups.writable.push(...writable);
11411
+ accountKeysFromLookups.readonly.push(...readonly);
11412
+ }
11413
+ }
11414
+
11415
+ const [header, staticAccountKeys] = compiledKeys.getMessageComponents();
11416
+ const accountKeys = new MessageAccountKeys(staticAccountKeys, accountKeysFromLookups);
11417
+ const compiledInstructions = accountKeys.compileInstructions(args.instructions);
11418
+ return new MessageV0({
11419
+ header,
11420
+ staticAccountKeys,
11421
+ recentBlockhash: args.recentBlockhash,
11422
+ compiledInstructions,
11423
+ addressTableLookups
11424
+ });
11425
+ }
11426
+
11206
11427
  serialize() {
11207
11428
  const encodedStaticAccountKeysLength = Array();
11208
11429
  encodeLength(encodedStaticAccountKeysLength, this.staticAccountKeys.length);
@@ -24392,6 +24613,7 @@ var solanaWeb3 = (function (exports) {
24392
24613
  exports.Lockup = Lockup;
24393
24614
  exports.MAX_SEED_LENGTH = MAX_SEED_LENGTH;
24394
24615
  exports.Message = Message;
24616
+ exports.MessageAccountKeys = MessageAccountKeys;
24395
24617
  exports.MessageV0 = MessageV0;
24396
24618
  exports.NONCE_ACCOUNT_LENGTH = NONCE_ACCOUNT_LENGTH;
24397
24619
  exports.NonceAccount = NonceAccount;