@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/README.md +38 -9
- package/dist/.tsbuildinfo +1 -1
- package/dist/browser.js +194 -62
- package/dist/browser.mjs +194 -62
- package/dist/cjs/index.js +201 -68
- package/dist/esm/core/aggregation/runner/participant-runner.js +4 -0
- package/dist/esm/core/aggregation/runner/participant-runner.js.map +1 -1
- package/dist/esm/core/updater.js +269 -0
- package/dist/esm/core/updater.js.map +1 -0
- package/dist/esm/did-btcr2.js +30 -42
- package/dist/esm/did-btcr2.js.map +1 -1
- package/dist/esm/index.js +2 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/types/core/aggregation/runner/participant-runner.d.ts +4 -0
- package/dist/types/core/aggregation/runner/participant-runner.d.ts.map +1 -1
- package/dist/types/core/updater.d.ts +178 -0
- package/dist/types/core/updater.d.ts.map +1 -0
- package/dist/types/did-btcr2.d.ts +23 -23
- package/dist/types/did-btcr2.d.ts.map +1 -1
- package/dist/types/index.d.ts +2 -1
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/core/aggregation/runner/participant-runner.ts +4 -0
- package/src/core/updater.ts +415 -0
- package/src/did-btcr2.ts +36 -66
- package/src/index.ts +2 -1
- package/dist/esm/core/update.js +0 -112
- package/dist/esm/core/update.js.map +0 -1
- package/dist/types/core/update.d.ts +0 -52
- package/dist/types/core/update.d.ts.map +0 -1
- package/src/core/update.ts +0 -158
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { DocumentBytes, HexString, KeyBytes, PatchOperation } from '@did-btcr2/common';
|
|
3
|
-
import type { SignedBTCR2Update } from '@did-btcr2/cryptosuite';
|
|
1
|
+
import type { DocumentBytes, KeyBytes, PatchOperation } from '@did-btcr2/common';
|
|
4
2
|
import type { DidMethod } from '@web5/dids';
|
|
5
3
|
import type { ResolutionOptions } from './core/interfaces.js';
|
|
6
4
|
import { Resolver } from './core/resolver.js';
|
|
5
|
+
import { Updater } from './core/updater.js';
|
|
7
6
|
import type { Btcr2DidDocument, DidVerificationMethod } from './utils/did-document.js';
|
|
8
7
|
export interface DidCreateOptions {
|
|
9
8
|
/** Type of identifier to create (key or external) */
|
|
@@ -71,34 +70,35 @@ export declare class DidBtcr2 implements DidMethod {
|
|
|
71
70
|
*/
|
|
72
71
|
static resolve(did: string, resolutionOptions?: ResolutionOptions): Resolver;
|
|
73
72
|
/**
|
|
74
|
-
* Entry point for section {@link https://dcdpr.github.io/did-btcr2/#
|
|
75
|
-
* See specification for the {@link https://dcdpr.github.io/did-btcr2/operations/resolve.html#process | Resolve Process}.
|
|
76
|
-
* See {@link Update | Update (class)} for class implementation.
|
|
73
|
+
* Entry point for section {@link https://dcdpr.github.io/did-btcr2/#update | 7.3 Update}.
|
|
77
74
|
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
75
|
+
* Factory method that validates the update parameters and returns a sans-I/O
|
|
76
|
+
* {@link Updater} state machine. The caller drives the updater through its
|
|
77
|
+
* phases (Construct → Sign → Broadcast → Complete) by calling `advance()` and
|
|
78
|
+
* `provide()`. The method package performs **zero I/O** — signing key retrieval
|
|
79
|
+
* (or KMS delegation) and the on-chain broadcast are the caller's responsibility.
|
|
80
|
+
*
|
|
81
|
+
* For a fully-wired version with Bitcoin broadcast and key handling, see
|
|
82
|
+
* `DidMethodApi.update()` in `@did-btcr2/api`.
|
|
83
|
+
*
|
|
84
|
+
* @param params Update construction parameters.
|
|
82
85
|
* @param {Btcr2DidDocument} params.sourceDocument The DID document being updated.
|
|
83
|
-
* @param {PatchOperation[]} params.patches The
|
|
84
|
-
* @param {
|
|
85
|
-
* @param {string} params.verificationMethodId The
|
|
86
|
-
* @param {string} params.beaconId The beacon ID
|
|
87
|
-
* @
|
|
88
|
-
* @
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
* header is not `zQ3s`
|
|
86
|
+
* @param {PatchOperation[]} params.patches The JSON Patch operations to apply.
|
|
87
|
+
* @param {number} params.sourceVersionId The version ID before applying the update.
|
|
88
|
+
* @param {string} params.verificationMethodId The verification method ID to sign with.
|
|
89
|
+
* @param {string} params.beaconId The beacon service ID to broadcast through.
|
|
90
|
+
* @returns {Updater} A sans-I/O state machine for driving the update.
|
|
91
|
+
* @throws {UpdateError} If the verification method is not authorized, not found,
|
|
92
|
+
* not of type `Multikey`, or does not have a `zQ3s` publicKeyMultibase prefix.
|
|
93
|
+
* Also throws if the beacon service is not found.
|
|
92
94
|
*/
|
|
93
|
-
static update({ sourceDocument, patches, sourceVersionId, verificationMethodId, beaconId,
|
|
95
|
+
static update({ sourceDocument, patches, sourceVersionId, verificationMethodId, beaconId, }: {
|
|
94
96
|
sourceDocument: Btcr2DidDocument;
|
|
95
97
|
patches: PatchOperation[];
|
|
96
98
|
sourceVersionId: number;
|
|
97
99
|
verificationMethodId: string;
|
|
98
100
|
beaconId: string;
|
|
99
|
-
|
|
100
|
-
bitcoin?: BitcoinConnection;
|
|
101
|
-
}): Promise<SignedBTCR2Update>;
|
|
101
|
+
}): Updater;
|
|
102
102
|
/**
|
|
103
103
|
* Given the W3C DID Document of a `did:btcr2` identifier, return the signing verification method that will be used
|
|
104
104
|
* for signing messages and credentials. If given, the `methodId` parameter is used to select the
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"did-btcr2.d.ts","sourceRoot":"","sources":["../../src/did-btcr2.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"did-btcr2.d.ts","sourceRoot":"","sources":["../../src/did-btcr2.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,aAAa,EACb,QAAQ,EACR,cAAc,EAAC,MAAM,mBAAmB,CAAC;AAS3C,OAAO,KAAK,EACV,SAAS,EAAC,MAAM,YAAY,CAAC;AAU/B,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAC9D,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAE5C,OAAO,KAAK,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AAEvF,MAAM,WAAW,gBAAgB;IAC/B,qDAAqD;IACrD,MAAM,EAAE,MAAM,CAAC;IACf,+BAA+B;IAC/B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,sBAAsB;IACtB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAKD;;;;;;;;;;;;GAYG;AACH,qBAAa,QAAS,YAAW,SAAS;IACxC;;OAEG;IACH,MAAM,CAAC,UAAU,EAAE,MAAM,CAAW;IAEpC;;;;;;;;;;;;;;;OAeG;IACH,MAAM,CAAC,MAAM,CAAC,YAAY,EAAE,QAAQ,GAAG,aAAa,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,MAAM;IAezF;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,MAAM,CAAC,OAAO,CACZ,GAAG,EAAE,MAAM,EACX,iBAAiB,GAAE,iBAAsB,GACxC,QAAQ;IAsBX;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACH,MAAM,CAAC,MAAM,CAAC,EACZ,cAAc,EACd,OAAO,EACP,eAAe,EACf,oBAAoB,EACpB,QAAQ,GACT,EAAE;QACD,cAAc,EAAE,gBAAgB,CAAC;QACjC,OAAO,EAAE,cAAc,EAAE,CAAC;QAC1B,eAAe,EAAE,MAAM,CAAC;QACxB,oBAAoB,EAAE,MAAM,CAAC;QAC7B,QAAQ,EAAE,MAAM,CAAC;KAClB,GAAG,OAAO;IA2DX;;;;;;;;;OASG;IACH,MAAM,CAAC,gBAAgB,CAAC,WAAW,EAAE,gBAAgB,EAAG,QAAQ,CAAC,EAAE,MAAM,GAAG,qBAAqB;CA0BlG"}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -11,6 +11,7 @@ export * from './core/beacon/beacon.js';
|
|
|
11
11
|
export * from './core/beacon/cas-beacon.js';
|
|
12
12
|
export * from './core/beacon/error.js';
|
|
13
13
|
export * from './core/beacon/factory.js';
|
|
14
|
+
export * from './core/beacon/fee-estimator.js';
|
|
14
15
|
export * from './core/beacon/interfaces.js';
|
|
15
16
|
export * from './core/beacon/signal-discovery.js';
|
|
16
17
|
export * from './core/beacon/singleton-beacon.js';
|
|
@@ -20,7 +21,7 @@ export * from './core/identifier.js';
|
|
|
20
21
|
export * from './core/interfaces.js';
|
|
21
22
|
export * from './core/resolver.js';
|
|
22
23
|
export * from './core/types.js';
|
|
23
|
-
export * from './core/
|
|
24
|
+
export * from './core/updater.js';
|
|
24
25
|
export * from './utils/appendix.js';
|
|
25
26
|
export * from './utils/did-document-builder.js';
|
|
26
27
|
export * from './utils/did-document.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,cAAc,+BAA+B,CAAC;AAC9C,cAAc,mCAAmC,CAAC;AAClD,cAAc,8BAA8B,CAAC;AAC7C,cAAc,uCAAuC,CAAC;AACtD,cAAc,8BAA8B,CAAC;AAC7C,cAAc,8BAA8B,CAAC;AAC7C,cAAc,sCAAsC,CAAC;AACrD,cAAc,uCAAuC,CAAC;AACtD,cAAc,oCAAoC,CAAC;AAGnD,cAAc,yBAAyB,CAAC;AACxC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,wBAAwB,CAAC;AACvC,cAAc,0BAA0B,CAAC;AACzC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,mCAAmC,CAAC;AAClD,cAAc,mCAAmC,CAAC;AAClD,cAAc,6BAA6B,CAAC;AAC5C,cAAc,wBAAwB,CAAC;AAGvC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,cAAc,+BAA+B,CAAC;AAC9C,cAAc,mCAAmC,CAAC;AAClD,cAAc,8BAA8B,CAAC;AAC7C,cAAc,uCAAuC,CAAC;AACtD,cAAc,8BAA8B,CAAC;AAC7C,cAAc,8BAA8B,CAAC;AAC7C,cAAc,sCAAsC,CAAC;AACrD,cAAc,uCAAuC,CAAC;AACtD,cAAc,oCAAoC,CAAC;AAGnD,cAAc,yBAAyB,CAAC;AACxC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,wBAAwB,CAAC;AACvC,cAAc,0BAA0B,CAAC;AACzC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,mCAAmC,CAAC;AAClD,cAAc,mCAAmC,CAAC;AAClD,cAAc,6BAA6B,CAAC;AAC5C,cAAc,wBAAwB,CAAC;AAGvC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,mBAAmB,CAAC;AAGlC,cAAc,qBAAqB,CAAC;AACpC,cAAc,iCAAiC,CAAC;AAChD,cAAc,yBAAyB,CAAC;AAGxC,cAAc,gBAAgB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@did-btcr2/method",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.28.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Reference implementation for the did:btcr2 DID method written in TypeScript and JavaScript. did:btcr2 is a censorship resistant DID Method using the Bitcoin blockchain as a Verifiable Data Registry to announce changes to the DID document. This is the core method implementation for the did-btcr2-js monorepo.",
|
|
6
6
|
"main": "./dist/cjs/index.js",
|
|
@@ -83,10 +83,10 @@
|
|
|
83
83
|
"multiformats": "^13.4.2",
|
|
84
84
|
"nostr-tools": "^2.23.3",
|
|
85
85
|
"@did-btcr2/bitcoin": "^0.5.3",
|
|
86
|
-
"@did-btcr2/common": "^9.0.0",
|
|
87
86
|
"@did-btcr2/cryptosuite": "^6.0.6",
|
|
88
87
|
"@did-btcr2/smt": "^0.2.4",
|
|
89
|
-
"@did-btcr2/keypair": "^0.11.4"
|
|
88
|
+
"@did-btcr2/keypair": "^0.11.4",
|
|
89
|
+
"@did-btcr2/common": "^9.0.0"
|
|
90
90
|
},
|
|
91
91
|
"devDependencies": {
|
|
92
92
|
"@eslint/js": "^9.39.4",
|
|
@@ -87,7 +87,11 @@ export interface AggregationParticipantRunnerOptions {
|
|
|
87
87
|
* keys: myKeys,
|
|
88
88
|
* shouldJoin: async (advert) => advert.beaconType === 'CASBeacon',
|
|
89
89
|
* onProvideUpdate: async ({ beaconAddress }) => {
|
|
90
|
+
<<<<<<< Updated upstream
|
|
90
91
|
* return Update.sign(myDid, unsigned, vm, secretKey);
|
|
92
|
+
=======
|
|
93
|
+
* return Updater.sign(myDid, unsigned, vm, secretKey);
|
|
94
|
+
>>>>>>> Stashed changes
|
|
91
95
|
* },
|
|
92
96
|
* });
|
|
93
97
|
*
|
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
import type { BitcoinConnection } from '@did-btcr2/bitcoin';
|
|
2
|
+
import type { KeyBytes, PatchOperation } from '@did-btcr2/common';
|
|
3
|
+
import { canonicalHash, INVALID_DID_UPDATE, JSONPatch, UpdateError } from '@did-btcr2/common';
|
|
4
|
+
import { SchnorrMultikey, type DataIntegrityConfig, type SignedBTCR2Update, type UnsignedBTCR2Update } from '@did-btcr2/cryptosuite';
|
|
5
|
+
import { DidDocument, type Btcr2DidDocument, type DidVerificationMethod } from '../utils/did-document.js';
|
|
6
|
+
import { BeaconFactory } from './beacon/factory.js';
|
|
7
|
+
import type { BeaconService } from './beacon/interfaces.js';
|
|
8
|
+
|
|
9
|
+
// ─── DataNeed types ──────────────────────────────────────────────────────────
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The updater needs the caller to supply a signing key (or a KMS-backed signature)
|
|
13
|
+
* for the given verification method. The unsigned update is attached so the caller
|
|
14
|
+
* can inspect it before producing a signature.
|
|
15
|
+
*/
|
|
16
|
+
export interface NeedSigningKey {
|
|
17
|
+
readonly kind: 'NeedSigningKey';
|
|
18
|
+
/** The verification method ID that requires a signing key. */
|
|
19
|
+
readonly verificationMethodId: string;
|
|
20
|
+
/** The unsigned update that will be signed. */
|
|
21
|
+
readonly unsignedUpdate: UnsignedBTCR2Update;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The updater needs the caller to ensure the beacon address is funded before
|
|
26
|
+
* broadcasting. The caller checks the beacon address for UTXOs, funds it if
|
|
27
|
+
* needed, and then calls `updater.provide(need)` to continue.
|
|
28
|
+
*
|
|
29
|
+
* If the beacon is already funded, the caller can provide immediately (no-op).
|
|
30
|
+
*/
|
|
31
|
+
export interface NeedFunding {
|
|
32
|
+
readonly kind: 'NeedFunding';
|
|
33
|
+
/** The Bitcoin address that must have a spendable UTXO for broadcast. */
|
|
34
|
+
readonly beaconAddress: string;
|
|
35
|
+
/** The beacon service this address belongs to. */
|
|
36
|
+
readonly beaconService: BeaconService;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The updater needs the caller to broadcast the signed update via the beacon.
|
|
41
|
+
*
|
|
42
|
+
* The caller decides how: for single-party beacons, call
|
|
43
|
+
* `Updater.announce(beaconService, signedUpdate, secretKey, bitcoin)` or
|
|
44
|
+
* `BeaconFactory.establish(beaconService).broadcastSignal(...)`. For multi-party
|
|
45
|
+
* aggregate beacons, hand off to the aggregation protocol.
|
|
46
|
+
*
|
|
47
|
+
* After the broadcast succeeds, the caller calls `updater.provide(need)` (with no
|
|
48
|
+
* data) to transition the updater to Complete.
|
|
49
|
+
*/
|
|
50
|
+
export interface NeedBroadcast {
|
|
51
|
+
readonly kind: 'NeedBroadcast';
|
|
52
|
+
/** The beacon service to broadcast through. Inspect `beaconService.type` to decide the path. */
|
|
53
|
+
readonly beaconService: BeaconService;
|
|
54
|
+
/** The signed update ready for broadcast. */
|
|
55
|
+
readonly signedUpdate: SignedBTCR2Update;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Discriminated union of all data needs the updater may request from the caller. */
|
|
59
|
+
export type UpdaterDataNeed = NeedSigningKey | NeedFunding | NeedBroadcast;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The result returned by the updater when it reaches the Complete phase.
|
|
63
|
+
*/
|
|
64
|
+
export interface UpdaterResult {
|
|
65
|
+
/** The signed update that was constructed, signed, and broadcast. */
|
|
66
|
+
signedUpdate: SignedBTCR2Update;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Output of {@link Updater.advance}. Either the updater needs data from the
|
|
71
|
+
* caller, or the update is complete.
|
|
72
|
+
*/
|
|
73
|
+
export type UpdaterState =
|
|
74
|
+
| { status: 'action-required'; needs: ReadonlyArray<UpdaterDataNeed> }
|
|
75
|
+
| { status: 'complete'; result: UpdaterResult };
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Internal phases of the Updater state machine.
|
|
79
|
+
* @internal
|
|
80
|
+
*/
|
|
81
|
+
enum UpdaterPhase {
|
|
82
|
+
Construct = 'Construct',
|
|
83
|
+
Sign = 'Sign',
|
|
84
|
+
Fund = 'Fund',
|
|
85
|
+
Broadcast = 'Broadcast',
|
|
86
|
+
Complete = 'Complete',
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Parameters for constructing an {@link Updater}. Built by
|
|
91
|
+
* {@link https://dcdpr.github.io/did-btcr2/operations/update.html | DidBtcr2.update}.
|
|
92
|
+
*/
|
|
93
|
+
export interface UpdaterParams {
|
|
94
|
+
sourceDocument: Btcr2DidDocument;
|
|
95
|
+
patches: PatchOperation[];
|
|
96
|
+
sourceVersionId: number;
|
|
97
|
+
verificationMethod: DidVerificationMethod;
|
|
98
|
+
beaconService: BeaconService;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Sans-I/O state machine for did:btcr2 updates — the counterpart to {@link Resolver}.
|
|
103
|
+
*
|
|
104
|
+
* Created by {@link DidBtcr2.update} (the factory). The caller drives the update by
|
|
105
|
+
* repeatedly calling {@link advance} and {@link provide}:
|
|
106
|
+
*
|
|
107
|
+
* ```typescript
|
|
108
|
+
* const updater = DidBtcr2.update({ sourceDocument, patches, ... });
|
|
109
|
+
* let state = updater.advance();
|
|
110
|
+
*
|
|
111
|
+
* while(state.status === 'action-required') {
|
|
112
|
+
* for(const need of state.needs) {
|
|
113
|
+
* switch(need.kind) {
|
|
114
|
+
* case 'NeedSigningKey':
|
|
115
|
+
* updater.provide(need, secretKeyBytes);
|
|
116
|
+
* break;
|
|
117
|
+
* case 'NeedFunding':
|
|
118
|
+
* // Check UTXOs at need.beaconAddress, fund if needed
|
|
119
|
+
* updater.provide(need);
|
|
120
|
+
* break;
|
|
121
|
+
* case 'NeedBroadcast':
|
|
122
|
+
* await Updater.announce(need.beaconService, need.signedUpdate, secretKey, bitcoin);
|
|
123
|
+
* updater.provide(need);
|
|
124
|
+
* break;
|
|
125
|
+
* }
|
|
126
|
+
* }
|
|
127
|
+
* state = updater.advance();
|
|
128
|
+
* }
|
|
129
|
+
*
|
|
130
|
+
* const { signedUpdate } = state.result;
|
|
131
|
+
* ```
|
|
132
|
+
*
|
|
133
|
+
* The Updater performs **zero I/O**. All external work (signing with a KMS or raw
|
|
134
|
+
* key, funding checks, Bitcoin transaction construction, broadcast) flows through
|
|
135
|
+
* the advance/provide protocol. This mirrors the {@link Resolver} pattern and makes
|
|
136
|
+
* the update path transport-agnostic and KMS-ready.
|
|
137
|
+
*
|
|
138
|
+
* The class also exposes static utility methods ({@link construct}, {@link sign},
|
|
139
|
+
* {@link announce}) for callers that need direct access to individual update steps
|
|
140
|
+
* outside the state machine (e.g., test vector generation scripts).
|
|
141
|
+
*
|
|
142
|
+
* @class Updater
|
|
143
|
+
*/
|
|
144
|
+
export class Updater {
|
|
145
|
+
#phase: UpdaterPhase = UpdaterPhase.Construct;
|
|
146
|
+
readonly #sourceDocument: Btcr2DidDocument;
|
|
147
|
+
readonly #patches: PatchOperation[];
|
|
148
|
+
readonly #sourceVersionId: number;
|
|
149
|
+
readonly #verificationMethod: DidVerificationMethod;
|
|
150
|
+
readonly #beaconService: BeaconService;
|
|
151
|
+
|
|
152
|
+
#unsignedUpdate: UnsignedBTCR2Update | null = null;
|
|
153
|
+
#signedUpdate: SignedBTCR2Update | null = null;
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* @internal — Use {@link DidBtcr2.update} to create instances.
|
|
157
|
+
*/
|
|
158
|
+
constructor(params: UpdaterParams) {
|
|
159
|
+
this.#sourceDocument = params.sourceDocument;
|
|
160
|
+
this.#patches = params.patches;
|
|
161
|
+
this.#sourceVersionId = params.sourceVersionId;
|
|
162
|
+
this.#verificationMethod = params.verificationMethod;
|
|
163
|
+
this.#beaconService = params.beaconService;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// ─── Public static utility methods ─────────────────────────────────────────
|
|
167
|
+
// Used by generate-vector.ts and other scripts that need direct access to
|
|
168
|
+
// individual update steps outside the state machine flow.
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Implements subsection {@link https://dcdpr.github.io/did-btcr2/operations/update.html#construct-btcr2-unsigned-update | 7.3.b Construct BTCR2 Unsigned Update}.
|
|
172
|
+
*
|
|
173
|
+
* @param {Btcr2DidDocument} sourceDocument The source DID document to be updated.
|
|
174
|
+
* @param {PatchOperation[]} patches The JSON Patch operations to apply.
|
|
175
|
+
* @param {number} sourceVersionId The version ID of the source document.
|
|
176
|
+
* @returns {UnsignedBTCR2Update} The constructed UnsignedBTCR2Update object.
|
|
177
|
+
* @throws {UpdateError} If the target document fails DID Core validation.
|
|
178
|
+
*/
|
|
179
|
+
static construct(
|
|
180
|
+
sourceDocument: Btcr2DidDocument,
|
|
181
|
+
patches: PatchOperation[],
|
|
182
|
+
sourceVersionId: number,
|
|
183
|
+
): UnsignedBTCR2Update {
|
|
184
|
+
const unsignedUpdate: UnsignedBTCR2Update = {
|
|
185
|
+
'@context' : [
|
|
186
|
+
'https://w3id.org/security/v2',
|
|
187
|
+
'https://w3id.org/zcap/v1',
|
|
188
|
+
'https://w3id.org/json-ld-patch/v1',
|
|
189
|
+
'https://btcr2.dev/context/v1'
|
|
190
|
+
],
|
|
191
|
+
patch : patches,
|
|
192
|
+
targetHash : '',
|
|
193
|
+
targetVersionId : sourceVersionId + 1,
|
|
194
|
+
sourceHash : canonicalHash(sourceDocument),
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
const targetDocument = JSONPatch.apply(sourceDocument, patches);
|
|
198
|
+
|
|
199
|
+
try {
|
|
200
|
+
DidDocument.isValid(targetDocument);
|
|
201
|
+
} catch (error) {
|
|
202
|
+
throw new UpdateError(
|
|
203
|
+
'Error validating targetDocument: ' + (error instanceof Error ? error.message : String(error)),
|
|
204
|
+
INVALID_DID_UPDATE, targetDocument
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
unsignedUpdate.targetHash = canonicalHash(targetDocument);
|
|
209
|
+
return unsignedUpdate;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Implements subsection {@link http://dcdpr.github.io/did-btcr2/operations/update.html#construct-btcr2-signed-update | 7.3.c Construct BTCR2 Signed Update }.
|
|
214
|
+
*
|
|
215
|
+
* @param {string} did The did-btcr2 identifier to derive the root capability from.
|
|
216
|
+
* @param {UnsignedBTCR2Update} unsignedUpdate The unsigned update to sign.
|
|
217
|
+
* @param {DidVerificationMethod} verificationMethod The verification method for signing.
|
|
218
|
+
* @param {KeyBytes} secretKey The secret key bytes.
|
|
219
|
+
* @returns {SignedBTCR2Update} The signed update with a Data Integrity proof.
|
|
220
|
+
*/
|
|
221
|
+
static sign(
|
|
222
|
+
did: string,
|
|
223
|
+
unsignedUpdate: UnsignedBTCR2Update,
|
|
224
|
+
verificationMethod: DidVerificationMethod,
|
|
225
|
+
secretKey: KeyBytes,
|
|
226
|
+
): SignedBTCR2Update {
|
|
227
|
+
const controller = verificationMethod.controller;
|
|
228
|
+
const id = verificationMethod.id.slice(verificationMethod.id.indexOf('#'));
|
|
229
|
+
const multikey = SchnorrMultikey.fromSecretKey(id, controller, secretKey);
|
|
230
|
+
|
|
231
|
+
const config: DataIntegrityConfig = {
|
|
232
|
+
'@context' : [
|
|
233
|
+
'https://w3id.org/security/v2',
|
|
234
|
+
'https://w3id.org/zcap/v1',
|
|
235
|
+
'https://w3id.org/json-ld-patch/v1',
|
|
236
|
+
'https://btcr2.dev/context/v1'
|
|
237
|
+
],
|
|
238
|
+
cryptosuite : 'bip340-jcs-2025',
|
|
239
|
+
type : 'DataIntegrityProof',
|
|
240
|
+
verificationMethod : verificationMethod.id,
|
|
241
|
+
proofPurpose : 'capabilityInvocation',
|
|
242
|
+
capability : `urn:zcap:root:${encodeURIComponent(did)}`,
|
|
243
|
+
capabilityAction : 'Write',
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
const diproof = multikey.toCryptosuite().toDataIntegrityProof();
|
|
247
|
+
return diproof.addProof(unsignedUpdate, config);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Implements subsection {@link https://dcdpr.github.io/did-btcr2/operations/update.html#announce-did-update | 7.3.d Announce DID Update}.
|
|
252
|
+
* Announces a signed update to the Bitcoin blockchain via the specified beacon.
|
|
253
|
+
*
|
|
254
|
+
* @param {BeaconService} beaconService The beacon service to broadcast through.
|
|
255
|
+
* @param {SignedBTCR2Update} update The signed update to announce.
|
|
256
|
+
* @param {KeyBytes} secretKey The secret key for signing the Bitcoin transaction.
|
|
257
|
+
* @param {BitcoinConnection} bitcoin The Bitcoin network connection.
|
|
258
|
+
* @returns {Promise<SignedBTCR2Update>} The signed update that was broadcast.
|
|
259
|
+
*/
|
|
260
|
+
static async announce(
|
|
261
|
+
beaconService: BeaconService,
|
|
262
|
+
update: SignedBTCR2Update,
|
|
263
|
+
secretKey: KeyBytes,
|
|
264
|
+
bitcoin: BitcoinConnection
|
|
265
|
+
): Promise<SignedBTCR2Update> {
|
|
266
|
+
const beacon = BeaconFactory.establish(beaconService);
|
|
267
|
+
return beacon.broadcastSignal(update, secretKey, bitcoin);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// ─── Private instance wrappers ─────────────────────────────────────────────
|
|
271
|
+
// Delegate to the public statics with bound instance fields for cleaner
|
|
272
|
+
// advance/provide code.
|
|
273
|
+
|
|
274
|
+
#construct(): UnsignedBTCR2Update {
|
|
275
|
+
return Updater.construct(this.#sourceDocument, this.#patches, this.#sourceVersionId);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
#sign(secretKey: KeyBytes): SignedBTCR2Update {
|
|
279
|
+
return Updater.sign(this.#sourceDocument.id, this.#unsignedUpdate!, this.#verificationMethod, secretKey);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// ─── State machine ─────────────────────────────────────────────────────────
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Advance the state machine. Returns either:
|
|
286
|
+
* - `{ status: 'action-required', needs }` — caller must provide data via {@link provide}
|
|
287
|
+
* - `{ status: 'complete', result }` — update is signed and broadcast
|
|
288
|
+
*/
|
|
289
|
+
advance(): UpdaterState {
|
|
290
|
+
while(true) {
|
|
291
|
+
switch(this.#phase) {
|
|
292
|
+
|
|
293
|
+
// Phase: Construct
|
|
294
|
+
// Build the unsigned update from source doc + patches. Pure, synchronous.
|
|
295
|
+
case UpdaterPhase.Construct: {
|
|
296
|
+
this.#unsignedUpdate = this.#construct();
|
|
297
|
+
this.#phase = UpdaterPhase.Sign;
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// Phase: Sign
|
|
302
|
+
// Emit NeedSigningKey — the caller supplies the secret key (or a KMS signature).
|
|
303
|
+
case UpdaterPhase.Sign: {
|
|
304
|
+
return {
|
|
305
|
+
status : 'action-required',
|
|
306
|
+
needs : [{
|
|
307
|
+
kind : 'NeedSigningKey',
|
|
308
|
+
verificationMethodId : this.#verificationMethod.id,
|
|
309
|
+
unsignedUpdate : this.#unsignedUpdate!,
|
|
310
|
+
}],
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// Phase: Fund
|
|
315
|
+
// Emit NeedFunding with the beacon address. The caller checks UTXOs,
|
|
316
|
+
// funds the address if needed, and provides to continue.
|
|
317
|
+
case UpdaterPhase.Fund: {
|
|
318
|
+
const beaconAddress = this.#beaconService.serviceEndpoint.replace('bitcoin:', '');
|
|
319
|
+
return {
|
|
320
|
+
status : 'action-required',
|
|
321
|
+
needs : [{
|
|
322
|
+
kind : 'NeedFunding',
|
|
323
|
+
beaconAddress,
|
|
324
|
+
beaconService : this.#beaconService,
|
|
325
|
+
}],
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// Phase: Broadcast
|
|
330
|
+
// Emit NeedBroadcast with the signed update + beacon service. The caller performs
|
|
331
|
+
// the actual on-chain announcement (or hands off to the aggregation protocol).
|
|
332
|
+
case UpdaterPhase.Broadcast: {
|
|
333
|
+
return {
|
|
334
|
+
status : 'action-required',
|
|
335
|
+
needs : [{
|
|
336
|
+
kind : 'NeedBroadcast',
|
|
337
|
+
beaconService : this.#beaconService,
|
|
338
|
+
signedUpdate : this.#signedUpdate!,
|
|
339
|
+
}],
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// Phase: Complete
|
|
344
|
+
case UpdaterPhase.Complete: {
|
|
345
|
+
return {
|
|
346
|
+
status : 'complete',
|
|
347
|
+
result : { signedUpdate: this.#signedUpdate! },
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Provide data the updater requested in a previous {@link advance} call.
|
|
356
|
+
* Call once per need, then call {@link advance} again to continue.
|
|
357
|
+
*
|
|
358
|
+
* @param need The DataNeed being fulfilled (from the `needs` array).
|
|
359
|
+
* @param data The data payload corresponding to the need kind (omit for NeedFunding/NeedBroadcast).
|
|
360
|
+
*/
|
|
361
|
+
provide(need: NeedSigningKey, data: KeyBytes): void;
|
|
362
|
+
provide(need: NeedFunding): void;
|
|
363
|
+
provide(need: NeedBroadcast): void;
|
|
364
|
+
provide(need: UpdaterDataNeed, data?: KeyBytes): void {
|
|
365
|
+
switch(need.kind) {
|
|
366
|
+
case 'NeedSigningKey': {
|
|
367
|
+
if(this.#phase !== UpdaterPhase.Sign) {
|
|
368
|
+
throw new UpdateError(
|
|
369
|
+
`Cannot provide NeedSigningKey: updater phase is ${this.#phase}, expected Sign.`,
|
|
370
|
+
INVALID_DID_UPDATE, { phase: this.#phase }
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
if(!data) {
|
|
374
|
+
throw new UpdateError(
|
|
375
|
+
'NeedSigningKey requires secret key bytes.',
|
|
376
|
+
INVALID_DID_UPDATE
|
|
377
|
+
);
|
|
378
|
+
}
|
|
379
|
+
if(!this.#unsignedUpdate) {
|
|
380
|
+
throw new UpdateError(
|
|
381
|
+
'Internal error: unsigned update missing in Sign phase.',
|
|
382
|
+
INVALID_DID_UPDATE
|
|
383
|
+
);
|
|
384
|
+
}
|
|
385
|
+
this.#signedUpdate = this.#sign(data);
|
|
386
|
+
this.#phase = UpdaterPhase.Fund;
|
|
387
|
+
break;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
case 'NeedFunding': {
|
|
391
|
+
if(this.#phase !== UpdaterPhase.Fund) {
|
|
392
|
+
throw new UpdateError(
|
|
393
|
+
`Cannot provide NeedFunding: updater phase is ${this.#phase}, expected Fund.`,
|
|
394
|
+
INVALID_DID_UPDATE, { phase: this.#phase }
|
|
395
|
+
);
|
|
396
|
+
}
|
|
397
|
+
// Caller has confirmed funding (or it was already funded). Continue.
|
|
398
|
+
this.#phase = UpdaterPhase.Broadcast;
|
|
399
|
+
break;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
case 'NeedBroadcast': {
|
|
403
|
+
if(this.#phase !== UpdaterPhase.Broadcast) {
|
|
404
|
+
throw new UpdateError(
|
|
405
|
+
`Cannot provide NeedBroadcast: updater phase is ${this.#phase}, expected Broadcast.`,
|
|
406
|
+
INVALID_DID_UPDATE, { phase: this.#phase }
|
|
407
|
+
);
|
|
408
|
+
}
|
|
409
|
+
// Caller has broadcast externally. Transition to Complete.
|
|
410
|
+
this.#phase = UpdaterPhase.Complete;
|
|
411
|
+
break;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
}
|