@solana/transactions 2.0.0-experimental.6466d76 → 2.0.0-experimental.651e653

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.
@@ -1,28 +1,12 @@
1
- import { getBase58Encoder, getBase58Decoder, getBase64Decoder, getStringEncoder, getStringDecoder } from '@solana/codecs-strings';
2
- import { getAddressFromPublicKey, getAddressComparator, getAddressEncoder, getAddressDecoder, assertIsAddress } from '@solana/addresses';
3
- import { createEncoder, mapDecoder, combineCodec, mapEncoder, createDecoder } from '@solana/codecs-core';
4
- import { getStructDecoder, getStructEncoder, getArrayEncoder, getArrayDecoder, getBytesEncoder, getBytesDecoder } from '@solana/codecs-data-structures';
5
- import { getShortU16Encoder, getShortU16Decoder, getU8Encoder, getU8Decoder } from '@solana/codecs-numbers';
1
+ import { getBase58Decoder, getBase64Decoder, getStringEncoder, getBase58Encoder, getStringDecoder } from '@solana/codecs-strings';
2
+ import { getAddressFromPublicKey, assertIsAddress, getAddressComparator, getAddressEncoder, getAddressDecoder } from '@solana/addresses';
6
3
  import { pipe } from '@solana/functional';
4
+ import { createEncoder, mapDecoder, combineCodec, mapEncoder, createDecoder } from '@solana/codecs-core';
5
+ import { getStructDecoder, getArrayDecoder, getBytesDecoder, getStructEncoder, getArrayEncoder, getBytesEncoder } from '@solana/codecs-data-structures';
6
+ import { getShortU16Decoder, getShortU16Encoder, getU8Encoder, getU8Decoder } from '@solana/codecs-numbers';
7
7
  import { signBytes } from '@solana/keys';
8
8
 
9
- // src/blockhash.ts
10
-
11
- // src/unsigned-transaction.ts
12
- function getUnsignedTransaction(transaction) {
13
- if ("signatures" in transaction) {
14
- const {
15
- signatures: _,
16
- // eslint-disable-line @typescript-eslint/no-unused-vars
17
- ...unsignedTransaction
18
- } = transaction;
19
- return unsignedTransaction;
20
- } else {
21
- return transaction;
22
- }
23
- }
24
-
25
- // src/blockhash.ts
9
+ // ../rpc-types/dist/index.browser.js
26
10
  var base58Encoder;
27
11
  function assertIsBlockhash(putativeBlockhash) {
28
12
  if (!base58Encoder)
@@ -46,6 +30,22 @@ function assertIsBlockhash(putativeBlockhash) {
46
30
  });
47
31
  }
48
32
  }
