@cogcoin/client 0.5.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 (289) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +136 -0
  3. package/dist/app-paths.d.ts +38 -0
  4. package/dist/app-paths.js +121 -0
  5. package/dist/art/banner.txt +13 -0
  6. package/dist/art/scroll.txt +13 -0
  7. package/dist/art/train-car.txt +6 -0
  8. package/dist/art/train-smoke.txt +6 -0
  9. package/dist/art/train.txt +6 -0
  10. package/dist/bitcoind/bootstrap/chainstate.d.ts +4 -0
  11. package/dist/bitcoind/bootstrap/chainstate.js +13 -0
  12. package/dist/bitcoind/bootstrap/constants.d.ts +7 -0
  13. package/dist/bitcoind/bootstrap/constants.js +12 -0
  14. package/dist/bitcoind/bootstrap/controller.d.ts +29 -0
  15. package/dist/bitcoind/bootstrap/controller.js +101 -0
  16. package/dist/bitcoind/bootstrap/download.d.ts +2 -0
  17. package/dist/bitcoind/bootstrap/download.js +196 -0
  18. package/dist/bitcoind/bootstrap/headers.d.ts +13 -0
  19. package/dist/bitcoind/bootstrap/headers.js +61 -0
  20. package/dist/bitcoind/bootstrap/paths.d.ts +4 -0
  21. package/dist/bitcoind/bootstrap/paths.js +15 -0
  22. package/dist/bitcoind/bootstrap/snapshot-file.d.ts +7 -0
  23. package/dist/bitcoind/bootstrap/snapshot-file.js +42 -0
  24. package/dist/bitcoind/bootstrap/state.d.ts +40 -0
  25. package/dist/bitcoind/bootstrap/state.js +70 -0
  26. package/dist/bitcoind/bootstrap/types.d.ts +28 -0
  27. package/dist/bitcoind/bootstrap/types.js +1 -0
  28. package/dist/bitcoind/bootstrap.d.ts +8 -0
  29. package/dist/bitcoind/bootstrap.js +7 -0
  30. package/dist/bitcoind/client/factory.d.ts +3 -0
  31. package/dist/bitcoind/client/factory.js +57 -0
  32. package/dist/bitcoind/client/follow-block-times.d.ts +8 -0
  33. package/dist/bitcoind/client/follow-block-times.js +25 -0
  34. package/dist/bitcoind/client/follow-loop.d.ts +10 -0
  35. package/dist/bitcoind/client/follow-loop.js +57 -0
  36. package/dist/bitcoind/client/internal-types.d.ts +63 -0
  37. package/dist/bitcoind/client/internal-types.js +18 -0
  38. package/dist/bitcoind/client/managed-client.d.ts +20 -0
  39. package/dist/bitcoind/client/managed-client.js +197 -0
  40. package/dist/bitcoind/client/rate-tracker.d.ts +2 -0
  41. package/dist/bitcoind/client/rate-tracker.js +24 -0
  42. package/dist/bitcoind/client/sync-engine.d.ts +3 -0
  43. package/dist/bitcoind/client/sync-engine.js +143 -0
  44. package/dist/bitcoind/client.d.ts +1 -0
  45. package/dist/bitcoind/client.js +1 -0
  46. package/dist/bitcoind/errors.d.ts +1 -0
  47. package/dist/bitcoind/errors.js +49 -0
  48. package/dist/bitcoind/index.d.ts +2 -0
  49. package/dist/bitcoind/index.js +1 -0
  50. package/dist/bitcoind/indexer-daemon-main.d.ts +1 -0
  51. package/dist/bitcoind/indexer-daemon-main.js +472 -0
  52. package/dist/bitcoind/indexer-daemon.d.ts +107 -0
  53. package/dist/bitcoind/indexer-daemon.js +391 -0
  54. package/dist/bitcoind/node.d.ts +8 -0
  55. package/dist/bitcoind/node.js +219 -0
  56. package/dist/bitcoind/normalize.d.ts +3 -0
  57. package/dist/bitcoind/normalize.js +47 -0
  58. package/dist/bitcoind/progress/assets.d.ts +10 -0
  59. package/dist/bitcoind/progress/assets.js +90 -0
  60. package/dist/bitcoind/progress/constants.d.ts +48 -0
  61. package/dist/bitcoind/progress/constants.js +53 -0
  62. package/dist/bitcoind/progress/controller.d.ts +28 -0
  63. package/dist/bitcoind/progress/controller.js +188 -0
  64. package/dist/bitcoind/progress/follow-scene.d.ts +40 -0
  65. package/dist/bitcoind/progress/follow-scene.js +367 -0
  66. package/dist/bitcoind/progress/formatting.d.ts +23 -0
  67. package/dist/bitcoind/progress/formatting.js +227 -0
  68. package/dist/bitcoind/progress/quote-scene.d.ts +4 -0
  69. package/dist/bitcoind/progress/quote-scene.js +137 -0
  70. package/dist/bitcoind/progress/train-scene.d.ts +9 -0
  71. package/dist/bitcoind/progress/train-scene.js +92 -0
  72. package/dist/bitcoind/progress/tty-renderer.d.ts +18 -0
  73. package/dist/bitcoind/progress/tty-renderer.js +150 -0
  74. package/dist/bitcoind/progress.d.ts +7 -0
  75. package/dist/bitcoind/progress.js +7 -0
  76. package/dist/bitcoind/quotes.d.ts +24 -0
  77. package/dist/bitcoind/quotes.js +195 -0
  78. package/dist/bitcoind/rpc.d.ts +71 -0
  79. package/dist/bitcoind/rpc.js +322 -0
  80. package/dist/bitcoind/service-paths.d.ts +19 -0
  81. package/dist/bitcoind/service-paths.js +49 -0
  82. package/dist/bitcoind/service.d.ts +40 -0
  83. package/dist/bitcoind/service.js +735 -0
  84. package/dist/bitcoind/testing.d.ts +9 -0
  85. package/dist/bitcoind/testing.js +9 -0
  86. package/dist/bitcoind/types.d.ts +396 -0
  87. package/dist/bitcoind/types.js +3 -0
  88. package/dist/bytes.d.ts +9 -0
  89. package/dist/bytes.js +36 -0
  90. package/dist/cli/commands/follow.d.ts +2 -0
  91. package/dist/cli/commands/follow.js +43 -0
  92. package/dist/cli/commands/mining-admin.d.ts +2 -0
  93. package/dist/cli/commands/mining-admin.js +92 -0
  94. package/dist/cli/commands/mining-read.d.ts +2 -0
  95. package/dist/cli/commands/mining-read.js +173 -0
  96. package/dist/cli/commands/mining-runtime.d.ts +2 -0
  97. package/dist/cli/commands/mining-runtime.js +108 -0
  98. package/dist/cli/commands/status.d.ts +2 -0
  99. package/dist/cli/commands/status.js +31 -0
  100. package/dist/cli/commands/sync.d.ts +2 -0
  101. package/dist/cli/commands/sync.js +52 -0
  102. package/dist/cli/commands/wallet-admin.d.ts +2 -0
  103. package/dist/cli/commands/wallet-admin.js +175 -0
  104. package/dist/cli/commands/wallet-mutation.d.ts +2 -0
  105. package/dist/cli/commands/wallet-mutation.js +681 -0
  106. package/dist/cli/commands/wallet-read.d.ts +2 -0
  107. package/dist/cli/commands/wallet-read.js +265 -0
  108. package/dist/cli/context.d.ts +3 -0
  109. package/dist/cli/context.js +75 -0
  110. package/dist/cli/io.d.ts +3 -0
  111. package/dist/cli/io.js +12 -0
  112. package/dist/cli/mining-format.d.ts +5 -0
  113. package/dist/cli/mining-format.js +156 -0
  114. package/dist/cli/mining-json.d.ts +49 -0
  115. package/dist/cli/mining-json.js +89 -0
  116. package/dist/cli/mutation-command-groups.d.ts +15 -0
  117. package/dist/cli/mutation-command-groups.js +71 -0
  118. package/dist/cli/mutation-json.d.ts +430 -0
  119. package/dist/cli/mutation-json.js +311 -0
  120. package/dist/cli/mutation-resolved-json.d.ts +124 -0
  121. package/dist/cli/mutation-resolved-json.js +129 -0
  122. package/dist/cli/mutation-success.d.ts +20 -0
  123. package/dist/cli/mutation-success.js +47 -0
  124. package/dist/cli/mutation-text-format.d.ts +22 -0
  125. package/dist/cli/mutation-text-format.js +171 -0
  126. package/dist/cli/mutation-text-write.d.ts +13 -0
  127. package/dist/cli/mutation-text-write.js +16 -0
  128. package/dist/cli/output.d.ts +185 -0
  129. package/dist/cli/output.js +1085 -0
  130. package/dist/cli/parse.d.ts +3 -0
  131. package/dist/cli/parse.js +971 -0
  132. package/dist/cli/preview-json.d.ts +416 -0
  133. package/dist/cli/preview-json.js +293 -0
  134. package/dist/cli/prompt.d.ts +3 -0
  135. package/dist/cli/prompt.js +33 -0
  136. package/dist/cli/read-json.d.ts +187 -0
  137. package/dist/cli/read-json.js +675 -0
  138. package/dist/cli/runner.d.ts +2 -0
  139. package/dist/cli/runner.js +129 -0
  140. package/dist/cli/signals.d.ts +3 -0
  141. package/dist/cli/signals.js +63 -0
  142. package/dist/cli/status-format.d.ts +2 -0
  143. package/dist/cli/status-format.js +48 -0
  144. package/dist/cli/types.d.ts +148 -0
  145. package/dist/cli/types.js +2 -0
  146. package/dist/cli/wallet-format.d.ts +29 -0
  147. package/dist/cli/wallet-format.js +637 -0
  148. package/dist/cli/workflow-hints.d.ts +13 -0
  149. package/dist/cli/workflow-hints.js +94 -0
  150. package/dist/cli-runner.d.ts +3 -0
  151. package/dist/cli-runner.js +3 -0
  152. package/dist/cli.d.ts +2 -0
  153. package/dist/cli.js +6 -0
  154. package/dist/client/default-client.d.ts +11 -0
  155. package/dist/client/default-client.js +118 -0
  156. package/dist/client/factory.d.ts +2 -0
  157. package/dist/client/factory.js +15 -0
  158. package/dist/client/initialization.d.ts +6 -0
  159. package/dist/client/initialization.js +30 -0
  160. package/dist/client/persistence.d.ts +5 -0
  161. package/dist/client/persistence.js +28 -0
  162. package/dist/client/store-adapter.d.ts +3 -0
  163. package/dist/client/store-adapter.js +20 -0
  164. package/dist/client.d.ts +2 -0
  165. package/dist/client.js +2 -0
  166. package/dist/index.d.ts +2 -0
  167. package/dist/index.js +1 -0
  168. package/dist/passive-status.d.ts +36 -0
  169. package/dist/passive-status.js +100 -0
  170. package/dist/sqlite/better-sqlite3.d.ts +26 -0
  171. package/dist/sqlite/better-sqlite3.js +4 -0
  172. package/dist/sqlite/checkpoints.d.ts +11 -0
  173. package/dist/sqlite/checkpoints.js +27 -0
  174. package/dist/sqlite/driver.d.ts +17 -0
  175. package/dist/sqlite/driver.js +98 -0
  176. package/dist/sqlite/index.d.ts +4 -0
  177. package/dist/sqlite/index.js +9 -0
  178. package/dist/sqlite/migrate.d.ts +2 -0
  179. package/dist/sqlite/migrate.js +37 -0
  180. package/dist/sqlite/store.d.ts +3 -0
  181. package/dist/sqlite/store.js +122 -0
  182. package/dist/sqlite/tip-meta.d.ts +26 -0
  183. package/dist/sqlite/tip-meta.js +97 -0
  184. package/dist/sqlite/types.d.ts +10 -0
  185. package/dist/sqlite/types.js +1 -0
  186. package/dist/types.d.ts +55 -0
  187. package/dist/types.js +1 -0
  188. package/dist/wallet/archive.d.ts +4 -0
  189. package/dist/wallet/archive.js +39 -0
  190. package/dist/wallet/cogop/constants.d.ts +32 -0
  191. package/dist/wallet/cogop/constants.js +32 -0
  192. package/dist/wallet/cogop/index.d.ts +32 -0
  193. package/dist/wallet/cogop/index.js +213 -0
  194. package/dist/wallet/cogop/numeric.d.ts +3 -0
  195. package/dist/wallet/cogop/numeric.js +24 -0
  196. package/dist/wallet/cogop/scriptpubkey.d.ts +2 -0
  197. package/dist/wallet/cogop/scriptpubkey.js +13 -0
  198. package/dist/wallet/cogop/validate-name.d.ts +2 -0
  199. package/dist/wallet/cogop/validate-name.js +18 -0
  200. package/dist/wallet/fs/atomic.d.ts +6 -0
  201. package/dist/wallet/fs/atomic.js +46 -0
  202. package/dist/wallet/fs/lock.d.ts +19 -0
  203. package/dist/wallet/fs/lock.js +61 -0
  204. package/dist/wallet/fs/status-file.d.ts +1 -0
  205. package/dist/wallet/fs/status-file.js +4 -0
  206. package/dist/wallet/lifecycle.d.ts +193 -0
  207. package/dist/wallet/lifecycle.js +1475 -0
  208. package/dist/wallet/material.d.ts +45 -0
  209. package/dist/wallet/material.js +118 -0
  210. package/dist/wallet/mining/config.d.ts +18 -0
  211. package/dist/wallet/mining/config.js +44 -0
  212. package/dist/wallet/mining/constants.d.ts +24 -0
  213. package/dist/wallet/mining/constants.js +24 -0
  214. package/dist/wallet/mining/control.d.ts +53 -0
  215. package/dist/wallet/mining/control.js +758 -0
  216. package/dist/wallet/mining/coordination.d.ts +40 -0
  217. package/dist/wallet/mining/coordination.js +121 -0
  218. package/dist/wallet/mining/hook-protocol.d.ts +47 -0
  219. package/dist/wallet/mining/hook-protocol.js +161 -0
  220. package/dist/wallet/mining/hook-runner.d.ts +1 -0
  221. package/dist/wallet/mining/hook-runner.js +52 -0
  222. package/dist/wallet/mining/hooks.d.ts +38 -0
  223. package/dist/wallet/mining/hooks.js +520 -0
  224. package/dist/wallet/mining/index.d.ts +8 -0
  225. package/dist/wallet/mining/index.js +6 -0
  226. package/dist/wallet/mining/runner.d.ts +155 -0
  227. package/dist/wallet/mining/runner.js +2574 -0
  228. package/dist/wallet/mining/runtime-artifacts.d.ts +17 -0
  229. package/dist/wallet/mining/runtime-artifacts.js +166 -0
  230. package/dist/wallet/mining/sentences.d.ts +23 -0
  231. package/dist/wallet/mining/sentences.js +281 -0
  232. package/dist/wallet/mining/state.d.ts +9 -0
  233. package/dist/wallet/mining/state.js +75 -0
  234. package/dist/wallet/mining/types.d.ts +141 -0
  235. package/dist/wallet/mining/types.js +1 -0
  236. package/dist/wallet/mining/visualizer.d.ts +19 -0
  237. package/dist/wallet/mining/visualizer.js +134 -0
  238. package/dist/wallet/mining/worker-main.d.ts +1 -0
  239. package/dist/wallet/mining/worker-main.js +17 -0
  240. package/dist/wallet/read/context.d.ts +20 -0
  241. package/dist/wallet/read/context.js +532 -0
  242. package/dist/wallet/read/filter.d.ts +9 -0
  243. package/dist/wallet/read/filter.js +42 -0
  244. package/dist/wallet/read/index.d.ts +4 -0
  245. package/dist/wallet/read/index.js +3 -0
  246. package/dist/wallet/read/project.d.ts +11 -0
  247. package/dist/wallet/read/project.js +300 -0
  248. package/dist/wallet/read/types.d.ts +144 -0
  249. package/dist/wallet/read/types.js +1 -0
  250. package/dist/wallet/runtime.d.ts +26 -0
  251. package/dist/wallet/runtime.js +28 -0
  252. package/dist/wallet/state/crypto.d.ts +31 -0
  253. package/dist/wallet/state/crypto.js +127 -0
  254. package/dist/wallet/state/provider.d.ts +37 -0
  255. package/dist/wallet/state/provider.js +312 -0
  256. package/dist/wallet/state/session.d.ts +12 -0
  257. package/dist/wallet/state/session.js +23 -0
  258. package/dist/wallet/state/storage.d.ts +19 -0
  259. package/dist/wallet/state/storage.js +55 -0
  260. package/dist/wallet/tx/anchor.d.ts +40 -0
  261. package/dist/wallet/tx/anchor.js +1210 -0
  262. package/dist/wallet/tx/cog.d.ts +92 -0
  263. package/dist/wallet/tx/cog.js +1055 -0
  264. package/dist/wallet/tx/common.d.ts +89 -0
  265. package/dist/wallet/tx/common.js +156 -0
  266. package/dist/wallet/tx/confirm.d.ts +15 -0
  267. package/dist/wallet/tx/confirm.js +24 -0
  268. package/dist/wallet/tx/domain-admin.d.ts +105 -0
  269. package/dist/wallet/tx/domain-admin.js +869 -0
  270. package/dist/wallet/tx/domain-market.d.ts +112 -0
  271. package/dist/wallet/tx/domain-market.js +1365 -0
  272. package/dist/wallet/tx/field.d.ts +101 -0
  273. package/dist/wallet/tx/field.js +1853 -0
  274. package/dist/wallet/tx/identity-selector.d.ts +12 -0
  275. package/dist/wallet/tx/identity-selector.js +52 -0
  276. package/dist/wallet/tx/index.d.ts +7 -0
  277. package/dist/wallet/tx/index.js +7 -0
  278. package/dist/wallet/tx/journal.d.ts +5 -0
  279. package/dist/wallet/tx/journal.js +31 -0
  280. package/dist/wallet/tx/register.d.ts +68 -0
  281. package/dist/wallet/tx/register.js +952 -0
  282. package/dist/wallet/tx/reputation.d.ts +72 -0
  283. package/dist/wallet/tx/reputation.js +693 -0
  284. package/dist/wallet/tx/targets.d.ts +7 -0
  285. package/dist/wallet/tx/targets.js +122 -0
  286. package/dist/wallet/types.d.ts +249 -0
  287. package/dist/wallet/types.js +1 -0
  288. package/dist/writing_quotes.json +1654 -0
  289. package/package.json +78 -0
