@solana/transactions 2.0.0-experimental.eeb355e → 2.0.0-experimental.ef09aec

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,8 +1,8 @@
1
1
  import { getBase58Encoder, getBase58Decoder, getBase64Decoder, getStringEncoder, getStringDecoder } from '@solana/codecs-strings';
2
- import { mapEncoder, mapDecoder, combineCodec } from '@solana/codecs-core';
3
- import { getStructEncoder, getArrayEncoder, getBytesEncoder, getStructDecoder, getArrayDecoder, getBytesDecoder } from '@solana/codecs-data-structures';
2
+ import { getAddressFromPublicKey, getAddressComparator, getAddressEncoder, getAddressDecoder, assertIsAddress } from '@solana/addresses';
3
+ import { mapDecoder, combineCodec, mapEncoder } from '@solana/codecs-core';
4
+ import { getStructDecoder, getStructEncoder, getArrayEncoder, getArrayDecoder, getBytesEncoder, getBytesDecoder } from '@solana/codecs-data-structures';
4
5
  import { getShortU16Encoder, getShortU16Decoder, getU8Encoder, getU8Decoder } from '@solana/codecs-numbers';
5
- import { getAddressFromPublicKey, getAddressComparator, assertIsAddress, getAddressEncoder, getAddressDecoder } from '@solana/addresses';
6
6
  import { pipe } from '@solana/functional';
7
7
  import { signBytes } from '@solana/keys';
8
8
 
