@solana/transactions 2.0.0-experimental.2bae733 → 2.0.0-experimental.2f6fdd4

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.
Files changed (52) hide show
  1. package/dist/index.browser.cjs +204 -68
  2. package/dist/index.browser.cjs.map +1 -1
  3. package/dist/index.browser.js +196 -67
  4. package/dist/index.browser.js.map +1 -1
  5. package/dist/index.development.js +264 -125
  6. package/dist/index.development.js.map +1 -1
  7. package/dist/index.native.js +196 -65
  8. package/dist/index.native.js.map +1 -1
  9. package/dist/index.node.cjs +206 -68
  10. package/dist/index.node.cjs.map +1 -1
  11. package/dist/index.node.js +196 -65
  12. package/dist/index.node.js.map +1 -1
  13. package/dist/index.production.min.js +11 -6
  14. package/dist/types/accounts.d.ts +1 -1
  15. package/dist/types/accounts.d.ts.map +1 -0
  16. package/dist/types/blockhash.d.ts +9 -1
  17. package/dist/types/blockhash.d.ts.map +1 -0
  18. package/dist/types/compile-address-table-lookups.d.ts +1 -1
  19. package/dist/types/compile-address-table-lookups.d.ts.map +1 -0
  20. package/dist/types/compile-header.d.ts.map +1 -0
  21. package/dist/types/compile-instructions.d.ts +1 -1
  22. package/dist/types/compile-instructions.d.ts.map +1 -0
  23. package/dist/types/compile-lifetime-token.d.ts.map +1 -0
  24. package/dist/types/compile-static-accounts.d.ts +1 -1
  25. package/dist/types/compile-static-accounts.d.ts.map +1 -0
  26. package/dist/types/compile-transaction.d.ts +11 -0
  27. package/dist/types/compile-transaction.d.ts.map +1 -0
  28. package/dist/types/create-transaction.d.ts.map +1 -0
  29. package/dist/types/durable-nonce.d.ts +18 -6
  30. package/dist/types/durable-nonce.d.ts.map +1 -0
  31. package/dist/types/fee-payer.d.ts +1 -1
  32. package/dist/types/fee-payer.d.ts.map +1 -0
  33. package/dist/types/index.d.ts +1 -0
  34. package/dist/types/index.d.ts.map +1 -0
  35. package/dist/types/instructions.d.ts.map +1 -0
  36. package/dist/types/message.d.ts.map +1 -0
  37. package/dist/types/serializers/address-table-lookup.d.ts.map +1 -0
  38. package/dist/types/serializers/header.d.ts.map +1 -0
  39. package/dist/types/serializers/instruction.d.ts.map +1 -0
  40. package/dist/types/serializers/message.d.ts.map +1 -0
  41. package/dist/types/serializers/transaction-version.d.ts.map +1 -0
  42. package/dist/types/serializers/transaction.d.ts +6 -0
  43. package/dist/types/serializers/transaction.d.ts.map +1 -0
  44. package/dist/types/serializers/unimplemented.d.ts.map +1 -0
  45. package/dist/types/signatures.d.ts +3 -4
  46. package/dist/types/signatures.d.ts.map +1 -0
  47. package/dist/types/types.d.ts.map +1 -0
  48. package/dist/types/unsigned-transaction.d.ts +4 -0
  49. package/dist/types/unsigned-transaction.d.ts.map +1 -0
  50. package/dist/types/wire-transaction.d.ts +6 -0
  51. package/dist/types/wire-transaction.d.ts.map +1 -0
  52. package/package.json +12 -16
@@ -1,77 +1,65 @@
1
- import { getBase58EncodedAddressFromPublicKey, signBytes, getBase58EncodedAddressComparator, getBase58EncodedAddressCodec } from '@solana/keys';
2
- import { struct, mapSerializer, array, shortU16, string, base58, u8, bytes } from '@metaplex-foundation/umi-serializers';
1
+ import { base58, struct, array, bytes, shortU16, mapSerializer, string, u8 } from '@metaplex-foundation/umi-serializers';
2
+ import { getAddressFromPublicKey, getBase58EncodedAddressComparator, getBase58EncodedAddressCodec } from '@solana/addresses';
3
+ import { signBytes } from '@solana/keys';
3
4
 
4
5
  // ../build-scripts/env-shim.ts
5
6
  var __DEV__ = /* @__PURE__ */ (() => process["env"].NODE_ENV === "development")();
6
7
 