@@ -0,0 +1,71 @@
1
+ import type { RpcBlock, RpcBlockchainInfo, RpcChainStatesResponse, RpcCreateWalletResult, RpcDecodedPsbt, RpcDescriptorInfo, RpcFinalizePsbtResult, RpcImportDescriptorRequest, RpcImportDescriptorResult, RpcListUnspentEntry, RpcMempoolEntry, RpcMempoolInfo, RpcRawMempoolVerbose, RpcListDescriptorsResult, RpcLockedUnspent, RpcLoadTxOutSetResult, RpcLoadWalletResult, RpcNetworkInfo, RpcTestMempoolAcceptResult, RpcWalletInfo, RpcWalletCreateFundedPsbtResult, RpcWalletProcessPsbtResult, RpcTransaction, RpcWalletTransaction, RpcZmqNotification } from "./types.js";
2
+ interface RpcRequestPayload {
3
+ readonly body: string;
4
+ readonly headers: Record<string, string>;
5
+ }
6
+ interface RpcResponsePayload {
7
+ readonly statusCode: number;
8
+ readonly bodyText: string;
9
+ }
10
+ interface RpcTransportOptions {
11
+ fetchImpl?: typeof fetch;
12
+ requestTimeoutMs?: number;
13
+ requestImpl?: (request: {
14
+ url: URL;
15
+ payload: RpcRequestPayload;
16
+ }) => Promise<RpcResponsePayload>;
17
+ }
18
+ export declare class BitcoinRpcClient {
19
+ #private;
20
+ constructor(url: string, cookieFile: string, options?: RpcTransportOptions);
21
+ call<T>(method: string, params?: unknown[]): Promise<T>;
22
+ callWallet<T>(walletName: string, method: string, params?: unknown[]): Promise<T>;
23
+ getBlockchainInfo(): Promise<RpcBlockchainInfo>;
24
+ getNetworkInfo(): Promise<RpcNetworkInfo>;
25
+ getBestBlockHash(): Promise<string>;
26
+ getBlockHash(height: number): Promise<string>;
27
+ getBlock(hashHex: string): Promise<RpcBlock>;
28
+ getChainStates(): Promise<RpcChainStatesResponse>;
29
+ loadTxOutSet(snapshotPath: string): Promise<RpcLoadTxOutSetResult>;
30
+ getZmqNotifications(): Promise<RpcZmqNotification[]>;
31
+ createWallet(walletName: string, options?: {
32
+ disablePrivateKeys?: boolean;
33
+ blank?: boolean;
34
+ passphrase?: string;
35
+ avoidReuse?: boolean;
36
+ descriptors?: boolean;
37
+ loadOnStartup?: boolean;
38
+ }): Promise<RpcCreateWalletResult>;
39
+ loadWallet(walletName: string, loadOnStartup?: boolean): Promise<RpcLoadWalletResult>;
40
+ unloadWallet(walletName: string, loadOnStartup?: boolean): Promise<null>;
41
+ listWallets(): Promise<string[]>;
42
+ getDescriptorInfo(descriptor: string): Promise<RpcDescriptorInfo>;
43
+ deriveAddresses(descriptor: string, range?: number | [number, number]): Promise<string[]>;
44
+ listDescriptors(walletName: string, privateOnly?: boolean): Promise<RpcListDescriptorsResult>;
45
+ importDescriptors(walletName: string, requests: RpcImportDescriptorRequest[]): Promise<RpcImportDescriptorResult[]>;
46
+ getWalletInfo(walletName: string): Promise<RpcWalletInfo>;
47
+ walletLock(walletName: string): Promise<null>;
48
+ walletPassphrase(walletName: string, passphrase: string, timeoutSeconds: number): Promise<null>;
49
+ walletProcessPsbt(walletName: string, psbt: string, sign?: boolean, sighashType?: string): Promise<RpcWalletProcessPsbtResult>;
50
+ listUnspent(walletName: string, minConf?: number): Promise<RpcListUnspentEntry[]>;
51
+ listLockUnspent(walletName: string): Promise<RpcLockedUnspent[]>;
52
+ lockUnspent(walletName: string, unlock: boolean, outputs: RpcLockedUnspent[]): Promise<boolean>;
53
+ walletCreateFundedPsbt(walletName: string, inputs: Array<{
54
+ txid: string;
55
+ vout: number;
56
+ }>, outputs: unknown[], locktime: number, options: Record<string, unknown>, bip32Derivs?: boolean): Promise<RpcWalletCreateFundedPsbtResult>;
57
+ decodePsbt(psbt: string): Promise<RpcDecodedPsbt>;
58
+ finalizePsbt(psbt: string, extract?: boolean): Promise<RpcFinalizePsbtResult>;
59
+ decodeRawTransaction(hex: string): Promise<RpcTransaction>;
60
+ testMempoolAccept(rawTransactions: string[]): Promise<RpcTestMempoolAcceptResult[]>;
61
+ sendRawTransaction(hex: string): Promise<string>;
62
+ getRawMempool(): Promise<string[]>;
63
+ getRawMempoolVerbose(): Promise<RpcRawMempoolVerbose>;
64
+ getMempoolInfo(): Promise<RpcMempoolInfo>;
65
+ getMempoolEntry(txid: string): Promise<RpcMempoolEntry>;
66
+ saveMempool(): Promise<null>;
67
+ getRawTransaction(txid: string, verbose?: boolean): Promise<RpcTransaction>;
68
+ getTransaction(walletName: string, txid: string): Promise<RpcWalletTransaction>;
69
+ stop(): Promise<string>;
70
+ }
71
+ export {};
@@ -0,0 +1,322 @@
1
+ import { request as httpRequest } from "node:http";
2
+ import { request as httpsRequest } from "node:https";
3
+ import { readFile } from "node:fs/promises";
4
+ export class BitcoinRpcClient {
5
+ #url;
6
+ #cookieFile;
7
+ #fetchImpl;
8
+ #requestTimeoutMs;
9
+ #requestImpl;
10
+ constructor(url, cookieFile, options = {}) {
11
+ this.#url = url;
12
+ this.#cookieFile = cookieFile;
13
+ this.#fetchImpl = options.fetchImpl ?? fetch;
14
+ this.#requestTimeoutMs = options.requestTimeoutMs ?? 15_000;
15
+ this.#requestImpl = options.requestImpl ?? this.#sendNodeRequest.bind(this);
16
+ }
17
+ async call(method, params = []) {
18
+ return this.#callAtUrl(this.#url, method, params);
19
+ }
20
+ async callWallet(walletName, method, params = []) {
21
+ const url = new URL(this.#url);
22
+ url.pathname = `${url.pathname.replace(/\/$/, "")}/wallet/${encodeURIComponent(walletName)}`;
23
+ return this.#callAtUrl(url.toString(), method, params);
24
+ }
25
+ async #callAtUrl(urlString, method, params = []) {
26
+ const payload = await this.#buildRequestPayload(method, params);
27
+ let response;
28
+ const abortSignal = AbortSignal.timeout(this.#requestTimeoutMs);
29
+ try {
30
+ response = await this.#fetchImpl(urlString, {
31
+ method: "POST",
32
+ headers: payload.headers,
33
+ body: payload.body,
34
+ signal: abortSignal,
35
+ });
36
+ }
37
+ catch (error) {
38
+ throw new Error(this.#describeTransportError(urlString, method, error), { cause: error });
39
+ }
40
+ return this.#parseResponse(method, response.status, await response.text());
41
+ }
42
+ async #buildRequestPayload(method, params) {
43
+ let cookie;
44
+ try {
45
+ cookie = (await readFile(this.#cookieFile, "utf8")).trim();
46
+ }
47
+ catch (error) {
48
+ throw new Error(this.#describeCookieReadError(method, error), { cause: error });
49
+ }
50
+ return {
51
+ headers: {
52
+ "content-type": "application/json",
53
+ authorization: `Basic ${Buffer.from(cookie).toString("base64")}`,
54
+ },
55
+ body: JSON.stringify({
56
+ jsonrpc: "1.0",
57
+ id: method,
58
+ method,
59
+ params,
60
+ }),
61
+ };
62
+ }
63
+ #parseResponse(method, statusCode, bodyText) {
64
+ let payload = null;
65
+ try {
66
+ payload = JSON.parse(bodyText);
67
+ }
68
+ catch (error) {
69
+ if (statusCode < 200 || statusCode >= 300) {
70
+ throw new Error(`bitcoind_rpc_http_${statusCode}`);
71
+ }
72
+ throw new Error(this.#describeInvalidResponseError(method), { cause: error });
73
+ }
74
+ if (payload?.error) {
75
+ throw new Error(`bitcoind_rpc_${method}_${payload.error.code}_${payload.error.message}`);
76
+ }
77
+ if (statusCode < 200 || statusCode >= 300) {
78
+ throw new Error(`bitcoind_rpc_http_${statusCode}`);
79
+ }
80
+ return payload.result;
81
+ }
82
+ #describeTransportError(urlString, method, error) {
83
+ const endpoint = new URL(urlString).host;
84
+ const detail = this.#extractErrorDetail(error);
85
+ if (detail === null) {
86
+ return `The managed Bitcoin RPC request to ${endpoint} for ${method} failed.`;
87
+ }
88
+ return `The managed Bitcoin RPC request to ${endpoint} for ${method} failed: ${detail}.`;
89
+ }
90
+ #describeInvalidResponseError(method) {
91
+ const endpoint = new URL(this.#url).host;
92
+ return `The managed Bitcoin RPC request to ${endpoint} for ${method} returned an invalid JSON response.`;
93
+ }
94
+ #describeCookieReadError(method, error) {
95
+ const detail = this.#extractErrorDetail(error);
96
+ if (this.#isMissingFileError(error)) {
97
+ return `The managed Bitcoin RPC cookie file is unavailable at ${this.#cookieFile} while preparing ${method}. The managed node is not running or is shutting down.`;
98
+ }
99
+ if (detail === null) {
100
+ return `The managed Bitcoin RPC cookie file could not be read at ${this.#cookieFile} while preparing ${method}.`;
101
+ }
102
+ return `The managed Bitcoin RPC cookie file could not be read at ${this.#cookieFile} while preparing ${method}: ${detail}.`;
103
+ }
104
+ #extractErrorDetail(error) {
105
+ const parts = [];
106
+ let current = error;
107
+ while (current instanceof Error) {
108
+ const message = current.message.trim();
109
+ if (message.length > 0 && message !== "fetch failed" && !parts.includes(message)) {
110
+ parts.push(message);
111
+ }
112
+ const next = current.cause;
113
+ if (next === undefined || next === current) {
114
+ break;
115
+ }
116
+ current = next;
117
+ }
118
+ return parts.length > 0 ? parts.join(" Caused by: ") : null;
119
+ }
120
+ #isMissingFileError(error) {
121
+ return error instanceof Error
122
+ && "code" in error
123
+ && error.code === "ENOENT";
124
+ }
125
+ async #callWithNodeRequest(method, params = []) {
126
+ const url = new URL(this.#url);
127
+ const payload = await this.#buildRequestPayload(method, params);
128
+ let response;
129
+ try {
130
+ response = await this.#requestImpl({ url, payload });
131
+ }
132
+ catch (error) {
133
+ throw new Error(this.#describeTransportError(this.#url, method, error), { cause: error });
134
+ }
135
+ return this.#parseResponse(method, response.statusCode, response.bodyText);
136
+ }
137
+ async #sendNodeRequest(request) {
138
+ const requester = request.url.protocol === "https:" ? httpsRequest : httpRequest;
139
+ const contentLength = Buffer.byteLength(request.payload.body);
140
+ return new Promise((resolve, reject) => {
141
+ let settled = false;
142
+ const finish = (handler) => {
143
+ if (settled) {
144
+ return;
145
+ }
146
+ settled = true;
147
+ handler();
148
+ };
149
+ const req = requester(request.url, {
150
+ method: "POST",
151
+ agent: false,
152
+ headers: {
153
+ ...request.payload.headers,
154
+ "content-length": String(contentLength),
155
+ },
156
+ }, (response) => {
157
+ const chunks = [];
158
+ response.on("data", (chunk) => {
159
+ chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
160
+ });
161
+ response.on("end", () => {
162
+ finish(() => {
163
+ resolve({
164
+ statusCode: response.statusCode ?? 0,
165
+ bodyText: Buffer.concat(chunks).toString("utf8"),
166
+ });
167
+ });
168
+ });
169
+ response.on("aborted", () => {
170
+ finish(() => {
171
+ reject(new Error("The managed Bitcoin RPC response was aborted."));
172
+ });
173
+ });
174
+ response.on("error", (error) => {
175
+ finish(() => {
176
+ reject(error);
177
+ });
178
+ });
179
+ });
180
+ req.setTimeout(this.#requestTimeoutMs);
181
+ req.on("socket", (socket) => {
182
+ socket.setTimeout(this.#requestTimeoutMs);
183
+ });
184
+ req.on("error", (error) => {
185
+ finish(() => {
186
+ reject(error);
187
+ });
188
+ });
189
+ req.end(request.payload.body);
190
+ });
191
+ }
192
+ getBlockchainInfo() {
193
+ return this.call("getblockchaininfo");
194
+ }
195
+ getNetworkInfo() {
196
+ return this.call("getnetworkinfo");
197
+ }
198
+ getBestBlockHash() {
199
+ return this.call("getbestblockhash");
200
+ }
201
+ getBlockHash(height) {
202
+ return this.call("getblockhash", [height]);
203
+ }
204
+ getBlock(hashHex) {
205
+ return this.call("getblock", [hashHex, 3]);
206
+ }
207
+ getChainStates() {
208
+ return this.call("getchainstates");
209
+ }
210
+ loadTxOutSet(snapshotPath) {
211
+ return this.#callWithNodeRequest("loadtxoutset", [snapshotPath]);
212
+ }
213
+ getZmqNotifications() {
214
+ return this.call("getzmqnotifications");
215
+ }
216
+ createWallet(walletName, options = {}) {
217
+ return this.call("createwallet", [
218
+ walletName,
219
+ options.disablePrivateKeys ?? false,
220
+ options.blank ?? true,
221
+ options.passphrase ?? "",
222
+ options.avoidReuse ?? false,
223
+ options.descriptors ?? true,
224
+ options.loadOnStartup ?? true,
225
+ ]);
226
+ }
227
+ loadWallet(walletName, loadOnStartup = true) {
228
+ return this.call("loadwallet", [walletName, loadOnStartup]);
229
+ }
230
+ unloadWallet(walletName, loadOnStartup = true) {
231
+ return this.call("unloadwallet", [walletName, loadOnStartup]);
232
+ }
233
+ listWallets() {
234
+ return this.call("listwallets");
235
+ }
236
+ getDescriptorInfo(descriptor) {
237
+ return this.call("getdescriptorinfo", [descriptor]);
238
+ }
239
+ deriveAddresses(descriptor, range) {
240
+ const params = range === undefined ? [descriptor] : [descriptor, range];
241
+ return this.call("deriveaddresses", params);
242
+ }
243
+ listDescriptors(walletName, privateOnly = false) {
244
+ return this.callWallet(walletName, "listdescriptors", [privateOnly]);
245
+ }
246
+ importDescriptors(walletName, requests) {
247
+ return this.callWallet(walletName, "importdescriptors", [requests]);
248
+ }
249
+ getWalletInfo(walletName) {
250
+ return this.callWallet(walletName, "getwalletinfo");
251
+ }
252
+ walletLock(walletName) {
253
+ return this.callWallet(walletName, "walletlock");
254
+ }
255
+ walletPassphrase(walletName, passphrase, timeoutSeconds) {
256
+ return this.callWallet(walletName, "walletpassphrase", [passphrase, timeoutSeconds]);
257
+ }
258
+ walletProcessPsbt(walletName, psbt, sign = true, sighashType = "DEFAULT") {
259
+ return this.callWallet(walletName, "walletprocesspsbt", [
260
+ psbt,
261
+ sign,
262
+ sighashType,
263
+ ]);
264
+ }
265
+ listUnspent(walletName, minConf = 1) {
266
+ return this.callWallet(walletName, "listunspent", [minConf]);
267
+ }
268
+ listLockUnspent(walletName) {
269
+ return this.callWallet(walletName, "listlockunspent");
270
+ }
271
+ lockUnspent(walletName, unlock, outputs) {
272
+ return this.callWallet(walletName, "lockunspent", [unlock, outputs]);
273
+ }
274
+ walletCreateFundedPsbt(walletName, inputs, outputs, locktime, options, bip32Derivs = true) {
275
+ return this.callWallet(walletName, "walletcreatefundedpsbt", [
276
+ inputs,
277
+ outputs,
278
+ locktime,
279
+ options,
280
+ bip32Derivs,
281
+ ]);
282
+ }
283
+ decodePsbt(psbt) {
284
+ return this.call("decodepsbt", [psbt]);
285
+ }
286
+ finalizePsbt(psbt, extract = true) {
287
+ return this.call("finalizepsbt", [psbt, extract]);
288
+ }
289
+ decodeRawTransaction(hex) {
290
+ return this.call("decoderawtransaction", [hex]);
291
+ }
292
+ testMempoolAccept(rawTransactions) {
293
+ return this.call("testmempoolaccept", [rawTransactions]);
294
+ }
295
+ sendRawTransaction(hex) {
296
+ return this.call("sendrawtransaction", [hex]);
297
+ }
298
+ getRawMempool() {
299
+ return this.call("getrawmempool");
300
+ }
301
+ getRawMempoolVerbose() {
302
+ return this.call("getrawmempool", [false, true]);
303
+ }
304
+ getMempoolInfo() {
305
+ return this.call("getmempoolinfo");
306
+ }
307
+ getMempoolEntry(txid) {
308
+ return this.call("getmempoolentry", [txid]);
309
+ }
310
+ saveMempool() {
311
+ return this.call("savemempool");
312
+ }
313
+ getRawTransaction(txid, verbose = true) {
314
+ return this.call("getrawtransaction", [txid, verbose]);
315
+ }
316
+ getTransaction(walletName, txid) {
317
+ return this.callWallet(walletName, "gettransaction", [txid, true, true]);
318
+ }
319
+ stop() {
320
+ return this.call("stop");
321
+ }
322
+ }
@@ -0,0 +1,19 @@
1
+ export declare const UNINITIALIZED_WALLET_ROOT_ID = "wallet-root-uninitialized";
2
+ export interface ManagedServicePaths {
3
+ dataRoot: string;
4
+ runtimeRoot: string;
5
+ walletRuntimeRoot: string;
6
+ bitcoindLockPath: string;
7
+ bitcoindStatusPath: string;
8
+ bitcoindPidPath: string;
9
+ bitcoindReadyPath: string;
10
+ bitcoindRuntimeConfigPath: string;
11
+ bitcoindWalletStatusPath: string;
12
+ bitcoinConfPath: string;
13
+ indexerRoot: string;
14
+ indexerServiceRoot: string;
15
+ indexerDaemonLockPath: string;
16
+ indexerDaemonStatusPath: string;
17
+ indexerDaemonSocketPath: string;
18
+ }
19
+ export declare function resolveManagedServicePaths(dataDir: string, walletRootId?: string): ManagedServicePaths;
@@ -0,0 +1,49 @@
1
+ import { createHash } from "node:crypto";
2
+ import { tmpdir } from "node:os";
3
+ import { dirname, join } from "node:path";
4
+ import { resolveCogcoinPathsForTesting, resolveDefaultBitcoindDataDirForTesting } from "../app-paths.js";
5
+ export const UNINITIALIZED_WALLET_ROOT_ID = "wallet-root-uninitialized";
6
+ function sanitizeWalletRootId(walletRootId) {
7
+ return walletRootId.replace(/[^a-zA-Z0-9._-]+/g, "-");
8
+ }
9
+ function createDataDirSuffix(dataDir) {
10
+ return createHash("sha256").update(dataDir).digest("hex").slice(0, 12);
11
+ }
12
+ function resolveIndexerDaemonSocketPath(serviceRootId) {
13
+ const socketId = createHash("sha256").update(serviceRootId).digest("hex").slice(0, 20);
14
+ if (process.platform === "win32") {
15
+ return `\\\\.\\pipe\\cogcoin-indexer-${socketId}`;
16
+ }
17
+ return join(tmpdir(), `cogcoin-indexer-${socketId}.sock`);
18
+ }
19
+ export function resolveManagedServicePaths(dataDir, walletRootId = UNINITIALIZED_WALLET_ROOT_ID) {
20
+ const normalizedWalletRootId = sanitizeWalletRootId(walletRootId);
21
+ const defaultPaths = resolveCogcoinPathsForTesting();
22
+ const defaultBitcoindDataDir = resolveDefaultBitcoindDataDirForTesting();
23
+ const useDefaultRoots = dataDir === defaultBitcoindDataDir;
24
+ const dataRoot = useDefaultRoots ? defaultPaths.dataRoot : dirname(dataDir);
25
+ const runtimeRoot = useDefaultRoots ? defaultPaths.runtimeRoot : join(dataRoot, "runtime");
26
+ const indexerRoot = useDefaultRoots ? defaultPaths.indexerRoot : join(dataRoot, "indexer");
27
+ const serviceRootId = useDefaultRoots
28
+ ? normalizedWalletRootId
29
+ : `${normalizedWalletRootId}-${createDataDirSuffix(dataDir)}`;
30
+ const walletRuntimeRoot = join(runtimeRoot, serviceRootId);
31
+ const indexerServiceRoot = join(indexerRoot, serviceRootId);
32
+ return {
33
+ dataRoot,
34
+ runtimeRoot,
35
+ walletRuntimeRoot,
36
+ bitcoindLockPath: join(walletRuntimeRoot, "bitcoind.lock"),
37
+ bitcoindStatusPath: join(walletRuntimeRoot, "bitcoind-status.json"),
38
+ bitcoindPidPath: join(walletRuntimeRoot, "bitcoind.pid"),
39
+ bitcoindReadyPath: join(walletRuntimeRoot, "bitcoind.ready"),
40
+ bitcoindRuntimeConfigPath: join(walletRuntimeRoot, "bitcoind-config.json"),
41
+ bitcoindWalletStatusPath: join(walletRuntimeRoot, "bitcoind-wallet.json"),
42
+ bitcoinConfPath: join(dataDir, "bitcoin.conf"),
43
+ indexerRoot,
44
+ indexerServiceRoot,
45
+ indexerDaemonLockPath: join(walletRuntimeRoot, "indexer-daemon.lock"),
46
+ indexerDaemonStatusPath: join(indexerServiceRoot, "status.json"),
47
+ indexerDaemonSocketPath: resolveIndexerDaemonSocketPath(serviceRootId),
48
+ };
49
+ }
@@ -0,0 +1,40 @@
1
+ import type { InternalManagedBitcoindOptions, ManagedBitcoindObservedStatus, ManagedBitcoindNodeHandle, ManagedCoreWalletReplicaStatus } from "./types.js";
2
+ interface ManagedWalletReplicaRpc {
3
+ listWallets(): Promise<string[]>;
4
+ loadWallet(walletName: string, loadOnStartup?: boolean): Promise<{
5
+ name: string;
6
+ warning: string;
7
+ }>;
8
+ createWallet(walletName: string, options: {
9
+ blank: boolean;
10
+ descriptors: boolean;
11
+ disablePrivateKeys: boolean;
12
+ loadOnStartup: boolean;
13
+ passphrase: string;
14
+ }): Promise<unknown>;
15
+ getWalletInfo(walletName: string): Promise<{
16
+ descriptors: boolean;
17
+ private_keys_enabled: boolean;
18
+ }>;
19
+ walletLock(walletName: string): Promise<null>;
20
+ }
21
+ type ManagedBitcoindServiceOptions = Pick<InternalManagedBitcoindOptions, "dataDir" | "chain" | "startHeight" | "walletRootId" | "rpcPort" | "zmqPort" | "p2pPort" | "pollIntervalMs" | "startupTimeoutMs" | "shutdownTimeoutMs" | "managedWalletPassphrase">;
22
+ export type ManagedBitcoindServiceCompatibility = "compatible" | "service-version-mismatch" | "wallet-root-mismatch" | "runtime-mismatch" | "unreachable" | "protocol-error";
23
+ export interface ManagedBitcoindServiceProbeResult {
24
+ compatibility: ManagedBitcoindServiceCompatibility;
25
+ status: ManagedBitcoindObservedStatus | null;
26
+ error: string | null;
27
+ }
28
+ export declare function createManagedWalletReplica(rpc: ManagedWalletReplicaRpc, walletRootId: string, options?: {
29
+ managedWalletPassphrase?: string;
30
+ }): Promise<ManagedCoreWalletReplicaStatus>;
31
+ export declare function probeManagedBitcoindService(options: ManagedBitcoindServiceOptions): Promise<ManagedBitcoindServiceProbeResult>;
32
+ export declare function attachOrStartManagedBitcoindService(options: ManagedBitcoindServiceOptions): Promise<ManagedBitcoindNodeHandle>;
33
+ export declare function readManagedBitcoindServiceStatusForTesting(dataDir: string, walletRootId?: string): Promise<ManagedBitcoindObservedStatus | null>;
34
+ export declare function shutdownManagedBitcoindServiceForTesting(options: {
35
+ dataDir: string;
36
+ chain?: "main" | "regtest";
37
+ walletRootId?: string;
38
+ shutdownTimeoutMs?: number;
39
+ }): Promise<void>;
40
+ export {};