@@ -252,7 +252,7 @@ function getAddressMapFromInstructions(feePayer, instructions) {
252
252
  const shouldReplaceEntry = (
253
253
  // Consider using the new LOOKUP_TABLE if its address is different...
254
254
  entry.lookupTableAddress !== accountMeta.lookupTableAddress && // ...and sorts before the existing one.
255
- (addressComparator || (addressComparator = getAddressComparator()))(
255
+ (addressComparator ||= getAddressComparator())(
256
256
  accountMeta.lookupTableAddress,
257
257
  entry.lookupTableAddress
258
258
  ) < 0
@@ -358,7 +358,7 @@ function getOrderedAccountsFromAddressMap(addressMap) {
358
358
  if (leftIsWritable !== isWritableRole(rightEntry.role)) {
359
359
  return leftIsWritable ? -1 : 1;
360
360
  }
361
- addressComparator || (addressComparator = getAddressComparator());
361
+ addressComparator ||= getAddressComparator();
362
362
  if (leftEntry[TYPE] === 1 /* LOOKUP_TABLE */ && rightEntry[TYPE] === 1 /* LOOKUP_TABLE */ && leftEntry.lookupTableAddress !== rightEntry.lookupTableAddress) {
363
363
  return addressComparator(leftEntry.lookupTableAddress, rightEntry.lookupTableAddress);
364
364
  } else {
@@ -371,16 +371,15 @@ function getOrderedAccountsFromAddressMap(addressMap) {
371
371
  return orderedAccounts;
372
372
  }
373
373
  function getCompiledAddressTableLookups(orderedAccounts) {
374
- var _a;
375
374
  const index = {};
376
375
  for (const account of orderedAccounts) {
377
376
  if (!("lookupTableAddress" in account)) {
378
377
  continue;
379
378
  }
380
- const entry = index[_a = account.lookupTableAddress] || (index[_a] = {
379
+ const entry = index[account.lookupTableAddress] ||= {
381
380
  readableIndices: [],
382
381
  writableIndices: []
383
- });
382
+ };
384
383
  if (account.role === AccountRole.WRITABLE) {
385
384
  entry.writableIndices.push(account.addressIndex);
386
385
  } else {
@@ -466,134 +465,6 @@ function compileMessage(transaction) {
466
465
  version: transaction.version
467
466
  };
468
467
  }
469
-
470
- // src/compile-transaction.ts
471
- function getCompiledTransaction(transaction) {
472
- const compiledMessage = compileMessage(transaction);
473
- let signatures;
474
- if ("signatures" in transaction) {
475
- signatures = [];
476
- for (let ii = 0; ii < compiledMessage.header.numSignerAccounts; ii++) {
477
- signatures[ii] = transaction.signatures[compiledMessage.staticAccounts[ii]] ?? new Uint8Array(Array(64).fill(0));
478
- }
479
- } else {
480
- signatures = Array(compiledMessage.header.numSignerAccounts).fill(new Uint8Array(Array(64).fill(0)));
481
- }
482
- return {
483
- compiledMessage,
484
- signatures
485
- };
486
- }
487
- function getAccountMetas(message) {
488
- const { header } = message;
489
- const numWritableSignerAccounts = header.numSignerAccounts - header.numReadonlySignerAccounts;
490
- const numWritableNonSignerAccounts = message.staticAccounts.length - header.numSignerAccounts - header.numReadonlyNonSignerAccounts;
491
- const accountMetas = [];
492
- let accountIndex = 0;
493
- for (let i = 0; i < numWritableSignerAccounts; i++) {
494
- accountMetas.push({
495
- address: message.staticAccounts[accountIndex],
496
- role: AccountRole.WRITABLE_SIGNER
497
- });
498
- accountIndex++;
499
- }
500
- for (let i = 0; i < header.numReadonlySignerAccounts; i++) {
501
- accountMetas.push({
502
- address: message.staticAccounts[accountIndex],
503
- role: AccountRole.READONLY_SIGNER
504
- });
505
- accountIndex++;
506
- }
507
- for (let i = 0; i < numWritableNonSignerAccounts; i++) {
508
- accountMetas.push({
509
- address: message.staticAccounts[accountIndex],
510
- role: AccountRole.WRITABLE
511
- });
512
- accountIndex++;
513
- }
514
- for (let i = 0; i < header.numReadonlyNonSignerAccounts; i++) {
515
- accountMetas.push({
516
- address: message.staticAccounts[accountIndex],
517
- role: AccountRole.READONLY
518
- });
519
- accountIndex++;
520
- }
521
- return accountMetas;
522
- }
523
- function convertInstruction(instruction, accountMetas) {
524
- const programAddress = accountMetas[instruction.programAddressIndex]?.address;
525
- if (!programAddress) {
526
- throw new Error(`Could not find program address at index ${instruction.programAddressIndex}`);
527
- }
528
- const accounts = instruction.accountIndices?.map((accountIndex) => accountMetas[accountIndex]);
529
- const { data } = instruction;
530
- return {
531
- programAddress,
532
- ...accounts && accounts.length ? { accounts } : {},
533
- ...data && data.length ? { data } : {}
534
- };
535
- }
536
- function getLifetimeConstraint(messageLifetimeToken, firstInstruction, lastValidBlockHeight) {
537
- if (!firstInstruction || !isAdvanceNonceAccountInstruction(firstInstruction)) {
538
- return {
539
- blockhash: messageLifetimeToken,
540
- lastValidBlockHeight: lastValidBlockHeight ?? 2n ** 64n - 1n
541
- // U64 MAX
542
- };
543
- } else {
544
- const nonceAccountAddress = firstInstruction.accounts[0].address;
545
- assertIsAddress(nonceAccountAddress);
546
- const nonceAuthorityAddress = firstInstruction.accounts[2].address;
547
- assertIsAddress(nonceAuthorityAddress);
548
- return {
549
- nonce: messageLifetimeToken,
550
- nonceAccountAddress,
551
- nonceAuthorityAddress
552
- };
553
- }
554
- }
555
- function convertSignatures(compiledTransaction) {
556
- const {
557
- compiledMessage: { staticAccounts },
558
- signatures
559
- } = compiledTransaction;
560
- return signatures.reduce((acc, sig, index) => {
561
- const allZeros = sig.every((byte) => byte === 0);
562
- if (allZeros)
563
- return acc;
564
- const address = staticAccounts[index];
565
- return { ...acc, [address]: sig };
566
- }, {});
567
- }
568
- function decompileTransaction(compiledTransaction, lastValidBlockHeight) {
569
- const { compiledMessage } = compiledTransaction;
570
- if ("addressTableLookups" in compiledMessage && compiledMessage.addressTableLookups.length > 0) {
571
- throw new Error("Cannot convert transaction with addressTableLookups");
572
- }
573
- const feePayer = compiledMessage.staticAccounts[0];
574
- if (!feePayer)
575
- throw new Error("No fee payer set in CompiledTransaction");
576
- const accountMetas = getAccountMetas(compiledMessage);
577
- const instructions = compiledMessage.instructions.map(
578
- (compiledInstruction) => convertInstruction(compiledInstruction, accountMetas)
579
- );
580
- const firstInstruction = instructions[0];
581
- const lifetimeConstraint = getLifetimeConstraint(
582
- compiledMessage.lifetimeToken,
583
- firstInstruction,
584
- lastValidBlockHeight
585
- );
586
- const signatures = convertSignatures(compiledTransaction);
587
- return pipe(
588
- createTransaction({ version: compiledMessage.version }),
589
- (tx) => setTransactionFeePayer(feePayer, tx),
590
- (tx) => instructions.reduce((acc, instruction) => {
591
- return appendTransactionInstruction(instruction, acc);
592
- }, tx),
593
- (tx) => "blockhash" in lifetimeConstraint ? setTransactionLifetimeUsingBlockhash(lifetimeConstraint, tx) : setTransactionLifetimeUsingDurableNonce(lifetimeConstraint, tx),
594
- (tx) => compiledTransaction.signatures.length ? { ...tx, signatures } : tx
595
- );
596
- }
597
468
  var lookupTableAddressDescription = __DEV__ ? "The address of the address lookup table account from which instruction addresses should be looked up" : "lookupTableAddress";
598
469
  var writableIndicesDescription = __DEV__ ? "The indices of the accounts in the lookup table that should be loaded as writeable" : "writableIndices";
599
470
  var readableIndicesDescription = __DEV__ ? "The indices of the accounts in the lookup table that should be loaded as read-only" : "readableIndices";
@@ -928,6 +799,137 @@ function getCompiledMessageDecoder() {
928
799
  }
929
800
  );
930
801
  }
802
+ function getCompiledMessageCodec() {
803
+ return combineCodec(getCompiledMessageEncoder(), getCompiledMessageDecoder());
804
+ }
805
+
806
+ // src/compile-transaction.ts
807
+ function getCompiledTransaction(transaction) {
808
+ const compiledMessage = compileMessage(transaction);
809
+ let signatures;
810
+ if ("signatures" in transaction) {
811
+ signatures = [];
812
+ for (let ii = 0; ii < compiledMessage.header.numSignerAccounts; ii++) {
813
+ signatures[ii] = transaction.signatures[compiledMessage.staticAccounts[ii]] ?? new Uint8Array(Array(64).fill(0));
814
+ }
815
+ } else {
816
+ signatures = Array(compiledMessage.header.numSignerAccounts).fill(new Uint8Array(Array(64).fill(0)));
817
+ }
818
+ return {
819
+ compiledMessage,
820
+ signatures
821
+ };
822
+ }
823
+ function getAccountMetas(message) {
824
+ const { header } = message;
825
+ const numWritableSignerAccounts = header.numSignerAccounts - header.numReadonlySignerAccounts;
826
+ const numWritableNonSignerAccounts = message.staticAccounts.length - header.numSignerAccounts - header.numReadonlyNonSignerAccounts;
827
+ const accountMetas = [];
828
+ let accountIndex = 0;
829
+ for (let i = 0; i < numWritableSignerAccounts; i++) {
830
+ accountMetas.push({
831
+ address: message.staticAccounts[accountIndex],
832
+ role: AccountRole.WRITABLE_SIGNER
833
+ });
834
+ accountIndex++;
835
+ }
836
+ for (let i = 0; i < header.numReadonlySignerAccounts; i++) {
837
+ accountMetas.push({
838
+ address: message.staticAccounts[accountIndex],
839
+ role: AccountRole.READONLY_SIGNER
840
+ });
841
+ accountIndex++;
842
+ }
843
+ for (let i = 0; i < numWritableNonSignerAccounts; i++) {
844
+ accountMetas.push({
845
+ address: message.staticAccounts[accountIndex],
846
+ role: AccountRole.WRITABLE
847
+ });
848
+ accountIndex++;
849
+ }
850
+ for (let i = 0; i < header.numReadonlyNonSignerAccounts; i++) {
851
+ accountMetas.push({
852
+ address: message.staticAccounts[accountIndex],
853
+ role: AccountRole.READONLY
854
+ });
855
+ accountIndex++;
856
+ }
857
+ return accountMetas;
858
+ }
859
+ function convertInstruction(instruction, accountMetas) {
860
+ const programAddress = accountMetas[instruction.programAddressIndex]?.address;
861
+ if (!programAddress) {
862
+ throw new Error(`Could not find program address at index ${instruction.programAddressIndex}`);
863
+ }
864
+ const accounts = instruction.accountIndices?.map((accountIndex) => accountMetas[accountIndex]);
865
+ const { data } = instruction;
866
+ return {
867
+ programAddress,
868
+ ...accounts && accounts.length ? { accounts } : {},
869
+ ...data && data.length ? { data } : {}
870
+ };
871
+ }
872
+ function getLifetimeConstraint(messageLifetimeToken, firstInstruction, lastValidBlockHeight) {
873
+ if (!firstInstruction || !isAdvanceNonceAccountInstruction(firstInstruction)) {
874
+ return {
875
+ blockhash: messageLifetimeToken,
876
+ lastValidBlockHeight: lastValidBlockHeight ?? 2n ** 64n - 1n
877
+ // U64 MAX
878
+ };
879
+ } else {
880
+ const nonceAccountAddress = firstInstruction.accounts[0].address;
881
+ assertIsAddress(nonceAccountAddress);
882
+ const nonceAuthorityAddress = firstInstruction.accounts[2].address;
883
+ assertIsAddress(nonceAuthorityAddress);
884
+ return {
885
+ nonce: messageLifetimeToken,
886
+ nonceAccountAddress,
887
+ nonceAuthorityAddress
888
+ };
889
+ }
890
+ }
891
+ function convertSignatures(compiledTransaction) {
892
+ const {
893
+ compiledMessage: { staticAccounts },
894
+ signatures
895
+ } = compiledTransaction;
896
+ return signatures.reduce((acc, sig, index) => {
897
+ const allZeros = sig.every((byte) => byte === 0);
898
+ if (allZeros)
899
+ return acc;
900
+ const address = staticAccounts[index];
901
+ return { ...acc, [address]: sig };
902
+ }, {});
903
+ }
904
+ function decompileTransaction(compiledTransaction, lastValidBlockHeight) {
905
+ const { compiledMessage } = compiledTransaction;
906
+ if ("addressTableLookups" in compiledMessage && compiledMessage.addressTableLookups.length > 0) {
907
+ throw new Error("Cannot convert transaction with addressTableLookups");
908
+ }
909
+ const feePayer = compiledMessage.staticAccounts[0];
910
+ if (!feePayer)
911
+ throw new Error("No fee payer set in CompiledTransaction");
912
+ const accountMetas = getAccountMetas(compiledMessage);
913
+ const instructions = compiledMessage.instructions.map(
914
+ (compiledInstruction) => convertInstruction(compiledInstruction, accountMetas)
915
+ );
916
+ const firstInstruction = instructions[0];
917
+ const lifetimeConstraint = getLifetimeConstraint(
918
+ compiledMessage.lifetimeToken,
919
+ firstInstruction,
920
+ lastValidBlockHeight
921
+ );
922
+ const signatures = convertSignatures(compiledTransaction);
923
+ return pipe(
924
+ createTransaction({ version: compiledMessage.version }),
925
+ (tx) => setTransactionFeePayer(feePayer, tx),
926
+ (tx) => instructions.reduce((acc, instruction) => {
927
+ return appendTransactionInstruction(instruction, acc);
928
+ }, tx),
929
+ (tx) => "blockhash" in lifetimeConstraint ? setTransactionLifetimeUsingBlockhash(lifetimeConstraint, tx) : setTransactionLifetimeUsingDurableNonce(lifetimeConstraint, tx),
930
+ (tx) => compiledTransaction.signatures.length ? { ...tx, signatures } : tx
931
+ );
932
+ }
931
933
 
932
934
  // src/serializers/transaction.ts
933
935
  var signaturesDescription = __DEV__ ? "A compact array of 64-byte, base-64 encoded Ed25519 signatures" : "signatures";
@@ -1033,6 +1035,6 @@ function getBase64EncodedWireTransaction(transaction) {
1033
1035
  return getBase64Decoder().decode(wireTransactionBytes)[0];
1034
1036
  }
1035
1037
 
1036
- export { appendTransactionInstruction, assertIsBlockhash, assertIsDurableNonceTransaction, assertTransactionIsFullySigned, createTransaction, getBase64EncodedWireTransaction, getSignatureFromTransaction, getTransactionCodec, getTransactionDecoder, getTransactionEncoder, isAdvanceNonceAccountInstruction, partiallySignTransaction, prependTransactionInstruction, setTransactionFeePayer, setTransactionLifetimeUsingBlockhash, setTransactionLifetimeUsingDurableNonce, signTransaction };
1038
+ export { appendTransactionInstruction, assertIsBlockhash, assertIsDurableNonceTransaction, assertTransactionIsFullySigned, compileMessage, createTransaction, getBase64EncodedWireTransaction, getCompiledMessageCodec, getCompiledMessageDecoder, getCompiledMessageEncoder, getSignatureFromTransaction, getTransactionCodec, getTransactionDecoder, getTransactionEncoder, isAdvanceNonceAccountInstruction, partiallySignTransaction, prependTransactionInstruction, setTransactionFeePayer, setTransactionLifetimeUsingBlockhash, setTransactionLifetimeUsingDurableNonce, signTransaction };
1037
1039
  //# sourceMappingURL=out.js.map
1038
1040
  //# sourceMappingURL=index.browser.js.map