@did-btcr2/aggregation 0.1.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/LICENSE +373 -0
- package/README.md +19 -0
- package/dist/.tsbuildinfo +1 -0
- package/dist/browser.js +24017 -0
- package/dist/browser.mjs +24011 -0
- package/dist/cjs/index.js +5690 -0
- package/dist/cjs/package.json +1 -0
- package/dist/esm/beacon-strategy.js +76 -0
- package/dist/esm/beacon-strategy.js.map +1 -0
- package/dist/esm/cohort.js +316 -0
- package/dist/esm/cohort.js.map +1 -0
- package/dist/esm/conditions.js +102 -0
- package/dist/esm/conditions.js.map +1 -0
- package/dist/esm/errors.js +22 -0
- package/dist/esm/errors.js.map +1 -0
- package/dist/esm/fallback-spend.js +132 -0
- package/dist/esm/fallback-spend.js.map +1 -0
- package/dist/esm/index.js +21 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/logger.js +15 -0
- package/dist/esm/logger.js.map +1 -0
- package/dist/esm/messages/base.js +36 -0
- package/dist/esm/messages/base.js.map +1 -0
- package/dist/esm/messages/bodies.js +149 -0
- package/dist/esm/messages/bodies.js.map +1 -0
- package/dist/esm/messages/constants.js +37 -0
- package/dist/esm/messages/constants.js.map +1 -0
- package/dist/esm/messages/factories.js +139 -0
- package/dist/esm/messages/factories.js.map +1 -0
- package/dist/esm/messages/guards.js +40 -0
- package/dist/esm/messages/guards.js.map +1 -0
- package/dist/esm/messages/index.js +6 -0
- package/dist/esm/messages/index.js.map +1 -0
- package/dist/esm/participant.js +588 -0
- package/dist/esm/participant.js.map +1 -0
- package/dist/esm/phases.js +45 -0
- package/dist/esm/phases.js.map +1 -0
- package/dist/esm/recovery-policy.js +156 -0
- package/dist/esm/recovery-policy.js.map +1 -0
- package/dist/esm/recovery-spend.js +106 -0
- package/dist/esm/recovery-spend.js.map +1 -0
- package/dist/esm/runner/aggregation-runner.js +75 -0
- package/dist/esm/runner/aggregation-runner.js.map +1 -0
- package/dist/esm/runner/events.js +2 -0
- package/dist/esm/runner/events.js.map +1 -0
- package/dist/esm/runner/index.js +6 -0
- package/dist/esm/runner/index.js.map +1 -0
- package/dist/esm/runner/participant-runner.js +407 -0
- package/dist/esm/runner/participant-runner.js.map +1 -0
- package/dist/esm/runner/service-runner.js +701 -0
- package/dist/esm/runner/service-runner.js.map +1 -0
- package/dist/esm/runner/typed-emitter.js +80 -0
- package/dist/esm/runner/typed-emitter.js.map +1 -0
- package/dist/esm/service.js +774 -0
- package/dist/esm/service.js.map +1 -0
- package/dist/esm/signer.js +33 -0
- package/dist/esm/signer.js.map +1 -0
- package/dist/esm/signing-session.js +195 -0
- package/dist/esm/signing-session.js.map +1 -0
- package/dist/esm/transport/didcomm.js +41 -0
- package/dist/esm/transport/didcomm.js.map +1 -0
- package/dist/esm/transport/error.js +12 -0
- package/dist/esm/transport/error.js.map +1 -0
- package/dist/esm/transport/factory.js +29 -0
- package/dist/esm/transport/factory.js.map +1 -0
- package/dist/esm/transport/http/client.js +342 -0
- package/dist/esm/transport/http/client.js.map +1 -0
- package/dist/esm/transport/http/envelope.js +126 -0
- package/dist/esm/transport/http/envelope.js.map +1 -0
- package/dist/esm/transport/http/errors.js +11 -0
- package/dist/esm/transport/http/errors.js.map +1 -0
- package/dist/esm/transport/http/inbox-buffer.js +45 -0
- package/dist/esm/transport/http/inbox-buffer.js.map +1 -0
- package/dist/esm/transport/http/index.js +12 -0
- package/dist/esm/transport/http/index.js.map +1 -0
- package/dist/esm/transport/http/nonce-cache.js +38 -0
- package/dist/esm/transport/http/nonce-cache.js.map +1 -0
- package/dist/esm/transport/http/protocol.js +28 -0
- package/dist/esm/transport/http/protocol.js.map +1 -0
- package/dist/esm/transport/http/rate-limiter.js +45 -0
- package/dist/esm/transport/http/rate-limiter.js.map +1 -0
- package/dist/esm/transport/http/request-auth.js +100 -0
- package/dist/esm/transport/http/request-auth.js.map +1 -0
- package/dist/esm/transport/http/server.js +475 -0
- package/dist/esm/transport/http/server.js.map +1 -0
- package/dist/esm/transport/http/sse-stream.js +110 -0
- package/dist/esm/transport/http/sse-stream.js.map +1 -0
- package/dist/esm/transport/http/sse-writer.js +25 -0
- package/dist/esm/transport/http/sse-writer.js.map +1 -0
- package/dist/esm/transport/in-memory.js +149 -0
- package/dist/esm/transport/in-memory.js.map +1 -0
- package/dist/esm/transport/index.js +8 -0
- package/dist/esm/transport/index.js.map +1 -0
- package/dist/esm/transport/nostr.js +491 -0
- package/dist/esm/transport/nostr.js.map +1 -0
- package/dist/esm/transport/transport.js +2 -0
- package/dist/esm/transport/transport.js.map +1 -0
- package/dist/types/beacon-strategy.d.ts +56 -0
- package/dist/types/beacon-strategy.d.ts.map +1 -0
- package/dist/types/cohort.d.ts +188 -0
- package/dist/types/cohort.d.ts.map +1 -0
- package/dist/types/conditions.d.ts +86 -0
- package/dist/types/conditions.d.ts.map +1 -0
- package/dist/types/errors.d.ts +14 -0
- package/dist/types/errors.d.ts.map +1 -0
- package/dist/types/fallback-spend.d.ts +94 -0
- package/dist/types/fallback-spend.d.ts.map +1 -0
- package/dist/types/index.d.ts +17 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/logger.d.ts +22 -0
- package/dist/types/logger.d.ts.map +1 -0
- package/dist/types/messages/base.d.ts +58 -0
- package/dist/types/messages/base.d.ts.map +1 -0
- package/dist/types/messages/bodies.d.ts +177 -0
- package/dist/types/messages/bodies.d.ts.map +1 -0
- package/dist/types/messages/constants.d.ts +31 -0
- package/dist/types/messages/constants.d.ts.map +1 -0
- package/dist/types/messages/factories.d.ts +217 -0
- package/dist/types/messages/factories.d.ts.map +1 -0
- package/dist/types/messages/guards.d.ts +11 -0
- package/dist/types/messages/guards.d.ts.map +1 -0
- package/dist/types/messages/index.d.ts +6 -0
- package/dist/types/messages/index.d.ts.map +1 -0
- package/dist/types/participant.d.ts +162 -0
- package/dist/types/participant.d.ts.map +1 -0
- package/dist/types/phases.d.ts +55 -0
- package/dist/types/phases.d.ts.map +1 -0
- package/dist/types/recovery-policy.d.ts +132 -0
- package/dist/types/recovery-policy.d.ts.map +1 -0
- package/dist/types/recovery-spend.d.ts +94 -0
- package/dist/types/recovery-spend.d.ts.map +1 -0
- package/dist/types/runner/aggregation-runner.d.ts +72 -0
- package/dist/types/runner/aggregation-runner.d.ts.map +1 -0
- package/dist/types/runner/events.d.ts +147 -0
- package/dist/types/runner/events.d.ts.map +1 -0
- package/dist/types/runner/index.d.ts +6 -0
- package/dist/types/runner/index.d.ts.map +1 -0
- package/dist/types/runner/participant-runner.d.ts +122 -0
- package/dist/types/runner/participant-runner.d.ts.map +1 -0
- package/dist/types/runner/service-runner.d.ts +228 -0
- package/dist/types/runner/service-runner.d.ts.map +1 -0
- package/dist/types/runner/typed-emitter.d.ts +41 -0
- package/dist/types/runner/typed-emitter.d.ts.map +1 -0
- package/dist/types/service.d.ts +177 -0
- package/dist/types/service.d.ts.map +1 -0
- package/dist/types/signer.d.ts +50 -0
- package/dist/types/signer.d.ts.map +1 -0
- package/dist/types/signing-session.d.ts +81 -0
- package/dist/types/signing-session.d.ts.map +1 -0
- package/dist/types/transport/didcomm.d.ts +23 -0
- package/dist/types/transport/didcomm.d.ts.map +1 -0
- package/dist/types/transport/error.d.ts +8 -0
- package/dist/types/transport/error.d.ts.map +1 -0
- package/dist/types/transport/factory.d.ts +28 -0
- package/dist/types/transport/factory.d.ts.map +1 -0
- package/dist/types/transport/http/client.d.ts +56 -0
- package/dist/types/transport/http/client.d.ts.map +1 -0
- package/dist/types/transport/http/envelope.d.ts +64 -0
- package/dist/types/transport/http/envelope.d.ts.map +1 -0
- package/dist/types/transport/http/errors.d.ts +9 -0
- package/dist/types/transport/http/errors.d.ts.map +1 -0
- package/dist/types/transport/http/inbox-buffer.d.ts +32 -0
- package/dist/types/transport/http/inbox-buffer.d.ts.map +1 -0
- package/dist/types/transport/http/index.d.ts +12 -0
- package/dist/types/transport/http/index.d.ts.map +1 -0
- package/dist/types/transport/http/nonce-cache.d.ts +26 -0
- package/dist/types/transport/http/nonce-cache.d.ts.map +1 -0
- package/dist/types/transport/http/protocol.d.ts +53 -0
- package/dist/types/transport/http/protocol.d.ts.map +1 -0
- package/dist/types/transport/http/rate-limiter.d.ts +41 -0
- package/dist/types/transport/http/rate-limiter.d.ts.map +1 -0
- package/dist/types/transport/http/request-auth.d.ts +50 -0
- package/dist/types/transport/http/request-auth.d.ts.map +1 -0
- package/dist/types/transport/http/server.d.ts +118 -0
- package/dist/types/transport/http/server.d.ts.map +1 -0
- package/dist/types/transport/http/sse-stream.d.ts +34 -0
- package/dist/types/transport/http/sse-stream.d.ts.map +1 -0
- package/dist/types/transport/http/sse-writer.d.ts +12 -0
- package/dist/types/transport/http/sse-writer.d.ts.map +1 -0
- package/dist/types/transport/in-memory.d.ts +64 -0
- package/dist/types/transport/in-memory.d.ts.map +1 -0
- package/dist/types/transport/index.d.ts +8 -0
- package/dist/types/transport/index.d.ts.map +1 -0
- package/dist/types/transport/nostr.d.ts +153 -0
- package/dist/types/transport/nostr.d.ts.map +1 -0
- package/dist/types/transport/transport.d.ts +62 -0
- package/dist/types/transport/transport.d.ts.map +1 -0
- package/package.json +128 -0
- package/src/beacon-strategy.ts +142 -0
- package/src/cohort.ts +422 -0
- package/src/conditions.ts +172 -0
- package/src/errors.ts +25 -0
- package/src/fallback-spend.ts +223 -0
- package/src/index.ts +23 -0
- package/src/logger.ts +33 -0
- package/src/messages/base.ts +80 -0
- package/src/messages/bodies.ts +328 -0
- package/src/messages/constants.ts +40 -0
- package/src/messages/factories.ts +297 -0
- package/src/messages/guards.ts +61 -0
- package/src/messages/index.ts +5 -0
- package/src/participant.ts +779 -0
- package/src/phases.ts +91 -0
- package/src/recovery-policy.ts +237 -0
- package/src/recovery-spend.ts +192 -0
- package/src/runner/aggregation-runner.ts +125 -0
- package/src/runner/events.ts +129 -0
- package/src/runner/index.ts +5 -0
- package/src/runner/participant-runner.ts +488 -0
- package/src/runner/service-runner.ts +891 -0
- package/src/runner/typed-emitter.ts +87 -0
- package/src/service.ts +1000 -0
- package/src/signer.ts +67 -0
- package/src/signing-session.ts +290 -0
- package/src/transport/didcomm.ts +59 -0
- package/src/transport/error.ts +13 -0
- package/src/transport/factory.ts +65 -0
- package/src/transport/http/client.ts +409 -0
- package/src/transport/http/envelope.ts +204 -0
- package/src/transport/http/errors.ts +11 -0
- package/src/transport/http/inbox-buffer.ts +53 -0
- package/src/transport/http/index.ts +11 -0
- package/src/transport/http/nonce-cache.ts +43 -0
- package/src/transport/http/protocol.ts +57 -0
- package/src/transport/http/rate-limiter.ts +75 -0
- package/src/transport/http/request-auth.ts +164 -0
- package/src/transport/http/server.ts +617 -0
- package/src/transport/http/sse-stream.ts +121 -0
- package/src/transport/http/sse-writer.ts +23 -0
- package/src/transport/in-memory.ts +178 -0
- package/src/transport/index.ts +7 -0
- package/src/transport/nostr.ts +576 -0
- package/src/transport/transport.ts +79 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"phases.js","sourceRoot":"","sources":["../../src/phases.ts"],"names":[],"mappings":"AAyBA,MAAM,CAAN,IAAY,kBAeX;AAfD,WAAY,kBAAkB;IAC5B,yCAAmB,CAAA;IACnB,+CAAyB,CAAA;IACzB,6CAAuB,CAAA;IACvB,6DAAuC,CAAA;IACvC,2DAAqC,CAAA;IACrC,yDAAmC,CAAA;IACnC,6CAAuB,CAAA;IACvB,uDAAiC,CAAA;IACjC,yDAAmC,CAAA;IACnC,iEAA2C,CAAA;IAC3C,6FAA6F;IAC7F,6DAAuC,CAAA;IACvC,2CAAqB,CAAA;IACrB,uCAAiB,CAAA;AACnB,CAAC,EAfW,kBAAkB,KAAlB,kBAAkB,QAe7B;AAiBD,MAAM,CAAN,IAAY,sBAgBX;AAhBD,WAAY,sBAAsB;IAChC,mDAAyB,CAAA;IACzB,6CAAmB,CAAA;IACnB,qDAA2B,CAAA;IAC3B,6DAAmC,CAAA;IACnC,+FAA+F;IAC/F,qDAA2B,CAAA;IAC3B,mEAAyC,CAAA;IACzC,2DAAiC,CAAA;IACjC,6DAAmC,CAAA;IACnC,iDAAuB,CAAA;IACvB,mEAAyC,CAAA;IACzC,2FAA2F;IAC3F,qEAA2C,CAAA;IAC3C,+CAAqB,CAAA;IACrB,2CAAiB,CAAA;AACnB,CAAC,EAhBW,sBAAsB,KAAtB,sBAAsB,QAgBjC;AAUD,MAAM,CAAN,IAAY,mBAOX;AAPD,WAAY,mBAAmB;IAC7B,gFAAyD,CAAA;IACzD,gFAAyD,CAAA;IACzD,8EAAuD,CAAA;IACvD,8EAAuD,CAAA;IACvD,4CAAqB,CAAA;IACrB,wCAAiB,CAAA;AACnB,CAAC,EAPW,mBAAmB,KAAnB,mBAAmB,QAO9B"}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Recovery policy for aggregate beacon outputs.
|
|
3
|
+
*
|
|
4
|
+
* An aggregate beacon UTXO is a Taproot output whose key path is the cohort's
|
|
5
|
+
* n-of-n MuSig2 aggregate key. A key-path-only output can be spent only with a
|
|
6
|
+
* signature from every cohort member, so a single missing signer would lock the
|
|
7
|
+
* funds forever. To prevent that, the output commits to a script tree with two
|
|
8
|
+
* leaves:
|
|
9
|
+
* - a k-of-n fallback leaf (`p2tr_ms`, BIP-342 CHECKSIGADD): any k cohort members
|
|
10
|
+
* can still push the announcement through if some signers go missing, so the
|
|
11
|
+
* beacon stays live without waiting out the timelock.
|
|
12
|
+
* - a CSV recovery leaf: after a relative-timelock delay the funder can reclaim
|
|
13
|
+
* the UTXO unilaterally with its own recovery key, so funds are never
|
|
14
|
+
* permanently stranded even if fewer than k members remain.
|
|
15
|
+
*
|
|
16
|
+
* This module is the seam between the funding model and the concrete script
|
|
17
|
+
* leaves. Only `operator-funded` is implemented today (the k-of-n fallback leaf
|
|
18
|
+
* plus one CSV leaf keyed to the operator's recovery key). A future
|
|
19
|
+
* `participant-funded` model can be added as another case returning
|
|
20
|
+
* per-participant refund leaves, without touching the cohort, service, or
|
|
21
|
+
* signing code. See ADR 042.
|
|
22
|
+
*/
|
|
23
|
+
import { schnorr } from '@noble/curves/secp256k1.js';
|
|
24
|
+
import { concatBytes } from '@noble/hashes/utils';
|
|
25
|
+
import { Script, p2tr_ms } from '@scure/btc-signer';
|
|
26
|
+
import { sortKeys } from '@scure/btc-signer/musig2';
|
|
27
|
+
import { AggregationCohortError } from './errors.js';
|
|
28
|
+
/** Default funding model when an advert/config leaves it unspecified. */
|
|
29
|
+
export const DEFAULT_FUNDING_MODEL = 'operator-funded';
|
|
30
|
+
/**
|
|
31
|
+
* Default relative-timelock (in blocks) before operator recovery is spendable.
|
|
32
|
+
* About one day at 10-minute blocks. Operators set their own per-cohort value;
|
|
33
|
+
* this is only a convenience default for callers.
|
|
34
|
+
*/
|
|
35
|
+
export const DEFAULT_RECOVERY_SEQUENCE = 144;
|
|
36
|
+
/**
|
|
37
|
+
* Maximum allowed `recoverySequence`, 0xffff (65535 blocks, about 455 days).
|
|
38
|
+
*
|
|
39
|
+
* The value is a BIP-68 block-based relative timelock and is placed directly in
|
|
40
|
+
* the spending input's nSequence. BIP-68 only uses the low 16 bits as the value
|
|
41
|
+
* when the type flag (bit 22) is clear, so constraining to [1, 0xffff] keeps the
|
|
42
|
+
* timelock block-based and, critically, leaves the disable flag (bit 31) clear.
|
|
43
|
+
* A value with bit 31 set would disable CHECKSEQUENCEVERIFY entirely, letting the
|
|
44
|
+
* recovery key spend with no delay; an upper bound of 0xffff makes that
|
|
45
|
+
* unrepresentable.
|
|
46
|
+
*/
|
|
47
|
+
export const MAX_RECOVERY_SEQUENCE = 0xffff;
|
|
48
|
+
/** BIP-341 default tapscript leaf version. */
|
|
49
|
+
export const TAPROOT_LEAF_VERSION = 0xc0;
|
|
50
|
+
/** BIP-340 compact-size (varint) prefix for a byte length (scripts stay well under 2^32). */
|
|
51
|
+
function compactSize(n) {
|
|
52
|
+
if (n < 0xfd)
|
|
53
|
+
return new Uint8Array([n]);
|
|
54
|
+
if (n <= 0xffff)
|
|
55
|
+
return new Uint8Array([0xfd, n & 0xff, (n >> 8) & 0xff]);
|
|
56
|
+
return new Uint8Array([0xfe, n & 0xff, (n >> 8) & 0xff, (n >> 16) & 0xff, (n >> 24) & 0xff]);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* BIP-341 tapleaf hash: `taggedHash("TapLeaf", leafVersion || compactSize(len) || script)`.
|
|
60
|
+
* Used to key a script-path signature to its leaf when assembling the witness
|
|
61
|
+
* for a script-path spend (the fallback and recovery spend builders).
|
|
62
|
+
*/
|
|
63
|
+
export function tapLeafHash(script, leafVersion = TAPROOT_LEAF_VERSION) {
|
|
64
|
+
return schnorr.utils.taggedHash('TapLeaf', concatBytes(new Uint8Array([leafVersion]), compactSize(script.length), script));
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Resolve the effective fallback threshold k from an advertised value and the
|
|
68
|
+
* cohort size n. When unadvertised it defaults to n-1 (tolerate one missing or
|
|
69
|
+
* defecting signer, the cheapest useful fallback witness), floored at 1. Both
|
|
70
|
+
* the service and every participant resolve this identically so they derive the
|
|
71
|
+
* same beacon address.
|
|
72
|
+
*/
|
|
73
|
+
export function resolveFallbackThreshold(advertised, n) {
|
|
74
|
+
return advertised ?? Math.max(1, n - 1);
|
|
75
|
+
}
|
|
76
|
+
function assertFallbackParams({ cohortKeys, fallbackThreshold }) {
|
|
77
|
+
const n = cohortKeys.length;
|
|
78
|
+
if (n === 0) {
|
|
79
|
+
throw new AggregationCohortError('Cannot build fallback leaf: no cohort keys.', 'NO_COHORT_KEYS');
|
|
80
|
+
}
|
|
81
|
+
for (const key of cohortKeys) {
|
|
82
|
+
if (key.length !== 33) {
|
|
83
|
+
throw new AggregationCohortError(`Cohort key must be a 33-byte compressed public key, got ${key.length} bytes.`, 'INVALID_COHORT_KEY', { length: key.length });
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (!Number.isInteger(fallbackThreshold) || fallbackThreshold < 1 || fallbackThreshold > n) {
|
|
87
|
+
throw new AggregationCohortError(`Fallback threshold must be an integer in [1, ${n}] (k-of-n), got ${fallbackThreshold}.`, 'INVALID_FALLBACK_THRESHOLD', { fallbackThreshold, n });
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Build the k-of-n fallback leaf script: a BIP-342 `p2tr_ms` CHECKSIGADD
|
|
92
|
+
* multisig over the cohort's x-only keys. Any k members can spend this leaf to
|
|
93
|
+
* push the announcement through when the optimistic n-of-n key path stalls.
|
|
94
|
+
*
|
|
95
|
+
* The keys are sorted per BIP-327 (matching the MuSig2 internal-key ordering)
|
|
96
|
+
* and reduced to x-only, so the leaf is identical for every party that builds it.
|
|
97
|
+
*/
|
|
98
|
+
export function buildFallbackLeaf(params) {
|
|
99
|
+
assertFallbackParams(params);
|
|
100
|
+
const xOnlyKeys = sortKeys(params.cohortKeys).map(k => k.slice(1));
|
|
101
|
+
return p2tr_ms(params.fallbackThreshold, xOnlyKeys).script;
|
|
102
|
+
}
|
|
103
|
+
function assertRecoveryParams({ recoveryKey, recoverySequence }) {
|
|
104
|
+
if (recoveryKey.length !== 32) {
|
|
105
|
+
throw new AggregationCohortError(`Recovery key must be a 32-byte x-only public key, got ${recoveryKey.length} bytes.`, 'INVALID_RECOVERY_KEY');
|
|
106
|
+
}
|
|
107
|
+
if (!Number.isInteger(recoverySequence) || recoverySequence < 1 || recoverySequence > MAX_RECOVERY_SEQUENCE) {
|
|
108
|
+
throw new AggregationCohortError(`Recovery sequence must be a block-based BIP-68 relative timelock in [1, ${MAX_RECOVERY_SEQUENCE}], got ${recoverySequence}.`, 'INVALID_RECOVERY_SEQUENCE');
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Build the CSV recovery leaf script:
|
|
113
|
+
* `<recoverySequence> CHECKSEQUENCEVERIFY DROP <recoveryKey> CHECKSIG`.
|
|
114
|
+
*
|
|
115
|
+
* After `recoverySequence` blocks (relative to the UTXO's confirmation) the
|
|
116
|
+
* holder of `recoveryKey` can spend the output via the script path.
|
|
117
|
+
*/
|
|
118
|
+
export function buildRecoveryScript(params) {
|
|
119
|
+
assertRecoveryParams(params);
|
|
120
|
+
return Script.encode([
|
|
121
|
+
params.recoverySequence,
|
|
122
|
+
'CHECKSEQUENCEVERIFY',
|
|
123
|
+
'DROP',
|
|
124
|
+
params.recoveryKey,
|
|
125
|
+
'CHECKSIG',
|
|
126
|
+
]);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Build the Taproot script-tree leaves for a funding model. The returned leaves
|
|
130
|
+
* are the script tree the beacon output key commits to alongside the cohort's
|
|
131
|
+
* MuSig2 internal key.
|
|
132
|
+
*
|
|
133
|
+
* Canonical leaf order is fallback (k-of-n, leaf A) then CSV recovery (leaf B).
|
|
134
|
+
* For the current two-leaf tree the Merkle root is order-invariant (a TapBranch
|
|
135
|
+
* sorts its two child hashes), but fixing the order keeps the construction
|
|
136
|
+
* deterministic and reviewable, and is consensus-affecting should the tree ever
|
|
137
|
+
* grow past two leaves.
|
|
138
|
+
*
|
|
139
|
+
* @throws {AggregationCohortError} when the funding model is reserved (e.g.
|
|
140
|
+
* `participant-funded`) or unknown, or when the fallback/recovery params are
|
|
141
|
+
* invalid.
|
|
142
|
+
*/
|
|
143
|
+
export function buildRecoveryLeaves(fundingModel, params) {
|
|
144
|
+
switch (fundingModel) {
|
|
145
|
+
case 'operator-funded':
|
|
146
|
+
return [
|
|
147
|
+
{ script: buildFallbackLeaf(params) },
|
|
148
|
+
{ script: buildRecoveryScript(params) },
|
|
149
|
+
];
|
|
150
|
+
case 'participant-funded':
|
|
151
|
+
throw new AggregationCohortError('Funding model \'participant-funded\' is reserved and not yet implemented.', 'UNSUPPORTED_FUNDING_MODEL', { fundingModel });
|
|
152
|
+
default:
|
|
153
|
+
throw new AggregationCohortError(`Unknown funding model: ${fundingModel}.`, 'UNKNOWN_FUNDING_MODEL', { fundingModel });
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
//# s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iBAAiB,CAAC,MAAM,CAAC,EAAE;gBACrC,EAAE,MAAM,EAAE,mBAAmB,CAAC,MAAM,CAAC,EAAE;aACxC,CAAC;QACJ,KAAK,oBAAoB;YACvB,MAAM,IAAI,sBAAsB,CAC9B,2EAA2E,EAC3E,2BAA2B,EAAE,EAAE,YAAY,EAAE,CAC9C,CAAC;QACJ;YACE,MAAM,IAAI,sBAAsB,CAC9B,0BAA0B,YAAY,GAAG,EACzC,uBAAuB,EAAE,EAAE,YAAY,EAAE,CAC1C,CAAC;IACN,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Recovery spend builder for aggregate beacon outputs.
|
|
3
|
+
*
|
|
4
|
+
* An aggregate beacon UTXO is a Taproot output whose key path is the cohort's
|
|
5
|
+
* n-of-n MuSig2 aggregate key and whose script path carries a timelocked
|
|
6
|
+
* recovery leaf (see {@link ./recovery-policy.ts} and ADR 042). When the
|
|
7
|
+
* optimistic cooperative key-path spend cannot complete (a missing or defecting
|
|
8
|
+
* signer), the funder reclaims the UTXO via the recovery leaf after the
|
|
9
|
+
* relative-timelock delay.
|
|
10
|
+
*
|
|
11
|
+
* This module builds and signs that script-path spend. It is pure with respect
|
|
12
|
+
* to the network: it takes a UTXO reference plus the cohort and recovery
|
|
13
|
+
* parameters and returns a finalized {@link Transaction}; broadcasting and
|
|
14
|
+
* confirmation are the caller's concern. The recovery key path is a single
|
|
15
|
+
* BIP-340 Schnorr signature, so unlike the MuSig2 key path it can be driven by
|
|
16
|
+
* an opaque or HSM-backed signer.
|
|
17
|
+
*
|
|
18
|
+
* The relative timelock is enforced by consensus (BIP-68/112): the spend is only
|
|
19
|
+
* valid once the UTXO has `recoverySequence` confirmations, and the input's
|
|
20
|
+
* nSequence is set accordingly. The transaction version is 2 so BIP-68 applies.
|
|
21
|
+
*/
|
|
22
|
+
import { getNetwork } from '@did-btcr2/bitcoin';
|
|
23
|
+
import { schnorr } from '@noble/curves/secp256k1.js';
|
|
24
|
+
import { p2tr, Transaction } from '@scure/btc-signer';
|
|
25
|
+
import { keyAggExport, keyAggregate, sortKeys } from '@scure/btc-signer/musig2';
|
|
26
|
+
import { AggregationCohortError } from './errors.js';
|
|
27
|
+
import { DEFAULT_FUNDING_MODEL, buildRecoveryLeaves, resolveFallbackThreshold } from './recovery-policy.js';
|
|
28
|
+
/**
|
|
29
|
+
* Conservative dust floor (satoshis). The standard p2pkh dust limit; a generic
|
|
30
|
+
* floor below which the recovered output would not be relayable regardless of
|
|
31
|
+
* the destination script type.
|
|
32
|
+
*/
|
|
33
|
+
const DUST_LIMIT_SATS = 546n;
|
|
34
|
+
function bytesEqual(a, b) {
|
|
35
|
+
return a.length === b.length && a.every((x, i) => x === b[i]);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Build and sign a script-path recovery spend of an aggregate beacon UTXO.
|
|
39
|
+
*
|
|
40
|
+
* Reconstructs the same Taproot output the cohort funded (MuSig2 internal key +
|
|
41
|
+
* recovery script tree), spends it through the recovery leaf with the operator's
|
|
42
|
+
* recovery key, and finalizes the witness (signature + leaf script + control
|
|
43
|
+
* block). The returned transaction is ready to broadcast once the UTXO has
|
|
44
|
+
* `recoverySequence` confirmations.
|
|
45
|
+
*
|
|
46
|
+
* @throws {AggregationCohortError} when the recovery secret key does not match
|
|
47
|
+
* the committed recovery key, when there are no cohort keys, or when the fee
|
|
48
|
+
* exceeds the UTXO value.
|
|
49
|
+
*/
|
|
50
|
+
export function buildRecoverySpend(params) {
|
|
51
|
+
const { cohortKeys, recoverySecretKey, recoveryKey, recoverySequence, fallbackThreshold, fundingModel, network, utxo, destinationAddress, fee, beaconAddress, } = params;
|
|
52
|
+
if (cohortKeys.length === 0) {
|
|
53
|
+
throw new AggregationCohortError('Cannot build recovery spend: no cohort keys.', 'NO_COHORT_KEYS');
|
|
54
|
+
}
|
|
55
|
+
// The recovery leaf commits to recoveryKey; signing it requires the matching
|
|
56
|
+
// secret. Fail loudly here rather than producing a transaction that cannot
|
|
57
|
+
// finalize (btc-signer would throw an opaque "No taproot scripts signed").
|
|
58
|
+
const derivedPub = schnorr.getPublicKey(recoverySecretKey);
|
|
59
|
+
if (!bytesEqual(derivedPub, recoveryKey)) {
|
|
60
|
+
throw new AggregationCohortError('Recovery secret key does not correspond to the committed recovery key.', 'RECOVERY_KEY_MISMATCH');
|
|
61
|
+
}
|
|
62
|
+
const out = utxo.value - fee;
|
|
63
|
+
if (out <= 0n) {
|
|
64
|
+
throw new AggregationCohortError(`Recovery fee ${fee} exceeds UTXO value ${utxo.value}.`, 'FEE_EXCEEDS_VALUE', { fee: fee.toString(), value: utxo.value.toString() });
|
|
65
|
+
}
|
|
66
|
+
if (out < DUST_LIMIT_SATS) {
|
|
67
|
+
throw new AggregationCohortError(`Recovered output ${out} is below the dust limit ${DUST_LIMIT_SATS}; the spend would not relay.`, 'DUST_OUTPUT', { output: out.toString(), dustLimit: DUST_LIMIT_SATS.toString() });
|
|
68
|
+
}
|
|
69
|
+
// Reconstruct the funded output. sortKeys mirrors the cohort's key ordering so
|
|
70
|
+
// the aggregate internal key (and therefore the address and control block)
|
|
71
|
+
// match the UTXO being spent.
|
|
72
|
+
const internalKey = keyAggExport(keyAggregate(sortKeys(cohortKeys)));
|
|
73
|
+
const leaves = buildRecoveryLeaves(fundingModel ?? DEFAULT_FUNDING_MODEL, {
|
|
74
|
+
recoveryKey, recoverySequence, cohortKeys,
|
|
75
|
+
fallbackThreshold: resolveFallbackThreshold(fallbackThreshold, cohortKeys.length),
|
|
76
|
+
});
|
|
77
|
+
const net = getNetwork(network);
|
|
78
|
+
// allowUnknownOutputs: the CSV recovery leaf is a custom script (see cohort.ts).
|
|
79
|
+
const payment = p2tr(internalKey, leaves, net, true);
|
|
80
|
+
// Catch a caller mismatch up front: if the reconstructed address does not match
|
|
81
|
+
// the cohort's funded address, the witnessUtxo script would not match the chain
|
|
82
|
+
// and the spend would be unspendable.
|
|
83
|
+
if (beaconAddress !== undefined && payment.address !== beaconAddress) {
|
|
84
|
+
throw new AggregationCohortError(`Reconstructed beacon address ${payment.address} does not match the cohort's funded address ${beaconAddress}.`, 'BEACON_ADDRESS_MISMATCH', { reconstructed: payment.address, expected: beaconAddress });
|
|
85
|
+
}
|
|
86
|
+
// allowUnknownInputs: the CSV recovery leaf is a custom (non-template) script,
|
|
87
|
+
// so btc-signer's finalizer treats it as 'unknown' and will only assemble the
|
|
88
|
+
// witness for it under this flag (it then signs "what we can": our single
|
|
89
|
+
// recovery signature). version 2 activates BIP-68 relative timelocks.
|
|
90
|
+
const tx = new Transaction({ version: 2, allowUnknownInputs: true });
|
|
91
|
+
// Only tapLeafScript is set (no tapInternalKey): btc-signer then skips the
|
|
92
|
+
// key-path branch and signs the recovery leaf with recoverySecretKey. The
|
|
93
|
+
// input nSequence carries the BIP-68 relative timelock the leaf enforces.
|
|
94
|
+
tx.addInput({
|
|
95
|
+
txid: utxo.txid,
|
|
96
|
+
index: utxo.vout,
|
|
97
|
+
witnessUtxo: { script: payment.script, amount: utxo.value },
|
|
98
|
+
tapLeafScript: payment.tapLeafScript,
|
|
99
|
+
sequence: recoverySequence,
|
|
100
|
+
});
|
|
101
|
+
tx.addOutputAddress(destinationAddress, out, net);
|
|
102
|
+
tx.signIdx(recoverySecretKey, 0);
|
|
103
|
+
tx.finalize();
|
|
104
|
+
return tx;
|
|
105
|
+
}
|
|
106
|
+
//# sourceMappingURL=recovery-spend.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"recovery-spend.js","sourceRoot":"","sources":["../../src/recovery-spend.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAC;AACrD,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,EAAE,YAAY,EAAE,YA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vF,CAAC;IACJ,CAAC;IAED,+EAA+E;IAC/E,8EAA8E;IAC9E,0EAA0E;IAC1E,sEAAsE;IACtE,MAAM,EAAE,GAAG,IAAI,WAAW,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,kBAAkB,EAAE,IAAI,EAAE,CAAC,CAAC;IACrE,2EAA2E;IAC3E,0EAA0E;IAC1E,0EAA0E;IAC1E,EAAE,CAAC,QAAQ,CAAC;QACV,IAAI,EAAY,IAAI,CAAC,IAAI;QACzB,KAAK,EAAW,IAAI,CAAC,IAAI;QACzB,WAAW,EAAK,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,KAAK,EAAE;QAC9D,aAAa,EAAG,OAAO,CAAC,aAAa;QACrC,QAAQ,EAAQ,gBAAgB;KACjC,CAAC,CAAC;IACH,EAAE,CAAC,gBAAgB,CAAC,kBAAkB,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;IAClD,EAAE,CAAC,OAAO,CAAC,iBAAiB,EAAE,CAAC,CAAC,CAAC;IACjC,EAAE,CAAC,QAAQ,EAAE,CAAC;IACd,OAAO,EAAE,CAAC;AACZ,CAAC"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { bytesToHex } from '@noble/hashes/utils';
|
|
2
|
+
import { DEFAULT_FUNDING_MODEL, DEFAULT_RECOVERY_SEQUENCE } from '../recovery-policy.js';
|
|
3
|
+
import { InMemoryBus, InMemoryTransport } from '../transport/in-memory.js';
|
|
4
|
+
import { AggregationParticipantRunner } from './participant-runner.js';
|
|
5
|
+
import { AggregationServiceRunner } from './service-runner.js';
|
|
6
|
+
/**
|
|
7
|
+
* High-level facades for driving an aggregation cohort to completion.
|
|
8
|
+
*
|
|
9
|
+
* @class AggregationRunner
|
|
10
|
+
*/
|
|
11
|
+
export class AggregationRunner {
|
|
12
|
+
/**
|
|
13
|
+
* Run a cohort of ONE participant entirely in-process and return the
|
|
14
|
+
* aggregated MuSig2 result.
|
|
15
|
+
*
|
|
16
|
+
* One party plays both the coordinating service and the lone participant,
|
|
17
|
+
* connected over an {@link InMemoryTransport} (no relay or HTTP server). This
|
|
18
|
+
* makes the single-participant aggregate-beacon path (the N=1 corner of the
|
|
19
|
+
* two-axis beacon matrix, see ADR 037) first-class, useful for generating
|
|
20
|
+
* and reproducing single-participant aggregate test vectors.
|
|
21
|
+
*
|
|
22
|
+
* The service advertises a cohort with `minParticipants: 1`; the participant
|
|
23
|
+
* joins, submits its update, and the two complete keygen, data distribution,
|
|
24
|
+
* validation, and a one-signer MuSig2 P2TR key-path signing round.
|
|
25
|
+
*
|
|
26
|
+
* @param options Service + participant identities, cohort config, and the
|
|
27
|
+
* update / tx-data callbacks.
|
|
28
|
+
* @returns The {@link AggregationResult} (cohort id, aggregated signature, signed tx).
|
|
29
|
+
*/
|
|
30
|
+
static async solo(options) {
|
|
31
|
+
const transport = new InMemoryTransport(new InMemoryBus());
|
|
32
|
+
transport.registerActor(options.service.did, options.service.keys);
|
|
33
|
+
transport.registerActor(options.participant.did, options.participant.keys);
|
|
34
|
+
// Pre-register communication keys both ways. Production exchanges these via
|
|
35
|
+
// the protocol handshake; in-process we wire them directly.
|
|
36
|
+
transport.registerPeer(options.participant.did, options.participant.keys.publicKey.compressed);
|
|
37
|
+
transport.registerPeer(options.service.did, options.service.keys.publicKey.compressed);
|
|
38
|
+
transport.start();
|
|
39
|
+
// In a solo run the service actor is the operator and holds full keys, so it
|
|
40
|
+
// can both advertise the recovery terms and, if ever needed, perform the
|
|
41
|
+
// recovery. Default the recovery key to the service's own x-only key.
|
|
42
|
+
const recoveryKey = options.config.recoveryKey
|
|
43
|
+
?? bytesToHex(options.service.keys.publicKey.compressed.slice(1));
|
|
44
|
+
const recoverySequence = options.config.recoverySequence ?? DEFAULT_RECOVERY_SEQUENCE;
|
|
45
|
+
const service = new AggregationServiceRunner({
|
|
46
|
+
transport,
|
|
47
|
+
did: options.service.did,
|
|
48
|
+
keys: options.service.keys,
|
|
49
|
+
config: { minParticipants: 1, network: options.config.network, beaconType: options.config.beaconType, recoveryKey, recoverySequence, fundingModel: DEFAULT_FUNDING_MODEL },
|
|
50
|
+
onProvideTxData: options.onProvideTxData,
|
|
51
|
+
feeEstimator: options.feeEstimator,
|
|
52
|
+
cohortTtlMs: options.cohortTtlMs,
|
|
53
|
+
phaseTimeoutMs: options.phaseTimeoutMs,
|
|
54
|
+
// In-process bus with the participant already listening: a single advert
|
|
55
|
+
// suffices, so disable the republish loop (no dangling interval).
|
|
56
|
+
advertRepeatIntervalMs: 0,
|
|
57
|
+
});
|
|
58
|
+
const participant = new AggregationParticipantRunner({
|
|
59
|
+
transport,
|
|
60
|
+
did: options.participant.did,
|
|
61
|
+
keys: options.participant.keys,
|
|
62
|
+
shouldJoin: async () => true,
|
|
63
|
+
onProvideUpdate: options.onProvideUpdate,
|
|
64
|
+
});
|
|
65
|
+
await participant.start();
|
|
66
|
+
try {
|
|
67
|
+
return await service.run();
|
|
68
|
+
}
|
|
69
|
+
finally {
|
|
70
|
+
participant.stop();
|
|
71
|
+
service.stop();
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=aggregation-runner.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aggregation-runner.js","sourceRoot":"","sources":["../../../src/runner/aggregation-runner.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,EAAE,qBAAqB,EAAE,yBAAyB,EAAE,MAAM,uBAAuB,CAAC;AAEzF,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAC3E,OAAO,EAAE,4BAA4B,EAAE,MAAM,yBAAyB,CAAC;AAEvE,OAAO,EAAE,wBAAwB,EAAE,MAAM,qBAAqB,CAAC;AA8C/D;;;;GAIG;AACH,MAAM,OAAO,iBAAiB;IAC5B;;;;;;;;;;;;;;;;;OAiB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,EAAE,OAAO,CAAC,MAAM,CAAC,UAAU,EAAE,WAAW,EAAE,gBAAgB,EAAE,YAAY,EAAE,qBAAqB,EAAE;YAC3L,eAAe,EAAU,OAAO,CAAC,eAAe;YAChD,YAAY,EAAa,OAAO,CAAC,YAAY;YAC7C,WAAW,EAAc,OAAO,CAAC,WAAW;YAC5C,cAAc,EAAW,OAAO,CAAC,cAAc;YAC/C,yEAAyE;YACzE,kEAAkE;YAClE,sBAAsB,EAAG,CAAC;SAC3B,CAAC,CAAC;QAEH,MAAM,WAAW,GAAG,IAAI,4BAA4B,CAAC;YACnD,SAAS;YACT,GAAG,EAAe,OAAO,CAAC,WAAW,CAAC,GAAG;YACzC,IAAI,EAAc,OAAO,CAAC,WAAW,CAAC,IAAI;YAC1C,UAAU,EAAQ,KAAK,IAAI,EAAE,CAAC,IAAI;YAClC,eAAe,EAAG,OAAO,CAAC,eAAe;SAC1C,CAAC,CAAC;QAEH,MAAM,WAAW,CAAC,KAAK,EAAE,CAAC;QAC1B,IAAI,CAAC;YACH,OAAO,MAAM,OAAO,CAAC,GAAG,EAAE,CAAC;QAC7B,CAAC;gBAAS,CAAC;YACT,WAAW,CAAC,IAAI,EAAE,CAAC;YACnB,OAAO,CAAC,IAAI,EAAE,CAAC;QACjB,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"events.js","sourceRoot":"","sources":["../../../src/runner/events.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/runner/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,aAAa,CAAC;AAC5B,cAAc,qBAAqB,CAAC;AACpC,cAAc,yBAAyB,CAAC;AACxC,cAAc,yBAAyB,CAAC"}
|