@solana/transactions 2.0.0-experimental.f07dced → 2.0.0-experimental.f2b6238

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,27 @@
1
1
  'use strict';
2
2
 
3
3
  var umiSerializers = require('@metaplex-foundation/umi-serializers');
4
+ var addresses = require('@solana/addresses');
4
5
  var keys = require('@solana/keys');
5
6
 
6
7
  // ../build-scripts/env-shim.ts
7
8
  var __DEV__ = /* @__PURE__ */ (() => process["env"].NODE_ENV === "development")();
9
+
10
+ // src/unsigned-transaction.ts
11
+ function getUnsignedTransaction(transaction) {
12
+ if ("signatures" in transaction) {
13
+ const {
14
+ signatures: _,
15
+ // eslint-disable-line @typescript-eslint/no-unused-vars
16
+ ...unsignedTransaction
17
+ } = transaction;
18
+ return unsignedTransaction;
19
+ } else {
20
+ return transaction;
21
+ }
22
+ }
23
+
24
+ // src/blockhash.ts
8
25
  function assertIsBlockhash(putativeBlockhash) {
9
26
  try {
10
27
  if (
@@ -25,6 +42,17 @@ function assertIsBlockhash(putativeBlockhash) {
25
42
  });
26
43
  }
27
44
  }
45
+ function setTransactionLifetimeUsingBlockhash(blockhashLifetimeConstraint, transaction) {
46
+ if ("lifetimeConstraint" in transaction && transaction.lifetimeConstraint.blockhash === blockhashLifetimeConstraint.blockhash && transaction.lifetimeConstraint.lastValidBlockHeight === blockhashLifetimeConstraint.lastValidBlockHeight) {
47
+ return transaction;
48
+ }
49
+ const out = {
50
+ ...getUnsignedTransaction(transaction),
51
+ lifetimeConstraint: blockhashLifetimeConstraint
52
+ };
53
+ Object.freeze(out);
54
+ return out;
55
+ }
28
56
 
29
57
  // src/create-transaction.ts
