@did-btcr2/method 0.26.0 → 0.27.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 +86 -233
- package/dist/.tsbuildinfo +1 -1
- package/dist/browser.js +24111 -20342
- package/dist/browser.mjs +24111 -20342
- package/dist/cjs/index.js +2463 -2174
- package/dist/esm/core/aggregation/cohort.js +178 -0
- package/dist/esm/core/aggregation/cohort.js.map +1 -0
- package/dist/esm/core/aggregation/errors.js +22 -0
- package/dist/esm/core/aggregation/errors.js.map +1 -0
- package/dist/esm/core/{beacon/aggregation/cohort → aggregation}/messages/base.js +0 -1
- package/dist/esm/core/aggregation/messages/base.js.map +1 -0
- package/dist/esm/core/aggregation/messages/constants.js +26 -0
- package/dist/esm/core/aggregation/messages/constants.js.map +1 -0
- package/dist/esm/core/aggregation/messages/factories.js +113 -0
- package/dist/esm/core/aggregation/messages/factories.js.map +1 -0
- package/dist/esm/core/aggregation/messages/guards.js +37 -0
- package/dist/esm/core/aggregation/messages/guards.js.map +1 -0
- package/dist/esm/core/aggregation/messages/index.js +5 -0
- package/dist/esm/core/aggregation/messages/index.js.map +1 -0
- package/dist/esm/core/aggregation/participant.js +376 -0
- package/dist/esm/core/aggregation/participant.js.map +1 -0
- package/dist/esm/core/aggregation/phases.js +39 -0
- package/dist/esm/core/aggregation/phases.js.map +1 -0
- package/dist/esm/core/aggregation/runner/events.js +2 -0
- package/dist/esm/core/aggregation/runner/events.js.map +1 -0
- package/dist/esm/core/aggregation/runner/index.js +5 -0
- package/dist/esm/core/aggregation/runner/index.js.map +1 -0
- package/dist/esm/core/aggregation/runner/participant-runner.js +282 -0
- package/dist/esm/core/aggregation/runner/participant-runner.js.map +1 -0
- package/dist/esm/core/aggregation/runner/service-runner.js +290 -0
- package/dist/esm/core/aggregation/runner/service-runner.js.map +1 -0
- package/dist/esm/core/aggregation/runner/typed-emitter.js +80 -0
- package/dist/esm/core/aggregation/runner/typed-emitter.js.map +1 -0
- package/dist/esm/core/aggregation/service.js +416 -0
- package/dist/esm/core/aggregation/service.js.map +1 -0
- package/dist/esm/core/aggregation/signing-session.js +133 -0
- package/dist/esm/core/aggregation/signing-session.js.map +1 -0
- package/dist/esm/core/aggregation/transport/didcomm.js +32 -0
- package/dist/esm/core/aggregation/transport/didcomm.js.map +1 -0
- package/dist/esm/core/aggregation/transport/error.js +12 -0
- package/dist/esm/core/aggregation/transport/error.js.map +1 -0
- package/dist/esm/core/aggregation/transport/factory.js +20 -0
- package/dist/esm/core/aggregation/transport/factory.js.map +1 -0
- package/dist/esm/core/aggregation/transport/index.js +6 -0
- package/dist/esm/core/aggregation/transport/index.js.map +1 -0
- package/dist/esm/core/aggregation/transport/nostr.js +262 -0
- package/dist/esm/core/aggregation/transport/nostr.js.map +1 -0
- package/dist/esm/core/aggregation/transport/transport.js +2 -0
- package/dist/esm/core/aggregation/transport/transport.js.map +1 -0
- package/dist/esm/core/beacon/beacon.js +80 -0
- package/dist/esm/core/beacon/beacon.js.map +1 -1
- package/dist/esm/core/beacon/cas-beacon.js +15 -56
- package/dist/esm/core/beacon/cas-beacon.js.map +1 -1
- package/dist/esm/core/beacon/error.js +0 -10
- package/dist/esm/core/beacon/error.js.map +1 -1
- package/dist/esm/core/beacon/fee-estimator.js +30 -0
- package/dist/esm/core/beacon/fee-estimator.js.map +1 -0
- package/dist/esm/core/beacon/singleton-beacon.js +10 -53
- package/dist/esm/core/beacon/singleton-beacon.js.map +1 -1
- package/dist/esm/core/beacon/smt-beacon.js +85 -9
- package/dist/esm/core/beacon/smt-beacon.js.map +1 -1
- package/dist/esm/core/identifier.js +13 -0
- package/dist/esm/core/identifier.js.map +1 -1
- package/dist/esm/core/resolver.js +9 -0
- package/dist/esm/core/resolver.js.map +1 -1
- package/dist/esm/index.js +14 -24
- package/dist/esm/index.js.map +1 -1
- package/dist/types/core/aggregation/cohort.d.ts +94 -0
- package/dist/types/core/aggregation/cohort.d.ts.map +1 -0
- package/dist/types/core/aggregation/errors.d.ts +14 -0
- package/dist/types/core/aggregation/errors.d.ts.map +1 -0
- package/dist/types/core/{beacon/aggregation/cohort → aggregation}/messages/base.d.ts +7 -1
- package/dist/types/core/aggregation/messages/base.d.ts.map +1 -0
- package/dist/types/core/aggregation/messages/constants.d.ts +23 -0
- package/dist/types/core/aggregation/messages/constants.d.ts.map +1 -0
- package/dist/types/core/aggregation/messages/factories.d.ts +177 -0
- package/dist/types/core/aggregation/messages/factories.d.ts.map +1 -0
- package/dist/types/core/aggregation/messages/guards.d.ts +11 -0
- package/dist/types/core/aggregation/messages/guards.d.ts.map +1 -0
- package/dist/types/core/aggregation/messages/index.d.ts +5 -0
- package/dist/types/core/aggregation/messages/index.d.ts.map +1 -0
- package/dist/types/core/aggregation/participant.d.ts +101 -0
- package/dist/types/core/aggregation/participant.d.ts.map +1 -0
- package/dist/types/core/aggregation/phases.d.ts +49 -0
- package/dist/types/core/aggregation/phases.d.ts.map +1 -0
- package/dist/types/core/aggregation/runner/events.d.ts +89 -0
- package/dist/types/core/aggregation/runner/events.d.ts.map +1 -0
- package/dist/types/core/aggregation/runner/index.d.ts +5 -0
- package/dist/types/core/aggregation/runner/index.d.ts.map +1 -0
- package/dist/types/core/aggregation/runner/participant-runner.d.ts +107 -0
- package/dist/types/core/aggregation/runner/participant-runner.d.ts.map +1 -0
- package/dist/types/core/aggregation/runner/service-runner.d.ts +102 -0
- package/dist/types/core/aggregation/runner/service-runner.d.ts.map +1 -0
- package/dist/types/core/aggregation/runner/typed-emitter.d.ts +41 -0
- package/dist/types/core/aggregation/runner/typed-emitter.d.ts.map +1 -0
- package/dist/types/core/aggregation/service.d.ts +112 -0
- package/dist/types/core/aggregation/service.d.ts.map +1 -0
- package/dist/types/core/aggregation/signing-session.d.ts +69 -0
- package/dist/types/core/aggregation/signing-session.d.ts.map +1 -0
- package/dist/types/core/aggregation/transport/didcomm.d.ts +20 -0
- package/dist/types/core/aggregation/transport/didcomm.d.ts.map +1 -0
- package/dist/types/core/{beacon/aggregation/communication → aggregation/transport}/error.d.ts +2 -2
- package/dist/types/core/aggregation/transport/error.d.ts.map +1 -0
- package/dist/types/core/aggregation/transport/factory.d.ts +13 -0
- package/dist/types/core/aggregation/transport/factory.d.ts.map +1 -0
- package/dist/types/core/aggregation/transport/index.d.ts +6 -0
- package/dist/types/core/aggregation/transport/index.d.ts.map +1 -0
- package/dist/types/core/aggregation/transport/nostr.d.ts +55 -0
- package/dist/types/core/aggregation/transport/nostr.d.ts.map +1 -0
- package/dist/types/core/aggregation/transport/transport.d.ts +37 -0
- package/dist/types/core/aggregation/transport/transport.d.ts.map +1 -0
- package/dist/types/core/beacon/beacon.d.ts +37 -2
- package/dist/types/core/beacon/beacon.d.ts.map +1 -1
- package/dist/types/core/beacon/cas-beacon.d.ts +19 -7
- package/dist/types/core/beacon/cas-beacon.d.ts.map +1 -1
- package/dist/types/core/beacon/error.d.ts +0 -6
- package/dist/types/core/beacon/error.d.ts.map +1 -1
- package/dist/types/core/beacon/fee-estimator.d.ts +40 -0
- package/dist/types/core/beacon/fee-estimator.d.ts.map +1 -0
- package/dist/types/core/beacon/interfaces.d.ts +8 -0
- package/dist/types/core/beacon/interfaces.d.ts.map +1 -1
- package/dist/types/core/beacon/singleton-beacon.d.ts +9 -2
- package/dist/types/core/beacon/singleton-beacon.d.ts.map +1 -1
- package/dist/types/core/beacon/smt-beacon.d.ts +27 -7
- package/dist/types/core/beacon/smt-beacon.d.ts.map +1 -1
- package/dist/types/core/identifier.d.ts +8 -0
- package/dist/types/core/identifier.d.ts.map +1 -1
- package/dist/types/core/interfaces.d.ts +2 -2
- package/dist/types/core/resolver.d.ts +11 -1
- package/dist/types/core/resolver.d.ts.map +1 -1
- package/dist/types/index.d.ts +9 -24
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +31 -30
- package/src/core/aggregation/cohort.ts +247 -0
- package/src/core/aggregation/errors.ts +25 -0
- package/src/core/{beacon/aggregation/cohort → aggregation}/messages/base.ts +8 -3
- package/src/core/aggregation/messages/constants.ts +28 -0
- package/src/core/aggregation/messages/factories.ts +240 -0
- package/src/core/aggregation/messages/guards.ts +55 -0
- package/src/core/aggregation/messages/index.ts +4 -0
- package/src/core/aggregation/participant.ts +510 -0
- package/src/core/aggregation/phases.ts +82 -0
- package/src/core/aggregation/runner/events.ts +77 -0
- package/src/core/aggregation/runner/index.ts +4 -0
- package/src/core/aggregation/runner/participant-runner.ts +360 -0
- package/src/core/aggregation/runner/service-runner.ts +365 -0
- package/src/core/aggregation/runner/typed-emitter.ts +87 -0
- package/src/core/aggregation/service.ts +547 -0
- package/src/core/aggregation/signing-session.ts +209 -0
- package/src/core/aggregation/transport/didcomm.ts +42 -0
- package/src/core/aggregation/transport/error.ts +13 -0
- package/src/core/aggregation/transport/factory.ts +29 -0
- package/src/core/aggregation/transport/index.ts +5 -0
- package/src/core/aggregation/transport/nostr.ts +333 -0
- package/src/core/aggregation/transport/transport.ts +46 -0
- package/src/core/beacon/beacon.ts +122 -2
- package/src/core/beacon/cas-beacon.ts +28 -76
- package/src/core/beacon/error.ts +0 -12
- package/src/core/beacon/fee-estimator.ts +52 -0
- package/src/core/beacon/interfaces.ts +10 -1
- package/src/core/beacon/singleton-beacon.ts +14 -75
- package/src/core/beacon/smt-beacon.ts +109 -11
- package/src/core/identifier.ts +17 -0
- package/src/core/interfaces.ts +2 -2
- package/src/core/resolver.ts +25 -2
- package/src/index.ts +15 -29
- package/dist/esm/core/beacon/aggregation/cohort/index.js +0 -237
- package/dist/esm/core/beacon/aggregation/cohort/index.js.map +0 -1
- package/dist/esm/core/beacon/aggregation/cohort/messages/base.js.map +0 -1
- package/dist/esm/core/beacon/aggregation/cohort/messages/constants.js +0 -11
- package/dist/esm/core/beacon/aggregation/cohort/messages/constants.js.map +0 -1
- package/dist/esm/core/beacon/aggregation/cohort/messages/index.js +0 -98
- package/dist/esm/core/beacon/aggregation/cohort/messages/index.js.map +0 -1
- package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/cohort-advert.js +0 -31
- package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/cohort-advert.js.map +0 -1
- package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/cohort-ready.js +0 -29
- package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/cohort-ready.js.map +0 -1
- package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/opt-in-accept.js +0 -27
- package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/opt-in-accept.js.map +0 -1
- package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/opt-in.js +0 -23
- package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/opt-in.js.map +0 -1
- package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/subscribe.js +0 -28
- package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/subscribe.js.map +0 -1
- package/dist/esm/core/beacon/aggregation/cohort/messages/sign/aggregated-nonce.js +0 -29
- package/dist/esm/core/beacon/aggregation/cohort/messages/sign/aggregated-nonce.js.map +0 -1
- package/dist/esm/core/beacon/aggregation/cohort/messages/sign/authorization-request.js +0 -30
- package/dist/esm/core/beacon/aggregation/cohort/messages/sign/authorization-request.js.map +0 -1
- package/dist/esm/core/beacon/aggregation/cohort/messages/sign/nonce-contribution.js +0 -30
- package/dist/esm/core/beacon/aggregation/cohort/messages/sign/nonce-contribution.js.map +0 -1
- package/dist/esm/core/beacon/aggregation/cohort/messages/sign/request-signature.js +0 -30
- package/dist/esm/core/beacon/aggregation/cohort/messages/sign/request-signature.js.map +0 -1
- package/dist/esm/core/beacon/aggregation/cohort/messages/sign/signature-authorization.js +0 -31
- package/dist/esm/core/beacon/aggregation/cohort/messages/sign/signature-authorization.js.map +0 -1
- package/dist/esm/core/beacon/aggregation/cohort/status.js +0 -8
- package/dist/esm/core/beacon/aggregation/cohort/status.js.map +0 -1
- package/dist/esm/core/beacon/aggregation/communication/adapter/did-comm.js +0 -121
- package/dist/esm/core/beacon/aggregation/communication/adapter/did-comm.js.map +0 -1
- package/dist/esm/core/beacon/aggregation/communication/adapter/nostr.js +0 -245
- package/dist/esm/core/beacon/aggregation/communication/adapter/nostr.js.map +0 -1
- package/dist/esm/core/beacon/aggregation/communication/error.js +0 -12
- package/dist/esm/core/beacon/aggregation/communication/error.js.map +0 -1
- package/dist/esm/core/beacon/aggregation/communication/factory.js +0 -21
- package/dist/esm/core/beacon/aggregation/communication/factory.js.map +0 -1
- package/dist/esm/core/beacon/aggregation/communication/service.js +0 -2
- package/dist/esm/core/beacon/aggregation/communication/service.js.map +0 -1
- package/dist/esm/core/beacon/aggregation/coordinator.js +0 -343
- package/dist/esm/core/beacon/aggregation/coordinator.js.map +0 -1
- package/dist/esm/core/beacon/aggregation/participant.js +0 -435
- package/dist/esm/core/beacon/aggregation/participant.js.map +0 -1
- package/dist/esm/core/beacon/aggregation/session/index.js +0 -244
- package/dist/esm/core/beacon/aggregation/session/index.js.map +0 -1
- package/dist/esm/core/beacon/aggregation/session/status.js +0 -11
- package/dist/esm/core/beacon/aggregation/session/status.js.map +0 -1
- package/dist/types/core/beacon/aggregation/cohort/index.d.ts +0 -136
- package/dist/types/core/beacon/aggregation/cohort/index.d.ts.map +0 -1
- package/dist/types/core/beacon/aggregation/cohort/messages/base.d.ts.map +0 -1
- package/dist/types/core/beacon/aggregation/cohort/messages/constants.d.ts +0 -11
- package/dist/types/core/beacon/aggregation/cohort/messages/constants.d.ts.map +0 -1
- package/dist/types/core/beacon/aggregation/cohort/messages/index.d.ts +0 -65
- package/dist/types/core/beacon/aggregation/cohort/messages/index.d.ts.map +0 -1
- package/dist/types/core/beacon/aggregation/cohort/messages/keygen/cohort-advert.d.ts +0 -29
- package/dist/types/core/beacon/aggregation/cohort/messages/keygen/cohort-advert.d.ts.map +0 -1
- package/dist/types/core/beacon/aggregation/cohort/messages/keygen/cohort-ready.d.ts +0 -26
- package/dist/types/core/beacon/aggregation/cohort/messages/keygen/cohort-ready.d.ts.map +0 -1
- package/dist/types/core/beacon/aggregation/cohort/messages/keygen/opt-in-accept.d.ts +0 -24
- package/dist/types/core/beacon/aggregation/cohort/messages/keygen/opt-in-accept.d.ts.map +0 -1
- package/dist/types/core/beacon/aggregation/cohort/messages/keygen/opt-in.d.ts +0 -20
- package/dist/types/core/beacon/aggregation/cohort/messages/keygen/opt-in.d.ts.map +0 -1
- package/dist/types/core/beacon/aggregation/cohort/messages/keygen/subscribe.d.ts +0 -25
- package/dist/types/core/beacon/aggregation/cohort/messages/keygen/subscribe.d.ts.map +0 -1
- package/dist/types/core/beacon/aggregation/cohort/messages/sign/aggregated-nonce.d.ts +0 -25
- package/dist/types/core/beacon/aggregation/cohort/messages/sign/aggregated-nonce.d.ts.map +0 -1
- package/dist/types/core/beacon/aggregation/cohort/messages/sign/authorization-request.d.ts +0 -26
- package/dist/types/core/beacon/aggregation/cohort/messages/sign/authorization-request.d.ts.map +0 -1
- package/dist/types/core/beacon/aggregation/cohort/messages/sign/nonce-contribution.d.ts +0 -26
- package/dist/types/core/beacon/aggregation/cohort/messages/sign/nonce-contribution.d.ts.map +0 -1
- package/dist/types/core/beacon/aggregation/cohort/messages/sign/request-signature.d.ts +0 -26
- package/dist/types/core/beacon/aggregation/cohort/messages/sign/request-signature.d.ts.map +0 -1
- package/dist/types/core/beacon/aggregation/cohort/messages/sign/signature-authorization.d.ts +0 -27
- package/dist/types/core/beacon/aggregation/cohort/messages/sign/signature-authorization.d.ts.map +0 -1
- package/dist/types/core/beacon/aggregation/cohort/status.d.ts +0 -8
- package/dist/types/core/beacon/aggregation/cohort/status.d.ts.map +0 -1
- package/dist/types/core/beacon/aggregation/communication/adapter/did-comm.d.ts +0 -89
- package/dist/types/core/beacon/aggregation/communication/adapter/did-comm.d.ts.map +0 -1
- package/dist/types/core/beacon/aggregation/communication/adapter/nostr.d.ts +0 -103
- package/dist/types/core/beacon/aggregation/communication/adapter/nostr.d.ts.map +0 -1
- package/dist/types/core/beacon/aggregation/communication/error.d.ts.map +0 -1
- package/dist/types/core/beacon/aggregation/communication/factory.d.ts +0 -10
- package/dist/types/core/beacon/aggregation/communication/factory.d.ts.map +0 -1
- package/dist/types/core/beacon/aggregation/communication/service.d.ts +0 -36
- package/dist/types/core/beacon/aggregation/communication/service.d.ts.map +0 -1
- package/dist/types/core/beacon/aggregation/coordinator.d.ts +0 -116
- package/dist/types/core/beacon/aggregation/coordinator.d.ts.map +0 -1
- package/dist/types/core/beacon/aggregation/participant.d.ts +0 -192
- package/dist/types/core/beacon/aggregation/participant.d.ts.map +0 -1
- package/dist/types/core/beacon/aggregation/session/index.d.ts +0 -156
- package/dist/types/core/beacon/aggregation/session/index.d.ts.map +0 -1
- package/dist/types/core/beacon/aggregation/session/status.d.ts +0 -11
- package/dist/types/core/beacon/aggregation/session/status.d.ts.map +0 -1
- package/src/core/beacon/aggregation/cohort/index.ts +0 -305
- package/src/core/beacon/aggregation/cohort/messages/constants.ts +0 -12
- package/src/core/beacon/aggregation/cohort/messages/index.ts +0 -143
- package/src/core/beacon/aggregation/cohort/messages/keygen/cohort-advert.ts +0 -44
- package/src/core/beacon/aggregation/cohort/messages/keygen/cohort-ready.ts +0 -40
- package/src/core/beacon/aggregation/cohort/messages/keygen/opt-in-accept.ts +0 -35
- package/src/core/beacon/aggregation/cohort/messages/keygen/opt-in.ts +0 -34
- package/src/core/beacon/aggregation/cohort/messages/keygen/subscribe.ts +0 -36
- package/src/core/beacon/aggregation/cohort/messages/sign/aggregated-nonce.ts +0 -39
- package/src/core/beacon/aggregation/cohort/messages/sign/authorization-request.ts +0 -40
- package/src/core/beacon/aggregation/cohort/messages/sign/nonce-contribution.ts +0 -40
- package/src/core/beacon/aggregation/cohort/messages/sign/request-signature.ts +0 -40
- package/src/core/beacon/aggregation/cohort/messages/sign/signature-authorization.ts +0 -41
- package/src/core/beacon/aggregation/cohort/status.ts +0 -7
- package/src/core/beacon/aggregation/communication/adapter/did-comm.ts +0 -148
- package/src/core/beacon/aggregation/communication/adapter/nostr.ts +0 -323
- package/src/core/beacon/aggregation/communication/error.ts +0 -13
- package/src/core/beacon/aggregation/communication/factory.ts +0 -25
- package/src/core/beacon/aggregation/communication/service.ts +0 -42
- package/src/core/beacon/aggregation/coordinator.ts +0 -419
- package/src/core/beacon/aggregation/participant.ts +0 -517
- package/src/core/beacon/aggregation/session/index.ts +0 -301
- package/src/core/beacon/aggregation/session/status.ts +0 -18
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import { canonicalHash, canonicalize, hash } from '@did-btcr2/common';
|
|
2
|
+
import type { SignedBTCR2Update } from '@did-btcr2/cryptosuite';
|
|
3
|
+
import type { SerializedSMTProof, TreeEntry } from '@did-btcr2/smt';
|
|
4
|
+
import { BTCR2MerkleTree } from '@did-btcr2/smt';
|
|
5
|
+
import { hexToBytes, randomBytes } from '@noble/hashes/utils';
|
|
6
|
+
import { keyAggExport, keyAggregate, sortKeys } from '@scure/btc-signer/musig2';
|
|
7
|
+
import { crypto as btcCrypto, payments } from 'bitcoinjs-lib';
|
|
8
|
+
import type { CASAnnouncement } from '../types.js';
|
|
9
|
+
import { AggregationCohortError } from './errors.js';
|
|
10
|
+
|
|
11
|
+
export type AggregationCohortParams = {
|
|
12
|
+
id?: string;
|
|
13
|
+
serviceDid?: string;
|
|
14
|
+
minParticipants?: number;
|
|
15
|
+
network: string;
|
|
16
|
+
beaconType?: string;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Represents an Aggregation Cohort — a set of Aggregation Participants who
|
|
21
|
+
* submitted cryptographic material to an Aggregation Service to coordinate
|
|
22
|
+
* signing of a shared n-of-n MuSig2 Bitcoin transaction.
|
|
23
|
+
*
|
|
24
|
+
* This is a pure data class — it holds cohort state and provides computation
|
|
25
|
+
* helpers (key aggregation, CAS Announcement building, SMT tree building).
|
|
26
|
+
* It performs no I/O and emits no messages. Both AggregationService and
|
|
27
|
+
* AggregationParticipant create their own AggregationCohort instances to
|
|
28
|
+
* track their respective views of the cohort state.
|
|
29
|
+
*
|
|
30
|
+
* @class AggregationCohort
|
|
31
|
+
*/
|
|
32
|
+
export class AggregationCohort {
|
|
33
|
+
/** Unique identifier for the cohort. */
|
|
34
|
+
id: string;
|
|
35
|
+
|
|
36
|
+
/** DID of the Aggregation Service managing this cohort. */
|
|
37
|
+
serviceDid: string;
|
|
38
|
+
|
|
39
|
+
/** Minimum number of participants required to finalize the cohort. */
|
|
40
|
+
minParticipants: number;
|
|
41
|
+
|
|
42
|
+
/** Network on which the cohort operates (mainnet, mutinynet, etc.). */
|
|
43
|
+
network: string;
|
|
44
|
+
|
|
45
|
+
/** Type of beacon used in the cohort: 'CASBeacon' or 'SMTBeacon'. */
|
|
46
|
+
beaconType: string;
|
|
47
|
+
|
|
48
|
+
/** List of participant DIDs that have been accepted into the cohort. */
|
|
49
|
+
participants: Array<string> = [];
|
|
50
|
+
|
|
51
|
+
/** Sorted list of cohort participants' compressed public keys. */
|
|
52
|
+
#cohortKeys: Array<Uint8Array> = [];
|
|
53
|
+
|
|
54
|
+
/** Taproot tweak (BIP-341 key-path-only). */
|
|
55
|
+
trMerkleRoot: Uint8Array = new Uint8Array();
|
|
56
|
+
|
|
57
|
+
/** The n-of-n MuSig2 Taproot beacon address. */
|
|
58
|
+
beaconAddress: string = '';
|
|
59
|
+
|
|
60
|
+
/** Pending DID updates submitted by participants, keyed by DID. */
|
|
61
|
+
pendingUpdates: Map<string, SignedBTCR2Update> = new Map();
|
|
62
|
+
|
|
63
|
+
/** CAS Beacon Announcement Map (DID → updateHash), set by buildCASAnnouncement(). */
|
|
64
|
+
casAnnouncement?: CASAnnouncement;
|
|
65
|
+
|
|
66
|
+
/** Per-participant SMT proofs, set by buildSMTTree(). */
|
|
67
|
+
smtProofs?: Map<string, SerializedSMTProof>;
|
|
68
|
+
|
|
69
|
+
/** Signal bytes (32 bytes) for OP_RETURN: SHA-256 of CAS announcement OR SMT root. */
|
|
70
|
+
signalBytes?: Uint8Array;
|
|
71
|
+
|
|
72
|
+
/** Set of participant DIDs that have approved the aggregated data. */
|
|
73
|
+
validationAcks: Set<string> = new Set();
|
|
74
|
+
|
|
75
|
+
/** Set of participant DIDs that have rejected the aggregated data. */
|
|
76
|
+
validationRejections: Set<string> = new Set();
|
|
77
|
+
|
|
78
|
+
constructor({ id, minParticipants, serviceDid, network, beaconType }: AggregationCohortParams) {
|
|
79
|
+
this.id = id || crypto.randomUUID();
|
|
80
|
+
this.minParticipants = minParticipants || 2;
|
|
81
|
+
this.serviceDid = serviceDid || '';
|
|
82
|
+
this.network = network;
|
|
83
|
+
this.beaconType = beaconType || 'CASBeacon';
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Sorted cohort keys (sorted on assignment per BIP-327). */
|
|
87
|
+
get cohortKeys(): Array<Uint8Array> {
|
|
88
|
+
return this.#cohortKeys;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
set cohortKeys(keys: Array<Uint8Array>) {
|
|
92
|
+
this.#cohortKeys = sortKeys(keys);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Computes the n-of-n MuSig2 Taproot beacon address from cohort keys.
|
|
97
|
+
* Sets `trMerkleRoot` to the BIP-341 key-path-only tweak.
|
|
98
|
+
*/
|
|
99
|
+
public computeBeaconAddress(): string {
|
|
100
|
+
if(this.#cohortKeys.length === 0) {
|
|
101
|
+
throw new AggregationCohortError(
|
|
102
|
+
'Cannot compute beacon address: no cohort keys.',
|
|
103
|
+
'NO_COHORT_KEYS', { cohortId: this.id }
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
const keyAggContext = keyAggregate(this.#cohortKeys);
|
|
107
|
+
const aggPubkey = keyAggExport(keyAggContext);
|
|
108
|
+
const payment = payments.p2tr({ internalPubkey: aggPubkey });
|
|
109
|
+
|
|
110
|
+
// BIP-341: key-path-only P2TR has no script tree, so payment.hash is null.
|
|
111
|
+
// Compute the tweak: taggedHash("TapTweak", internalPubkey).
|
|
112
|
+
this.trMerkleRoot = payment.hash ?? btcCrypto.taggedHash('TapTweak', aggPubkey);
|
|
113
|
+
|
|
114
|
+
if(!payment.address) {
|
|
115
|
+
throw new AggregationCohortError(
|
|
116
|
+
'Failed to compute Taproot address',
|
|
117
|
+
'BEACON_ADDRESS_ERROR', { cohortId: this.id }
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
this.beaconAddress = payment.address;
|
|
121
|
+
return payment.address;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Validates that the participant's key is in the cohort and the beacon address
|
|
126
|
+
* matches the locally-computed one. Used by participants to verify cohort ready
|
|
127
|
+
* messages from the service.
|
|
128
|
+
*/
|
|
129
|
+
public validateMembership(
|
|
130
|
+
participantPkHex: string,
|
|
131
|
+
cohortKeysHex: Array<string>,
|
|
132
|
+
expectedBeaconAddress: string
|
|
133
|
+
): void {
|
|
134
|
+
if(!cohortKeysHex.includes(participantPkHex)) {
|
|
135
|
+
throw new AggregationCohortError(
|
|
136
|
+
`Participant key not found in cohort ${this.id}.`,
|
|
137
|
+
'COHORT_VALIDATION_ERROR', { cohortId: this.id, participantPkHex }
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
this.cohortKeys = cohortKeysHex.map(k => hexToBytes(k));
|
|
141
|
+
const computed = this.computeBeaconAddress();
|
|
142
|
+
if(computed !== expectedBeaconAddress) {
|
|
143
|
+
throw new AggregationCohortError(
|
|
144
|
+
`Computed beacon address ${computed} does not match expected ${expectedBeaconAddress}.`,
|
|
145
|
+
'BEACON_ADDRESS_MISMATCH', { cohortId: this.id, computed, expected: expectedBeaconAddress }
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
public addUpdate(participantDid: string, signedUpdate: SignedBTCR2Update): void {
|
|
151
|
+
if(!this.participants.includes(participantDid)) {
|
|
152
|
+
throw new AggregationCohortError(
|
|
153
|
+
`Participant ${participantDid} is not in cohort ${this.id}.`,
|
|
154
|
+
'UNKNOWN_PARTICIPANT', { cohortId: this.id, participantDid }
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
this.pendingUpdates.set(participantDid, signedUpdate);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
public hasAllUpdates(): boolean {
|
|
161
|
+
return this.pendingUpdates.size === this.participants.length;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Builds a CAS Announcement Map from collected updates.
|
|
166
|
+
* Maps each participant DID → base64url canonical hash of their signed update.
|
|
167
|
+
* Computes signal bytes as SHA-256 of canonicalized announcement.
|
|
168
|
+
*/
|
|
169
|
+
public buildCASAnnouncement(): CASAnnouncement {
|
|
170
|
+
if(!this.hasAllUpdates()) {
|
|
171
|
+
throw new AggregationCohortError(
|
|
172
|
+
'Cannot build CAS Announcement: not all updates collected.',
|
|
173
|
+
'INCOMPLETE_UPDATES', { cohortId: this.id, collected: this.pendingUpdates.size, total: this.participants.length }
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
const announcement: CASAnnouncement = {};
|
|
177
|
+
for(const [did, signedUpdate] of this.pendingUpdates) {
|
|
178
|
+
announcement[did] = canonicalHash(signedUpdate);
|
|
179
|
+
}
|
|
180
|
+
this.casAnnouncement = announcement;
|
|
181
|
+
this.signalBytes = hash(canonicalize(announcement));
|
|
182
|
+
return announcement;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Builds an SMT tree from collected updates.
|
|
187
|
+
* Each entry uses a random 32-byte nonce + canonicalized signed update bytes.
|
|
188
|
+
* Stores per-participant proofs and the SMT root as signalBytes.
|
|
189
|
+
*/
|
|
190
|
+
public buildSMTTree(): Map<string, SerializedSMTProof> {
|
|
191
|
+
if(!this.hasAllUpdates()) {
|
|
192
|
+
throw new AggregationCohortError(
|
|
193
|
+
'Cannot build SMT tree: not all updates collected.',
|
|
194
|
+
'INCOMPLETE_UPDATES', { cohortId: this.id }
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
const tree = new BTCR2MerkleTree();
|
|
198
|
+
const entries: TreeEntry[] = [];
|
|
199
|
+
const encoder = new TextEncoder();
|
|
200
|
+
|
|
201
|
+
for(const [did, signedUpdate] of this.pendingUpdates) {
|
|
202
|
+
const canonicalBytes = encoder.encode(canonicalize(signedUpdate));
|
|
203
|
+
const nonce = randomBytes(32);
|
|
204
|
+
entries.push({ did, nonce, signedUpdate: canonicalBytes });
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
tree.addEntries(entries);
|
|
208
|
+
tree.finalize();
|
|
209
|
+
|
|
210
|
+
this.signalBytes = tree.rootHash;
|
|
211
|
+
this.smtProofs = new Map();
|
|
212
|
+
for(const [did] of this.pendingUpdates) {
|
|
213
|
+
this.smtProofs.set(did, tree.proof(did));
|
|
214
|
+
}
|
|
215
|
+
return this.smtProofs;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
public addValidation(participantDid: string, approved: boolean): void {
|
|
219
|
+
if(!this.participants.includes(participantDid)) {
|
|
220
|
+
throw new AggregationCohortError(
|
|
221
|
+
`Unknown participant ${participantDid} in cohort ${this.id}.`,
|
|
222
|
+
'UNKNOWN_PARTICIPANT', { cohortId: this.id, participantDid }
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
if(approved) {
|
|
226
|
+
this.validationAcks.add(participantDid);
|
|
227
|
+
} else {
|
|
228
|
+
this.validationRejections.add(participantDid);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* True when every participant has either approved or rejected the aggregated data.
|
|
234
|
+
*/
|
|
235
|
+
public hasAllValidationResponses(): boolean {
|
|
236
|
+
return this.validationAcks.size + this.validationRejections.size === this.participants.length;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* True when all participants approved. Note: differs from {@link hasAllValidationResponses} —
|
|
241
|
+
* this returns false if any participant rejected, even if all responses are in.
|
|
242
|
+
*/
|
|
243
|
+
public isFullyValidated(): boolean {
|
|
244
|
+
return this.validationRejections.size === 0
|
|
245
|
+
&& this.validationAcks.size === this.participants.length;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { MethodError } from '@did-btcr2/common';
|
|
2
|
+
|
|
3
|
+
export class AggregationServiceError extends MethodError {
|
|
4
|
+
constructor(message: string, type: string = 'AggregationServiceError', data?: Record<string, any>) {
|
|
5
|
+
super(message, type, data);
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export class AggregationParticipantError extends MethodError {
|
|
10
|
+
constructor(message: string, type: string = 'AggregationParticipantError', data?: Record<string, any>) {
|
|
11
|
+
super(message, type, data);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class AggregationCohortError extends MethodError {
|
|
16
|
+
constructor(message: string, type: string = 'AggregationCohortError', data?: Record<string, any>) {
|
|
17
|
+
super(message, type, data);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class SigningSessionError extends MethodError {
|
|
22
|
+
constructor(message: string, type: string = 'SigningSessionError', data?: Record<string, any>) {
|
|
23
|
+
super(message, type, data);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
export const MESSAGE_PREFIX = 'https://btcr2.tools/';
|
|
2
|
-
|
|
3
1
|
export type BaseBody = {
|
|
4
2
|
cohortId: string;
|
|
5
3
|
cohortSize?: number;
|
|
@@ -12,8 +10,15 @@ export type BaseBody = {
|
|
|
12
10
|
nonceContribution?: Uint8Array;
|
|
13
11
|
partialSignature?: Uint8Array;
|
|
14
12
|
pendingTx?: string;
|
|
13
|
+
prevOutValue?: string;
|
|
14
|
+
communicationPk?: Uint8Array;
|
|
15
15
|
beaconType?: string;
|
|
16
16
|
data?: string;
|
|
17
|
+
signedUpdate?: Record<string, unknown>;
|
|
18
|
+
casAnnouncement?: Record<string, string>;
|
|
19
|
+
smtProof?: Record<string, unknown>;
|
|
20
|
+
signalBytesHex?: string;
|
|
21
|
+
approved?: boolean;
|
|
17
22
|
};
|
|
18
23
|
|
|
19
24
|
export type Base = {
|
|
@@ -48,4 +53,4 @@ export class BaseMessage {
|
|
|
48
53
|
body : this.body
|
|
49
54
|
};
|
|
50
55
|
}
|
|
51
|
-
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Message type URLs for the did:btcr2 Aggregate Beacon protocol.
|
|
3
|
+
*
|
|
4
|
+
* Naming follows the spec (https://dcdpr.github.io/did-btcr2/beacons/aggregate-beacons.html):
|
|
5
|
+
* - Step 1 (Cohort Formation): COHORT_ADVERT, COHORT_OPT_IN, COHORT_OPT_IN_ACCEPT, COHORT_READY
|
|
6
|
+
* - Step 2 (Update Submission): SUBMIT_UPDATE
|
|
7
|
+
* - Step 3 (Aggregate & Validate): DISTRIBUTE_AGGREGATED_DATA, VALIDATION_ACK
|
|
8
|
+
* - Step 4 (Sign & Broadcast): AUTHORIZATION_REQUEST, NONCE_CONTRIBUTION,
|
|
9
|
+
* AGGREGATED_NONCE, SIGNATURE_AUTHORIZATION
|
|
10
|
+
*/
|
|
11
|
+
export const AGGREGATION_MESSAGE_PREFIX = 'https://btcr2.dev/aggregation';
|
|
12
|
+
|
|
13
|
+
// Step 1: Cohort Formation
|
|
14
|
+
export const COHORT_ADVERT = `${AGGREGATION_MESSAGE_PREFIX}/keygen/cohort_advert`;
|
|
15
|
+
export const COHORT_OPT_IN = `${AGGREGATION_MESSAGE_PREFIX}/keygen/cohort_opt_in`;
|
|
16
|
+
export const COHORT_OPT_IN_ACCEPT = `${AGGREGATION_MESSAGE_PREFIX}/keygen/cohort_opt_in_accept`;
|
|
17
|
+
export const COHORT_READY = `${AGGREGATION_MESSAGE_PREFIX}/keygen/cohort_ready`;
|
|
18
|
+
|
|
19
|
+
// Step 2 + 3: Update Submission, Aggregation, Validation
|
|
20
|
+
export const SUBMIT_UPDATE = `${AGGREGATION_MESSAGE_PREFIX}/update/submit_update`;
|
|
21
|
+
export const DISTRIBUTE_AGGREGATED_DATA = `${AGGREGATION_MESSAGE_PREFIX}/update/distribute_aggregated_data`;
|
|
22
|
+
export const VALIDATION_ACK = `${AGGREGATION_MESSAGE_PREFIX}/update/validation_ack`;
|
|
23
|
+
|
|
24
|
+
// Step 4: Signing
|
|
25
|
+
export const AUTHORIZATION_REQUEST = `${AGGREGATION_MESSAGE_PREFIX}/sign/authorization_request`;
|
|
26
|
+
export const NONCE_CONTRIBUTION = `${AGGREGATION_MESSAGE_PREFIX}/sign/nonce_contribution`;
|
|
27
|
+
export const AGGREGATED_NONCE = `${AGGREGATION_MESSAGE_PREFIX}/sign/aggregated_nonce`;
|
|
28
|
+
export const SIGNATURE_AUTHORIZATION = `${AGGREGATION_MESSAGE_PREFIX}/sign/signature_authorization`;
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { BaseMessage } from './base.js';
|
|
2
|
+
import {
|
|
3
|
+
AGGREGATED_NONCE,
|
|
4
|
+
AUTHORIZATION_REQUEST,
|
|
5
|
+
COHORT_ADVERT,
|
|
6
|
+
COHORT_OPT_IN,
|
|
7
|
+
COHORT_OPT_IN_ACCEPT,
|
|
8
|
+
COHORT_READY,
|
|
9
|
+
DISTRIBUTE_AGGREGATED_DATA,
|
|
10
|
+
NONCE_CONTRIBUTION,
|
|
11
|
+
SIGNATURE_AUTHORIZATION,
|
|
12
|
+
SUBMIT_UPDATE,
|
|
13
|
+
VALIDATION_ACK,
|
|
14
|
+
} from './constants.js';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Step 1: Cohort Formation
|
|
18
|
+
* Factory functions for creating messages related to the cohort formation step, where cohorts are
|
|
19
|
+
* formed and participants opt in to join the cohort.
|
|
20
|
+
*/
|
|
21
|
+
type CohortAdvertMessage = {
|
|
22
|
+
from: string;
|
|
23
|
+
cohortId: string;
|
|
24
|
+
cohortSize: number;
|
|
25
|
+
beaconType: string;
|
|
26
|
+
network: string;
|
|
27
|
+
communicationPk: Uint8Array;
|
|
28
|
+
};
|
|
29
|
+
type CohortOptInMessage = {
|
|
30
|
+
from: string;
|
|
31
|
+
to: string;
|
|
32
|
+
cohortId: string;
|
|
33
|
+
participantPk: Uint8Array;
|
|
34
|
+
communicationPk: Uint8Array;
|
|
35
|
+
};
|
|
36
|
+
type CohortOptInAcceptMessage = {
|
|
37
|
+
from: string;
|
|
38
|
+
to: string;
|
|
39
|
+
cohortId: string;
|
|
40
|
+
};
|
|
41
|
+
type CohortReadyMessage = {
|
|
42
|
+
from: string;
|
|
43
|
+
to: string;
|
|
44
|
+
cohortId: string;
|
|
45
|
+
beaconAddress: string;
|
|
46
|
+
cohortKeys: Array<Uint8Array>;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Factory function for creating a Cohort Advert message, which is used to announce the formation of
|
|
51
|
+
* a new cohort and invite participants to join.
|
|
52
|
+
* @param {CohortAdvertMessage} fields - The fields required to create a Cohort Advert message.
|
|
53
|
+
* @returns {BaseMessage} The created Cohort Advert message.
|
|
54
|
+
*/
|
|
55
|
+
export function createCohortAdvertMessage(fields: CohortAdvertMessage): BaseMessage {
|
|
56
|
+
const { from, ...body } = fields;
|
|
57
|
+
return new BaseMessage({ type: COHORT_ADVERT, from, body });
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Factory function for creating a Cohort Opt-In message, which is sent by a participant to express
|
|
62
|
+
* interest in joining a cohort.
|
|
63
|
+
* @param {CohortOptInMessage} fields - The fields required to create a Cohort Opt-In message, which
|
|
64
|
+
* is sent by a participant to express interest in joining a cohort.
|
|
65
|
+
* @returns {BaseMessage} The created Cohort Opt-In message.
|
|
66
|
+
*/
|
|
67
|
+
export function createCohortOptInMessage(fields: CohortOptInMessage): BaseMessage {
|
|
68
|
+
const { from, to, ...body } = fields;
|
|
69
|
+
return new BaseMessage({ type: COHORT_OPT_IN, from, to, body });
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function createCohortOptInAcceptMessage(fields: CohortOptInAcceptMessage): BaseMessage {
|
|
73
|
+
const { from, to, ...body } = fields;
|
|
74
|
+
return new BaseMessage({ type: COHORT_OPT_IN_ACCEPT, from, to, body });
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function createCohortReadyMessage(fields: CohortReadyMessage): BaseMessage {
|
|
78
|
+
const { from, to, ...body } = fields;
|
|
79
|
+
return new BaseMessage({ type: COHORT_READY, from, to, body });
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Step 2: Update Submission
|
|
84
|
+
* Factory functions for creating messages related to the update submission step, where participants
|
|
85
|
+
* submit their signed updates for aggregation.
|
|
86
|
+
*/
|
|
87
|
+
type SubmitUpdateMessage = {
|
|
88
|
+
from: string;
|
|
89
|
+
to: string;
|
|
90
|
+
cohortId: string;
|
|
91
|
+
signedUpdate: Record<string, unknown>;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Factory function for creating a Submit Update message, which is sent by a participant to submit
|
|
96
|
+
* their signed update for aggregation.
|
|
97
|
+
* @param {SubmitUpdateMessage} fields - The fields required to create a Submit Update message,
|
|
98
|
+
* which is sent by a participant to submit their signed update for aggregation.
|
|
99
|
+
* @returns {BaseMessage} The created Submit Update message.
|
|
100
|
+
*/
|
|
101
|
+
export function createSubmitUpdateMessage(fields: SubmitUpdateMessage): BaseMessage {
|
|
102
|
+
const { from, to, ...body } = fields;
|
|
103
|
+
return new BaseMessage({ type: SUBMIT_UPDATE, from, to, body });
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Step 3: Aggregate & Validate
|
|
108
|
+
* Factory functions for creating messages related to the aggregate and validate step, where
|
|
109
|
+
* participants aggregate their updates and validate the aggregated data.
|
|
110
|
+
*/
|
|
111
|
+
|
|
112
|
+
type DistributeAggregatedDataMessage = {
|
|
113
|
+
from: string;
|
|
114
|
+
to: string;
|
|
115
|
+
cohortId: string;
|
|
116
|
+
beaconType: string;
|
|
117
|
+
signalBytesHex: string;
|
|
118
|
+
casAnnouncement?: Record<string, string>;
|
|
119
|
+
smtProof?: Record<string, unknown>;
|
|
120
|
+
};
|
|
121
|
+
type ValidationAckMessage = {
|
|
122
|
+
from: string;
|
|
123
|
+
to: string;
|
|
124
|
+
cohortId: string;
|
|
125
|
+
approved: boolean;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Factory function for creating a Distribute Aggregated Data message, which is sent by the
|
|
130
|
+
* aggregator to distribute the aggregated data to participants for validation.
|
|
131
|
+
* @param {DistributeAggregatedDataMessage} fields - The fields required to create a Distribute
|
|
132
|
+
* Aggregated Data message, which is sent by the aggregator to distribute the aggregated data to
|
|
133
|
+
* participants for validation.
|
|
134
|
+
* @returns {BaseMessage} The created Distribute Aggregated Data message.
|
|
135
|
+
*/
|
|
136
|
+
export function createDistributeAggregatedDataMessage(
|
|
137
|
+
fields: DistributeAggregatedDataMessage
|
|
138
|
+
): BaseMessage {
|
|
139
|
+
const { from, to, ...body } = fields;
|
|
140
|
+
return new BaseMessage({ type: DISTRIBUTE_AGGREGATED_DATA, from, to, body });
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Factory function for creating a Validation Acknowledgment message, which is sent by a participant
|
|
145
|
+
* to acknowledge.
|
|
146
|
+
* @param {ValidationAckMessage} fields - The fields required to create a Validation Acknowledgment
|
|
147
|
+
* message, which is sent by a participant to acknowledge the validation of the aggregated data.
|
|
148
|
+
* @returns {BaseMessage} The created Validation Acknowledgment message.
|
|
149
|
+
*/
|
|
150
|
+
export function createValidationAckMessage(fields: ValidationAckMessage): BaseMessage {
|
|
151
|
+
const { from, to, ...body } = fields;
|
|
152
|
+
return new BaseMessage({ type: VALIDATION_ACK, from, to, body });
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Step 4: Signing
|
|
157
|
+
* Factory functions for creating messages related to the signing step, where participants request
|
|
158
|
+
* signatures, contribute nonces, and authorize signatures.
|
|
159
|
+
*/
|
|
160
|
+
|
|
161
|
+
type AuthorizationRequestMessage = {
|
|
162
|
+
from: string;
|
|
163
|
+
to: string;
|
|
164
|
+
cohortId: string;
|
|
165
|
+
sessionId: string;
|
|
166
|
+
pendingTx: string;
|
|
167
|
+
prevOutValue: string;
|
|
168
|
+
};
|
|
169
|
+
type NonceContributionMessage = {
|
|
170
|
+
from: string;
|
|
171
|
+
to: string;
|
|
172
|
+
cohortId: string;
|
|
173
|
+
sessionId: string;
|
|
174
|
+
nonceContribution: Uint8Array;
|
|
175
|
+
};
|
|
176
|
+
type AggregatedNonceMessage = {
|
|
177
|
+
from: string;
|
|
178
|
+
to: string;
|
|
179
|
+
cohortId: string;
|
|
180
|
+
sessionId: string;
|
|
181
|
+
aggregatedNonce: Uint8Array;
|
|
182
|
+
};
|
|
183
|
+
type SignatureAuthorizationMessage = {
|
|
184
|
+
from: string;
|
|
185
|
+
to: string;
|
|
186
|
+
cohortId: string;
|
|
187
|
+
sessionId: string;
|
|
188
|
+
partialSignature: Uint8Array;
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Factory function for creating an Authorization Request message, which is sent by a participant to
|
|
193
|
+
* request authorization for their signature.
|
|
194
|
+
* @param {AuthorizationRequestMessage} fields - The fields required to create an Authorization
|
|
195
|
+
* Request message, which is sent by a participant to request authorization for their signature.
|
|
196
|
+
* @returns {BaseMessage} The created Authorization Request message.
|
|
197
|
+
*/
|
|
198
|
+
export function createAuthorizationRequestMessage(fields: AuthorizationRequestMessage): BaseMessage {
|
|
199
|
+
const { from, to, ...body } = fields;
|
|
200
|
+
return new BaseMessage({ type: AUTHORIZATION_REQUEST, from, to, body });
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Factory function for creating a Nonce Contribution message, which is sent by a participant to
|
|
205
|
+
* contribute their nonce for the signature aggregation process.
|
|
206
|
+
* @param {NonceContributionMessage} fields - The fields required to create a Nonce Contribution
|
|
207
|
+
* message, which is sent by a participant to contribute their nonce for the signature aggregation
|
|
208
|
+
* process.
|
|
209
|
+
* @returns {BaseMessage} The created Nonce Contribution message.
|
|
210
|
+
*/
|
|
211
|
+
export function createNonceContributionMessage(fields: NonceContributionMessage): BaseMessage {
|
|
212
|
+
const { from, to, ...body } = fields;
|
|
213
|
+
return new BaseMessage({ type: NONCE_CONTRIBUTION, from, to, body });
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Factory function for creating an Aggregated Nonce message, which is sent by the aggregator to
|
|
218
|
+
* distribute the aggregated nonce to participants for the signature aggregation process.
|
|
219
|
+
* @param {AggregatedNonceMessage} fields - The fields required to create an Aggregated Nonce
|
|
220
|
+
* message, which is sent by the aggregator to distribute the aggregated nonce to participants for
|
|
221
|
+
* the signature aggregation process.
|
|
222
|
+
* @returns {BaseMessage} The created Aggregated Nonce message.
|
|
223
|
+
*/
|
|
224
|
+
export function createAggregatedNonceMessage(fields: AggregatedNonceMessage): BaseMessage {
|
|
225
|
+
const { from, to, ...body } = fields;
|
|
226
|
+
return new BaseMessage({ type: AGGREGATED_NONCE, from, to, body });
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Factory function for creating a Signature Authorization message, which is sent by a participant
|
|
231
|
+
* to authorize their partial signature for the signature aggregation process.
|
|
232
|
+
* @param {SignatureAuthorizationMessage} fields - The fields required to create a Signature
|
|
233
|
+
* Authorization message, which is sent by a participant to authorize their partial signature for
|
|
234
|
+
* the signature aggregation process.
|
|
235
|
+
* @returns {BaseMessage} The created Signature Authorization message.
|
|
236
|
+
*/
|
|
237
|
+
export function createSignatureAuthorizationMessage(fields: SignatureAuthorizationMessage): BaseMessage {
|
|
238
|
+
const { from, to, ...body } = fields;
|
|
239
|
+
return new BaseMessage({ type: SIGNATURE_AUTHORIZATION, from, to, body });
|
|
240
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AGGREGATED_NONCE,
|
|
3
|
+
AUTHORIZATION_REQUEST,
|
|
4
|
+
COHORT_ADVERT,
|
|
5
|
+
COHORT_OPT_IN,
|
|
6
|
+
COHORT_OPT_IN_ACCEPT,
|
|
7
|
+
COHORT_READY,
|
|
8
|
+
DISTRIBUTE_AGGREGATED_DATA,
|
|
9
|
+
NONCE_CONTRIBUTION,
|
|
10
|
+
SIGNATURE_AUTHORIZATION,
|
|
11
|
+
SUBMIT_UPDATE,
|
|
12
|
+
VALIDATION_ACK,
|
|
13
|
+
} from './constants.js';
|
|
14
|
+
|
|
15
|
+
const KEYGEN_VALUES: Set<string> = new Set([
|
|
16
|
+
COHORT_ADVERT,
|
|
17
|
+
COHORT_OPT_IN,
|
|
18
|
+
COHORT_OPT_IN_ACCEPT,
|
|
19
|
+
COHORT_READY,
|
|
20
|
+
]);
|
|
21
|
+
|
|
22
|
+
const UPDATE_VALUES: Set<string> = new Set([
|
|
23
|
+
SUBMIT_UPDATE,
|
|
24
|
+
DISTRIBUTE_AGGREGATED_DATA,
|
|
25
|
+
VALIDATION_ACK,
|
|
26
|
+
]);
|
|
27
|
+
|
|
28
|
+
const SIGN_VALUES: Set<string> = new Set([
|
|
29
|
+
AUTHORIZATION_REQUEST,
|
|
30
|
+
NONCE_CONTRIBUTION,
|
|
31
|
+
AGGREGATED_NONCE,
|
|
32
|
+
SIGNATURE_AUTHORIZATION,
|
|
33
|
+
]);
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Checks if the provided type is a valid aggregation message type.
|
|
37
|
+
*/
|
|
38
|
+
export function isAggregationMessageType(type: string): boolean {
|
|
39
|
+
return KEYGEN_VALUES.has(type) || UPDATE_VALUES.has(type) || SIGN_VALUES.has(type);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Checks if the message type belongs to the keygen phase. */
|
|
43
|
+
export function isKeygenMessageType(type: string): boolean {
|
|
44
|
+
return KEYGEN_VALUES.has(type);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Checks if the message type belongs to the update phase. */
|
|
48
|
+
export function isUpdateMessageType(type: string): boolean {
|
|
49
|
+
return UPDATE_VALUES.has(type);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Checks if the message type belongs to the signing phase. */
|
|
53
|
+
export function isSignMessageType(type: string): boolean {
|
|
54
|
+
return SIGN_VALUES.has(type);
|
|
55
|
+
}
|