@bitgen/sdk 0.1.6 → 1.0.4

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 (338) hide show
  1. package/CHANGELOG.md +63 -2
  2. package/README.md +27 -106
  3. package/dist/cjs/client.d.ts +20 -8
  4. package/dist/cjs/client.d.ts.map +1 -1
  5. package/dist/cjs/client.js +33 -12
  6. package/dist/cjs/client.js.map +1 -1
  7. package/dist/cjs/constants.d.ts +256 -0
  8. package/dist/cjs/constants.d.ts.map +1 -0
  9. package/dist/cjs/constants.js +214 -0
  10. package/dist/cjs/constants.js.map +1 -0
  11. package/dist/cjs/error.d.ts +17 -10
  12. package/dist/cjs/error.d.ts.map +1 -1
  13. package/dist/cjs/error.js +25 -7
  14. package/dist/cjs/error.js.map +1 -1
  15. package/dist/cjs/http.d.ts +13 -8
  16. package/dist/cjs/http.d.ts.map +1 -1
  17. package/dist/cjs/http.js +135 -44
  18. package/dist/cjs/http.js.map +1 -1
  19. package/dist/cjs/index.d.ts +14 -1
  20. package/dist/cjs/index.d.ts.map +1 -1
  21. package/dist/cjs/index.js +30 -1
  22. package/dist/cjs/index.js.map +1 -1
  23. package/dist/cjs/resources/apikeys.d.ts +18 -0
  24. package/dist/cjs/resources/apikeys.d.ts.map +1 -0
  25. package/dist/cjs/resources/apikeys.js +31 -0
  26. package/dist/cjs/resources/apikeys.js.map +1 -0
  27. package/dist/cjs/resources/asset.d.ts +17 -5
  28. package/dist/cjs/resources/asset.d.ts.map +1 -1
  29. package/dist/cjs/resources/asset.js +17 -6
  30. package/dist/cjs/resources/asset.js.map +1 -1
  31. package/dist/cjs/resources/bank.d.ts +11 -6
  32. package/dist/cjs/resources/bank.d.ts.map +1 -1
  33. package/dist/cjs/resources/bank.js +17 -9
  34. package/dist/cjs/resources/bank.js.map +1 -1
  35. package/dist/cjs/resources/core.d.ts +12 -0
  36. package/dist/cjs/resources/core.d.ts.map +1 -0
  37. package/dist/cjs/resources/core.js +22 -0
  38. package/dist/cjs/resources/core.js.map +1 -0
  39. package/dist/cjs/resources/custody.d.ts +27 -0
  40. package/dist/cjs/resources/custody.d.ts.map +1 -0
  41. package/dist/cjs/resources/custody.js +38 -0
  42. package/dist/cjs/resources/custody.js.map +1 -0
  43. package/dist/cjs/resources/customer.d.ts +20 -0
  44. package/dist/cjs/resources/customer.d.ts.map +1 -0
  45. package/dist/cjs/resources/customer.js +38 -0
  46. package/dist/cjs/resources/customer.js.map +1 -0
  47. package/dist/cjs/resources/staking.d.ts +32 -0
  48. package/dist/cjs/resources/staking.d.ts.map +1 -0
  49. package/dist/cjs/resources/staking.js +69 -0
  50. package/dist/cjs/resources/staking.js.map +1 -0
  51. package/dist/cjs/resources/trading.d.ts +24 -0
  52. package/dist/cjs/resources/trading.d.ts.map +1 -0
  53. package/dist/cjs/resources/trading.js +41 -0
  54. package/dist/cjs/resources/trading.js.map +1 -0
  55. package/dist/cjs/resources/transaction.d.ts +6 -9
  56. package/dist/cjs/resources/transaction.d.ts.map +1 -1
  57. package/dist/cjs/resources/transaction.js +10 -15
  58. package/dist/cjs/resources/transaction.js.map +1 -1
  59. package/dist/cjs/resources/webhooks.d.ts +46 -0
  60. package/dist/cjs/resources/webhooks.d.ts.map +1 -0
  61. package/dist/cjs/resources/webhooks.js +142 -0
  62. package/dist/cjs/resources/webhooks.js.map +1 -0
  63. package/dist/cjs/types/apikeys.d.ts +43 -0
  64. package/dist/cjs/types/apikeys.d.ts.map +1 -0
  65. package/dist/cjs/types/apikeys.js +3 -0
  66. package/dist/cjs/types/apikeys.js.map +1 -0
  67. package/dist/cjs/types/asset.d.ts +57 -0
  68. package/dist/cjs/types/asset.d.ts.map +1 -0
  69. package/dist/cjs/types/asset.js +3 -0
  70. package/dist/cjs/types/asset.js.map +1 -0
  71. package/dist/cjs/types/bank.d.ts +58 -0
  72. package/dist/cjs/types/bank.d.ts.map +1 -0
  73. package/dist/cjs/types/bank.js +3 -0
  74. package/dist/cjs/types/bank.js.map +1 -0
  75. package/dist/cjs/types/core.d.ts +32 -0
  76. package/dist/cjs/types/core.d.ts.map +1 -0
  77. package/dist/cjs/types/core.js +3 -0
  78. package/dist/cjs/types/core.js.map +1 -0
  79. package/dist/cjs/types/custody.d.ts +50 -0
  80. package/dist/cjs/types/custody.d.ts.map +1 -0
  81. package/dist/cjs/types/custody.js +3 -0
  82. package/dist/cjs/types/custody.js.map +1 -0
  83. package/dist/cjs/types/customer.d.ts +190 -0
  84. package/dist/cjs/types/customer.d.ts.map +1 -0
  85. package/dist/cjs/types/customer.js +3 -0
  86. package/dist/cjs/types/customer.js.map +1 -0
  87. package/dist/cjs/types/index.d.ts +78 -194
  88. package/dist/cjs/types/index.d.ts.map +1 -1
  89. package/dist/cjs/types/index.js +0 -1
  90. package/dist/cjs/types/index.js.map +1 -1
  91. package/dist/cjs/types/staking.d.ts +88 -0
  92. package/dist/cjs/types/staking.d.ts.map +1 -0
  93. package/dist/cjs/types/staking.js +3 -0
  94. package/dist/cjs/types/staking.js.map +1 -0
  95. package/dist/cjs/types/trading.d.ts +51 -0
  96. package/dist/cjs/types/trading.d.ts.map +1 -0
  97. package/dist/cjs/types/trading.js +3 -0
  98. package/dist/cjs/types/trading.js.map +1 -0
  99. package/dist/cjs/types/transaction.d.ts +61 -0
  100. package/dist/cjs/types/transaction.d.ts.map +1 -0
  101. package/dist/cjs/types/transaction.js +3 -0
  102. package/dist/cjs/types/transaction.js.map +1 -0
  103. package/dist/cjs/types/webhooks.d.ts +55 -0
  104. package/dist/cjs/types/webhooks.d.ts.map +1 -0
  105. package/dist/cjs/types/webhooks.js +3 -0
  106. package/dist/cjs/types/webhooks.js.map +1 -0
  107. package/dist/cjs/utils.d.ts +21 -2
  108. package/dist/cjs/utils.d.ts.map +1 -1
  109. package/dist/cjs/utils.js +67 -4
  110. package/dist/cjs/utils.js.map +1 -1
  111. package/dist/cjs/version.d.ts +2 -0
  112. package/dist/cjs/version.d.ts.map +1 -0
  113. package/dist/cjs/version.js +6 -0
  114. package/dist/cjs/version.js.map +1 -0
  115. package/dist/esm/client.d.ts +20 -8
  116. package/dist/esm/client.d.ts.map +1 -1
  117. package/dist/esm/client.js +33 -12
  118. package/dist/esm/client.js.map +1 -1
  119. package/dist/esm/constants.d.ts +256 -0
  120. package/dist/esm/constants.d.ts.map +1 -0
  121. package/dist/esm/constants.js +211 -0
  122. package/dist/esm/constants.js.map +1 -0
  123. package/dist/esm/error.d.ts +17 -10
  124. package/dist/esm/error.d.ts.map +1 -1
  125. package/dist/esm/error.js +24 -6
  126. package/dist/esm/error.js.map +1 -1
  127. package/dist/esm/http.d.ts +13 -8
  128. package/dist/esm/http.d.ts.map +1 -1
  129. package/dist/esm/http.js +136 -45
  130. package/dist/esm/http.js.map +1 -1
  131. package/dist/esm/index.d.ts +14 -1
  132. package/dist/esm/index.d.ts.map +1 -1
  133. package/dist/esm/index.js +6 -0
  134. package/dist/esm/index.js.map +1 -1
  135. package/dist/esm/resources/apikeys.d.ts +18 -0
  136. package/dist/esm/resources/apikeys.d.ts.map +1 -0
  137. package/dist/esm/resources/apikeys.js +27 -0
  138. package/dist/esm/resources/apikeys.js.map +1 -0
  139. package/dist/esm/resources/asset.d.ts +17 -5
  140. package/dist/esm/resources/asset.d.ts.map +1 -1
  141. package/dist/esm/resources/asset.js +17 -6
  142. package/dist/esm/resources/asset.js.map +1 -1
  143. package/dist/esm/resources/bank.d.ts +11 -6
  144. package/dist/esm/resources/bank.d.ts.map +1 -1
  145. package/dist/esm/resources/bank.js +18 -10
  146. package/dist/esm/resources/bank.js.map +1 -1
  147. package/dist/esm/resources/core.d.ts +12 -0
  148. package/dist/esm/resources/core.d.ts.map +1 -0
  149. package/dist/esm/resources/core.js +18 -0
  150. package/dist/esm/resources/core.js.map +1 -0
  151. package/dist/esm/resources/custody.d.ts +27 -0
  152. package/dist/esm/resources/custody.d.ts.map +1 -0
  153. package/dist/esm/resources/custody.js +34 -0
  154. package/dist/esm/resources/custody.js.map +1 -0
  155. package/dist/esm/resources/customer.d.ts +20 -0
  156. package/dist/esm/resources/customer.d.ts.map +1 -0
  157. package/dist/esm/resources/customer.js +34 -0
  158. package/dist/esm/resources/customer.js.map +1 -0
  159. package/dist/esm/resources/staking.d.ts +32 -0
  160. package/dist/esm/resources/staking.d.ts.map +1 -0
  161. package/dist/esm/resources/staking.js +65 -0
  162. package/dist/esm/resources/staking.js.map +1 -0
  163. package/dist/esm/resources/trading.d.ts +24 -0
  164. package/dist/esm/resources/trading.d.ts.map +1 -0
  165. package/dist/esm/resources/trading.js +37 -0
  166. package/dist/esm/resources/trading.js.map +1 -0
  167. package/dist/esm/resources/transaction.d.ts +6 -9
  168. package/dist/esm/resources/transaction.d.ts.map +1 -1
  169. package/dist/esm/resources/transaction.js +11 -16
  170. package/dist/esm/resources/transaction.js.map +1 -1
  171. package/dist/esm/resources/webhooks.d.ts +46 -0
  172. package/dist/esm/resources/webhooks.d.ts.map +1 -0
  173. package/dist/esm/resources/webhooks.js +138 -0
  174. package/dist/esm/resources/webhooks.js.map +1 -0
  175. package/dist/esm/types/apikeys.d.ts +43 -0
  176. package/dist/esm/types/apikeys.d.ts.map +1 -0
  177. package/dist/esm/types/apikeys.js +2 -0
  178. package/dist/esm/types/apikeys.js.map +1 -0
  179. package/dist/esm/types/asset.d.ts +57 -0
  180. package/dist/esm/types/asset.d.ts.map +1 -0
  181. package/dist/esm/types/asset.js +2 -0
  182. package/dist/esm/types/asset.js.map +1 -0
  183. package/dist/esm/types/bank.d.ts +58 -0
  184. package/dist/esm/types/bank.d.ts.map +1 -0
  185. package/dist/esm/types/bank.js +2 -0
  186. package/dist/esm/types/bank.js.map +1 -0
  187. package/dist/esm/types/core.d.ts +32 -0
  188. package/dist/esm/types/core.d.ts.map +1 -0
  189. package/dist/esm/types/core.js +2 -0
  190. package/dist/esm/types/core.js.map +1 -0
  191. package/dist/esm/types/custody.d.ts +50 -0
  192. package/dist/esm/types/custody.d.ts.map +1 -0
  193. package/dist/esm/types/custody.js +2 -0
  194. package/dist/esm/types/custody.js.map +1 -0
  195. package/dist/esm/types/customer.d.ts +190 -0
  196. package/dist/esm/types/customer.d.ts.map +1 -0
  197. package/dist/esm/types/customer.js +2 -0
  198. package/dist/esm/types/customer.js.map +1 -0
  199. package/dist/esm/types/index.d.ts +78 -194
  200. package/dist/esm/types/index.d.ts.map +1 -1
  201. package/dist/esm/types/index.js +0 -1
  202. package/dist/esm/types/index.js.map +1 -1
  203. package/dist/esm/types/staking.d.ts +88 -0
  204. package/dist/esm/types/staking.d.ts.map +1 -0
  205. package/dist/esm/types/staking.js +2 -0
  206. package/dist/esm/types/staking.js.map +1 -0
  207. package/dist/esm/types/trading.d.ts +51 -0
  208. package/dist/esm/types/trading.d.ts.map +1 -0
  209. package/dist/esm/types/trading.js +2 -0
  210. package/dist/esm/types/trading.js.map +1 -0
  211. package/dist/esm/types/transaction.d.ts +61 -0
  212. package/dist/esm/types/transaction.d.ts.map +1 -0
  213. package/dist/esm/types/transaction.js +2 -0
  214. package/dist/esm/types/transaction.js.map +1 -0
  215. package/dist/esm/types/webhooks.d.ts +55 -0
  216. package/dist/esm/types/webhooks.d.ts.map +1 -0
  217. package/dist/esm/types/webhooks.js +2 -0
  218. package/dist/esm/types/webhooks.js.map +1 -0
  219. package/dist/esm/utils.d.ts +21 -2
  220. package/dist/esm/utils.d.ts.map +1 -1
  221. package/dist/esm/utils.js +62 -4
  222. package/dist/esm/utils.js.map +1 -1
  223. package/dist/esm/version.d.ts +2 -0
  224. package/dist/esm/version.d.ts.map +1 -0
  225. package/dist/esm/version.js +3 -0
  226. package/dist/esm/version.js.map +1 -0
  227. package/package.json +29 -12
  228. package/readme/concepts.md +128 -0
  229. package/readme/configuration.md +72 -0
  230. package/readme/errors.md +67 -0
  231. package/readme/installation.md +43 -0
  232. package/readme/media/activation.svg +58 -0
  233. package/readme/media/deposit-flow.svg +70 -0
  234. package/readme/media/order-buy.svg +43 -0
  235. package/readme/media/order-sell.svg +47 -0
  236. package/readme/media/purchase-flow.svg +61 -0
  237. package/readme/media/sale-flow.svg +61 -0
  238. package/readme/media/staking-position.svg +75 -0
  239. package/readme/media/transaction-lifecycle.svg +61 -0
  240. package/readme/media/webhook-delivery.svg +53 -0
  241. package/readme/media/withdrawal-flow.svg +64 -0
  242. package/readme/quick-start.md +87 -0
  243. package/readme/resource/apikeys.md +103 -0
  244. package/readme/resource/asset.md +121 -0
  245. package/readme/resource/bank.md +161 -0
  246. package/readme/resource/core.md +91 -0
  247. package/readme/resource/custody.md +166 -0
  248. package/readme/resource/customer.md +210 -0
  249. package/readme/resource/staking.md +281 -0
  250. package/readme/resource/trading.md +171 -0
  251. package/readme/resource/transaction.md +110 -0
  252. package/readme/resource/webhooks.md +309 -0
  253. package/src/client.ts +46 -0
  254. package/src/constants.ts +281 -0
  255. package/src/error.ts +36 -0
  256. package/src/http.ts +201 -0
  257. package/src/index.ts +111 -0
  258. package/src/resources/apikeys.ts +30 -0
  259. package/src/resources/asset.ts +28 -0
  260. package/src/resources/bank.ts +39 -0
  261. package/src/resources/core.ts +19 -0
  262. package/src/resources/custody.ts +37 -0
  263. package/src/resources/customer.ts +46 -0
  264. package/src/resources/staking.ts +88 -0
  265. package/src/resources/trading.ts +41 -0
  266. package/src/resources/transaction.ts +20 -0
  267. package/src/resources/webhooks.ts +169 -0
  268. package/src/types/apikeys.ts +35 -0
  269. package/src/types/asset.ts +59 -0
  270. package/src/types/bank.ts +60 -0
  271. package/src/types/core.ts +32 -0
  272. package/src/types/custody.ts +40 -0
  273. package/src/types/customer.ts +189 -0
  274. package/src/types/index.ts +215 -0
  275. package/src/types/staking.ts +87 -0
  276. package/src/types/trading.ts +56 -0
  277. package/src/types/transaction.ts +62 -0
  278. package/src/types/webhooks.ts +60 -0
  279. package/src/utils.ts +74 -0
  280. package/src/version.ts +2 -0
  281. package/dist/cjs/resources/order.d.ts +0 -17
  282. package/dist/cjs/resources/order.d.ts.map +0 -1
  283. package/dist/cjs/resources/order.js +0 -30
  284. package/dist/cjs/resources/order.js.map +0 -1
  285. package/dist/cjs/resources/stats.d.ts +0 -11
  286. package/dist/cjs/resources/stats.d.ts.map +0 -1
  287. package/dist/cjs/resources/stats.js +0 -18
  288. package/dist/cjs/resources/stats.js.map +0 -1
  289. package/dist/cjs/resources/user.d.ts +0 -22
  290. package/dist/cjs/resources/user.d.ts.map +0 -1
  291. package/dist/cjs/resources/user.js +0 -37
  292. package/dist/cjs/resources/user.js.map +0 -1
  293. package/dist/cjs/resources/wallet.d.ts +0 -11
  294. package/dist/cjs/resources/wallet.d.ts.map +0 -1
  295. package/dist/cjs/resources/wallet.js +0 -19
  296. package/dist/cjs/resources/wallet.js.map +0 -1
  297. package/dist/esm/resources/order.d.ts +0 -17
  298. package/dist/esm/resources/order.d.ts.map +0 -1
  299. package/dist/esm/resources/order.js +0 -26
  300. package/dist/esm/resources/order.js.map +0 -1
  301. package/dist/esm/resources/stats.d.ts +0 -11
  302. package/dist/esm/resources/stats.d.ts.map +0 -1
  303. package/dist/esm/resources/stats.js +0 -14
  304. package/dist/esm/resources/stats.js.map +0 -1
  305. package/dist/esm/resources/user.d.ts +0 -22
  306. package/dist/esm/resources/user.d.ts.map +0 -1
  307. package/dist/esm/resources/user.js +0 -33
  308. package/dist/esm/resources/user.js.map +0 -1
  309. package/dist/esm/resources/wallet.d.ts +0 -11
  310. package/dist/esm/resources/wallet.d.ts.map +0 -1
  311. package/dist/esm/resources/wallet.js +0 -15
  312. package/dist/esm/resources/wallet.js.map +0 -1
  313. package/dist/types/client.d.ts +0 -19
  314. package/dist/types/client.d.ts.map +0 -1
  315. package/dist/types/error.d.ts +0 -15
  316. package/dist/types/error.d.ts.map +0 -1
  317. package/dist/types/http.d.ts +0 -21
  318. package/dist/types/http.d.ts.map +0 -1
  319. package/dist/types/index.d.ts +0 -4
  320. package/dist/types/index.d.ts.map +0 -1
  321. package/dist/types/resources/asset.d.ts +0 -11
  322. package/dist/types/resources/asset.d.ts.map +0 -1
  323. package/dist/types/resources/bank.d.ts +0 -13
  324. package/dist/types/resources/bank.d.ts.map +0 -1
  325. package/dist/types/resources/order.d.ts +0 -17
  326. package/dist/types/resources/order.d.ts.map +0 -1
  327. package/dist/types/resources/stats.d.ts +0 -11
  328. package/dist/types/resources/stats.d.ts.map +0 -1
  329. package/dist/types/resources/transaction.d.ts +0 -15
  330. package/dist/types/resources/transaction.d.ts.map +0 -1
  331. package/dist/types/resources/user.d.ts +0 -22
  332. package/dist/types/resources/user.d.ts.map +0 -1
  333. package/dist/types/resources/wallet.d.ts +0 -11
  334. package/dist/types/resources/wallet.d.ts.map +0 -1
  335. package/dist/types/types/index.d.ts +0 -213
  336. package/dist/types/types/index.d.ts.map +0 -1
  337. package/dist/types/utils.d.ts +0 -7
  338. package/dist/types/utils.d.ts.map +0 -1
