@secondlayer/stacks 5.1.3 → 6.1.0

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 (166) hide show
  1. package/README.md +1 -8
  2. package/dist/shared/{stacks-qt8ehebn.js → stacks-esm-0rdxcta7.js} +2 -2
  3. package/dist/shared/{stacks-qt8ehebn.js.map → stacks-esm-0rdxcta7.js.map} +1 -1
  4. package/dist/shared/{stacks-4hs8r4q2.js → stacks-esm-1jae0a3r.js} +2 -2
  5. package/dist/shared/{stacks-xjkn3en1.cjs.map → stacks-esm-1jae0a3r.js.map} +1 -1
  6. package/dist/shared/{stacks-4d7c3x27.js → stacks-esm-2mc7ebrm.js} +3 -3
  7. package/dist/shared/{stacks-4d7c3x27.js.map → stacks-esm-2mc7ebrm.js.map} +1 -1
  8. package/dist/shared/{stacks-8brgfvqx.js → stacks-esm-3g33bqsm.js} +4 -4
  9. package/dist/shared/{stacks-8brgfvqx.js.map → stacks-esm-3g33bqsm.js.map} +1 -1
  10. package/dist/shared/{stacks-gjz26k6b.js → stacks-esm-3pv5avgr.js} +59 -15
  11. package/dist/shared/stacks-esm-3pv5avgr.js.map +12 -0
  12. package/dist/shared/{stacks-qex691bh.js → stacks-esm-743jfh10.js} +6 -6
  13. package/dist/shared/{stacks-qex691bh.js.map → stacks-esm-743jfh10.js.map} +1 -1
  14. package/dist/shared/{stacks-wxfd6r4b.js → stacks-esm-7fpqgtyx.js} +3 -3
  15. package/dist/shared/{stacks-wxfd6r4b.js.map → stacks-esm-7fpqgtyx.js.map} +1 -1
  16. package/dist/shared/{stacks-71k0vmct.js → stacks-esm-9rnx0zxr.js} +5 -5
  17. package/dist/shared/{stacks-71k0vmct.js.map → stacks-esm-9rnx0zxr.js.map} +1 -1
  18. package/dist/shared/{stacks-fd4rx56e.js → stacks-esm-aw2mj3sj.js} +7 -7
  19. package/dist/shared/{stacks-fd4rx56e.js.map → stacks-esm-aw2mj3sj.js.map} +1 -1
  20. package/dist/shared/{stacks-qs5fzts5.js → stacks-esm-cvhazvhd.js} +2 -2
  21. package/dist/shared/{stacks-qs5fzts5.js.map → stacks-esm-cvhazvhd.js.map} +1 -1
  22. package/dist/shared/{stacks-69nq48jp.js → stacks-esm-fcfdn2zx.js} +5 -5
  23. package/dist/shared/{stacks-69nq48jp.js.map → stacks-esm-fcfdn2zx.js.map} +1 -1
  24. package/dist/shared/{stacks-x0qqwrev.js → stacks-esm-hr7fpxtj.js} +4 -4
  25. package/dist/shared/{stacks-x0qqwrev.js.map → stacks-esm-hr7fpxtj.js.map} +1 -1
  26. package/dist/shared/{stacks-mbt36bzt.js → stacks-esm-kds4n82p.js} +11 -11
  27. package/dist/shared/{stacks-mbt36bzt.js.map → stacks-esm-kds4n82p.js.map} +1 -1
  28. package/dist/shared/{stacks-kg1npstf.js → stacks-esm-n53kvwmt.js} +2 -2
  29. package/dist/shared/{stacks-kg1npstf.js.map → stacks-esm-n53kvwmt.js.map} +1 -1
  30. package/dist/shared/{stacks-v2rfr46a.js → stacks-esm-pxhfqfff.js} +4 -4
  31. package/dist/shared/{stacks-v2rfr46a.js.map → stacks-esm-pxhfqfff.js.map} +1 -1
  32. package/dist/shared/{stacks-yajyej2y.js → stacks-esm-q19a17fd.js} +3 -3
  33. package/dist/shared/{stacks-yajyej2y.js.map → stacks-esm-q19a17fd.js.map} +1 -1
  34. package/dist/shared/{stacks-v03zef3g.js → stacks-esm-qk0p9fr6.js} +9 -9
  35. package/dist/shared/{stacks-v03zef3g.js.map → stacks-esm-qk0p9fr6.js.map} +1 -1
  36. package/dist/shared/{stacks-qcae5852.js → stacks-esm-qkj67g80.js} +2 -2
  37. package/dist/shared/{stacks-qcae5852.js.map → stacks-esm-qkj67g80.js.map} +1 -1
  38. package/dist/shared/{stacks-mftyrc60.js → stacks-esm-r6dejrgr.js} +4 -4
  39. package/dist/shared/{stacks-mftyrc60.js.map → stacks-esm-r6dejrgr.js.map} +1 -1
  40. package/dist/shared/{stacks-9c1yam8b.js → stacks-esm-r9q4wnyw.js} +5 -5
  41. package/dist/shared/{stacks-9c1yam8b.js.map → stacks-esm-r9q4wnyw.js.map} +1 -1
  42. package/dist/shared/{stacks-51ybf135.js → stacks-esm-skmcf5n2.js} +5 -5
  43. package/dist/shared/{stacks-51ybf135.js.map → stacks-esm-skmcf5n2.js.map} +1 -1
  44. package/dist/shared/{stacks-yvb7rvwk.js → stacks-esm-v45990hx.js} +2 -2
  45. package/dist/shared/{stacks-8vzxax5e.cjs.map → stacks-esm-v45990hx.js.map} +1 -1
  46. package/dist/shared/{stacks-z90xdr1x.js → stacks-esm-wv67m4g6.js} +3 -3
  47. package/dist/shared/{stacks-z90xdr1x.js.map → stacks-esm-wv67m4g6.js.map} +1 -1
  48. package/dist/shared/{stacks-1m3m1agc.js → stacks-esm-x1r01353.js} +5 -5
  49. package/dist/shared/{stacks-1m3m1agc.js.map → stacks-esm-x1r01353.js.map} +1 -1
  50. package/dist/src/accounts/index.cjs +558 -4
  51. package/dist/src/accounts/index.cjs.map +10 -4
  52. package/dist/src/accounts/index.js +3 -3
  53. package/dist/src/actions/index.cjs +3815 -1
  54. package/dist/src/actions/index.cjs.map +65 -3
  55. package/dist/src/actions/index.js +14 -14
  56. package/dist/src/bitcoin/index.cjs +2477 -1
  57. package/dist/src/bitcoin/index.cjs.map +31 -3
  58. package/dist/src/bitcoin/index.js +11 -11
  59. package/dist/src/bns/index.cjs +4439 -17
  60. package/dist/src/bns/index.cjs.map +50 -3
  61. package/dist/src/bns/index.js +14 -14
  62. package/dist/src/chains/index.cjs +135 -1
  63. package/dist/src/chains/index.cjs.map +5 -3
  64. package/dist/src/chains/index.js +1 -1
  65. package/dist/src/clarity/index.cjs +2019 -1
  66. package/dist/src/clarity/index.cjs.map +24 -3
  67. package/dist/src/clarity/index.js +8 -8
  68. package/dist/src/filters/index.cjs +725 -1
  69. package/dist/src/filters/index.cjs.map +11 -3
  70. package/dist/src/filters/index.d.cts +76 -13
  71. package/dist/src/filters/index.d.ts +76 -13
  72. package/dist/src/filters/index.js +8 -4
  73. package/dist/src/filters/index.js.map +1 -1
  74. package/dist/src/index.cjs +3935 -62
  75. package/dist/src/index.cjs.map +69 -4
  76. package/dist/src/index.d.cts +62 -11
  77. package/dist/src/index.d.ts +62 -11
  78. package/dist/src/index.js +16 -16
  79. package/dist/src/postconditions/index.cjs +1726 -1
  80. package/dist/src/postconditions/index.cjs.map +20 -3
  81. package/dist/src/postconditions/index.js +6 -6
  82. package/dist/src/pox/index.cjs +3855 -1
  83. package/dist/src/pox/index.cjs.map +48 -3
  84. package/dist/src/pox/index.js +13 -13
  85. package/dist/src/pox5/index.cjs +4054 -29
  86. package/dist/src/pox5/index.cjs.map +54 -3
  87. package/dist/src/pox5/index.js +15 -15
  88. package/dist/src/sbtc/index.cjs +3705 -4
  89. package/dist/src/sbtc/index.cjs.map +47 -3
  90. package/dist/src/sbtc/index.js +12 -12
  91. package/dist/src/stackingdao/index.cjs +3751 -8
  92. package/dist/src/stackingdao/index.cjs.map +45 -3
  93. package/dist/src/stackingdao/index.js +12 -12
  94. package/dist/src/subscriptions/index.cjs +153 -1
  95. package/dist/src/subscriptions/index.cjs.map +6 -3
  96. package/dist/src/subscriptions/index.js +2 -2
  97. package/dist/src/transactions/index.cjs +2139 -1
  98. package/dist/src/transactions/index.cjs.map +23 -3
  99. package/dist/src/transactions/index.js +6 -6
  100. package/dist/src/utils/index.cjs +435 -3
  101. package/dist/src/utils/index.cjs.map +9 -3
  102. package/dist/src/utils/index.js +3 -3
  103. package/package.json +3 -23
  104. package/dist/shared/stacks-02actsrx.cjs +0 -17
  105. package/dist/shared/stacks-02actsrx.cjs.map +0 -10
  106. package/dist/shared/stacks-03vfm21t.cjs +0 -222
  107. package/dist/shared/stacks-03vfm21t.cjs.map +0 -12
  108. package/dist/shared/stacks-24h5k94q.cjs +0 -74
  109. package/dist/shared/stacks-24h5k94q.cjs.map +0 -11
  110. package/dist/shared/stacks-37kz5t9f.cjs +0 -764
  111. package/dist/shared/stacks-37kz5t9f.cjs.map +0 -23
  112. package/dist/shared/stacks-3mt2q2na.cjs +0 -65
  113. package/dist/shared/stacks-3mt2q2na.cjs.map +0 -9
  114. package/dist/shared/stacks-4hs8r4q2.js.map +0 -10
  115. package/dist/shared/stacks-8vzxax5e.cjs +0 -45
  116. package/dist/shared/stacks-agk106zs.cjs +0 -267
  117. package/dist/shared/stacks-agk106zs.cjs.map +0 -12
  118. package/dist/shared/stacks-bdgh9n83.cjs +0 -212
  119. package/dist/shared/stacks-bdgh9n83.cjs.map +0 -11
  120. package/dist/shared/stacks-dpxnxhqb.cjs +0 -51
  121. package/dist/shared/stacks-dpxnxhqb.cjs.map +0 -11
  122. package/dist/shared/stacks-ee53k9j2.cjs +0 -651
  123. package/dist/shared/stacks-ee53k9j2.cjs.map +0 -13
  124. package/dist/shared/stacks-enpj9krt.cjs +0 -38
  125. package/dist/shared/stacks-enpj9krt.cjs.map +0 -10
  126. package/dist/shared/stacks-f35mnrmg.cjs +0 -164
  127. package/dist/shared/stacks-f35mnrmg.cjs.map +0 -10
  128. package/dist/shared/stacks-fhzwsn5d.cjs +0 -261
  129. package/dist/shared/stacks-fhzwsn5d.cjs.map +0 -12
  130. package/dist/shared/stacks-gjz26k6b.js.map +0 -12
  131. package/dist/shared/stacks-gza2n1zs.cjs +0 -73
  132. package/dist/shared/stacks-gza2n1zs.cjs.map +0 -11
  133. package/dist/shared/stacks-kem68d80.cjs +0 -156
  134. package/dist/shared/stacks-kem68d80.cjs.map +0 -10
  135. package/dist/shared/stacks-n98w7g57.cjs +0 -234
  136. package/dist/shared/stacks-n98w7g57.cjs.map +0 -10
  137. package/dist/shared/stacks-p337hp6m.cjs +0 -336
  138. package/dist/shared/stacks-p337hp6m.cjs.map +0 -25
  139. package/dist/shared/stacks-q34k4vnc.cjs +0 -286
  140. package/dist/shared/stacks-q34k4vnc.cjs.map +0 -12
  141. package/dist/shared/stacks-rryme9bv.cjs +0 -569
  142. package/dist/shared/stacks-rryme9bv.cjs.map +0 -13
  143. package/dist/shared/stacks-s5vej5z6.cjs +0 -410
  144. package/dist/shared/stacks-s5vej5z6.cjs.map +0 -15
  145. package/dist/shared/stacks-sdqkapct.cjs +0 -67
  146. package/dist/shared/stacks-sdqkapct.cjs.map +0 -12
  147. package/dist/shared/stacks-vea7nde7.cjs +0 -731
  148. package/dist/shared/stacks-vea7nde7.cjs.map +0 -13
  149. package/dist/shared/stacks-wk8yp13j.cjs +0 -33
  150. package/dist/shared/stacks-wk8yp13j.cjs.map +0 -11
  151. package/dist/shared/stacks-xjkn3en1.cjs +0 -42
  152. package/dist/shared/stacks-xnrywjf1.cjs +0 -131
  153. package/dist/shared/stacks-xnrywjf1.cjs.map +0 -11
  154. package/dist/shared/stacks-yvb7rvwk.js.map +0 -10
  155. package/dist/src/tools/btc/index.cjs +0 -131
  156. package/dist/src/tools/btc/index.cjs.map +0 -10
  157. package/dist/src/tools/btc/index.d.cts +0 -10
  158. package/dist/src/tools/btc/index.d.ts +0 -10
  159. package/dist/src/tools/btc/index.js +0 -90
  160. package/dist/src/tools/btc/index.js.map +0 -10
  161. package/dist/src/tools/index.cjs +0 -3865
  162. package/dist/src/tools/index.cjs.map +0 -80
  163. package/dist/src/tools/index.d.cts +0 -206
  164. package/dist/src/tools/index.d.ts +0 -206
  165. package/dist/src/tools/index.js +0 -3827
  166. package/dist/src/tools/index.js.map +0 -80