30
58
  function createTransaction({
@@ -38,66 +66,6 @@ function createTransaction({
38
66
  return out;
39
67
  }
40
68
 
41
- // src/fee-payer.ts
42
- function setTransactionFeePayer(feePayer, transaction) {
43
- if ("feePayer" in transaction && feePayer === transaction.feePayer) {
44
- return transaction;
45
- }
46
- let out;
47
- if ("signatures" in transaction) {
48
- const {
49
- signatures: _,
50
- // eslint-disable-line @typescript-eslint/no-unused-vars
51
- ...unsignedTransaction
52
- } = transaction;
53
- out = {
54
- ...unsignedTransaction,
55
- feePayer
56
- };
57
- } else {
58
- out = {
59
- ...transaction,
60
- feePayer
61
- };
62
- }
63
- Object.freeze(out);
64
- return out;
65
- }
66
-
67
- // src/instructions.ts
68
- function replaceInstructions(transaction, nextInstructions) {
69
- let out;
70
- if ("signatures" in transaction) {
71
- const {
72
- signatures: _,
73
- // eslint-disable-line @typescript-eslint/no-unused-vars
74
- ...unsignedTransaction
75
- } = transaction;
76
- out = {
77
- ...unsignedTransaction,
78
- instructions: nextInstructions
79
- };
80
- } else {
81
- out = {
82
- ...transaction,
83
- instructions: nextInstructions
84
- };
85
- }
86
- return out;
87
- }
88
- function appendTransactionInstruction(instruction, transaction) {
89
- const nextInstructions = [...transaction.instructions, instruction];
90
- const out = replaceInstructions(transaction, nextInstructions);
91
- Object.freeze(out);
92
- return out;
93
- }
94
- function prependTransactionInstruction(instruction, transaction) {
95
- const nextInstructions = [instruction, ...transaction.instructions];
96
- const out = replaceInstructions(transaction, nextInstructions);
97
- Object.freeze(out);
98
- return out;
99
- }
100
-
101
69
  // ../instructions/dist/index.browser.js
102
70
  var AccountRole = /* @__PURE__ */ ((AccountRole2) => {
103
71
  AccountRole2[AccountRole2["WRITABLE_SIGNER"] = /* 3 */
@@ -120,6 +88,96 @@ function isWritableRole(role) {
120
88
  function mergeRoles(roleA, roleB) {
121
89
  return roleA | roleB;
122
90
  }
91
+
92
+ // src/durable-nonce.ts
93
+ var RECENT_BLOCKHASHES_SYSVAR_ADDRESS = "SysvarRecentB1ockHashes11111111111111111111";
94
+ var SYSTEM_PROGRAM_ADDRESS = "11111111111111111111111111111111";
95
+ function assertIsDurableNonceTransaction(transaction) {
96
+ if (!isDurableNonceTransaction(transaction)) {
97
+ throw new Error("Transaction is not a durable nonce transaction");
98
+ }
99
+ }
100
+ function createAdvanceNonceAccountInstruction(nonceAccountAddress, nonceAuthorityAddress) {
101
+ return {
102
+ accounts: [
103
+ { address: nonceAccountAddress, role: AccountRole.WRITABLE },
104
+ {
105
+ address: RECENT_BLOCKHASHES_SYSVAR_ADDRESS,
106
+ role: AccountRole.READONLY
107
+ },
108
+ { address: nonceAuthorityAddress, role: AccountRole.READONLY_SIGNER }
109
+ ],
110
+ data: new Uint8Array([4, 0, 0, 0]),
111
+ programAddress: SYSTEM_PROGRAM_ADDRESS
112
+ };
113
+ }
114
+ function isAdvanceNonceAccountInstruction(instruction) {
115
+ return instruction.programAddress === SYSTEM_PROGRAM_ADDRESS && // Test for `AdvanceNonceAccount` instruction data
116
+ instruction.data != null && isAdvanceNonceAccountInstructionData(instruction.data) && // Test for exactly 3 accounts
117
+ instruction.accounts?.length === 3 && // First account is nonce account address
118
+ instruction.accounts[0].address != null && instruction.accounts[0].role === AccountRole.WRITABLE && // Second account is recent blockhashes sysvar
119
+ instruction.accounts[1].address === RECENT_BLOCKHASHES_SYSVAR_ADDRESS && instruction.accounts[1].role === AccountRole.READONLY && // Third account is nonce authority account
120
+ instruction.accounts[2].address != null && instruction.accounts[2].role === AccountRole.READONLY_SIGNER;
121
+ }
122
+ function isAdvanceNonceAccountInstructionData(data) {
123
+ return data.byteLength === 4 && data[0] === 4 && data[1] === 0 && data[2] === 0 && data[3] === 0;
124
+ }
125
+ function isDurableNonceTransaction(transaction) {
126
+ return "lifetimeConstraint" in transaction && typeof transaction.lifetimeConstraint.nonce === "string" && transaction.instructions[0] != null && isAdvanceNonceAccountInstruction(transaction.instructions[0]);
127
+ }
128
+ function setTransactionLifetimeUsingDurableNonce({
129
+ nonce,
130
+ nonceAccountAddress,
131
+ nonceAuthorityAddress
132
+ }, transaction) {
133
+ const isAlreadyDurableNonceTransaction = isDurableNonceTransaction(transaction);
134
+ if (isAlreadyDurableNonceTransaction && transaction.lifetimeConstraint.nonce === nonce && transaction.instructions[0].accounts[0].address === nonceAccountAddress && transaction.instructions[0].accounts[2].address === nonceAuthorityAddress) {
135
+ return transaction;
136
+ }
137
+ const out = {
138
+ ...getUnsignedTransaction(transaction),
139
+ instructions: [
140
+ createAdvanceNonceAccountInstruction(nonceAccountAddress, nonceAuthorityAddress),
141
+ ...isAlreadyDurableNonceTransaction ? transaction.instructions.slice(1) : transaction.instructions
142
+ ],
143
+ lifetimeConstraint: {
144
+ nonce
145
+ }
146
+ };
147
+ Object.freeze(out);
148
+ return out;
149
+ }
150
+
151
+ // src/fee-payer.ts
152
+ function setTransactionFeePayer(feePayer, transaction) {
153
+ if ("feePayer" in transaction && feePayer === transaction.feePayer) {
154
+ return transaction;
155
+ }
156
+ const out = {
157
+ ...getUnsignedTransaction(transaction),
158
+ feePayer
159
+ };
160
+ Object.freeze(out);
161
+ return out;
162
+ }
163
+
164
+ // src/instructions.ts
165
+ function appendTransactionInstruction(instruction, transaction) {
166
+ const out = {
167
+ ...getUnsignedTransaction(transaction),
168
+ instructions: [...transaction.instructions, instruction]
169
+ };
170
+ Object.freeze(out);
171
+ return out;
172
+ }
173
+ function prependTransactionInstruction(instruction, transaction) {
174
+ const out = {
175
+ ...getUnsignedTransaction(transaction),
176
+ instructions: [instruction, ...transaction.instructions]
177
+ };
178
+ Object.freeze(out);
179
+ return out;
180
+ }
123
181
  function upsert(addressMap, address, update) {
124
182
  addressMap[address] = update(addressMap[address] ?? { role: AccountRole.READONLY });
125
183
  }
@@ -172,7 +230,7 @@ function getAddressMapFromInstructions(feePayer, instructions) {
172
230
  const shouldReplaceEntry = (
173
231
  // Consider using the new LOOKUP_TABLE if its address is different...
174
232
  entry.lookupTableAddress !== accountMeta.lookupTableAddress && // ...and sorts before the existing one.
175
- (addressComparator || (addressComparator = keys.getBase58EncodedAddressComparator()))(
233
+ (addressComparator || (addressComparator = addresses.getBase58EncodedAddressComparator()))(
176
234
  accountMeta.lookupTableAddress,
177
235
  entry.lookupTableAddress
178
236
  ) < 0
@@ -278,7 +336,7 @@ function getOrderedAccountsFromAddressMap(addressMap) {
278
336
  if (leftIsWritable !== isWritableRole(rightEntry.role)) {
279
337
  return leftIsWritable ? -1 : 1;
280
338
  }
281
- addressComparator || (addressComparator = keys.getBase58EncodedAddressComparator());
339
+ addressComparator || (addressComparator = addresses.getBase58EncodedAddressComparator());
282
340
  if (leftEntry[TYPE] === 1 /* LOOKUP_TABLE */ && rightEntry[TYPE] === 1 /* LOOKUP_TABLE */ && leftEntry.lookupTableAddress !== rightEntry.lookupTableAddress) {
283
341
  return addressComparator(leftEntry.lookupTableAddress, rightEntry.lookupTableAddress);
284
342
  } else {
@@ -307,7 +365,7 @@ function getCompiledAddressTableLookups(orderedAccounts) {
307
365
  entry.readableIndices.push(account.addressIndex);
308
366
  }
309
367
  }
310
- return Object.keys(index).sort(keys.getBase58EncodedAddressComparator()).map((lookupTableAddress) => ({
368
+ return Object.keys(index).sort(addresses.getBase58EncodedAddressComparator()).map((lookupTableAddress) => ({
311
369
  lookupTableAddress,
312
370
  ...index[lookupTableAddress]
313
371
  }));
@@ -391,7 +449,7 @@ function getAddressTableLookupCodec() {
391
449
  [
392
450
  [
393
451
  "lookupTableAddress",
394
- keys.getBase58EncodedAddressCodec(
452
+ addresses.getBase58EncodedAddressCodec(
395
453
  __DEV__ ? {
396
454
  description: "The address of the address lookup table account from which instruction addresses should be looked up"
397
455
  } : void 0
@@ -466,7 +524,7 @@ function getInstructionCodec() {
466
524
  )
467
525
  ],
468
526
  [
469
- "addressIndices",
527
+ "accountIndices",
470
528
  umiSerializers.array(
471
529
  umiSerializers.u8({
472
530
  description: __DEV__ ? "The index of an account, according to the well-ordered accounts list for this transaction" : ""
@@ -486,23 +544,23 @@ function getInstructionCodec() {
486
544
  ]
487
545
  ]),
488
546
  (value) => {
489
- if (value.addressIndices !== void 0 && value.data !== void 0) {
547
+ if (value.accountIndices !== void 0 && value.data !== void 0) {
490
548
  return value;
491
549
  }
492
550
  return {
493
551
  ...value,
494
- addressIndices: value.addressIndices ?? [],
552
+ accountIndices: value.accountIndices ?? [],
495
553
  data: value.data ?? new Uint8Array(0)
496
554
  };
497
555
  },
498
556
  (value) => {
499
- if (value.addressIndices.length && value.data.byteLength) {
557
+ if (value.accountIndices.length && value.data.byteLength) {
500
558
  return value;
501
559
  }
502
- const { addressIndices, data, ...rest } = value;
560
+ const { accountIndices, data, ...rest } = value;
503
561
  return {
504
562
  ...rest,
505
- ...addressIndices.length ? { addressIndices } : null,
563
+ ...accountIndices.length ? { accountIndices } : null,
506
564
  ...data.byteLength ? { data } : null
507
565
  };
508
566
  }
@@ -588,7 +646,7 @@ function getPreludeStructSerializerTuple() {
588
646
  ["header", getMessageHeaderCodec()],
589
647
  [
590
648
  "staticAccounts",
591
- umiSerializers.array(keys.getBase58EncodedAddressCodec(), {
649
+ umiSerializers.array(addresses.getBase58EncodedAddressCodec(), {
592
650
  description: __DEV__ ? "A compact-array of static account addresses belonging to this transaction" : "",
593
651
  size: umiSerializers.shortU16()
594
652
  })
@@ -630,16 +688,20 @@ async function getCompiledMessageSignature(message, secretKey) {
630
688
  const signature = await keys.signBytes(secretKey, wireMessageBytes);
631
689
  return signature;
632
690
  }
633
- async function signTransaction(keyPair, transaction) {
691
+ async function signTransaction(keyPairs, transaction) {
634
692
  const compiledMessage = compileMessage(transaction);
635
- const [signerPublicKey, signature] = await Promise.all([
636
- keys.getBase58EncodedAddressFromPublicKey(keyPair.publicKey),
637
- getCompiledMessageSignature(compiledMessage, keyPair.privateKey)
638
- ]);
639
- const nextSignatures = {
640
- ..."signatures" in transaction ? transaction.signatures : null,
641
- ...{ [signerPublicKey]: signature }
642
- };
693
+ const nextSignatures = "signatures" in transaction ? { ...transaction.signatures } : {};
694
+ const publicKeySignaturePairs = await Promise.all(
695
+ keyPairs.map(
696
+ (keyPair) => Promise.all([
697
+ addresses.getAddressFromPublicKey(keyPair.publicKey),
698
+ getCompiledMessageSignature(compiledMessage, keyPair.privateKey)
699
+ ])
700
+ )
701
+ );
702
+ for (const [signerPublicKey, signature] of publicKeySignaturePairs) {
703
+ nextSignatures[signerPublicKey] = signature;
704
+ }
643
705
  const out = {
644
706
  ...transaction,
645
707
  signatures: nextSignatures
@@ -703,10 +765,13 @@ function getBase64EncodedWireTransaction(transaction) {
703
765
 
704
766
  exports.appendTransactionInstruction = appendTransactionInstruction;
705
767
  exports.assertIsBlockhash = assertIsBlockhash;
768
+ exports.assertIsDurableNonceTransaction = assertIsDurableNonceTransaction;
706
769
  exports.createTransaction = createTransaction;
707
770
  exports.getBase64EncodedWireTransaction = getBase64EncodedWireTransaction;
708
771
  exports.prependTransactionInstruction = prependTransactionInstruction;
709
772
  exports.setTransactionFeePayer = setTransactionFeePayer;
773
+ exports.setTransactionLifetimeUsingBlockhash = setTransactionLifetimeUsingBlockhash;
774
+ exports.setTransactionLifetimeUsingDurableNonce = setTransactionLifetimeUsingDurableNonce;
710
775
  exports.signTransaction = signTransaction;
711
776
  //# sourceMappingURL=out.js.map
712
777
  //# sourceMappingURL=index.browser.cjs.map