@solana/web3.js 1.56.2 → 1.59.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.
@@ -11,7 +11,7 @@ import { toBigIntLE, toBufferLE } from 'bigint-buffer';
11
11
  import { coerce, instance, string, tuple, literal, unknown, union, type, optional, any, number, array, nullable, create, boolean, record, assert as assert$1 } from 'superstruct';
12
12
  import { Client } from 'rpc-websockets';
13
13
  import RpcClient from 'jayson/lib/client/browser';
14
- import sha3 from 'js-sha3';
14
+ import { keccak_256 } from '@noble/hashes/sha3';
15
15
  import { hmac } from '@noble/hashes/hmac';
16
16
  import * as secp256k1 from '@noble/secp256k1';
17
17
 
@@ -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,10 +418,74 @@ 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
  */
412
-
413
489
  const publicKey = (property = 'publicKey') => {
414
490
  return BufferLayout.blob(32, property);
415
491
  };
@@ -473,6 +549,13 @@ const lockup = (property = 'lockup') => {
473
549
  const voteInit = (property = 'voteInit') => {
474
550
  return BufferLayout.struct([publicKey('nodePubkey'), publicKey('authorizedVoter'), publicKey('authorizedWithdrawer'), BufferLayout.u8('commission')], property);
475
551
  };
552
+ /**
553
+ * Layout for a VoteAuthorizeWithSeedArgs object
554
+ */
555
+
556
+ const voteAuthorizeWithSeedArgs = (property = 'voteAuthorizeWithSeedArgs') => {
557
+ return BufferLayout.struct([BufferLayout.u32('voteAuthorizationType'), publicKey('currentAuthorityDerivedKeyOwnerPubkey'), rustString('currentAuthorityDerivedKeySeed'), publicKey('newAuthorized')], property);
558
+ };
476
559
  function getAlloc(type, fields) {
477
560
  const getItemAlloc = item => {
478
561
  if (item.span >= 0) {
@@ -485,6 +568,11 @@ function getAlloc(type, fields) {
485
568
  if (Array.isArray(field)) {
486
569
  return field.length * getItemAlloc(item.elementLayout);
487
570
  }
571
+ } else if ('fields' in item) {
572
+ // This is a `Structure` whose size needs to be recursively measured.
573
+ return getAlloc({
574
+ layout: item
575
+ }, fields[item.property]);
488
576
  } // Couldn't determine allocated size of layout
489
577
 
490
578
 
@@ -531,6 +619,129 @@ function encodeLength(bytes, len) {
531
619
  }
532
620
  }
533
621
 
622
+ function assert (condition, message) {
623
+ if (!condition) {
624
+ throw new Error(message || 'Assertion failed');
625
+ }
626
+ }
627
+
628
+ class CompiledKeys {
629
+ constructor(payer, keyMetaMap) {
630
+ this.payer = void 0;
631
+ this.keyMetaMap = void 0;
632
+ this.payer = payer;
633
+ this.keyMetaMap = keyMetaMap;
634
+ }
635
+
636
+ static compile(instructions, payer) {
637
+ const keyMetaMap = new Map();
638
+
639
+ const getOrInsertDefault = pubkey => {
640
+ const address = pubkey.toBase58();
641
+ let keyMeta = keyMetaMap.get(address);
642
+
643
+ if (keyMeta === undefined) {
644
+ keyMeta = {
645
+ isSigner: false,
646
+ isWritable: false,
647
+ isInvoked: false
648
+ };
649
+ keyMetaMap.set(address, keyMeta);
650
+ }
651
+
652
+ return keyMeta;
653
+ };
654
+
655
+ const payerKeyMeta = getOrInsertDefault(payer);
656
+ payerKeyMeta.isSigner = true;
657
+ payerKeyMeta.isWritable = true;
658
+
659
+ for (const ix of instructions) {
660
+ getOrInsertDefault(ix.programId).isInvoked = true;
661
+
662
+ for (const accountMeta of ix.keys) {
663
+ const keyMeta = getOrInsertDefault(accountMeta.pubkey);
664
+ keyMeta.isSigner || (keyMeta.isSigner = accountMeta.isSigner);
665
+ keyMeta.isWritable || (keyMeta.isWritable = accountMeta.isWritable);
666
+ }
667
+ }
668
+
669
+ return new CompiledKeys(payer, keyMetaMap);
670
+ }
671
+
672
+ getMessageComponents() {
673
+ const mapEntries = [...this.keyMetaMap.entries()];
674
+ assert(mapEntries.length <= 256, 'Max static account keys length exceeded');
675
+ const writableSigners = mapEntries.filter(([, meta]) => meta.isSigner && meta.isWritable);
676
+ const readonlySigners = mapEntries.filter(([, meta]) => meta.isSigner && !meta.isWritable);
677
+ const writableNonSigners = mapEntries.filter(([, meta]) => !meta.isSigner && meta.isWritable);
678
+ const readonlyNonSigners = mapEntries.filter(([, meta]) => !meta.isSigner && !meta.isWritable);
679
+ const header = {
680
+ numRequiredSignatures: writableSigners.length + readonlySigners.length,
681
+ numReadonlySignedAccounts: readonlySigners.length,
682
+ numReadonlyUnsignedAccounts: readonlyNonSigners.length
683
+ }; // sanity checks
684
+
685
+ {
686
+ assert(writableSigners.length > 0, 'Expected at least one writable signer key');
687
+ const [payerAddress] = writableSigners[0];
688
+ assert(payerAddress === this.payer.toBase58(), 'Expected first writable signer key to be the fee payer');
689
+ }
690
+ 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))];
691
+ return [header, staticAccountKeys];
692
+ }
693
+
694
+ extractTableLookup(lookupTable) {
695
+ const [writableIndexes, drainedWritableKeys] = this.drainKeysFoundInLookupTable(lookupTable.state.addresses, keyMeta => !keyMeta.isSigner && !keyMeta.isInvoked && keyMeta.isWritable);
696
+ const [readonlyIndexes, drainedReadonlyKeys] = this.drainKeysFoundInLookupTable(lookupTable.state.addresses, keyMeta => !keyMeta.isSigner && !keyMeta.isInvoked && !keyMeta.isWritable); // Don't extract lookup if no keys were found
697
+
698
+ if (writableIndexes.length === 0 && readonlyIndexes.length === 0) {
699
+ return;
700
+ }
701
+
702
+ return [{
703
+ accountKey: lookupTable.key,
704
+ writableIndexes,
705
+ readonlyIndexes
706
+ }, {
707
+ writable: drainedWritableKeys,
708
+ readonly: drainedReadonlyKeys
709
+ }];
710
+ }
711
+ /** @internal */
712
+
713
+
714
+ drainKeysFoundInLookupTable(lookupTableEntries, keyMetaFilter) {
715
+ const lookupTableIndexes = new Array();
716
+ const drainedKeys = new Array();
717
+
718
+ for (const [address, keyMeta] of this.keyMetaMap.entries()) {
719
+ if (keyMetaFilter(keyMeta)) {
720
+ const key = new PublicKey(address);
721
+ const lookupTableIndex = lookupTableEntries.findIndex(entry => entry.equals(key));
722
+
723
+ if (lookupTableIndex >= 0) {
724
+ assert(lookupTableIndex < 256, 'Max lookup table index exceeded');
725
+ lookupTableIndexes.push(lookupTableIndex);
726
+ drainedKeys.push(key);
727
+ this.keyMetaMap.delete(address);
728
+ }
729
+ }
730
+ }
731
+
732
+ return [lookupTableIndexes, drainedKeys];
733
+ }
734
+
735
+ }
736
+
737
+ /**
738
+ * An instruction to execute by a program
739
+ *
740
+ * @property {number} programIdIndex
741
+ * @property {number[]} accounts
742
+ * @property {string} data
743
+ */
744
+
534
745
  /**
535
746
  * List of instructions to be processed atomically
536
747
  */
