@did-btcr2/method 0.19.0 → 0.20.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 +715 -1445
- package/dist/browser.mjs +715 -1445
- package/dist/cjs/core/beacon/beacon.js +25 -0
- package/dist/cjs/core/beacon/beacon.js.map +1 -0
- package/dist/cjs/core/beacon/cas-beacon.js +20 -36
- package/dist/cjs/core/beacon/cas-beacon.js.map +1 -1
- package/dist/cjs/core/beacon/error.js +4 -4
- package/dist/cjs/core/beacon/error.js.map +1 -1
- package/dist/cjs/core/beacon/factory.js +5 -7
- package/dist/cjs/core/beacon/factory.js.map +1 -1
- package/dist/cjs/core/beacon/interfaces.js +1 -31
- package/dist/cjs/core/beacon/interfaces.js.map +1 -1
- package/dist/cjs/core/beacon/signal-discovery.js +183 -0
- package/dist/cjs/core/beacon/signal-discovery.js.map +1 -0
- package/dist/cjs/core/beacon/singleton.js +54 -81
- package/dist/cjs/core/beacon/singleton.js.map +1 -1
- package/dist/cjs/core/beacon/smt-beacon.js +22 -39
- package/dist/cjs/core/beacon/smt-beacon.js.map +1 -1
- package/dist/cjs/core/beacon/utils.js +4 -9
- package/dist/cjs/core/beacon/utils.js.map +1 -1
- package/dist/cjs/core/resolve.js +87 -277
- package/dist/cjs/core/resolve.js.map +1 -1
- package/dist/cjs/core/update.js +62 -153
- package/dist/cjs/core/update.js.map +1 -1
- package/dist/cjs/did-btcr2.js +80 -75
- package/dist/cjs/did-btcr2.js.map +1 -1
- package/dist/cjs/index.js +3 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/utils/appendix.js +6 -15
- package/dist/cjs/utils/appendix.js.map +1 -1
- package/dist/cjs/utils/did-document-builder.js +5 -0
- package/dist/cjs/utils/did-document-builder.js.map +1 -1
- package/dist/cjs/utils/did-document.js +11 -14
- package/dist/cjs/utils/did-document.js.map +1 -1
- package/dist/esm/core/beacon/beacon.js +25 -0
- package/dist/esm/core/beacon/beacon.js.map +1 -0
- package/dist/esm/core/beacon/cas-beacon.js +20 -36
- package/dist/esm/core/beacon/cas-beacon.js.map +1 -1
- package/dist/esm/core/beacon/error.js +4 -4
- package/dist/esm/core/beacon/error.js.map +1 -1
- package/dist/esm/core/beacon/factory.js +5 -7
- package/dist/esm/core/beacon/factory.js.map +1 -1
- package/dist/esm/core/beacon/interfaces.js +1 -31
- package/dist/esm/core/beacon/interfaces.js.map +1 -1
- package/dist/esm/core/beacon/signal-discovery.js +183 -0
- package/dist/esm/core/beacon/signal-discovery.js.map +1 -0
- package/dist/esm/core/beacon/singleton.js +54 -81
- package/dist/esm/core/beacon/singleton.js.map +1 -1
- package/dist/esm/core/beacon/smt-beacon.js +22 -39
- package/dist/esm/core/beacon/smt-beacon.js.map +1 -1
- package/dist/esm/core/beacon/utils.js +4 -9
- package/dist/esm/core/beacon/utils.js.map +1 -1
- package/dist/esm/core/resolve.js +87 -277
- package/dist/esm/core/resolve.js.map +1 -1
- package/dist/esm/core/update.js +62 -153
- package/dist/esm/core/update.js.map +1 -1
- package/dist/esm/did-btcr2.js +80 -75
- package/dist/esm/did-btcr2.js.map +1 -1
- package/dist/esm/index.js +3 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/utils/appendix.js +6 -15
- package/dist/esm/utils/appendix.js.map +1 -1
- package/dist/esm/utils/did-document-builder.js +5 -0
- package/dist/esm/utils/did-document-builder.js.map +1 -1
- package/dist/esm/utils/did-document.js +11 -14
- package/dist/esm/utils/did-document.js.map +1 -1
- package/dist/types/core/beacon/beacon.d.ts +44 -0
- package/dist/types/core/beacon/beacon.d.ts.map +1 -0
- package/dist/types/core/beacon/cas-beacon.d.ts +19 -30
- package/dist/types/core/beacon/cas-beacon.d.ts.map +1 -1
- package/dist/types/core/beacon/error.d.ts +2 -2
- package/dist/types/core/beacon/error.d.ts.map +1 -1
- package/dist/types/core/beacon/factory.d.ts +4 -6
- package/dist/types/core/beacon/factory.d.ts.map +1 -1
- package/dist/types/core/beacon/interfaces.d.ts +7 -46
- package/dist/types/core/beacon/interfaces.d.ts.map +1 -1
- package/dist/types/core/beacon/signal-discovery.d.ts +25 -0
- package/dist/types/core/beacon/signal-discovery.d.ts.map +1 -0
- package/dist/types/core/beacon/singleton.d.ts +16 -29
- package/dist/types/core/beacon/singleton.d.ts.map +1 -1
- package/dist/types/core/beacon/smt-beacon.d.ts +21 -33
- package/dist/types/core/beacon/smt-beacon.d.ts.map +1 -1
- package/dist/types/core/beacon/utils.d.ts.map +1 -1
- package/dist/types/core/interfaces.d.ts +1 -8
- package/dist/types/core/interfaces.d.ts.map +1 -1
- package/dist/types/core/resolve.d.ts +27 -43
- package/dist/types/core/resolve.d.ts.map +1 -1
- package/dist/types/core/types.d.ts +21 -8
- package/dist/types/core/types.d.ts.map +1 -1
- package/dist/types/core/update.d.ts +30 -73
- package/dist/types/core/update.d.ts.map +1 -1
- package/dist/types/did-btcr2.d.ts +37 -44
- package/dist/types/did-btcr2.d.ts.map +1 -1
- package/dist/types/index.d.ts +3 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/utils/appendix.d.ts.map +1 -1
- package/dist/types/utils/did-document-builder.d.ts +5 -0
- package/dist/types/utils/did-document-builder.d.ts.map +1 -1
- package/dist/types/utils/did-document.d.ts +16 -14
- package/dist/types/utils/did-document.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/core/beacon/beacon.ts +58 -0
- package/src/core/beacon/cas-beacon.ts +30 -44
- package/src/core/beacon/error.ts +5 -6
- package/src/core/beacon/factory.ts +7 -9
- package/src/core/beacon/interfaces.ts +7 -64
- package/src/core/beacon/signal-discovery.ts +237 -0
- package/src/core/beacon/singleton.ts +74 -94
- package/src/core/beacon/smt-beacon.ts +32 -49
- package/src/core/beacon/utils.ts +16 -13
- package/src/core/interfaces.ts +1 -9
- package/src/core/resolve.ts +116 -360
- package/src/core/types.ts +25 -8
- package/src/core/update.ts +90 -235
- package/src/did-btcr2.ts +131 -95
- package/src/index.ts +8 -1
- package/src/utils/appendix.ts +8 -22
- package/src/utils/did-document-builder.ts +5 -0
- package/src/utils/did-document.ts +41 -45
|
@@ -1,56 +1,39 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { Beacon } from './beacon.js';
|
|
2
|
+
import { SMTBeaconError } from './error.js';
|
|
3
3
|
/**
|
|
4
|
-
* TODO: Finish implementation
|
|
5
4
|
* Implements {@link https://dcdpr.github.io/did-btcr2/terminology.html#smt-beacon | SMTBeacon}.
|
|
6
5
|
* @class SMTBeacon
|
|
7
6
|
* @type {SMTBeacon}
|
|
8
|
-
* @extends {
|
|
7
|
+
* @extends {Beacon}
|
|
9
8
|
*/
|
|
10
|
-
export class SMTBeacon extends
|
|
9
|
+
export class SMTBeacon extends Beacon {
|
|
11
10
|
/**
|
|
12
|
-
* Creates an instance of
|
|
11
|
+
* Creates an instance of SMTBeacon.
|
|
13
12
|
* @param {BeaconService} service The Beacon service.
|
|
14
|
-
* @param {Array<BeaconSignal>} signals The SingletonBeacon sidecar data.
|
|
15
|
-
* @param {SidecarData} sidecar The sidecar data.
|
|
16
13
|
*/
|
|
17
|
-
constructor(service
|
|
18
|
-
super({ ...service, type: 'SMTBeacon' }
|
|
14
|
+
constructor(service) {
|
|
15
|
+
super({ ...service, type: 'SMTBeacon' });
|
|
19
16
|
}
|
|
20
17
|
/**
|
|
21
|
-
*
|
|
22
|
-
* @param {
|
|
23
|
-
* @param {SidecarData} sidecar The sidecar data.
|
|
24
|
-
* @returns {
|
|
18
|
+
* Implements {@link https://dcdpr.github.io/did-btcr2/operations/resolve.html#process-smt-beacon | 7.2.e.1 Process SMT Beacon}.
|
|
19
|
+
* @param {Array<BeaconSignal>} signals The array of Beacon Signals to process.
|
|
20
|
+
* @param {SidecarData} sidecar The sidecar data associated with the SMT Beacon.
|
|
21
|
+
* @returns {Promise<Array<[SignedBTCR2Update, BlockMetadata]>>} The processed signals.
|
|
22
|
+
* @throws {SMTBeaconError} if processing fails.
|
|
25
23
|
*/
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
processSignals(signals, sidecar) {
|
|
25
|
+
throw new SMTBeaconError('Method not implemented.', `METHOD_NOT_IMPLEMENTED`, { signals, sidecar });
|
|
28
26
|
}
|
|
29
27
|
/**
|
|
30
|
-
*
|
|
31
|
-
* @param {
|
|
32
|
-
* @
|
|
33
|
-
* @
|
|
28
|
+
* Broadcast CAS Beacon signal to the Bitcoin network.
|
|
29
|
+
* @param {SignedBTCR2Update} signedUpdate The signed BTCR2 update to broadcast.
|
|
30
|
+
* @param {KeyBytes} secretKey The secret key for signing the Bitcoin transaction.
|
|
31
|
+
* @param {BitcoinNetworkConnection} bitcoin The Bitcoin network connection.
|
|
32
|
+
* @return {Promise<SignedBTCR2Update>} The signed update that was broadcasted.
|
|
33
|
+
* @throws {SMTBeaconError} if broadcasting fails.
|
|
34
34
|
*/
|
|
35
|
-
|
|
36
|
-
throw new
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Process SMTBeacon signals.
|
|
40
|
-
* @returns {Promise<Array<BTCR2SignedUpdate>>} The processed signed update or undefined.
|
|
41
|
-
* @throws {MethodError} if the signal processing fails.
|
|
42
|
-
*/
|
|
43
|
-
async processSignals() {
|
|
44
|
-
throw new MethodError('Method not implemented.', `METHOD_NOT_IMPLEMENTED`);
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Broadcast a SMTBeacon signal.
|
|
48
|
-
* @param {HexString} updateHash The hash of the BTCR2 update to broadcast.
|
|
49
|
-
* @returns {Promise<SignalsMetadata>} The result of the broadcast.
|
|
50
|
-
* @throws {MethodError} if the broadcast fails.
|
|
51
|
-
*/
|
|
52
|
-
async broadcastSignal(updateHash) {
|
|
53
|
-
throw new MethodError('Method not implemented.', `METHOD_NOT_IMPLEMENTED`, { updateHash });
|
|
35
|
+
async broadcastSignal(signedUpdate, secretKey, bitcoin) {
|
|
36
|
+
throw new SMTBeaconError('Method not implemented.', `METHOD_NOT_IMPLEMENTED`, { signedUpdate, secretKey, bitcoin });
|
|
54
37
|
}
|
|
55
38
|
}
|
|
56
39
|
//# sourceMappingURL=smt-beacon.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"smt-beacon.js","sourceRoot":"","sources":["../../../../src/core/beacon/smt-beacon.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"smt-beacon.js","sourceRoot":"","sources":["../../../../src/core/beacon/smt-beacon.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAG5C;;;;;GAKG;AACH,MAAM,OAAO,SAAU,SAAQ,MAAM;IACnC;;;OAGG;IACH,YAAY,OAAsB;QAChC,KAAK,CAAC,EAAE,GAAG,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC,CAAC;IAC3C,CAAC;IAED;;;;;;OAMG;IACH,cAAc,CACZ,OAA4B,EAC5B,OAAoB;QAEpB,MAAM,IAAI,cAAc,CAAC,yBAAyB,EAAE,wBAAwB,EAAE,EAAC,OAAO,EAAE,OAAO,EAAC,CAAC,CAAC;IACpG,CAAC;IAED;;;;;;;OAOG;IACH,KAAK,CAAC,eAAe,CACnB,YAA+B,EAC/B,SAAmB,EACnB,OAAiC;QAEjC,MAAM,IAAI,cAAc,CAAC,yBAAyB,EAAE,wBAAwB,EAAE,EAAC,YAAY,EAAE,SAAS,EAAE,OAAO,EAAC,CAAC,CAAC;IACpH,CAAC;CACF"}
|
|
@@ -3,6 +3,7 @@ import { DidMethodError, MethodError } from '@did-btcr2/common';
|
|
|
3
3
|
import { payments } from 'bitcoinjs-lib';
|
|
4
4
|
import { Appendix } from '../../utils/appendix.js';
|
|
5
5
|
import { Identifier } from '../identifier.js';
|
|
6
|
+
import { BeaconError } from './error.js';
|
|
6
7
|
/**
|
|
7
8
|
* Static class of utility functions for the Beacon Service
|
|
8
9
|
* @class BeaconUtils
|
|
@@ -36,9 +37,6 @@ export class BeaconUtils {
|
|
|
36
37
|
// Return false if the serviceEndpoint is not a valid BIP21 bitcoin address.
|
|
37
38
|
if ([obj.serviceEndpoint].flat().some(ep => typeof ep === 'string' && !ep.startsWith('bitcoin:')))
|
|
38
39
|
return false;
|
|
39
|
-
// Return false if the casType exists and is not a string.
|
|
40
|
-
if (obj.casType && typeof obj.casType !== 'string')
|
|
41
|
-
return false;
|
|
42
40
|
// Else return true
|
|
43
41
|
return true;
|
|
44
42
|
}
|
|
@@ -66,8 +64,7 @@ export class BeaconUtils {
|
|
|
66
64
|
return addrTypes.map((addrType) => this.createBeaconService(did, addrType, beaconType));
|
|
67
65
|
}
|
|
68
66
|
catch (error) {
|
|
69
|
-
|
|
70
|
-
process.exit(1);
|
|
67
|
+
throw new BeaconError('Failed to create beacon services: ' + error.message, 'BEACON_SERVICE_ERROR', { did, beaconType });
|
|
71
68
|
}
|
|
72
69
|
}
|
|
73
70
|
/**
|
|
@@ -94,8 +91,7 @@ export class BeaconUtils {
|
|
|
94
91
|
return { id, type: beaconType, serviceEndpoint, };
|
|
95
92
|
}
|
|
96
93
|
catch (error) {
|
|
97
|
-
|
|
98
|
-
process.exit(1);
|
|
94
|
+
throw new BeaconError('Failed to create beacon service: ' + error.message, 'BEACON_SERVICE_ERROR', { did, beaconType });
|
|
99
95
|
}
|
|
100
96
|
}
|
|
101
97
|
/**
|
|
@@ -134,8 +130,7 @@ export class BeaconUtils {
|
|
|
134
130
|
];
|
|
135
131
|
}
|
|
136
132
|
catch (error) {
|
|
137
|
-
|
|
138
|
-
process.exit(1);
|
|
133
|
+
throw new BeaconError('Failed to create beacon services: ' + error.message, 'BEACON_SERVICE_ERROR', { id, publicKey, network, beaconType });
|
|
139
134
|
}
|
|
140
135
|
}
|
|
141
136
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../../src/core/beacon/utils.ts"],"names":[],"mappings":"AAAA,OAA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jD,OAAO,IAAI,CAAC,iBAAiB,CAAC,WAAW,CAAC,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IACxE,CAAC;CACF"}
|
package/dist/cjs/core/resolve.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { DateUtils, IdentifierHrp, INVALID_DID, INVALID_DID_DOCUMENT, INVALID_DID_UPDATE, JSONPatch, JSONUtils, LATE_PUBLISHING_ERROR,
|
|
1
|
+
import { getNetwork } from '@did-btcr2/bitcoin';
|
|
2
|
+
import { DateUtils, IdentifierHrp, INVALID_DID, INVALID_DID_DOCUMENT, INVALID_DID_UPDATE, JSONPatch, JSONUtils, LATE_PUBLISHING_ERROR, MISSING_UPDATE_DATA, ResolveError } from '@did-btcr2/common';
|
|
3
3
|
import { BIP340Cryptosuite, BIP340DataIntegrityProof, SchnorrMultikey } from '@did-btcr2/cryptosuite';
|
|
4
4
|
import { CompressedSecp256k1PublicKey } from '@did-btcr2/keypair';
|
|
5
5
|
import { bytesToHex } from '@noble/hashes/utils';
|
|
@@ -7,6 +7,7 @@ import { canonicalization, DidBtcr2 } from '../did-btcr2.js';
|
|
|
7
7
|
import { Appendix } from '../utils/appendix.js';
|
|
8
8
|
import { DidDocument, ID_PLACEHOLDER_VALUE } from '../utils/did-document.js';
|
|
9
9
|
import { BeaconFactory } from './beacon/factory.js';
|
|
10
|
+
import { BeaconSignalDiscovery } from './beacon/signal-discovery.js';
|
|
10
11
|
import { BeaconUtils } from './beacon/utils.js';
|
|
11
12
|
import { Identifier } from './identifier.js';
|
|
12
13
|
/**
|
|
@@ -19,62 +20,6 @@ import { Identifier } from './identifier.js';
|
|
|
19
20
|
* @type {Resolve}
|
|
20
21
|
*/
|
|
21
22
|
export class Resolve {
|
|
22
|
-
/**
|
|
23
|
-
* Implements subsection {@link https://dcdpr.github.io/did-btcr2/operations/resolve.html#process-sidecar-data | Process Sidecar Data}
|
|
24
|
-
* @param {Sidecar} sidecar The sidecar data to process.
|
|
25
|
-
* @returns {SidecarData} The processed sidecar data containing maps of updates, CAS announcements, and SMT proofs.
|
|
26
|
-
*/
|
|
27
|
-
static processSidecarData(sidecar = {}) {
|
|
28
|
-
// BTCR2 Signed Updates map
|
|
29
|
-
const updateMap = new Map();
|
|
30
|
-
if (sidecar.updates?.length)
|
|
31
|
-
for (const update of sidecar.updates) {
|
|
32
|
-
updateMap.set(canonicalization.process(update, { encoding: 'hex' }), update);
|
|
33
|
-
}
|
|
34
|
-
// CAS Announcements map
|
|
35
|
-
const casMap = new Map();
|
|
36
|
-
if (sidecar.casUpdates?.length)
|
|
37
|
-
for (const update of sidecar.casUpdates) {
|
|
38
|
-
casMap.set(canonicalization.process(update, { encoding: 'hex' }), update);
|
|
39
|
-
}
|
|
40
|
-
// SMT Proofs map
|
|
41
|
-
const smtMap = new Map();
|
|
42
|
-
if (sidecar.smtProofs?.length)
|
|
43
|
-
for (const proof of sidecar.smtProofs) {
|
|
44
|
-
smtMap.set(proof.id, proof);
|
|
45
|
-
}
|
|
46
|
-
return { updateMap, casMap, smtMap };
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Implements subsection {@link https://dcdpr.github.io/did-btcr2/operations/resolve.html#establish-current-document | 7.2.d Establish current_document}.
|
|
50
|
-
* Resolution begins by creating an Initial Did Document called current_document (Current DID Document).
|
|
51
|
-
* The current_document is iteratively patched with BTCR2 Signed Updates announced by Authorized Beacon Signals.
|
|
52
|
-
* @param {DidComponents} didComponents The decoded components of the did.
|
|
53
|
-
* @param {GenesisDocument} genesisDocument The genesis document for resolving the DID Document.
|
|
54
|
-
* @returns {Promise<DidDocument>} The resolved DID Document object.
|
|
55
|
-
* @throws {DidError} if the DID hrp is invalid, no sidecarData passed and hrp = "x".
|
|
56
|
-
*/
|
|
57
|
-
static async establishCurrentDocument(didComponents, genesisDocument) {
|
|
58
|
-
// Deconstruct the hrp from the components
|
|
59
|
-
const { hrp, genesisBytes } = didComponents;
|
|
60
|
-
// If hrp `x`, perform external resolution
|
|
61
|
-
if (hrp === IdentifierHrp.x) {
|
|
62
|
-
if (!genesisDocument)
|
|
63
|
-
throw new ResolveError('External resolution requires genesisDocument', MISSING_UPDATE_DATA, { didComponents });
|
|
64
|
-
return await this.external(didComponents, genesisDocument);
|
|
65
|
-
}
|
|
66
|
-
// Check for hrp `k`
|
|
67
|
-
if (hrp === IdentifierHrp.k) {
|
|
68
|
-
// Validate genesis bytes as a compressed secp256k1 public key
|
|
69
|
-
if (!CompressedSecp256k1PublicKey.isValid(genesisBytes)) {
|
|
70
|
-
throw new ResolveError('Deterministic resolution requires valid secp256k1 public key', INVALID_DID, { genesisBytes });
|
|
71
|
-
}
|
|
72
|
-
// Perform deterministic resolution
|
|
73
|
-
return this.deterministic(didComponents);
|
|
74
|
-
}
|
|
75
|
-
// Else, throw an error for unsupported hrp
|
|
76
|
-
throw new ResolveError(`Unsupported DID hrp ${hrp}`, INVALID_DID, { hrp });
|
|
77
|
-
}
|
|
78
23
|
/**
|
|
79
24
|
* 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}.
|
|
80
25
|
* @param {DidComponents} didComponents The decoded components of the did.
|
|
@@ -111,7 +56,7 @@ export class Resolve {
|
|
|
111
56
|
* @param {DidComponents} didComponents BTCR2 DID components used to resolve the DID Document
|
|
112
57
|
* @param {GenesisDocument} genesisDocument The genesis document for resolving the DID Document.
|
|
113
58
|
* @returns {Promise<DidDocument>} The resolved DID Document object
|
|
114
|
-
* @throws {
|
|
59
|
+
* @throws {ResolveError} InvalidDidDocument if not conformant to DID Core v1.1
|
|
115
60
|
*/
|
|
116
61
|
static async external(didComponents, genesisDocument) {
|
|
117
62
|
// Canonicalize and sha256 hash the currentDocument
|
|
@@ -120,7 +65,7 @@ export class Resolve {
|
|
|
120
65
|
const genesisBytes = bytesToHex(didComponents.genesisBytes);
|
|
121
66
|
// If the genesisBytes do not match the hashBytes, throw an error
|
|
122
67
|
if (genesisBytes !== hashBytes) {
|
|
123
|
-
throw new
|
|
68
|
+
throw new ResolveError(`Initial document mismatch: genesisBytes ${genesisBytes} !== hashBytes ${hashBytes}`, INVALID_DID_DOCUMENT, { genesisBytes, hashBytes });
|
|
124
69
|
}
|
|
125
70
|
// Encode the did from the didComponents
|
|
126
71
|
const did = Identifier.encode(didComponents);
|
|
@@ -129,42 +74,94 @@ export class Resolve {
|
|
|
129
74
|
// Return a W3C conformant DID Document
|
|
130
75
|
return new DidDocument(currentDocument);
|
|
131
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* Implements subsection {@link https://dcdpr.github.io/did-btcr2/operations/resolve.html#process-sidecar-data | Process Sidecar Data}
|
|
79
|
+
* @param {Sidecar} sidecar The sidecar data to process.
|
|
80
|
+
* @returns {ProcessedSidecar} The processed sidecar data containing maps of updates, CAS announcements, and SMT proofs.
|
|
81
|
+
*/
|
|
82
|
+
static sidecarData(sidecar = {}) {
|
|
83
|
+
// BTCR2 Signed Updates map
|
|
84
|
+
const updateMap = new Map();
|
|
85
|
+
if (sidecar.updates?.length)
|
|
86
|
+
for (const update of sidecar.updates) {
|
|
87
|
+
updateMap.set(canonicalization.process(update, { encoding: 'hex' }), update);
|
|
88
|
+
}
|
|
89
|
+
// CAS Announcements map
|
|
90
|
+
const casMap = new Map();
|
|
91
|
+
if (sidecar.casUpdates?.length)
|
|
92
|
+
for (const update of sidecar.casUpdates) {
|
|
93
|
+
casMap.set(canonicalization.process(update, { encoding: 'hex' }), update);
|
|
94
|
+
}
|
|
95
|
+
// SMT Proofs map
|
|
96
|
+
const smtMap = new Map();
|
|
97
|
+
if (sidecar.smtProofs?.length)
|
|
98
|
+
for (const proof of sidecar.smtProofs) {
|
|
99
|
+
smtMap.set(proof.id, proof);
|
|
100
|
+
}
|
|
101
|
+
return { updateMap, casMap, smtMap };
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Implements subsection {@link https://dcdpr.github.io/did-btcr2/operations/resolve.html#establish-current-document | 7.2.d Establish current_document}.
|
|
105
|
+
* Resolution begins by creating an Initial Did Document called current_document (Current DID Document).
|
|
106
|
+
* The current_document is iteratively patched with BTCR2 Signed Updates announced by Authorized Beacon Signals.
|
|
107
|
+
* @param {DidComponents} didComponents The decoded components of the did.
|
|
108
|
+
* @param {GenesisDocument} genesisDocument The genesis document for resolving the DID Document.
|
|
109
|
+
* @returns {Promise<DidDocument>} The resolved DID Document object.
|
|
110
|
+
* @throws {ResolveError} if the DID hrp is invalid, no sidecarData passed and hrp = "x".
|
|
111
|
+
*/
|
|
112
|
+
static async currentDocument(didComponents, genesisDocument) {
|
|
113
|
+
// Deconstruct the hrp from the components
|
|
114
|
+
const { hrp, genesisBytes } = didComponents;
|
|
115
|
+
// If hrp `x`, perform external resolution
|
|
116
|
+
if (hrp === IdentifierHrp.x) {
|
|
117
|
+
if (!genesisDocument)
|
|
118
|
+
throw new ResolveError('External resolution requires genesisDocument', MISSING_UPDATE_DATA, { didComponents });
|
|
119
|
+
return await this.external(didComponents, genesisDocument);
|
|
120
|
+
}
|
|
121
|
+
// Check for hrp `k`
|
|
122
|
+
if (hrp === IdentifierHrp.k) {
|
|
123
|
+
// Validate genesis bytes as a compressed secp256k1 public key
|
|
124
|
+
if (!CompressedSecp256k1PublicKey.isValid(genesisBytes)) {
|
|
125
|
+
throw new ResolveError('Deterministic resolution requires valid secp256k1 public key', INVALID_DID, { genesisBytes });
|
|
126
|
+
}
|
|
127
|
+
// Perform deterministic resolution
|
|
128
|
+
return this.deterministic(didComponents);
|
|
129
|
+
}
|
|
130
|
+
// Else, throw an error for unsupported hrp
|
|
131
|
+
throw new ResolveError(`Unsupported DID hrp ${hrp}`, INVALID_DID, { hrp });
|
|
132
|
+
}
|
|
132
133
|
/**
|
|
133
134
|
* Finds uses the beacon services in the currentDocument to scan for onchain Beacon Signals (transactions) containing
|
|
134
135
|
* Signal Bytes (last output in OP_RETURN transaction).
|
|
135
136
|
* @param {Array<BeaconService>} beaconServices The array of BeaconService objects to search for signals.
|
|
136
137
|
* @param {SidecarData} sidecarData The sidecar data containing maps of updates, CAS announcements, and SMT proofs.
|
|
137
138
|
* @param {BitcoinNetworkConnection} bitcoin The bitcoin network connection used to fetch beacon signals
|
|
138
|
-
* @
|
|
139
|
-
* @returns {Promise<Array<[BTCR2SignedUpdate, BlockMetadata]>>} The array of BTCR2 Signed Updates announced by the Beacon Signals.
|
|
139
|
+
* @returns {Promise<Array<[SignedBTCR2Update, BlockMetadata]>>} The array of BTCR2 Signed Updates announced by the Beacon Signals.
|
|
140
140
|
*/
|
|
141
|
-
static async
|
|
142
|
-
//
|
|
143
|
-
const beaconServicesSignals =
|
|
144
|
-
? await
|
|
145
|
-
: await
|
|
146
|
-
//
|
|
147
|
-
const
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
unsortedUpdates.push(...processed);
|
|
156
|
-
}
|
|
157
|
-
return unsortedUpdates;
|
|
141
|
+
static async beaconSignals(beaconServices, sidecarData, bitcoin) {
|
|
142
|
+
// Discover Beacon Signals via indexer server (esplora/electrs) or full node
|
|
143
|
+
const beaconServicesSignals = bitcoin.network.rest
|
|
144
|
+
? await BeaconSignalDiscovery.indexer(beaconServices, bitcoin)
|
|
145
|
+
: await BeaconSignalDiscovery.fullnode(beaconServices, bitcoin);
|
|
146
|
+
// Process each beacon's signals in parallel
|
|
147
|
+
const promises = Array.from(beaconServicesSignals.entries()).map(async ([service, signals]) => {
|
|
148
|
+
// Skip beacons with no signals
|
|
149
|
+
if (!signals.length)
|
|
150
|
+
return [];
|
|
151
|
+
// Establish a typed beacon and process its signals
|
|
152
|
+
return BeaconFactory.establish(service).processSignals(signals, sidecarData);
|
|
153
|
+
});
|
|
154
|
+
return (await Promise.all(promises)).flat();
|
|
158
155
|
}
|
|
159
156
|
/**
|
|
160
157
|
* Implements subsection {@link https://dcdpr.github.io/did-btcr2/operations/resolve.html#process-updates | 7.2.f Process updates Array}.
|
|
161
158
|
* @param {DidDocument} currentDocument The current DID Document to apply the updates to.
|
|
162
|
-
* @param {Array<[
|
|
159
|
+
* @param {Array<[SignedBTCR2Update, BlockMetadata]>} unsortedUpdates The unsorted array of BTCR2 Signed Updates and their associated Block Metadata.
|
|
163
160
|
* @param {string} [versionTime] The optional version time to limit updates to.
|
|
164
161
|
* @param {string} [versionId] The optional version id to limit updates to.
|
|
165
162
|
* @returns {Promise<DidResolutionResponse>} The updated DID Document, number of confirmations, and version id.
|
|
166
163
|
*/
|
|
167
|
-
static async
|
|
164
|
+
static async updates(currentDocument, unsortedUpdates, versionTime, versionId) {
|
|
168
165
|
// Start the version number being processed at 1
|
|
169
166
|
let currentVersionId = 1;
|
|
170
167
|
// Initialize an empty array to hold the update hashes
|
|
@@ -198,7 +195,7 @@ export class Resolve {
|
|
|
198
195
|
// If update.targetVersionId <= currentVersionId, confirm duplicate update
|
|
199
196
|
if (update.targetVersionId <= currentVersionId) {
|
|
200
197
|
updateHashHistory.push(currentDocumentHash);
|
|
201
|
-
this.
|
|
198
|
+
this.confirmDuplicate(update, updateHashHistory);
|
|
202
199
|
}
|
|
203
200
|
// If update.targetVersionId == currentVersionId + 1, apply the update
|
|
204
201
|
else if (update.targetVersionId === currentVersionId + 1) {
|
|
@@ -210,7 +207,7 @@ export class Resolve {
|
|
|
210
207
|
throw new ResolveError(`Hash mismatch: update.sourceHash !== currentDocumentHash`, INVALID_DID_UPDATE, { sourceHash, currentDocumentHash });
|
|
211
208
|
}
|
|
212
209
|
// Apply the update to the currentDocument and set it in the response
|
|
213
|
-
response.currentDocument = await this.
|
|
210
|
+
response.currentDocument = await this.applyUpdate(response.currentDocument, update);
|
|
214
211
|
// Create unsigned_update by removing the proof property from update.
|
|
215
212
|
const unsignedUpdate = JSONUtils.deleteKeys(update, ['proof']);
|
|
216
213
|
// Push the canonicalized unsigned update hash to the updateHashHistory
|
|
@@ -239,198 +236,13 @@ export class Resolve {
|
|
|
239
236
|
// Return response data
|
|
240
237
|
return response;
|
|
241
238
|
}
|
|
242
|
-
/**
|
|
243
|
-
* Retrieves the beacon signals for the given array of BeaconService objects
|
|
244
|
-
* using a esplora/electrs REST API connection via a bitcoin I/O driver.
|
|
245
|
-
* @param {Array<BeaconService>} beaconServices Array of BeaconService objects to retrieve signals for
|
|
246
|
-
* @param {BitcoinNetworkConnection} bitcoin Bitcoin network connection to use for REST calls
|
|
247
|
-
* @returns {Promise<Array<BeaconSignal>>} Promise resolving to an array of BeaconSignal objects
|
|
248
|
-
*/
|
|
249
|
-
static async queryBlockchainIndexer(beaconServices, bitcoin) {
|
|
250
|
-
// Empty array of beaconSignals
|
|
251
|
-
const beaconServiceSignals = new Map();
|
|
252
|
-
// Iterate over each beacon
|
|
253
|
-
for (const beaconService of beaconServices) {
|
|
254
|
-
beaconServiceSignals.set(beaconService, []);
|
|
255
|
-
// Get the transactions for the beacon address via REST
|
|
256
|
-
const beaconSignals = await bitcoin.network.rest.address.getTxs(beaconService.serviceEndpoint);
|
|
257
|
-
// If no signals are found, continue
|
|
258
|
-
if (!beaconSignals || !beaconSignals.length) {
|
|
259
|
-
continue;
|
|
260
|
-
}
|
|
261
|
-
// Iterate over each signal
|
|
262
|
-
for (const beaconSignal of beaconSignals) {
|
|
263
|
-
// Get the last vout in the transaction
|
|
264
|
-
const signalVout = beaconSignal.vout.slice(-1)[0];
|
|
265
|
-
/**
|
|
266
|
-
* Look for OP_RETURN in last vout scriptpubkey_asm
|
|
267
|
-
* Vout (rest) format:
|
|
268
|
-
* {
|
|
269
|
-
* scriptpubkey: '6a20570f177c65e64fb5cf61180b664cdddf09ab76153c2b192e22006e5b22a3917a',
|
|
270
|
-
* scriptpubkey_asm: 'OP_RETURN OP_PUSHBYTES_32 570f177c65e64fb5cf61180b664cdddf09ab76153c2b192e22006e5b22a3917a',
|
|
271
|
-
* scriptpubkey_type: 'op_return',
|
|
272
|
-
* value: 0
|
|
273
|
-
* }
|
|
274
|
-
*/
|
|
275
|
-
if (!signalVout || !signalVout.scriptpubkey_asm.includes('OP_RETURN')) {
|
|
276
|
-
// If not found, continue to next signal
|
|
277
|
-
continue;
|
|
278
|
-
}
|
|
279
|
-
// Construct output map for easier access
|
|
280
|
-
const outputMap = new Map(Object.entries(signalVout));
|
|
281
|
-
// Grab the signal vout scriptpubkey
|
|
282
|
-
const signalVoutScriptPubkey = outputMap.get('scriptpubkey_asm');
|
|
283
|
-
// If the signal vout scriptpubkey does not exist, continue to next signal
|
|
284
|
-
if (!signalVoutScriptPubkey) {
|
|
285
|
-
continue;
|
|
286
|
-
}
|
|
287
|
-
// Extract hex string hash of the signal bytes from the scriptpubkey
|
|
288
|
-
const updateHash = signalVoutScriptPubkey.split(' ').slice(-1)[0];
|
|
289
|
-
if (!updateHash) {
|
|
290
|
-
continue;
|
|
291
|
-
}
|
|
292
|
-
const confirmations = await bitcoin.network.rest.block.count() - beaconSignal.status.block_height + 1;
|
|
293
|
-
// Push the beacon signal object to the signals array for the beacon service
|
|
294
|
-
beaconServiceSignals.get(beaconService)?.push({
|
|
295
|
-
tx: beaconSignal,
|
|
296
|
-
signalBytes: updateHash,
|
|
297
|
-
blockMetadata: {
|
|
298
|
-
confirmations,
|
|
299
|
-
height: beaconSignal.status.block_height,
|
|
300
|
-
time: beaconSignal.status.block_time,
|
|
301
|
-
}
|
|
302
|
-
});
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
// Return the beaconSignals
|
|
306
|
-
return beaconServiceSignals;
|
|
307
|
-
}
|
|
308
|
-
/**
|
|
309
|
-
* Traverse the full blockchain from genesis to chain top looking for beacon signals.
|
|
310
|
-
* @param {Array<BeaconService>} beaconServices Array of BeaconService objects to search for signals.
|
|
311
|
-
* @param {BitcoinNetworkConnection} bitcoin Bitcoin network connection to use for RPC calls.
|
|
312
|
-
* @returns {Promise<Array<BeaconSignal>>} Promise resolving to an array of BeaconSignal objects.
|
|
313
|
-
*/
|
|
314
|
-
static async traverseFullBlockchain(beaconServices, bitcoin) {
|
|
315
|
-
const beaconServiceSignals = new Map();
|
|
316
|
-
for (const beaconService of beaconServices) {
|
|
317
|
-
beaconServiceSignals.set(beaconService, []);
|
|
318
|
-
}
|
|
319
|
-
// Get the RPC connection from the bitcoin network
|
|
320
|
-
const rpc = bitcoin.network.rpc;
|
|
321
|
-
// Ensure that the RPC connection is available
|
|
322
|
-
if (!rpc) {
|
|
323
|
-
throw new ResolveError('RPC connection is not available', 'RPC_CONNECTION_ERROR', bitcoin);
|
|
324
|
-
}
|
|
325
|
-
// Get the current block height
|
|
326
|
-
const targetHeight = await rpc.getBlockCount();
|
|
327
|
-
// Set genesis height
|
|
328
|
-
let height = 0;
|
|
329
|
-
// Opt into rpc connection to get the block data at the blockhash
|
|
330
|
-
let block = await bitcoin.network.rpc.getBlock({ height });
|
|
331
|
-
console.info(`Searching for beacon signals, please wait ...`);
|
|
332
|
-
while (block.height <= targetHeight) {
|
|
333
|
-
// Iterate over each transaction in the block
|
|
334
|
-
for (const tx of block.tx) {
|
|
335
|
-
// If the txid is a coinbase, continue ...
|
|
336
|
-
if (tx.txid === GENESIS_TX_ID) {
|
|
337
|
-
continue;
|
|
338
|
-
}
|
|
339
|
-
// Iterate over each input in the transaction
|
|
340
|
-
for (const vin of tx.vin) {
|
|
341
|
-
// If the vin is a coinbase transaction, continue ...
|
|
342
|
-
if (vin.coinbase) {
|
|
343
|
-
continue;
|
|
344
|
-
}
|
|
345
|
-
// If the vin txinwitness contains a coinbase did, continue ...
|
|
346
|
-
if (vin.txinwitness && vin.txinwitness.length === 1 && vin.txinwitness[0] === TXIN_WITNESS_COINBASE) {
|
|
347
|
-
continue;
|
|
348
|
-
}
|
|
349
|
-
// If the txid from the vin is undefined, continue ...
|
|
350
|
-
if (!vin.txid) {
|
|
351
|
-
continue;
|
|
352
|
-
}
|
|
353
|
-
// If the vout from the vin is undefined, continue ...
|
|
354
|
-
if (vin.vout === undefined) {
|
|
355
|
-
continue;
|
|
356
|
-
}
|
|
357
|
-
// Get the previous output transaction data
|
|
358
|
-
const prevout = await rpc.getRawTransaction(vin.txid, 2);
|
|
359
|
-
// If the previous output vout at the vin.vout index is undefined, continue ...
|
|
360
|
-
if (!prevout.vout[vin.vout]) {
|
|
361
|
-
continue;
|
|
362
|
-
}
|
|
363
|
-
// Get the address from the scriptPubKey from the prevvout (previous output's input at the vout index)
|
|
364
|
-
const scriptPubKey = prevout.vout[vin.vout].scriptPubKey;
|
|
365
|
-
// If the scriptPubKey.address is undefined, continue ...
|
|
366
|
-
if (!scriptPubKey.address) {
|
|
367
|
-
continue;
|
|
368
|
-
}
|
|
369
|
-
// If the beaconAddress from prevvout scriptPubKey is not a beacon service endpoint address, continue ...
|
|
370
|
-
const beaconService = BeaconUtils.getBeaconServicesMap(beaconServices).get(scriptPubKey.address);
|
|
371
|
-
if (!beaconService) {
|
|
372
|
-
continue;
|
|
373
|
-
}
|
|
374
|
-
/**
|
|
375
|
-
* Look for 'OP_RETURN' in prevout.vout[vin.vout].scriptPubKey.asm, else continue ...
|
|
376
|
-
*
|
|
377
|
-
* TxOut (rpc) format:
|
|
378
|
-
* {
|
|
379
|
-
* value: 0,
|
|
380
|
-
* n: 1,
|
|
381
|
-
* scriptPubKey: {
|
|
382
|
-
* asm: 'OP_RETURN 570f177c65e64fb5cf61180b664cdddf09ab76153c2b192e22006e5b22a3917a',
|
|
383
|
-
* desc: 'raw(6a20570f177c65e64fb5cf61180b664cdddf09ab76153c2b192e22006e5b22a3917a)#cdgj3pm4',
|
|
384
|
-
* hex: '6a20570f177c65e64fb5cf61180b664cdddf09ab76153c2b192e22006e5b22a3917a',
|
|
385
|
-
* type: 'nulldata'
|
|
386
|
-
* }
|
|
387
|
-
* }
|
|
388
|
-
*/
|
|
389
|
-
const txVoutScriptPubkeyAsm = prevout.vout[vin.vout].scriptPubKey.asm;
|
|
390
|
-
if (!txVoutScriptPubkeyAsm.includes('OP_RETURN')) {
|
|
391
|
-
continue;
|
|
392
|
-
}
|
|
393
|
-
// Log the found txid and beacon
|
|
394
|
-
console.info(`Tx ${tx.txid} contains beacon service address ${scriptPubKey.address} and OP_RETURN!`, tx);
|
|
395
|
-
// Extract hex string hash of the signal bytes from the scriptpubkey
|
|
396
|
-
const updateHash = txVoutScriptPubkeyAsm.split(' ').slice(-1)[0];
|
|
397
|
-
if (!updateHash) {
|
|
398
|
-
continue;
|
|
399
|
-
}
|
|
400
|
-
// Push the beacon signal object to the beacon signals array for that beacon service
|
|
401
|
-
beaconServiceSignals.get(beaconService)?.push({
|
|
402
|
-
tx,
|
|
403
|
-
signalBytes: updateHash,
|
|
404
|
-
blockMetadata: {
|
|
405
|
-
height: block.height,
|
|
406
|
-
time: block.time,
|
|
407
|
-
confirmations: block.confirmations
|
|
408
|
-
}
|
|
409
|
-
});
|
|
410
|
-
}
|
|
411
|
-
;
|
|
412
|
-
}
|
|
413
|
-
// Increment the height
|
|
414
|
-
height += 1;
|
|
415
|
-
// Check if we've reached the chain tip
|
|
416
|
-
const tip = await rpc.getBlockCount();
|
|
417
|
-
if (height > tip) {
|
|
418
|
-
// If so, break the loop
|
|
419
|
-
console.info(`Chain tip reached ${height}, breaking ...`);
|
|
420
|
-
break;
|
|
421
|
-
}
|
|
422
|
-
// Reset the block var to the next block data
|
|
423
|
-
block = await rpc.getBlock({ height });
|
|
424
|
-
}
|
|
425
|
-
return beaconServiceSignals;
|
|
426
|
-
}
|
|
427
239
|
/**
|
|
428
240
|
* Implements subsection {@link https://dcdpr.github.io/did-btcr2/#confirm-duplicate-update | 7.2.f.1 Confirm Duplicate Update}.
|
|
429
241
|
* This step confirms that an update with a lower-than-expected targetVersionId is a true duplicate.
|
|
430
|
-
* @param {
|
|
242
|
+
* @param {SignedBTCR2Update} update The BTCR2 Signed Update to confirm as a duplicate.
|
|
431
243
|
* @returns {void} Does not return a value, but throws an error if the update is not a valid duplicate.
|
|
432
244
|
*/
|
|
433
|
-
static
|
|
245
|
+
static confirmDuplicate(update, updateHashHistory) {
|
|
434
246
|
// Create unsigned_update by removing the proof property from update.
|
|
435
247
|
const unsignedUpdate = JSONUtils.deleteKeys(update, ['proof']);
|
|
436
248
|
// Hash unsignedUpdate with JSON Document Hashing algorithm
|
|
@@ -445,11 +257,11 @@ export class Resolve {
|
|
|
445
257
|
/**
|
|
446
258
|
* Implements subsection {@link https://dcdpr.github.io/did-btcr2/operations/resolve.html#apply-update | 7.2.f.3 Apply Update}.
|
|
447
259
|
* @param {DidDocument} currentDocument The current DID Document to apply the update to.
|
|
448
|
-
* @param {
|
|
260
|
+
* @param {SignedBTCR2Update} update The BTCR2 Signed Update to apply.
|
|
449
261
|
* @returns {Promise<DidDocument>} The updated DID Document after applying the update.
|
|
450
262
|
* @throws {ResolveError} If the update is invalid or cannot be applied.
|
|
451
263
|
*/
|
|
452
|
-
static async
|
|
264
|
+
static async applyUpdate(currentDocument, update) {
|
|
453
265
|
// Get the capability id from the to update proof.
|
|
454
266
|
const capabilityId = update.proof?.capability;
|
|
455
267
|
// Since this field is optional, check that it exists
|
|
@@ -475,10 +287,8 @@ export class Resolve {
|
|
|
475
287
|
}
|
|
476
288
|
// Get the verificationMethod from the DID Document using the verificationMethodId.
|
|
477
289
|
const vm = DidBtcr2.getSigningMethod(currentDocument, verificationMethodId);
|
|
478
|
-
// Split the vmId by the `#` to get the id and controller.
|
|
479
|
-
const [vmController, vmId] = vm.id.split('#');
|
|
480
290
|
// Construct a new SchnorrMultikey.
|
|
481
|
-
const multikey = SchnorrMultikey.
|
|
291
|
+
const multikey = SchnorrMultikey.fromVerificationMethod(vm);
|
|
482
292
|
// Construct a new BIP340Cryptosuite with the SchnorrMultikey.
|
|
483
293
|
const cryptosuite = new BIP340Cryptosuite(multikey);
|
|
484
294
|
// Canonicalize the update
|
|
@@ -489,7 +299,7 @@ export class Resolve {
|
|
|
489
299
|
const verificationResult = diProof.verifyProof(canonicalUpdate, 'capabilityInvocation');
|
|
490
300
|
// If the result is not verified, throw INVALID_DID_UPDATE error
|
|
491
301
|
if (!verificationResult.verified) {
|
|
492
|
-
throw new
|
|
302
|
+
throw new ResolveError('Invalid update: proof not verified', INVALID_DID_UPDATE, verificationResult);
|
|
493
303
|
}
|
|
494
304
|
// Apply the update.patch to the currentDocument to get the updatedDocument.
|
|
495
305
|
const updatedDocument = JSONPatch.apply(currentDocument, update.patch);
|
|
@@ -502,7 +312,7 @@ export class Resolve {
|
|
|
502
312
|
// Make sure the update.targetHash equals currentDocumentHash.
|
|
503
313
|
if (updateTargetHash !== currentDocumentHash) {
|
|
504
314
|
// If they do not match, throw INVALID_DID_UPDATE error.
|
|
505
|
-
throw new
|
|
315
|
+
throw new ResolveError(`Invalid update: updateTargetHash !== currentDocumentHash`, INVALID_DID_UPDATE, { updateTargetHash, currentDocumentHash });
|
|
506
316
|
}
|
|
507
317
|
// Return final updatedDocument.
|
|
508
318
|
return updatedDocument;
|