@solana/transactions 2.0.0-experimental.cf6ca9d → 2.0.0-experimental.d085c72

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,10 +1,9 @@
1
- import { base58 } from '@metaplex-foundation/umi-serializers';
2
- import { mapEncoder, mapDecoder, combineCodec } from '@solana/codecs-core';
3
- import { getStructEncoder, getArrayEncoder, getBytesEncoder, getStructDecoder, getArrayDecoder, getBytesDecoder } from '@solana/codecs-data-structures';
1
+ import { getBase58Encoder, getBase58Decoder, getBase64Decoder, getStringEncoder, getStringDecoder } from '@solana/codecs-strings';
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
- import { getStringEncoder, getBase58Encoder, getStringDecoder, getBase58Decoder } from '@solana/codecs-strings';
8
7
  import { signBytes } from '@solana/keys';
9
8
 
10
9
  // ../build-scripts/env-shim.ts
@@ -25,7 +24,10 @@ function getUnsignedTransaction(transaction) {
25
24
  }
26
25
 
27
26
  // src/blockhash.ts
27
+ var base58Encoder;
28
28
  function assertIsBlockhash(putativeBlockhash) {
29
+ if (!base58Encoder)
30
+ base58Encoder = getBase58Encoder();
29
31
  try {
30
32
  if (
31
33
  // Lowest value (32 bytes of zeroes)
@@ -34,7 +36,7 @@ function assertIsBlockhash(putativeBlockhash) {
34
36
  ) {
35
37
  throw new Error("Expected input string to decode to a byte array of length 32.");
36
38
  }
37
- const bytes = base58.serialize(putativeBlockhash);
39
+ const bytes = base58Encoder.encode(putativeBlockhash);
38
40
  const numBytes = bytes.byteLength;
39
41
  if (numBytes !== 32) {
40
42
  throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${numBytes}`);
@@ -250,7 +252,7 @@ function getAddressMapFromInstructions(feePayer, instructions) {
250
252
  const shouldReplaceEntry = (
251
253
  // Consider using the new LOOKUP_TABLE if its address is different...
252
254
  entry.lookupTableAddress !== accountMeta.lookupTableAddress && // ...and sorts before the existing one.
253
- (addressComparator || (addressComparator = getAddressComparator()))(
255
+ (addressComparator ||= getAddressComparator())(
254
256
  accountMeta.lookupTableAddress,
255
257
  entry.lookupTableAddress
256
258
  ) < 0
@@ -356,7 +358,7 @@ function getOrderedAccountsFromAddressMap(addressMap) {
356
358
  if (leftIsWritable !== isWritableRole(rightEntry.role)) {
357
359
  return leftIsWritable ? -1 : 1;
358
360
  }
359
- addressComparator || (addressComparator = getAddressComparator());
361
+ addressComparator ||= getAddressComparator();
360
362
  if (leftEntry[TYPE] === 1 /* LOOKUP_TABLE */ && rightEntry[TYPE] === 1 /* LOOKUP_TABLE */ && leftEntry.lookupTableAddress !== rightEntry.lookupTableAddress) {
361
363
  return addressComparator(leftEntry.lookupTableAddress, rightEntry.lookupTableAddress);
362
364
  } else {
@@ -369,16 +371,15 @@ function getOrderedAccountsFromAddressMap(addressMap) {
369
371
  return orderedAccounts;
370
372
  }
371
373
  function getCompiledAddressTableLookups(orderedAccounts) {
372
- var _a;
373
374
  const index = {};
374
375
  for (const account of orderedAccounts) {
375
376
  if (!("lookupTableAddress" in account)) {
376
377
  continue;
377
378
  }
378
- const entry = index[_a = account.lookupTableAddress] || (index[_a] = {
379
+ const entry = index[account.lookupTableAddress] ||= {
379
380
  readableIndices: [],
380
381
  writableIndices: []
381
- });
382
+ };
382
383
  if (account.role === AccountRole.WRITABLE) {
383
384
  entry.writableIndices.push(account.addressIndex);
384
385
  } else {
@@ -464,134 +465,6 @@ function compileMessage(transaction) {
464
465
  version: transaction.version
465
466
  };
466
467
  }
467
-
468
- // src/compile-transaction.ts
469
- function getCompiledTransaction(transaction) {
470
- const compiledMessage = compileMessage(transaction);
471
- let signatures;
472
- if ("signatures" in transaction) {
473
- signatures = [];
474
- for (let ii = 0; ii < compiledMessage.header.numSignerAccounts; ii++) {
475
- signatures[ii] = transaction.signatures[compiledMessage.staticAccounts[ii]] ?? new Uint8Array(Array(64).fill(0));
476
- }
477
- } else {
478
- signatures = Array(compiledMessage.header.numSignerAccounts).fill(new Uint8Array(Array(64).fill(0)));
479
- }
480
- return {
481
- compiledMessage,
482
- signatures
483
- };
484
- }
485
- function getAccountMetas(message) {
486
- const { header } = message;
487
- const numWritableSignerAccounts = header.numSignerAccounts - header.numReadonlySignerAccounts;
488
- const numWritableNonSignerAccounts = message.staticAccounts.length - header.numSignerAccounts - header.numReadonlyNonSignerAccounts;
489
- const accountMetas = [];
490
- let accountIndex = 0;
491
- for (let i = 0; i < numWritableSignerAccounts; i++) {
492
- accountMetas.push({
493
- address: message.staticAccounts[accountIndex],
494
- role: AccountRole.WRITABLE_SIGNER
495
- });
496
- accountIndex++;
497
- }
498
- for (let i = 0; i < header.numReadonlySignerAccounts; i++) {
499
- accountMetas.push({
500
- address: message.staticAccounts[accountIndex],
501
- role: AccountRole.READONLY_SIGNER
502
- });
503
- accountIndex++;
504
- }
505
- for (let i = 0; i < numWritableNonSignerAccounts; i++) {
506
- accountMetas.push({
507
- address: message.staticAccounts[accountIndex],
508
- role: AccountRole.WRITABLE
509
- });
510
- accountIndex++;
511
- }
512
- for (let i = 0; i < header.numReadonlyNonSignerAccounts; i++) {
513
- accountMetas.push({
514
- address: message.staticAccounts[accountIndex],
515
- role: AccountRole.READONLY
516
- });
517
- accountIndex++;
518
- }
519
- return accountMetas;
520
- }
521
- function convertInstruction(instruction, accountMetas) {
522
- const programAddress = accountMetas[instruction.programAddressIndex]?.address;
523
- if (!programAddress) {
524
- throw new Error(`Could not find program address at index ${instruction.programAddressIndex}`);
525
- }
526
- const accounts = instruction.accountIndices?.map((accountIndex) => accountMetas[accountIndex]);
527
- const { data } = instruction;
528
- return {
529
- programAddress,
530
- ...accounts && accounts.length ? { accounts } : {},
531
- ...data && data.length ? { data } : {}
532
- };
533
- }
534
- function getLifetimeConstraint(messageLifetimeToken, firstInstruction, lastValidBlockHeight) {
535
- if (!firstInstruction || !isAdvanceNonceAccountInstruction(firstInstruction)) {
536
- return {
537
- blockhash: messageLifetimeToken,
538
- lastValidBlockHeight: lastValidBlockHeight ?? 2n ** 64n - 1n
539
- // U64 MAX
540
- };
541
- } else {
542
- const nonceAccountAddress = firstInstruction.accounts[0].address;
543
- assertIsAddress(nonceAccountAddress);
544
- const nonceAuthorityAddress = firstInstruction.accounts[2].address;
545
- assertIsAddress(nonceAuthorityAddress);
546
- return {
547
- nonce: messageLifetimeToken,
548
- nonceAccountAddress,
549
- nonceAuthorityAddress
550
- };
551
- }
552
- }
553
- function convertSignatures(compiledTransaction) {
554
- const {
555
- compiledMessage: { staticAccounts },
556
- signatures
557
- } = compiledTransaction;
558
- return signatures.reduce((acc, sig, index) => {
559
- const allZeros = sig.every((byte) => byte === 0);
560
- if (allZeros)
561
- return acc;
562
- const address = staticAccounts[index];
563
- return { ...acc, [address]: sig };
564
- }, {});
565
- }
566
- function decompileTransaction(compiledTransaction, lastValidBlockHeight) {
567
- const { compiledMessage } = compiledTransaction;
568
- if ("addressTableLookups" in compiledMessage && compiledMessage.addressTableLookups.length > 0) {
569
- throw new Error("Cannot convert transaction with addressTableLookups");
570
- }
571
- const feePayer = compiledMessage.staticAccounts[0];
572
- if (!feePayer)
573
- throw new Error("No fee payer set in CompiledTransaction");
574
- const accountMetas = getAccountMetas(compiledMessage);
575
- const instructions = compiledMessage.instructions.map(
576
- (compiledInstruction) => convertInstruction(compiledInstruction, accountMetas)
577
- );
578
- const firstInstruction = instructions[0];
579
- const lifetimeConstraint = getLifetimeConstraint(
580
- compiledMessage.lifetimeToken,
581
- firstInstruction,
582
- lastValidBlockHeight
583
- );
584
- const signatures = convertSignatures(compiledTransaction);
585
- return pipe(
586
- createTransaction({ version: compiledMessage.version }),
587
- (tx) => setTransactionFeePayer(feePayer, tx),
588
- (tx) => instructions.reduce((acc, instruction) => {
589
- return appendTransactionInstruction(instruction, acc);
590
- }, tx),
591
- (tx) => "blockhash" in lifetimeConstraint ? setTransactionLifetimeUsingBlockhash(lifetimeConstraint, tx) : setTransactionLifetimeUsingDurableNonce(lifetimeConstraint, tx),
592
- (tx) => compiledTransaction.signatures.length ? { ...tx, signatures } : tx
593
- );
594
- }
595
468
  var lookupTableAddressDescription = __DEV__ ? "The address of the address lookup table account from which instruction addresses should be looked up" : "lookupTableAddress";
596
469
  var writableIndicesDescription = __DEV__ ? "The indices of the accounts in the lookup table that should be loaded as writeable" : "writableIndices";
597
470
  var readableIndicesDescription = __DEV__ ? "The indices of the accounts in the lookup table that should be loaded as read-only" : "readableIndices";
@@ -926,6 +799,137 @@ function getCompiledMessageDecoder() {
926
799
  }
927
800
  );
928
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
+ }
929
933
 
930
934
  // src/serializers/transaction.ts
931
935
  var signaturesDescription = __DEV__ ? "A compact array of 64-byte, base-64 encoded Ed25519 signatures" : "signatures";
@@ -979,52 +983,20 @@ function getTransactionDecoder(lastValidBlockHeight) {
979
983
  function getTransactionCodec(lastValidBlockHeight) {
980
984
  return combineCodec(getTransactionEncoder(), getTransactionDecoder(lastValidBlockHeight));
981
985
  }
982
- function assertIsTransactionSignature(putativeTransactionSignature) {
983
- try {
984
- if (
985
- // Lowest value (64 bytes of zeroes)
986
- putativeTransactionSignature.length < 64 || // Highest value (64 bytes of 255)
987
- putativeTransactionSignature.length > 88
988
- ) {
989
- throw new Error("Expected input string to decode to a byte array of length 64.");
990
- }
991
- const bytes = base58.serialize(putativeTransactionSignature);
992
- const numBytes = bytes.byteLength;
993
- if (numBytes !== 64) {
994
- throw new Error(`Expected input string to decode to a byte array of length 64. Actual length: ${numBytes}`);
995
- }
996
- } catch (e) {
997
- throw new Error(`\`${putativeTransactionSignature}\` is not a transaction signature`, {
998
- cause: e
999
- });
1000
- }
1001
- }
1002
- function isTransactionSignature(putativeTransactionSignature) {
1003
- if (
1004
- // Lowest value (64 bytes of zeroes)
1005
- putativeTransactionSignature.length < 64 || // Highest value (64 bytes of 255)
1006
- putativeTransactionSignature.length > 88
1007
- ) {
1008
- return false;
1009
- }
1010
- const bytes = base58.serialize(putativeTransactionSignature);
1011
- const numBytes = bytes.byteLength;
1012
- if (numBytes !== 64) {
1013
- return false;
1014
- }
1015
- return true;
1016
- }
986
+ var base58Decoder;
1017
987
  function getSignatureFromTransaction(transaction) {
988
+ if (!base58Decoder)
989
+ base58Decoder = getBase58Decoder();
1018
990
  const signatureBytes = transaction.signatures[transaction.feePayer];
1019
991
  if (!signatureBytes) {
1020
992
  throw new Error(
1021
993
  "Could not determine this transaction's signature. Make sure that the transaction has been signed by its fee payer."
1022
994
  );
1023
995
  }
1024
- const transactionSignature2 = base58.deserialize(signatureBytes)[0];
1025
- return transactionSignature2;
996
+ const transactionSignature = base58Decoder.decode(signatureBytes)[0];
997
+ return transactionSignature;
1026
998
  }
1027
- async function signTransaction(keyPairs, transaction) {
999
+ async function partiallySignTransaction(keyPairs, transaction) {
1028
1000
  const compiledMessage = compileMessage(transaction);
1029
1001
  const nextSignatures = "signatures" in transaction ? { ...transaction.signatures } : {};
1030
1002
  const wireMessageBytes = getCompiledMessageEncoder().encode(compiledMessage);
@@ -1043,9 +1015,11 @@ async function signTransaction(keyPairs, transaction) {
1043
1015
  Object.freeze(out);
1044
1016
  return out;
1045
1017
  }
1046
- function transactionSignature(putativeTransactionSignature) {
1047
- assertIsTransactionSignature(putativeTransactionSignature);
1048
- return putativeTransactionSignature;
1018
+ async function signTransaction(keyPairs, transaction) {
1019
+ const out = await partiallySignTransaction(keyPairs, transaction);
1020
+ assertTransactionIsFullySigned(out);
1021
+ Object.freeze(out);
1022
+ return out;
1049
1023
  }
1050
1024
  function assertTransactionIsFullySigned(transaction) {
1051
1025
  const signerAddressesFromInstructions = transaction.instructions.flatMap((i) => i.accounts?.filter((a) => isSignerRole(a.role)) ?? []).map((a) => a.address);
@@ -1056,15 +1030,11 @@ function assertTransactionIsFullySigned(transaction) {
1056
1030
  }
1057
1031
  });
1058
1032
  }
1059
-
1060
- // src/wire-transaction.ts
1061
1033
  function getBase64EncodedWireTransaction(transaction) {
1062
1034
  const wireTransactionBytes = getTransactionEncoder().encode(transaction);
1063
- {
1064
- return btoa(String.fromCharCode(...wireTransactionBytes));
1065
- }
1035
+ return getBase64Decoder().decode(wireTransactionBytes)[0];
1066
1036
  }
1067
1037
 
1068
- export { appendTransactionInstruction, assertIsBlockhash, assertIsDurableNonceTransaction, assertIsTransactionSignature, assertTransactionIsFullySigned, createTransaction, getBase64EncodedWireTransaction, getSignatureFromTransaction, getTransactionCodec, getTransactionDecoder, getTransactionEncoder, isAdvanceNonceAccountInstruction, isTransactionSignature, prependTransactionInstruction, setTransactionFeePayer, setTransactionLifetimeUsingBlockhash, setTransactionLifetimeUsingDurableNonce, signTransaction, transactionSignature };
1038
+ export { appendTransactionInstruction, assertIsBlockhash, assertIsDurableNonceTransaction, assertTransactionIsFullySigned, compileMessage, createTransaction, getBase64EncodedWireTransaction, getCompiledMessageCodec, getCompiledMessageDecoder, getCompiledMessageEncoder, getSignatureFromTransaction, getTransactionCodec, getTransactionDecoder, getTransactionEncoder, isAdvanceNonceAccountInstruction, partiallySignTransaction, prependTransactionInstruction, setTransactionFeePayer, setTransactionLifetimeUsingBlockhash, setTransactionLifetimeUsingDurableNonce, signTransaction };
1069
1039
  //# sourceMappingURL=out.js.map
1070
1040
  //# sourceMappingURL=index.native.js.map