7
- // src/create-transaction.ts
8
- function createTransaction({
9
- version
10
- }) {
11
- const out = {
12
- instructions: [],
13
- version
14
- };
15
- Object.freeze(out);
16
- return out;
17
- }
18
-
19
- // src/fee-payer.ts
20
- function setTransactionFeePayer(feePayer, transaction) {
21
- if ("feePayer" in transaction && feePayer === transaction.feePayer) {
22
- return transaction;
23
- }
24
- let out;
8
+ // src/unsigned-transaction.ts
9
+ function getUnsignedTransaction(transaction) {
25
10
  if ("signatures" in transaction) {
26
11
  const {
27
12
  signatures: _,
28
13
  // eslint-disable-line @typescript-eslint/no-unused-vars
29
14
  ...unsignedTransaction
30
15
  } = transaction;
31
- out = {
32
- ...unsignedTransaction,
33
- feePayer
34
- };
16
+ return unsignedTransaction;
35
17
  } else {
36
- out = {
37
- ...transaction,
38
- feePayer
39
- };
18
+ return transaction;
40
19
  }
41
- Object.freeze(out);
42
- return out;
43
20
  }
44
21
 
45
- // src/instructions.ts
46
- function replaceInstructions(transaction, nextInstructions) {
47
- let out;
48
- if ("signatures" in transaction) {
49
- const {
50
- signatures: _,
51
- // eslint-disable-line @typescript-eslint/no-unused-vars
52
- ...unsignedTransaction
53
- } = transaction;
54
- out = {
55
- ...unsignedTransaction,
56
- instructions: nextInstructions
57
- };
58
- } else {
59
- out = {
60
- ...transaction,
61
- instructions: nextInstructions
62
- };
22
+ // src/blockhash.ts
23
+ function assertIsBlockhash(putativeBlockhash) {
24
+ try {
25
+ if (
26
+ // Lowest value (32 bytes of zeroes)
27
+ putativeBlockhash.length < 32 || // Highest value (32 bytes of 255)
28
+ putativeBlockhash.length > 44
29
+ ) {
30
+ throw new Error("Expected input string to decode to a byte array of length 32.");
31
+ }
32
+ const bytes3 = base58.serialize(putativeBlockhash);
33
+ const numBytes = bytes3.byteLength;
34
+ if (numBytes !== 32) {
35
+ throw new Error(`Expected input string to decode to a byte array of length 32. Actual length: ${numBytes}`);
36
+ }
37
+ } catch (e) {
38
+ throw new Error(`\`${putativeBlockhash}\` is not a blockhash`, {
39
+ cause: e
40
+ });
63
41
  }
64
- return out;
65
42
  }
66
- function appendTransactionInstruction(instruction, transaction) {
67
- const nextInstructions = [...transaction.instructions, instruction];
68
- const out = replaceInstructions(transaction, nextInstructions);
43
+ function setTransactionLifetimeUsingBlockhash(blockhashLifetimeConstraint, transaction) {
44
+ if ("lifetimeConstraint" in transaction && transaction.lifetimeConstraint.blockhash === blockhashLifetimeConstraint.blockhash && transaction.lifetimeConstraint.lastValidBlockHeight === blockhashLifetimeConstraint.lastValidBlockHeight) {
45
+ return transaction;
46
+ }
47
+ const out = {
48
+ ...getUnsignedTransaction(transaction),
49
+ lifetimeConstraint: blockhashLifetimeConstraint
50
+ };
69
51
  Object.freeze(out);
70
52
  return out;
71
53
  }
72
- function prependTransactionInstruction(instruction, transaction) {
73
- const nextInstructions = [instruction, ...transaction.instructions];
74
- const out = replaceInstructions(transaction, nextInstructions);
54
+
55
+ // src/create-transaction.ts
56
+ function createTransaction({
57
+ version
58
+ }) {
59
+ const out = {
60
+ instructions: [],
61
+ version
62
+ };
75
63
  Object.freeze(out);
76
64
  return out;
77
65
  }
@@ -98,6 +86,96 @@ function isWritableRole(role) {
98
86
  function mergeRoles(roleA, roleB) {
99
87
  return roleA | roleB;
100
88
  }
89
+
90
+ // src/durable-nonce.ts
91
+ var RECENT_BLOCKHASHES_SYSVAR_ADDRESS = "SysvarRecentB1ockHashes11111111111111111111";
92
+ var SYSTEM_PROGRAM_ADDRESS = "11111111111111111111111111111111";
93
+ function assertIsDurableNonceTransaction(transaction) {
94
+ if (!isDurableNonceTransaction(transaction)) {
95
+ throw new Error("Transaction is not a durable nonce transaction");
96
+ }
97
+ }
98
+ function createAdvanceNonceAccountInstruction(nonceAccountAddress, nonceAuthorityAddress) {
99
+ return {
100
+ accounts: [
101
+ { address: nonceAccountAddress, role: AccountRole.WRITABLE },
102
+ {
103
+ address: RECENT_BLOCKHASHES_SYSVAR_ADDRESS,
104
+ role: AccountRole.READONLY
105
+ },
106
+ { address: nonceAuthorityAddress, role: AccountRole.READONLY_SIGNER }
107
+ ],
108
+ data: new Uint8Array([4, 0, 0, 0]),
109
+ programAddress: SYSTEM_PROGRAM_ADDRESS
110
+ };
111
+ }
112
+ function isAdvanceNonceAccountInstruction(instruction) {
113
+ return instruction.programAddress === SYSTEM_PROGRAM_ADDRESS && // Test for `AdvanceNonceAccount` instruction data
114
+ instruction.data != null && isAdvanceNonceAccountInstructionData(instruction.data) && // Test for exactly 3 accounts
115
+ instruction.accounts?.length === 3 && // First account is nonce account address
116
+ instruction.accounts[0].address != null && instruction.accounts[0].role === AccountRole.WRITABLE && // Second account is recent blockhashes sysvar
117
+ instruction.accounts[1].address === RECENT_BLOCKHASHES_SYSVAR_ADDRESS && instruction.accounts[1].role === AccountRole.READONLY && // Third account is nonce authority account
118
+ instruction.accounts[2].address != null && instruction.accounts[2].role === AccountRole.READONLY_SIGNER;
119
+ }
120
+ function isAdvanceNonceAccountInstructionData(data) {
121
+ return data.byteLength === 4 && data[0] === 4 && data[1] === 0 && data[2] === 0 && data[3] === 0;
122
+ }
123
+ function isDurableNonceTransaction(transaction) {
124
+ return "lifetimeConstraint" in transaction && typeof transaction.lifetimeConstraint.nonce === "string" && transaction.instructions[0] != null && isAdvanceNonceAccountInstruction(transaction.instructions[0]);
125
+ }
126
+ function setTransactionLifetimeUsingDurableNonce({
127
+ nonce,
128
+ nonceAccountAddress,
129
+ nonceAuthorityAddress
130
+ }, transaction) {
131
+ const isAlreadyDurableNonceTransaction = isDurableNonceTransaction(transaction);
132
+ if (isAlreadyDurableNonceTransaction && transaction.lifetimeConstraint.nonce === nonce && transaction.instructions[0].accounts[0].address === nonceAccountAddress && transaction.instructions[0].accounts[2].address === nonceAuthorityAddress) {
133
+ return transaction;
134
+ }
135
+ const out = {
136
+ ...getUnsignedTransaction(transaction),
137
+ instructions: [
138
+ createAdvanceNonceAccountInstruction(nonceAccountAddress, nonceAuthorityAddress),
139
+ ...isAlreadyDurableNonceTransaction ? transaction.instructions.slice(1) : transaction.instructions
140
+ ],
141
+ lifetimeConstraint: {
142
+ nonce
143
+ }
144
+ };
145
+ Object.freeze(out);
146
+ return out;
147
+ }
148
+
149
+ // src/fee-payer.ts
150
+ function setTransactionFeePayer(feePayer, transaction) {
151
+ if ("feePayer" in transaction && feePayer === transaction.feePayer) {
152
+ return transaction;
153
+ }
154
+ const out = {
155
+ ...getUnsignedTransaction(transaction),
156
+ feePayer
157
+ };
158
+ Object.freeze(out);
159
+ return out;
160
+ }
161
+
162
+ // src/instructions.ts
163
+ function appendTransactionInstruction(instruction, transaction) {
164
+ const out = {
165
+ ...getUnsignedTransaction(transaction),
166
+ instructions: [...transaction.instructions, instruction]
167
+ };
168
+ Object.freeze(out);
169
+ return out;
170
+ }
171
+ function prependTransactionInstruction(instruction, transaction) {
172
+ const out = {
173
+ ...getUnsignedTransaction(transaction),
174
+ instructions: [instruction, ...transaction.instructions]
175
+ };
176
+ Object.freeze(out);
177
+ return out;
178
+ }
101
179
  function upsert(addressMap, address, update) {
102
180
  addressMap[address] = update(addressMap[address] ?? { role: AccountRole.READONLY });
103
181
  }
@@ -444,7 +522,7 @@ function getInstructionCodec() {
444
522
  )
445
523
  ],
446
524
  [
447
- "addressIndices",
525
+ "accountIndices",
448
526
  array(
449
527
  u8({
450
528
  description: __DEV__ ? "The index of an account, according to the well-ordered accounts list for this transaction" : ""
@@ -464,23 +542,23 @@ function getInstructionCodec() {
464
542
  ]
465
543
  ]),
466
544
  (value) => {
467
- if (value.addressIndices !== void 0 && value.data !== void 0) {
545
+ if (value.accountIndices !== void 0 && value.data !== void 0) {
468
546
  return value;
469
547
  }
470
548
  return {
471
549
  ...value,
472
- addressIndices: value.addressIndices ?? [],
550
+ accountIndices: value.accountIndices ?? [],
473
551
  data: value.data ?? new Uint8Array(0)
474
552
  };
475
553
  },
476
554
  (value) => {
477
- if (value.addressIndices.length && value.data.byteLength) {
555
+ if (value.accountIndices.length && value.data.byteLength) {
478
556
  return value;
479
557
  }
480
- const { addressIndices, data, ...rest } = value;
558
+ const { accountIndices, data, ...rest } = value;
481
559
  return {
482
560
  ...rest,
483
- ...addressIndices.length ? { addressIndices } : null,
561
+ ...accountIndices.length ? { accountIndices } : null,
484
562
  ...data.byteLength ? { data } : null
485
563
  };
486
564
  }
@@ -507,8 +585,8 @@ var BASE_CONFIG = {
507
585
  fixedSize: null,
508
586
  maxSize: 1
509
587
  };
510
- function deserialize(bytes2, offset = 0) {
511
- const firstByte = bytes2[offset];
588
+ function deserialize(bytes3, offset = 0) {
589
+ const firstByte = bytes3[offset];
512
590
  if ((firstByte & VERSION_FLAG_MASK) === 0) {
513
591
  return ["legacy", offset];
514
592
  } else {
@@ -611,7 +689,7 @@ async function getCompiledMessageSignature(message, secretKey) {
611
689
  async function signTransaction(keyPair, transaction) {
612
690
  const compiledMessage = compileMessage(transaction);
613
691
  const [signerPublicKey, signature] = await Promise.all([
614
- getBase58EncodedAddressFromPublicKey(keyPair.publicKey),
692
+ getAddressFromPublicKey(keyPair.publicKey),
615
693
  getCompiledMessageSignature(compiledMessage, keyPair.privateKey)
616
694
  ]);
617
695
  const nextSignatures = {
@@ -626,6 +704,59 @@ async function signTransaction(keyPair, transaction) {
626
704
  return out;
627
705
  }
628
706
 
629
- export { appendTransactionInstruction, createTransaction, prependTransactionInstruction, setTransactionFeePayer, signTransaction };
707
+ // src/compile-transaction.ts
708
+ function getCompiledTransaction(transaction) {
709
+ const compiledMessage = compileMessage(transaction);
710
+ let signatures;
711
+ if ("signatures" in transaction) {
712
+ signatures = [];
713
+ for (let ii = 0; ii < compiledMessage.header.numSignerAccounts; ii++) {
714
+ signatures[ii] = transaction.signatures[compiledMessage.staticAccounts[ii]] ?? new Uint8Array(Array(64).fill(0));
715
+ }
716
+ } else {
717
+ signatures = Array(compiledMessage.header.numSignerAccounts).fill(new Uint8Array(Array(64).fill(0)));
718
+ }
719
+ return {
720
+ compiledMessage,
721
+ signatures
722
+ };
723
+ }
724
+
725
+ // src/serializers/transaction.ts
726
+ var BASE_CONFIG3 = {
727
+ description: __DEV__ ? "The wire format of a Solana transaction" : "",
728
+ fixedSize: null,
729
+ maxSize: null
730
+ };
731
+ function serialize3(transaction) {
732
+ const compiledTransaction = getCompiledTransaction(transaction);
733
+ return struct([
734
+ [
735
+ "signatures",
736
+ array(bytes({ size: 64 }), {
737
+ ...__DEV__ ? { description: "A compact array of 64-byte, base-64 encoded Ed25519 signatures" } : null,
738
+ size: shortU16()
739
+ })
740
+ ],
741
+ ["compiledMessage", getCompiledMessageEncoder()]
742
+ ]).serialize(compiledTransaction);
743
+ }
744
+ function getTransactionEncoder() {
745
+ return {
746
+ ...BASE_CONFIG3,
747
+ deserialize: getUnimplementedDecoder("CompiledMessage"),
748
+ serialize: serialize3
749
+ };
750
+ }
751
+
752
+ // src/wire-transaction.ts
753
+ function getBase64EncodedWireTransaction(transaction) {
754
+ const wireTransactionBytes = getTransactionEncoder().serialize(transaction);
755
+ {
756
+ return btoa(String.fromCharCode(...wireTransactionBytes));
757
+ }
758
+ }
759
+
760
+ export { appendTransactionInstruction, assertIsBlockhash, assertIsDurableNonceTransaction, createTransaction, getBase64EncodedWireTransaction, prependTransactionInstruction, setTransactionFeePayer, setTransactionLifetimeUsingBlockhash, setTransactionLifetimeUsingDurableNonce, signTransaction };
630
761
  //# sourceMappingURL=out.js.map
631
762
  //# sourceMappingURL=index.native.js.map