33
+
34
+ // src/unsigned-transaction.ts
35
+ function getUnsignedTransaction(transaction) {
36
+ if ("signatures" in transaction) {
37
+ const {
38
+ signatures: _,
39
+ // eslint-disable-line @typescript-eslint/no-unused-vars
40
+ ...unsignedTransaction
41
+ } = transaction;
42
+ return unsignedTransaction;
43
+ } else {
44
+ return transaction;
45
+ }
46
+ }
47
+
48
+ // src/blockhash.ts
49
49
  function isTransactionWithBlockhashLifetime(transaction) {
50
50
  const lifetimeConstraintShapeMatches = "lifetimeConstraint" in transaction && typeof transaction.lifetimeConstraint.blockhash === "string" && typeof transaction.lifetimeConstraint.lastValidBlockHeight === "bigint";
51
51
  if (!lifetimeConstraintShapeMatches)
@@ -215,6 +215,151 @@ function prependTransactionInstruction(instruction, transaction) {
215
215
  Object.freeze(out);
216
216
  return out;
217
217
  }
218
+
219
+ // src/decompile-transaction.ts
220
+ function getAccountMetas(message) {
221
+ const { header } = message;
222
+ const numWritableSignerAccounts = header.numSignerAccounts - header.numReadonlySignerAccounts;
223
+ const numWritableNonSignerAccounts = message.staticAccounts.length - header.numSignerAccounts - header.numReadonlyNonSignerAccounts;
224
+ const accountMetas = [];
225
+ let accountIndex = 0;
226
+ for (let i = 0; i < numWritableSignerAccounts; i++) {
227
+ accountMetas.push({
228
+ address: message.staticAccounts[accountIndex],
229
+ role: AccountRole.WRITABLE_SIGNER
230
+ });
231
+ accountIndex++;
232
+ }
233
+ for (let i = 0; i < header.numReadonlySignerAccounts; i++) {
234
+ accountMetas.push({
235
+ address: message.staticAccounts[accountIndex],
236
+ role: AccountRole.READONLY_SIGNER
237
+ });
238
+ accountIndex++;
239
+ }
240
+ for (let i = 0; i < numWritableNonSignerAccounts; i++) {
241
+ accountMetas.push({
242
+ address: message.staticAccounts[accountIndex],
243
+ role: AccountRole.WRITABLE
244
+ });
245
+ accountIndex++;
246
+ }
247
+ for (let i = 0; i < header.numReadonlyNonSignerAccounts; i++) {
248
+ accountMetas.push({
249
+ address: message.staticAccounts[accountIndex],
250
+ role: AccountRole.READONLY
251
+ });
252
+ accountIndex++;
253
+ }
254
+ return accountMetas;
255
+ }
256
+ function getAddressLookupMetas(compiledAddressTableLookups, addressesByLookupTableAddress) {
257
+ const compiledAddressTableLookupAddresses = compiledAddressTableLookups.map((l) => l.lookupTableAddress);
258
+ const missing = compiledAddressTableLookupAddresses.filter((a) => addressesByLookupTableAddress[a] === void 0);
259
+ if (missing.length > 0) {
260
+ const missingAddresses = missing.join(", ");
261
+ throw new Error(`Addresses not provided for lookup tables: [${missingAddresses}]`);
262
+ }
263
+ const readOnlyMetas = [];
264
+ const writableMetas = [];
265
+ for (const lookup of compiledAddressTableLookups) {
266
+ const addresses = addressesByLookupTableAddress[lookup.lookupTableAddress];
267
+ const highestIndex = Math.max(...lookup.readableIndices, ...lookup.writableIndices);
268
+ if (highestIndex >= addresses.length) {
269
+ throw new Error(
270
+ `Cannot look up index ${highestIndex} in lookup table [${lookup.lookupTableAddress}]. The lookup table may have been extended since the addresses provided were retrieved.`
271
+ );
272
+ }
273
+ const readOnlyForLookup = lookup.readableIndices.map((r) => ({
274
+ address: addresses[r],
275
+ addressIndex: r,
276
+ lookupTableAddress: lookup.lookupTableAddress,
277
+ role: AccountRole.READONLY
278
+ }));
279
+ readOnlyMetas.push(...readOnlyForLookup);
280
+ const writableForLookup = lookup.writableIndices.map((w) => ({
281
+ address: addresses[w],
282
+ addressIndex: w,
283
+ lookupTableAddress: lookup.lookupTableAddress,
284
+ role: AccountRole.WRITABLE
285
+ }));
286
+ writableMetas.push(...writableForLookup);
287
+ }
288
+ return [...writableMetas, ...readOnlyMetas];
289
+ }
290
+ function convertInstruction(instruction, accountMetas) {
291
+ const programAddress = accountMetas[instruction.programAddressIndex]?.address;
292
+ if (!programAddress) {
293
+ throw new Error(`Could not find program address at index ${instruction.programAddressIndex}`);
294
+ }
295
+ const accounts = instruction.accountIndices?.map((accountIndex) => accountMetas[accountIndex]);
296
+ const { data } = instruction;
297
+ return {
298
+ programAddress,
299
+ ...accounts && accounts.length ? { accounts } : {},
300
+ ...data && data.length ? { data } : {}
301
+ };
302
+ }
303
+ function getLifetimeConstraint(messageLifetimeToken, firstInstruction, lastValidBlockHeight) {
304
+ if (!firstInstruction || !isAdvanceNonceAccountInstruction(firstInstruction)) {
305
+ return {
306
+ blockhash: messageLifetimeToken,
307
+ lastValidBlockHeight: lastValidBlockHeight ?? 2n ** 64n - 1n
308
+ // U64 MAX
309
+ };
310
+ } else {
311
+ const nonceAccountAddress = firstInstruction.accounts[0].address;
312
+ assertIsAddress(nonceAccountAddress);
313
+ const nonceAuthorityAddress = firstInstruction.accounts[2].address;
314
+ assertIsAddress(nonceAuthorityAddress);
315
+ return {
316
+ nonce: messageLifetimeToken,
317
+ nonceAccountAddress,
318
+ nonceAuthorityAddress
319
+ };
320
+ }
321
+ }
322
+ function convertSignatures(compiledTransaction) {
323
+ const {
324
+ compiledMessage: { staticAccounts },
325
+ signatures
326
+ } = compiledTransaction;
327
+ return signatures.reduce((acc, sig, index) => {
328
+ const allZeros = sig.every((byte) => byte === 0);
329
+ if (allZeros)
330
+ return acc;
331
+ const address = staticAccounts[index];
332
+ return { ...acc, [address]: sig };
333
+ }, {});
334
+ }
335
+ function decompileTransaction(compiledTransaction, config) {
336
+ const { compiledMessage } = compiledTransaction;
337
+ const feePayer = compiledMessage.staticAccounts[0];
338
+ if (!feePayer)
339
+ throw new Error("No fee payer set in CompiledTransaction");
340
+ const accountMetas = getAccountMetas(compiledMessage);
341
+ const accountLookupMetas = "addressTableLookups" in compiledMessage && compiledMessage.addressTableLookups !== void 0 && compiledMessage.addressTableLookups.length > 0 ? getAddressLookupMetas(compiledMessage.addressTableLookups, config?.addressesByLookupTableAddress ?? {}) : [];
342
+ const transactionMetas = [...accountMetas, ...accountLookupMetas];
343
+ const instructions = compiledMessage.instructions.map(
344
+ (compiledInstruction) => convertInstruction(compiledInstruction, transactionMetas)
345
+ );
346
+ const firstInstruction = instructions[0];
347
+ const lifetimeConstraint = getLifetimeConstraint(
348
+ compiledMessage.lifetimeToken,
349
+ firstInstruction,
350
+ config?.lastValidBlockHeight
351
+ );
352
+ const signatures = convertSignatures(compiledTransaction);
353
+ return pipe(
354
+ createTransaction({ version: compiledMessage.version }),
355
+ (tx) => setTransactionFeePayer(feePayer, tx),
356
+ (tx) => instructions.reduce((acc, instruction) => {
357
+ return appendTransactionInstruction(instruction, acc);
358
+ }, tx),
359
+ (tx) => "blockhash" in lifetimeConstraint ? setTransactionLifetimeUsingBlockhash(lifetimeConstraint, tx) : setTransactionLifetimeUsingDurableNonce(lifetimeConstraint, tx),
360
+ (tx) => Object.keys(signatures).length > 0 ? { ...tx, signatures } : tx
361
+ );
362
+ }
218
363
  function upsert(addressMap, address, update) {
219
364
  addressMap[address] = update(addressMap[address] ?? { role: AccountRole.READONLY });
220
365
  }
@@ -708,149 +853,6 @@ function getCompiledTransaction(transaction) {
708
853
  signatures
709
854
  };
710
855
  }
