@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
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Abstract base class for all BTCR2 Beacon types.
|
|
3
|
+
* A Beacon is a service listed in a BTCR2 DID document that informs resolvers
|
|
4
|
+
* how to find authentic updates to the DID.
|
|
5
|
+
*
|
|
6
|
+
* Beacons are lightweight typed wrappers around a {@link BeaconService} configuration.
|
|
7
|
+
* Dependencies (signals, sidecar data, bitcoin connection) are passed as method
|
|
8
|
+
* parameters rather than held as instance state.
|
|
9
|
+
*
|
|
10
|
+
* Use {@link BeaconFactory.establish} to create typed instances from service config.
|
|
11
|
+
*
|
|
12
|
+
* @abstract
|
|
13
|
+
* @class Beacon
|
|
14
|
+
* @type {Beacon}
|
|
15
|
+
*/
|
|
16
|
+
export class Beacon {
|
|
17
|
+
/**
|
|
18
|
+
* The Beacon service configuration parsed from the DID Document.
|
|
19
|
+
*/
|
|
20
|
+
service;
|
|
21
|
+
constructor(service) {
|
|
22
|
+
this.service = service;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=beacon.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"beacon.js","sourceRoot":"","sources":["../../../../src/core/beacon/beacon.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;;GAcG;AACH,MAAM,OAAgB,MAAM;IAC1B;;OAEG;IACM,OAAO,CAAgB;IAEhC,YAAY,OAAsB;QAChC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;CA4BF"}
|
|
@@ -1,55 +1,39 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { Beacon } from './beacon.js';
|
|
2
|
+
import { CASBeaconError } from './error.js';
|
|
3
3
|
/**
|
|
4
4
|
* Implements {@link https://dcdpr.github.io/did-btcr2/terminology.html#cas-beacon | CAS Beacon}.
|
|
5
|
-
*
|
|
6
5
|
* @class CASBeacon
|
|
7
6
|
* @type {CASBeacon}
|
|
8
|
-
* @extends {
|
|
7
|
+
* @extends {Beacon}
|
|
9
8
|
*/
|
|
10
|
-
export class CASBeacon extends
|
|
9
|
+
export class CASBeacon extends Beacon {
|
|
11
10
|
/**
|
|
12
11
|
* Creates an instance of CASBeacon.
|
|
13
12
|
* @param {BeaconService} service The service of the Beacon.
|
|
14
|
-
* @param {?BeaconSidecarData} [sidecar] The sidecar data of the Beacon.
|
|
15
13
|
*/
|
|
16
|
-
constructor(service
|
|
17
|
-
super({ ...service, type: 'CASBeacon' }
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Establish a CASBeacon instance based on the provided service and sidecar data.
|
|
21
|
-
* @param {BeaconService} service - The beacon service configuration.
|
|
22
|
-
* @param {SidecarData} sidecar - The sidecar data.
|
|
23
|
-
* @returns {CASBeacon} The established CASBeacon instance.
|
|
24
|
-
*/
|
|
25
|
-
static establish(service, signals, sidecar) {
|
|
26
|
-
return new CASBeacon(service, signals, sidecar);
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* TODO: Figure out if this is necessary or not.
|
|
30
|
-
* @param {HexString} updateHash The hash of the update to generate the signal for.
|
|
31
|
-
* @returns {BeaconSignal} The generated signal.
|
|
32
|
-
* @throws {MethodError} if the signal is invalid.
|
|
33
|
-
*/
|
|
34
|
-
generateSignal(updateHash) {
|
|
35
|
-
throw new Error('Method not implemented.' + updateHash);
|
|
14
|
+
constructor(service) {
|
|
15
|
+
super({ ...service, type: 'CASBeacon' });
|
|
36
16
|
}
|
|
37
17
|
/**
|
|
38
18
|
* Implements {@link https://dcdpr.github.io/did-btcr2/operations/resolve.html#process-cas-beacon | 7.2.e.1 Process CAS Beacon}.
|
|
39
|
-
* @
|
|
40
|
-
* @
|
|
19
|
+
* @param {Array<BeaconSignal>} signals The array of Beacon Signals to process.
|
|
20
|
+
* @param {SidecarData} sidecar The sidecar data associated with the CAS Beacon.
|
|
21
|
+
* @returns {Promise<Array<[SignedBTCR2Update, BlockMetadata]>>} The processed signals.
|
|
22
|
+
* @throws {CASBeaconError} if processing fails.
|
|
41
23
|
*/
|
|
42
|
-
processSignals() {
|
|
43
|
-
throw new
|
|
24
|
+
processSignals(signals, sidecar) {
|
|
25
|
+
throw new CASBeaconError('Method not implemented.', `METHOD_NOT_IMPLEMENTED`, { signals, sidecar });
|
|
44
26
|
}
|
|
45
27
|
/**
|
|
46
|
-
*
|
|
47
|
-
* @param {
|
|
48
|
-
* @
|
|
49
|
-
* @
|
|
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 {CASBeaconError} if broadcasting fails.
|
|
50
34
|
*/
|
|
51
|
-
async broadcastSignal(
|
|
52
|
-
throw new
|
|
35
|
+
async broadcastSignal(signedUpdate, secretKey, bitcoin) {
|
|
36
|
+
throw new CASBeaconError('Method not implemented.', `METHOD_NOT_IMPLEMENTED`, { signedUpdate, secretKey, bitcoin });
|
|
53
37
|
}
|
|
54
38
|
}
|
|
55
39
|
//# sourceMappingURL=cas-beacon.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cas-beacon.js","sourceRoot":"","sources":["../../../../src/core/beacon/cas-beacon.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"cas-beacon.js","sourceRoot":"","sources":["../../../../src/core/beacon/cas-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,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC,CAAC;IACtG,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"}
|
|
@@ -24,13 +24,13 @@ export class AggregateBeaconError extends MethodError {
|
|
|
24
24
|
super(message, type, data);
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
|
-
export class
|
|
28
|
-
constructor(message, type = '
|
|
27
|
+
export class CASBeaconError extends MethodError {
|
|
28
|
+
constructor(message, type = 'CASBeaconError', data) {
|
|
29
29
|
super(message, type, data);
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
|
-
export class
|
|
33
|
-
constructor(message, type = '
|
|
32
|
+
export class SMTBeaconError extends MethodError {
|
|
33
|
+
constructor(message, type = 'SMTBeaconError', data) {
|
|
34
34
|
super(message, type, data);
|
|
35
35
|
}
|
|
36
36
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"error.js","sourceRoot":"","sources":["../../../../src/core/beacon/error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD,MAAM,OAAO,WAAY,SAAQ,WAAW;IAC1C,YAAY,OAAe,EAAE,OAAe,aAAa,EAAE,IAA0B;QACnF,KAAK,CAAC,OAAO,EAA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AW;IACnD,YAAY,OAAe,EAAE,OAAe,sBAAsB,EAAE,IAA0B;QAC5F,KAAK,CAAC,OAAO,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;IAC7B,CAAC;CACF;AAED,MAAM,OAAO,cAAe,SAAQ,WAAW;IAC7C,YAAY,OAAe,EAAE,OAAe,gBAAgB,EAAE,IAA0B;QACtF,KAAK,CAAC,OAAO,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;IAC7B,CAAC;CACF;AAED,MAAM,OAAO,cAAe,SAAQ,WAAW;IAC7C,YAAY,OAAe,EAAE,OAAe,gBAAgB,EAAE,IAA0B;QACtF,KAAK,CAAC,OAAO,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;IAC7B,CAAC;CACF"}
|
|
@@ -10,19 +10,17 @@ import { SMTBeacon } from './smt-beacon.js';
|
|
|
10
10
|
export class BeaconFactory {
|
|
11
11
|
/**
|
|
12
12
|
* Establish a Beacon instance based on the provided service and optional sidecar data.
|
|
13
|
-
* @param {BeaconService} service
|
|
14
|
-
* @param {Array<BeaconSignal>} signals - The array of beacon signals.
|
|
15
|
-
* @param {SidecarData} sidecar - The sidecar data associated with the beacon.
|
|
13
|
+
* @param {BeaconService} service The beacon service configuration.
|
|
16
14
|
* @returns {Beacon} The established Beacon instance.
|
|
17
15
|
*/
|
|
18
|
-
static establish(service
|
|
16
|
+
static establish(service) {
|
|
19
17
|
switch (service.type) {
|
|
20
18
|
case 'SingletonBeacon':
|
|
21
|
-
return new SingletonBeacon(service
|
|
19
|
+
return new SingletonBeacon(service);
|
|
22
20
|
case 'CASBeacon':
|
|
23
|
-
return new CASBeacon(service
|
|
21
|
+
return new CASBeacon(service);
|
|
24
22
|
case 'SMTBeacon':
|
|
25
|
-
return new SMTBeacon(service
|
|
23
|
+
return new SMTBeacon(service);
|
|
26
24
|
default:
|
|
27
25
|
throw new MethodError('Invalid Beacon Type', 'INVALID_BEACON_ERROR', service);
|
|
28
26
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"factory.js","sourceRoot":"","sources":["../../../../src/core/beacon/factory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C;;;;GAIG;AACH,MAAM,OAAO,aAAa;IACxB
|
|
1
|
+
{"version":3,"file":"factory.js","sourceRoot":"","sources":["../../../../src/core/beacon/factory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C;;;;GAIG;AACH,MAAM,OAAO,aAAa;IACxB;;;;OAIG;IACH,MAAM,CAAC,SAAS,CAAC,OAAsB;QACrC,QAAQ,OAAO,CAAC,IAAI,EAAE,CAAC;YACrB,KAAK,iBAAiB;gBACpB,OAAO,IAAI,eAAe,CAAC,OAAO,CAAC,CAAC;YACtC,KAAK,WAAW;gBACd,OAAO,IAAI,SAAS,CAAC,OAAO,CAAC,CAAC;YAChC,KAAK,WAAW;gBACd,OAAO,IAAI,SAAS,CAAC,OAAO,CAAC,CAAC;YAChC;gBACE,MAAM,IAAI,WAAW,CAAC,qBAAqB,EAAE,sBAAsB,EAAE,OAAO,CAAC,CAAC;QAClF,CAAC;IACH,CAAC;CACF"}
|
|
@@ -1,32 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
* Abstract class representing an AggregateBeacon.
|
|
3
|
-
* @abstract
|
|
4
|
-
* @class AggregateBeacon
|
|
5
|
-
* @type {AggregateBeacon}
|
|
6
|
-
*/
|
|
7
|
-
export class AggregateBeacon {
|
|
8
|
-
/**
|
|
9
|
-
* The Beacon service object parsed from the DID Document.
|
|
10
|
-
*/
|
|
11
|
-
service;
|
|
12
|
-
/**
|
|
13
|
-
* The array of Beacon Signals associated with this Beacon service.
|
|
14
|
-
*/
|
|
15
|
-
signals;
|
|
16
|
-
/**
|
|
17
|
-
* The sidecar data associated with this Beacon service.
|
|
18
|
-
* TODO: Make this more specific to Beacon type.
|
|
19
|
-
*/
|
|
20
|
-
sidecar;
|
|
21
|
-
/**
|
|
22
|
-
* The Bitcoin network connection associated with this Beacon service.
|
|
23
|
-
*/
|
|
24
|
-
bitcoin;
|
|
25
|
-
constructor(service, signals, sidecar, bitcoin) {
|
|
26
|
-
this.service = service;
|
|
27
|
-
this.signals = signals;
|
|
28
|
-
this.sidecar = sidecar;
|
|
29
|
-
this.bitcoin = bitcoin;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
1
|
+
export {};
|
|
32
2
|
//# sourceMappingURL=interfaces.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interfaces.js","sourceRoot":"","sources":["../../../../src/core/beacon/interfaces.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"interfaces.js","sourceRoot":"","sources":["../../../../src/core/beacon/interfaces.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { GENESIS_TX_ID, TXIN_WITNESS_COINBASE } from '@did-btcr2/bitcoin';
|
|
2
|
+
import { ResolveError } from '@did-btcr2/common';
|
|
3
|
+
import { BeaconUtils } from './utils.js';
|
|
4
|
+
/**
|
|
5
|
+
* Static utility class for discovering Beacon Signals on the Bitcoin blockchain.
|
|
6
|
+
* Extracted from {@link Resolve} for single-responsibility and independent testability.
|
|
7
|
+
*
|
|
8
|
+
* @class BeaconSignalDiscovery
|
|
9
|
+
*/
|
|
10
|
+
export class BeaconSignalDiscovery {
|
|
11
|
+
/**
|
|
12
|
+
* Retrieves the beacon signals for the given array of BeaconService objects
|
|
13
|
+
* using an esplora/electrs REST API connection via a bitcoin I/O driver.
|
|
14
|
+
* @param {Array<BeaconService>} beaconServices Array of BeaconService objects to retrieve signals for
|
|
15
|
+
* @param {BitcoinNetworkConnection} bitcoin Bitcoin network connection to use for REST calls
|
|
16
|
+
* @returns {Promise<Map<BeaconService, Array<BeaconSignal>>>} Map of beacon service to its discovered signals
|
|
17
|
+
*/
|
|
18
|
+
static async indexer(beaconServices, bitcoin) {
|
|
19
|
+
const beaconServiceSignals = new Map();
|
|
20
|
+
// Fetch the current block count once before the loop
|
|
21
|
+
const currentBlockCount = await bitcoin.network.rest.block.count();
|
|
22
|
+
// Iterate over each beacon
|
|
23
|
+
for (const beaconService of beaconServices) {
|
|
24
|
+
beaconServiceSignals.set(beaconService, []);
|
|
25
|
+
// Get the transactions for the beacon address via REST
|
|
26
|
+
const beaconSignals = await bitcoin.network.rest.address.getTxs(beaconService.serviceEndpoint);
|
|
27
|
+
// If no signals are found, continue
|
|
28
|
+
if (!beaconSignals || !beaconSignals.length) {
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
// Iterate over each signal
|
|
32
|
+
for (const beaconSignal of beaconSignals) {
|
|
33
|
+
// Get the last vout in the transaction
|
|
34
|
+
const signalVout = beaconSignal.vout.slice(-1)[0];
|
|
35
|
+
/**
|
|
36
|
+
* Look for OP_RETURN in last vout scriptpubkey_asm
|
|
37
|
+
* Vout (rest) format:
|
|
38
|
+
* {
|
|
39
|
+
* scriptpubkey: '6a20570f177c65e64fb5cf61180b664cdddf09ab76153c2b192e22006e5b22a3917a',
|
|
40
|
+
* scriptpubkey_asm: 'OP_RETURN OP_PUSHBYTES_32 570f177c65e64fb5cf61180b664cdddf09ab76153c2b192e22006e5b22a3917a',
|
|
41
|
+
* scriptpubkey_type: 'op_return',
|
|
42
|
+
* value: 0
|
|
43
|
+
* }
|
|
44
|
+
*/
|
|
45
|
+
if (!signalVout || !signalVout.scriptpubkey_asm.includes('OP_RETURN')) {
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
// Construct output map for easier access
|
|
49
|
+
const outputMap = new Map(Object.entries(signalVout));
|
|
50
|
+
// Grab the signal vout scriptpubkey
|
|
51
|
+
const signalVoutScriptPubkey = outputMap.get('scriptpubkey_asm');
|
|
52
|
+
// If the signal vout scriptpubkey does not exist, continue to next signal
|
|
53
|
+
if (!signalVoutScriptPubkey) {
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
// Extract hex string hash of the signal bytes from the scriptpubkey
|
|
57
|
+
const updateHash = signalVoutScriptPubkey.split(' ').slice(-1)[0];
|
|
58
|
+
if (!updateHash) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
// Use the pre-fetched block count instead of calling per-signal
|
|
62
|
+
const confirmations = currentBlockCount - beaconSignal.status.block_height + 1;
|
|
63
|
+
// Push the beacon signal object to the signals array for the beacon service
|
|
64
|
+
beaconServiceSignals.get(beaconService)?.push({
|
|
65
|
+
tx: beaconSignal,
|
|
66
|
+
signalBytes: updateHash,
|
|
67
|
+
blockMetadata: {
|
|
68
|
+
confirmations,
|
|
69
|
+
height: beaconSignal.status.block_height,
|
|
70
|
+
time: beaconSignal.status.block_time,
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return beaconServiceSignals;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Traverse the full blockchain from genesis to chain top looking for beacon signals.
|
|
79
|
+
* @param {Array<BeaconService>} beaconServices Array of BeaconService objects to search for signals.
|
|
80
|
+
* @param {BitcoinNetworkConnection} bitcoin Bitcoin network connection to use for RPC calls.
|
|
81
|
+
* @returns {Promise<Map<BeaconService, Array<BeaconSignal>>>} Map of beacon service to its discovered signals.
|
|
82
|
+
*/
|
|
83
|
+
static async fullnode(beaconServices, bitcoin) {
|
|
84
|
+
const beaconServiceSignals = new Map();
|
|
85
|
+
for (const beaconService of beaconServices) {
|
|
86
|
+
beaconServiceSignals.set(beaconService, []);
|
|
87
|
+
}
|
|
88
|
+
// Get the RPC connection from the bitcoin network
|
|
89
|
+
const rpc = bitcoin.network.rpc;
|
|
90
|
+
// Ensure that the RPC connection is available
|
|
91
|
+
if (!rpc) {
|
|
92
|
+
throw new ResolveError('RPC connection is not available', 'RPC_CONNECTION_ERROR', bitcoin);
|
|
93
|
+
}
|
|
94
|
+
// Get the current block height once before the loop
|
|
95
|
+
const targetHeight = await rpc.getBlockCount();
|
|
96
|
+
// Hoist the beacon services map before the loop
|
|
97
|
+
const beaconServicesMap = BeaconUtils.getBeaconServicesMap(beaconServices);
|
|
98
|
+
// Set genesis height
|
|
99
|
+
let height = 0;
|
|
100
|
+
// Opt into rpc connection to get the block data at the blockhash
|
|
101
|
+
let block = await bitcoin.network.rpc.getBlock({ height });
|
|
102
|
+
console.info(`Searching for beacon signals, please wait ...`);
|
|
103
|
+
while (block.height <= targetHeight) {
|
|
104
|
+
// Iterate over each transaction in the block
|
|
105
|
+
for (const tx of block.tx) {
|
|
106
|
+
// If the txid is a coinbase, continue ...
|
|
107
|
+
if (tx.txid === GENESIS_TX_ID) {
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
// Iterate over each input in the transaction
|
|
111
|
+
for (const vin of tx.vin) {
|
|
112
|
+
// If the vin is a coinbase transaction, continue ...
|
|
113
|
+
if (vin.coinbase) {
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
// If the vin txinwitness contains a coinbase did, continue ...
|
|
117
|
+
if (vin.txinwitness && vin.txinwitness.length === 1 && vin.txinwitness[0] === TXIN_WITNESS_COINBASE) {
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
// If the txid from the vin is undefined, continue ...
|
|
121
|
+
if (!vin.txid) {
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
// If the vout from the vin is undefined, continue ...
|
|
125
|
+
if (vin.vout === undefined) {
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
// Get the previous output transaction data
|
|
129
|
+
const prevout = await rpc.getRawTransaction(vin.txid, 2);
|
|
130
|
+
// If the previous output vout at the vin.vout index is undefined, continue ...
|
|
131
|
+
if (!prevout.vout[vin.vout]) {
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
// Get the address from the scriptPubKey from the prevvout
|
|
135
|
+
const scriptPubKey = prevout.vout[vin.vout].scriptPubKey;
|
|
136
|
+
// If the scriptPubKey.address is undefined, continue ...
|
|
137
|
+
if (!scriptPubKey.address) {
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
// Use the hoisted beaconServicesMap instead of rebuilding per-vin
|
|
141
|
+
const beaconService = beaconServicesMap.get(scriptPubKey.address);
|
|
142
|
+
if (!beaconService) {
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
// Look for 'OP_RETURN' in the scriptPubKey asm
|
|
146
|
+
const txVoutScriptPubkeyAsm = prevout.vout[vin.vout].scriptPubKey.asm;
|
|
147
|
+
if (!txVoutScriptPubkeyAsm.includes('OP_RETURN')) {
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
// Log the found txid and beacon
|
|
151
|
+
console.info(`Tx ${tx.txid} contains beacon service address ${scriptPubKey.address} and OP_RETURN!`, tx);
|
|
152
|
+
// Extract hex string hash of the signal bytes from the scriptpubkey
|
|
153
|
+
const updateHash = txVoutScriptPubkeyAsm.split(' ').slice(-1)[0];
|
|
154
|
+
if (!updateHash) {
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
// Push the beacon signal object to the beacon signals array for that beacon service
|
|
158
|
+
beaconServiceSignals.get(beaconService)?.push({
|
|
159
|
+
tx,
|
|
160
|
+
signalBytes: updateHash,
|
|
161
|
+
blockMetadata: {
|
|
162
|
+
height: block.height,
|
|
163
|
+
time: block.time,
|
|
164
|
+
confirmations: block.confirmations
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
;
|
|
169
|
+
}
|
|
170
|
+
// Increment the height
|
|
171
|
+
height += 1;
|
|
172
|
+
// Use pre-fetched targetHeight instead of calling rpc.getBlockCount() every iteration
|
|
173
|
+
if (height > targetHeight) {
|
|
174
|
+
console.info(`Chain tip reached ${height}, breaking ...`);
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
// Reset the block var to the next block data
|
|
178
|
+
block = await rpc.getBlock({ height });
|
|
179
|
+
}
|
|
180
|
+
return beaconServiceSignals;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
//# sourceMappingURL=signal-discovery.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signal-discovery.js","sourceRoot":"","sources":["../../../../src/core/beacon/signal-discovery.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,aAAa,EAEb,qBAAqB,EACtB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEjD,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAEzC;;;;;GAKG;AACH,MAAM,OAAO,qBAAqB;IAEhC;;;;;;OAMG;IACH,MAAM,CAAC,KAAK,CAAC,OAAO,CAClB,cAAoC,EACpC,OAAiC;QAEjC,MAAM,oBAAoB,GAAG,IAAI,GAAG,EAAsC,CAAC;QAE3E,qDAAqD;QACrD,MAAM,iBAAiB,GAAG,MAAM,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;QAEnE,2BAA2B;QAC3B,KAAK,MAAM,aAAa,IAAI,cAAc,EAAE,CAAC;YAC3C,oBAAoB,CAAC,GAAG,CAAC,aAAa,EAAE,EAAE,CAAC,CAAC;YAC5C,uDAAuD;YACvD,MAAM,aAAa,GAAG,MAAM,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,CAC7D,aAAa,CAAC,eAAyB,CACxC,CAA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, MISSING_UPDATE_DATA } from '@did-btcr2/common';
|
|
2
|
+
import { SchnorrKeyPair } from '@did-btcr2/keypair';
|
|
3
|
+
import { Signer } from '@did-btcr2/kms';
|
|
4
4
|
import { opcodes, Psbt, script } from 'bitcoinjs-lib';
|
|
5
5
|
import { base58btc } from 'multiformats/bases/base58';
|
|
6
6
|
import { canonicalization } from '../../did-btcr2.js';
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
7
|
+
import { Beacon } from './beacon.js';
|
|
8
|
+
import { SingletonBeaconError } from './error.js';
|
|
9
9
|
/**
|
|
10
10
|
* Implements {@link https://dcdpr.github.io/did-btcr2/terminology.html#singleton-beacon | Singleton Beacon}.
|
|
11
|
-
*
|
|
12
11
|
* @class SingletonBeacon
|
|
13
12
|
* @type {SingletonBeacon}
|
|
14
13
|
* @extends {AggregateBeacon}
|
|
15
14
|
*/
|
|
16
|
-
export class SingletonBeacon extends
|
|
15
|
+
export class SingletonBeacon extends Beacon {
|
|
17
16
|
/**
|
|
18
17
|
* Creates an instance of SingletonBeacon.
|
|
19
|
-
* @param {BeaconService} service The
|
|
20
|
-
*
|
|
21
|
-
*/
|
|
22
|
-
constructor(service, signals, sidecar, bitcoin) {
|
|
23
|
-
super({ ...service, type: 'SingletonBeacon' }, signals, sidecar, bitcoin);
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Static, convenience method for establishing a CASBeacon object.
|
|
27
|
-
* @param {string} service The Beacon service.
|
|
28
|
-
* @param {SidecarData} sidecar The sidecar data.
|
|
29
|
-
* @returns {SingletonBeacon} The Singleton Beacon.
|
|
30
|
-
*/
|
|
31
|
-
static establish(service, signals, sidecar) {
|
|
32
|
-
return new SingletonBeacon(service, signals, sidecar);
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Generates a Beacon Signal for a Singleton Beacon Service.
|
|
36
|
-
* @param {HexString} updateHash The update hash to be included in the Beacon Signal.
|
|
37
|
-
* @returns {BeaconSignal} The generated signal.
|
|
38
|
-
* @throws {MethodError} if the signal is invalid.
|
|
18
|
+
* @param {BeaconService} service The BeaconService object representing the funded beacon to announce the update to.
|
|
19
|
+
*
|
|
39
20
|
*/
|
|
40
|
-
|
|
41
|
-
|
|
21
|
+
constructor(service) {
|
|
22
|
+
super({ ...service, type: 'SingletonBeacon' });
|
|
42
23
|
}
|
|
43
24
|
/**
|
|
44
25
|
* Processes an array of Beacon Signals associated with a Singleton Beacon Service.
|
|
45
|
-
* @returns {Promise<
|
|
26
|
+
* @returns {Promise<SignedBTCR2Update | undefined>} The DID Update payload announced by the Beacon Signal.
|
|
46
27
|
* @throws {SingletonBeaconError} if the signalTx is invalid or the signalSidecarData is invalid.
|
|
47
28
|
*/
|
|
48
|
-
async processSignals() {
|
|
29
|
+
async processSignals(signals, sidecar) {
|
|
49
30
|
// Initialize an empty array to hold the BTCR2 signed updates
|
|
50
31
|
const updates = new Array();
|
|
51
|
-
// Loop through each signal in
|
|
52
|
-
for (const signal of
|
|
32
|
+
// Loop through each signal in signals
|
|
33
|
+
for (const signal of signals) {
|
|
53
34
|
// Grab the beacon signal bytes hash from the signal
|
|
54
35
|
const updateHash = signal.signalBytes;
|
|
55
36
|
// Use the updateHash as the sidecar data lookup key to retrieve the btcr2 update
|
|
56
|
-
const signedUpdate =
|
|
37
|
+
const signedUpdate = sidecar.updateMap.get(updateHash);
|
|
57
38
|
// If no btcr2 update is found in sidecar data maps, throw missingUpdateData error.
|
|
58
39
|
if (!signedUpdate) {
|
|
59
40
|
throw new SingletonBeaconError(`BTCR2 Signed Update not found for update hash ${updateHash}.`, MISSING_UPDATE_DATA, signal);
|
|
@@ -74,70 +55,62 @@ export class SingletonBeacon extends AggregateBeacon {
|
|
|
74
55
|
return updates;
|
|
75
56
|
}
|
|
76
57
|
/**
|
|
77
|
-
* Broadcasts a SingletonBeacon signal.
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
* @
|
|
58
|
+
* Broadcasts a SingletonBeacon signal to the Bitcoin network.
|
|
59
|
+
* @param {SignedBTCR2Update} signedUpdate The signed BTCR2 update to broadcast.
|
|
60
|
+
* @param {KeyBytes} secretKey The secret key for signing the Bitcoin transaction.
|
|
61
|
+
* @param {BitcoinNetworkConnection} bitcoin The Bitcoin network connection.
|
|
62
|
+
* @returns {Promise<SignedBTCR2Update>} The signed update that was broadcast.
|
|
81
63
|
* @throws {SingletonBeaconError} if the bitcoin address is invalid or unfunded.
|
|
82
64
|
*/
|
|
83
|
-
async broadcastSignal(
|
|
84
|
-
//
|
|
85
|
-
// 2. Set bitcoinAddress to the decoding of addressURI following BIP21.
|
|
65
|
+
async broadcastSignal(signedUpdate, secretKey, bitcoin) {
|
|
66
|
+
// Convert the serviceEndpoint to a bitcoin address by removing the 'bitcoin:' prefix
|
|
86
67
|
const bitcoinAddress = this.service.serviceEndpoint.replace('bitcoin:', '');
|
|
87
|
-
//
|
|
88
|
-
|
|
89
|
-
|
|
68
|
+
// Query the Bitcoin network for UTXOs associated with the bitcoinAddress
|
|
69
|
+
const utxos = await bitcoin.network.rest.address.getUtxos(bitcoinAddress);
|
|
70
|
+
// If no utxos are found, throw an error indicating the address is unfunded.
|
|
90
71
|
if (!utxos.length) {
|
|
91
72
|
throw new SingletonBeaconError('No UTXOs found, please fund address!', 'UNFUNDED_BEACON_ADDRESS', { bitcoinAddress });
|
|
92
73
|
}
|
|
93
|
-
|
|
74
|
+
// Sort utxos by block height and take the most recent one
|
|
75
|
+
const utxo = utxos.sort((a, b) => b.status.block_height - a.status.block_height).shift();
|
|
76
|
+
// If no utxos are found, throw an error.
|
|
94
77
|
if (!utxo) {
|
|
95
78
|
throw new SingletonBeaconError('Beacon bitcoin address unfunded or utxos unconfirmed.', 'UNFUNDED_BEACON_ADDRESS', { bitcoinAddress });
|
|
96
79
|
}
|
|
97
|
-
//
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
const input = {
|
|
108
|
-
hash: txid,
|
|
109
|
-
index: vout,
|
|
80
|
+
// Get the previous tx to the utxo being spent
|
|
81
|
+
const prevTx = await bitcoin.network.rest.transaction.getHex(utxo.txid);
|
|
82
|
+
// Canonicalize and hash the signed update for OP_RETURN output
|
|
83
|
+
const updateHash = canonicalization.canonicalhash(signedUpdate);
|
|
84
|
+
// Construct a spend transaction
|
|
85
|
+
const spendTx = new Psbt({ network: bitcoin.network.data })
|
|
86
|
+
// Spend tx contains the utxo as its input
|
|
87
|
+
.addInput({
|
|
88
|
+
hash: utxo.txid,
|
|
89
|
+
index: utxo.vout,
|
|
110
90
|
nonWitnessUtxo: Buffer.from(prevTx, 'hex')
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
.addInput(input)
|
|
91
|
+
})
|
|
92
|
+
// Add a change output minus a fee of 500 sats
|
|
93
|
+
// TODO: calculate fee based on transaction vsize and current fee rates
|
|
115
94
|
.addOutput({ address: bitcoinAddress, value: BigInt(utxo.value) - BigInt(500) })
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
//
|
|
119
|
-
const
|
|
120
|
-
const keyUri = new CompressedSecp256k1PublicKey(components.genesisBytes).hex;
|
|
121
|
-
const keyPair = Kms.getKey(keyUri);
|
|
95
|
+
// Add an OP_RETURN output containing the update hash
|
|
96
|
+
.addOutput({ script: script.compile([opcodes.OP_RETURN, updateHash]), value: 0n });
|
|
97
|
+
// Construct a Schnorr key pair from the secret key
|
|
98
|
+
const keyPair = SchnorrKeyPair.fromSecret(secretKey);
|
|
122
99
|
if (!keyPair) {
|
|
123
|
-
throw new
|
|
100
|
+
throw new SingletonBeaconError('Key pair not found.', 'KEY_PAIR_NOT_FOUND', { secretKey });
|
|
124
101
|
}
|
|
125
|
-
|
|
126
|
-
|
|
102
|
+
// Construct a signer object from the key pair and bitcoin network
|
|
103
|
+
const signer = new Signer({ keyPair, network: bitcoin.network.name });
|
|
104
|
+
// Sign 0th input, finalize extract to hex in prep for broadcast
|
|
127
105
|
const signedTx = spendTx.signInput(0, signer)
|
|
128
106
|
.finalizeAllInputs()
|
|
129
107
|
.extractTransaction()
|
|
130
108
|
.toHex();
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
}
|
|
134
|
-
//
|
|
135
|
-
|
|
136
|
-
if (!spentTx) {
|
|
137
|
-
throw new SingletonBeaconError('Failed to send raw transaction.', 'SEND_FAILED', { spentTx });
|
|
138
|
-
}
|
|
139
|
-
// Return the signed update and the spend tx id.
|
|
140
|
-
return spentTx;
|
|
109
|
+
// Broadcast spendTx to the Bitcoin network.
|
|
110
|
+
const txid = await bitcoin.network.rest.transaction.send(signedTx);
|
|
111
|
+
console.info(`Broadcasted Singleton Beacon signal with txid ${txid}`);
|
|
112
|
+
// Return the signed update
|
|
113
|
+
return signedUpdate;
|
|
141
114
|
}
|
|
142
115
|
}
|
|
143
116
|
//# sourceMappingURL=singleton.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"singleton.js","sourceRoot":"","sources":["../../../../src/core/beacon/singleton.ts"],"names":[],"mappings":"AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"singleton.js","sourceRoot":"","sources":["../../../../src/core/beacon/singleton.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,oBAAoB,EAAY,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAExF,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AACxC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AACtD,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AACtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAEtD,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;A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