@@ -1,569 +0,0 @@
1
- // src/transactions/authorization.ts
2
- var import_hmac = require("@noble/hashes/hmac.js");
3
- var import_sha2 = require("@noble/hashes/sha2.js");
4
- var import_secp256k1 = require("@noble/secp256k1");
5
- import_secp256k1.etc.hmacSha256Sync = (key, ...msgs) => {
6
- const h = import_hmac.hmac.create(import_sha2.sha256, key);
7
- for (const msg of msgs)
8
- h.update(msg);
9
- return h.digest();
10
- };
11
- var EMPTY_SIG = bytesToHex(new Uint8Array(RECOVERABLE_ECDSA_SIG_LENGTH_BYTES));
12
- function createSingleSigSpendingCondition(publicKey, nonce, fee) {
13
- const pubKeyBytes = hexToBytes(publicKey);
14
- const signer = hashP2PKH(pubKeyBytes);
15
- const isCompressed = pubKeyBytes.length === 33;
16
- return {
17
- hashMode: AddressHashMode.P2PKH,
18
- signer,
19
- nonce,
20
- fee,
21
- keyEncoding: isCompressed ? PubKeyEncoding.Compressed : PubKeyEncoding.Uncompressed,
22
- signature: EMPTY_SIG
23
- };
24
- }
25
- function createStandardAuth(spendingCondition) {
26
- return { authType: AuthType.Standard, spendingCondition };
27
- }
28
- function createSponsoredAuth(spendingCondition, sponsorSpendingCondition) {
29
- return {
30
- authType: AuthType.Sponsored,
31
- spendingCondition,
32
- sponsorSpendingCondition: sponsorSpendingCondition ?? createSingleSigSpendingCondition("0".repeat(66), 0n, 0n)
33
- };
34
- }
35
- function clearCondition(condition) {
36
- if ("signature" in condition) {
37
- return {
38
- ...condition,
39
- nonce: 0n,
40
- fee: 0n,
41
- signature: EMPTY_SIG
42
- };
43
- }
44
- return {
45
- ...condition,
46
- nonce: 0n,
47
- fee: 0n,
48
- fields: []
49
- };
50
- }
51
- function sponsorSigningSentinel() {
52
- return {
53
- hashMode: AddressHashMode.P2PKH,
54
- signer: "00".repeat(20),
55
- nonce: 0n,
56
- fee: 0n,
57
- keyEncoding: PubKeyEncoding.Compressed,
58
- signature: EMPTY_SIG
59
- };
60
- }
61
- function intoInitialSighashAuth(auth) {
62
- if (auth.authType === AuthType.Standard) {
63
- return createStandardAuth(clearCondition(auth.spendingCondition));
64
- }
65
- return createSponsoredAuth(clearCondition(auth.spendingCondition), sponsorSigningSentinel());
66
- }
67
- function sigHashPreSign(curSigHash, authType, fee, nonce) {
68
- const data = concatBytes(hexToBytes(curSigHash), new Uint8Array([authType]), intToBytes(fee, 8), intToBytes(nonce, 8));
69
- return txidFromBytes(data);
70
- }
71
- function sigHashPostSign(curSigHash, pubKeyEncoding, signature) {
72
- const data = concatBytes(hexToBytes(curSigHash), new Uint8Array([pubKeyEncoding]), hexToBytes(signature));
73
- return txidFromBytes(data);
74
- }
75
- function signWithKey(privateKey, messageHash) {
76
- const keyBytes = hexToBytes(privateKey).slice(0, 32);
77
- const sig = import_secp256k1.sign(messageHash, keyBytes, { lowS: true });
78
- const recoveryIdHex = intToHex(sig.recovery, 1);
79
- return recoveryIdHex + sig.toCompactHex();
80
- }
81
- function nextSignature(curSigHash, authType, fee, nonce, privateKey) {
82
- const sigHashPre = sigHashPreSign(curSigHash, authType, fee, nonce);
83
- const keyBytes = hexToBytes(privateKey).slice(0, 32);
84
- const pubKey = import_secp256k1.getPublicKey(keyBytes, true);
85
- const isCompressed = pubKey.length === 33;
86
- const pubKeyEncoding = isCompressed ? PubKeyEncoding.Compressed : PubKeyEncoding.Uncompressed;
87
- const nextSig = signWithKey(privateKey, sigHashPre);
88
- const nextSigHash = sigHashPostSign(sigHashPre, pubKeyEncoding, nextSig);
89
- return { nextSig, nextSigHash };
90
- }
91
-
92
- // src/transactions/multisig.ts
93
- var import_secp256k12 = require("@noble/secp256k1");
94
- var OP_CHECKMULTISIG = 174;
95
- function isNonSequential(hashMode) {
96
- return (hashMode & 4) !== 0;
97
- }
98
- function makeRedeemScript(publicKeys, signaturesRequired) {
99
- const parts = [];
100
- parts.push(new Uint8Array([80 + signaturesRequired]));
101
- for (const pk of publicKeys) {
102
- const pkBytes = hexToBytes(pk);
103
- parts.push(new Uint8Array([pkBytes.length]));
104
- parts.push(pkBytes);
105
- }
106
- parts.push(new Uint8Array([80 + publicKeys.length]));
107
- parts.push(new Uint8Array([OP_CHECKMULTISIG]));
108
- return concatBytes(...parts);
109
- }
110
- function makeMultiSigAddress(publicKeys, signaturesRequired, chain) {
111
- if (signaturesRequired < 1 || signaturesRequired > publicKeys.length) {
112
- throw new Error(`signaturesRequired must be between 1 and ${publicKeys.length}`);
113
- }
114
- const redeemScript = makeRedeemScript(publicKeys, signaturesRequired);
115
- const signer = hash160(redeemScript);
116
- const version = chain?.addressVersion?.multiSig ?? 20;
117
- return c32address(version, bytesToHex(signer));
118
- }
119
- function createMultiSigSpendingCondition(publicKeys, signaturesRequired, nonce, fee, hashMode = AddressHashMode.P2SH) {
120
- const redeemScript = makeRedeemScript(publicKeys, signaturesRequired);
121
- const signer = bytesToHex(hash160(redeemScript));
122
- return {
123
- hashMode,
124
- signer,
125
- nonce,
126
- fee,
127
- fields: [],
128
- signaturesRequired
129
- };
130
- }
131
- function initialSigHash(tx) {
132
- const cleared = {
133
- ...tx,
134
- auth: intoInitialSighashAuth(tx.auth)
135
- };
136
- return txidFromBytes(serializeTransaction(cleared));
137
- }
138
- function nonSequentialPreSignHash(tx) {
139
- const condition = tx.auth.spendingCondition;
140
- const initHash = initialSigHash(tx);
141
- return sigHashPreSign(initHash, AuthType.Standard, condition.fee, condition.nonce);
142
- }
143
- function replayMultiSigSigHash(tx) {
144
- const condition = tx.auth.spendingCondition;
145
- if (isNonSequential(condition.hashMode)) {
146
- const preSign = nonSequentialPreSignHash(tx);
147
- let curSigHash2 = preSign;
148
- for (const field of condition.fields) {
149
- curSigHash2 = sigHashPostSign(curSigHash2, field.pubKeyEncoding, field.data);
150
- }
151
- return curSigHash2;
152
- }
153
- let curSigHash = initialSigHash(tx);
154
- for (const field of condition.fields) {
155
- const preSign = sigHashPreSign(curSigHash, AuthType.Standard, condition.fee, condition.nonce);
156
- curSigHash = sigHashPostSign(preSign, field.pubKeyEncoding, field.data);
157
- }
158
- return curSigHash;
159
- }
160
- function signMultiSig(tx, privateKey, publicKeys) {
161
- const keyBytes = hexToBytes(privateKey).slice(0, 32);
162
- const signerPubKey = bytesToHex(import_secp256k12.getPublicKey(keyBytes, true));
163
- const signerIndex = publicKeys.findIndex((pk) => pk === signerPubKey);
164
- if (signerIndex === -1) {
165
- throw new Error("Private key does not correspond to any signer in the multi-sig");
166
- }
167
- const condition = {
168
- ...tx.auth.spendingCondition
169
- };
170
- if (isNonSequential(condition.hashMode)) {
171
- return signNonSequential(tx, condition, privateKey);
172
- }
173
- return signSequential(tx, condition, privateKey, publicKeys, signerIndex);
174
- }
175
- function signNonSequential(tx, condition, privateKey) {
176
- const fields = [...condition.fields];
177
- const preSignHash = nonSequentialPreSignHash(tx);
178
- const keyBytes = hexToBytes(privateKey).slice(0, 32);
179
- const sig = import_secp256k12.sign(preSignHash, keyBytes, { lowS: true });
180
- const recoveryIdHex = intToHex(sig.recovery, 1);
181
- const nextSig = recoveryIdHex + sig.toCompactHex();
182
- fields.push({
183
- type: "signature",
184
- pubKeyEncoding: PubKeyEncoding.Compressed,
185
- data: nextSig
186
- });
187
- const newCondition = { ...condition, fields };
188
- const sigCount = fields.filter((f) => f.type === "signature").length;
189
- if (sigCount >= condition.signaturesRequired) {
190
- return {
191
- ...tx,
192
- auth: { ...tx.auth, spendingCondition: newCondition }
193
- };
194
- }
195
- return {
196
- ...tx,
197
- auth: { ...tx.auth, spendingCondition: newCondition }
198
- };
199
- }
200
- function signSequential(tx, condition, privateKey, publicKeys, signerIndex) {
201
- const fields = [...condition.fields];
202
- let curSigHash = initialSigHash(tx);
203
- for (const field of fields) {
204
- const preSign = sigHashPreSign(curSigHash, AuthType.Standard, condition.fee, condition.nonce);
205
- curSigHash = sigHashPostSign(preSign, field.pubKeyEncoding, field.data);
206
- }
207
- for (let i = fields.length;i < signerIndex; i++) {
208
- const pubKeyField = {
209
- type: "publicKey",
210
- pubKeyEncoding: PubKeyEncoding.Compressed,
211
- data: publicKeys[i]
212
- };
213
- fields.push(pubKeyField);
214
- const preSign = sigHashPreSign(curSigHash, AuthType.Standard, condition.fee, condition.nonce);
215
- curSigHash = sigHashPostSign(preSign, pubKeyField.pubKeyEncoding, pubKeyField.data);
216
- }
217
- const { nextSig, nextSigHash } = nextSignature(curSigHash, AuthType.Standard, condition.fee, condition.nonce, privateKey);
218
- fields.push({
219
- type: "signature",
220
- pubKeyEncoding: PubKeyEncoding.Compressed,
221
- data: nextSig
222
- });
223
- curSigHash = nextSigHash;
224
- const newCondition = { ...condition, fields };
225
- const sigCount = fields.filter((f) => f.type === "signature").length;
226
- let finalCondition = newCondition;
227
- if (sigCount >= condition.signaturesRequired) {
228
- finalCondition = finalizeMultiSigCondition(newCondition, publicKeys, curSigHash);
229
- }
230
- return {
231
- ...tx,
232
- auth: {
233
- ...tx.auth,
234
- spendingCondition: finalCondition
235
- }
236
- };
237
- }
238
- async function signMultiSigWithAccount(tx, account, publicKeys) {
239
- const signerPubKey = account.publicKey;
240
- const signerIndex = publicKeys.findIndex((pk) => pk === signerPubKey);
241
- if (signerIndex === -1) {
242
- throw new Error("Account does not correspond to any signer in the multi-sig");
243
- }
244
- const condition = {
245
- ...tx.auth.spendingCondition
246
- };
247
- if (isNonSequential(condition.hashMode)) {
248
- return signNonSequentialWithAccount(tx, condition, account);
249
- }
250
- return signSequentialWithAccount(tx, condition, account, publicKeys, signerIndex);
251
- }
252
- async function signNonSequentialWithAccount(tx, condition, account) {
253
- const fields = [...condition.fields];
254
- const preSignHash = nonSequentialPreSignHash(tx);
255
- const sigBytes = await account.sign(hexToBytes(preSignHash));
256
- const nextSig = bytesToHex(sigBytes);
257
- fields.push({
258
- type: "signature",
259
- pubKeyEncoding: PubKeyEncoding.Compressed,
260
- data: nextSig
261
- });
262
- const newCondition = { ...condition, fields };
263
- return {
264
- ...tx,
265
- auth: { ...tx.auth, spendingCondition: newCondition }
266
- };
267
- }
268
- async function signSequentialWithAccount(tx, condition, account, publicKeys, signerIndex) {
269
- const fields = [...condition.fields];
270
- let curSigHash = initialSigHash(tx);
271
- for (const field of fields) {
272
- const preSign2 = sigHashPreSign(curSigHash, AuthType.Standard, condition.fee, condition.nonce);
273
- curSigHash = sigHashPostSign(preSign2, field.pubKeyEncoding, field.data);
274
- }
275
- for (let i = fields.length;i < signerIndex; i++) {
276
- const pubKeyField = {
277
- type: "publicKey",
278
- pubKeyEncoding: PubKeyEncoding.Compressed,
279
- data: publicKeys[i]
280
- };
281
- fields.push(pubKeyField);
282
- const preSign2 = sigHashPreSign(curSigHash, AuthType.Standard, condition.fee, condition.nonce);
283
- curSigHash = sigHashPostSign(preSign2, pubKeyField.pubKeyEncoding, pubKeyField.data);
284
- }
285
- const preSign = sigHashPreSign(curSigHash, AuthType.Standard, condition.fee, condition.nonce);
286
- const sigBytes = await account.sign(hexToBytes(preSign));
287
- const nextSig = bytesToHex(sigBytes);
288
- const nextSigHash = sigHashPostSign(preSign, PubKeyEncoding.Compressed, nextSig);
289
- fields.push({
290
- type: "signature",
291
- pubKeyEncoding: PubKeyEncoding.Compressed,
292
- data: nextSig
293
- });
294
- curSigHash = nextSigHash;
295
- const newCondition = { ...condition, fields };
296
- const sigCount = fields.filter((f) => f.type === "signature").length;
297
- let finalCondition = newCondition;
298
- if (sigCount >= condition.signaturesRequired) {
299
- finalCondition = finalizeMultiSigCondition(newCondition, publicKeys, curSigHash);
300
- }
301
- return {
302
- ...tx,
303
- auth: {
304
- ...tx.auth,
305
- spendingCondition: finalCondition
306
- }
307
- };
308
- }
309
- function finalizeMultiSig(tx, publicKeys) {
310
- const condition = tx.auth.spendingCondition;
311
- if (isNonSequential(condition.hashMode)) {
312
- return tx;
313
- }
314
- const curSigHash = replayMultiSigSigHash(tx);
315
- const finalCondition = finalizeMultiSigCondition(condition, publicKeys, curSigHash);
316
- return {
317
- ...tx,
318
- auth: {
319
- ...tx.auth,
320
- spendingCondition: finalCondition
321
- }
322
- };
323
- }
324
- function finalizeMultiSigCondition(condition, publicKeys, curSigHash) {
325
- const fields = [...condition.fields];
326
- let sigHash = curSigHash;
327
- for (let i = fields.length;i < publicKeys.length; i++) {
328
- const pubKeyField = {
329
- type: "publicKey",
330
- pubKeyEncoding: PubKeyEncoding.Compressed,
331
- data: publicKeys[i]
332
- };
333
- fields.push(pubKeyField);
334
- const preSign = sigHashPreSign(sigHash, AuthType.Standard, condition.fee, condition.nonce);
335
- sigHash = sigHashPostSign(preSign, pubKeyField.pubKeyEncoding, pubKeyField.data);
336
- }
337
- return { ...condition, fields };
338
- }
339
- function combineMultiSigSignatures(baseTx, signedTxs) {
340
- const condition = baseTx.auth.spendingCondition;
341
- if (!isNonSequential(condition.hashMode)) {
342
- throw new Error("combineMultiSigSignatures only works with non-sequential hash modes (SIP-027)");
343
- }
344
- const seenSigs = new Set;
345
- const signatures = [];
346
- for (const stx of signedTxs) {
347
- const stxCondition = stx.auth.spendingCondition;
348
- for (const field of stxCondition.fields) {
349
- if (field.type === "signature" && !seenSigs.has(field.data)) {
350
- seenSigs.add(field.data);
351
- signatures.push(field);
352
- }
353
- }
354
- }
355
- if (signatures.length === 0) {
356
- throw new Error("No signatures found in the provided transactions");
357
- }
358
- const newCondition = {
359
- ...condition,
360
- fields: signatures
361
- };
362
- return {
363
- ...baseTx,
364
- auth: { ...baseTx.auth, spendingCondition: newCondition }
365
- };
366
- }
367
-
368
- // src/transactions/build.ts
369
- function resolvePcMode(mode) {
370
- if (mode === "allow")
371
- return PostConditionModeWire.Allow;
372
- if (mode === "originator")
373
- return PostConditionModeWire.Originator;
374
- return PostConditionModeWire.Deny;
375
- }
376
- function convertPostConditions(pcs) {
377
- if (!pcs || pcs.length === 0)
378
- return [];
379
- return pcs.map((pc) => convertPostCondition(typeof pc === "string" ? fromHex(pc) : pc));
380
- }
381
- function resolveSpendingCondition(opts, nonce, fee) {
382
- if (opts.publicKeys) {
383
- return createMultiSigSpendingCondition(opts.publicKeys, opts.signaturesRequired, nonce, fee, opts.hashMode);
384
- }
385
- return createSingleSigSpendingCondition(opts.publicKey, nonce, fee);
386
- }
387
- function resolveAuth(spendingCondition, sponsored) {
388
- return sponsored ? createSponsoredAuth(spendingCondition) : createStandardAuth(spendingCondition);
389
- }
390
- function resolveVersionAndChainId(chain) {
391
- if (!chain)
392
- return { version: 0, chainId: 1 };
393
- return { version: chain.transactionVersion, chainId: chain.id };
394
- }
395
- function buildTokenTransfer(options) {
396
- const { version, chainId } = resolveVersionAndChainId(options.chain);
397
- const fee = intToBigInt(options.fee);
398
- const nonce = intToBigInt(options.nonce);
399
- const spendingCondition = resolveSpendingCondition(options, nonce, fee);
400
- const recipient = Cl.principal(options.recipient);
401
- const tx = {
402
- version,
403
- chainId,
404
- auth: resolveAuth(spendingCondition, options.sponsored),
405
- anchorMode: AnchorMode.Any,
406
- postConditionMode: resolvePcMode(options.postConditionMode),
407
- postConditions: convertPostConditions(options.postConditions),
408
- payload: {
409
- payloadType: PayloadType.TokenTransfer,
410
- recipient,
411
- amount: intToBigInt(options.amount),
412
- memo: options.memo ?? ""
413
- }
414
- };
415
- if (options.publicKeys) {
416
- tx._multisig = { publicKeys: options.publicKeys };
417
- }
418
- return tx;
419
- }
420
- function buildContractCall(options) {
421
- const { version, chainId } = resolveVersionAndChainId(options.chain);
422
- const fee = intToBigInt(options.fee);
423
- const nonce = intToBigInt(options.nonce);
424
- const spendingCondition = resolveSpendingCondition(options, nonce, fee);
425
- const tx = {
426
- version,
427
- chainId,
428
- auth: resolveAuth(spendingCondition, options.sponsored),
429
- anchorMode: AnchorMode.Any,
430
- postConditionMode: resolvePcMode(options.postConditionMode),
431
- postConditions: convertPostConditions(options.postConditions),
432
- payload: {
433
- payloadType: PayloadType.ContractCall,
434
- contractAddress: options.contractAddress,
435
- contractName: options.contractName,
436
- functionName: options.functionName,
437
- functionArgs: options.functionArgs
438
- }
439
- };
440
- if (options.publicKeys) {
441
- tx._multisig = { publicKeys: options.publicKeys };
442
- }
443
- return tx;
444
- }
445
- function buildContractDeploy(options) {
446
- const { version, chainId } = resolveVersionAndChainId(options.chain);
447
- const fee = intToBigInt(options.fee);
448
- const nonce = intToBigInt(options.nonce);
449
- const spendingCondition = resolveSpendingCondition(options, nonce, fee);
450
- const clarityVersion = options.clarityVersion ?? ClarityVersion.Clarity4;
451
- const tx = {
452
- version,
453
- chainId,
454
- auth: resolveAuth(spendingCondition, options.sponsored),
455
- anchorMode: AnchorMode.Any,
456
- postConditionMode: resolvePcMode(options.postConditionMode),
457
- postConditions: convertPostConditions(options.postConditions),
458
- payload: {
459
- payloadType: PayloadType.VersionedSmartContract,
460
- clarityVersion,
461
- contractName: options.contractName,
462
- codeBody: options.codeBody
463
- }
464
- };
465
- if (options.publicKeys) {
466
- tx._multisig = { publicKeys: options.publicKeys };
467
- }
468
- return tx;
469
- }
470
-
471
- // src/transactions/signer.ts
472
- function txid(tx) {
473
- return txidFromBytes(serializeTransaction(tx));
474
- }
475
- function signBegin(tx) {
476
- const cleared = {
477
- ...tx,
478
- auth: intoInitialSighashAuth(tx.auth)
479
- };
480
- return txid(cleared);
481
- }
482
- function signTransaction(tx, privateKey) {
483
- const sigHash = signBegin(tx);
484
- const condition = tx.auth.spendingCondition;
485
- const { nextSig } = nextSignature(sigHash, AuthType.Standard, condition.fee, condition.nonce, privateKey);
486
- return {
487
- ...tx,
488
- auth: {
489
- ...tx.auth,
490
- spendingCondition: {
491
- ...condition,
492
- signature: nextSig
493
- }
494
- }
495
- };
496
- }
497
- async function signTransactionWithAccount(tx, account) {
498
- const sigHash = signBegin(tx);
499
- const condition = tx.auth.spendingCondition;
500
- const sigHashPre = sigHashPreSign(sigHash, AuthType.Standard, condition.fee, condition.nonce);
501
- const sigBytes = await account.sign(hexToBytes(sigHashPre));
502
- const nextSig = bytesToHex(sigBytes);
503
- return {
504
- ...tx,
505
- auth: {
506
- ...tx.auth,
507
- spendingCondition: {
508
- ...condition,
509
- signature: nextSig
510
- }
511
- }
512
- };
513
- }
514
- function getTransactionId(tx) {
515
- return txid(tx);
516
- }
517
- function getOriginSigHash(tx) {
518
- const condition = tx.auth.spendingCondition;
519
- if ("fields" in condition) {
520
- return replayMultiSigSigHash(tx);
521
- }
522
- const initialSigHash2 = signBegin(tx);
523
- const sc = condition;
524
- const preSign = sigHashPreSign(initialSigHash2, AuthType.Standard, sc.fee, sc.nonce);
525
- return sigHashPostSign(preSign, sc.keyEncoding, sc.signature);
526
- }
527
- function signSponsor(tx, privateKey) {
528
- if (tx.auth.authType !== AuthType.Sponsored) {
529
- throw new Error("Transaction must be sponsored");
530
- }
531
- const auth = tx.auth;
532
- const sponsorCondition = auth.sponsorSpendingCondition;
533
- const originSigHash = getOriginSigHash(tx);
534
- const { nextSig } = nextSignature(originSigHash, AuthType.Sponsored, sponsorCondition.fee, sponsorCondition.nonce, privateKey);
535
- return {
536
- ...tx,
537
- auth: {
538
- ...auth,
539
- sponsorSpendingCondition: {
540
- ...sponsorCondition,
541
- signature: nextSig
542
- }
543
- }
544
- };
545
- }
546
- async function signSponsorWithAccount(tx, account) {
547
- if (tx.auth.authType !== AuthType.Sponsored) {
548
- throw new Error("Transaction must be sponsored");
549
- }
550
- const auth = tx.auth;
551
- const sponsorCondition = auth.sponsorSpendingCondition;
552
- const originSigHash = getOriginSigHash(tx);
553
- const sigHashPre = sigHashPreSign(originSigHash, AuthType.Sponsored, sponsorCondition.fee, sponsorCondition.nonce);
554
- const sigBytes = await account.sign(hexToBytes(sigHashPre));
555
- const nextSig = bytesToHex(sigBytes);
556
- return {
557
- ...tx,
558
- auth: {
559
- ...auth,
560
- sponsorSpendingCondition: {
561
- ...sponsorCondition,
562
- signature: nextSig
563
- }
564
- }
565
- };
566
- }
567
-
568
- //# debugId=4803818E842AF84364756E2164756E21
569
- //# sourceMappingURL=stacks-rryme9bv.cjs.map