@did-btcr2/method 0.27.0 → 0.28.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.
package/dist/browser.js CHANGED
@@ -97563,11 +97563,12 @@ a=end-of-candidates
97563
97563
  SigningSessionPhase: () => SigningSessionPhase,
97564
97564
  SingletonBeacon: () => SingletonBeacon,
97565
97565
  SingletonBeaconError: () => SingletonBeaconError,
97566
+ StaticFeeEstimator: () => StaticFeeEstimator,
97566
97567
  TransportAdapterError: () => TransportAdapterError,
97567
97568
  TransportError: () => TransportError,
97568
97569
  TransportFactory: () => TransportFactory,
97569
97570
  TypedEventEmitter: () => TypedEventEmitter,
97570
- Update: () => Update,
97571
+ Updater: () => Updater,
97571
97572
  VALIDATION_ACK: () => VALIDATION_ACK,
97572
97573
  createAggregatedNonceMessage: () => createAggregatedNonceMessage,
97573
97574
  createAuthorizationRequestMessage: () => createAuthorizationRequestMessage,
@@ -138629,9 +138630,8 @@ a=end-of-candidates
138629
138630
  // src/did-btcr2.ts
138630
138631
  init_shim();
138631
138632
  var ecc = __toESM(require_dist2(), 1);
138632
- init_utils();
138633
138633
 
138634
- // src/core/update.ts
138634
+ // src/core/updater.ts
138635
138635
  init_shim();
138636
138636
 
138637
138637
  // src/utils/did-document.ts
@@ -138997,17 +138997,37 @@ a=end-of-candidates
138997
138997
  }
138998
138998
  };
138999
138999
 