@@ -568,6 +779,27 @@ class Message {
568
779
  return [];
569
780
  }
570
781
 
782
+ getAccountKeys() {
783
+ return new MessageAccountKeys(this.staticAccountKeys);
784
+ }
785
+
786
+ static compile(args) {
787
+ const compiledKeys = CompiledKeys.compile(args.instructions, args.payerKey);
788
+ const [header, staticAccountKeys] = compiledKeys.getMessageComponents();
789
+ const accountKeys = new MessageAccountKeys(staticAccountKeys);
790
+ const instructions = accountKeys.compileInstructions(args.instructions).map(ix => ({
791
+ programIdIndex: ix.programIdIndex,
792
+ accounts: ix.accountKeyIndexes,
793
+ data: bs58.encode(ix.data)
794
+ }));
795
+ return new Message({
796
+ header,
797
+ accountKeys: staticAccountKeys,
798
+ recentBlockhash: args.recentBlockhash,
799
+ instructions
800
+ });
801
+ }
802
+
571
803
  isAccountSigner(index) {
572
804
  return index < this.header.numRequiredSignatures;
573
805
  }
@@ -657,7 +889,7 @@ class Message {
657
889
  for (let i = 0; i < accountCount; i++) {
658
890
  const account = byteArray.slice(0, PUBLIC_KEY_LENGTH);
659
891
  byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
660
- accountKeys.push(bs58.encode(Buffer.from(account)));
892
+ accountKeys.push(new PublicKey(Buffer.from(account)));
661
893
  }
662
894
 
663
895
  const recentBlockhash = byteArray.slice(0, PUBLIC_KEY_LENGTH);
@@ -696,12 +928,6 @@ class Message {
696
928
 
697
929
  }
698
930
 
699
- function assert (condition, message) {
700
- if (!condition) {
701
- throw new Error(message || 'Assertion failed');
702
- }
703
- }
704
-
705
931
  /**
706
932
  * Message constructor arguments
707
933
  */
@@ -724,6 +950,102 @@ class MessageV0 {
724
950
  return 0;
725
951
  }
726
952
 
953
+ get numAccountKeysFromLookups() {
954
+ let count = 0;
955
+
956
+ for (const lookup of this.addressTableLookups) {
957
+ count += lookup.readonlyIndexes.length + lookup.writableIndexes.length;
958
+ }
959
+
960
+ return count;
961
+ }
962
+
963
+ getAccountKeys(args) {
964
+ let accountKeysFromLookups;
965
+
966
+ if (args && 'accountKeysFromLookups' in args) {
967
+ if (this.numAccountKeysFromLookups != args.accountKeysFromLookups.writable.length + args.accountKeysFromLookups.readonly.length) {
968
+ throw new Error('Failed to get account keys because of a mismatch in the number of account keys from lookups');
969
+ }
970
+
971
+ accountKeysFromLookups = args.accountKeysFromLookups;
972
+ } else if (args && 'addressLookupTableAccounts' in args) {
973
+ accountKeysFromLookups = this.resolveAddressTableLookups(args.addressLookupTableAccounts);
974
+ } else if (this.addressTableLookups.length > 0) {
975
+ throw new Error('Failed to get account keys because address table lookups were not resolved');
976
+ }
977
+
978
+ return new MessageAccountKeys(this.staticAccountKeys, accountKeysFromLookups);
979
+ }
980
+
981
+ resolveAddressTableLookups(addressLookupTableAccounts) {
982
+ const accountKeysFromLookups = {
983
+ writable: [],
984
+ readonly: []
985
+ };
986
+
987
+ for (const tableLookup of this.addressTableLookups) {
988
+ const tableAccount = addressLookupTableAccounts.find(account => account.key.equals(tableLookup.accountKey));
989
+
990
+ if (!tableAccount) {
991
+ throw new Error(`Failed to find address lookup table account for table key ${tableLookup.accountKey.toBase58()}`);
992
+ }
993
+
994
+ for (const index of tableLookup.writableIndexes) {
995
+ if (index < tableAccount.state.addresses.length) {
996
+ accountKeysFromLookups.writable.push(tableAccount.state.addresses[index]);
997
+ } else {
998
+ throw new Error(`Failed to find address for index ${index} in address lookup table ${tableLookup.accountKey.toBase58()}`);
999
+ }
1000
+ }
1001
+
1002
+ for (const index of tableLookup.readonlyIndexes) {
1003
+ if (index < tableAccount.state.addresses.length) {
1004
+ accountKeysFromLookups.readonly.push(tableAccount.state.addresses[index]);
1005
+ } else {
1006
+ throw new Error(`Failed to find address for index ${index} in address lookup table ${tableLookup.accountKey.toBase58()}`);
1007
+ }
1008
+ }
1009
+ }
1010
+
1011
+ return accountKeysFromLookups;
1012
+ }
1013
+
1014
+ static compile(args) {
1015
+ const compiledKeys = CompiledKeys.compile(args.instructions, args.payerKey);
1016
+ const addressTableLookups = new Array();
1017
+ const accountKeysFromLookups = {
1018
+ writable: new Array(),
1019
+ readonly: new Array()
1020
+ };
1021
+ const lookupTableAccounts = args.addressLookupTableAccounts || [];
1022
+
1023
+ for (const lookupTable of lookupTableAccounts) {
1024
+ const extractResult = compiledKeys.extractTableLookup(lookupTable);
1025
+
1026
+ if (extractResult !== undefined) {
1027
+ const [addressTableLookup, {
1028
+ writable,
1029
+ readonly
1030
+ }] = extractResult;
1031
+ addressTableLookups.push(addressTableLookup);
1032
+ accountKeysFromLookups.writable.push(...writable);
1033
+ accountKeysFromLookups.readonly.push(...readonly);
1034
+ }
1035
+ }
1036
+
1037
+ const [header, staticAccountKeys] = compiledKeys.getMessageComponents();
1038
+ const accountKeys = new MessageAccountKeys(staticAccountKeys, accountKeysFromLookups);
1039
+ const compiledInstructions = accountKeys.compileInstructions(args.instructions);
1040
+ return new MessageV0({
1041
+ header,
1042
+ staticAccountKeys,
1043
+ recentBlockhash: args.recentBlockhash,
1044
+ compiledInstructions,
1045
+ addressTableLookups
1046
+ });
1047
+ }
1048
+
727
1049
  serialize() {
728
1050
  const encodedStaticAccountKeysLength = Array();
729
1051
  encodeLength(encodedStaticAccountKeysLength, this.staticAccountKeys.length);
@@ -1618,6 +1940,114 @@ class Transaction {
1618
1940
 
1619
1941
  }
1620
1942
 
1943
+ class TransactionMessage {
1944
+ constructor(args) {
1945
+ this.accountKeys = void 0;
1946
+ this.instructions = void 0;
1947
+ this.recentBlockhash = void 0;
1948
+ this.accountKeys = args.accountKeys;
1949
+ this.instructions = args.instructions;
1950
+ this.recentBlockhash = args.recentBlockhash;
1951
+ }
1952
+
1953
+ static decompile(message, args) {
1954
+ const {
1955
+ header,
1956
+ compiledInstructions,
1957
+ recentBlockhash
1958
+ } = message;
1959
+ const {
1960
+ numRequiredSignatures,
1961
+ numReadonlySignedAccounts,
1962
+ numReadonlyUnsignedAccounts
1963
+ } = header;
1964
+ const numWritableSignedAccounts = numRequiredSignatures - numReadonlySignedAccounts;
1965
+ assert(numWritableSignedAccounts > 0, 'Message header is invalid');
1966
+ const numWritableUnsignedAccounts = message.staticAccountKeys.length - numReadonlyUnsignedAccounts;
1967
+ assert(numWritableUnsignedAccounts >= 0, 'Message header is invalid');
1968
+ const accountKeys = message.getAccountKeys(args);
1969
+ const instructions = [];
1970
+
1971
+ for (const compiledIx of compiledInstructions) {
1972
+ const keys = [];
1973
+
1974
+ for (const keyIndex of compiledIx.accountKeyIndexes) {
1975
+ const pubkey = accountKeys.get(keyIndex);
1976
+
1977
+ if (pubkey === undefined) {
1978
+ throw new Error(`Failed to find key for account key index ${keyIndex}`);
1979
+ }
1980
+
1981
+ const isSigner = keyIndex < numRequiredSignatures;
1982
+ let isWritable;
1983
+
1984
+ if (isSigner) {
1985
+ isWritable = keyIndex < numWritableSignedAccounts;
1986
+ } else if (keyIndex < accountKeys.staticAccountKeys.length) {
1987
+ isWritable = keyIndex - numRequiredSignatures < numWritableUnsignedAccounts;
1988
+ } else {
1989
+ isWritable = keyIndex - accountKeys.staticAccountKeys.length < // accountKeysFromLookups cannot be undefined because we already found a pubkey for this index above
1990
+ accountKeys.accountKeysFromLookups.writable.length;
1991
+ }
1992
+
1993
+ keys.push({
1994
+ pubkey,
1995
+ isSigner: keyIndex < header.numRequiredSignatures,
1996
+ isWritable
1997
+ });
1998
+ }
1999
+
2000
+ const programId = accountKeys.get(compiledIx.programIdIndex);
2001
+
2002
+ if (programId === undefined) {
2003
+ throw new Error(`Failed to find program id for program id index ${compiledIx.programIdIndex}`);
2004
+ }
2005
+
2006
+ instructions.push(new TransactionInstruction({
2007
+ programId,
2008
+ data: toBuffer(compiledIx.data),
2009
+ keys
2010
+ }));
2011
+ }
2012
+
2013
+ return new TransactionMessage({
2014
+ accountKeys,
2015
+ instructions,
2016
+ recentBlockhash
2017
+ });
2018
+ }
2019
+
2020
+ compileToLegacyMessage() {
2021
+ const payerKey = this.accountKeys.get(0);
2022
+
2023
+ if (payerKey === undefined) {
2024
+ throw new Error('Failed to compile message because no account keys were found');
2025
+ }
2026
+
2027
+ return Message.compile({
2028
+ payerKey,
2029
+ recentBlockhash: this.recentBlockhash,
2030
+ instructions: this.instructions
2031
+ });
2032
+ }
2033
+
2034
+ compileToV0Message(addressLookupTableAccounts) {
2035
+ const payerKey = this.accountKeys.get(0);
2036
+
2037
+ if (payerKey === undefined) {
2038
+ throw new Error('Failed to compile message because no account keys were found');
2039
+ }
2040
+
2041
+ return MessageV0.compile({
2042
+ payerKey,
2043
+ recentBlockhash: this.recentBlockhash,
2044
+ instructions: this.instructions,
2045
+ addressLookupTableAccounts
2046
+ });
2047
+ }
2048
+
2049
+ }
2050
+
1621
2051
  /**
1622
2052
  * Versioned transaction class
1623
2053
  */
@@ -5921,10 +6351,44 @@ class Connection {
5921
6351
  }
5922
6352
  /**
5923
6353
  * Simulate a transaction
6354
+ *
6355
+ * @deprecated Instead, call {@link simulateTransaction} with {@link
6356
+ * VersionedTransaction} and {@link SimulateTransactionConfig} parameters
5924
6357
  */
5925
6358
 
5926
6359
 
5927
- async simulateTransaction(transactionOrMessage, signers, includeAccounts) {
6360
+ /**
6361
+ * Simulate a transaction
6362
+ */
6363
+ // eslint-disable-next-line no-dupe-class-members
6364
+ async simulateTransaction(transactionOrMessage, configOrSigners, includeAccounts) {
6365
+ if ('message' in transactionOrMessage) {
6366
+ const versionedTx = transactionOrMessage;
6367
+ const wireTransaction = versionedTx.serialize();
6368
+ const encodedTransaction = Buffer.from(wireTransaction).toString('base64');
6369
+
6370
+ if (Array.isArray(configOrSigners) || includeAccounts !== undefined) {
6371
+ throw new Error('Invalid arguments');
6372
+ }
6373
+
6374
+ const config = configOrSigners || {};
6375
+ config.encoding = 'base64';
6376
+
6377
+ if (!('commitment' in config)) {
6378
+ config.commitment = this.commitment;
6379
+ }
6380
+
6381
+ const args = [encodedTransaction, config];
6382
+ const unsafeRes = await this._rpcRequest('simulateTransaction', args);
6383
+ const res = create(unsafeRes, SimulatedTransactionResponseStruct);
6384
+
6385
+ if ('error' in res) {
6386
+ throw new Error('failed to simulate transaction: ' + res.error.message);
6387
+ }
6388
+
6389
+ return res.result;
6390
+ }
6391
+
5928
6392
  let transaction;
5929
6393
 
5930
6394
  if (transactionOrMessage instanceof Transaction) {
@@ -5940,6 +6404,12 @@ class Connection {
5940
6404
  transaction._message = transaction._json = undefined;
5941
6405
  }
5942
6406
 
6407
+ if (configOrSigners !== undefined && !Array.isArray(configOrSigners)) {
6408
+ throw new Error('Invalid arguments');
6409
+ }
6410
+
6411
+ const signers = configOrSigners;
6412
+
5943
6413
  if (transaction.nonceInfo && signers) {
5944
6414
  transaction.sign(...signers);
5945
6415
  } else {
@@ -6022,10 +6492,32 @@ class Connection {
6022
6492
  }
6023
6493
  /**
6024
6494
  * Sign and send a transaction
6495
+ *
6496
+ * @deprecated Instead, call {@link sendTransaction} with a {@link
6497
+ * VersionedTransaction}
6025
6498
  */
6026
6499
 
6027
6500
 
6028
- async sendTransaction(transaction, signers, options) {
6501
+ /**
6502
+ * Sign and send a transaction
6503
+ */
6504
+ // eslint-disable-next-line no-dupe-class-members
6505
+ async sendTransaction(transaction, signersOrOptions, options) {
6506
+ if ('message' in transaction) {
6507
+ if (signersOrOptions && Array.isArray(signersOrOptions)) {
6508
+ throw new Error('Invalid arguments');
6509
+ }
6510
+
6511
+ const wireTransaction = transaction.serialize();
6512
+ return await this.sendRawTransaction(wireTransaction, options);
6513
+ }
6514
+
6515
+ if (signersOrOptions === undefined || !Array.isArray(signersOrOptions)) {
6516
+ throw new Error('Invalid arguments');
6517
+ }
6518
+
6519
+ const signers = signersOrOptions;
6520
+
6029
6521
  if (transaction.nonceInfo) {
6030
6522
  transaction.sign(...signers);
6031
6523
  } else {
@@ -7552,7 +8044,7 @@ class Secp256k1Program {
7552
8044
  assert(publicKey.length === PUBLIC_KEY_BYTES, `Public key must be ${PUBLIC_KEY_BYTES} bytes but received ${publicKey.length} bytes`);
7553
8045
 
7554
8046
  try {
7555
- return Buffer.from(sha3.keccak_256.update(toBuffer(publicKey)).digest()).slice(-ETHEREUM_ADDRESS_BYTES);
8047
+ return Buffer.from(keccak_256(toBuffer(publicKey))).slice(-ETHEREUM_ADDRESS_BYTES);
7556
8048
  } catch (error) {
7557
8049
  throw new Error(`Error constructing Ethereum address: ${error}`);
7558
8050
  }
@@ -7652,7 +8144,7 @@ class Secp256k1Program {
7652
8144
  /* isCompressed */
7653
8145
  ).slice(1); // throw away leading byte
7654
8146
 
7655
- const messageHash = Buffer.from(sha3.keccak_256.update(toBuffer(message)).digest());
8147
+ const messageHash = Buffer.from(keccak_256(toBuffer(message)));
7656
8148
  const [signature, recoveryId] = ecdsaSign(messageHash, privateKey);
7657
8149
  return this.createInstructionWithPublicKey({
7658
8150
  publicKey,
@@ -8546,6 +9038,33 @@ class VoteInstruction {
8546
9038
  }
8547
9039
  };
8548
9040
  }
9041
+ /**
9042
+ * Decode an authorize instruction and retrieve the instruction params.
9043
+ */
9044
+
9045
+
9046
+ static decodeAuthorizeWithSeed(instruction) {
9047
+ this.checkProgramId(instruction.programId);
9048
+ this.checkKeyLength(instruction.keys, 3);
9049
+ const {
9050
+ voteAuthorizeWithSeedArgs: {
9051
+ currentAuthorityDerivedKeyOwnerPubkey,
9052
+ currentAuthorityDerivedKeySeed,
9053
+ newAuthorized,
9054
+ voteAuthorizationType
9055
+ }
9056
+ } = decodeData$1(VOTE_INSTRUCTION_LAYOUTS.AuthorizeWithSeed, instruction.data);
9057
+ return {
9058
+ currentAuthorityDerivedKeyBasePubkey: instruction.keys[2].pubkey,
9059
+ currentAuthorityDerivedKeyOwnerPubkey: new PublicKey(currentAuthorityDerivedKeyOwnerPubkey),
9060
+ currentAuthorityDerivedKeySeed: currentAuthorityDerivedKeySeed,
9061
+ newAuthorizedPubkey: new PublicKey(newAuthorized),
9062
+ voteAuthorizationType: {
9063
+ index: voteAuthorizationType
9064
+ },
9065
+ votePubkey: instruction.keys[0].pubkey
9066
+ };
9067
+ }
8549
9068
  /**
8550
9069
  * Decode a withdraw instruction and retrieve the instruction params.
8551
9070
  */
@@ -8602,6 +9121,10 @@ const VOTE_INSTRUCTION_LAYOUTS = Object.freeze({
8602
9121
  Withdraw: {
8603
9122
  index: 3,
8604
9123
  layout: BufferLayout.struct([BufferLayout.u32('instruction'), BufferLayout.ns64('lamports')])
9124
+ },
9125
+ AuthorizeWithSeed: {
9126
+ index: 10,
9127
+ layout: BufferLayout.struct([BufferLayout.u32('instruction'), voteAuthorizeWithSeedArgs()])
8605
9128
  }
8606
9129
  });
8607
9130
  /**
@@ -8730,6 +9253,49 @@ class VoteProgram {
8730
9253
  data
8731
9254
  });
8732
9255
  }
9256
+ /**
9257
+ * Generate a transaction that authorizes a new Voter or Withdrawer on the Vote account
9258
+ * where the current Voter or Withdrawer authority is a derived key.
9259
+ */
9260
+
9261
+
9262
+ static authorizeWithSeed(params) {
9263
+ const {
9264
+ currentAuthorityDerivedKeyBasePubkey,
9265
+ currentAuthorityDerivedKeyOwnerPubkey,
9266
+ currentAuthorityDerivedKeySeed,
9267
+ newAuthorizedPubkey,
9268
+ voteAuthorizationType,
9269
+ votePubkey
9270
+ } = params;
9271
+ const type = VOTE_INSTRUCTION_LAYOUTS.AuthorizeWithSeed;
9272
+ const data = encodeData(type, {
9273
+ voteAuthorizeWithSeedArgs: {
9274
+ currentAuthorityDerivedKeyOwnerPubkey: toBuffer(currentAuthorityDerivedKeyOwnerPubkey.toBuffer()),
9275
+ currentAuthorityDerivedKeySeed: currentAuthorityDerivedKeySeed,
9276
+ newAuthorized: toBuffer(newAuthorizedPubkey.toBuffer()),
9277
+ voteAuthorizationType: voteAuthorizationType.index
9278
+ }
9279
+ });
9280
+ const keys = [{
9281
+ pubkey: votePubkey,
9282
+ isSigner: false,
9283
+ isWritable: true
9284
+ }, {
9285
+ pubkey: SYSVAR_CLOCK_PUBKEY,
9286
+ isSigner: false,
9287
+ isWritable: false
9288
+ }, {
9289
+ pubkey: currentAuthorityDerivedKeyBasePubkey,
9290
+ isSigner: true,
9291
+ isWritable: false
9292
+ }];
9293
+ return new Transaction().add({
9294
+ keys,
9295
+ programId: this.programId,
9296
+ data
9297
+ });
9298
+ }
8733
9299
  /**
8734
9300
  * Generate a transaction to withdraw from a Vote account.
8735
9301
  */
@@ -9052,5 +9618,5 @@ async function sendAndConfirmRawTransaction(connection, rawTransaction, confirma
9052
9618
 
9053
9619
  const LAMPORTS_PER_SOL = 1000000000;
9054
9620
 
9055
- 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 };
9621
+ 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, TransactionMessage, TransactionStatus, VALIDATOR_INFO_KEY, VERSION_PREFIX_MASK, VOTE_PROGRAM_ID, ValidatorInfo, VersionedMessage, VersionedTransaction, VoteAccount, VoteAuthorizationLayout, VoteInit, VoteInstruction, VoteProgram, clusterApiUrl, sendAndConfirmRawTransaction, sendAndConfirmTransaction };
9056
9622
  //# sourceMappingURL=index.browser.esm.js.map