711
- function getAccountMetas(message) {
712
- const { header } = message;
713
- const numWritableSignerAccounts = header.numSignerAccounts - header.numReadonlySignerAccounts;
714
- const numWritableNonSignerAccounts = message.staticAccounts.length - header.numSignerAccounts - header.numReadonlyNonSignerAccounts;
715
- const accountMetas = [];
716
- let accountIndex = 0;
717
- for (let i = 0; i < numWritableSignerAccounts; i++) {
718
- accountMetas.push({
719
- address: message.staticAccounts[accountIndex],
720
- role: AccountRole.WRITABLE_SIGNER
721
- });
722
- accountIndex++;
723
- }
724
- for (let i = 0; i < header.numReadonlySignerAccounts; i++) {
725
- accountMetas.push({
726
- address: message.staticAccounts[accountIndex],
727
- role: AccountRole.READONLY_SIGNER
728
- });
729
- accountIndex++;
730
- }
731
- for (let i = 0; i < numWritableNonSignerAccounts; i++) {
732
- accountMetas.push({
733
- address: message.staticAccounts[accountIndex],
734
- role: AccountRole.WRITABLE
735
- });
736
- accountIndex++;
737
- }
738
- for (let i = 0; i < header.numReadonlyNonSignerAccounts; i++) {
739
- accountMetas.push({
740
- address: message.staticAccounts[accountIndex],
741
- role: AccountRole.READONLY
742
- });
743
- accountIndex++;
744
- }
745
- return accountMetas;
746
- }
747
- function getAddressLookupMetas(compiledAddressTableLookups, addressesByLookupTableAddress) {
748
- const compiledAddressTableLookupAddresses = compiledAddressTableLookups.map((l) => l.lookupTableAddress);
749
- const missing = compiledAddressTableLookupAddresses.filter((a) => addressesByLookupTableAddress[a] === void 0);
750
- if (missing.length > 0) {
751
- const missingAddresses = missing.join(", ");
752
- throw new Error(`Addresses not provided for lookup tables: [${missingAddresses}]`);
753
- }
754
- const readOnlyMetas = [];
755
- const writableMetas = [];
756
- for (const lookup of compiledAddressTableLookups) {
757
- const addresses = addressesByLookupTableAddress[lookup.lookupTableAddress];
758
- const highestIndex = Math.max(...lookup.readableIndices, ...lookup.writableIndices);
759
- if (highestIndex >= addresses.length) {
760
- throw new Error(
761
- `Cannot look up index ${highestIndex} in lookup table [${lookup.lookupTableAddress}]. The lookup table may have been extended since the addresses provided were retrieved.`
762
- );
763
- }
764
- const readOnlyForLookup = lookup.readableIndices.map((r) => ({
765
- address: addresses[r],
766
- addressIndex: r,
767
- lookupTableAddress: lookup.lookupTableAddress,
768
- role: AccountRole.READONLY
769
- }));
770
- readOnlyMetas.push(...readOnlyForLookup);
771
- const writableForLookup = lookup.writableIndices.map((w) => ({
772
- address: addresses[w],
773
- addressIndex: w,
774
- lookupTableAddress: lookup.lookupTableAddress,
775
- role: AccountRole.WRITABLE
776
- }));
777
- writableMetas.push(...writableForLookup);
778
- }
779
- return [...writableMetas, ...readOnlyMetas];
780
- }
781
- function convertInstruction(instruction, accountMetas) {
782
- const programAddress = accountMetas[instruction.programAddressIndex]?.address;
783
- if (!programAddress) {
784
- throw new Error(`Could not find program address at index ${instruction.programAddressIndex}`);
785
- }
786
- const accounts = instruction.accountIndices?.map((accountIndex) => accountMetas[accountIndex]);
787
- const { data } = instruction;
788
- return {
789
- programAddress,
790
- ...accounts && accounts.length ? { accounts } : {},
791
- ...data && data.length ? { data } : {}
792
- };
793
- }
794
- function getLifetimeConstraint(messageLifetimeToken, firstInstruction, lastValidBlockHeight) {
795
- if (!firstInstruction || !isAdvanceNonceAccountInstruction(firstInstruction)) {
796
- return {
797
- blockhash: messageLifetimeToken,
798
- lastValidBlockHeight: lastValidBlockHeight ?? 2n ** 64n - 1n
799
- // U64 MAX
800
- };
801
- } else {
802
- const nonceAccountAddress = firstInstruction.accounts[0].address;
803
- assertIsAddress(nonceAccountAddress);
804
- const nonceAuthorityAddress = firstInstruction.accounts[2].address;
805
- assertIsAddress(nonceAuthorityAddress);
806
- return {
807
- nonce: messageLifetimeToken,
808
- nonceAccountAddress,
809
- nonceAuthorityAddress
810
- };
811
- }
812
- }
813
- function convertSignatures(compiledTransaction) {
814
- const {
815
- compiledMessage: { staticAccounts },
816
- signatures
817
- } = compiledTransaction;
818
- return signatures.reduce((acc, sig, index) => {
819
- const allZeros = sig.every((byte) => byte === 0);
820
- if (allZeros)
821
- return acc;
822
- const address = staticAccounts[index];
823
- return { ...acc, [address]: sig };
824
- }, {});
825
- }
826
- function decompileTransaction(compiledTransaction, config) {
827
- const { compiledMessage } = compiledTransaction;
828
- const feePayer = compiledMessage.staticAccounts[0];
829
- if (!feePayer)
830
- throw new Error("No fee payer set in CompiledTransaction");
831
- const accountMetas = getAccountMetas(compiledMessage);
832
- const accountLookupMetas = "addressTableLookups" in compiledMessage && compiledMessage.addressTableLookups !== void 0 && compiledMessage.addressTableLookups.length > 0 ? getAddressLookupMetas(compiledMessage.addressTableLookups, config?.addressesByLookupTableAddress ?? {}) : [];
833
- const transactionMetas = [...accountMetas, ...accountLookupMetas];
834
- const instructions = compiledMessage.instructions.map(
835
- (compiledInstruction) => convertInstruction(compiledInstruction, transactionMetas)
836
- );
837
- const firstInstruction = instructions[0];
838
- const lifetimeConstraint = getLifetimeConstraint(
839
- compiledMessage.lifetimeToken,
840
- firstInstruction,
841
- config?.lastValidBlockHeight
842
- );
843
- const signatures = convertSignatures(compiledTransaction);
844
- return pipe(
845
- createTransaction({ version: compiledMessage.version }),
846
- (tx) => setTransactionFeePayer(feePayer, tx),
847
- (tx) => instructions.reduce((acc, instruction) => {
848
- return appendTransactionInstruction(instruction, acc);
849
- }, tx),
850
- (tx) => "blockhash" in lifetimeConstraint ? setTransactionLifetimeUsingBlockhash(lifetimeConstraint, tx) : setTransactionLifetimeUsingDurableNonce(lifetimeConstraint, tx),
851
- (tx) => compiledTransaction.signatures.length ? { ...tx, signatures } : tx
852
- );
853
- }
854
856
 
