@did-btcr2/method 0.13.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 (310) hide show
  1. package/LICENSE +373 -0
  2. package/README.md +7 -0
  3. package/dist/browser.js +2364 -0
  4. package/dist/browser.js.map +7 -0
  5. package/dist/browser.mjs +2364 -0
  6. package/dist/browser.mjs.map +7 -0
  7. package/dist/cjs/bitcoin/constants.js +20 -0
  8. package/dist/cjs/bitcoin/constants.js.map +1 -0
  9. package/dist/cjs/bitcoin/errors.js +11 -0
  10. package/dist/cjs/bitcoin/errors.js.map +1 -0
  11. package/dist/cjs/bitcoin/index.js +95 -0
  12. package/dist/cjs/bitcoin/index.js.map +1 -0
  13. package/dist/cjs/bitcoin/interface.js +2 -0
  14. package/dist/cjs/bitcoin/interface.js.map +1 -0
  15. package/dist/cjs/bitcoin/network.js +17 -0
  16. package/dist/cjs/bitcoin/network.js.map +1 -0
  17. package/dist/cjs/bitcoin/rest-client.js +289 -0
  18. package/dist/cjs/bitcoin/rest-client.js.map +1 -0
  19. package/dist/cjs/bitcoin/rpc-client.js +722 -0
  20. package/dist/cjs/bitcoin/rpc-client.js.map +1 -0
  21. package/dist/cjs/bitcoin/taproot.js +219 -0
  22. package/dist/cjs/bitcoin/taproot.js.map +1 -0
  23. package/dist/cjs/btcr2/beacon/aggregation/coordinator.js +120 -0
  24. package/dist/cjs/btcr2/beacon/aggregation/coordinator.js.map +1 -0
  25. package/dist/cjs/btcr2/beacon/aggregation/messages/advert.js +24 -0
  26. package/dist/cjs/btcr2/beacon/aggregation/messages/advert.js.map +1 -0
  27. package/dist/cjs/btcr2/beacon/aggregation/messages/base.js +37 -0
  28. package/dist/cjs/btcr2/beacon/aggregation/messages/base.js.map +1 -0
  29. package/dist/cjs/btcr2/beacon/aggregation/messages/cohort-set.js +25 -0
  30. package/dist/cjs/btcr2/beacon/aggregation/messages/cohort-set.js.map +1 -0
  31. package/dist/cjs/btcr2/beacon/aggregation/messages/keygen.js +8 -0
  32. package/dist/cjs/btcr2/beacon/aggregation/messages/keygen.js.map +1 -0
  33. package/dist/cjs/btcr2/beacon/aggregation/messages/opt-in.js +23 -0
  34. package/dist/cjs/btcr2/beacon/aggregation/messages/opt-in.js.map +1 -0
  35. package/dist/cjs/btcr2/beacon/aggregation/messages/sign.js +7 -0
  36. package/dist/cjs/btcr2/beacon/aggregation/messages/sign.js.map +1 -0
  37. package/dist/cjs/btcr2/beacon/aggregation/models/cohort/index.js +92 -0
  38. package/dist/cjs/btcr2/beacon/aggregation/models/cohort/index.js.map +1 -0
  39. package/dist/cjs/btcr2/beacon/aggregation/models/cohort/status.js +8 -0
  40. package/dist/cjs/btcr2/beacon/aggregation/models/cohort/status.js.map +1 -0
  41. package/dist/cjs/btcr2/beacon/aggregation/participant.js +2 -0
  42. package/dist/cjs/btcr2/beacon/aggregation/participant.js.map +1 -0
  43. package/dist/cjs/btcr2/beacon/aggregation/protocol/nostr.js +57 -0
  44. package/dist/cjs/btcr2/beacon/aggregation/protocol/nostr.js.map +1 -0
  45. package/dist/cjs/btcr2/beacon/aggregation/protocol/service.js +2 -0
  46. package/dist/cjs/btcr2/beacon/aggregation/protocol/service.js.map +1 -0
  47. package/dist/cjs/btcr2/beacon/cid-aggregate.js +116 -0
  48. package/dist/cjs/btcr2/beacon/cid-aggregate.js.map +1 -0
  49. package/dist/cjs/btcr2/beacon/factory.js +30 -0
  50. package/dist/cjs/btcr2/beacon/factory.js.map +1 -0
  51. package/dist/cjs/btcr2/beacon/singleton.js +220 -0
  52. package/dist/cjs/btcr2/beacon/singleton.js.map +1 -0
  53. package/dist/cjs/btcr2/beacon/smt-aggregate.js +126 -0
  54. package/dist/cjs/btcr2/beacon/smt-aggregate.js.map +1 -0
  55. package/dist/cjs/btcr2/crud/create.js +102 -0
  56. package/dist/cjs/btcr2/crud/create.js.map +1 -0
  57. package/dist/cjs/btcr2/crud/deactivate.js +14 -0
  58. package/dist/cjs/btcr2/crud/deactivate.js.map +1 -0
  59. package/dist/cjs/btcr2/crud/read.js +686 -0
  60. package/dist/cjs/btcr2/crud/read.js.map +1 -0
  61. package/dist/cjs/btcr2/crud/update.js +195 -0
  62. package/dist/cjs/btcr2/crud/update.js.map +1 -0
  63. package/dist/cjs/btcr2/key-manager/index.js +290 -0
  64. package/dist/cjs/btcr2/key-manager/index.js.map +1 -0
  65. package/dist/cjs/btcr2/key-manager/interface.js +2 -0
  66. package/dist/cjs/btcr2/key-manager/interface.js.map +1 -0
  67. package/dist/cjs/did-btcr2.js +222 -0
  68. package/dist/cjs/did-btcr2.js.map +1 -0
  69. package/dist/cjs/index.js +27 -0
  70. package/dist/cjs/index.js.map +1 -0
  71. package/dist/cjs/interfaces/beacon.js +41 -0
  72. package/dist/cjs/interfaces/beacon.js.map +1 -0
  73. package/dist/cjs/interfaces/crud.js +2 -0
  74. package/dist/cjs/interfaces/crud.js.map +1 -0
  75. package/dist/cjs/interfaces/ibeacon.js +2 -0
  76. package/dist/cjs/interfaces/ibeacon.js.map +1 -0
  77. package/dist/cjs/package.json +1 -0
  78. package/dist/cjs/types/bitcoin.js +62 -0
  79. package/dist/cjs/types/bitcoin.js.map +1 -0
  80. package/dist/cjs/types/crud.js +2 -0
  81. package/dist/cjs/types/crud.js.map +1 -0
  82. package/dist/cjs/utils/appendix.js +221 -0
  83. package/dist/cjs/utils/appendix.js.map +1 -0
  84. package/dist/cjs/utils/beacons.js +206 -0
  85. package/dist/cjs/utils/beacons.js.map +1 -0
  86. package/dist/cjs/utils/did-document-builder.js +61 -0
  87. package/dist/cjs/utils/did-document-builder.js.map +1 -0
  88. package/dist/cjs/utils/did-document.js +380 -0
  89. package/dist/cjs/utils/did-document.js.map +1 -0
  90. package/dist/cjs/utils/general.js +195 -0
  91. package/dist/cjs/utils/general.js.map +1 -0
  92. package/dist/cjs/utils/identifier.js +238 -0
  93. package/dist/cjs/utils/identifier.js.map +1 -0
  94. package/dist/esm/bitcoin/constants.js +20 -0
  95. package/dist/esm/bitcoin/constants.js.map +1 -0
  96. package/dist/esm/bitcoin/errors.js +11 -0
  97. package/dist/esm/bitcoin/errors.js.map +1 -0
  98. package/dist/esm/bitcoin/index.js +95 -0
  99. package/dist/esm/bitcoin/index.js.map +1 -0
  100. package/dist/esm/bitcoin/interface.js +2 -0
  101. package/dist/esm/bitcoin/interface.js.map +1 -0
  102. package/dist/esm/bitcoin/network.js +17 -0
  103. package/dist/esm/bitcoin/network.js.map +1 -0
  104. package/dist/esm/bitcoin/rest-client.js +289 -0
  105. package/dist/esm/bitcoin/rest-client.js.map +1 -0
  106. package/dist/esm/bitcoin/rpc-client.js +722 -0
  107. package/dist/esm/bitcoin/rpc-client.js.map +1 -0
  108. package/dist/esm/bitcoin/taproot.js +219 -0
  109. package/dist/esm/bitcoin/taproot.js.map +1 -0
  110. package/dist/esm/btcr2/beacon/aggregation/coordinator.js +120 -0
  111. package/dist/esm/btcr2/beacon/aggregation/coordinator.js.map +1 -0
  112. package/dist/esm/btcr2/beacon/aggregation/messages/advert.js +24 -0
  113. package/dist/esm/btcr2/beacon/aggregation/messages/advert.js.map +1 -0
  114. package/dist/esm/btcr2/beacon/aggregation/messages/base.js +37 -0
  115. package/dist/esm/btcr2/beacon/aggregation/messages/base.js.map +1 -0
  116. package/dist/esm/btcr2/beacon/aggregation/messages/cohort-set.js +25 -0
  117. package/dist/esm/btcr2/beacon/aggregation/messages/cohort-set.js.map +1 -0
  118. package/dist/esm/btcr2/beacon/aggregation/messages/keygen.js +8 -0
  119. package/dist/esm/btcr2/beacon/aggregation/messages/keygen.js.map +1 -0
  120. package/dist/esm/btcr2/beacon/aggregation/messages/opt-in.js +23 -0
  121. package/dist/esm/btcr2/beacon/aggregation/messages/opt-in.js.map +1 -0
  122. package/dist/esm/btcr2/beacon/aggregation/messages/sign.js +7 -0
  123. package/dist/esm/btcr2/beacon/aggregation/messages/sign.js.map +1 -0
  124. package/dist/esm/btcr2/beacon/aggregation/models/cohort/index.js +92 -0
  125. package/dist/esm/btcr2/beacon/aggregation/models/cohort/index.js.map +1 -0
  126. package/dist/esm/btcr2/beacon/aggregation/models/cohort/status.js +8 -0
  127. package/dist/esm/btcr2/beacon/aggregation/models/cohort/status.js.map +1 -0
  128. package/dist/esm/btcr2/beacon/aggregation/participant.js +2 -0
  129. package/dist/esm/btcr2/beacon/aggregation/participant.js.map +1 -0
  130. package/dist/esm/btcr2/beacon/aggregation/protocol/nostr.js +57 -0
  131. package/dist/esm/btcr2/beacon/aggregation/protocol/nostr.js.map +1 -0
  132. package/dist/esm/btcr2/beacon/aggregation/protocol/service.js +2 -0
  133. package/dist/esm/btcr2/beacon/aggregation/protocol/service.js.map +1 -0
  134. package/dist/esm/btcr2/beacon/cid-aggregate.js +116 -0
  135. package/dist/esm/btcr2/beacon/cid-aggregate.js.map +1 -0
  136. package/dist/esm/btcr2/beacon/factory.js +30 -0
  137. package/dist/esm/btcr2/beacon/factory.js.map +1 -0
  138. package/dist/esm/btcr2/beacon/singleton.js +220 -0
  139. package/dist/esm/btcr2/beacon/singleton.js.map +1 -0
  140. package/dist/esm/btcr2/beacon/smt-aggregate.js +126 -0
  141. package/dist/esm/btcr2/beacon/smt-aggregate.js.map +1 -0
  142. package/dist/esm/btcr2/crud/create.js +102 -0
  143. package/dist/esm/btcr2/crud/create.js.map +1 -0
  144. package/dist/esm/btcr2/crud/deactivate.js +14 -0
  145. package/dist/esm/btcr2/crud/deactivate.js.map +1 -0
  146. package/dist/esm/btcr2/crud/read.js +686 -0
  147. package/dist/esm/btcr2/crud/read.js.map +1 -0
  148. package/dist/esm/btcr2/crud/update.js +195 -0
  149. package/dist/esm/btcr2/crud/update.js.map +1 -0
  150. package/dist/esm/btcr2/key-manager/index.js +290 -0
  151. package/dist/esm/btcr2/key-manager/index.js.map +1 -0
  152. package/dist/esm/btcr2/key-manager/interface.js +2 -0
  153. package/dist/esm/btcr2/key-manager/interface.js.map +1 -0
  154. package/dist/esm/did-btcr2.js +222 -0
  155. package/dist/esm/did-btcr2.js.map +1 -0
  156. package/dist/esm/index.js +27 -0
  157. package/dist/esm/index.js.map +1 -0
  158. package/dist/esm/interfaces/beacon.js +41 -0
  159. package/dist/esm/interfaces/beacon.js.map +1 -0
  160. package/dist/esm/interfaces/crud.js +2 -0
  161. package/dist/esm/interfaces/crud.js.map +1 -0
  162. package/dist/esm/interfaces/ibeacon.js +2 -0
  163. package/dist/esm/interfaces/ibeacon.js.map +1 -0
  164. package/dist/esm/types/bitcoin.js +62 -0
  165. package/dist/esm/types/bitcoin.js.map +1 -0
  166. package/dist/esm/types/crud.js +2 -0
  167. package/dist/esm/types/crud.js.map +1 -0
  168. package/dist/esm/utils/appendix.js +221 -0
  169. package/dist/esm/utils/appendix.js.map +1 -0
  170. package/dist/esm/utils/beacons.js +206 -0
  171. package/dist/esm/utils/beacons.js.map +1 -0
  172. package/dist/esm/utils/did-document-builder.js +61 -0
  173. package/dist/esm/utils/did-document-builder.js.map +1 -0
  174. package/dist/esm/utils/did-document.js +380 -0
  175. package/dist/esm/utils/did-document.js.map +1 -0
  176. package/dist/esm/utils/general.js +195 -0
  177. package/dist/esm/utils/general.js.map +1 -0
  178. package/dist/esm/utils/identifier.js +238 -0
  179. package/dist/esm/utils/identifier.js.map +1 -0
  180. package/dist/types/bitcoin/constants.d.ts +19 -0
  181. package/dist/types/bitcoin/constants.d.ts.map +1 -0
  182. package/dist/types/bitcoin/errors.d.ts +5 -0
  183. package/dist/types/bitcoin/errors.d.ts.map +1 -0
  184. package/dist/types/bitcoin/index.d.ts +75 -0
  185. package/dist/types/bitcoin/index.d.ts.map +1 -0
  186. package/dist/types/bitcoin/interface.d.ts +86 -0
  187. package/dist/types/bitcoin/interface.d.ts.map +1 -0
  188. package/dist/types/bitcoin/network.d.ts +2 -0
  189. package/dist/types/bitcoin/network.d.ts.map +1 -0
  190. package/dist/types/bitcoin/rest-client.d.ts +268 -0
  191. package/dist/types/bitcoin/rest-client.d.ts.map +1 -0
  192. package/dist/types/bitcoin/rpc-client.d.ts +506 -0
  193. package/dist/types/bitcoin/rpc-client.d.ts.map +1 -0
  194. package/dist/types/bitcoin/taproot.d.ts +34 -0
  195. package/dist/types/bitcoin/taproot.d.ts.map +1 -0
  196. package/dist/types/btcr2/beacon/aggregation/coordinator.d.ts +74 -0
  197. package/dist/types/btcr2/beacon/aggregation/coordinator.d.ts.map +1 -0
  198. package/dist/types/btcr2/beacon/aggregation/messages/advert.d.ts +22 -0
  199. package/dist/types/btcr2/beacon/aggregation/messages/advert.d.ts.map +1 -0
  200. package/dist/types/btcr2/beacon/aggregation/messages/base.d.ts +36 -0
  201. package/dist/types/btcr2/beacon/aggregation/messages/base.d.ts.map +1 -0
  202. package/dist/types/btcr2/beacon/aggregation/messages/cohort-set.d.ts +23 -0
  203. package/dist/types/btcr2/beacon/aggregation/messages/cohort-set.d.ts.map +1 -0
  204. package/dist/types/btcr2/beacon/aggregation/messages/keygen.d.ts +6 -0
  205. package/dist/types/btcr2/beacon/aggregation/messages/keygen.d.ts.map +1 -0
  206. package/dist/types/btcr2/beacon/aggregation/messages/opt-in.d.ts +22 -0
  207. package/dist/types/btcr2/beacon/aggregation/messages/opt-in.d.ts.map +1 -0
  208. package/dist/types/btcr2/beacon/aggregation/messages/sign.d.ts +5 -0
  209. package/dist/types/btcr2/beacon/aggregation/messages/sign.d.ts.map +1 -0
  210. package/dist/types/btcr2/beacon/aggregation/models/cohort/index.d.ts +77 -0
  211. package/dist/types/btcr2/beacon/aggregation/models/cohort/index.d.ts.map +1 -0
  212. package/dist/types/btcr2/beacon/aggregation/models/cohort/status.d.ts +7 -0
  213. package/dist/types/btcr2/beacon/aggregation/models/cohort/status.d.ts.map +1 -0
  214. package/dist/types/btcr2/beacon/aggregation/participant.d.ts +1 -0
  215. package/dist/types/btcr2/beacon/aggregation/participant.d.ts.map +1 -0
  216. package/dist/types/btcr2/beacon/aggregation/protocol/nostr.d.ts +36 -0
  217. package/dist/types/btcr2/beacon/aggregation/protocol/nostr.d.ts.map +1 -0
  218. package/dist/types/btcr2/beacon/aggregation/protocol/service.d.ts +6 -0
  219. package/dist/types/btcr2/beacon/aggregation/protocol/service.d.ts.map +1 -0
  220. package/dist/types/btcr2/beacon/cid-aggregate.d.ts +103 -0
  221. package/dist/types/btcr2/beacon/cid-aggregate.d.ts.map +1 -0
  222. package/dist/types/btcr2/beacon/factory.d.ts +17 -0
  223. package/dist/types/btcr2/beacon/factory.d.ts.map +1 -0
  224. package/dist/types/btcr2/beacon/singleton.d.ts +93 -0
  225. package/dist/types/btcr2/beacon/singleton.d.ts.map +1 -0
  226. package/dist/types/btcr2/beacon/smt-aggregate.d.ts +112 -0
  227. package/dist/types/btcr2/beacon/smt-aggregate.d.ts.map +1 -0
  228. package/dist/types/btcr2/crud/create.d.ts +92 -0
  229. package/dist/types/btcr2/crud/create.d.ts.map +1 -0
  230. package/dist/types/btcr2/crud/deactivate.d.ts +13 -0
  231. package/dist/types/btcr2/crud/deactivate.d.ts.map +1 -0
  232. package/dist/types/btcr2/crud/read.d.ts +341 -0
  233. package/dist/types/btcr2/crud/read.d.ts.map +1 -0
  234. package/dist/types/btcr2/crud/update.d.ts +83 -0
  235. package/dist/types/btcr2/crud/update.d.ts.map +1 -0
  236. package/dist/types/btcr2/key-manager/index.d.ts +145 -0
  237. package/dist/types/btcr2/key-manager/index.d.ts.map +1 -0
  238. package/dist/types/btcr2/key-manager/interface.d.ts +113 -0
  239. package/dist/types/btcr2/key-manager/interface.d.ts.map +1 -0
  240. package/dist/types/did-btcr2.d.ts +117 -0
  241. package/dist/types/did-btcr2.d.ts.map +1 -0
  242. package/dist/types/index.d.ts +26 -0
  243. package/dist/types/index.d.ts.map +1 -0
  244. package/dist/types/interfaces/beacon.d.ts +57 -0
  245. package/dist/types/interfaces/beacon.d.ts.map +1 -0
  246. package/dist/types/interfaces/crud.d.ts +35 -0
  247. package/dist/types/interfaces/crud.d.ts.map +1 -0
  248. package/dist/types/interfaces/ibeacon.d.ts +66 -0
  249. package/dist/types/interfaces/ibeacon.d.ts.map +1 -0
  250. package/dist/types/types/bitcoin.d.ts +827 -0
  251. package/dist/types/types/bitcoin.d.ts.map +1 -0
  252. package/dist/types/types/crud.d.ts +38 -0
  253. package/dist/types/types/crud.d.ts.map +1 -0
  254. package/dist/types/utils/appendix.d.ts +118 -0
  255. package/dist/types/utils/appendix.d.ts.map +1 -0
  256. package/dist/types/utils/beacons.d.ts +156 -0
  257. package/dist/types/utils/beacons.d.ts.map +1 -0
  258. package/dist/types/utils/did-document-builder.d.ts +13 -0
  259. package/dist/types/utils/did-document-builder.d.ts.map +1 -0
  260. package/dist/types/utils/did-document.d.ts +211 -0
  261. package/dist/types/utils/did-document.d.ts.map +1 -0
  262. package/dist/types/utils/general.d.ts +85 -0
  263. package/dist/types/utils/general.d.ts.map +1 -0
  264. package/dist/types/utils/identifier.d.ts +59 -0
  265. package/dist/types/utils/identifier.d.ts.map +1 -0
  266. package/package.json +137 -0
  267. package/src/bitcoin/constants.ts +19 -0
  268. package/src/bitcoin/errors.ts +10 -0
  269. package/src/bitcoin/index.ts +154 -0
  270. package/src/bitcoin/interface.ts +160 -0
  271. package/src/bitcoin/network.ts +17 -0
  272. package/src/bitcoin/rest-client.ts +415 -0
  273. package/src/bitcoin/rpc-client.ts +888 -0
  274. package/src/bitcoin/taproot.ts +237 -0
  275. package/src/btcr2/beacon/aggregation/coordinator.ts +135 -0
  276. package/src/btcr2/beacon/aggregation/messages/advert.ts +36 -0
  277. package/src/btcr2/beacon/aggregation/messages/base.ts +59 -0
  278. package/src/btcr2/beacon/aggregation/messages/cohort-set.ts +37 -0
  279. package/src/btcr2/beacon/aggregation/messages/keygen.ts +8 -0
  280. package/src/btcr2/beacon/aggregation/messages/opt-in.ts +35 -0
  281. package/src/btcr2/beacon/aggregation/messages/sign.ts +7 -0
  282. package/src/btcr2/beacon/aggregation/models/cohort/index.ts +112 -0
  283. package/src/btcr2/beacon/aggregation/models/cohort/status.ts +7 -0
  284. package/src/btcr2/beacon/aggregation/participant.ts +0 -0
  285. package/src/btcr2/beacon/aggregation/protocol/nostr.ts +81 -0
  286. package/src/btcr2/beacon/aggregation/protocol/service.ts +6 -0
  287. package/src/btcr2/beacon/cid-aggregate.ts +154 -0
  288. package/src/btcr2/beacon/factory.ts +36 -0
  289. package/src/btcr2/beacon/singleton.ts +257 -0
  290. package/src/btcr2/beacon/smt-aggregate.ts +136 -0
  291. package/src/btcr2/crud/create.ts +160 -0
  292. package/src/btcr2/crud/deactivate.ts +13 -0
  293. package/src/btcr2/crud/read.ts +946 -0
  294. package/src/btcr2/crud/update.ts +277 -0
  295. package/src/btcr2/key-manager/index.ts +364 -0
  296. package/src/btcr2/key-manager/interface.ts +129 -0
  297. package/src/canonicalize.d.ts +6 -0
  298. package/src/did-btcr2.ts +288 -0
  299. package/src/index.ts +34 -0
  300. package/src/interfaces/beacon.ts +68 -0
  301. package/src/interfaces/crud.ts +36 -0
  302. package/src/interfaces/ibeacon.ts +76 -0
  303. package/src/types/bitcoin.ts +1028 -0
  304. package/src/types/crud.ts +41 -0
  305. package/src/utils/appendix.ts +257 -0
  306. package/src/utils/beacons.ts +276 -0
  307. package/src/utils/did-document-builder.ts +73 -0
  308. package/src/utils/did-document.ts +474 -0
  309. package/src/utils/general.ts +204 -0
  310. package/src/utils/identifier.ts +276 -0
