@did-btcr2/method 0.18.0 → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (262) hide show
  1. package/dist/browser.js +5240 -6079
  2. package/dist/browser.mjs +5240 -6079
  3. package/dist/cjs/core/beacon/aggregation/cohort/index.js +3 -3
  4. package/dist/cjs/core/beacon/aggregation/cohort/index.js.map +1 -1
  5. package/dist/cjs/core/beacon/aggregation/cohort/messages/base.js +1 -9
  6. package/dist/cjs/core/beacon/aggregation/cohort/messages/base.js.map +1 -1
  7. package/dist/cjs/core/beacon/aggregation/cohort/messages/keygen/cohort-advert.js +2 -1
  8. package/dist/cjs/core/beacon/aggregation/cohort/messages/keygen/cohort-advert.js.map +1 -1
  9. package/dist/cjs/core/beacon/aggregation/cohort/messages/keygen/cohort-ready.js +2 -1
  10. package/dist/cjs/core/beacon/aggregation/cohort/messages/keygen/cohort-ready.js.map +1 -1
  11. package/dist/cjs/core/beacon/aggregation/cohort/messages/keygen/opt-in-accept.js +2 -1
  12. package/dist/cjs/core/beacon/aggregation/cohort/messages/keygen/opt-in-accept.js.map +1 -1
  13. package/dist/cjs/core/beacon/aggregation/cohort/messages/keygen/opt-in.js +2 -1
  14. package/dist/cjs/core/beacon/aggregation/cohort/messages/keygen/opt-in.js.map +1 -1
  15. package/dist/cjs/core/beacon/aggregation/cohort/messages/keygen/subscribe.js +2 -1
  16. package/dist/cjs/core/beacon/aggregation/cohort/messages/keygen/subscribe.js.map +1 -1
  17. package/dist/cjs/core/beacon/aggregation/cohort/messages/sign/aggregated-nonce.js +2 -1
  18. package/dist/cjs/core/beacon/aggregation/cohort/messages/sign/aggregated-nonce.js.map +1 -1
  19. package/dist/cjs/core/beacon/aggregation/cohort/messages/sign/authorization-request.js +2 -1
  20. package/dist/cjs/core/beacon/aggregation/cohort/messages/sign/authorization-request.js.map +1 -1
  21. package/dist/cjs/core/beacon/aggregation/cohort/messages/sign/nonce-contribution.js +2 -1
  22. package/dist/cjs/core/beacon/aggregation/cohort/messages/sign/nonce-contribution.js.map +1 -1
  23. package/dist/cjs/core/beacon/aggregation/cohort/messages/sign/request-signature.js +2 -1
  24. package/dist/cjs/core/beacon/aggregation/cohort/messages/sign/request-signature.js.map +1 -1
  25. package/dist/cjs/core/beacon/aggregation/cohort/messages/sign/signature-authorization.js +2 -1
  26. package/dist/cjs/core/beacon/aggregation/cohort/messages/sign/signature-authorization.js.map +1 -1
  27. package/dist/cjs/core/beacon/aggregation/communication/adapter/nostr.js +36 -18
  28. package/dist/cjs/core/beacon/aggregation/communication/adapter/nostr.js.map +1 -1
  29. package/dist/cjs/core/beacon/aggregation/coordinator.js +14 -14
  30. package/dist/cjs/core/beacon/aggregation/coordinator.js.map +1 -1
  31. package/dist/cjs/core/beacon/aggregation/participant.js +4 -3
  32. package/dist/cjs/core/beacon/aggregation/participant.js.map +1 -1
  33. package/dist/cjs/core/beacon/aggregation/session/index.js +1 -1
  34. package/dist/cjs/core/beacon/aggregation/session/index.js.map +1 -1
  35. package/dist/cjs/core/beacon/cas-beacon.js +55 -0
  36. package/dist/cjs/core/beacon/cas-beacon.js.map +1 -0
  37. package/dist/cjs/core/beacon/factory.js +11 -10
  38. package/dist/cjs/core/beacon/factory.js.map +1 -1
  39. package/dist/cjs/core/beacon/interfaces.js +32 -0
  40. package/dist/cjs/core/beacon/interfaces.js.map +1 -0
  41. package/dist/cjs/core/beacon/singleton.js +59 -135
  42. package/dist/cjs/core/beacon/singleton.js.map +1 -1
  43. package/dist/cjs/core/beacon/smt-beacon.js +56 -0
  44. package/dist/cjs/core/beacon/smt-beacon.js.map +1 -0
  45. package/dist/cjs/core/beacon/utils.js +67 -105
  46. package/dist/cjs/core/beacon/utils.js.map +1 -1
  47. package/dist/cjs/core/identifier.js +18 -21
  48. package/dist/cjs/core/identifier.js.map +1 -1
  49. package/dist/cjs/core/interfaces.js +2 -0
  50. package/dist/cjs/core/interfaces.js.map +1 -0
  51. package/dist/cjs/core/resolve.js +511 -0
  52. package/dist/cjs/core/resolve.js.map +1 -0
  53. package/dist/cjs/{utils → core}/types.js.map +1 -1
  54. package/dist/cjs/core/{crud/update.js → update.js} +62 -52
  55. package/dist/cjs/core/update.js.map +1 -0
  56. package/dist/cjs/did-btcr2.js +109 -75
  57. package/dist/cjs/did-btcr2.js.map +1 -1
  58. package/dist/cjs/index.js +14 -15
  59. package/dist/cjs/index.js.map +1 -1
  60. package/dist/cjs/utils/appendix.js +10 -18
  61. package/dist/cjs/utils/appendix.js.map +1 -1
  62. package/dist/cjs/utils/did-document.js +51 -58
  63. package/dist/cjs/utils/did-document.js.map +1 -1
  64. package/dist/cjs/utils/general.js +1 -1
  65. package/dist/cjs/utils/general.js.map +1 -1
  66. package/dist/esm/core/beacon/aggregation/cohort/index.js +3 -3
  67. package/dist/esm/core/beacon/aggregation/cohort/index.js.map +1 -1
  68. package/dist/esm/core/beacon/aggregation/cohort/messages/base.js +1 -9
  69. package/dist/esm/core/beacon/aggregation/cohort/messages/base.js.map +1 -1
  70. package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/cohort-advert.js +2 -1
  71. package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/cohort-advert.js.map +1 -1
  72. package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/cohort-ready.js +2 -1
  73. package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/cohort-ready.js.map +1 -1
  74. package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/opt-in-accept.js +2 -1
  75. package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/opt-in-accept.js.map +1 -1
  76. package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/opt-in.js +2 -1
  77. package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/opt-in.js.map +1 -1
  78. package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/subscribe.js +2 -1
  79. package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/subscribe.js.map +1 -1
  80. package/dist/esm/core/beacon/aggregation/cohort/messages/sign/aggregated-nonce.js +2 -1
  81. package/dist/esm/core/beacon/aggregation/cohort/messages/sign/aggregated-nonce.js.map +1 -1
  82. package/dist/esm/core/beacon/aggregation/cohort/messages/sign/authorization-request.js +2 -1
  83. package/dist/esm/core/beacon/aggregation/cohort/messages/sign/authorization-request.js.map +1 -1
  84. package/dist/esm/core/beacon/aggregation/cohort/messages/sign/nonce-contribution.js +2 -1
  85. package/dist/esm/core/beacon/aggregation/cohort/messages/sign/nonce-contribution.js.map +1 -1
  86. package/dist/esm/core/beacon/aggregation/cohort/messages/sign/request-signature.js +2 -1
  87. package/dist/esm/core/beacon/aggregation/cohort/messages/sign/request-signature.js.map +1 -1
  88. package/dist/esm/core/beacon/aggregation/cohort/messages/sign/signature-authorization.js +2 -1
  89. package/dist/esm/core/beacon/aggregation/cohort/messages/sign/signature-authorization.js.map +1 -1
  90. package/dist/esm/core/beacon/aggregation/communication/adapter/nostr.js +36 -18
  91. package/dist/esm/core/beacon/aggregation/communication/adapter/nostr.js.map +1 -1
  92. package/dist/esm/core/beacon/aggregation/coordinator.js +14 -14
  93. package/dist/esm/core/beacon/aggregation/coordinator.js.map +1 -1
  94. package/dist/esm/core/beacon/aggregation/participant.js +4 -3
  95. package/dist/esm/core/beacon/aggregation/participant.js.map +1 -1
  96. package/dist/esm/core/beacon/aggregation/session/index.js +1 -1
  97. package/dist/esm/core/beacon/aggregation/session/index.js.map +1 -1
  98. package/dist/esm/core/beacon/cas-beacon.js +55 -0
  99. package/dist/esm/core/beacon/cas-beacon.js.map +1 -0
  100. package/dist/esm/core/beacon/factory.js +11 -10
  101. package/dist/esm/core/beacon/factory.js.map +1 -1
  102. package/dist/esm/core/beacon/interfaces.js +32 -0
  103. package/dist/esm/core/beacon/interfaces.js.map +1 -0
  104. package/dist/esm/core/beacon/singleton.js +59 -135
  105. package/dist/esm/core/beacon/singleton.js.map +1 -1
  106. package/dist/esm/core/beacon/smt-beacon.js +56 -0
  107. package/dist/esm/core/beacon/smt-beacon.js.map +1 -0
  108. package/dist/esm/core/beacon/utils.js +67 -105
  109. package/dist/esm/core/beacon/utils.js.map +1 -1
  110. package/dist/esm/core/identifier.js +18 -21
  111. package/dist/esm/core/identifier.js.map +1 -1
  112. package/dist/esm/core/interfaces.js +2 -0
  113. package/dist/esm/core/interfaces.js.map +1 -0
  114. package/dist/esm/core/resolve.js +511 -0
  115. package/dist/esm/core/resolve.js.map +1 -0
  116. package/dist/esm/{utils → core}/types.js.map +1 -1
  117. package/dist/esm/core/{crud/update.js → update.js} +62 -52
  118. package/dist/esm/core/update.js.map +1 -0
  119. package/dist/esm/did-btcr2.js +109 -75
  120. package/dist/esm/did-btcr2.js.map +1 -1
  121. package/dist/esm/index.js +14 -15
  122. package/dist/esm/index.js.map +1 -1
  123. package/dist/esm/utils/appendix.js +10 -18
  124. package/dist/esm/utils/appendix.js.map +1 -1
  125. package/dist/esm/utils/did-document.js +51 -58
  126. package/dist/esm/utils/did-document.js.map +1 -1
  127. package/dist/esm/utils/general.js +1 -1
  128. package/dist/esm/utils/general.js.map +1 -1
  129. package/dist/types/core/beacon/aggregation/cohort/index.d.ts +1 -1
  130. package/dist/types/core/beacon/aggregation/cohort/messages/base.d.ts +0 -7
  131. package/dist/types/core/beacon/aggregation/cohort/messages/base.d.ts.map +1 -1
  132. package/dist/types/core/beacon/aggregation/cohort/messages/keygen/cohort-advert.d.ts.map +1 -1
  133. package/dist/types/core/beacon/aggregation/cohort/messages/keygen/cohort-ready.d.ts.map +1 -1
  134. package/dist/types/core/beacon/aggregation/cohort/messages/keygen/opt-in-accept.d.ts.map +1 -1
  135. package/dist/types/core/beacon/aggregation/cohort/messages/keygen/opt-in.d.ts.map +1 -1
  136. package/dist/types/core/beacon/aggregation/cohort/messages/keygen/subscribe.d.ts.map +1 -1
  137. package/dist/types/core/beacon/aggregation/cohort/messages/sign/aggregated-nonce.d.ts.map +1 -1
  138. package/dist/types/core/beacon/aggregation/cohort/messages/sign/authorization-request.d.ts.map +1 -1
  139. package/dist/types/core/beacon/aggregation/cohort/messages/sign/nonce-contribution.d.ts.map +1 -1
  140. package/dist/types/core/beacon/aggregation/cohort/messages/sign/request-signature.d.ts.map +1 -1
  141. package/dist/types/core/beacon/aggregation/cohort/messages/sign/signature-authorization.d.ts.map +1 -1
  142. package/dist/types/core/beacon/aggregation/communication/adapter/nostr.d.ts +13 -5
  143. package/dist/types/core/beacon/aggregation/communication/adapter/nostr.d.ts.map +1 -1
  144. package/dist/types/core/beacon/aggregation/coordinator.d.ts +17 -29
  145. package/dist/types/core/beacon/aggregation/coordinator.d.ts.map +1 -1
  146. package/dist/types/core/beacon/aggregation/participant.d.ts.map +1 -1
  147. package/dist/types/core/beacon/cas-beacon.d.ts +47 -0
  148. package/dist/types/core/beacon/cas-beacon.d.ts.map +1 -0
  149. package/dist/types/core/beacon/factory.d.ts +5 -4
  150. package/dist/types/core/beacon/factory.d.ts.map +1 -1
  151. package/dist/types/core/beacon/interfaces.d.ts +98 -0
  152. package/dist/types/core/beacon/interfaces.d.ts.map +1 -0
  153. package/dist/types/core/beacon/singleton.d.ts +22 -65
  154. package/dist/types/core/beacon/singleton.d.ts.map +1 -1
  155. package/dist/types/core/beacon/smt-beacon.d.ts +48 -0
  156. package/dist/types/core/beacon/smt-beacon.d.ts.map +1 -0
  157. package/dist/types/core/beacon/utils.d.ts +19 -97
  158. package/dist/types/core/beacon/utils.d.ts.map +1 -1
  159. package/dist/types/core/identifier.d.ts.map +1 -1
  160. package/dist/types/core/interfaces.d.ts +94 -0
  161. package/dist/types/core/interfaces.d.ts.map +1 -0
  162. package/dist/types/core/resolve.d.ts +105 -0
  163. package/dist/types/core/resolve.d.ts.map +1 -0
  164. package/dist/types/core/types.d.ts +71 -0
  165. package/dist/types/core/types.d.ts.map +1 -0
  166. package/dist/types/core/{crud/update.d.ts → update.d.ts} +21 -20
  167. package/dist/types/core/update.d.ts.map +1 -0
  168. package/dist/types/did-btcr2.d.ts +45 -57
  169. package/dist/types/did-btcr2.d.ts.map +1 -1
  170. package/dist/types/index.d.ts +14 -15
  171. package/dist/types/index.d.ts.map +1 -1
  172. package/dist/types/utils/appendix.d.ts +5 -10
  173. package/dist/types/utils/appendix.d.ts.map +1 -1
  174. package/dist/types/utils/did-document-builder.d.ts +1 -1
  175. package/dist/types/utils/did-document-builder.d.ts.map +1 -1
  176. package/dist/types/utils/did-document.d.ts +31 -29
  177. package/dist/types/utils/did-document.d.ts.map +1 -1
  178. package/package.json +5 -5
  179. package/src/core/beacon/aggregation/cohort/index.ts +3 -3
  180. package/src/core/beacon/aggregation/cohort/messages/base.ts +1 -12
  181. package/src/core/beacon/aggregation/cohort/messages/keygen/cohort-advert.ts +2 -2
  182. package/src/core/beacon/aggregation/cohort/messages/keygen/cohort-ready.ts +2 -2
  183. package/src/core/beacon/aggregation/cohort/messages/keygen/opt-in-accept.ts +2 -2
  184. package/src/core/beacon/aggregation/cohort/messages/keygen/opt-in.ts +2 -2
  185. package/src/core/beacon/aggregation/cohort/messages/keygen/subscribe.ts +2 -2
  186. package/src/core/beacon/aggregation/cohort/messages/sign/aggregated-nonce.ts +2 -2
  187. package/src/core/beacon/aggregation/cohort/messages/sign/authorization-request.ts +2 -2
  188. package/src/core/beacon/aggregation/cohort/messages/sign/nonce-contribution.ts +2 -2
  189. package/src/core/beacon/aggregation/cohort/messages/sign/request-signature.ts +2 -2
  190. package/src/core/beacon/aggregation/cohort/messages/sign/signature-authorization.ts +2 -2
  191. package/src/core/beacon/aggregation/communication/adapter/nostr.ts +43 -21
  192. package/src/core/beacon/aggregation/coordinator.ts +41 -29
  193. package/src/core/beacon/aggregation/participant.ts +4 -3
  194. package/src/core/beacon/aggregation/session/index.ts +1 -1
  195. package/src/core/beacon/cas-beacon.ts +67 -0
  196. package/src/core/beacon/factory.ts +13 -15
  197. package/src/core/beacon/interfaces.ts +124 -0
  198. package/src/core/beacon/singleton.ts +75 -145
  199. package/src/core/beacon/smt-beacon.ts +70 -0
  200. package/src/core/beacon/utils.ts +80 -170
  201. package/src/core/identifier.ts +21 -24
  202. package/src/core/interfaces.ts +101 -0
  203. package/src/core/resolve.ts +707 -0
  204. package/src/core/types.ts +78 -0
  205. package/src/core/{crud/update.ts → update.ts} +75 -68
  206. package/src/did-btcr2.ts +152 -92
  207. package/src/index.ts +14 -24
  208. package/src/utils/appendix.ts +18 -22
  209. package/src/utils/did-document-builder.ts +1 -1
  210. package/src/utils/did-document.ts +67 -71
  211. package/src/utils/general.ts +1 -1
  212. package/dist/cjs/core/beacon/cid-aggregate.js +0 -116
  213. package/dist/cjs/core/beacon/cid-aggregate.js.map +0 -1
  214. package/dist/cjs/core/beacon/smt-aggregate.js +0 -126
  215. package/dist/cjs/core/beacon/smt-aggregate.js.map +0 -1
  216. package/dist/cjs/core/crud/deactivate.js +0 -14
  217. package/dist/cjs/core/crud/deactivate.js.map +0 -1
  218. package/dist/cjs/core/crud/read.js +0 -679
  219. package/dist/cjs/core/crud/read.js.map +0 -1
  220. package/dist/cjs/core/crud/update.js.map +0 -1
  221. package/dist/cjs/interfaces/beacon.js +0 -41
  222. package/dist/cjs/interfaces/beacon.js.map +0 -1
  223. package/dist/cjs/interfaces/crud.js +0 -2
  224. package/dist/cjs/interfaces/crud.js.map +0 -1
  225. package/dist/esm/core/beacon/cid-aggregate.js +0 -116
  226. package/dist/esm/core/beacon/cid-aggregate.js.map +0 -1
  227. package/dist/esm/core/beacon/smt-aggregate.js +0 -126
  228. package/dist/esm/core/beacon/smt-aggregate.js.map +0 -1
  229. package/dist/esm/core/crud/deactivate.js +0 -14
  230. package/dist/esm/core/crud/deactivate.js.map +0 -1
  231. package/dist/esm/core/crud/read.js +0 -679
  232. package/dist/esm/core/crud/read.js.map +0 -1
  233. package/dist/esm/core/crud/update.js.map +0 -1
  234. package/dist/esm/interfaces/beacon.js +0 -41
  235. package/dist/esm/interfaces/beacon.js.map +0 -1
  236. package/dist/esm/interfaces/crud.js +0 -2
  237. package/dist/esm/interfaces/crud.js.map +0 -1
  238. package/dist/types/core/beacon/cid-aggregate.d.ts +0 -102
  239. package/dist/types/core/beacon/cid-aggregate.d.ts.map +0 -1
  240. package/dist/types/core/beacon/smt-aggregate.d.ts +0 -111
  241. package/dist/types/core/beacon/smt-aggregate.d.ts.map +0 -1
  242. package/dist/types/core/crud/deactivate.d.ts +0 -13
  243. package/dist/types/core/crud/deactivate.d.ts.map +0 -1
  244. package/dist/types/core/crud/read.d.ts +0 -334
  245. package/dist/types/core/crud/read.d.ts.map +0 -1
  246. package/dist/types/core/crud/update.d.ts.map +0 -1
  247. package/dist/types/interfaces/beacon.d.ts +0 -116
  248. package/dist/types/interfaces/beacon.d.ts.map +0 -1
  249. package/dist/types/interfaces/crud.d.ts +0 -32
  250. package/dist/types/interfaces/crud.d.ts.map +0 -1
  251. package/dist/types/utils/types.d.ts +0 -38
  252. package/dist/types/utils/types.d.ts.map +0 -1
  253. package/src/canonicalize.d.ts +0 -6
  254. package/src/core/beacon/cid-aggregate.ts +0 -153
  255. package/src/core/beacon/smt-aggregate.ts +0 -135
  256. package/src/core/crud/deactivate.ts +0 -13
  257. package/src/core/crud/read.ts +0 -948
  258. package/src/interfaces/beacon.ts +0 -137
  259. package/src/interfaces/crud.ts +0 -33
  260. package/src/utils/types.ts +0 -41
  261. /package/dist/cjs/{utils → core}/types.js +0 -0
  262. /package/dist/esm/{utils → core}/types.js +0 -0