139000
- // src/core/update.ts
139001
- var Update = class {
139000
+ // src/core/updater.ts
139001
+ var Updater = class _Updater {
139002
+ #phase = "Construct" /* Construct */;
139003
+ #sourceDocument;
139004
+ #patches;
139005
+ #sourceVersionId;
139006
+ #verificationMethod;
139007
+ #beaconService;
139008
+ #unsignedUpdate = null;
139009
+ #signedUpdate = null;
139010
+ /**
139011
+ * @internal — Use {@link DidBtcr2.update} to create instances.
139012
+ */
139013
+ constructor(params) {
139014
+ this.#sourceDocument = params.sourceDocument;
139015
+ this.#patches = params.patches;
139016
+ this.#sourceVersionId = params.sourceVersionId;
139017
+ this.#verificationMethod = params.verificationMethod;
139018
+ this.#beaconService = params.beaconService;
139019
+ }
139020
+ // ─── Public static utility methods ─────────────────────────────────────────
139021
+ // Used by generate-vector.ts and other scripts that need direct access to
139022
+ // individual update steps outside the state machine flow.
139002
139023
  /**
139003
139024
  * Implements subsection {@link https://dcdpr.github.io/did-btcr2/operations/update.html#construct-btcr2-unsigned-update | 7.3.b Construct BTCR2 Unsigned Update}.
139004
- * This process constructs a BTCR2 Unsigned Update conformant to the spec template.
139005
139025
  *
139006
139026
  * @param {Btcr2DidDocument} sourceDocument The source DID document to be updated.
139007
- * @param {PatchOperation[]} patches The array of JSON Patch operations to apply to the sourceDocument.
139027
+ * @param {PatchOperation[]} patches The JSON Patch operations to apply.
139008
139028
  * @param {number} sourceVersionId The version ID of the source document.
139009
139029
  * @returns {UnsignedBTCR2Update} The constructed UnsignedBTCR2Update object.
139010
- * @throws {UpdateError} InvalidDid if sourceDocument.id does not match identifier.
139030
+ * @throws {UpdateError} If the target document fails DID Core validation.
139011
139031
  */
139012
139032
  static construct(sourceDocument, patches, sourceVersionId) {
139013
139033
  const unsignedUpdate = {
@@ -139037,13 +139057,12 @@ a=end-of-candidates
139037
139057
  }
139038
139058
  /**
139039
139059
  * Implements subsection {@link http://dcdpr.github.io/did-btcr2/operations/update.html#construct-btcr2-signed-update | 7.3.c Construct BTCR2 Signed Update }.
139040
- * This process constructs a BTCR2 Signed Update from a BTCR2 Unsigned Update.
139041
139060
  *
139042
- * @param {string} did The did-btcr2 identifier to derive the root capability from
139043
- * @param {UnsignedBTCR2Update} unsignedUpdate The updatePayload object to be signed
139044
- * @param {DidVerificationMethod} verificationMethod The verificationMethod object to be used for signing
139045
- * @returns {SignedBTCR2Update} Did update payload secured with a proof => SignedBTCR2Update
139046
- * @throws {UpdateError} if the privateKeyBytes are invalid
139061
+ * @param {string} did The did-btcr2 identifier to derive the root capability from.
139062
+ * @param {UnsignedBTCR2Update} unsignedUpdate The unsigned update to sign.
139063
+ * @param {DidVerificationMethod} verificationMethod The verification method for signing.
139064
+ * @param {KeyBytes} secretKey The secret key bytes.
139065
+ * @returns {SignedBTCR2Update} The signed update with a Data Integrity proof.
139047
139066
  */
139048
139067
  static sign(did, unsignedUpdate, verificationMethod, secretKey) {
139049
139068
  const controller = verificationMethod.controller;
@@ -139068,17 +139087,141 @@ a=end-of-candidates
139068
139087
  }
139069
139088
  /**
139070
139089
  * Implements subsection {@link https://dcdpr.github.io/did-btcr2/operations/update.html#announce-did-update | 7.3.d Announce DID Update}.
139071
- * BTCR2 Signed Updates are announced to the Bitcoin blockchain depending on the Beacon Type.
139072
- * @param {BeaconService} beaconService The BeaconService object representing the funded beacon to announce the update to.
139073
- * @param {SignedBTCR2Update} update The signed update object to be announced.
139074
- * @param {KeyBytes} secretKey The private key used to sign the update for announcement.
139075
- * @returns {SignedBTCR2Update} The SignedBTCR2Update object containing data to validate the Beacon Signal
139076
- * @throws {UpdateError} if the beaconService type is invalid
139090
+ * Announces a signed update to the Bitcoin blockchain via the specified beacon.
139091
+ *
139092
+ * @param {BeaconService} beaconService The beacon service to broadcast through.
139093
+ * @param {SignedBTCR2Update} update The signed update to announce.
139094
+ * @param {KeyBytes} secretKey The secret key for signing the Bitcoin transaction.
139095
+ * @param {BitcoinConnection} bitcoin The Bitcoin network connection.
139096
+ * @returns {Promise<SignedBTCR2Update>} The signed update that was broadcast.
139077
139097
  */
139078
139098
  static async announce(beaconService, update, secretKey, bitcoin3) {
139079
139099
  const beacon = BeaconFactory.establish(beaconService);
139080
- const result = await beacon.broadcastSignal(update, secretKey, bitcoin3);
139081
- return result;
139100
+ return beacon.broadcastSignal(update, secretKey, bitcoin3);
139101
+ }
139102
+ // ─── Private instance wrappers ─────────────────────────────────────────────
139103
+ // Delegate to the public statics with bound instance fields for cleaner
139104
+ // advance/provide code.
139105
+ #construct() {
139106
+ return _Updater.construct(this.#sourceDocument, this.#patches, this.#sourceVersionId);
139107
+ }
139108
+ #sign(secretKey) {
139109
+ return _Updater.sign(this.#sourceDocument.id, this.#unsignedUpdate, this.#verificationMethod, secretKey);
139110
+ }
139111
+ // ─── State machine ─────────────────────────────────────────────────────────
139112
+ /**
139113
+ * Advance the state machine. Returns either:
139114
+ * - `{ status: 'action-required', needs }` — caller must provide data via {@link provide}
139115
+ * - `{ status: 'complete', result }` — update is signed and broadcast
139116
+ */
139117
+ advance() {
139118
+ while (true) {
139119
+ switch (this.#phase) {
139120
+ // Phase: Construct
139121
+ // Build the unsigned update from source doc + patches. Pure, synchronous.
139122
+ case "Construct" /* Construct */: {
139123
+ this.#unsignedUpdate = this.#construct();
139124
+ this.#phase = "Sign" /* Sign */;
139125
+ continue;
139126
+ }
139127
+ // Phase: Sign
139128
+ // Emit NeedSigningKey — the caller supplies the secret key (or a KMS signature).
139129
+ case "Sign" /* Sign */: {
139130
+ return {
139131
+ status: "action-required",
139132
+ needs: [{
139133
+ kind: "NeedSigningKey",
139134
+ verificationMethodId: this.#verificationMethod.id,
139135
+ unsignedUpdate: this.#unsignedUpdate
139136
+ }]
139137
+ };
139138
+ }
139139
+ // Phase: Fund
139140
+ // Emit NeedFunding with the beacon address. The caller checks UTXOs,
139141
+ // funds the address if needed, and provides to continue.
139142
+ case "Fund" /* Fund */: {
139143
+ const beaconAddress = this.#beaconService.serviceEndpoint.replace("bitcoin:", "");
139144
+ return {
139145
+ status: "action-required",
139146
+ needs: [{
139147
+ kind: "NeedFunding",
139148
+ beaconAddress,
139149
+ beaconService: this.#beaconService
139150
+ }]
139151
+ };
139152
+ }
139153
+ // Phase: Broadcast
139154
+ // Emit NeedBroadcast with the signed update + beacon service. The caller performs
139155
+ // the actual on-chain announcement (or hands off to the aggregation protocol).
139156
+ case "Broadcast" /* Broadcast */: {
139157
+ return {
139158
+ status: "action-required",
139159
+ needs: [{
139160
+ kind: "NeedBroadcast",
139161
+ beaconService: this.#beaconService,
139162
+ signedUpdate: this.#signedUpdate
139163
+ }]
139164
+ };
139165
+ }
139166
+ // Phase: Complete
139167
+ case "Complete" /* Complete */: {
139168
+ return {
139169
+ status: "complete",
139170
+ result: { signedUpdate: this.#signedUpdate }
139171
+ };
139172
+ }
139173
+ }
139174
+ }
139175
+ }
139176
+ provide(need, data) {
139177
+ switch (need.kind) {
139178
+ case "NeedSigningKey": {
139179
+ if (this.#phase !== "Sign" /* Sign */) {
139180
+ throw new UpdateError(
139181
+ `Cannot provide NeedSigningKey: updater phase is ${this.#phase}, expected Sign.`,
139182
+ INVALID_DID_UPDATE,
139183
+ { phase: this.#phase }
139184
+ );
139185
+ }
139186
+ if (!data) {
139187
+ throw new UpdateError(
139188
+ "NeedSigningKey requires secret key bytes.",
139189
+ INVALID_DID_UPDATE
139190
+ );
139191
+ }
139192
+ if (!this.#unsignedUpdate) {
139193
+ throw new UpdateError(
139194
+ "Internal error: unsigned update missing in Sign phase.",
139195
+ INVALID_DID_UPDATE
139196
+ );
139197
+ }
139198
+ this.#signedUpdate = this.#sign(data);
139199
+ this.#phase = "Fund" /* Fund */;
139200
+ break;
139201
+ }
139202
+ case "NeedFunding": {
139203
+ if (this.#phase !== "Fund" /* Fund */) {
139204
+ throw new UpdateError(
139205
+ `Cannot provide NeedFunding: updater phase is ${this.#phase}, expected Fund.`,
139206
+ INVALID_DID_UPDATE,
139207
+ { phase: this.#phase }
139208
+ );
139209
+ }
139210
+ this.#phase = "Broadcast" /* Broadcast */;
139211
+ break;
139212
+ }
139213
+ case "NeedBroadcast": {
139214
+ if (this.#phase !== "Broadcast" /* Broadcast */) {
139215
+ throw new UpdateError(
139216
+ `Cannot provide NeedBroadcast: updater phase is ${this.#phase}, expected Broadcast.`,
139217
+ INVALID_DID_UPDATE,
139218
+ { phase: this.#phase }
139219
+ );
139220
+ }
139221
+ this.#phase = "Complete" /* Complete */;
139222
+ break;
139223
+ }
139224
+ }
139082
139225
  }
139083
139226
  };
139084
139227
 
@@ -139148,42 +139291,35 @@ a=end-of-candidates
139148
139291
  });
139149
139292
  }
139150
139293
  /**
139151
- * Entry point for section {@link https://dcdpr.github.io/did-btcr2/#read | 7.3 Update}.
139152
- * See specification for the {@link https://dcdpr.github.io/did-btcr2/operations/resolve.html#process | Resolve Process}.
139153
- * See {@link Update | Update (class)} for class implementation.
139154
- *
139155
- * BTCR2 DID documents can be updated by anchoring BTCR2 Updates to Bitcoin transactions. These transactions MAY be
139156
- * published to the Bitcoin network. Any property in the DID document may be updated except the id. Doing so would
139157
- * invalidate the DID document.
139158
- * @param params An object containing the parameters for the update operation.
139294
+ * Entry point for section {@link https://dcdpr.github.io/did-btcr2/#update | 7.3 Update}.
139295
+ *
139296
+ * Factory method that validates the update parameters and returns a sans-I/O
139297
+ * {@link Updater} state machine. The caller drives the updater through its
139298
+ * phases (Construct Sign Broadcast Complete) by calling `advance()` and
139299
+ * `provide()`. The method package performs **zero I/O** signing key retrieval
139300
+ * (or KMS delegation) and the on-chain broadcast are the caller's responsibility.
139301
+ *
139302
+ * For a fully-wired version with Bitcoin broadcast and key handling, see
139303
+ * `DidMethodApi.update()` in `@did-btcr2/api`.
139304
+ *
139305
+ * @param params Update construction parameters.
139159
139306
  * @param {Btcr2DidDocument} params.sourceDocument The DID document being updated.
139160
- * @param {PatchOperation[]} params.patches The array of JSON Patch operations to apply to the sourceDocument.
139161
- * @param {string} params.sourceVersionId The version ID before applying the update.
139162
- * @param {string} params.verificationMethodId The verificationMethod ID to sign the update with.
139163
- * @param {string} params.beaconId The beacon ID associated with the update.
139164
- * @param {KeyBytes | HexString} [params.signingMaterial] Optional signing material (key bytes or hex string).
139165
- * @param {BitcoinConnection} [params.bitcoin] Optional Bitcoin network connection for announcing the update. If not provided, a default connection will be initialized.
139166
- * @return {Promise<SignedBTCR2Update>} Promise resolving to the signed BTCR2 update.
139167
- * @throws {UpdateError} if no verificationMethod, verificationMethod type is not `Multikey` or the publicKeyMultibase
139168
- * header is not `zQ3s`
139307
+ * @param {PatchOperation[]} params.patches The JSON Patch operations to apply.
139308
+ * @param {number} params.sourceVersionId The version ID before applying the update.
139309
+ * @param {string} params.verificationMethodId The verification method ID to sign with.
139310
+ * @param {string} params.beaconId The beacon service ID to broadcast through.
139311
+ * @returns {Updater} A sans-I/O state machine for driving the update.
139312
+ * @throws {UpdateError} If the verification method is not authorized, not found,
139313
+ * not of type `Multikey`, or does not have a `zQ3s` publicKeyMultibase prefix.
139314
+ * Also throws if the beacon service is not found.
139169
139315
  */
139170
- static async update({
139316
+ static update({
139171
139317
  sourceDocument,
139172
139318
  patches,
139173
139319
  sourceVersionId,
139174
139320
  verificationMethodId,
139175
- beaconId,
139176
- signingMaterial,
139177
- bitcoin: bitcoin3
139321
+ beaconId
139178
139322
  }) {
139179
- if (!signingMaterial) {
139180
- throw new UpdateError(
139181
- "Missing signing material for update",
139182
- INVALID_DID_UPDATE,
139183
- { signingMaterial }
139184
- );
139185
- }
139186
- const secretKey = typeof signingMaterial === "string" ? hexToBytes(signingMaterial) : signingMaterial;
139187
139323
  if (!sourceDocument.capabilityInvocation?.some((vr) => vr === verificationMethodId)) {
139188
139324
  throw new UpdateError(
139189
139325
  "Invalid verificationMethodId: not authorized for capabilityInvocation",
@@ -139213,8 +139349,6 @@ a=end-of-candidates
139213
139349
  verificationMethod
139214
139350
  );
139215
139351
  }
139216
- const update = Update.construct(sourceDocument, patches, sourceVersionId);
139217
- const signed = Update.sign(sourceDocument.id, update, verificationMethod, secretKey);
139218
139352
  const beaconService = sourceDocument.service.filter((service) => service.id === beaconId).filter((service) => !!service).shift();
139219
139353
  if (!beaconService) {
139220
139354
  throw new UpdateError(
@@ -139223,15 +139357,13 @@ a=end-of-candidates
139223
139357
  { sourceDocument, beaconId }
139224
139358
  );
139225
139359
  }
139226
- if (!bitcoin3) {
139227
- throw new UpdateError(
139228
- "Bitcoin connection required for update. Pass a configured `bitcoin` parameter or use DidBtcr2Api which injects it automatically.",
139229
- INVALID_DID_UPDATE,
139230
- { beaconId }
139231
- );
139232
- }
139233
- await Update.announce(beaconService, signed, secretKey, bitcoin3);
139234
- return signed;
139360
+ return new Updater({
139361
+ sourceDocument,
139362
+ patches,
139363
+ sourceVersionId,
139364
+ verificationMethod,
139365
+ beaconService
139366
+ });
139235
139367
  }
139236
139368
  /**
139237
139369
  * Given the W3C DID Document of a `did:btcr2` identifier, return the signing verification method that will be used