855
857
  // src/serializers/transaction.ts
856
858
  function getCompiledTransactionEncoder() {
@@ -938,6 +940,6 @@ function getBase64EncodedWireTransaction(transaction) {
938
940
  return getBase64Decoder().decode(wireTransactionBytes);
939
941
  }
940
942
 
941
- export { appendTransactionInstruction, assertIsBlockhash, assertIsDurableNonceTransaction, assertIsTransactionWithBlockhashLifetime, assertTransactionIsFullySigned, compileMessage, createTransaction, getBase64EncodedWireTransaction, getCompiledMessageCodec, getCompiledMessageDecoder, getCompiledMessageEncoder, getSignatureFromTransaction, getTransactionCodec, getTransactionDecoder, getTransactionEncoder, isAdvanceNonceAccountInstruction, partiallySignTransaction, prependTransactionInstruction, setTransactionFeePayer, setTransactionLifetimeUsingBlockhash, setTransactionLifetimeUsingDurableNonce, signTransaction };
943
+ export { appendTransactionInstruction, assertIsDurableNonceTransaction, assertIsTransactionWithBlockhashLifetime, assertTransactionIsFullySigned, compileMessage, createTransaction, decompileTransaction, getBase64EncodedWireTransaction, getCompiledMessageCodec, getCompiledMessageDecoder, getCompiledMessageEncoder, getCompiledTransactionDecoder, getSignatureFromTransaction, getTransactionCodec, getTransactionDecoder, getTransactionEncoder, isAdvanceNonceAccountInstruction, partiallySignTransaction, prependTransactionInstruction, setTransactionFeePayer, setTransactionLifetimeUsingBlockhash, setTransactionLifetimeUsingDurableNonce, signTransaction };
942
944
  //# sourceMappingURL=out.js.map
943
945
  //# sourceMappingURL=index.native.js.map