@bigmi/core 0.0.1

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 (286) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/LICENSE.md +21 -0
  3. package/README.md +137 -0
  4. package/dist/cjs/actions/getBalance.d.ts +7 -0
  5. package/dist/cjs/actions/getBalance.js +11 -0
  6. package/dist/cjs/actions/getBalance.js.map +1 -0
  7. package/dist/cjs/actions/getBlock.d.ts +12 -0
  8. package/dist/cjs/actions/getBlock.js +31 -0
  9. package/dist/cjs/actions/getBlock.js.map +1 -0
  10. package/dist/cjs/actions/getBlockCount.d.ts +4 -0
  11. package/dist/cjs/actions/getBlockCount.js +11 -0
  12. package/dist/cjs/actions/getBlockCount.js.map +1 -0
  13. package/dist/cjs/actions/getBlockStats.d.ts +14 -0
  14. package/dist/cjs/actions/getBlockStats.js +27 -0
  15. package/dist/cjs/actions/getBlockStats.js.map +1 -0
  16. package/dist/cjs/actions/getUTXOTransaction.d.ts +9 -0
  17. package/dist/cjs/actions/getUTXOTransaction.js +24 -0
  18. package/dist/cjs/actions/getUTXOTransaction.js.map +1 -0
  19. package/dist/cjs/actions/sendUTXOTransaction.d.ts +8 -0
  20. package/dist/cjs/actions/sendUTXOTransaction.js +15 -0
  21. package/dist/cjs/actions/sendUTXOTransaction.js.map +1 -0
  22. package/dist/cjs/actions/signPsbt.d.ts +3 -0
  23. package/dist/cjs/actions/signPsbt.js +11 -0
  24. package/dist/cjs/actions/signPsbt.js.map +1 -0
  25. package/dist/cjs/actions/waitForTransaction.d.ts +32 -0
  26. package/dist/cjs/actions/waitForTransaction.js +185 -0
  27. package/dist/cjs/actions/waitForTransaction.js.map +1 -0
  28. package/dist/cjs/actions/watchBlockNumber.d.ts +14 -0
  29. package/dist/cjs/actions/watchBlockNumber.js +45 -0
  30. package/dist/cjs/actions/watchBlockNumber.js.map +1 -0
  31. package/dist/cjs/chains/bitcoin.d.ts +36 -0
  32. package/dist/cjs/chains/bitcoin.js +21 -0
  33. package/dist/cjs/chains/bitcoin.js.map +1 -0
  34. package/dist/cjs/clients/types.d.ts +27 -0
  35. package/dist/cjs/clients/types.js +3 -0
  36. package/dist/cjs/clients/types.js.map +1 -0
  37. package/dist/cjs/decorators/UTXOAPIActions.d.ts +6 -0
  38. package/dist/cjs/decorators/UTXOAPIActions.js +10 -0
  39. package/dist/cjs/decorators/UTXOAPIActions.js.map +1 -0
  40. package/dist/cjs/decorators/UTXOActions.d.ts +10 -0
  41. package/dist/cjs/decorators/UTXOActions.js +14 -0
  42. package/dist/cjs/decorators/UTXOActions.js.map +1 -0
  43. package/dist/cjs/index.d.ts +38 -0
  44. package/dist/cjs/index.js +58 -0
  45. package/dist/cjs/index.js.map +1 -0
  46. package/dist/cjs/package.json +1 -0
  47. package/dist/cjs/transports/ankr.d.ts +2 -0
  48. package/dist/cjs/transports/ankr.js +21 -0
  49. package/dist/cjs/transports/ankr.js.map +1 -0
  50. package/dist/cjs/transports/blockchair.d.ts +2 -0
  51. package/dist/cjs/transports/blockchair.js +24 -0
  52. package/dist/cjs/transports/blockchair.js.map +1 -0
  53. package/dist/cjs/transports/blockcypher.d.ts +2 -0
  54. package/dist/cjs/transports/blockcypher.js +21 -0
  55. package/dist/cjs/transports/blockcypher.js.map +1 -0
  56. package/dist/cjs/transports/getHttpRpcClient.d.ts +19 -0
  57. package/dist/cjs/transports/getHttpRpcClient.js +77 -0
  58. package/dist/cjs/transports/getHttpRpcClient.js.map +1 -0
  59. package/dist/cjs/transports/getRpcProviderMethods.d.ts +2 -0
  60. package/dist/cjs/transports/getRpcProviderMethods.js +22 -0
  61. package/dist/cjs/transports/getRpcProviderMethods.js.map +1 -0
  62. package/dist/cjs/transports/mempool.d.ts +2 -0
  63. package/dist/cjs/transports/mempool.js +17 -0
  64. package/dist/cjs/transports/mempool.js.map +1 -0
  65. package/dist/cjs/transports/types.d.ts +60 -0
  66. package/dist/cjs/transports/types.js +3 -0
  67. package/dist/cjs/transports/types.js.map +1 -0
  68. package/dist/cjs/transports/utxo.d.ts +6 -0
  69. package/dist/cjs/transports/utxo.js +56 -0
  70. package/dist/cjs/transports/utxo.js.map +1 -0
  71. package/dist/cjs/types/blockStats.d.ts +34 -0
  72. package/dist/cjs/types/blockStats.js +3 -0
  73. package/dist/cjs/types/blockStats.js.map +1 -0
  74. package/dist/cjs/types/transaction.d.ts +41 -0
  75. package/dist/cjs/types/transaction.js +3 -0
  76. package/dist/cjs/types/transaction.js.map +1 -0
  77. package/dist/cjs/utils/cancelTransaction.d.ts +2 -0
  78. package/dist/cjs/utils/cancelTransaction.js +51 -0
  79. package/dist/cjs/utils/cancelTransaction.js.map +1 -0
  80. package/dist/cjs/utils/getUTXOAddress.d.ts +19 -0
  81. package/dist/cjs/utils/getUTXOAddress.js +120 -0
  82. package/dist/cjs/utils/getUTXOAddress.js.map +1 -0
  83. package/dist/cjs/utils/isUTXOAddress.d.ts +2 -0
  84. package/dist/cjs/utils/isUTXOAddress.js +18 -0
  85. package/dist/cjs/utils/isUTXOAddress.js.map +1 -0
  86. package/dist/cjs/utils/modifyFee.d.ts +2 -0
  87. package/dist/cjs/utils/modifyFee.js +50 -0
  88. package/dist/cjs/utils/modifyFee.js.map +1 -0
  89. package/dist/cjs/utils/observe.d.ts +11 -0
  90. package/dist/cjs/utils/observe.js +48 -0
  91. package/dist/cjs/utils/observe.js.map +1 -0
  92. package/dist/cjs/utils/poll.d.ts +9 -0
  93. package/dist/cjs/utils/poll.js +30 -0
  94. package/dist/cjs/utils/poll.js.map +1 -0
  95. package/dist/cjs/utils/sleep.d.ts +1 -0
  96. package/dist/cjs/utils/sleep.js +9 -0
  97. package/dist/cjs/utils/sleep.js.map +1 -0
  98. package/dist/esm/actions/getBalance.d.ts +8 -0
  99. package/dist/esm/actions/getBalance.js +8 -0
  100. package/dist/esm/actions/getBalance.js.map +1 -0
  101. package/dist/esm/actions/getBlock.d.ts +12 -0
  102. package/dist/esm/actions/getBlock.js +28 -0
  103. package/dist/esm/actions/getBlock.js.map +1 -0
  104. package/dist/esm/actions/getBlockCount.d.ts +4 -0
  105. package/dist/esm/actions/getBlockCount.js +8 -0
  106. package/dist/esm/actions/getBlockCount.js.map +1 -0
  107. package/dist/esm/actions/getBlockStats.d.ts +14 -0
  108. package/dist/esm/actions/getBlockStats.js +24 -0
  109. package/dist/esm/actions/getBlockStats.js.map +1 -0
  110. package/dist/esm/actions/getUTXOTransaction.d.ts +11 -0
  111. package/dist/esm/actions/getUTXOTransaction.js +21 -0
  112. package/dist/esm/actions/getUTXOTransaction.js.map +1 -0
  113. package/dist/esm/actions/sendUTXOTransaction.d.ts +10 -0
  114. package/dist/esm/actions/sendUTXOTransaction.js +12 -0
  115. package/dist/esm/actions/sendUTXOTransaction.js.map +1 -0
  116. package/dist/esm/actions/signPsbt.d.ts +3 -0
  117. package/dist/esm/actions/signPsbt.js +8 -0
  118. package/dist/esm/actions/signPsbt.js.map +1 -0
  119. package/dist/esm/actions/waitForTransaction.d.ts +75 -0
  120. package/dist/esm/actions/waitForTransaction.js +230 -0
  121. package/dist/esm/actions/waitForTransaction.js.map +1 -0
  122. package/dist/esm/actions/watchBlockNumber.d.ts +25 -0
  123. package/dist/esm/actions/watchBlockNumber.js +58 -0
  124. package/dist/esm/actions/watchBlockNumber.js.map +1 -0
  125. package/dist/esm/chains/bitcoin.d.ts +36 -0
  126. package/dist/esm/chains/bitcoin.js +18 -0
  127. package/dist/esm/chains/bitcoin.js.map +1 -0
  128. package/dist/esm/clients/types.d.ts +43 -0
  129. package/dist/esm/clients/types.js +2 -0
  130. package/dist/esm/clients/types.js.map +1 -0
  131. package/dist/esm/decorators/UTXOAPIActions.d.ts +6 -0
  132. package/dist/esm/decorators/UTXOAPIActions.js +7 -0
  133. package/dist/esm/decorators/UTXOAPIActions.js.map +1 -0
  134. package/dist/esm/decorators/UTXOActions.d.ts +10 -0
  135. package/dist/esm/decorators/UTXOActions.js +11 -0
  136. package/dist/esm/decorators/UTXOActions.js.map +1 -0
  137. package/dist/esm/index.d.ts +38 -0
  138. package/dist/esm/index.js +31 -0
  139. package/dist/esm/index.js.map +1 -0
  140. package/dist/esm/package.json +1 -0
  141. package/dist/esm/transports/ankr.d.ts +2 -0
  142. package/dist/esm/transports/ankr.js +18 -0
  143. package/dist/esm/transports/ankr.js.map +1 -0
  144. package/dist/esm/transports/blockchair.d.ts +2 -0
  145. package/dist/esm/transports/blockchair.js +21 -0
  146. package/dist/esm/transports/blockchair.js.map +1 -0
  147. package/dist/esm/transports/blockcypher.d.ts +2 -0
  148. package/dist/esm/transports/blockcypher.js +18 -0
  149. package/dist/esm/transports/blockcypher.js.map +1 -0
  150. package/dist/esm/transports/getHttpRpcClient.d.ts +24 -0
  151. package/dist/esm/transports/getHttpRpcClient.js +74 -0
  152. package/dist/esm/transports/getHttpRpcClient.js.map +1 -0
  153. package/dist/esm/transports/getRpcProviderMethods.d.ts +2 -0
  154. package/dist/esm/transports/getRpcProviderMethods.js +19 -0
  155. package/dist/esm/transports/getRpcProviderMethods.js.map +1 -0
  156. package/dist/esm/transports/mempool.d.ts +2 -0
  157. package/dist/esm/transports/mempool.js +14 -0
  158. package/dist/esm/transports/mempool.js.map +1 -0
  159. package/dist/esm/transports/types.d.ts +60 -0
  160. package/dist/esm/transports/types.js +2 -0
  161. package/dist/esm/transports/types.js.map +1 -0
  162. package/dist/esm/transports/utxo.d.ts +6 -0
  163. package/dist/esm/transports/utxo.js +53 -0
  164. package/dist/esm/transports/utxo.js.map +1 -0
  165. package/dist/esm/types/blockStats.d.ts +34 -0
  166. package/dist/esm/types/blockStats.js +2 -0
  167. package/dist/esm/types/blockStats.js.map +1 -0
  168. package/dist/esm/types/transaction.d.ts +41 -0
  169. package/dist/esm/types/transaction.js +2 -0
  170. package/dist/esm/types/transaction.js.map +1 -0
  171. package/dist/esm/utils/cancelTransaction.d.ts +2 -0
  172. package/dist/esm/utils/cancelTransaction.js +55 -0
  173. package/dist/esm/utils/cancelTransaction.js.map +1 -0
  174. package/dist/esm/utils/getUTXOAddress.d.ts +19 -0
  175. package/dist/esm/utils/getUTXOAddress.js +116 -0
  176. package/dist/esm/utils/getUTXOAddress.js.map +1 -0
  177. package/dist/esm/utils/isUTXOAddress.d.ts +2 -0
  178. package/dist/esm/utils/isUTXOAddress.js +14 -0
  179. package/dist/esm/utils/isUTXOAddress.js.map +1 -0
  180. package/dist/esm/utils/modifyFee.d.ts +2 -0
  181. package/dist/esm/utils/modifyFee.js +53 -0
  182. package/dist/esm/utils/modifyFee.js.map +1 -0
  183. package/dist/esm/utils/observe.d.ts +18 -0
  184. package/dist/esm/utils/observe.js +51 -0
  185. package/dist/esm/utils/observe.js.map +1 -0
  186. package/dist/esm/utils/poll.d.ts +12 -0
  187. package/dist/esm/utils/poll.js +30 -0
  188. package/dist/esm/utils/poll.js.map +1 -0
  189. package/dist/esm/utils/sleep.d.ts +1 -0
  190. package/dist/esm/utils/sleep.js +6 -0
  191. package/dist/esm/utils/sleep.js.map +1 -0
  192. package/dist/types/actions/getBalance.d.ts +9 -0
  193. package/dist/types/actions/getBalance.d.ts.map +1 -0
  194. package/dist/types/actions/getBlock.d.ts +13 -0
  195. package/dist/types/actions/getBlock.d.ts.map +1 -0
  196. package/dist/types/actions/getBlockCount.d.ts +5 -0
  197. package/dist/types/actions/getBlockCount.d.ts.map +1 -0
  198. package/dist/types/actions/getBlockStats.d.ts +15 -0
  199. package/dist/types/actions/getBlockStats.d.ts.map +1 -0
  200. package/dist/types/actions/getUTXOTransaction.d.ts +12 -0
  201. package/dist/types/actions/getUTXOTransaction.d.ts.map +1 -0
  202. package/dist/types/actions/sendUTXOTransaction.d.ts +11 -0
  203. package/dist/types/actions/sendUTXOTransaction.d.ts.map +1 -0
  204. package/dist/types/actions/signPsbt.d.ts +4 -0
  205. package/dist/types/actions/signPsbt.d.ts.map +1 -0
  206. package/dist/types/actions/waitForTransaction.d.ts +76 -0
  207. package/dist/types/actions/waitForTransaction.d.ts.map +1 -0
  208. package/dist/types/actions/watchBlockNumber.d.ts +26 -0
  209. package/dist/types/actions/watchBlockNumber.d.ts.map +1 -0
  210. package/dist/types/chains/bitcoin.d.ts +37 -0
  211. package/dist/types/chains/bitcoin.d.ts.map +1 -0
  212. package/dist/types/clients/types.d.ts +44 -0
  213. package/dist/types/clients/types.d.ts.map +1 -0
  214. package/dist/types/decorators/UTXOAPIActions.d.ts +7 -0
  215. package/dist/types/decorators/UTXOAPIActions.d.ts.map +1 -0
  216. package/dist/types/decorators/UTXOActions.d.ts +11 -0
  217. package/dist/types/decorators/UTXOActions.d.ts.map +1 -0
  218. package/dist/types/index.d.ts +39 -0
  219. package/dist/types/index.d.ts.map +1 -0
  220. package/dist/types/transports/ankr.d.ts +3 -0
  221. package/dist/types/transports/ankr.d.ts.map +1 -0
  222. package/dist/types/transports/blockchair.d.ts +3 -0
  223. package/dist/types/transports/blockchair.d.ts.map +1 -0
  224. package/dist/types/transports/blockcypher.d.ts +3 -0
  225. package/dist/types/transports/blockcypher.d.ts.map +1 -0
  226. package/dist/types/transports/getHttpRpcClient.d.ts +25 -0
  227. package/dist/types/transports/getHttpRpcClient.d.ts.map +1 -0
  228. package/dist/types/transports/getRpcProviderMethods.d.ts +3 -0
  229. package/dist/types/transports/getRpcProviderMethods.d.ts.map +1 -0
  230. package/dist/types/transports/mempool.d.ts +3 -0
  231. package/dist/types/transports/mempool.d.ts.map +1 -0
  232. package/dist/types/transports/types.d.ts +61 -0
  233. package/dist/types/transports/types.d.ts.map +1 -0
  234. package/dist/types/transports/utxo.d.ts +7 -0
  235. package/dist/types/transports/utxo.d.ts.map +1 -0
  236. package/dist/types/types/blockStats.d.ts +35 -0
  237. package/dist/types/types/blockStats.d.ts.map +1 -0
  238. package/dist/types/types/transaction.d.ts +42 -0
  239. package/dist/types/types/transaction.d.ts.map +1 -0
  240. package/dist/types/utils/cancelTransaction.d.ts +3 -0
  241. package/dist/types/utils/cancelTransaction.d.ts.map +1 -0
  242. package/dist/types/utils/getUTXOAddress.d.ts +20 -0
  243. package/dist/types/utils/getUTXOAddress.d.ts.map +1 -0
  244. package/dist/types/utils/isUTXOAddress.d.ts +3 -0
  245. package/dist/types/utils/isUTXOAddress.d.ts.map +1 -0
  246. package/dist/types/utils/modifyFee.d.ts +3 -0
  247. package/dist/types/utils/modifyFee.d.ts.map +1 -0
  248. package/dist/types/utils/observe.d.ts +19 -0
  249. package/dist/types/utils/observe.d.ts.map +1 -0
  250. package/dist/types/utils/poll.d.ts +13 -0
  251. package/dist/types/utils/poll.d.ts.map +1 -0
  252. package/dist/types/utils/sleep.d.ts +2 -0
  253. package/dist/types/utils/sleep.d.ts.map +1 -0
  254. package/package.json +50 -0
  255. package/src/actions/getBalance.ts +26 -0
  256. package/src/actions/getBlock.ts +58 -0
  257. package/src/actions/getBlockCount.ts +20 -0
  258. package/src/actions/getBlockStats.ts +55 -0
  259. package/src/actions/getUTXOTransaction.ts +43 -0
  260. package/src/actions/sendUTXOTransaction.ts +29 -0
  261. package/src/actions/signPsbt.ts +23 -0
  262. package/src/actions/waitForTransaction.ts +387 -0
  263. package/src/actions/watchBlockNumber.ts +105 -0
  264. package/src/chains/bitcoin.ts +18 -0
  265. package/src/clients/types.ts +48 -0
  266. package/src/decorators/UTXOAPIActions.ts +20 -0
  267. package/src/decorators/UTXOActions.ts +37 -0
  268. package/src/index.ts +97 -0
  269. package/src/transports/ankr.ts +30 -0
  270. package/src/transports/blockchair.ts +27 -0
  271. package/src/transports/blockcypher.ts +32 -0
  272. package/src/transports/getHttpRpcClient.ts +122 -0
  273. package/src/transports/getRpcProviderMethods.ts +22 -0
  274. package/src/transports/mempool.ts +34 -0
  275. package/src/transports/types.ts +71 -0
  276. package/src/transports/utxo.ts +84 -0
  277. package/src/types/blockStats.ts +35 -0
  278. package/src/types/transaction.ts +43 -0
  279. package/src/utils/cancelTransaction.ts +75 -0
  280. package/src/utils/getUTXOAddress.ts +148 -0
  281. package/src/utils/isUTXOAddress.ts +18 -0
  282. package/src/utils/modifyFee.ts +78 -0
  283. package/src/utils/observe.ts +81 -0
  284. package/src/utils/poll.ts +48 -0
  285. package/src/utils/sleep.ts +5 -0
  286. package/tsconfig.json +10 -0