@@ -0,0 +1,309 @@
1
+ # Webhooks
2
+
3
+ BITGEN pushes the events of your organization — a customer created, an identity validated, a deposit credited, a withdrawal sent, an order executed, a compliance alert… — to an HTTPS endpoint of yours, each delivery signed with a secret shared with you. `client.webhooks` sets up that endpoint and its secret, subscribes your organization to the events of the catalogue, reads the delivery logs, and verifies the deliveries your endpoint receives with `verify`.
4
+
5
+ Examples use `client`, a configured `BitgenClient` ([Configuration](../configuration.md)). Wherever the API expects your organization, the SDK sends the `scope` of the client.
6
+
7
+ ![Receiving a delivery: the POST, its verification, the 2xx answer and the retries](../media/webhook-delivery.svg)
8
+
9
+ ## Methods
10
+
11
+ | Method | What it does | Returns |
12
+ |---|---|---|
13
+ | `activate(params)` | Activates the webhooks of your organization: sets the endpoint, generates the secret | `void` |
14
+ | `updateEndpoint(params)` | Changes the endpoint | `void` |
15
+ | `regenerate()` | Generates a new secret | `void` |
16
+ | `list(params?)` | Reads the secret, the endpoint and the subscriptions of your organization | `WebhookSubscriptions` |
17
+ | `subscribe(event)` | Subscribes your organization to an event of the catalogue | `Created` (the subscription) |
18
+ | `archive(subscriber)` | Archives a subscription: the event is no longer delivered | `void` |
19
+ | `reactivate(subscriber)` | Reactivates an archived subscription | `void` |
20
+ | `logs(subscriber, params?)` | Lists the delivery attempts of a subscription | `Page<DeliveryLog>` |
21
+ | `catalog()` | Lists the events of the catalogue | `Page<WebhookType>` |
22
+ | `catalogItem(webhook)` | Reads one event of the catalogue | `WebhookType` |
23
+ | `verify(input)` | Verifies a delivery received by your endpoint and returns its envelope | `WebhookEvent` |
24
+
25
+ TypeScript types of this resource, exported by the package: `WebhookType`, `Subscriber`, `WebhookSubscriptions`, `DeliveryLog`, `WebhookEvent`, `VerifyInput` — the constants `WebhookEventName`, `SubscriberState` (also types) — plus the shared `Created`, `PageParams`.
26
+
27
+ ## Activate
28
+
29
+ ```
30
+ client.webhooks.activate(params: { endpoint: string }): Promise<void>
31
+ ```
32
+
33
+ | Parameter | Type | Description |
34
+ |---|---|---|
35
+ | `params.endpoint` | `string` | The URL BITGEN will deliver the events to — `https://` is added when the scheme is missing; `http://` is refused outside a local environment |
36
+
37
+ ```ts
38
+ await client.webhooks.activate({ endpoint: 'https://example.com/bitgen' })
39
+
40
+ const { secret } = await client.webhooks.list() // the initial secret is read with list(): keep it server-side for `verify`
41
+ ```
42
+
43
+ Activation generates the secret of your organization. `activate` does not return it: the API returns the current secret in `list()` (`secret` field, next to `endpoint` and the subscriptions). Once active, a second activation answers `429 webhook_security_already_enabled`. The API answers with an empty body: the promise resolves with `undefined`.
44
+
45
+ ## UpdateEndpoint
46
+
47
+ ```
48
+ client.webhooks.updateEndpoint(params: { endpoint: string }): Promise<void>
49
+ ```
50
+
51
+ | Parameter | Type | Description |
52
+ |---|---|---|
53
+ | `params.endpoint` | `string` | The new URL — same rules as `activate` |
54
+
55
+ ```ts
56
+ await client.webhooks.updateEndpoint({ endpoint: 'https://example.com/bitgen/v2' })
57
+ ```
58
+
59
+ Before activation, the API answers `404 unknown_webhook_security`. The promise resolves with `undefined`.
60
+
61
+ ## Regenerate
62
+
63
+ ```
64
+ client.webhooks.regenerate(): Promise<void>
65
+ ```
66
+
67
+ ```ts
68
+ await client.webhooks.regenerate() // 1. a new secret is created — it is not returned
69
+
70
+ const { secret } = await client.webhooks.list() // 2. the new secret — configure your receiving endpoint with it, the previous one stops validating immediately
71
+ ```
72
+
73
+ `regenerate()` creates a new secret but does not return it: the API returns the current secret in `list()` (`secret` field, next to `endpoint` and the subscriptions). The deliveries are signed with the new one from then on, the previous one stops validating immediately. Before activation, the API answers `404 unknown_webhook_security`. The promise resolves with `undefined`.
74
+
75
+ ## List
76
+
77
+ ```
78
+ client.webhooks.list(params?: { includeArchived?: boolean }): Promise<WebhookSubscriptions>
79
+ ```
80
+
81
+ | Parameter | Type | Description |
82
+ |---|---|---|
83
+ | `params.includeArchived` | `boolean` | Also returns the `SubscriberState.ARCHIVED` subscriptions — default `false` ([Query booleans](../concepts.md#query-booleans)) |
84
+
85
+ ```ts
86
+ import { SubscriberState } from '@bitgen/sdk'
87
+
88
+ const { secret, endpoint, items } = await client.webhooks.list({ includeArchived: true })
89
+
90
+ for (const subscription of items) {
91
+ console.log(subscription.webhook.name, subscription.state === SubscriberState.ENABLED) // 'custody.sent' true
92
+ }
93
+ ```
94
+
95
+ Returns a `WebhookSubscriptions`:
96
+
97
+ | Field | Description |
98
+ |---|---|
99
+ | `secret` | The secret of your organization, for `verify` |
100
+ | `endpoint` | The URL the events are delivered to |
101
+ | `items` | The subscriptions (`Subscriber`): `uuid`, `state` (`SubscriberState.ENABLED` or `SubscriberState.ARCHIVED`), `updatedAt` (epoch seconds), `webhook` (the event of the catalogue: `uuid`, `state` (a `SubscriberState` too), `name`, `label` — its display names, a raw JSON string `{"fr": "…", "en": "…"}` — and `data`, internal) |
102
+
103
+ Before activation, the API answers `404 unknown_webhook_security`.
104
+
105
+ ## Subscribe
106
+
107
+ ```
108
+ client.webhooks.subscribe(event: string | WebhookType): Promise<Created>
109
+ ```
110
+
111
+ | Parameter | Type | Description |
112
+ |---|---|---|
113
+ | `event` | `string` or `WebhookType` | The event, by name (`WebhookEventName.CUSTODY_SENT`), by uuid of the catalogue, or by the `WebhookType` of `catalog()` |
114
+
115
+ ```ts
116
+ import { WebhookEventName } from '@bitgen/sdk'
117
+
118
+ const { uuid } = await client.webhooks.subscribe(WebhookEventName.CUSTODY_SENT)
119
+ ```
120
+
121
+ Returns a `Created` — the uuid of the subscription, for `archive`, `reactivate` and `logs`. A subscription that already exists answers `409 webhook_subscription_already_exists`; an unknown event, `404 unknown_webhook`. The events of the catalogue:
122
+
123
+ | Family | Events |
124
+ |---|---|
125
+ | Customers | `user.created`, `user.identity.started`, `user.identity.pending`, `user.identity.step.validated`, `user.identity.step.rejected`, `user.identity.step.requested`, `user.identity.validated`, `user.identity.renew`, `user.identity.request` |
126
+ | Organizations | `organization.created`, `organization.identity.started`, `organization.identity.pending`, `organization.identity.step.validated`, `organization.identity.step.rejected`, `organization.identity.step.requested`, `organization.identity.validated`, `organization.identity.renew`, `organization.identity.request` |
127
+ | Bank | `bank.credited`, `bank.debited`, `bank.transaction` |
128
+ | Custody | `custody.transaction`, `custody.wallet.created`, `custody.sent`, `custody.received` |
129
+ | Trading | `trading.buy`, `trading.sell` |
130
+ | Staking | `staking.requested`, `staking.status`, `staking.rewards`, `staking.claimed` |
131
+ | Compliance | `alert.opened`, `alert.status` |
132
+
133
+ The `WebhookEventName` constant carries these names — `WebhookEventName.CUSTODY_SENT` for `custody.sent`, `USER_IDENTITY_STEP_VALIDATED` for `user.identity.step.validated`… — and as a type it also accepts any other string, since the catalogue may grow ([Constants](../concepts.md#constants)).
134
+
135
+ ## Archive
136
+
137
+ ```
138
+ client.webhooks.archive(subscriber: string | Subscriber): Promise<void>
139
+ ```
140
+
141
+ `subscriber` is the uuid returned by `subscribe`, or a `Subscriber` of `list()`.
142
+
143
+ ```ts
144
+ await client.webhooks.archive('SUBSCRIBER_UUID')
145
+ ```
146
+
147
+ The subscription becomes `SubscriberState.ARCHIVED`: the event is no longer delivered. An unknown subscription answers `404 unknown_webhook_subscriber`. The promise resolves with `undefined`.
148
+
149
+ ## Reactivate
150
+
151
+ ```
152
+ client.webhooks.reactivate(subscriber: string | Subscriber): Promise<void>
153
+ ```
154
+
155
+ `subscriber` is the uuid returned by `subscribe`, or a `Subscriber` of `list({ includeArchived: true })`.
156
+
157
+ ```ts
158
+ await client.webhooks.reactivate('SUBSCRIBER_UUID')
159
+ ```
160
+
161
+ The subscription becomes `SubscriberState.ENABLED` again. An unknown subscription answers `404 unknown_webhook_subscriber`. The promise resolves with `undefined`.
162
+
163
+ ## Logs
164
+
165
+ ```
166
+ client.webhooks.logs(subscriber: string | Subscriber, params?: PageParams): Promise<Page<DeliveryLog>>
167
+ ```
168
+
169
+ `subscriber` is the uuid returned by `subscribe`, or a `Subscriber` of `list()`.
170
+
171
+ | Parameter | Type | Description |
172
+ |---|---|---|
173
+ | `params.offset`, `params.limit` | `number` | [Pagination](../concepts.md#pagination) |
174
+
175
+ ```ts
176
+ const { count, items } = await client.webhooks.logs('SUBSCRIBER_UUID', { offset: 0, limit: 50 })
177
+
178
+ for (const delivery of items) {
179
+ console.log(delivery.date, delivery.http_code, delivery.attempts) // 1701000000 204 1
180
+ }
181
+ ```
182
+
183
+ Returns a page of `DeliveryLog`: `date` (epoch seconds), `webhook` (the event name), `url` (the endpoint called), `status` (`SENT` or `FAILED` for that attempt), `http_code` (the status your endpoint answered, or `null`), `duration_ms` (or `null`), `attempts` (attempt number), `payload` (the delivered body), `error` (failure reason, `null` on success).
184
+
185
+ ## Catalog
186
+
187
+ ```
188
+ client.webhooks.catalog(): Promise<Page<WebhookType>>
189
+ ```
190
+
191
+ ```ts
192
+ import { SubscriberState } from '@bitgen/sdk'
193
+
194
+ const { items } = await client.webhooks.catalog()
195
+
196
+ for (const type of items) {
197
+ console.log(type.name, type.state === SubscriberState.ENABLED) // 'custody.sent' true
198
+ }
199
+ ```
200
+
201
+ Returns every event of the catalogue, `SubscriberState.ARCHIVED` ones included, as `WebhookType`: `uuid`, `state` (`SubscriberState.ENABLED` or `SubscriberState.ARCHIVED`), `name`, `label` (its display names, a raw JSON string `{"fr": "…", "en": "…"}`), `data` (internal, a raw JSON string).
202
+
203
+ ## CatalogItem
204
+
205
+ ```
206
+ client.webhooks.catalogItem(webhook: string | WebhookType): Promise<WebhookType>
207
+ ```
208
+
209
+ `webhook` is the uuid of the event, or a `WebhookType` of `catalog()`.
210
+
211
+ ```ts
212
+ const type = await client.webhooks.catalogItem('WEBHOOK_UUID')
213
+ ```
214
+
215
+ Returns one `WebhookType`; an unknown uuid answers `404 unknown_webhook`.
216
+
217
+ ## Verify
218
+
219
+ ```
220
+ client.webhooks.verify(input: VerifyInput): Promise<WebhookEvent>
221
+ ```
222
+
223
+ No request: the verification runs locally, with the secret of your organization.
224
+
225
+ Each delivery is a `POST` to your endpoint, with `Content-Type: application/json`, a JSON body `{ delivery_id, timestamp, event, data }` and two headers:
226
+
227
+ | Header | Content |
228
+ |---|---|
229
+ | `X-BITGEN-Timestamp` | The time of the delivery, epoch seconds |
230
+ | `X-BITGEN-Signature` | `sha256=<hex>`, where `hex = HMAC_SHA256(secret, "<timestamp>.<rawBody>")` |
231
+
232
+ Answer with a 2xx status: a delivery that does not get one is retried 4 times (after 60 s, 5 min, 15 min and 1 h).
233
+
234
+ | Parameter | Type | Description |
235
+ |---|---|---|
236
+ | `input.rawBody` | `string` or `Uint8Array` | The body exactly as received — the bytes, never a re-serialized JSON |
237
+ | `input.headers` | `Record<string, string \| string[] \| undefined>` | The headers as received (Node's `IncomingHttpHeaders`); names are matched case-insensitively |
238
+ | `input.secret` | `string` | The secret of your organization (`list().secret`) |
239
+ | `input.tolerance` | `number` | Optional: the maximum distance, in seconds, between now and the timestamp of the delivery — `300` by default, `0` disables the check |
240
+
241
+ ```ts
242
+ import { createServer } from 'node:http'
243
+ import { BitgenClient, BitgenError, WebhookEventName } from '@bitgen/sdk'
244
+
245
+ const client = new BitgenClient({ scope: 'YOUR_SCOPE_UUID', apiKey: 'YOUR_API_KEY' })
246
+ const { secret } = await client.webhooks.list()
247
+
248
+ createServer((req, res) => {
249
+ const chunks: Buffer[] = []
250
+ req.on('data', (chunk: Buffer) => chunks.push(chunk))
251
+ req.on('end', async () => {
252
+ try {
253
+ const event = await client.webhooks.verify({ rawBody: Buffer.concat(chunks), headers: req.headers, secret })
254
+ if (event.event === WebhookEventName.CUSTODY_SENT) {
255
+ // event.data is `unknown`: its shape depends on the event
256
+ }
257
+ res.writeHead(204).end()
258
+ } catch (err) {
259
+ res.writeHead(err instanceof BitgenError ? 400 : 500).end()
260
+ }
261
+ })
262
+ }).listen(8080)
263
+ ```
264
+
265
+ `verify` recomputes the signature on the raw bytes, compares it with `X-BITGEN-Signature` in constant time, checks `X-BITGEN-Timestamp` against `tolerance`, and only then parses the body. The SDK is stateless: a delivery may be replayed within the freshness window and verify again — an endpoint that wants to ignore a replay deduplicates on `delivery_id` on its side. It returns the envelope, a `WebhookEvent`:
266
+
267
+ | Field | Description |
268
+ |---|---|
269
+ | `delivery_id` | The delivery |
270
+ | `timestamp` | The time of the delivery, epoch seconds |
271
+ | `event` | The event name (`WebhookEventName`) — compare it with the constant |
272
+ | `data` | The payload of the event — typed `unknown`: its shape depends on the event |
273
+
274
+ A delivery that fails the verification rejects the promise with a `BitgenError` whose `status` is `0` and whose `code` is one of:
275
+
276
+ | `code` | Meaning |
277
+ |---|---|
278
+ | `missing_signature` | No `X-BITGEN-Signature` header |
279
+ | `invalid_signature` | The signature does not match the body and the secret |
280
+ | `missing_timestamp` | No `X-BITGEN-Timestamp` header, or not a number |
281
+ | `timestamp_expired` | The timestamp of the delivery is more than `tolerance` seconds away from now |
282
+ | `invalid_payload` | The body is not the JSON envelope |
283
+
284
+ An `input` that is not usable — a `rawBody` that is neither a string nor bytes, `headers` that are not an object, an empty `secret`, a `tolerance` that is not a number of seconds — rejects with a `TypeError` ([Rejected promises](../errors.md#rejected-promises)).
285
+
286
+ ## Errors
287
+
288
+ In addition to the [common errors](../errors.md#common-errors):
289
+
290
+ | Status | `code` | Meaning |
291
+ |---|---|---|
292
+ | `0` | `missing_signature`, `invalid_signature`, `missing_timestamp`, `timestamp_expired`, `invalid_payload` | `verify`: the delivery fails the verification ([verify](#verify)) |
293
+ | `400` | `webhook_security_https_required` | `activate`, `updateEndpoint`: an `http://` endpoint outside a local environment |
294
+ | `404` | `unknown_webhook_security` | The webhooks of your organization are not activated |
295
+ | `404` | `unknown_webhook` | Unknown event |
296
+ | `404` | `unknown_webhook_subscriber` | Unknown subscription |
297
+ | `409` | `webhook_subscription_already_exists` | `subscribe`: your organization is already subscribed to this event |
298
+ | `412` | `organization_not_enabled` | Your organization is closed |
299
+ | `422` | `invalid_include_archived` | `includeArchived` is not a boolean value |
300
+ | `429` | `webhook_security_already_enabled` | `activate`: the webhooks are already active |
301
+
302
+ ## Related
303
+
304
+ - [Errors](../errors.md) — `BitgenError`, `status: 0`
305
+ - [Customers](customer.md) — `user.created`, `user.identity.*`
306
+ - [Bank accounts](bank.md) — `bank.credited`, `bank.debited`, `bank.transaction`
307
+ - [Custody wallets](custody.md) — `custody.wallet.created`, `custody.received`, `custody.sent`, `custody.transaction`
308
+ - [Trading](trading.md) — `trading.buy`, `trading.sell`
309
+ - [Staking](staking.md) — `staking.requested`, `staking.status`, `staking.rewards`, `staking.claimed`
package/src/client.ts ADDED
@@ -0,0 +1,46 @@
1
+ import { HttpClient } from './http.js'
2
+ import { ApikeysResource } from './resources/apikeys.js'
3
+ import { AssetResource } from './resources/asset.js'
4
+ import { BankResource } from './resources/bank.js'
5
+ import { CoreResource } from './resources/core.js'
6
+ import { CustodyResource } from './resources/custody.js'
7
+ import { CustomerResource } from './resources/customer.js'
8
+ import { StakingResource } from './resources/staking.js'
9
+ import { TradingResource } from './resources/trading.js'
10
+ import { TransactionResource } from './resources/transaction.js'
11
+ import { WebhooksResource } from './resources/webhooks.js'
12
+ import type { BitgenConfig } from './types/index.js'
13
+
14
+ /**
15
+ * Entry point of the SDK: one instance per API key, resources hang off it (`client.asset`…).
16
+ * Invalid config throws a `TypeError` here, before any request is sent.
17
+ */
18
+ export class BitgenClient {
19
+ protected readonly http: HttpClient
20
+
21
+ readonly asset: AssetResource
22
+ readonly customer: CustomerResource
23
+ readonly bank: BankResource
24
+ readonly custody: CustodyResource
25
+ readonly trading: TradingResource
26
+ readonly transaction: TransactionResource
27
+ readonly core: CoreResource
28
+ readonly staking: StakingResource
29
+ readonly webhooks: WebhooksResource
30
+ readonly apikeys: ApikeysResource
31
+
32
+ constructor(config: BitgenConfig) {
33
+ this.http = new HttpClient(config)
34
+
35
+ this.asset = new AssetResource(this.http)
36
+ this.customer = new CustomerResource(this.http)
37
+ this.bank = new BankResource(this.http)
38
+ this.custody = new CustodyResource(this.http)
39
+ this.trading = new TradingResource(this.http)
40
+ this.transaction = new TransactionResource(this.http)
41
+ this.core = new CoreResource(this.http)
42
+ this.staking = new StakingResource(this.http, this.core)
43
+ this.webhooks = new WebhooksResource(this.http)
44
+ this.apikeys = new ApikeysResource(this.http)
45
+ }
46
+ }
@@ -0,0 +1,281 @@
1
+ /** Target environment — value goes in `BitgenConfig.env` (`env: Env.PRODUCTION`) */
2
+ export const Env = Object.freeze({
3
+ PRODUCTION: 'production',
4
+ SANDBOX: 'sandbox',
5
+ STAGING: 'staging',
6
+ LOCALHOST: 'localhost',
7
+ } as const)
8
+
9
+ /**
10
+ * Asset ISO codes, accepted wherever an asset is expected (the API takes a uuid or an iso in any case).
11
+ * The `iso` returned by the API has its stored case (`ETH` today): compare it case-insensitively.
12
+ * Provisional list — extended once the production list is confirmed.
13
+ */
14
+ export const Asset = Object.freeze({
15
+ BTC: 'btc',
16
+ ETH: 'eth',
17
+ USDC: 'usdc',
18
+ XRP: 'xrp',
19
+ SOL: 'sol',
20
+ } as const)
21
+
22
+ // ─── Enumerated values (mirror of the API's; value and type share the name, like `Env` and `Asset`) ─────
23
+
24
+ /** States of an API key — `Apikey.state`; `client.apikeys.list` leaves the `REVOKED` ones out unless `includeRevoked` */
25
+ export const ApikeyState = Object.freeze({
26
+ ENABLED: 'ENABLED',
27
+ REVOKED: 'REVOKED',
28
+ } as const)
29
+ /** The values of `ApikeyState` */
30
+ export type ApikeyState = (typeof ApikeyState)[keyof typeof ApikeyState]
31
+
32
+ /** States of an asset — `Asset.state` is a string, compare it with `AssetState.AVAILABLE` */
33
+ export const AssetState = Object.freeze({
34
+ AVAILABLE: 'AVAILABLE',
35
+ UNAVAILABLE: 'UNAVAILABLE',
36
+ ARCHIVED: 'ARCHIVED',
37
+ HIDDEN: 'HIDDEN',
38
+ } as const)
39
+ /** The values of `AssetState` */
40
+ export type AssetState = (typeof AssetState)[keyof typeof AssetState]
41
+
42
+ /** Filter of the EUR operations (`client.bank.operations`) — `ALL` (default), or one kind */
43
+ export const BankDirection = Object.freeze({
44
+ ALL: 'ALL',
45
+ DEPOSIT: 'DEPOSIT',
46
+ WITHDRAWAL: 'WITHDRAWAL',
47
+ PURCHASE: 'PURCHASE',
48
+ SELL: 'SELL',
49
+ } as const)
50
+ /** The values of `BankDirection` */
51
+ export type BankDirection = (typeof BankDirection)[keyof typeof BankDirection]
52
+
53
+ /** States of a connector — `Core.state`; also the `state` filter of `client.core.list` */
54
+ export const CoreState = Object.freeze({
55
+ ENABLED: 'ENABLED',
56
+ DISABLED: 'DISABLED',
57
+ } as const)
58
+ /** The values of `CoreState` */
59
+ export type CoreState = (typeof CoreState)[keyof typeof CoreState]
60
+
61
+ /** Kinds of connectors — `Core.type`; also the `type` filter of `client.core.list` */
62
+ export const CoreType = Object.freeze({
63
+ /** Identity verification service */
64
+ IDENTITY: 'IDENTITY',
65
+ /** Anti-money-laundering service */
66
+ AML: 'AML',
67
+ /** Exchange */
68
+ TRADING: 'TRADING',
69
+ /** Custodian */
70
+ CUSTODY: 'CUSTODY',
71
+ /** Staking provider */
72
+ STAKING: 'STAKING',
73
+ /** Bank */
74
+ RAMP: 'RAMP',
75
+ } as const)
76
+ /** The values of `CoreType` */
77
+ export type CoreType = (typeof CoreType)[keyof typeof CoreType]
78
+
79
+ /** States of a customer — `Customer.state`, compare it with `CustomerState.ENABLED` */
80
+ export const CustomerState = Object.freeze({
81
+ CREATED: 'CREATED',
82
+ ENABLED: 'ENABLED',
83
+ CLOSED: 'CLOSED',
84
+ FROZEN: 'FROZEN',
85
+ } as const)
86
+ /** The values of `CustomerState` */
87
+ export type CustomerState = (typeof CustomerState)[keyof typeof CustomerState]
88
+
89
+ /** `KYC` (a person) or `KYB` (a business) — `Identity.mode` */
90
+ export const IdentityMode = Object.freeze({
91
+ KYC: 'KYC',
92
+ KYB: 'KYB',
93
+ } as const)
94
+ /** The values of `IdentityMode` */
95
+ export type IdentityMode = (typeof IdentityMode)[keyof typeof IdentityMode]
96
+
97
+ /** States of an identity file — `Identity.state` */
98
+ export const IdentityState = Object.freeze({
99
+ CREATED: 'CREATED',
100
+ IN_PROGRESS: 'IN_PROGRESS',
101
+ WAIT: 'WAIT',
102
+ PENDING: 'PENDING',
103
+ VALIDATED: 'VALIDATED',
104
+ REJECTED: 'REJECTED',
105
+ FROZEN: 'FROZEN',
106
+ EXPIRED: 'EXPIRED',
107
+ CLOSED: 'CLOSED',
108
+ } as const)
109
+ /** The values of `IdentityState` */
110
+ export type IdentityState = (typeof IdentityState)[keyof typeof IdentityState]
111
+
112
+ /** Language of the BITGEN web application and of the emails — `Locale.FR` (default) or `Locale.EN` */
113
+ export const Locale = Object.freeze({
114
+ FR: 'FR',
115
+ EN: 'EN',
116
+ } as const)
117
+ /** The values of `Locale` */
118
+ export type Locale = (typeof Locale)[keyof typeof Locale]
119
+
120
+ /** Side of an order — `Order.side` */
121
+ export const OrderSide = Object.freeze({
122
+ BUY: 'BUY',
123
+ SELL: 'SELL',
124
+ } as const)
125
+ /** The values of `OrderSide` */
126
+ export type OrderSide = (typeof OrderSide)[keyof typeof OrderSide]
127
+
128
+ /** States of an order — `Order.state`. A purchase goes `REGISTERED` → `EXECUTING` → `FILLED` → `DELIVERING` → `DONE`; a sale `REGISTERED` → `TRANSFERRING` → `DEPOSITED` → `EXECUTING` → `FILLED` → `DONE`. `PARKED`: executed but nothing was received (terminal); `FAILED` */
129
+ export const OrderState = Object.freeze({
130
+ REGISTERED: 'REGISTERED',
131
+ TRANSFERRING: 'TRANSFERRING',
132
+ DEPOSITED: 'DEPOSITED',
133
+ EXECUTING: 'EXECUTING',
134
+ FILLED: 'FILLED',
135
+ DELIVERING: 'DELIVERING',
136
+ DONE: 'DONE',
137
+ PARKED: 'PARKED',
138
+ FAILED: 'FAILED',
139
+ } as const)
140
+ /** The values of `OrderState` */
141
+ export type OrderState = (typeof OrderState)[keyof typeof OrderState]
142
+
143
+ /** Category of a customer — the `organization` of `client.customer.create` (`CUSTOMER` by default; `B2B` also opens a KYB file) and `setup.choosenOrganization` */
144
+ export const OrganizationCategory = Object.freeze({
145
+ CUSTOMER: 'CUSTOMER',
146
+ B2B: 'B2B',
147
+ } as const)
148
+ /** The values of `OrganizationCategory` */
149
+ export type OrganizationCategory = (typeof OrganizationCategory)[keyof typeof OrganizationCategory]
150
+
151
+ /** Kinds of staking movements — `StakingMovement.kind`; also the `direction` filter of `client.staking.list` / `movements` */
152
+ export const StakingMovementKind = Object.freeze({
153
+ STAKE: 'STAKE',
154
+ UNSTAKE: 'UNSTAKE',
155
+ WITHDRAW: 'WITHDRAW',
156
+ REWARD: 'REWARD',
157
+ } as const)
158
+ /** The values of `StakingMovementKind` */
159
+ export type StakingMovementKind = (typeof StakingMovementKind)[keyof typeof StakingMovementKind]
160
+
161
+ /** States of a staking movement — `StakingMovement.state` */
162
+ export const StakingMovementState = Object.freeze({
163
+ REQUESTED: 'REQUESTED',
164
+ PENDING: 'PENDING',
165
+ COMPLETED: 'COMPLETED',
166
+ FAILED: 'FAILED',
167
+ CANCELED: 'CANCELED',
168
+ } as const)
169
+ /** The values of `StakingMovementState` */
170
+ export type StakingMovementState = (typeof StakingMovementState)[keyof typeof StakingMovementState]
171
+
172
+ /** States of a staking position — `StakingPosition.state` */
173
+ export const StakingPositionState = Object.freeze({
174
+ CREATED: 'CREATED',
175
+ ENABLED: 'ENABLED',
176
+ UNSTAKING: 'UNSTAKING',
177
+ CLOSED: 'CLOSED',
178
+ FAILED: 'FAILED',
179
+ } as const)
180
+ /** The values of `StakingPositionState` */
181
+ export type StakingPositionState = (typeof StakingPositionState)[keyof typeof StakingPositionState]
182
+
183
+ /** States of a webhook subscription (`Subscriber.state`) and of an event of the catalogue (`WebhookType.state`) — `ARCHIVED` ones are left out of `client.webhooks.list` unless `includeArchived` */
184
+ export const SubscriberState = Object.freeze({
185
+ ENABLED: 'ENABLED',
186
+ ARCHIVED: 'ARCHIVED',
187
+ } as const)
188
+ /** The values of `SubscriberState` */
189
+ export type SubscriberState = (typeof SubscriberState)[keyof typeof SubscriberState]
190
+
191
+ /** Filter of `client.trading.list`: only purchases, or only sales — lowercase, as the API expects it */
192
+ export const TradingDirection = Object.freeze({
193
+ BUY: 'buy',
194
+ SELL: 'sell',
195
+ } as const)
196
+ /** The values of `TradingDirection` */
197
+ export type TradingDirection = (typeof TradingDirection)[keyof typeof TradingDirection]
198
+
199
+ /** Direction of a transaction — `Transaction.direction`; also a filter of `client.transaction.list` */
200
+ export const TransactionDirection = Object.freeze({
201
+ IN: 'IN',
202
+ OUT: 'OUT',
203
+ } as const)
204
+ /** The values of `TransactionDirection` */
205
+ export type TransactionDirection = (typeof TransactionDirection)[keyof typeof TransactionDirection]
206
+
207
+ /** Where a transaction comes from: the EUR account (`BANK`) or a custody wallet (`CUSTODY`) — `Transaction.source`; also a filter of `client.transaction.list` */
208
+ export const TransactionSource = Object.freeze({
209
+ BANK: 'BANK',
210
+ CUSTODY: 'CUSTODY',
211
+ } as const)
212
+ /** The values of `TransactionSource` */
213
+ export type TransactionSource = (typeof TransactionSource)[keyof typeof TransactionSource]
214
+
215
+ /** States of a transaction (`TRANSFERING` is the API's spelling) — `Transaction.state`; also the `status` filter of `client.transaction.list` */
216
+ export const TransactionState = Object.freeze({
217
+ ANALYZING: 'ANALYZING',
218
+ PENDING: 'PENDING',
219
+ COMPLETED: 'COMPLETED',
220
+ FROZEN: 'FROZEN',
221
+ FAILED: 'FAILED',
222
+ TRANSFERING: 'TRANSFERING',
223
+ SEIZED: 'SEIZED',
224
+ } as const)
225
+ /** The values of `TransactionState` */
226
+ export type TransactionState = (typeof TransactionState)[keyof typeof TransactionState]
227
+
228
+ /** States of a wallet — `Wallet.state` */
229
+ export const WalletState = Object.freeze({
230
+ CREATED: 'CREATED',
231
+ FROZEN: 'FROZEN',
232
+ } as const)
233
+ /** The values of `WalletState` */
234
+ export type WalletState = (typeof WalletState)[keyof typeof WalletState]
235
+
236
+ /** `USER`: the wallet of a customer — `TREASURY`: a wallet of the organization itself (read-only). `Wallet.type` */
237
+ export const WalletType = Object.freeze({
238
+ USER: 'USER',
239
+ TREASURY: 'TREASURY',
240
+ } as const)
241
+ /** The values of `WalletType` */
242
+ export type WalletType = (typeof WalletType)[keyof typeof WalletType]
243
+
244
+ /** Events of the catalogue — `WebhookEvent.event` and `WebhookType.name` (the catalogue may grow: any string stays accepted); `client.webhooks.subscribe` takes a constant, any name or uuid of the catalogue, or a `WebhookType` */
245
+ export const WebhookEventName = Object.freeze({
246
+ USER_CREATED: 'user.created',
247
+ ORGANIZATION_CREATED: 'organization.created',
248
+ USER_IDENTITY_STARTED: 'user.identity.started',
249
+ USER_IDENTITY_PENDING: 'user.identity.pending',
250
+ USER_IDENTITY_STEP_VALIDATED: 'user.identity.step.validated',
251
+ USER_IDENTITY_STEP_REJECTED: 'user.identity.step.rejected',
252
+ USER_IDENTITY_STEP_REQUESTED: 'user.identity.step.requested',
253
+ USER_IDENTITY_VALIDATED: 'user.identity.validated',
254
+ USER_IDENTITY_RENEW: 'user.identity.renew',
255
+ USER_IDENTITY_REQUEST: 'user.identity.request',
256
+ ORGANIZATION_IDENTITY_STARTED: 'organization.identity.started',
257
+ ORGANIZATION_IDENTITY_PENDING: 'organization.identity.pending',
258
+ ORGANIZATION_IDENTITY_STEP_VALIDATED: 'organization.identity.step.validated',
259
+ ORGANIZATION_IDENTITY_STEP_REJECTED: 'organization.identity.step.rejected',
260
+ ORGANIZATION_IDENTITY_STEP_REQUESTED: 'organization.identity.step.requested',
261
+ ORGANIZATION_IDENTITY_VALIDATED: 'organization.identity.validated',
262
+ ORGANIZATION_IDENTITY_RENEW: 'organization.identity.renew',
263
+ ORGANIZATION_IDENTITY_REQUEST: 'organization.identity.request',
264
+ BANK_CREDITED: 'bank.credited',
265
+ BANK_DEBITED: 'bank.debited',
266
+ BANK_TRANSACTION: 'bank.transaction',
267
+ CUSTODY_TRANSACTION: 'custody.transaction',
268
+ CUSTODY_WALLET_CREATED: 'custody.wallet.created',
269
+ CUSTODY_SENT: 'custody.sent',
270
+ CUSTODY_RECEIVED: 'custody.received',
271
+ TRADING_BUY: 'trading.buy',
272
+ TRADING_SELL: 'trading.sell',
273
+ STAKING_REQUESTED: 'staking.requested',
274
+ STAKING_STATUS: 'staking.status',
275
+ STAKING_REWARDS: 'staking.rewards',
276
+ STAKING_CLAIMED: 'staking.claimed',
277
+ ALERT_OPENED: 'alert.opened',
278
+ ALERT_STATUS: 'alert.status',
279
+ } as const)
280
+ /** Known event names — the catalogue may grow: any string is accepted */
281
+ export type WebhookEventName = (typeof WebhookEventName)[keyof typeof WebhookEventName] | (string & {})
package/src/error.ts ADDED
@@ -0,0 +1,36 @@
1
+ import type { BitgenErrorBody } from './types/index.js'
2
+
3
+ /** Max length kept for `code`: the API sends short snake_case codes, anything longer is a foreign body (proxy page…) */
4
+ export const MAX_CODE_LENGTH = 200
5
+
6
+ /** Brand shared by every copy of the SDK loaded in a process (ESM and CJS builds side by side) */
7
+ const BRAND = Symbol.for('@bitgen/sdk:BitgenError')
8
+
9
+ export class BitgenError extends Error {
10
+ /** HTTP status code — `0` when no HTTP response was received (`request_timeout`, `network_error`) */
11
+ readonly status: number
12
+ /** Stable BITGEN error code (e.g. `invalid_amount`), or the raw response text (≤ 200 chars) when the API did not answer JSON */
13
+ readonly code: string
14
+
15
+ constructor(status: number, body: BitgenErrorBody, options?: { cause?: unknown }) {
16
+ const code = body.message.slice(0, MAX_CODE_LENGTH)
17
+ super(`[${status}] ${code}`, options)
18
+ this.name = 'BitgenError'
19
+ this.status = status
20
+ this.code = code
21
+ Object.defineProperty(this, BRAND, { value: true })
22
+ }
23
+
24
+ /** `instanceof` also recognizes a BitgenError thrown by another copy of the SDK (ESM and CJS builds loaded together) */
25
+ static override [Symbol.hasInstance](value: unknown): boolean {
26
+ if (Function.prototype[Symbol.hasInstance].call(this, value)) {
27
+ return true
28
+ }
29
+ return this === BitgenError && typeof value === 'object' && value !== null && BRAND in value
30
+ }
31
+
32
+ /** `JSON.stringify(err)` → `{ name, message, status, code }` (`cause` is left out) */
33
+ toJSON(): { name: string, message: string, status: number, code: string } {
34
+ return { name: this.name, message: this.message, status: this.status, code: this.code }
35
+ }
36
+ }