@solana/transactions 2.0.0-experimental.6466d76 → 2.0.0-experimental.64d583b

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,13 @@
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
+ import { SolanaError, SOLANA_ERROR__TRANSACTION_SIGNATURE_NOT_COMPUTABLE, SOLANA_ERROR__TRANSACTION_MISSING_SIGNATURES } from '@solana/errors';
7
8
  import { signBytes } from '@solana/keys';
8
9
 
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
10
+ // ../rpc-types/dist/index.browser.js
26
11
  var base58Encoder;
27
12
  function assertIsBlockhash(putativeBlockhash) {
28
13
  if (!base58Encoder)
@@ -46,6 +31,22 @@ function assertIsBlockhash(putativeBlockhash) {
46
31
  });
47
32
  }
48
33
  }
34
+
35
+ // src/unsigned-transaction.ts
36
+ function getUnsignedTransaction(transaction) {
37
+ if ("signatures" in transaction) {
38
+ const {
39
+ signatures: _,
40
+ // eslint-disable-line @typescript-eslint/no-unused-vars
41
+ ...unsignedTransaction
42
+ } = transaction;
43
+ return unsignedTransaction;
44
+ } else {
45
+ return transaction;
46
+ }
47
+ }
48
+
49
+ // src/blockhash.ts
49
50
  function isTransactionWithBlockhashLifetime(transaction) {
50
51
  const lifetimeConstraintShapeMatches = "lifetimeConstraint" in transaction && typeof transaction.lifetimeConstraint.blockhash === "string" && typeof transaction.lifetimeConstraint.lastValidBlockHeight === "bigint";
51
52
  if (!lifetimeConstraintShapeMatches)
@@ -215,6 +216,151 @@ function prependTransactionInstruction(instruction, transaction) {
215
216
  Object.freeze(out);
216
217
  return out;
217
218
  }
219
+
220
+ // src/decompile-transaction.ts
221
+ function getAccountMetas(message) {
222
+ const { header } = message;
223
+ const numWritableSignerAccounts = header.numSignerAccounts - header.numReadonlySignerAccounts;
224
+ const numWritableNonSignerAccounts = message.staticAccounts.length - header.numSignerAccounts - header.numReadonlyNonSignerAccounts;
225
+ const accountMetas = [];
226
+ let accountIndex = 0;
227
+ for (let i = 0; i < numWritableSignerAccounts; i++) {
228
+ accountMetas.push({
229
+ address: message.staticAccounts[accountIndex],
230
+ role: AccountRole.WRITABLE_SIGNER
231
+ });
232
+ accountIndex++;
233
+ }
234
+ for (let i = 0; i < header.numReadonlySignerAccounts; i++) {
235
+ accountMetas.push({
236
+ address: message.staticAccounts[accountIndex],
237
+ role: AccountRole.READONLY_SIGNER
238
+ });
239
+ accountIndex++;
240
+ }
241
+ for (let i = 0; i < numWritableNonSignerAccounts; i++) {
242
+ accountMetas.push({
243
+ address: message.staticAccounts[accountIndex],
244
+ role: AccountRole.WRITABLE
245
+ });
246
+ accountIndex++;
247
+ }
248
+ for (let i = 0; i < header.numReadonlyNonSignerAccounts; i++) {
249
+ accountMetas.push({
250
+ address: message.staticAccounts[accountIndex],
251
+ role: AccountRole.READONLY
252
+ });
253
+ accountIndex++;
254
+ }
255
+ return accountMetas;
256
+ }
257
+ function getAddressLookupMetas(compiledAddressTableLookups, addressesByLookupTableAddress) {
258
+ const compiledAddressTableLookupAddresses = compiledAddressTableLookups.map((l) => l.lookupTableAddress);
259
+ const missing = compiledAddressTableLookupAddresses.filter((a) => addressesByLookupTableAddress[a] === void 0);
260
+ if (missing.length > 0) {
261
+ const missingAddresses = missing.join(", ");
262
+ throw new Error(`Addresses not provided for lookup tables: [${missingAddresses}]`);
263
+ }
264
+ const readOnlyMetas = [];
265
+ const writableMetas = [];
266
+ for (const lookup of compiledAddressTableLookups) {
267
+ const addresses = addressesByLookupTableAddress[lookup.lookupTableAddress];
268
+ const highestIndex = Math.max(...lookup.readableIndices, ...lookup.writableIndices);
269
+ if (highestIndex >= addresses.length) {
270
+ throw new Error(
271
+ `Cannot look up index ${highestIndex} in lookup table [${lookup.lookupTableAddress}]. The lookup table may have been extended since the addresses provided were retrieved.`
272
+ );
273
+ }
274
+ const readOnlyForLookup = lookup.readableIndices.map((r) => ({
275
+ address: addresses[r],
276
+ addressIndex: r,
277
+ lookupTableAddress: lookup.lookupTableAddress,
278
+ role: AccountRole.READONLY
279
+ }));
280
+ readOnlyMetas.push(...readOnlyForLookup);
281
+ const writableForLookup = lookup.writableIndices.map((w) => ({
282
+ address: addresses[w],
283
+ addressIndex: w,
284
+ lookupTableAddress: lookup.lookupTableAddress,
285
+ role: AccountRole.WRITABLE
286
+ }));
287
+ writableMetas.push(...writableForLookup);
288
+ }
289
+ return [...writableMetas, ...readOnlyMetas];
290
+ }
291
+ function convertInstruction(instruction, accountMetas) {
292
+ const programAddress = accountMetas[instruction.programAddressIndex]?.address;
293
+ if (!programAddress) {
294
+ throw new Error(`Could not find program address at index ${instruction.programAddressIndex}`);
295
+ }
296
+ const accounts = instruction.accountIndices?.map((accountIndex) => accountMetas[accountIndex]);
297
+ const { data } = instruction;
298
+ return {
299
+ programAddress,
300
+ ...accounts && accounts.length ? { accounts } : {},
301
+ ...data && data.length ? { data } : {}
302
+ };
303
+ }
304
+ function getLifetimeConstraint(messageLifetimeToken, firstInstruction, lastValidBlockHeight) {
305
+ if (!firstInstruction || !isAdvanceNonceAccountInstruction(firstInstruction)) {
306
+ return {
307
+ blockhash: messageLifetimeToken,
308
+ lastValidBlockHeight: lastValidBlockHeight ?? 2n ** 64n - 1n
309
+ // U64 MAX
310
+ };
311
+ } else {
312
+ const nonceAccountAddress = firstInstruction.accounts[0].address;
313
+ assertIsAddress(nonceAccountAddress);
314
+ const nonceAuthorityAddress = firstInstruction.accounts[2].address;
315
+ assertIsAddress(nonceAuthorityAddress);
316
+ return {
317
+ nonce: messageLifetimeToken,
318
+ nonceAccountAddress,
319
+ nonceAuthorityAddress
320
+ };
321
+ }
322
+ }
323
+ function convertSignatures(compiledTransaction) {
324
+ const {
325
+ compiledMessage: { staticAccounts },
326
+ signatures
327
+ } = compiledTransaction;
328
+ return signatures.reduce((acc, sig, index) => {
329
+ const allZeros = sig.every((byte) => byte === 0);
330
+ if (allZeros)
331
+ return acc;
332
+ const address = staticAccounts[index];
333
+ return { ...acc, [address]: sig };
334
+ }, {});
335
+ }
336
+ function decompileTransaction(compiledTransaction, config) {
337
+ const { compiledMessage } = compiledTransaction;
338
+ const feePayer = compiledMessage.staticAccounts[0];
339
+ if (!feePayer)
340
+ throw new Error("No fee payer set in CompiledTransaction");
341
+ const accountMetas = getAccountMetas(compiledMessage);
342
+ const accountLookupMetas = "addressTableLookups" in compiledMessage && compiledMessage.addressTableLookups !== void 0 && compiledMessage.addressTableLookups.length > 0 ? getAddressLookupMetas(compiledMessage.addressTableLookups, config?.addressesByLookupTableAddress ?? {}) : [];
343
+ const transactionMetas = [...accountMetas, ...accountLookupMetas];
344
+ const instructions = compiledMessage.instructions.map(
345
+ (compiledInstruction) => convertInstruction(compiledInstruction, transactionMetas)
346
+ );
347
+ const firstInstruction = instructions[0];
348
+ const lifetimeConstraint = getLifetimeConstraint(
349
+ compiledMessage.lifetimeToken,
350
+ firstInstruction,
351
+ config?.lastValidBlockHeight
352
+ );
353
+ const signatures = convertSignatures(compiledTransaction);
354
+ return pipe(
355
+ createTransaction({ version: compiledMessage.version }),
356
+ (tx) => setTransactionFeePayer(feePayer, tx),
357
+ (tx) => instructions.reduce((acc, instruction) => {
358
+ return appendTransactionInstruction(instruction, acc);
359
+ }, tx),
360
+ (tx) => "blockhash" in lifetimeConstraint ? setTransactionLifetimeUsingBlockhash(lifetimeConstraint, tx) : setTransactionLifetimeUsingDurableNonce(lifetimeConstraint, tx),
361
+ (tx) => Object.keys(signatures).length > 0 ? { ...tx, signatures } : tx
362
+ );
363
+ }
218
364
  function upsert(addressMap, address, update) {
219
365
  addressMap[address] = update(addressMap[address] ?? { role: AccountRole.READONLY });
220
366
  }
@@ -708,149 +854,6 @@ function getCompiledTransaction(transaction) {
708
854
  signatures
709
855
  };
710
856
  }
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
857
 
