@did-btcr2/api 0.5.0 → 0.8.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 CHANGED
@@ -1,3 +1,132 @@
1
- # API
1
+ # @did-btcr2/api
2
2
 
3
- API layer for accessing the did:btcr2 SDK
3
+ High-level SDK facade for the did:btcr2 DID method. Wraps `@did-btcr2/method` and the surrounding crypto / bitcoin / key-management packages behind a single ergonomic entry point.
4
+
5
+ Part of the [`did-btcr2-js`](https://github.com/dcdpr/did-btcr2-js) monorepo.
6
+
7
+ ## Summary
8
+
9
+ The lower-level packages (`@did-btcr2/method`, `@did-btcr2/cryptosuite`, `@did-btcr2/key-manager`, `@did-btcr2/bitcoin`) are designed to be composable and sans-I/O. This package is the thin layer above them: it owns Bitcoin endpoint configuration, CAS retrieval, key management, and the dispatch loop for the sans-I/O state machines.
10
+
11
+ If you're integrating did:btcr2 into an app, start here. If you're customizing the protocol, drop down to `@did-btcr2/method` directly.
12
+
13
+ - **`DidBtcr2Api`** is the main facade. Lazy sub-facades for crypto, did, key manager, bitcoin, CAS, and the DID method itself.
14
+ - **`createApi(config?)`** is the factory. Pass `btc`, `cas`, `kms`, and `logger` overrides.
15
+ - **`UpdateBuilder`** is a fluent chain over `DidMethodApi.update()` for callers who prefer named steps over a positional argument bag.
16
+ - **`tryResolveDid(did)`** returns a discriminated `{ ok, document } | { ok, error, errorMessage }` instead of throwing, for cases where resolution failure is an expected outcome.
17
+
18
+ The api wires the configured `BitcoinApi` into the sans-I/O Resolver and Updater state machines, fulfilling `NeedBeaconSignals`, `NeedFunding`, `NeedBroadcast`, and CAS-related needs (`NeedGenesisDocument`, `NeedCASAnnouncement`, `NeedSignedUpdate`) automatically. `NeedSMTProof` is not auto-fulfilled by the facade: SMT proofs must be provided upfront via `options.sidecar.smtProofs`. Multi-party aggregation is out of scope here; drive the Updater directly and hand `NeedBroadcast` to the aggregation runner from `@did-btcr2/method`.
19
+
20
+ ## Install
21
+
22
+ ```bash
23
+ npm install @did-btcr2/api
24
+ ```
25
+
26
+ Or with pnpm:
27
+
28
+ ```bash
29
+ pnpm add @did-btcr2/api
30
+ ```
31
+
32
+ **Runtime note:** ESM-only package (requires `import`, not `require`). Ships a browser bundle at `dist/browser.mjs` for bundler-based environments. Requires Node >= 22.
33
+
34
+ ## Key Exports
35
+
36
+ | Concern | Entry point |
37
+ |---|---|
38
+ | Main facade | `DidBtcr2Api`, `createApi(config?)` |
39
+ | Sub-facades | `BitcoinApi`, `CasApi`, `CryptoApi`, `DidApi`, `KeyManagerApi`, `DidMethodApi` |
40
+ | Fluent update | `UpdateBuilder` (from `api.btcr2.buildUpdate(...)`) |
41
+ | Config types | `ApiConfig`, `BitcoinApiConfig`, `CasConfig`, `Logger` |
42
+ | Resolution result | `ResolutionResult` (`tryResolveDid` return type) |
43
+ | Re-exports from method/common | `DidDocument`, `DidDocumentBuilder`, `Identifier`, `IdentifierTypes` |
44
+
45
+ ## Quick Start
46
+
47
+ ### Generate a DID and resolve it
48
+
49
+ ```typescript
50
+ import { createApi } from '@did-btcr2/api';
51
+
52
+ const api = createApi({ btc: { network: 'mutinynet' } });
53
+
54
+ // Generate keypair, derive DID, import the secret into the in-process KMS.
55
+ const { did, keyId } = api.generateDid({ network: 'mutinynet' });
56
+
57
+ // Resolve. Bitcoin signals are fetched automatically via the configured BitcoinApi.
58
+ const resolution = await api.resolveDid(did);
59
+ console.log(resolution.didDocument?.id);
60
+ ```
61
+
62
+ ### Update via the fluent builder
63
+
64
+ ```typescript
65
+ import { LocalSigner } from '@did-btcr2/keypair';
66
+
67
+ const signed = await api.btcr2
68
+ .buildUpdate(currentDoc)
69
+ .patch({ op: 'add', path: '/service/-', value: newService })
70
+ .version(2)
71
+ .verificationMethodId('#initialKey')
72
+ .beacon('#beacon-0')
73
+ .signer(new LocalSigner(secretKey))
74
+ .execute();
75
+ ```
76
+
77
+ ### Resolve without throwing
78
+
79
+ ```typescript
80
+ const result = await api.tryResolveDid(did);
81
+ if (result.ok) {
82
+ console.log(result.document);
83
+ } else {
84
+ console.warn(`resolve failed: ${result.error} - ${result.errorMessage}`);
85
+ }
86
+ ```
87
+
88
+ ### Sign with a KMS-backed signer (HSM / cloud / external keystore)
89
+
90
+ ```typescript
91
+ import { KeyManagerSigner } from '@did-btcr2/key-manager';
92
+
93
+ const signer = new KeyManagerSigner(api.kms.kms, keyId);
94
+
95
+ await api.updateDid({
96
+ did,
97
+ patches : [{ op: 'add', path: '/service/-', value: newService }],
98
+ verificationMethodId : '#initialKey',
99
+ beaconId : '#beacon-0',
100
+ signer,
101
+ });
102
+ ```
103
+
104
+ ## Architecture Principles
105
+
106
+ - **Lazy sub-facades.** `api.btc` / `api.cas` / `api.btcr2` instantiate on first access. Creating an api without a Bitcoin config and never touching the chain costs nothing.
107
+ - **Layered config.** Constructor config is applied first, then per-call overrides win. Bitcoin endpoint defaults come from `@did-btcr2/bitcoin`'s `DEFAULT_BITCOIN_NETWORK_CONFIG`.
108
+ - **CAS has a sensible default.** If no `cas` config is passed, `api.cas` defaults to a read-only HTTP gateway against `https://ipfs.io`. Override for write capability or an alternative gateway.
109
+ - **Driver injection.** `api.btcr2.resolve(did, options)` accepts an optional override; the api passes its own `BitcoinConnection` automatically when none is provided.
110
+
111
+ ## Build & Test
112
+
113
+ ```bash
114
+ # From packages/api/
115
+ pnpm build # Compile ESM + browser bundle + type declarations
116
+ pnpm build:tests # Compile tests to tests/compiled/
117
+ pnpm test # Run the test suite with coverage
118
+ pnpm lint # ESLint (zero warnings tolerated)
119
+ ```
120
+
121
+ The `lib/` directory contains end-to-end scripts that exercise the full update path against regtest, mutinynet, signet, testnet3, and testnet4. Run with `bun packages/api/lib/e2e-*.ts` or `tsx`. On non-regtest networks the scripts persist generated secret keys to `lib/.e2e-keys/` (gitignored) so funds at beacon addresses can be recovered.
122
+
123
+ ## Documentation
124
+
125
+ - **Package docs on btcr2.dev** [btcr2.dev/impls/ts](https://btcr2.dev/impls/ts)
126
+ - **[ADR-006](../../docs/adr/006-api-package-boundary.md)** API package boundary
127
+ - **[ADR-024](../../docs/adr/024-api-facade-lazy-and-layered-config.md)** API facade lazy initialization + layered config
128
+ - **Source reference** See JSDoc on `DidBtcr2Api`, `DidMethodApi`, and the sub-facade classes.
129
+
130
+ ## License
131
+
132
+ [MPL-2.0](https://github.com/dcdpr/did-btcr2-js/blob/main/LICENSE)