@@ -0,0 +1,707 @@
1
+ import {
2
+ BitcoinNetworkConnection,
3
+ BlockV3,
4
+ GENESIS_TX_ID,
5
+ getNetwork,
6
+ RawTransactionV2,
7
+ TXIN_WITNESS_COINBASE
8
+ } from '@did-btcr2/bitcoin';
9
+ import {
10
+ DateUtils,
11
+ IdentifierHrp,
12
+ INVALID_DID,
13
+ INVALID_DID_DOCUMENT,
14
+ INVALID_DID_UPDATE,
15
+ JSONPatch,
16
+ JSONUtils,
17
+ LATE_PUBLISHING_ERROR,
18
+ MethodError,
19
+ MISSING_UPDATE_DATA,
20
+ ResolveError
21
+ } from '@did-btcr2/common';
22
+ import {
23
+ BIP340Cryptosuite,
24
+ BIP340DataIntegrityProof,
25
+ BTCR2SignedUpdate,
26
+ BTCR2UnsignedUpdate,
27
+ SchnorrMultikey
28
+ } from '@did-btcr2/cryptosuite';
29
+ import { CompressedSecp256k1PublicKey } from '@did-btcr2/keypair';
30
+ import { bytesToHex } from '@noble/hashes/utils';
31
+ import { canonicalization, DidBtcr2 } from '../did-btcr2.js';
32
+ import { Appendix } from '../utils/appendix.js';
33
+ import { DidDocument, ID_PLACEHOLDER_VALUE } from '../utils/did-document.js';
34
+ import { BeaconFactory } from './beacon/factory.js';
35
+ import { BeaconService, BeaconSignal, BlockMetadata } from './beacon/interfaces.js';
36
+ import { BeaconUtils } from './beacon/utils.js';
37
+ import { DidComponents, Identifier } from './identifier.js';
38
+ import { SMTProof } from './interfaces.js';
39
+ import { CASAnnouncement, Sidecar, SidecarData } from './types.js';
40
+
41
+ /**
42
+ * The response object for DID Resolution.
43
+ */
44
+ export interface DidResolutionResponse {
45
+ currentDocument: DidDocument;
46
+ confirmations: number;
47
+ versionId: string;
48
+ updated: string;
49
+ }
50
+
51
+ /**
52
+ * Implements {@link https://dcdpr.github.io/did-btcr2/operations/resolve.html | 7.2 Resolve}.
53
+ * Resolving a did:btcr2 identifier iteratively builds a DID document by applying BTCR2 Updates
54
+ * to an Initial DID Document that have been committed to the Bitcoin blockchain by Authorized
55
+ * Beacon Signals. The Initial DID Document is either deterministically created from the DID or
56
+ * provided by Sidecar Data.
57
+ * @class Resolve
58
+ * @type {Resolve}
59
+ */
60
+ export class Resolve {
61
+ /**
62
+ * Implements subsection {@link https://dcdpr.github.io/did-btcr2/operations/resolve.html#process-sidecar-data | Process Sidecar Data}
63
+ * @param {Sidecar} sidecar The sidecar data to process.
64
+ * @returns {SidecarData} The processed sidecar data containing maps of updates, CAS announcements, and SMT proofs.
65
+ */
66
+ static processSidecarData(sidecar: Sidecar = {} as Sidecar): SidecarData {
67
+ // BTCR2 Signed Updates map
68
+ const updateMap = new Map<string, BTCR2SignedUpdate>();
69
+ if(sidecar.updates?.length)
70
+ for(const update of sidecar.updates) {
71
+ updateMap.set(canonicalization.process(update, { encoding: 'hex' }), update);
72
+ }
73
+
74
+ // CAS Announcements map
75
+ const casMap = new Map<string, CASAnnouncement>();
76
+ if(sidecar.casUpdates?.length)
77
+ for(const update of sidecar.casUpdates) {
78
+ casMap.set(canonicalization.process(update, { encoding: 'hex' }), update);
79
+ }
80
+
81
+ // SMT Proofs map
82
+ const smtMap = new Map<string, SMTProof>();
83
+ if(sidecar.smtProofs?.length)
84
+ for(const proof of sidecar.smtProofs) {
85
+ smtMap.set(proof.id, proof);
86
+ }
87
+
88
+ return { updateMap, casMap, smtMap };
89
+ }
90
+
91
+ /**
92
+ * Implements subsection {@link https://dcdpr.github.io/did-btcr2/operations/resolve.html#establish-current-document | 7.2.d Establish current_document}.
93
+ * Resolution begins by creating an Initial Did Document called current_document (Current DID Document).
94
+ * The current_document is iteratively patched with BTCR2 Signed Updates announced by Authorized Beacon Signals.
95
+ * @param {DidComponents} didComponents The decoded components of the did.
96
+ * @param {GenesisDocument} genesisDocument The genesis document for resolving the DID Document.
97
+ * @returns {Promise<DidDocument>} The resolved DID Document object.
98
+ * @throws {DidError} if the DID hrp is invalid, no sidecarData passed and hrp = "x".
99
+ */
100
+ static async establishCurrentDocument(
101
+ didComponents: DidComponents,
102
+ genesisDocument?: object,
103
+ ): Promise<DidDocument> {
104
+ // Deconstruct the hrp from the components
105
+ const { hrp, genesisBytes } = didComponents;
106
+
107
+ // If hrp `x`, perform external resolution
108
+ if (hrp === IdentifierHrp.x) {
109
+ if(!genesisDocument)
110
+ throw new ResolveError(
111
+ 'External resolution requires genesisDocument',
112
+ MISSING_UPDATE_DATA, { didComponents }
113
+ );
114
+ return await this.external(didComponents, genesisDocument);
115
+ }
116
+
117
+ // Check for hrp `k`
118
+ if(hrp === IdentifierHrp.k){
119
+ // Validate genesis bytes as a compressed secp256k1 public key
120
+ if(!CompressedSecp256k1PublicKey.isValid(genesisBytes)) {
121
+ throw new ResolveError(
122
+ 'Deterministic resolution requires valid secp256k1 public key',
123
+ INVALID_DID, { genesisBytes }
124
+ );
125
+ }
126
+ // Perform deterministic resolution
127
+ return this.deterministic(didComponents);
128
+ }
129
+
130
+ // Else, throw an error for unsupported hrp
131
+ throw new ResolveError(`Unsupported DID hrp ${hrp}`, INVALID_DID, { hrp });
132
+ }
133
+
134
+ /**
135
+ * Implements subsection {@link https://dcdpr.github.io/did-btcr2/operations/resolve.html#if-genesis_bytes-is-a-secp256k1-public-key | 7.2.d.1 if genesis bytes is a secp256k1 Public Key}.
136
+ * @param {DidComponents} didComponents The decoded components of the did.
137
+ * @returns {DidDocument} The resolved DID Document object.
138
+ */
139
+ static deterministic(didComponents: DidComponents): DidDocument {
140
+ // Encode the did from the didComponents
141
+ const did = Identifier.encode(didComponents);
142
+
143
+ // Deconstruct the bytes from the given components
144
+ const { genesisBytes } = didComponents;
145
+
146
+ // Construct a new CompressedSecp256k1PublicKey and deconstruct the publicKey and publicKeyMultibase
147
+ const { multibase: publicKeyMultibase } = new CompressedSecp256k1PublicKey(genesisBytes);
148
+
149
+ // Generate the service field for the DID Document
150
+ const service = BeaconUtils.generateBeaconServices({
151
+ id : did,
152
+ publicKey : genesisBytes,
153
+ network : getNetwork(didComponents.network),
154
+ beaconType : 'SingletonBeacon'
155
+ });
156
+
157
+ return new DidDocument({
158
+ id : did,
159
+ controller : [did],
160
+ verificationMethod : [{
161
+ id : `${did}#initialKey`,
162
+ type : 'Multikey',
163
+ controller : did,
164
+ publicKeyMultibase : publicKeyMultibase.encoded
165
+ }],
166
+ service
167
+ });
168
+ }
169
+
170
+ /**
171
+ * Implements subsection {@link https://dcdpr.github.io/did-btcr2/operations/resolve.html#if-genesis_bytes-is-a-sha-256-hash | 7.2.d.2 if genesis_bytes is a SHA-256 Hash}.
172
+ * @param {DidComponents} didComponents BTCR2 DID components used to resolve the DID Document
173
+ * @param {GenesisDocument} genesisDocument The genesis document for resolving the DID Document.
174
+ * @returns {Promise<DidDocument>} The resolved DID Document object
175
+ * @throws {MethodError} InvalidDidDocument if not conformant to DID Core v1.1
176
+ */
177
+ static async external(
178
+ didComponents: DidComponents,
179
+ genesisDocument: object,
180
+ ): Promise<DidDocument> {
181
+ // Canonicalize and sha256 hash the currentDocument
182
+ const hashBytes = canonicalization.process(genesisDocument, { encoding: 'hex' });
183
+
184
+ // Compare the genesisBytes to the hashBytes
185
+ const genesisBytes = bytesToHex(didComponents.genesisBytes);
186
+
187
+ // If the genesisBytes do not match the hashBytes, throw an error
188
+ if (genesisBytes !== hashBytes) {
189
+ throw new MethodError(
190
+ `Initial document mismatch: genesisBytes ${genesisBytes} !== hashBytes ${hashBytes}`,
191
+ INVALID_DID_DOCUMENT, { genesisBytes, hashBytes }
192
+ );
193
+ }
194
+
195
+ // Encode the did from the didComponents
196
+ const did = Identifier.encode(didComponents);
197
+
198
+ // Replace the placeholder did with the did throughout the currentDocument.
199
+ const currentDocument = JSON.parse(
200
+ JSON.stringify(genesisDocument).replaceAll(ID_PLACEHOLDER_VALUE, did)
201
+ );
202
+
203
+ // Return a W3C conformant DID Document
204
+ return new DidDocument(currentDocument);
205
+ }
206
+
207
+ /**
208
+ * Finds uses the beacon services in the currentDocument to scan for onchain Beacon Signals (transactions) containing
209
+ * Signal Bytes (last output in OP_RETURN transaction).
210
+ * @param {Array<BeaconService>} beaconServices The array of BeaconService objects to search for signals.
211
+ * @param {SidecarData} sidecarData The sidecar data containing maps of updates, CAS announcements, and SMT proofs.
212
+ * @param {BitcoinNetworkConnection} bitcoin The bitcoin network connection used to fetch beacon signals
213
+ * @param {boolean} [fullBlockchainTraversal=false] Whether to perform a full blockchain traversal or use an indexer
214
+ * @returns {Promise<Array<[BTCR2SignedUpdate, BlockMetadata]>>} The array of BTCR2 Signed Updates announced by the Beacon Signals.
215
+ */
216
+ static async processBeaconSignals(
217
+ beaconServices: Array<BeaconService>,
218
+ sidecarData: SidecarData,
219
+ bitcoin: BitcoinNetworkConnection,
220
+ fullBlockchainTraversal?: boolean
221
+ ): Promise<Array<[BTCR2SignedUpdate, BlockMetadata]>> {
222
+ // Query indexer or perform a full blockchain traversal for Beacon Signals
223
+ const beaconServicesSignals = !fullBlockchainTraversal
224
+ ? await this.queryBlockchainIndexer(beaconServices, bitcoin)
225
+ : await this.traverseFullBlockchain(beaconServices, bitcoin);
226
+
227
+
228
+ // Set updates to an empty array
229
+ const unsortedUpdates = new Array<[BTCR2SignedUpdate, BlockMetadata]>();
230
+
231
+ // Iterate over each beacon service and its signals
232
+ for(const [service, signals] of beaconServicesSignals) {
233
+ // Establish a beacon object
234
+ const beacon = BeaconFactory.establish(service, signals, sidecarData);
235
+ // Process its signals
236
+ const processed = await beacon.processSignals();
237
+ // Append the processed updates to the updates array
238
+ unsortedUpdates.push(...processed);
239
+ }
240
+
241
+ return unsortedUpdates;
242
+ }
243
+
244
+ /**
245
+ * Implements subsection {@link https://dcdpr.github.io/did-btcr2/operations/resolve.html#process-updates | 7.2.f Process updates Array}.
246
+ * @param {DidDocument} currentDocument The current DID Document to apply the updates to.
247
+ * @param {Array<[BTCR2SignedUpdate, BlockMetadata]>} unsortedUpdates The unsorted array of BTCR2 Signed Updates and their associated Block Metadata.
248
+ * @param {string} [versionTime] The optional version time to limit updates to.
249
+ * @param {string} [versionId] The optional version id to limit updates to.
250
+ * @returns {Promise<DidResolutionResponse>} The updated DID Document, number of confirmations, and version id.
251
+ */
252
+ static async processUpdatesArray(
253
+ currentDocument: DidDocument,
254
+ unsortedUpdates: Array<[BTCR2SignedUpdate, BlockMetadata]>,
255
+ versionTime?: string,
256
+ versionId?: string
257
+ ): Promise<DidResolutionResponse> {
258
+ // Start the version number being processed at 1
259
+ let currentVersionId = 1;
260
+
261
+ // Initialize an empty array to hold the update hashes
262
+ const updateHashHistory: string[] = [];
263
+
264
+ // 1. Sort updates by targetVersionId (ascending), using blockheight as tie-breaker
265
+ const updates = unsortedUpdates.sort(([upd0, blk0], [upd1, blk1]) =>
266
+ upd0.targetVersionId - upd1.targetVersionId || blk0.height - blk1.height
267
+ );
268
+
269
+ // Create a default response object
270
+ const response = {
271
+ currentDocument,
272
+ versionId : `${currentVersionId}`,
273
+ confirmations : 0,
274
+ updated : ''
275
+ };
276
+
277
+ // Iterate over each (update block) pair
278
+ for(const [update, block] of updates) {
279
+ // Get the hash of the current document
280
+ const currentDocumentHash = canonicalization.process(response.currentDocument, { encoding: 'base58' });
281
+
282
+ // Set confirmations to the block confirmations
283
+ response.confirmations = block.confirmations;
284
+
285
+ // Safely convert versionTime to timestamp
286
+ const vTime = DateUtils.dateStringToTimestamp(versionTime || '');
287
+ // Safely convert block.blocktime to timestamp
288
+ const bTime = DateUtils.blocktimeToTimestamp(block.time);
289
+ // Set the updated field to the blocktime of the current update
290
+ response.updated = DateUtils.toISOStringNonFractional(bTime);
291
+
292
+ // if resolutionOptions.versionTime is defined and the blocktime is more recent, return currentDocument
293
+ if(vTime < bTime) {
294
+ return response;
295
+ }
296
+
297
+ // Check update.targetVersionId against currentVersionId
298
+ // If update.targetVersionId <= currentVersionId, confirm duplicate update
299
+ if(update.targetVersionId <= currentVersionId) {
300
+ updateHashHistory.push(currentDocumentHash);
301
+ this.confirmDuplicateUpdate(update, updateHashHistory);
302
+ }
303
+
304
+ // If update.targetVersionId == currentVersionId + 1, apply the update
305
+ else if (update.targetVersionId === currentVersionId + 1) {
306
+ // Prepend `z` to the sourceHash if it does not start with it
307
+ const sourceHash = update.sourceHash.startsWith('z') ? update.sourceHash : `z${update.sourceHash}`;
308
+
309
+ // Check if update.sourceHash !== currentDocumentHash
310
+ if (sourceHash !== currentDocumentHash) {
311
+ // Raise an INVALID_DID_UPDATE error if they do not match
312
+ throw new ResolveError(
313
+ `Hash mismatch: update.sourceHash !== currentDocumentHash`,
314
+ INVALID_DID_UPDATE, { sourceHash, currentDocumentHash }
315
+ );
316
+ }
317
+ // Apply the update to the currentDocument and set it in the response
318
+ response.currentDocument = await this.applyDidUpdate(response.currentDocument, update);
319
+ // Create unsigned_update by removing the proof property from update.
320
+ const unsignedUpdate = JSONUtils.deleteKeys(update, ['proof']) as BTCR2UnsignedUpdate;
321
+ // Push the canonicalized unsigned update hash to the updateHashHistory
322
+ updateHashHistory.push(canonicalization.process(unsignedUpdate, { encoding: 'base58' }));
323
+ }
324
+
325
+ // If update.targetVersionId > currentVersionId + 1, throw LATE_PUBLISHING error
326
+ else if(update.targetVersionId > currentVersionId + 1) {
327
+ throw new ResolveError(
328
+ `Version Id Mismatch: targetVersionId cannot be > currentVersionId + 1`,
329
+ 'LATE_PUBLISHING_ERROR', {
330
+ targetVersionId : update.targetVersionId,
331
+ currentVersionId : String(currentVersionId + 1)
332
+ }
333
+ );
334
+ }
335
+
336
+ // Increment currentVersionId
337
+ currentVersionId++;
338
+ // Set currentVersionId in response
339
+ response.versionId = `${currentVersionId}`;
340
+
341
+ // If resolutionOptions.versionId is defined and <= currentVersionId, return currentDocument
342
+ if(currentVersionId >= Number(versionId)) {
343
+ return response;
344
+ }
345
+
346
+ // Check if the current document is deactivated before further processing
347
+ if(currentDocument.deactivated) {
348
+ return response;
349
+ }
350
+ }
351
+
352
+ // Return response data
353
+ return response;
354
+ }
355
+
356
+ /**
357
+ * Retrieves the beacon signals for the given array of BeaconService objects
358
+ * using a esplora/electrs REST API connection via a bitcoin I/O driver.
359
+ * @param {Array<BeaconService>} beaconServices Array of BeaconService objects to retrieve signals for
360
+ * @param {BitcoinNetworkConnection} bitcoin Bitcoin network connection to use for REST calls
361
+ * @returns {Promise<Array<BeaconSignal>>} Promise resolving to an array of BeaconSignal objects
362
+ */
363
+ static async queryBlockchainIndexer(
364
+ beaconServices: Array<BeaconService>,
365
+ bitcoin: BitcoinNetworkConnection
366
+ ): Promise<Map<BeaconService, Array<BeaconSignal>>> {
367
+ // Empty array of beaconSignals
368
+ const beaconServiceSignals = new Map<BeaconService, Array<BeaconSignal>>();
369
+
370
+ // Iterate over each beacon
371
+ for (const beaconService of beaconServices) {
372
+ beaconServiceSignals.set(beaconService, []);
373
+ // Get the transactions for the beacon address via REST
374
+ const beaconSignals = await bitcoin.network.rest.address.getTxs(
375
+ beaconService.serviceEndpoint as string
376
+ );
377
+
378
+ // If no signals are found, continue
379
+ if (!beaconSignals || !beaconSignals.length) {
380
+ continue;
381
+ }
382
+
383
+ // Iterate over each signal
384
+ for (const beaconSignal of beaconSignals) {
385
+ // Get the last vout in the transaction
386
+ const signalVout = beaconSignal.vout.slice(-1)[0];
387
+
388
+ /**
389
+ * Look for OP_RETURN in last vout scriptpubkey_asm
390
+ * Vout (rest) format:
391
+ * {
392
+ * scriptpubkey: '6a20570f177c65e64fb5cf61180b664cdddf09ab76153c2b192e22006e5b22a3917a',
393
+ * scriptpubkey_asm: 'OP_RETURN OP_PUSHBYTES_32 570f177c65e64fb5cf61180b664cdddf09ab76153c2b192e22006e5b22a3917a',
394
+ * scriptpubkey_type: 'op_return',
395
+ * value: 0
396
+ * }
397
+ */
398
+ if(!signalVout || !signalVout.scriptpubkey_asm.includes('OP_RETURN')) {
399
+ // If not found, continue to next signal
400
+ continue;
401
+ }
402
+
403
+ // Construct output map for easier access
404
+ const outputMap = new Map<string, string | number>(Object.entries(signalVout));
405
+
406
+ // Grab the signal vout scriptpubkey
407
+ const signalVoutScriptPubkey = outputMap.get('scriptpubkey_asm') as string;
408
+
409
+ // If the signal vout scriptpubkey does not exist, continue to next signal
410
+ if(!signalVoutScriptPubkey){
411
+ continue;
412
+ }
413
+
414
+ // Extract hex string hash of the signal bytes from the scriptpubkey
415
+ const updateHash = signalVoutScriptPubkey.split(' ').slice(-1)[0];
416
+ if(!updateHash) {
417
+ continue;
418
+ }
419
+
420
+ const confirmations = await bitcoin.network.rest.block.count() - beaconSignal.status.block_height + 1;
421
+ // Push the beacon signal object to the signals array for the beacon service
422
+ beaconServiceSignals.get(beaconService)?.push({
423
+ tx : beaconSignal,
424
+ signalBytes : updateHash,
425
+ blockMetadata : {
426
+ confirmations,
427
+ height : beaconSignal.status.block_height,
428
+ time : beaconSignal.status.block_time,
429
+ }
430
+ });
431
+ }
432
+ }
433
+
434
+
435
+ // Return the beaconSignals
436
+ return beaconServiceSignals;
437
+ }
438
+
439
+ /**
440
+ * Traverse the full blockchain from genesis to chain top looking for beacon signals.
441
+ * @param {Array<BeaconService>} beaconServices Array of BeaconService objects to search for signals.
442
+ * @param {BitcoinNetworkConnection} bitcoin Bitcoin network connection to use for RPC calls.
443
+ * @returns {Promise<Array<BeaconSignal>>} Promise resolving to an array of BeaconSignal objects.
444
+ */
445
+ static async traverseFullBlockchain(
446
+ beaconServices: Array<BeaconService>,
447
+ bitcoin: BitcoinNetworkConnection
448
+ ): Promise<Map<BeaconService, Array<BeaconSignal>>> {
449
+ const beaconServiceSignals = new Map<BeaconService, Array<BeaconSignal>>();
450
+
451
+ for(const beaconService of beaconServices) {
452
+ beaconServiceSignals.set(beaconService, []);
453
+ }
454
+
455
+ // Get the RPC connection from the bitcoin network
456
+ const rpc = bitcoin.network.rpc;
457
+
458
+ // Ensure that the RPC connection is available
459
+ if(!rpc) {
460
+ throw new ResolveError('RPC connection is not available', 'RPC_CONNECTION_ERROR', bitcoin);
461
+ }
462
+
463
+ // Get the current block height
464
+ const targetHeight = await rpc.getBlockCount();
465
+
466
+ // Set genesis height
467
+ let height = 0;
468
+
469
+ // Opt into rpc connection to get the block data at the blockhash
470
+ let block = await bitcoin.network.rpc!.getBlock({ height }) as BlockV3;
471
+
472
+ console.info(`Searching for beacon signals, please wait ...`);
473
+ while (block.height <= targetHeight) {
474
+ // Iterate over each transaction in the block
475
+ for (const tx of block.tx) {
476
+ // If the txid is a coinbase, continue ...
477
+ if (tx.txid === GENESIS_TX_ID) {
478
+ continue;
479
+ }
480
+
481
+ // Iterate over each input in the transaction
482
+ for (const vin of tx.vin) {
483
+
484
+ // If the vin is a coinbase transaction, continue ...
485
+ if (vin.coinbase) {
486
+ continue;
487
+ }
488
+
489
+ // If the vin txinwitness contains a coinbase did, continue ...
490
+ if (vin.txinwitness && vin.txinwitness.length === 1 && vin.txinwitness[0] === TXIN_WITNESS_COINBASE) {
491
+ continue;
492
+ }
493
+
494
+ // If the txid from the vin is undefined, continue ...
495
+ if (!vin.txid) {
496
+ continue;
497
+ }
498
+
499
+ // If the vout from the vin is undefined, continue ...
500
+ if (vin.vout === undefined) {
501
+ continue;
502
+ }
503
+
504
+ // Get the previous output transaction data
505
+ const prevout = await rpc.getRawTransaction(vin.txid, 2) as RawTransactionV2;
506
+
507
+ // If the previous output vout at the vin.vout index is undefined, continue ...
508
+ if (!prevout.vout[vin.vout]) {
509
+ continue;
510
+ }
511
+
512
+ // Get the address from the scriptPubKey from the prevvout (previous output's input at the vout index)
513
+ const scriptPubKey = prevout.vout[vin.vout].scriptPubKey;
514
+
515
+ // If the scriptPubKey.address is undefined, continue ...
516
+ if (!scriptPubKey.address) {
517
+ continue;
518
+ }
519
+
520
+ // If the beaconAddress from prevvout scriptPubKey is not a beacon service endpoint address, continue ...
521
+ const beaconService = BeaconUtils.getBeaconServicesMap(beaconServices).get(scriptPubKey.address);
522
+ if (!beaconService) {
523
+ continue;
524
+ }
525
+
526
+ /**
527
+ * Look for 'OP_RETURN' in prevout.vout[vin.vout].scriptPubKey.asm, else continue ...
528
+ *
529
+ * TxOut (rpc) format:
530
+ * {
531
+ * value: 0,
532
+ * n: 1,
533
+ * scriptPubKey: {
534
+ * asm: 'OP_RETURN 570f177c65e64fb5cf61180b664cdddf09ab76153c2b192e22006e5b22a3917a',
535
+ * desc: 'raw(6a20570f177c65e64fb5cf61180b664cdddf09ab76153c2b192e22006e5b22a3917a)#cdgj3pm4',
536
+ * hex: '6a20570f177c65e64fb5cf61180b664cdddf09ab76153c2b192e22006e5b22a3917a',
537
+ * type: 'nulldata'
538
+ * }
539
+ * }
540
+ */
541
+ const txVoutScriptPubkeyAsm = prevout.vout[vin.vout].scriptPubKey.asm;
542
+ if(!txVoutScriptPubkeyAsm.includes('OP_RETURN')) {
543
+ continue;
544
+ }
545
+
546
+ // Log the found txid and beacon
547
+ console.info(`Tx ${tx.txid} contains beacon service address ${scriptPubKey.address} and OP_RETURN!`, tx);
548
+
549
+ // Extract hex string hash of the signal bytes from the scriptpubkey
550
+ const updateHash = txVoutScriptPubkeyAsm.split(' ').slice(-1)[0];
551
+ if(!updateHash) {
552
+ continue;
553
+ }
554
+
555
+ // Push the beacon signal object to the beacon signals array for that beacon service
556
+ beaconServiceSignals.get(beaconService)?.push({
557
+ tx,
558
+ signalBytes : updateHash,
559
+ blockMetadata : {
560
+ height : block.height,
561
+ time : block.time,
562
+ confirmations : block.confirmations
563
+ }
564
+ });
565
+ };
566
+ }
567
+
568
+ // Increment the height
569
+ height += 1;
570
+
571
+ // Check if we've reached the chain tip
572
+ const tip = await rpc.getBlockCount();
573
+ if(height > tip) {
574
+ // If so, break the loop
575
+ console.info(`Chain tip reached ${height}, breaking ...`);
576
+ break;
577
+ }
578
+
579
+ // Reset the block var to the next block data
580
+ block = await rpc.getBlock({ height }) as BlockV3;
581
+ }
582
+
583
+ return beaconServiceSignals;
584
+ }
585
+
586
+ /**
587
+ * Implements subsection {@link https://dcdpr.github.io/did-btcr2/#confirm-duplicate-update | 7.2.f.1 Confirm Duplicate Update}.
588
+ * This step confirms that an update with a lower-than-expected targetVersionId is a true duplicate.
589
+ * @param {BTCR2SignedUpdate} update The BTCR2 Signed Update to confirm as a duplicate.
590
+ * @returns {void} Does not return a value, but throws an error if the update is not a valid duplicate.
591
+ */
592
+ static confirmDuplicateUpdate(update: BTCR2SignedUpdate, updateHashHistory: string[]): void {
593
+ // Create unsigned_update by removing the proof property from update.
594
+ const unsignedUpdate = JSONUtils.deleteKeys(update, ['proof']) as BTCR2UnsignedUpdate;
595
+
596
+ // Hash unsignedUpdate with JSON Document Hashing algorithm
597
+ const unsignedUpdateHash = canonicalization.process(unsignedUpdate);
598
+
599
+ // 5. Let historicalUpdateHash equal updateHashHistory[updateHashIndex].
600
+ const historicalUpdateHash = updateHashHistory[update.targetVersionId - 2];
601
+
602
+ // Check if the updateHash matches the historical hash
603
+ if (updateHashHistory[update.targetVersionId - 2] !== unsignedUpdateHash) {
604
+ throw new ResolveError(
605
+ `Invalid duplicate: ${unsignedUpdateHash} does not match ${historicalUpdateHash}`,
606
+ LATE_PUBLISHING_ERROR, { unsignedUpdateHash, updateHashHistory }
607
+ );
608
+ }
609
+ }
610
+
611
+ /**
612
+ * Implements subsection {@link https://dcdpr.github.io/did-btcr2/operations/resolve.html#apply-update | 7.2.f.3 Apply Update}.
613
+ * @param {DidDocument} currentDocument The current DID Document to apply the update to.
614
+ * @param {BTCR2SignedUpdate} update The BTCR2 Signed Update to apply.
615
+ * @returns {Promise<DidDocument>} The updated DID Document after applying the update.
616
+ * @throws {ResolveError} If the update is invalid or cannot be applied.
617
+ */
618
+ static async applyDidUpdate(
619
+ currentDocument: DidDocument,
620
+ update: BTCR2SignedUpdate
621
+ ): Promise<DidDocument> {
622
+ // Get the capability id from the to update proof.
623
+ const capabilityId = update.proof?.capability;
624
+ // Since this field is optional, check that it exists
625
+ if (!capabilityId) {
626
+ // If it does not exist, throw INVALID_DID_UPDATE error
627
+ throw new ResolveError('No root capability found in update', INVALID_DID_UPDATE, update);
628
+ }
629
+
630
+ // Get the root capability object by dereferencing the capabilityId
631
+ const rootCapability = Appendix.dereferenceZcapId(capabilityId);
632
+
633
+ // Deconstruct the invocationTarget and controller from the root capability
634
+ const { invocationTarget, controller: rootController } = rootCapability;
635
+ // Check that both invocationTarget and rootController equal currentDocument.id
636
+ if (![invocationTarget, rootController].every((id) => id === currentDocument.id)) {
637
+ // If they do not all match, throw INVALID_DID_UPDATE error
638
+ throw new ResolveError(
639
+ 'Invalid root capability',
640
+ INVALID_DID_UPDATE, { rootCapability, currentDocument }
641
+ );
642
+ }
643
+
644
+ // Get the verificationMethod field from the update proof as verificationMethodId.
645
+ const verificationMethodId = update.proof?.verificationMethod;
646
+ // Since this field is optional, check that it exists
647
+ if(!verificationMethodId) {
648
+ // If it does not exist, throw INVALID_DID_UPDATE error
649
+ throw new ResolveError('No verificationMethod found in update', INVALID_DID_UPDATE, update);
650
+ }
651
+
652
+ // Get the verificationMethod from the DID Document using the verificationMethodId.
653
+ const vm = DidBtcr2.getSigningMethod(currentDocument, verificationMethodId);
654
+
655
+ // Split the vmId by the `#` to get the id and controller.
656
+ const [vmController, vmId] = vm.id.split('#');
657
+
658
+ // Construct a new SchnorrMultikey.
659
+ const multikey = SchnorrMultikey.fromPublicKeyMultibase(
660
+ `#${vmId}`, vmController, vm.publicKeyMultibase
661
+ );
662
+
663
+ // Construct a new BIP340Cryptosuite with the SchnorrMultikey.
664
+ const cryptosuite = new BIP340Cryptosuite(multikey);
665
+
666
+ // Canonicalize the update
667
+ const canonicalUpdate = canonicalization.canonicalize(update);
668
+
669
+ // Construct a DataIntegrityProof with the cryptosuite
670
+ const diProof = new BIP340DataIntegrityProof(cryptosuite);
671
+
672
+ // Call the verifyProof method
673
+ const verificationResult = diProof.verifyProof(canonicalUpdate, 'capabilityInvocation');
674
+
675
+ // If the result is not verified, throw INVALID_DID_UPDATE error
676
+ if (!verificationResult.verified) {
677
+ throw new MethodError(
678
+ 'Invalid update: proof not verified',
679
+ INVALID_DID_UPDATE, verificationResult
680
+ );
681
+ }
682
+
683
+ // Apply the update.patch to the currentDocument to get the updatedDocument.
684
+ const updatedDocument = JSONPatch.apply(currentDocument, update.patch) as DidDocument;
685
+
686
+ // Verify that updatedDocument is conformant to DID Core v1.1.
687
+ DidDocument.validate(updatedDocument);
688
+
689
+ // Canonicalize and hash the updatedDocument to get the currentDocumentHash.
690
+ const currentDocumentHash = canonicalization.process(currentDocument, { encoding: 'base58' });
691
+
692
+ // Prepare the update targetHash for comparison with currentDocumentHash.
693
+ const updateTargetHash = update.targetHash.startsWith('z') ? update.targetHash : `z${update.targetHash}`;
694
+
695
+ // Make sure the update.targetHash equals currentDocumentHash.
696
+ if (updateTargetHash !== currentDocumentHash) {
697
+ // If they do not match, throw INVALID_DID_UPDATE error.
698
+ throw new MethodError(
699
+ `Invalid update: updateTargetHash !== currentDocumentHash`,
700
+ INVALID_DID_UPDATE, { updateTargetHash, currentDocumentHash }
701
+ );
702
+ }
703
+
704
+ // Return final updatedDocument.
705
+ return updatedDocument;
706
+ }
707
+ }