855
858
  // src/serializers/transaction.ts
856
859
  function getCompiledTransactionEncoder() {
@@ -888,9 +891,7 @@ function getSignatureFromTransaction(transaction) {
888
891
  base58Decoder = getBase58Decoder();
889
892
  const signatureBytes = transaction.signatures[transaction.feePayer];
890
893
  if (!signatureBytes) {
891
- throw new Error(
892
- "Could not determine this transaction's signature. Make sure that the transaction has been signed by its fee payer."
893
- );
894
+ throw new SolanaError(SOLANA_ERROR__TRANSACTION_SIGNATURE_NOT_COMPUTABLE);
894
895
  }
895
896
  const transactionSignature = base58Decoder.decode(signatureBytes);
896
897
  return transactionSignature;
@@ -930,7 +931,9 @@ function assertTransactionIsFullySigned(transaction) {
930
931
  }
931
932
  });
932
933
  if (missingSigs.length > 0) {
933
- throw new Error("Transaction is missing signatures for addresses: " + missingSigs.join(", "));
934
+ throw new SolanaError(SOLANA_ERROR__TRANSACTION_MISSING_SIGNATURES, {
935
+ addresses: missingSigs
936
+ });
934
937
  }
935
938
  }
936
939
  function getBase64EncodedWireTransaction(transaction) {
@@ -938,6 +941,6 @@ function getBase64EncodedWireTransaction(transaction) {
938
941
  return getBase64Decoder().decode(wireTransactionBytes);
939
942
  }
940
943
 
941
- export { appendTransactionInstruction, assertIsBlockhash, assertIsDurableNonceTransaction, assertIsTransactionWithBlockhashLifetime, assertTransactionIsFullySigned, compileMessage, createTransaction, getBase64EncodedWireTransaction, getCompiledMessageCodec, getCompiledMessageDecoder, getCompiledMessageEncoder, getSignatureFromTransaction, getTransactionCodec, getTransactionDecoder, getTransactionEncoder, isAdvanceNonceAccountInstruction, partiallySignTransaction, prependTransactionInstruction, setTransactionFeePayer, setTransactionLifetimeUsingBlockhash, setTransactionLifetimeUsingDurableNonce, signTransaction };
944
+ 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
945
  //# sourceMappingURL=out.js.map
943
946
  //# sourceMappingURL=index.browser.js.map