@cogcoin/client 1.1.7 → 1.1.9

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 (284) hide show
  1. package/README.md +1 -1
  2. package/dist/bitcoind/service.js +1 -1
  3. package/dist/cli/commands/mining-read.js +1 -1
  4. package/dist/cli/commands/wallet-mutation/anchor.d.ts +2 -0
  5. package/dist/cli/commands/wallet-mutation/anchor.js +33 -0
  6. package/dist/cli/commands/wallet-mutation/bitcoin-transfer.d.ts +2 -0
  7. package/dist/cli/commands/wallet-mutation/bitcoin-transfer.js +32 -0
  8. package/dist/cli/commands/wallet-mutation/cog.d.ts +2 -0
  9. package/dist/cli/commands/wallet-mutation/cog.js +131 -0
  10. package/dist/cli/commands/wallet-mutation/context.d.ts +3 -0
  11. package/dist/cli/commands/wallet-mutation/context.js +18 -0
  12. package/dist/cli/commands/wallet-mutation/domain-admin.d.ts +2 -0
  13. package/dist/cli/commands/wallet-mutation/domain-admin.js +173 -0
  14. package/dist/cli/commands/wallet-mutation/domain-market.d.ts +2 -0
  15. package/dist/cli/commands/wallet-mutation/domain-market.js +107 -0
  16. package/dist/cli/commands/wallet-mutation/field.d.ts +2 -0
  17. package/dist/cli/commands/wallet-mutation/field.js +125 -0
  18. package/dist/cli/commands/wallet-mutation/register.d.ts +2 -0
  19. package/dist/cli/commands/wallet-mutation/register.js +38 -0
  20. package/dist/cli/commands/wallet-mutation/registry.d.ts +3 -0
  21. package/dist/cli/commands/wallet-mutation/registry.js +39 -0
  22. package/dist/cli/commands/wallet-mutation/reputation.d.ts +2 -0
  23. package/dist/cli/commands/wallet-mutation/reputation.js +57 -0
  24. package/dist/cli/commands/wallet-mutation/types.d.ts +32 -0
  25. package/dist/cli/commands/wallet-mutation/types.js +1 -0
  26. package/dist/cli/commands/wallet-mutation.js +13 -765
  27. package/dist/cli/commands/wallet-read.js +4 -4
  28. package/dist/cli/mutation-success.d.ts +0 -2
  29. package/dist/cli/output/classify.d.ts +7 -0
  30. package/dist/cli/output/classify.js +94 -0
  31. package/dist/cli/output/render.d.ts +2 -0
  32. package/dist/cli/output/render.js +13 -0
  33. package/dist/cli/output/rules/cli-surface.d.ts +2 -0
  34. package/dist/cli/output/rules/cli-surface.js +110 -0
  35. package/dist/cli/output/rules/generic.d.ts +2 -0
  36. package/dist/cli/output/rules/generic.js +13 -0
  37. package/dist/cli/output/rules/index.d.ts +2 -0
  38. package/dist/cli/output/rules/index.js +24 -0
  39. package/dist/cli/output/rules/mining-update.d.ts +2 -0
  40. package/dist/cli/output/rules/mining-update.js +68 -0
  41. package/dist/cli/output/rules/services.d.ts +2 -0
  42. package/dist/cli/output/rules/services.js +110 -0
  43. package/dist/cli/output/rules/wallet-admin.d.ts +2 -0
  44. package/dist/cli/output/rules/wallet-admin.js +224 -0
  45. package/dist/cli/output/rules/wallet-mutations.d.ts +2 -0
  46. package/dist/cli/output/rules/wallet-mutations.js +274 -0
  47. package/dist/cli/output/types.d.ts +16 -0
  48. package/dist/cli/output/types.js +1 -0
  49. package/dist/cli/output.d.ts +2 -168
  50. package/dist/cli/output.js +6 -989
  51. package/dist/cli/pagination.d.ts +15 -0
  52. package/dist/cli/pagination.js +16 -0
  53. package/dist/cli/recommendations.d.ts +4 -0
  54. package/dist/cli/recommendations.js +108 -0
  55. package/dist/cli/wallet-format/availability.d.ts +5 -0
  56. package/dist/cli/wallet-format/availability.js +96 -0
  57. package/dist/cli/wallet-format/balance.d.ts +2 -0
  58. package/dist/cli/wallet-format/balance.js +162 -0
  59. package/dist/cli/wallet-format/domains.d.ts +8 -0
  60. package/dist/cli/wallet-format/domains.js +84 -0
  61. package/dist/cli/wallet-format/fields.d.ts +6 -0
  62. package/dist/cli/wallet-format/fields.js +61 -0
  63. package/dist/cli/wallet-format/identity.d.ts +5 -0
  64. package/dist/cli/wallet-format/identity.js +19 -0
  65. package/dist/cli/wallet-format/locks.d.ts +7 -0
  66. package/dist/cli/wallet-format/locks.js +52 -0
  67. package/dist/cli/wallet-format/overview.d.ts +2 -0
  68. package/dist/cli/wallet-format/overview.js +122 -0
  69. package/dist/cli/wallet-format/pending.d.ts +13 -0
  70. package/dist/cli/wallet-format/pending.js +101 -0
  71. package/dist/cli/wallet-format/shared.d.ts +7 -0
  72. package/dist/cli/wallet-format/shared.js +31 -0
  73. package/dist/cli/wallet-format/status.d.ts +3 -0
  74. package/dist/cli/wallet-format/status.js +27 -0
  75. package/dist/cli/wallet-format.d.ts +8 -30
  76. package/dist/cli/wallet-format.js +8 -830
  77. package/dist/cli/wallet-read-helpers.d.ts +6 -0
  78. package/dist/cli/wallet-read-helpers.js +17 -0
  79. package/dist/wallet/mining/candidate.d.ts +1 -1
  80. package/dist/wallet/mining/candidate.js +3 -3
  81. package/dist/wallet/mining/constants.d.ts +2 -2
  82. package/dist/wallet/mining/constants.js +2 -2
  83. package/dist/wallet/mining/engine-state.js +10 -0
  84. package/dist/wallet/mining/sentence-protocol.d.ts +2 -2
  85. package/dist/wallet/mining/sentences.js +8 -8
  86. package/dist/wallet/mining/visualizer-sync.js +79 -15
  87. package/dist/wallet/read/context.js +1 -1
  88. package/dist/wallet/reset/artifacts.d.ts +16 -0
  89. package/dist/wallet/reset/artifacts.js +141 -0
  90. package/dist/wallet/reset/execution.d.ts +38 -0
  91. package/dist/wallet/reset/execution.js +458 -0
  92. package/dist/wallet/reset/preflight.d.ts +7 -0
  93. package/dist/wallet/reset/preflight.js +116 -0
  94. package/dist/wallet/reset/preview.d.ts +2 -0
  95. package/dist/wallet/reset/preview.js +50 -0
  96. package/dist/wallet/reset/process-cleanup.d.ts +12 -0
  97. package/dist/wallet/reset/process-cleanup.js +179 -0
  98. package/dist/wallet/reset/types.d.ts +189 -0
  99. package/dist/wallet/reset/types.js +1 -0
  100. package/dist/wallet/reset.d.ts +4 -119
  101. package/dist/wallet/reset.js +4 -882
  102. package/dist/wallet/tx/anchor/confirm.d.ts +15 -0
  103. package/dist/wallet/tx/anchor/confirm.js +60 -0
  104. package/dist/wallet/tx/anchor/draft.d.ts +39 -0
  105. package/dist/wallet/tx/anchor/draft.js +167 -0
  106. package/dist/wallet/tx/anchor/index.d.ts +5 -0
  107. package/dist/wallet/tx/anchor/index.js +148 -0
  108. package/dist/wallet/tx/anchor/intent.d.ts +61 -0
  109. package/dist/wallet/tx/anchor/intent.js +101 -0
  110. package/dist/wallet/tx/anchor/plan.d.ts +3 -0
  111. package/dist/wallet/tx/anchor/plan.js +18 -0
  112. package/dist/wallet/tx/anchor/result.d.ts +25 -0
  113. package/dist/wallet/tx/anchor/result.js +20 -0
  114. package/dist/wallet/tx/anchor.d.ts +1 -39
  115. package/dist/wallet/tx/anchor.js +1 -494
  116. package/dist/wallet/tx/bitcoin-transfer/confirm.d.ts +7 -0
  117. package/dist/wallet/tx/bitcoin-transfer/confirm.js +11 -0
  118. package/dist/wallet/tx/bitcoin-transfer/index.d.ts +5 -0
  119. package/dist/wallet/tx/bitcoin-transfer/index.js +112 -0
  120. package/dist/wallet/tx/bitcoin-transfer/intent.d.ts +52 -0
  121. package/dist/wallet/tx/bitcoin-transfer/intent.js +74 -0
  122. package/dist/wallet/tx/bitcoin-transfer/plan.d.ts +5 -0
  123. package/dist/wallet/tx/bitcoin-transfer/plan.js +21 -0
  124. package/dist/wallet/tx/bitcoin-transfer/result.d.ts +19 -0
  125. package/dist/wallet/tx/bitcoin-transfer/result.js +16 -0
  126. package/dist/wallet/tx/bitcoin-transfer.d.ts +1 -35
  127. package/dist/wallet/tx/bitcoin-transfer.js +1 -200
  128. package/dist/wallet/tx/cog/confirm.d.ts +13 -0
  129. package/dist/wallet/tx/cog/confirm.js +59 -0
  130. package/dist/wallet/tx/cog/draft.d.ts +20 -0
  131. package/dist/wallet/tx/cog/draft.js +114 -0
  132. package/dist/wallet/tx/cog/index.d.ts +6 -0
  133. package/dist/wallet/tx/cog/index.js +117 -0
  134. package/dist/wallet/tx/cog/intent.d.ts +30 -0
  135. package/dist/wallet/tx/cog/intent.js +169 -0
  136. package/dist/wallet/tx/cog/plan.d.ts +19 -0
  137. package/dist/wallet/tx/cog/plan.js +65 -0
  138. package/dist/wallet/tx/cog/result.d.ts +27 -0
  139. package/dist/wallet/tx/cog/result.js +28 -0
  140. package/dist/wallet/tx/cog/types.d.ts +186 -0
  141. package/dist/wallet/tx/cog/types.js +2 -0
  142. package/dist/wallet/tx/cog/variants/claim.d.ts +3 -0
  143. package/dist/wallet/tx/cog/variants/claim.js +92 -0
  144. package/dist/wallet/tx/cog/variants/lock.d.ts +2 -0
  145. package/dist/wallet/tx/cog/variants/lock.js +102 -0
  146. package/dist/wallet/tx/cog/variants/send.d.ts +2 -0
  147. package/dist/wallet/tx/cog/variants/send.js +77 -0
  148. package/dist/wallet/tx/cog.d.ts +1 -96
  149. package/dist/wallet/tx/cog.js +1 -824
  150. package/dist/wallet/tx/common.d.ts +14 -199
  151. package/dist/wallet/tx/common.js +10 -493
  152. package/dist/wallet/tx/domain-admin/confirm.d.ts +17 -0
  153. package/dist/wallet/tx/domain-admin/confirm.js +58 -0
  154. package/dist/wallet/tx/domain-admin/draft.d.ts +20 -0
  155. package/dist/wallet/tx/domain-admin/draft.js +161 -0
  156. package/dist/wallet/tx/domain-admin/index.d.ts +9 -0
  157. package/dist/wallet/tx/domain-admin/index.js +150 -0
  158. package/dist/wallet/tx/domain-admin/intent.d.ts +12 -0
  159. package/dist/wallet/tx/domain-admin/intent.js +61 -0
  160. package/dist/wallet/tx/domain-admin/plan.d.ts +19 -0
  161. package/dist/wallet/tx/domain-admin/plan.js +64 -0
  162. package/dist/wallet/tx/domain-admin/result.d.ts +19 -0
  163. package/dist/wallet/tx/domain-admin/result.js +33 -0
  164. package/dist/wallet/tx/domain-admin/types.d.ts +162 -0
  165. package/dist/wallet/tx/domain-admin/types.js +1 -0
  166. package/dist/wallet/tx/domain-admin/variants/canonical.d.ts +2 -0
  167. package/dist/wallet/tx/domain-admin/variants/canonical.js +22 -0
  168. package/dist/wallet/tx/domain-admin/variants/delegate.d.ts +3 -0
  169. package/dist/wallet/tx/domain-admin/variants/delegate.js +60 -0
  170. package/dist/wallet/tx/domain-admin/variants/endpoint.d.ts +3 -0
  171. package/dist/wallet/tx/domain-admin/variants/endpoint.js +102 -0
  172. package/dist/wallet/tx/domain-admin/variants/miner.d.ts +3 -0
  173. package/dist/wallet/tx/domain-admin/variants/miner.js +59 -0
  174. package/dist/wallet/tx/domain-admin.d.ts +1 -107
  175. package/dist/wallet/tx/domain-admin.js +1 -729
  176. package/dist/wallet/tx/domain-market/confirm.d.ts +6 -0
  177. package/dist/wallet/tx/domain-market/confirm.js +52 -0
  178. package/dist/wallet/tx/domain-market/draft.d.ts +43 -0
  179. package/dist/wallet/tx/domain-market/draft.js +286 -0
  180. package/dist/wallet/tx/domain-market/index.d.ts +6 -0
  181. package/dist/wallet/tx/domain-market/index.js +145 -0
  182. package/dist/wallet/tx/domain-market/intent.d.ts +15 -0
  183. package/dist/wallet/tx/domain-market/intent.js +131 -0
  184. package/dist/wallet/tx/domain-market/plan.d.ts +31 -0
  185. package/dist/wallet/tx/domain-market/plan.js +98 -0
  186. package/dist/wallet/tx/domain-market/result.d.ts +45 -0
  187. package/dist/wallet/tx/domain-market/result.js +88 -0
  188. package/dist/wallet/tx/domain-market/types.d.ts +221 -0
  189. package/dist/wallet/tx/domain-market/types.js +1 -0
  190. package/dist/wallet/tx/domain-market/variants/buy.d.ts +2 -0
  191. package/dist/wallet/tx/domain-market/variants/buy.js +103 -0
  192. package/dist/wallet/tx/domain-market/variants/sell.d.ts +2 -0
  193. package/dist/wallet/tx/domain-market/variants/sell.js +91 -0
  194. package/dist/wallet/tx/domain-market/variants/transfer.d.ts +2 -0
  195. package/dist/wallet/tx/domain-market/variants/transfer.js +105 -0
  196. package/dist/wallet/tx/domain-market.d.ts +1 -116
  197. package/dist/wallet/tx/domain-market.js +1 -1078
  198. package/dist/wallet/tx/draft-build.d.ts +60 -0
  199. package/dist/wallet/tx/draft-build.js +127 -0
  200. package/dist/wallet/tx/executor.d.ts +6 -40
  201. package/dist/wallet/tx/executor.js +6 -100
  202. package/dist/wallet/tx/fee.d.ts +30 -0
  203. package/dist/wallet/tx/fee.js +98 -0
  204. package/dist/wallet/tx/field/confirm.d.ts +11 -0
  205. package/dist/wallet/tx/field/confirm.js +19 -0
  206. package/dist/wallet/tx/field/draft.d.ts +23 -0
  207. package/dist/wallet/tx/field/draft.js +202 -0
  208. package/dist/wallet/tx/field/index.d.ts +5 -0
  209. package/dist/wallet/tx/field/index.js +140 -0
  210. package/dist/wallet/tx/field/intent.d.ts +5 -0
  211. package/dist/wallet/tx/field/intent.js +50 -0
  212. package/dist/wallet/tx/field/plan.d.ts +20 -0
  213. package/dist/wallet/tx/field/plan.js +65 -0
  214. package/dist/wallet/tx/field/result.d.ts +29 -0
  215. package/dist/wallet/tx/field/result.js +103 -0
  216. package/dist/wallet/tx/field/types.d.ts +163 -0
  217. package/dist/wallet/tx/field/types.js +1 -0
  218. package/dist/wallet/tx/field/variants/clear.d.ts +2 -0
  219. package/dist/wallet/tx/field/variants/clear.js +60 -0
  220. package/dist/wallet/tx/field/variants/create.d.ts +2 -0
  221. package/dist/wallet/tx/field/variants/create.js +67 -0
  222. package/dist/wallet/tx/field/variants/set.d.ts +2 -0
  223. package/dist/wallet/tx/field/variants/set.js +195 -0
  224. package/dist/wallet/tx/field.d.ts +1 -95
  225. package/dist/wallet/tx/field.js +1 -920
  226. package/dist/wallet/tx/mining-preemption.d.ts +6 -0
  227. package/dist/wallet/tx/mining-preemption.js +7 -0
  228. package/dist/wallet/tx/primitives.d.ts +13 -0
  229. package/dist/wallet/tx/primitives.js +42 -0
  230. package/dist/wallet/tx/psbt-assert.d.ts +14 -0
  231. package/dist/wallet/tx/psbt-assert.js +39 -0
  232. package/dist/wallet/tx/publish.d.ts +37 -0
  233. package/dist/wallet/tx/publish.js +88 -0
  234. package/dist/wallet/tx/readiness.d.ts +7 -0
  235. package/dist/wallet/tx/readiness.js +61 -0
  236. package/dist/wallet/tx/reconcile.d.ts +24 -0
  237. package/dist/wallet/tx/reconcile.js +72 -0
  238. package/dist/wallet/tx/register/confirm.d.ts +6 -0
  239. package/dist/wallet/tx/register/confirm.js +66 -0
  240. package/dist/wallet/tx/register/draft.d.ts +42 -0
  241. package/dist/wallet/tx/register/draft.js +181 -0
  242. package/dist/wallet/tx/register/index.d.ts +6 -0
  243. package/dist/wallet/tx/register/index.js +158 -0
  244. package/dist/wallet/tx/register/intent.d.ts +74 -0
  245. package/dist/wallet/tx/register/intent.js +119 -0
  246. package/dist/wallet/tx/register/plan.d.ts +43 -0
  247. package/dist/wallet/tx/register/plan.js +168 -0
  248. package/dist/wallet/tx/register/result.d.ts +78 -0
  249. package/dist/wallet/tx/register/result.js +41 -0
  250. package/dist/wallet/tx/register.d.ts +1 -70
  251. package/dist/wallet/tx/register.js +1 -681
  252. package/dist/wallet/tx/reputation/confirm.d.ts +11 -0
  253. package/dist/wallet/tx/reputation/confirm.js +51 -0
  254. package/dist/wallet/tx/reputation/draft.d.ts +20 -0
  255. package/dist/wallet/tx/reputation/draft.js +130 -0
  256. package/dist/wallet/tx/reputation/index.d.ts +4 -0
  257. package/dist/wallet/tx/reputation/index.js +162 -0
  258. package/dist/wallet/tx/reputation/intent.d.ts +36 -0
  259. package/dist/wallet/tx/reputation/intent.js +157 -0
  260. package/dist/wallet/tx/reputation/plan.d.ts +19 -0
  261. package/dist/wallet/tx/reputation/plan.js +64 -0
  262. package/dist/wallet/tx/reputation/result.d.ts +21 -0
  263. package/dist/wallet/tx/reputation/result.js +31 -0
  264. package/dist/wallet/tx/reputation/types.d.ts +130 -0
  265. package/dist/wallet/tx/reputation/types.js +1 -0
  266. package/dist/wallet/tx/reputation.d.ts +1 -74
  267. package/dist/wallet/tx/reputation.js +1 -556
  268. package/dist/wallet/tx/signing.d.ts +18 -0
  269. package/dist/wallet/tx/signing.js +31 -0
  270. package/dist/wallet/tx/state-persist.d.ts +27 -0
  271. package/dist/wallet/tx/state-persist.js +54 -0
  272. package/dist/wallet/tx/types.d.ts +44 -0
  273. package/dist/wallet/tx/types.js +1 -0
  274. package/package.json +1 -1
  275. package/dist/cli/mining-json.d.ts +0 -20
  276. package/dist/cli/mining-json.js +0 -46
  277. package/dist/cli/mutation-json.d.ts +0 -325
  278. package/dist/cli/mutation-json.js +0 -269
  279. package/dist/cli/mutation-resolved-json.d.ts +0 -117
  280. package/dist/cli/mutation-resolved-json.js +0 -123
  281. package/dist/cli/preview-json.d.ts +0 -319
  282. package/dist/cli/preview-json.js +0 -254
  283. package/dist/cli/read-json.d.ts +0 -190
  284. package/dist/cli/read-json.js +0 -627