@@ -0,0 +1,206 @@
1
+ import { Btcr2Error, DidBtcr2Error } from '@did-btcr2/common';
2
+ import { payments } from 'bitcoinjs-lib';
3
+ import { BeaconFactory } from '../btcr2/beacon/factory.js';
4
+ import { Btc1Appendix } from './appendix.js';
5
+ /**
6
+ * Static class of utility functions for the Beacon Service
7
+ * @class BeaconUtils
8
+ * @type {BeaconUtils}
9
+ */
10
+ export class BeaconUtils {
11
+ /**
12
+ * Converts a BIP21 Bitcoin URI to a Bitcoin address
13
+ * @param {string} uri The BIP21 Bitcoin URI to convert
14
+ * @returns {string} The Bitcoin address extracted from the URI
15
+ * @throws {DidBtcr2Error} if the URI is not a valid Bitcoin URI
16
+ */
17
+ static parseBitcoinAddress(uri) {
18
+ if (!uri.startsWith('bitcoin:')) {
19
+ throw new DidBtcr2Error('Invalid Bitcoin URI format', { type: 'BEACON_ERROR' });
20
+ }
21
+ return uri.replace('bitcoin:', '').split('?')[0]; // Extracts address from "bitcoin:<address>?params"
22
+ }
23
+ /**
24
+ * Validates that the given object is a Beacon Service
25
+ * @param {BeaconService} obj The object to validate
26
+ * @returns {boolean} A boolean indicating whether the object is a Beacon Service
27
+ */
28
+ static isBeaconService(obj) {
29
+ // Return false if the given obj is not a valid DidService.
30
+ if (!Btc1Appendix.isDidService(obj))
31
+ return false;
32
+ // Return false if the type is not a valid beacon service type.
33
+ if (!['SingletonBeacon', 'CIDAggregateBeacon', 'SMTAggregateBeacon'].includes(obj.type))
34
+ return false;
35
+ // Return false if the serviceEndpoint is not a valid BIP21 bitcoin address.
36
+ if ([obj.serviceEndpoint].flat().some(ep => typeof ep === 'string' && !ep.startsWith('bitcoin:')))
37
+ return false;
38
+ // Return false if the casType exists and is not a string.
39
+ if (obj.casType && typeof obj.casType !== 'string')
40
+ return false;
41
+ // Else return true
42
+ return true;
43
+ }
44
+ /**
45
+ * Extracts the services from a given DID Document
46
+ * @param {DidDocument} didDocument The DID Document to extract the services from
47
+ * @returns {DidService[]} An array of DidService objects
48
+ * @throws {TypeError} if the didDocument is not provided
49
+ */
50
+ static getBeaconServices(didDocument) {
51
+ // Filter out any invalid did service objects.
52
+ const didServices = didDocument.service?.filter(Btc1Appendix.isDidService) ?? [];
53
+ // Filter for valid beacon service objects.
54
+ return (didServices.filter(this.isBeaconService) ?? []);
55
+ }
56
+ /**
57
+ * Generate all 3 Beacon Service Endpoints for a given public key.
58
+ * @param {GenerateBitcoinAddrsParams} params Required parameters for generating Beacon Services.
59
+ * @param {KeyBytes} params.publicKey Public key bytes used to generate the beacon object serviceEndpoint.
60
+ * @param {Network} params.network Bitcoin network interface from bitcoinlib-js.
61
+ * @returns {Array<Array<string>>} 2D Array of bitcoin addresses (p2pkh, p2wpkh, p2tr).
62
+ * @throws {DidBtcr2Error} if the bitcoin address is invalid.
63
+ */
64
+ static generateBeaconAddresses({ identifier, publicKey, network }) {
65
+ try {
66
+ const p2pkh = payments.p2pkh({ pubkey: publicKey, network }).address;
67
+ const p2wpkh = payments.p2wpkh({ pubkey: publicKey, network }).address;
68
+ const p2tr = payments.p2tr({ network, internalPubkey: publicKey.slice(1, 33) }).address;
69
+ if (!p2pkh || !p2wpkh || !p2tr) {
70
+ throw new DidBtcr2Error('Failed to generate bitcoin addresses');
71
+ }
72
+ return [
73
+ [`${identifier}#initialP2PKH`, p2pkh],
74
+ [`${identifier}#initialP2WPKH`, p2wpkh],
75
+ [`${identifier}#initialP2TR`, p2tr]
76
+ ];
77
+ }
78
+ catch (error) {
79
+ console.error(error);
80
+ process.exit(1);
81
+ }
82
+ }
83
+ /**
84
+ * Generate a set of Beacon Services for a given public key.
85
+ * @param {GenerateBeaconServicesParams} params Required parameters for generating Beacon Services.
86
+ * @param {KeyBytes} params.publicKey Public key bytes used to generate the beacon object serviceEndpoint.
87
+ * @param {Network} params.network Bitcoin network interface from bitcoinlib-js.
88
+ * @param {string} params.beaconType The type of beacon service to create.
89
+ * @param {string} params.addressType The type of address to create (p2pkh, p2wpkh, p2tr).
90
+ * @returns {BeaconService} A BeaconService object.
91
+ * @throws {DidBtcr2Error} if the bitcoin address is invalid.
92
+ */
93
+ static generateBeaconService({ id, publicKey: pubkey, network, addressType, type }) {
94
+ try {
95
+ if (!id.includes('#')) {
96
+ id = `${id}#initial${addressType.toUpperCase()}`;
97
+ }
98
+ const serviceEndpoint = `bitcoin:${payments[addressType]({ pubkey, network }).address}`;
99
+ return { id, type, serviceEndpoint, };
100
+ }
101
+ catch (error) {
102
+ console.error(error);
103
+ process.exit(1);
104
+ }
105
+ }
106
+ /**
107
+ * Generate a custom Beacon Service.
108
+ * @param {GenerateBeaconServicesParams} params Required parameters for generating Beacon Services.
109
+ * @returns
110
+ */
111
+ static generateBeaconServiceCustom({ id, publicKey: pubkey, network, addressType, type }) {
112
+ try {
113
+ if (!id.includes('#')) {
114
+ throw new Btcr2Error('Invalid id format. It should include a fragment identifier (e.g., #initialP2PKH).', 'BEACON_ERROR', { id });
115
+ }
116
+ const serviceEndpoint = `bitcoin:${payments[addressType]({ pubkey, network }).address}`;
117
+ return { id, type, serviceEndpoint, };
118
+ }
119
+ catch (error) {
120
+ console.error(error);
121
+ process.exit(1);
122
+ }
123
+ }
124
+ /**
125
+ * Generate beacon services.
126
+ * @param {GenerateBeaconServicesParams} params Required parameters for generating Beacon Services.
127
+ * @param {string} params.network The name of the Bitcoin network to use.
128
+ * @param {Uint8Array} params.publicKey Byte array representation of a public key used to generate a new btcr2 key-id-type.
129
+ * @param {string} params.beaconType Optional beacon type to use (default: SingletonBeacon).
130
+ * @returns {DidService[]} Array of DidService objects.
131
+ */
132
+ static generateBeaconServices({ identifier, network, type, publicKey }) {
133
+ // Generate the bitcoin addresses
134
+ const bitcoinAddrs = this.generateBeaconAddresses({ identifier, publicKey, network, });
135
+ // Map the bitcoin addresses to the beacon service
136
+ return bitcoinAddrs.map(([id, address]) => {
137
+ // Convert the address to a BIP-21 URI
138
+ const serviceEndpoint = `bitcoin:${address}`;
139
+ // Create the beacon object
140
+ const beacon = BeaconFactory.establish({ id, type, serviceEndpoint });
141
+ // Return the beacon service
142
+ return beacon.service;
143
+ });
144
+ }
145
+ /**
146
+ * Generate a single beacon service.
147
+ * @param {GenerateBeaconParams} params Required parameters for generating a single Beacon Service.
148
+ * @param {string} params.identifier The identifier for the beacon service.
149
+ * @param {string} params.network The name of the Bitcoin network to use.
150
+ * @param {Uint8Array} params.publicKey Byte array representation of a public key used to generate a new btcr2 key-id-type.
151
+ * @param {string} params.type The type of beacon service to create.
152
+ * @returns {BeaconService} A BeaconService object.
153
+ * @throws {DidBtcr2Error} if the bitcoin address is invalid.
154
+ */
155
+ static generateBeacon({ identifier, network, type, publicKey }) {
156
+ // Generate the bitcoin addresses
157
+ const bitcoinAddrs = this.generateBeaconAddresses({ identifier, publicKey, network, });
158
+ // Map the bitcoin addresses to the beacon service
159
+ const beacon = bitcoinAddrs.map(([id, address]) => {
160
+ // Convert the address to a BIP-21 URI
161
+ const serviceEndpoint = `bitcoin:${address}`;
162
+ // Create the beacon object
163
+ const beacon = BeaconFactory.establish({ id, type, serviceEndpoint });
164
+ // Return the beacon service
165
+ return beacon.service;
166
+ });
167
+ return beacon[0];
168
+ }
169
+ /**
170
+ * Manufacture a pre-filled Beacon using the BeaconFactory.
171
+ * @param {BeaconServicesParams} params Required parameters for generating a single Beacon Service.
172
+ * @param {string} params.serviceId The type of service being created (#initialP2PKH, #initialP2WPKH, #initialP2TR).
173
+ * @param {string} params.beaconType The type of beacon service being created (SingletonBeacon, SMTAggregatorBeacon).
174
+ * @param {BitcoinAddress} params.bitcoinAddress The bitcoin address to use for the service endpoint.
175
+ * @returns {BeaconService} One BeaconService object.
176
+ */
177
+ static manufactureBeacon(params) {
178
+ return BeaconFactory.establish(params).service;
179
+ }
180
+ /**
181
+ * Convert beacon service endpoints from BIP-21 URIs to addresses.
182
+ * @param {Array<BeaconService>} beacons The list of beacon services.
183
+ * @returns {Array<BeaconServiceAddress>} An array of beacon services with address: bitcoinAddress.
184
+ */
185
+ static toBeaconServiceAddress(beacons) {
186
+ return beacons.map((beacon) => ({ ...beacon, address: beacon.serviceEndpoint.replace('bitcoin:', '') }));
187
+ }
188
+ /**
189
+ * Create a map of address => beaconService with address field.
190
+ * @param {Array<BeaconService>} beacons The list of beacon services.
191
+ * @returns {Map<string, BeaconServiceAddress>} A map of address => beaconService.
192
+ */
193
+ static getBeaconServiceAddressMap(beacons) {
194
+ const beaconAddrs = this.toBeaconServiceAddress(beacons);
195
+ return new Map(beaconAddrs.map((beacon) => ([beacon.address, beacon])));
196
+ }
197
+ /**
198
+ * Get the beacon service ids from a list of beacon services.
199
+ * @param {Btc1DidDocument} didDocument The DID Document to extract the services from.
200
+ * @returns {string[]} An array of beacon service ids.
201
+ */
202
+ static getBeaconServiceIds(didDocument) {
203
+ return this.getBeaconServices(didDocument).map((beacon) => beacon.id);
204
+ }
205
+ }
206
+ //# sourceMappingURL=beacons.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"beacons.js","sourceRoot":"","sources":["../../../src/utils/beacons.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,aAAa,EAAmB,MAAM,mBAAmB,CAAC;AAE/E,OAAO,EAAY,QAAQ,EAAE,MAAM,eAAe,CAAC;AACnD,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAE3D,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AA6B7C;;;;GAIG;AACH,MAAM,OAAO,WAAW;IACtB;;;;;OAKG;IACI,MAAM,CAAC,mBAAmB,CAAC,GAAW;QAC3C,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,CAAC;YAChC,MAAM,IAAI,aAAa,CAAC,4BAA4B,EAAE,EAAE,IAAI,EAAE,cAAc,EAAE,CAAC,CAAC;QAClF,CAAC;QACD,OAAO,GAAG,CAAC,OAAO,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,mDAAmD;IACvG,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,eAAe,CAAC,GAAyB;QACrD,2DAA2D;QAC3D,IAAG,CAAC,YAAY,CAAC,YAAY,CAAC,GAAG,CAAC;YAAE,OAAO,KAAK,CAAC;QAEjD,+DAA+D;QAC/D,IAAG,CAAC,CAAC,iBAAiB,EAAE,oBAAoB,EAAE,oBAAoB,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC;YAAE,OAAO,KAAK,CAAC;QAErG,4EAA4E;QAC5E,IAAI,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC,IAAI,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC,OAAO,EAAE,KAAK,Q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1,61 @@
1
+ import { DidDocumentError, INVALID_DID_DOCUMENT } from '@did-btcr2/common';
2
+ import { Btc1DidDocument } from './did-document.js';
3
+ export class Btc1DidDocumentBuilder {
4
+ document = {};
5
+ constructor(initialDocument) {
6
+ if (!initialDocument.id) {
7
+ throw new DidDocumentError('Missing required "id" property', INVALID_DID_DOCUMENT, initialDocument);
8
+ }
9
+ this.document.id = initialDocument.id;
10
+ this.document.verificationMethod = initialDocument.verificationMethod ?? [];
11
+ if (initialDocument['@context']) {
12
+ this.document['@context'] = initialDocument['@context'];
13
+ }
14
+ }
15
+ withController(controller) {
16
+ if (controller) {
17
+ this.document.controller = controller ?? [this.document.id];
18
+ }
19
+ return this;
20
+ }
21
+ withAuthentication(authentication) {
22
+ if (authentication) {
23
+ this.document.authentication = authentication;
24
+ }
25
+ return this;
26
+ }
27
+ withAssertionMethod(assertionMethod) {
28
+ if (assertionMethod) {
29
+ this.document.assertionMethod = assertionMethod;
30
+ }
31
+ return this;
32
+ }
33
+ withCapabilityInvocation(capabilityInvocation) {
34
+ if (capabilityInvocation) {
35
+ this.document.capabilityInvocation = capabilityInvocation;
36
+ }
37
+ return this;
38
+ }
39
+ withCapabilityDelegation(capabilityDelegation) {
40
+ if (capabilityDelegation) {
41
+ this.document.capabilityDelegation = capabilityDelegation;
42
+ }
43
+ return this;
44
+ }
45
+ withService(service) {
46
+ if (service) {
47
+ this.document.service = service;
48
+ }
49
+ return this;
50
+ }
51
+ build() {
52
+ const didDocument = new Btc1DidDocument(this.document);
53
+ for (const key of Object.keys(didDocument)) {
54
+ if (didDocument[key] === undefined) {
55
+ delete didDocument[key];
56
+ }
57
+ }
58
+ return didDocument;
59
+ }
60
+ }
61
+ //# sourceMappingURL=did-document-builder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"did-document-builder.js","sourceRoot":"","sources":["../../../src/utils/did-document-builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,oBAAoB,EAAE,MAAM,mBA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D,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAED,wBAAwB,CAAC,oBAA4D;QACnF,IAAI,oBAAoB,EAAE,CAAC;YACzB,IAAI,CAAC,QAAQ,CAAC,oBAAoB,GAAG,oBAAoB,CAAC;QAC5D,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAED,WAAW,CAAC,OAA6B;QACvC,IAAI,OAAO,EAAE,CAAC;YACZ,IAAI,CAAC,QAAQ,CAAC,OAAO,GAAG,OAAO,CAAC;QAClC,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAED,KAAK;QACH,MAAM,WAAW,GAAG,IAAI,eAAe,CAAC,IAAI,CAAC,QAA2B,CAAC,CAAC;QAE1E,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,EAAE,CAAC;YAC3C,IAAI,WAAW,CAAC,GAA4B,CAAC,KAAK,SAAS,EAAE,CAAC;gBAC5D,OAAO,WAAW,CAAC,GAA4B,CAAC,CAAC;YACnD,CAAC;QACH,CAAC;QAED,OAAO,WAAW,CAAC;IACrB,CAAC;CACF"}
@@ -0,0 +1,380 @@
1
+ import { BTCR2_DID_DOCUMENT_CONTEXT, Btcr2IdentifierTypes, DidDocumentError, ID_PLACEHOLDER_VALUE, INVALID_DID_DOCUMENT, Logger } from '@did-btcr2/common';
2
+ import { Btc1Appendix } from './appendix.js';
3
+ import { BeaconUtils } from './beacons.js';
4
+ import { Btc1Identifier } from './identifier.js';
5
+ export const BECH32M_CHARS = '';
6
+ export const BTC1_DID_REGEX = /did:btcr2:(x1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]*)/g;
7
+ /**
8
+ * DID BTCR2 Verification Method extends the DidVerificationMethod class adding helper methods and properties
9
+ * @class Btc1VerificationMethod
10
+ * @type {Btc1VerificationMethod}
11
+ *
12
+ */
13
+ export class Btc1VerificationMethod {
14
+ id;
15
+ type;
16
+ controller;
17
+ publicKeyMultibase;
18
+ secretKeyMultibase;
19
+ constructor({ id, type, controller, publicKeyMultibase, secretKeyMultibase }) {
20
+ this.id = id;
21
+ this.type = type;
22
+ this.controller = controller;
23
+ this.publicKeyMultibase = publicKeyMultibase;
24
+ this.secretKeyMultibase = secretKeyMultibase;
25
+ if (!secretKeyMultibase) {
26
+ delete this.secretKeyMultibase;
27
+ }
28
+ }
29
+ }
30
+ /**
31
+ * BTCR2 DID Document extends the DidDocument class adding helper methods and properties
32
+ * @class Btc1DidDocument
33
+ * @type {Btc1DidDocument}
34
+ * @implements {IBtc1DidDocument}
35
+ * @property {string} id - The identifier of the DID Document.
36
+ * @property {Array<string>} [controller] - The controller of the DID Document.
37
+ * @property {Array<string | JSONObject>} ['@context'] - The context of the DID Document.
38
+ * @property {Array<DidVerificationMethod>} verificationMethod - The verification methods of the DID Document.
39
+ * @property {Array<string | Btc1VerificationMethod>} [authentication] - The authentication methods of the DID Document.
40
+ * @property {Array<string | Btc1VerificationMethod>} [assertionMethod] - The assertion methods of the DID Document.
41
+ * @property {Array<string | Btc1VerificationMethod>} [capabilityInvocation] - The capability invocation methods of the DID Document.
42
+ * @property {Array<string | Btc1VerificationMethod>} [capabilityDelegation] - The capability delegation methods of the DID Document.
43
+ * @property {Array<BeaconService>} service - The services of the DID Document.
44
+ */
45
+ export class Btc1DidDocument {
46
+ id;
47
+ controller;
48
+ '@context' = BTCR2_DID_DOCUMENT_CONTEXT;
49
+ verificationMethod;
50
+ authentication;
51
+ assertionMethod;
52
+ capabilityInvocation;
53
+ capabilityDelegation;
54
+ service;
55
+ constructor(document) {
56
+ // Set the ID and ID type
57
+ const idType = document.id.includes('k1')
58
+ ? Btcr2IdentifierTypes.KEY
59
+ : Btcr2IdentifierTypes.EXTERNAL;
60
+ // Validate ID and parts for non-intermediate
61
+ const isIntermediate = document.id === ID_PLACEHOLDER_VALUE;
62
+ // Deconstruct the document parts for validation
63
+ const { id, controller, verificationMethod: vm, service } = document;
64
+ if (!isIntermediate) {
65
+ if (!Btc1DidDocument.isValidId(id)) {
66
+ throw new DidDocumentError(`Invalid id: ${id}`, INVALID_DID_DOCUMENT, document);
67
+ }
68
+ if (!Btc1DidDocument.isValidController(controller ?? [id])) {
69
+ throw new DidDocumentError(`Invalid controller: ${controller}`, INVALID_DID_DOCUMENT, document);
70
+ }
71
+ if (!Btc1DidDocument.isValidVerificationMethods(vm)) {
72
+ throw new DidDocumentError('Invalid verificationMethod: ' + vm, INVALID_DID_DOCUMENT, document);
73
+ }
74
+ if (!Btc1DidDocument.isValidServices(service)) {
75
+ throw new DidDocumentError('Invalid service: ' + service, INVALID_DID_DOCUMENT, document);
76
+ }
77
+ }
78
+ // Set core properties
79
+ this.id = document.id;
80
+ this.verificationMethod = document.verificationMethod;
81
+ this.service = document.service;
82
+ this['@context'] = document['@context'] || BTCR2_DID_DOCUMENT_CONTEXT;
83
+ this.controller = document.controller || [this.id];
84
+ // Relationships logic based on idType
85
+ if (idType === Btcr2IdentifierTypes.KEY) {
86
+ // auto-generate #initialKey if missing
87
+ const keyRef = `${this.id}#initialKey`;
88
+ this.authentication = document.authentication || [keyRef];
89
+ this.assertionMethod = document.assertionMethod || [keyRef];
90
+ this.capabilityInvocation = document.capabilityInvocation || [keyRef];
91
+ this.capabilityDelegation = document.capabilityDelegation || [keyRef];
92
+ }
93
+ else {
94
+ // EXTERNAL: use provided arrays, must be defined
95
+ this.authentication = document.authentication;
96
+ this.assertionMethod = document.assertionMethod;
97
+ this.capabilityInvocation = document.capabilityInvocation;
98
+ this.capabilityDelegation = document.capabilityDelegation;
99
+ }
100
+ // Sanitize the DID Document
101
+ Btc1DidDocument.sanitize(this);
102
+ // If the DID Document is not an intermediateDocument, validate it
103
+ if (!isIntermediate) {
104
+ Btc1DidDocument.validate(this);
105
+ }
106
+ else {
107
+ this.validateIntermediate();
108
+ }
109
+ }
110
+ /**
111
+ * Convert the Btc1DidDocument to a JSON object.
112
+ * @returns {JSONObject} The JSON representation of the Btc1DidDocument.
113
+ */
114
+ json() {
115
+ return {
116
+ id: this.id,
117
+ controller: this.controller,
118
+ '@context': this['@context'],
119
+ verificationMethod: this.verificationMethod,
120
+ authentication: this.authentication,
121
+ assertionMethod: this.assertionMethod,
122
+ capabilityInvocation: this.capabilityInvocation,
123
+ capabilityDelegation: this.capabilityDelegation,
124
+ service: this.service
125
+ };
126
+ }
127
+ /**
128
+ * Create a minimal Btc1DidDocument from "k1" btcr2 identifier.
129
+ * @param {string} publicKeyMultibase The public key in multibase format.
130
+ * @param {Array<BeaconService>} service The beacon services to be included in the document.
131
+ * @returns {Btc1DidDocument} A new Btc1DidDocument with the placeholder ID.
132
+ */
133
+ static fromKeyIdentifier(id, publicKeyMultibase, service) {
134
+ // Ensure the ID is in the correct format
135
+ id = id.includes('#') ? id : `${id}#initialKey`;
136
+ // Create the verification method and the Btc1DidDocument
137
+ const document = {
138
+ id,
139
+ verificationMethod: [
140
+ new Btc1VerificationMethod({
141
+ id,
142
+ type: 'Multikey',
143
+ controller: id,
144
+ publicKeyMultibase
145
+ })
146
+ ],
147
+ service
148
+ };
149
+ return new Btc1DidDocument(document);
150
+ }
151
+ /**
152
+ * Create a Btc1DidDocument from "x1" btcr2 identifier.
153
+ * @param {ExternalData} data The verification methods of the DID Document.
154
+ * @returns {Btc1DidDocument} A new Btc1DidDocument.
155
+ */
156
+ static fromExternalIdentifier(data) {
157
+ return new Btc1DidDocument(data);
158
+ }
159
+ /**
160
+ * Sanitize the DID Document by removing undefined values
161
+ * @returns {Btc1DidDocument} The sanitized DID Document
162
+ */
163
+ static sanitize(doc) {
164
+ for (const key of Object.keys(doc)) {
165
+ if (doc[key] === undefined) {
166
+ delete doc[key];
167
+ }
168
+ }
169
+ return doc;
170
+ }
171
+ /**
172
+ * Validates a Btc1DidDocument by breaking it into modular validation methods.
173
+ * @param {Btc1DidDocument} didDocument The DID document to validate.
174
+ * @returns {boolean} True if the DID document is valid.
175
+ * @throws {DidDocumentError} If any validation check fails.
176
+ */
177
+ static isValid(didDocument) {
178
+ if (!this.isValidContext(didDocument?.['@context'])) {
179
+ throw new DidDocumentError('Invalid "@context"', INVALID_DID_DOCUMENT, didDocument);
180
+ }
181
+ if (!this.isValidId(didDocument?.id)) {
182
+ throw new DidDocumentError('Invalid "id"', INVALID_DID_DOCUMENT, didDocument);
183
+ }
184
+ if (!this.isValidVerificationMethods(didDocument?.verificationMethod)) {
185
+ throw new DidDocumentError('Invalid "verificationMethod"', INVALID_DID_DOCUMENT, didDocument);
186
+ }
187
+ if (!this.isValidServices(didDocument?.service)) {
188
+ throw new DidDocumentError('Invalid "service"', INVALID_DID_DOCUMENT, didDocument);
189
+ }
190
+ if (!this.isValidVerificationRelationships(didDocument)) {
191
+ throw new DidDocumentError('Invalid verification relationships', INVALID_DID_DOCUMENT, didDocument);
192
+ }
193
+ return true;
194
+ }
195
+ /**
196
+ * Validates that "@context" exists and includes correct values.
197
+ * @private
198
+ * @param {Btc1DidDocument['@context']} context The context to validate.
199
+ * @returns {boolean} True if the context is valid.
200
+ */
201
+ static isValidContext(context) {
202
+ if (!context)
203
+ return false;
204
+ if (!Array.isArray(context))
205
+ return false;
206
+ if (!context.every(ctx => typeof ctx === 'string' && BTCR2_DID_DOCUMENT_CONTEXT.includes(ctx)))
207
+ return false;
208
+ return true;
209
+ }
210
+ /**
211
+ * Validates that the DID Document has a valid id.
212
+ * @private
213
+ * @param {string} id The id to validate.
214
+ * @returns {boolean} True if the id is valid.
215
+ */
216
+ static isValidId(id) {
217
+ try {
218
+ Btc1Identifier.decode(id);
219
+ return true;
220
+ }
221
+ catch (error) {
222
+ Logger.error('Invalid DID Document ID', error);
223
+ return false;
224
+ }
225
+ }
226
+ /**
227
+ * Validates that the controller exists and is correctly formatted.
228
+ * @param {Array<string>} controller The controller to validate.
229
+ * @returns {boolean} True if the controller is valid.
230
+ */
231
+ static isValidController(controller) {
232
+ if (!controller)
233
+ return false;
234
+ if (!Array.isArray(controller))
235
+ return false;
236
+ if (!controller.every(c => typeof c === 'string'))
237
+ return false;
238
+ return true;
239
+ }
240
+ /**
241
+ * Validates that verification methods exist and are correctly formatted.
242
+ * @private
243
+ * @param {DidVerificationMethod[]} verificationMethod The verification methods to validate.
244
+ * @returns {boolean} True if the verification methods are valid.
245
+ */
246
+ static isValidVerificationMethods(verificationMethod) {
247
+ return Array.isArray(verificationMethod) && verificationMethod.every(Btc1Appendix.isDidVerificationMethod);
248
+ }
249
+ /**
250
+ * Validates that the DID Document has valid services.
251
+ * @private
252
+ * @param {DidService[]} service The services to validate.
253
+ * @returns {boolean} True if the services are valid.
254
+ */
255
+ static isValidServices(service) {
256
+ return Array.isArray(service) && service.every(BeaconUtils.isBeaconService);
257
+ }
258
+ /**
259
+ * Validates verification relationships (authentication, assertionMethod, capabilityInvocation, capabilityDelegation).
260
+ * @private
261
+ * @param {Btc1DidDocument} didDocument The DID Document to validate.
262
+ * @returns {boolean} True if the verification relationships are valid.
263
+ */
264
+ static isValidVerificationRelationships(didDocument) {
265
+ // Define the available verification relationships
266
+ const possibleVerificationRelationships = [
267
+ 'authentication',
268
+ 'assertionMethod',
269
+ 'capabilityInvocation',
270
+ 'capabilityDelegation'
271
+ ];
272
+ // Get the DID Document keys
273
+ const verificationRelationships = Object.keys(didDocument);
274
+ // Filter the DID Document keys to only those that are in the available verification relationships
275
+ const availableVerificationRelationships = possibleVerificationRelationships.filter(key => verificationRelationships.includes(key));
276
+ // Check if all available verification relationships are valid
277
+ return availableVerificationRelationships.every((key) =>
278
+ // Check if the key exists in the DID Document
279
+ didDocument[key] &&
280
+ // Check if the key is an array
281
+ Array.isArray(didDocument[key]) &&
282
+ // Check that every value in the array is a string or DidVerificationMethod
283
+ didDocument[key].every(entry => typeof entry === 'string' || Btc1Appendix.isDidVerificationMethod(entry)));
284
+ }
285
+ /**
286
+ * Validate the DID Document
287
+ * @returns {Btc1DidDocument} Validated DID Document.
288
+ * @throws {DidDocumentError} If the DID Document is invalid.
289
+ */
290
+ static validate(didDocument) {
291
+ // Validate the DID Document
292
+ if (didDocument.id === ID_PLACEHOLDER_VALUE) {
293
+ didDocument.validateIntermediate();
294
+ }
295
+ else {
296
+ Btc1DidDocument.isValid(didDocument);
297
+ }
298
+ // Return the DID Document
299
+ return didDocument;
300
+ }
301
+ /**
302
+ * Validate the IntermediateDidDocument.
303
+ * @returns {boolean} True if the IntermediateDidDocument is valid.
304
+ */
305
+ validateIntermediate() {
306
+ // Validate the id
307
+ if (this.id !== ID_PLACEHOLDER_VALUE) {
308
+ throw new DidDocumentError('Invalid IntermediateDidDocument ID', INVALID_DID_DOCUMENT, this);
309
+ }
310
+ // Validate the controller
311
+ if (!this.controller?.every(c => c === ID_PLACEHOLDER_VALUE)) {
312
+ throw new DidDocumentError('Invalid IntermediateDidDocument controller', INVALID_DID_DOCUMENT, this);
313
+ }
314
+ // Validate the verificationMethod
315
+ if (!this.verificationMethod.every(vm => vm.id.includes(ID_PLACEHOLDER_VALUE) && vm.controller === ID_PLACEHOLDER_VALUE)) {
316
+ throw new DidDocumentError('Invalid IntermediateDidDocument verificationMethod', INVALID_DID_DOCUMENT, this);
317
+ }
318
+ // Validate the service
319
+ if (!this.service.every(svc => svc.id.includes(ID_PLACEHOLDER_VALUE))) {
320
+ throw new DidDocumentError('Invalid IntermediateDidDocument service', INVALID_DID_DOCUMENT, this);
321
+ }
322
+ if (!Btc1DidDocument.isValidVerificationRelationships(this)) {
323
+ // Return true if all validations pass
324
+ throw new DidDocumentError('Invalid IntermediateDidDocument assertionMethod', INVALID_DID_DOCUMENT, this);
325
+ }
326
+ }
327
+ /**
328
+ * Convert the Btc1DidDocument to an IntermediateDidDocument.
329
+ * @returns {IntermediateDidDocument} The IntermediateDidDocument representation of the Btc1DidDocument.
330
+ */
331
+ toIntermediate() {
332
+ if (this.id.includes('k1')) {
333
+ throw new DidDocumentError('Cannot convert a key identifier to an intermediate document', INVALID_DID_DOCUMENT, this);
334
+ }
335
+ return new IntermediateDidDocument(this);
336
+ }
337
+ }
338
+ /**
339
+ * IntermediateDidDocument extends the Btc1DidDocument class for creating and managing intermediate DID documents.
340
+ * This class is used to create a minimal DID document with a placeholder ID.
341
+ * It is used in the process of creating a new DID document.
342
+ * @class IntermediateDidDocument
343
+ * @extends {Btc1DidDocument}
344
+ */
345
+ export class IntermediateDidDocument extends Btc1DidDocument {
346
+ constructor(document) {
347
+ const intermediateDocument = JSON.cloneReplace(document, BTC1_DID_REGEX, ID_PLACEHOLDER_VALUE);
348
+ super(intermediateDocument);
349
+ }
350
+ /**
351
+ * Create a minimal IntermediateDidDocument with a placeholder ID.
352
+ * @param {Array<Btc1VerificationMethod>} verificationMethod The public key in multibase format.
353
+ * @param {VerificationRelationships} relationships The public key in multibase format.
354
+ * @param {Array<BeaconService>} service The service to be included in the document.
355
+ * @returns {IntermediateDidDocument} A new IntermediateDidDocument with the placeholder ID.
356
+ */
357
+ static create(verificationMethod, relationships, service) {
358
+ const id = ID_PLACEHOLDER_VALUE;
359
+ return new IntermediateDidDocument({ id, ...relationships, verificationMethod, service, });
360
+ }
361
+ /**
362
+ * Convert the IntermediateDidDocument to a Btc1DidDocument by replacing the placeholder value with the provided DID.
363
+ * @param did The DID to replace the placeholder value in the document.
364
+ * @returns {Btc1DidDocument} A new Btc1DidDocument with the placeholder value replaced by the provided DID.
365
+ */
366
+ toBtc1DidDocument(did) {
367
+ const stringThis = JSON.stringify(this).replaceAll(ID_PLACEHOLDER_VALUE, did);
368
+ const parseThis = JSON.parse(stringThis);
369
+ return new Btc1DidDocument(parseThis);
370
+ }
371
+ /**
372
+ * Create a Btc1DidDocument from a JSON object.
373
+ * @param {JSONObject} object The JSON object to convert.
374
+ * @returns {Btc1DidDocument} The created Btc1DidDocument.
375
+ */
376
+ static from(object) {
377
+ return new IntermediateDidDocument(object).toBtc1DidDocument(object.id);
378
+ }
379
+ }
380
+ //# sourceMappingURL=did-document.js.map