@@ -0,0 +1,148 @@
1
+ import { sha256 } from '@noble/hashes/sha256'
2
+ import { type Decoded, bech32, bech32m } from 'bech32'
3
+ import bs58 from 'bs58'
4
+
5
+ export enum UTXONetwork {
6
+ Mainnet = 'mainnet',
7
+ Testnet = 'testnet',
8
+ Regtest = 'regtest',
9
+ }
10
+
11
+ export enum UTXOAddressType {
12
+ p2pkh = 'p2pkh',
13
+ p2sh = 'p2sh',
14
+ p2wpkh = 'p2wpkh',
15
+ p2wsh = 'p2wsh',
16
+ p2tr = 'p2tr',
17
+ }
18
+
19
+ export type UTXOAddress = {
20
+ bech32: boolean
21
+ network: UTXONetwork
22
+ address: string
23
+ type: UTXOAddressType
24
+ }
25
+
26
+ const addressTypes: {
27
+ [key: number]: { type: UTXOAddressType; network: UTXONetwork }
28
+ } = {
29
+ 0: {
30
+ type: UTXOAddressType.p2pkh,
31
+ network: UTXONetwork.Mainnet,
32
+ },
33
+ 111: {
34
+ type: UTXOAddressType.p2pkh,
35
+ network: UTXONetwork.Testnet,
36
+ },
37
+ 5: {
38
+ type: UTXOAddressType.p2sh,
39
+ network: UTXONetwork.Mainnet,
40
+ },
41
+ 196: {
42
+ type: UTXOAddressType.p2sh,
43
+ network: UTXONetwork.Testnet,
44
+ },
45
+ }
46
+
47
+ const parseBech32 = (address: string): UTXOAddress => {
48
+ let decoded: Decoded
49
+
50
+ try {
51
+ if (
52
+ address.startsWith('bc1p') ||
53
+ address.startsWith('tb1p') ||
54
+ address.startsWith('bcrt1p')
55
+ ) {
56
+ decoded = bech32m.decode(address)
57
+ } else {
58
+ decoded = bech32.decode(address)
59
+ }
60
+ } catch (_error) {
61
+ throw new Error('Invalid address')
62
+ }
63
+
64
+ const mapPrefixToNetwork: { [key: string]: UTXONetwork } = {
65
+ bc: UTXONetwork.Mainnet,
66
+ tb: UTXONetwork.Testnet,
67
+ bcrt: UTXONetwork.Regtest,
68
+ }
69
+
70
+ const network: UTXONetwork = mapPrefixToNetwork[decoded.prefix]
71
+
72
+ if (network === undefined) {
73
+ throw new Error('Invalid address')
74
+ }
75
+
76
+ const witnessVersion = decoded.words[0]
77
+
78
+ if (witnessVersion < 0 || witnessVersion > 16) {
79
+ throw new Error('Invalid address')
80
+ }
81
+ const data = bech32.fromWords(decoded.words.slice(1))
82
+
83
+ let type: UTXOAddressType
84
+ if (data.length === 20) {
85
+ type = UTXOAddressType.p2wpkh
86
+ } else if (witnessVersion === 1) {
87
+ type = UTXOAddressType.p2tr
88
+ } else {
89
+ type = UTXOAddressType.p2wsh
90
+ }
91
+
92
+ return {
93
+ bech32: true,
94
+ network,
95
+ address,
96
+ type,
97
+ }
98
+ }
99
+
100
+ export const getUTXOAddress = (address: string): UTXOAddress => {
101
+ let decoded: Uint8Array
102
+ const prefix = address.substring(0, 2).toLowerCase()
103
+
104
+ if (prefix === 'bc' || prefix === 'tb') {
105
+ return parseBech32(address)
106
+ }
107
+
108
+ try {
109
+ decoded = bs58.decode(address)
110
+ } catch (_error) {
111
+ throw new Error('Invalid address')
112
+ }
113
+
114
+ const { length } = decoded
115
+
116
+ if (length !== 25) {
117
+ throw new Error('Invalid address')
118
+ }
119
+
120
+ const version = decoded[0]
121
+
122
+ const checksum = decoded.slice(length - 4, length)
123
+ const body = decoded.slice(0, length - 4)
124
+
125
+ const expectedChecksum = sha256(sha256(body)).slice(0, 4)
126
+
127
+ if (
128
+ checksum.some(
129
+ (value: number, index: number) => value !== expectedChecksum[index]
130
+ )
131
+ ) {
132
+ throw new Error('Invalid address')
133
+ }
134
+
135
+ const validVersions = Object.keys(addressTypes).map(Number)
136
+
137
+ if (!validVersions.includes(version)) {
138
+ throw new Error('Invalid address')
139
+ }
140
+
141
+ const addressType = addressTypes[version]
142
+
143
+ return {
144
+ ...addressType,
145
+ address,
146
+ bech32: false,
147
+ }
148
+ }
@@ -0,0 +1,18 @@
1
+ import { type UTXONetwork, getUTXOAddress } from './getUTXOAddress.js'
2
+
3
+ export const isUTXOAddress = (
4
+ address: string,
5
+ network?: UTXONetwork
6
+ ): boolean => {
7
+ try {
8
+ const utxoAddress = getUTXOAddress(address)
9
+
10
+ if (network) {
11
+ return network === utxoAddress.network
12
+ }
13
+
14
+ return true
15
+ } catch (_error) {
16
+ return false
17
+ }
18
+ }
@@ -0,0 +1,78 @@
1
+ import { Psbt, address } from 'bitcoinjs-lib'
2
+
3
+ interface InputData {
4
+ hash: Uint8Array
5
+ index: number
6
+ nonWitnessUtxo?: Uint8Array
7
+ witnessUtxo?: {
8
+ script: Uint8Array
9
+ value: bigint
10
+ }
11
+ redeemScript?: Uint8Array
12
+ witnessScript?: Uint8Array
13
+ }
14
+
15
+ export function modifyFee(psbt: Psbt, newFee: bigint, accountAddress: string) {
16
+ const newPsbt = new Psbt()
17
+ const inputs = psbt.data.inputs
18
+ const outputs = psbt.txOutputs
19
+
20
+ // Add inputs to the new PSBT
21
+ for (let i = 0; i < inputs.length; i++) {
22
+ const input = inputs[i]
23
+ const inputData: InputData = {
24
+ hash: psbt.txInputs[i].hash,
25
+ index: psbt.txInputs[i].index,
26
+ }
27
+
28
+ // Include UTXO information
29
+ if (input.nonWitnessUtxo) {
30
+ inputData.nonWitnessUtxo = input.nonWitnessUtxo
31
+ } else if (input.witnessUtxo) {
32
+ inputData.witnessUtxo = input.witnessUtxo
33
+ }
34
+
35
+ // Include scripts if necessary
36
+ if (input.redeemScript) {
37
+ inputData.redeemScript = input.redeemScript
38
+ }
39
+ if (input.witnessScript) {
40
+ inputData.witnessScript = input.witnessScript
41
+ }
42
+
43
+ newPsbt.addInput(inputData)
44
+ }
45
+
46
+ const changeOutputScript = address.toOutputScript(accountAddress)
47
+
48
+ // Add outputs to the new PSBT
49
+ for (const output of outputs) {
50
+ const outputData = {
51
+ script: output.script,
52
+ value: output.value,
53
+ }
54
+
55
+ const scriptsAreEqual =
56
+ output.script.length === changeOutputScript.length &&
57
+ output.script.every((value, index) => value === changeOutputScript[index])
58
+
59
+ if (scriptsAreEqual) {
60
+ outputData.value = output.value - newFee
61
+ if (outputData.value < 0) {
62
+ throw new Error(
63
+ 'Insufficient funds to adjust the fee by the specified amount.'
64
+ )
65
+ }
66
+ }
67
+
68
+ newPsbt.addOutput(outputData)
69
+ }
70
+
71
+ // Modify the input sequence number to enable RBF
72
+ newPsbt.txInputs.forEach((_, index) => {
73
+ // Set sequence number to less than 0xfffffffe, e.g., 0xfffffffd
74
+ newPsbt.setInputSequence(index, 0xfffffffd)
75
+ })
76
+
77
+ return newPsbt
78
+ }
@@ -0,0 +1,81 @@
1
+ import type { MaybePromise } from 'viem'
2
+
3
+ type Callback = ((...args: any[]) => any) | undefined
4
+ type Callbacks = Record<string, Callback>
5
+
6
+ /** @internal */
7
+ export const listenersCache = /*#__PURE__*/ new Map<
8
+ string,
9
+ { id: number; fns: Callbacks }[]
10
+ >()
11
+ /** @internal */
12
+ export const cleanupCache = /*#__PURE__*/ new Map<string, () => void>()
13
+
14
+ type EmitFunction<callbacks extends Callbacks> = (
15
+ emit: callbacks
16
+ ) => MaybePromise<void | (() => void)>
17
+
18
+ let callbackCount = 0
19
+
20
+ /**
21
+ * @description Sets up an observer for a given function. If another function
22
+ * is set up under the same observer id, the function will only be called once
23
+ * for both instances of the observer.
24
+ */
25
+ export function observe<callbacks extends Callbacks>(
26
+ observerId: string,
27
+ callbacks: callbacks,
28
+ fn: EmitFunction<callbacks>
29
+ ) {
30
+ const callbackId = ++callbackCount
31
+
32
+ const getListeners = () => listenersCache.get(observerId) || []
33
+
34
+ const unsubscribe = () => {
35
+ const listeners = getListeners()
36
+ listenersCache.set(
37
+ observerId,
38
+ listeners.filter((cb: any) => cb.id !== callbackId)
39
+ )
40
+ }
41
+
42
+ const unwatch = () => {
43
+ const cleanup = cleanupCache.get(observerId)
44
+ if (getListeners().length === 1 && cleanup) {
45
+ cleanup()
46
+ }
47
+ unsubscribe()
48
+ }
49
+
50
+ const listeners = getListeners()
51
+ listenersCache.set(observerId, [
52
+ ...listeners,
53
+ { id: callbackId, fns: callbacks },
54
+ ])
55
+
56
+ if (listeners && listeners.length > 0) {
57
+ return unwatch
58
+ }
59
+
60
+ const emit: callbacks = {} as callbacks
61
+ for (const key in callbacks) {
62
+ emit[key] = ((
63
+ ...args: Parameters<NonNullable<callbacks[keyof callbacks]>>
64
+ ) => {
65
+ const listeners = getListeners()
66
+ if (listeners.length === 0) {
67
+ return
68
+ }
69
+ for (const listener of listeners) {
70
+ listener.fns[key]?.(...args)
71
+ }
72
+ }) as callbacks[Extract<keyof callbacks, string>]
73
+ }
74
+
75
+ const cleanup = fn(emit)
76
+ if (typeof cleanup === 'function') {
77
+ cleanupCache.set(observerId, cleanup)
78
+ }
79
+
80
+ return unwatch
81
+ }
@@ -0,0 +1,48 @@
1
+ import { sleep } from './sleep.js'
2
+
3
+ type PollOptions<T> = {
4
+ // Whether or not to emit when the polling starts.
5
+ emitOnBegin?: boolean | undefined
6
+ // The initial wait time (in ms) before polling.
7
+ initialWaitTime?: ((data: T | undefined) => Promise<number>) | undefined
8
+ // The interval (in ms).
9
+ interval: number
10
+ }
11
+
12
+ /**
13
+ * @description Polls a function at a specified interval.
14
+ */
15
+ export function poll<T>(
16
+ fn: ({ unpoll }: { unpoll: () => void }) => Promise<T | undefined>,
17
+ { emitOnBegin, initialWaitTime, interval }: PollOptions<T>
18
+ ) {
19
+ let active = true
20
+
21
+ const unwatch = () => {
22
+ active = false
23
+ }
24
+
25
+ const watch = async () => {
26
+ let data: T | undefined = undefined
27
+ if (emitOnBegin) {
28
+ data = await fn({ unpoll: unwatch })
29
+ }
30
+
31
+ const initialWait = (await initialWaitTime?.(data)) ?? interval
32
+ await sleep(initialWait)
33
+
34
+ const poll = async () => {
35
+ if (!active) {
36
+ return
37
+ }
38
+ await fn({ unpoll: unwatch })
39
+ await sleep(interval)
40
+ poll()
41
+ }
42
+
43
+ poll()
44
+ }
45
+ watch()
46
+
47
+ return unwatch
48
+ }
@@ -0,0 +1,5 @@
1
+ export function sleep(ms: number): Promise<null> {
2
+ return new Promise((resolve) => {
3
+ setTimeout(() => resolve(null), ms)
4
+ })
5
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "composite": true,
5
+ "sourceMap": true,
6
+ "rootDir": "./src",
7
+ "moduleResolution": "Node"
8
+ },
9
+ "include": ["./src/**/*", "./src/**/*.json"]
10
+ }