@@ -1,824 +1 @@
1
- import { createHash, randomBytes } from "node:crypto";
2
- import { getBalance, getLock, lookupDomain } from "@cogcoin/indexer/queries";
3
- import { attachOrStartManagedBitcoindService } from "../../bitcoind/service.js";
4
- import { createRpcClient } from "../../bitcoind/node.js";
5
- import {} from "../runtime.js";
6
- import {} from "../state/provider.js";
7
- import { serializeCogClaim, serializeCogLock, serializeCogTransfer, } from "../cogop/index.js";
8
- import { openWalletReadContext } from "../read/index.js";
9
- import { assertFixedInputPrefixMatches, assertFundingInputsAfterFixedPrefix, assertWalletMutationContextReady, buildWalletMutationTransactionWithReserveFallback, createFundingMutationSender, createWalletMutationFeeMetadata, formatCogAmount, getDecodedInputScriptPubKeyHex, isLocalWalletScript, mergeFixedWalletInputs, outpointKey, saveWalletStatePreservingUnlock, unlockTemporaryBuilderLocks, updateMutationRecord, } from "./common.js";
10
- import { confirmTypedAcknowledgement, confirmYesNo } from "./confirm.js";
11
- import { executeWalletMutationOperation, publishWalletMutation, resolveExistingWalletMutation, } from "./executor.js";
12
- import { getCanonicalIdentitySelector, resolveIdentityBySelector, } from "./identity-selector.js";
13
- import { upsertPendingMutation } from "./journal.js";
14
- import { normalizeBtcTarget } from "./targets.js";
15
- const MAX_LOCK_DURATION_BLOCKS = 262_800;
16
- const ZERO_PREIMAGE_HEX = "00".repeat(32);
17
- function normalizeDomainName(value) {
18
- const normalized = value.trim().toLowerCase();
19
- if (normalized.length === 0) {
20
- throw new Error("wallet_cog_missing_domain");
21
- }
22
- return normalized;
23
- }
24
- function normalizePositiveAmount(amountCogtoshi, errorCode) {
25
- if (amountCogtoshi <= 0n) {
26
- throw new Error(errorCode);
27
- }
28
- return amountCogtoshi;
29
- }
30
- function encodeOpReturnScript(payload) {
31
- if (payload.length <= 75) {
32
- return Buffer.concat([
33
- Buffer.from([0x6a, payload.length]),
34
- Buffer.from(payload),
35
- ]).toString("hex");
36
- }
37
- return Buffer.concat([
38
- Buffer.from([0x6a, 0x4c, payload.length]),
39
- Buffer.from(payload),
40
- ]).toString("hex");
41
- }
42
- function satsToBtcNumber(value) {
43
- return Number(value) / 100_000_000;
44
- }
45
- function valueToSats(value) {
46
- const text = typeof value === "number" ? value.toFixed(8) : value;
47
- const match = /^(-?)(\d+)(?:\.(\d{0,8}))?$/.exec(text.trim());
48
- if (match == null) {
49
- throw new Error(`wallet_cog_invalid_amount_${text}`);
50
- }
51
- const sign = match[1] === "-" ? -1n : 1n;
52
- const whole = BigInt(match[2] ?? "0");
53
- const fraction = BigInt((match[3] ?? "").padEnd(8, "0"));
54
- return sign * ((whole * 100000000n) + fraction);
55
- }
56
- function createIntentFingerprint(parts) {
57
- return createHash("sha256")
58
- .update(parts.map((part) => String(part)).join("\n"))
59
- .digest("hex");
60
- }
61
- function parseHex32(value, errorCode) {
62
- const normalized = value.trim().toLowerCase();
63
- if (!/^[0-9a-f]{64}$/.test(normalized)) {
64
- throw new Error(errorCode);
65
- }
66
- return Buffer.from(normalized, "hex");
67
- }
68
- function sha256Hex(value) {
69
- return createHash("sha256").update(value).digest("hex");
70
- }
71
- function ensureUsableSender(sender, errorPrefix, amountCogtoshi) {
72
- if (sender.address === null) {
73
- throw new Error(`${errorPrefix}_sender_address_unavailable`);
74
- }
75
- if (sender.readOnly) {
76
- throw new Error(`${errorPrefix}_sender_read_only`);
77
- }
78
- if (sender.observedCogBalance === null || sender.observedCogBalance < amountCogtoshi) {
79
- throw new Error(`${errorPrefix}_insufficient_cog_balance`);
80
- }
81
- }
82
- function createResolvedSenderSummary(identity) {
83
- return {
84
- selector: getCanonicalIdentitySelector(identity),
85
- localIndex: identity.index,
86
- scriptPubKeyHex: identity.scriptPubKeyHex,
87
- address: identity.address,
88
- };
89
- }
90
- function resolveIdentitySender(context, errorPrefix, amountCogtoshi, selector) {
91
- assertWalletMutationContextReady(context, errorPrefix);
92
- const identity = resolveIdentityBySelector(context, selector ?? context.model.walletAddress ?? "", errorPrefix);
93
- ensureUsableSender(identity, errorPrefix, amountCogtoshi);
94
- return {
95
- state: context.localState.state,
96
- sender: createFundingMutationSender(context.localState.state),
97
- resolved: {
98
- sender: createResolvedSenderSummary(identity),
99
- claimPath: null,
100
- },
101
- };
102
- }
103
- function resolveClaimSender(context, lockId, preimageHex, reclaim) {
104
- const errorPrefix = reclaim ? "wallet_reclaim" : "wallet_claim";
105
- assertWalletMutationContextReady(context, errorPrefix);
106
- const currentHeight = context.snapshot.state.history.currentHeight;
107
- if (currentHeight === null) {
108
- throw new Error(`${errorPrefix}_current_height_unavailable`);
109
- }
110
- const lock = getLock(context.snapshot.state, lockId);
111
- if (lock === null || lock.status !== "active") {
112
- throw new Error(`${errorPrefix}_lock_not_found`);
113
- }
114
- const recipientDomain = lookupDomain(context.snapshot.state, context.model.domains.find((domain) => domain.domainId === lock.recipientDomainId)?.name ?? "")
115
- ?? [...context.snapshot.state.consensus.domainsById.values()].find((entry) => entry.domainId === lock.recipientDomainId)
116
- ?? null;
117
- const recipientDomainName = recipientDomain?.name ?? null;
118
- if (reclaim) {
119
- if (currentHeight < lock.timeoutHeight) {
120
- throw new Error("wallet_reclaim_before_timeout");
121
- }
122
- const lockerHex = Buffer.from(lock.lockerScriptPubKey).toString("hex");
123
- if (lockerHex !== context.localState.state.funding.scriptPubKeyHex || context.model.walletAddress == null) {
124
- throw new Error("wallet_reclaim_sender_not_local");
125
- }
126
- const senderIdentity = resolveIdentityBySelector(context, context.model.walletAddress, errorPrefix);
127
- ensureUsableSender(senderIdentity, errorPrefix, 0n);
128
- return {
129
- state: context.localState.state,
130
- sender: createFundingMutationSender(context.localState.state),
131
- recipientDomainName,
132
- amountCogtoshi: lock.amount,
133
- lockId: lock.lockId,
134
- resolved: {
135
- sender: createResolvedSenderSummary(senderIdentity),
136
- claimPath: "timeout-reclaim",
137
- },
138
- };
139
- }
140
- if (currentHeight >= lock.timeoutHeight) {
141
- throw new Error("wallet_claim_lock_expired");
142
- }
143
- const preimage = parseHex32(preimageHex, "wallet_claim_invalid_preimage");
144
- if (sha256Hex(preimage) !== Buffer.from(lock.condition).toString("hex")) {
145
- throw new Error("wallet_claim_preimage_mismatch");
146
- }
147
- if (recipientDomain === null) {
148
- throw new Error("wallet_claim_recipient_domain_missing");
149
- }
150
- const recipientOwnerHex = Buffer.from(recipientDomain.ownerScriptPubKey).toString("hex");
151
- if (recipientOwnerHex !== context.localState.state.funding.scriptPubKeyHex || context.model.walletAddress == null) {
152
- throw new Error("wallet_claim_sender_not_local");
153
- }
154
- const senderIdentity = resolveIdentityBySelector(context, context.model.walletAddress, errorPrefix);
155
- ensureUsableSender(senderIdentity, errorPrefix, 0n);
156
- return {
157
- state: context.localState.state,
158
- sender: createFundingMutationSender(context.localState.state),
159
- recipientDomainName,
160
- amountCogtoshi: lock.amount,
161
- lockId: lock.lockId,
162
- resolved: {
163
- sender: createResolvedSenderSummary(senderIdentity),
164
- claimPath: "recipient-claim",
165
- },
166
- };
167
- }
168
- function parseTimeoutHeight(currentHeight, rawRelative, rawAbsolute) {
169
- if ((rawRelative == null) === (rawAbsolute == null)) {
170
- throw new Error("wallet_lock_timeout_requires_exactly_one_mode");
171
- }
172
- if (rawAbsolute != null) {
173
- if (!Number.isInteger(rawAbsolute)) {
174
- throw new Error("wallet_lock_invalid_timeout_height");
175
- }
176
- return rawAbsolute;
177
- }
178
- const trimmed = rawRelative.trim().toLowerCase();
179
- let blocks;
180
- if (/^[1-9]\d*$/.test(trimmed)) {
181
- blocks = Number.parseInt(trimmed, 10);
182
- }
183
- else {
184
- const match = /^(\d+)(m|h|d|w)$/.exec(trimmed);
185
- if (match == null) {
186
- throw new Error("wallet_lock_invalid_timeout_duration");
187
- }
188
- const value = Number.parseInt(match[1], 10);
189
- const minutesPerUnit = match[2] === "m" ? 1
190
- : match[2] === "h" ? 60
191
- : match[2] === "d" ? 24 * 60
192
- : 7 * 24 * 60;
193
- blocks = Math.ceil((value * minutesPerUnit) / 10);
194
- }
195
- return currentHeight + blocks;
196
- }
197
- function buildPlanForCogOperation(options) {
198
- const fundingUtxos = options.allUtxos.filter((entry) => entry.scriptPubKey === options.state.funding.scriptPubKeyHex
199
- && entry.confirmations >= 1
200
- && entry.spendable !== false
201
- && entry.safe !== false);
202
- return {
203
- sender: options.sender,
204
- changeAddress: options.state.funding.address,
205
- fixedInputs: [],
206
- outputs: [{ data: Buffer.from(options.opReturnData).toString("hex") }],
207
- changePosition: 1,
208
- expectedOpReturnScriptHex: encodeOpReturnScript(options.opReturnData),
209
- allowedFundingScriptPubKeyHex: options.state.funding.scriptPubKeyHex,
210
- eligibleFundingOutpointKeys: new Set(fundingUtxos.map((entry) => outpointKey({ txid: entry.txid, vout: entry.vout }))),
211
- errorPrefix: options.errorPrefix,
212
- };
213
- }
214
- function validateFundedDraft(decoded, funded, plan) {
215
- const inputs = decoded.tx.vin;
216
- const outputs = decoded.tx.vout;
217
- if (inputs.length === 0) {
218
- throw new Error(`${plan.errorPrefix}_missing_sender_input`);
219
- }
220
- if (outputs[0]?.scriptPubKey?.hex !== plan.expectedOpReturnScriptHex) {
221
- throw new Error(`${plan.errorPrefix}_opreturn_mismatch`);
222
- }
223
- const expectedWithoutChange = 1;
224
- if (funded.changepos === -1) {
225
- if (outputs.length !== expectedWithoutChange) {
226
- throw new Error(`${plan.errorPrefix}_unexpected_output_count`);
227
- }
228
- return;
229
- }
230
- if (funded.changepos !== plan.changePosition || outputs.length !== expectedWithoutChange + 1) {
231
- throw new Error(`${plan.errorPrefix}_change_position_mismatch`);
232
- }
233
- if (outputs[funded.changepos]?.scriptPubKey?.hex !== plan.allowedFundingScriptPubKeyHex) {
234
- throw new Error(`${plan.errorPrefix}_change_output_mismatch`);
235
- }
236
- }
237
- async function buildTransaction(options) {
238
- return buildWalletMutationTransactionWithReserveFallback({
239
- rpc: options.rpc,
240
- walletName: options.walletName,
241
- state: options.state,
242
- plan: options.plan,
243
- validateFundedDraft,
244
- finalizeErrorCode: `${options.plan.errorPrefix}_finalize_failed`,
245
- mempoolRejectPrefix: `${options.plan.errorPrefix}_mempool_rejected`,
246
- feeRate: options.feeRateSatVb,
247
- });
248
- }
249
- function createDraftMutation(options) {
250
- if (options.existing !== null && options.existing !== undefined) {
251
- return {
252
- ...options.existing,
253
- kind: options.kind,
254
- domainName: options.domainName ?? "",
255
- senderScriptPubKeyHex: options.sender.scriptPubKeyHex,
256
- senderLocalIndex: options.sender.localIndex,
257
- recipientScriptPubKeyHex: options.recipientScriptPubKeyHex ?? null,
258
- recipientDomainName: options.recipientDomainName ?? null,
259
- amountCogtoshi: options.amountCogtoshi ?? null,
260
- timeoutHeight: options.timeoutHeight ?? null,
261
- conditionHex: options.conditionHex ?? null,
262
- lockId: options.lockId ?? null,
263
- preimageHex: options.preimageHex ?? null,
264
- status: "draft",
265
- lastUpdatedAtUnixMs: options.nowUnixMs,
266
- attemptedTxid: null,
267
- attemptedWtxid: null,
268
- ...createWalletMutationFeeMetadata(options.feeSelection),
269
- temporaryBuilderLockedOutpoints: [],
270
- };
271
- }
272
- return {
273
- mutationId: randomBytes(12).toString("hex"),
274
- kind: options.kind,
275
- domainName: options.domainName ?? "",
276
- parentDomainName: null,
277
- senderScriptPubKeyHex: options.sender.scriptPubKeyHex,
278
- senderLocalIndex: options.sender.localIndex,
279
- recipientScriptPubKeyHex: options.recipientScriptPubKeyHex ?? null,
280
- recipientDomainName: options.recipientDomainName ?? null,
281
- amountCogtoshi: options.amountCogtoshi ?? null,
282
- timeoutHeight: options.timeoutHeight ?? null,
283
- conditionHex: options.conditionHex ?? null,
284
- lockId: options.lockId ?? null,
285
- preimageHex: options.preimageHex ?? null,
286
- intentFingerprintHex: options.intentFingerprintHex,
287
- status: "draft",
288
- createdAtUnixMs: options.nowUnixMs,
289
- lastUpdatedAtUnixMs: options.nowUnixMs,
290
- attemptedTxid: null,
291
- attemptedWtxid: null,
292
- ...createWalletMutationFeeMetadata(options.feeSelection),
293
- temporaryBuilderLockedOutpoints: [],
294
- };
295
- }
296
- async function reconcilePendingCogMutation(options) {
297
- if (options.mutation.status === "confirmed" || options.mutation.status === "live") {
298
- return {
299
- state: options.state,
300
- mutation: options.mutation,
301
- resolution: options.mutation.status,
302
- };
303
- }
304
- if (options.mutation.status === "repair-required") {
305
- return {
306
- state: options.state,
307
- mutation: options.mutation,
308
- resolution: "repair-required",
309
- };
310
- }
311
- if (options.mutation.kind === "claim" && options.context.snapshot !== null && options.mutation.lockId != null) {
312
- const lock = getLock(options.context.snapshot.state, options.mutation.lockId);
313
- const expectedStatus = options.mutation.preimageHex === ZERO_PREIMAGE_HEX ? "reclaimed" : "claimed";
314
- if (lock !== null
315
- && lock.status === expectedStatus
316
- && Buffer.from(lock.resolverScriptPubKey ?? new Uint8Array()).toString("hex") === options.mutation.senderScriptPubKeyHex) {
317
- await unlockTemporaryBuilderLocks(options.rpc, options.walletName, options.mutation.temporaryBuilderLockedOutpoints);
318
- const confirmed = updateMutationRecord(options.mutation, "confirmed", options.nowUnixMs, {
319
- temporaryBuilderLockedOutpoints: [],
320
- });
321
- const nextState = {
322
- ...upsertPendingMutation(options.state, confirmed),
323
- stateRevision: options.state.stateRevision + 1,
324
- lastWrittenAtUnixMs: options.nowUnixMs,
325
- };
326
- await saveWalletStatePreservingUnlock({
327
- state: nextState,
328
- provider: options.provider,
329
- nowUnixMs: options.nowUnixMs,
330
- paths: options.paths,
331
- });
332
- return { state: nextState, mutation: confirmed, resolution: "confirmed" };
333
- }
334
- }
335
- const known = options.mutation.attemptedTxid === null
336
- ? false
337
- : await options.rpc.getRawTransaction(options.mutation.attemptedTxid, true).then(() => true).catch(() => false);
338
- if (known) {
339
- await unlockTemporaryBuilderLocks(options.rpc, options.walletName, options.mutation.temporaryBuilderLockedOutpoints);
340
- const live = updateMutationRecord(options.mutation, "live", options.nowUnixMs, {
341
- temporaryBuilderLockedOutpoints: [],
342
- });
343
- const nextState = {
344
- ...upsertPendingMutation(options.state, live),
345
- stateRevision: options.state.stateRevision + 1,
346
- lastWrittenAtUnixMs: options.nowUnixMs,
347
- };
348
- await saveWalletStatePreservingUnlock({
349
- state: nextState,
350
- provider: options.provider,
351
- nowUnixMs: options.nowUnixMs,
352
- paths: options.paths,
353
- });
354
- return { state: nextState, mutation: live, resolution: "live" };
355
- }
356
- return { state: options.state, mutation: options.mutation, resolution: "continue" };
357
- }
358
- async function confirmSend(prompter, resolved, target, normalizedRecipient, amountCogtoshi, assumeYes = false) {
359
- prompter.writeLine(`You are sending ${formatCogAmount(amountCogtoshi)}.`);
360
- prompter.writeLine(`Resolved sender: ${resolved.sender.selector} (${resolved.sender.address})`);
361
- prompter.writeLine(`Recipient: ${normalizedRecipient.address ?? `spk:${normalizedRecipient.scriptPubKeyHex}`}`);
362
- if (normalizedRecipient.opaque) {
363
- await confirmTypedAcknowledgement(prompter, {
364
- assumeYes,
365
- expected: target.trim(),
366
- prompt: "Type the exact target to continue: ",
367
- errorCode: "wallet_send_confirmation_rejected",
368
- requiresTtyErrorCode: "wallet_send_requires_tty",
369
- typedAckRequiredErrorCode: "wallet_send_typed_ack_required",
370
- });
371
- return;
372
- }
373
- await confirmYesNo(prompter, "This will publish an on-chain COG transfer.", {
374
- assumeYes,
375
- errorCode: "wallet_send_confirmation_rejected",
376
- requiresTtyErrorCode: "wallet_send_requires_tty",
377
- });
378
- }
379
- async function confirmLock(prompter, resolved, amountCogtoshi, recipientDomainName, timeoutHeight, assumeYes = false) {
380
- prompter.writeLine(`You are locking ${formatCogAmount(amountCogtoshi)}.`);
381
- prompter.writeLine(`Resolved sender: ${resolved.sender.selector} (${resolved.sender.address})`);
382
- prompter.writeLine(`Recipient domain: ${recipientDomainName}`);
383
- prompter.writeLine(`Resolved timeout height: ${timeoutHeight}`);
384
- await confirmYesNo(prompter, "This creates an escrowed COG lock and the funds cannot be spent until claimed or reclaimed.", {
385
- assumeYes,
386
- errorCode: "wallet_mutation_confirmation_rejected",
387
- requiresTtyErrorCode: "wallet_lock_requires_tty",
388
- });
389
- }
390
- async function confirmClaim(prompter, options) {
391
- prompter.writeLine(`${options.kind === "claim" ? "Claiming" : "Reclaiming"} lock:${options.lockId} for ${formatCogAmount(options.amountCogtoshi)}.`);
392
- prompter.writeLine(`Resolved sender: ${options.resolved.sender.selector} (${options.resolved.sender.address})`);
393
- if (options.resolved.claimPath !== null) {
394
- prompter.writeLine(`Resolved path: ${options.resolved.claimPath}.`);
395
- }
396
- if (options.recipientDomainName !== null) {
397
- prompter.writeLine(`Recipient domain: ${options.recipientDomainName}`);
398
- }
399
- if (options.kind === "claim") {
400
- prompter.writeLine("Warning: the claim preimage becomes public in the mempool and on-chain.");
401
- }
402
- await confirmYesNo(prompter, options.kind === "claim"
403
- ? "This spends the lock via the recipient claim path."
404
- : "This spends the lock via the timeout reclaim path.", {
405
- assumeYes: options.assumeYes,
406
- errorCode: options.kind === "claim"
407
- ? "wallet_claim_confirmation_rejected"
408
- : "wallet_reclaim_confirmation_rejected",
409
- requiresTtyErrorCode: options.kind === "claim"
410
- ? "wallet_claim_requires_tty"
411
- : "wallet_reclaim_requires_tty",
412
- });
413
- }
414
- export async function sendCog(options) {
415
- const amountCogtoshi = normalizePositiveAmount(options.amountCogtoshi, "wallet_send_invalid_amount");
416
- const recipient = normalizeBtcTarget(options.target);
417
- const execution = await executeWalletMutationOperation({
418
- ...options,
419
- controlLockPurpose: "wallet-send",
420
- preemptionReason: "wallet-send",
421
- resolveOperation(readContext) {
422
- const operation = resolveIdentitySender(readContext, "wallet_send", amountCogtoshi, options.fromIdentity);
423
- if (operation.sender.scriptPubKeyHex === recipient.scriptPubKeyHex) {
424
- throw new Error("wallet_send_self_transfer");
425
- }
426
- return {
427
- ...operation,
428
- amountCogtoshi,
429
- recipient,
430
- };
431
- },
432
- createIntentFingerprint(operation) {
433
- return createIntentFingerprint([
434
- "send",
435
- operation.state.walletRootId,
436
- operation.sender.scriptPubKeyHex,
437
- operation.recipient.scriptPubKeyHex,
438
- operation.amountCogtoshi,
439
- ]);
440
- },
441
- async resolveExistingMutation({ operation, existingMutation, execution }) {
442
- if (existingMutation === null) {
443
- return { state: operation.state, replacementFixedInputs: null, result: null };
444
- }
445
- return resolveExistingWalletMutation({
446
- existingMutation,
447
- execution,
448
- repairRequiredErrorCode: "wallet_send_repair_required",
449
- reconcileExistingMutation: (mutation) => reconcilePendingCogMutation({
450
- state: operation.state,
451
- mutation,
452
- provider: execution.provider,
453
- nowUnixMs: execution.nowUnixMs,
454
- paths: execution.paths,
455
- rpc: execution.rpc,
456
- walletName: execution.walletName,
457
- context: execution.readContext,
458
- }),
459
- createReuseResult: ({ mutation, resolution, fees }) => ({
460
- kind: "send",
461
- txid: mutation.attemptedTxid ?? "unknown",
462
- status: resolution,
463
- reusedExisting: true,
464
- amountCogtoshi: operation.amountCogtoshi,
465
- recipientScriptPubKeyHex: operation.recipient.scriptPubKeyHex,
466
- resolved: operation.resolved,
467
- fees,
468
- }),
469
- });
470
- },
471
- confirm({ operation }) {
472
- return confirmSend(options.prompter, operation.resolved, options.target, operation.recipient, operation.amountCogtoshi, options.assumeYes);
473
- },
474
- createDraftMutation({ operation, existingMutation, execution, intentFingerprintHex }) {
475
- return {
476
- mutation: createDraftMutation({
477
- kind: "send",
478
- sender: operation.sender,
479
- recipientScriptPubKeyHex: operation.recipient.scriptPubKeyHex,
480
- amountCogtoshi: operation.amountCogtoshi,
481
- intentFingerprintHex,
482
- nowUnixMs: execution.nowUnixMs,
483
- feeSelection: execution.feeSelection,
484
- existing: existingMutation,
485
- }),
486
- prepared: null,
487
- };
488
- },
489
- async build({ operation, state, execution, replacementFixedInputs }) {
490
- const sendPlan = buildPlanForCogOperation({
491
- state,
492
- allUtxos: await execution.rpc.listUnspent(execution.walletName, 1),
493
- sender: operation.sender,
494
- opReturnData: serializeCogTransfer(operation.amountCogtoshi, Buffer.from(operation.recipient.scriptPubKeyHex, "hex")).opReturnData,
495
- errorPrefix: "wallet_send",
496
- });
497
- return buildTransaction({
498
- rpc: execution.rpc,
499
- walletName: execution.walletName,
500
- state,
501
- plan: {
502
- ...sendPlan,
503
- fixedInputs: mergeFixedWalletInputs(sendPlan.fixedInputs, replacementFixedInputs),
504
- },
505
- feeRateSatVb: execution.feeSelection.feeRateSatVb,
506
- });
507
- },
508
- publish({ state, execution, built, mutation }) {
509
- return publishWalletMutation({
510
- rpc: execution.rpc,
511
- walletName: execution.walletName,
512
- snapshotHeight: execution.readContext.snapshot?.tip?.height ?? null,
513
- built,
514
- mutation,
515
- state,
516
- provider: execution.provider,
517
- nowUnixMs: execution.nowUnixMs,
518
- paths: execution.paths,
519
- errorPrefix: "wallet_send",
520
- });
521
- },
522
- createResult({ operation, mutation, built, status, reusedExisting, fees }) {
523
- return {
524
- kind: "send",
525
- txid: mutation.attemptedTxid ?? built?.txid ?? "unknown",
526
- status: status,
527
- reusedExisting,
528
- amountCogtoshi: operation.amountCogtoshi,
529
- recipientScriptPubKeyHex: operation.recipient.scriptPubKeyHex,
530
- resolved: operation.resolved,
531
- fees,
532
- };
533
- },
534
- });
535
- return execution.result;
536
- }
537
- export async function lockCogToDomain(options) {
538
- const amountCogtoshi = normalizePositiveAmount(options.amountCogtoshi, "wallet_lock_invalid_amount");
539
- const normalizedRecipientDomainName = normalizeDomainName(options.recipientDomainName);
540
- const condition = parseHex32(options.conditionHex, "wallet_lock_invalid_condition");
541
- if (condition.equals(Buffer.alloc(32))) {
542
- throw new Error("wallet_lock_invalid_condition");
543
- }
544
- const execution = await executeWalletMutationOperation({
545
- ...options,
546
- controlLockPurpose: "wallet-lock-cog",
547
- preemptionReason: "wallet-cog-lock",
548
- resolveOperation(readContext) {
549
- assertWalletMutationContextReady(readContext, "wallet_lock");
550
- const currentHeight = readContext.snapshot.state.history.currentHeight;
551
- if (currentHeight === null) {
552
- throw new Error("wallet_lock_current_height_unavailable");
553
- }
554
- const timeoutHeight = parseTimeoutHeight(currentHeight, options.timeoutBlocksOrDuration, options.timeoutHeight ?? null);
555
- if (timeoutHeight <= currentHeight || timeoutHeight > currentHeight + MAX_LOCK_DURATION_BLOCKS) {
556
- throw new Error("wallet_lock_invalid_timeout_height");
557
- }
558
- const recipientDomain = lookupDomain(readContext.snapshot.state, normalizedRecipientDomainName);
559
- if (recipientDomain === null) {
560
- throw new Error("wallet_lock_domain_not_found");
561
- }
562
- if (!recipientDomain.anchored) {
563
- throw new Error("wallet_lock_domain_not_anchored");
564
- }
565
- if (readContext.snapshot.state.consensus.nextLockId === 0xffff_ffff) {
566
- throw new Error("wallet_lock_id_space_exhausted");
567
- }
568
- return {
569
- ...resolveIdentitySender(readContext, "wallet_lock", amountCogtoshi, options.fromIdentity),
570
- amountCogtoshi,
571
- normalizedRecipientDomainName,
572
- recipientDomain,
573
- timeoutHeight,
574
- conditionHex: Buffer.from(condition).toString("hex"),
575
- };
576
- },
577
- createIntentFingerprint(operation) {
578
- return createIntentFingerprint([
579
- "lock",
580
- operation.state.walletRootId,
581
- operation.sender.scriptPubKeyHex,
582
- operation.normalizedRecipientDomainName,
583
- operation.amountCogtoshi,
584
- operation.timeoutHeight,
585
- operation.conditionHex,
586
- ]);
587
- },
588
- async resolveExistingMutation({ operation, existingMutation, execution }) {
589
- if (existingMutation === null) {
590
- return { state: operation.state, replacementFixedInputs: null, result: null };
591
- }
592
- return resolveExistingWalletMutation({
593
- existingMutation,
594
- execution,
595
- repairRequiredErrorCode: "wallet_lock_repair_required",
596
- reconcileExistingMutation: (mutation) => reconcilePendingCogMutation({
597
- state: operation.state,
598
- mutation,
599
- provider: execution.provider,
600
- nowUnixMs: execution.nowUnixMs,
601
- paths: execution.paths,
602
- rpc: execution.rpc,
603
- walletName: execution.walletName,
604
- context: execution.readContext,
605
- }),
606
- createReuseResult: ({ mutation, resolution, fees }) => ({
607
- kind: "lock",
608
- txid: mutation.attemptedTxid ?? "unknown",
609
- status: resolution,
610
- reusedExisting: true,
611
- amountCogtoshi: operation.amountCogtoshi,
612
- recipientDomainName: operation.normalizedRecipientDomainName,
613
- resolved: operation.resolved,
614
- fees,
615
- }),
616
- });
617
- },
618
- confirm({ operation }) {
619
- return confirmLock(options.prompter, operation.resolved, operation.amountCogtoshi, operation.normalizedRecipientDomainName, operation.timeoutHeight, options.assumeYes);
620
- },
621
- createDraftMutation({ operation, existingMutation, execution, intentFingerprintHex }) {
622
- return {
623
- mutation: createDraftMutation({
624
- kind: "lock",
625
- sender: operation.sender,
626
- amountCogtoshi: operation.amountCogtoshi,
627
- recipientDomainName: operation.normalizedRecipientDomainName,
628
- timeoutHeight: operation.timeoutHeight,
629
- conditionHex: operation.conditionHex,
630
- intentFingerprintHex,
631
- nowUnixMs: execution.nowUnixMs,
632
- feeSelection: execution.feeSelection,
633
- existing: existingMutation,
634
- }),
635
- prepared: null,
636
- };
637
- },
638
- async build({ operation, state, execution, replacementFixedInputs }) {
639
- const lockPlan = buildPlanForCogOperation({
640
- state,
641
- allUtxos: await execution.rpc.listUnspent(execution.walletName, 1),
642
- sender: operation.sender,
643
- opReturnData: serializeCogLock(operation.amountCogtoshi, operation.timeoutHeight, operation.recipientDomain.domainId, Buffer.from(operation.conditionHex, "hex")).opReturnData,
644
- errorPrefix: "wallet_lock",
645
- });
646
- return buildTransaction({
647
- rpc: execution.rpc,
648
- walletName: execution.walletName,
649
- state,
650
- plan: {
651
- ...lockPlan,
652
- fixedInputs: mergeFixedWalletInputs(lockPlan.fixedInputs, replacementFixedInputs),
653
- },
654
- feeRateSatVb: execution.feeSelection.feeRateSatVb,
655
- });
656
- },
657
- publish({ state, execution, built, mutation }) {
658
- return publishWalletMutation({
659
- rpc: execution.rpc,
660
- walletName: execution.walletName,
661
- snapshotHeight: execution.readContext.snapshot?.tip?.height ?? null,
662
- built,
663
- mutation,
664
- state,
665
- provider: execution.provider,
666
- nowUnixMs: execution.nowUnixMs,
667
- paths: execution.paths,
668
- errorPrefix: "wallet_lock",
669
- });
670
- },
671
- createResult({ operation, mutation, built, status, reusedExisting, fees }) {
672
- return {
673
- kind: "lock",
674
- txid: mutation.attemptedTxid ?? built?.txid ?? "unknown",
675
- status: status,
676
- reusedExisting,
677
- amountCogtoshi: operation.amountCogtoshi,
678
- recipientDomainName: operation.normalizedRecipientDomainName,
679
- resolved: operation.resolved,
680
- fees,
681
- };
682
- },
683
- });
684
- return execution.result;
685
- }
686
- async function runClaimLikeMutation(options, reclaim) {
687
- const preimageHex = reclaim ? ZERO_PREIMAGE_HEX : options.preimageHex;
688
- const errorPrefix = reclaim ? "wallet_reclaim" : "wallet_claim";
689
- const execution = await executeWalletMutationOperation({
690
- ...options,
691
- controlLockPurpose: reclaim ? "wallet-reclaim" : "wallet-claim",
692
- preemptionReason: reclaim ? "wallet-reclaim" : "wallet-claim",
693
- resolveOperation(readContext) {
694
- return {
695
- ...resolveClaimSender(readContext, options.lockId, preimageHex, reclaim),
696
- preimageHex,
697
- errorPrefix,
698
- };
699
- },
700
- createIntentFingerprint(operation) {
701
- return createIntentFingerprint([
702
- reclaim ? "reclaim" : "claim",
703
- operation.state.walletRootId,
704
- operation.sender.scriptPubKeyHex,
705
- operation.lockId,
706
- operation.preimageHex,
707
- ]);
708
- },
709
- async resolveExistingMutation({ operation, existingMutation, execution }) {
710
- if (existingMutation === null) {
711
- return { state: operation.state, replacementFixedInputs: null, result: null };
712
- }
713
- return resolveExistingWalletMutation({
714
- existingMutation,
715
- execution,
716
- repairRequiredErrorCode: `${errorPrefix}_repair_required`,
717
- reconcileExistingMutation: (mutation) => reconcilePendingCogMutation({
718
- state: operation.state,
719
- mutation,
720
- provider: execution.provider,
721
- nowUnixMs: execution.nowUnixMs,
722
- paths: execution.paths,
723
- rpc: execution.rpc,
724
- walletName: execution.walletName,
725
- context: execution.readContext,
726
- }),
727
- createReuseResult: ({ mutation, resolution, fees }) => ({
728
- kind: "claim",
729
- txid: mutation.attemptedTxid ?? "unknown",
730
- status: resolution,
731
- reusedExisting: true,
732
- amountCogtoshi: operation.amountCogtoshi,
733
- recipientDomainName: operation.recipientDomainName,
734
- lockId: operation.lockId,
735
- resolved: operation.resolved,
736
- fees,
737
- }),
738
- });
739
- },
740
- confirm({ operation }) {
741
- return confirmClaim(options.prompter, {
742
- kind: reclaim ? "reclaim" : "claim",
743
- lockId: operation.lockId,
744
- recipientDomainName: operation.recipientDomainName,
745
- amountCogtoshi: operation.amountCogtoshi,
746
- resolved: operation.resolved,
747
- assumeYes: options.assumeYes,
748
- });
749
- },
750
- createDraftMutation({ operation, existingMutation, execution, intentFingerprintHex }) {
751
- return {
752
- mutation: createDraftMutation({
753
- kind: "claim",
754
- sender: operation.sender,
755
- amountCogtoshi: operation.amountCogtoshi,
756
- recipientDomainName: operation.recipientDomainName,
757
- lockId: operation.lockId,
758
- preimageHex: operation.preimageHex,
759
- intentFingerprintHex,
760
- nowUnixMs: execution.nowUnixMs,
761
- feeSelection: execution.feeSelection,
762
- existing: existingMutation,
763
- }),
764
- prepared: null,
765
- };
766
- },
767
- async build({ operation, state, execution, replacementFixedInputs }) {
768
- const claimPlan = buildPlanForCogOperation({
769
- state,
770
- allUtxos: await execution.rpc.listUnspent(execution.walletName, 1),
771
- sender: operation.sender,
772
- opReturnData: serializeCogClaim(operation.lockId, Buffer.from(operation.preimageHex, "hex")).opReturnData,
773
- errorPrefix,
774
- });
775
- return buildTransaction({
776
- rpc: execution.rpc,
777
- walletName: execution.walletName,
778
- state,
779
- plan: {
780
- ...claimPlan,
781
- fixedInputs: mergeFixedWalletInputs(claimPlan.fixedInputs, replacementFixedInputs),
782
- },
783
- feeRateSatVb: execution.feeSelection.feeRateSatVb,
784
- });
785
- },
786
- publish({ state, execution, built, mutation }) {
787
- return publishWalletMutation({
788
- rpc: execution.rpc,
789
- walletName: execution.walletName,
790
- snapshotHeight: execution.readContext.snapshot?.tip?.height ?? null,
791
- built,
792
- mutation,
793
- state,
794
- provider: execution.provider,
795
- nowUnixMs: execution.nowUnixMs,
796
- paths: execution.paths,
797
- errorPrefix,
798
- });
799
- },
800
- createResult({ operation, mutation, built, status, reusedExisting, fees }) {
801
- return {
802
- kind: "claim",
803
- txid: mutation.attemptedTxid ?? built?.txid ?? "unknown",
804
- status: status,
805
- reusedExisting,
806
- amountCogtoshi: operation.amountCogtoshi,
807
- recipientDomainName: operation.recipientDomainName,
808
- lockId: operation.lockId,
809
- resolved: operation.resolved,
810
- fees,
811
- };
812
- },
813
- });
814
- return execution.result;
815
- }
816
- export async function claimCogLock(options) {
817
- return runClaimLikeMutation(options, false);
818
- }
819
- export async function reclaimCogLock(options) {
820
- return runClaimLikeMutation({
821
- ...options,
822
- preimageHex: ZERO_PREIMAGE_HEX,
823
- }, true);
824
- }
1
+ export * from "./cog/index.js";