@did-btcr2/method 0.13.1
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 +7 -0
- package/dist/browser.js +2364 -0
- package/dist/browser.js.map +7 -0
- package/dist/browser.mjs +2364 -0
- package/dist/browser.mjs.map +7 -0
- package/dist/cjs/bitcoin/constants.js +20 -0
- package/dist/cjs/bitcoin/constants.js.map +1 -0
- package/dist/cjs/bitcoin/errors.js +11 -0
- package/dist/cjs/bitcoin/errors.js.map +1 -0
- package/dist/cjs/bitcoin/index.js +95 -0
- package/dist/cjs/bitcoin/index.js.map +1 -0
- package/dist/cjs/bitcoin/interface.js +2 -0
- package/dist/cjs/bitcoin/interface.js.map +1 -0
- package/dist/cjs/bitcoin/network.js +17 -0
- package/dist/cjs/bitcoin/network.js.map +1 -0
- package/dist/cjs/bitcoin/rest-client.js +289 -0
- package/dist/cjs/bitcoin/rest-client.js.map +1 -0
- package/dist/cjs/bitcoin/rpc-client.js +722 -0
- package/dist/cjs/bitcoin/rpc-client.js.map +1 -0
- package/dist/cjs/bitcoin/taproot.js +219 -0
- package/dist/cjs/bitcoin/taproot.js.map +1 -0
- package/dist/cjs/btcr2/beacon/aggregation/coordinator.js +120 -0
- package/dist/cjs/btcr2/beacon/aggregation/coordinator.js.map +1 -0
- package/dist/cjs/btcr2/beacon/aggregation/messages/advert.js +24 -0
- package/dist/cjs/btcr2/beacon/aggregation/messages/advert.js.map +1 -0
- package/dist/cjs/btcr2/beacon/aggregation/messages/base.js +37 -0
- package/dist/cjs/btcr2/beacon/aggregation/messages/base.js.map +1 -0
- package/dist/cjs/btcr2/beacon/aggregation/messages/cohort-set.js +25 -0
- package/dist/cjs/btcr2/beacon/aggregation/messages/cohort-set.js.map +1 -0
- package/dist/cjs/btcr2/beacon/aggregation/messages/keygen.js +8 -0
- package/dist/cjs/btcr2/beacon/aggregation/messages/keygen.js.map +1 -0
- package/dist/cjs/btcr2/beacon/aggregation/messages/opt-in.js +23 -0
- package/dist/cjs/btcr2/beacon/aggregation/messages/opt-in.js.map +1 -0
- package/dist/cjs/btcr2/beacon/aggregation/messages/sign.js +7 -0
- package/dist/cjs/btcr2/beacon/aggregation/messages/sign.js.map +1 -0
- package/dist/cjs/btcr2/beacon/aggregation/models/cohort/index.js +92 -0
- package/dist/cjs/btcr2/beacon/aggregation/models/cohort/index.js.map +1 -0
- package/dist/cjs/btcr2/beacon/aggregation/models/cohort/status.js +8 -0
- package/dist/cjs/btcr2/beacon/aggregation/models/cohort/status.js.map +1 -0
- package/dist/cjs/btcr2/beacon/aggregation/participant.js +2 -0
- package/dist/cjs/btcr2/beacon/aggregation/participant.js.map +1 -0
- package/dist/cjs/btcr2/beacon/aggregation/protocol/nostr.js +57 -0
- package/dist/cjs/btcr2/beacon/aggregation/protocol/nostr.js.map +1 -0
- package/dist/cjs/btcr2/beacon/aggregation/protocol/service.js +2 -0
- package/dist/cjs/btcr2/beacon/aggregation/protocol/service.js.map +1 -0
- package/dist/cjs/btcr2/beacon/cid-aggregate.js +116 -0
- package/dist/cjs/btcr2/beacon/cid-aggregate.js.map +1 -0
- package/dist/cjs/btcr2/beacon/factory.js +30 -0
- package/dist/cjs/btcr2/beacon/factory.js.map +1 -0
- package/dist/cjs/btcr2/beacon/singleton.js +220 -0
- package/dist/cjs/btcr2/beacon/singleton.js.map +1 -0
- package/dist/cjs/btcr2/beacon/smt-aggregate.js +126 -0
- package/dist/cjs/btcr2/beacon/smt-aggregate.js.map +1 -0
- package/dist/cjs/btcr2/crud/create.js +102 -0
- package/dist/cjs/btcr2/crud/create.js.map +1 -0
- package/dist/cjs/btcr2/crud/deactivate.js +14 -0
- package/dist/cjs/btcr2/crud/deactivate.js.map +1 -0
- package/dist/cjs/btcr2/crud/read.js +686 -0
- package/dist/cjs/btcr2/crud/read.js.map +1 -0
- package/dist/cjs/btcr2/crud/update.js +195 -0
- package/dist/cjs/btcr2/crud/update.js.map +1 -0
- package/dist/cjs/btcr2/key-manager/index.js +290 -0
- package/dist/cjs/btcr2/key-manager/index.js.map +1 -0
- package/dist/cjs/btcr2/key-manager/interface.js +2 -0
- package/dist/cjs/btcr2/key-manager/interface.js.map +1 -0
- package/dist/cjs/did-btcr2.js +222 -0
- package/dist/cjs/did-btcr2.js.map +1 -0
- package/dist/cjs/index.js +27 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/interfaces/beacon.js +41 -0
- package/dist/cjs/interfaces/beacon.js.map +1 -0
- package/dist/cjs/interfaces/crud.js +2 -0
- package/dist/cjs/interfaces/crud.js.map +1 -0
- package/dist/cjs/interfaces/ibeacon.js +2 -0
- package/dist/cjs/interfaces/ibeacon.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/types/bitcoin.js +62 -0
- package/dist/cjs/types/bitcoin.js.map +1 -0
- package/dist/cjs/types/crud.js +2 -0
- package/dist/cjs/types/crud.js.map +1 -0
- package/dist/cjs/utils/appendix.js +221 -0
- package/dist/cjs/utils/appendix.js.map +1 -0
- package/dist/cjs/utils/beacons.js +206 -0
- package/dist/cjs/utils/beacons.js.map +1 -0
- package/dist/cjs/utils/did-document-builder.js +61 -0
- package/dist/cjs/utils/did-document-builder.js.map +1 -0
- package/dist/cjs/utils/did-document.js +380 -0
- package/dist/cjs/utils/did-document.js.map +1 -0
- package/dist/cjs/utils/general.js +195 -0
- package/dist/cjs/utils/general.js.map +1 -0
- package/dist/cjs/utils/identifier.js +238 -0
- package/dist/cjs/utils/identifier.js.map +1 -0
- package/dist/esm/bitcoin/constants.js +20 -0
- package/dist/esm/bitcoin/constants.js.map +1 -0
- package/dist/esm/bitcoin/errors.js +11 -0
- package/dist/esm/bitcoin/errors.js.map +1 -0
- package/dist/esm/bitcoin/index.js +95 -0
- package/dist/esm/bitcoin/index.js.map +1 -0
- package/dist/esm/bitcoin/interface.js +2 -0
- package/dist/esm/bitcoin/interface.js.map +1 -0
- package/dist/esm/bitcoin/network.js +17 -0
- package/dist/esm/bitcoin/network.js.map +1 -0
- package/dist/esm/bitcoin/rest-client.js +289 -0
- package/dist/esm/bitcoin/rest-client.js.map +1 -0
- package/dist/esm/bitcoin/rpc-client.js +722 -0
- package/dist/esm/bitcoin/rpc-client.js.map +1 -0
- package/dist/esm/bitcoin/taproot.js +219 -0
- package/dist/esm/bitcoin/taproot.js.map +1 -0
- package/dist/esm/btcr2/beacon/aggregation/coordinator.js +120 -0
- package/dist/esm/btcr2/beacon/aggregation/coordinator.js.map +1 -0
- package/dist/esm/btcr2/beacon/aggregation/messages/advert.js +24 -0
- package/dist/esm/btcr2/beacon/aggregation/messages/advert.js.map +1 -0
- package/dist/esm/btcr2/beacon/aggregation/messages/base.js +37 -0
- package/dist/esm/btcr2/beacon/aggregation/messages/base.js.map +1 -0
- package/dist/esm/btcr2/beacon/aggregation/messages/cohort-set.js +25 -0
- package/dist/esm/btcr2/beacon/aggregation/messages/cohort-set.js.map +1 -0
- package/dist/esm/btcr2/beacon/aggregation/messages/keygen.js +8 -0
- package/dist/esm/btcr2/beacon/aggregation/messages/keygen.js.map +1 -0
- package/dist/esm/btcr2/beacon/aggregation/messages/opt-in.js +23 -0
- package/dist/esm/btcr2/beacon/aggregation/messages/opt-in.js.map +1 -0
- package/dist/esm/btcr2/beacon/aggregation/messages/sign.js +7 -0
- package/dist/esm/btcr2/beacon/aggregation/messages/sign.js.map +1 -0
- package/dist/esm/btcr2/beacon/aggregation/models/cohort/index.js +92 -0
- package/dist/esm/btcr2/beacon/aggregation/models/cohort/index.js.map +1 -0
- package/dist/esm/btcr2/beacon/aggregation/models/cohort/status.js +8 -0
- package/dist/esm/btcr2/beacon/aggregation/models/cohort/status.js.map +1 -0
- package/dist/esm/btcr2/beacon/aggregation/participant.js +2 -0
- package/dist/esm/btcr2/beacon/aggregation/participant.js.map +1 -0
- package/dist/esm/btcr2/beacon/aggregation/protocol/nostr.js +57 -0
- package/dist/esm/btcr2/beacon/aggregation/protocol/nostr.js.map +1 -0
- package/dist/esm/btcr2/beacon/aggregation/protocol/service.js +2 -0
- package/dist/esm/btcr2/beacon/aggregation/protocol/service.js.map +1 -0
- package/dist/esm/btcr2/beacon/cid-aggregate.js +116 -0
- package/dist/esm/btcr2/beacon/cid-aggregate.js.map +1 -0
- package/dist/esm/btcr2/beacon/factory.js +30 -0
- package/dist/esm/btcr2/beacon/factory.js.map +1 -0
- package/dist/esm/btcr2/beacon/singleton.js +220 -0
- package/dist/esm/btcr2/beacon/singleton.js.map +1 -0
- package/dist/esm/btcr2/beacon/smt-aggregate.js +126 -0
- package/dist/esm/btcr2/beacon/smt-aggregate.js.map +1 -0
- package/dist/esm/btcr2/crud/create.js +102 -0
- package/dist/esm/btcr2/crud/create.js.map +1 -0
- package/dist/esm/btcr2/crud/deactivate.js +14 -0
- package/dist/esm/btcr2/crud/deactivate.js.map +1 -0
- package/dist/esm/btcr2/crud/read.js +686 -0
- package/dist/esm/btcr2/crud/read.js.map +1 -0
- package/dist/esm/btcr2/crud/update.js +195 -0
- package/dist/esm/btcr2/crud/update.js.map +1 -0
- package/dist/esm/btcr2/key-manager/index.js +290 -0
- package/dist/esm/btcr2/key-manager/index.js.map +1 -0
- package/dist/esm/btcr2/key-manager/interface.js +2 -0
- package/dist/esm/btcr2/key-manager/interface.js.map +1 -0
- package/dist/esm/did-btcr2.js +222 -0
- package/dist/esm/did-btcr2.js.map +1 -0
- package/dist/esm/index.js +27 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/interfaces/beacon.js +41 -0
- package/dist/esm/interfaces/beacon.js.map +1 -0
- package/dist/esm/interfaces/crud.js +2 -0
- package/dist/esm/interfaces/crud.js.map +1 -0
- package/dist/esm/interfaces/ibeacon.js +2 -0
- package/dist/esm/interfaces/ibeacon.js.map +1 -0
- package/dist/esm/types/bitcoin.js +62 -0
- package/dist/esm/types/bitcoin.js.map +1 -0
- package/dist/esm/types/crud.js +2 -0
- package/dist/esm/types/crud.js.map +1 -0
- package/dist/esm/utils/appendix.js +221 -0
- package/dist/esm/utils/appendix.js.map +1 -0
- package/dist/esm/utils/beacons.js +206 -0
- package/dist/esm/utils/beacons.js.map +1 -0
- package/dist/esm/utils/did-document-builder.js +61 -0
- package/dist/esm/utils/did-document-builder.js.map +1 -0
- package/dist/esm/utils/did-document.js +380 -0
- package/dist/esm/utils/did-document.js.map +1 -0
- package/dist/esm/utils/general.js +195 -0
- package/dist/esm/utils/general.js.map +1 -0
- package/dist/esm/utils/identifier.js +238 -0
- package/dist/esm/utils/identifier.js.map +1 -0
- package/dist/types/bitcoin/constants.d.ts +19 -0
- package/dist/types/bitcoin/constants.d.ts.map +1 -0
- package/dist/types/bitcoin/errors.d.ts +5 -0
- package/dist/types/bitcoin/errors.d.ts.map +1 -0
- package/dist/types/bitcoin/index.d.ts +75 -0
- package/dist/types/bitcoin/index.d.ts.map +1 -0
- package/dist/types/bitcoin/interface.d.ts +86 -0
- package/dist/types/bitcoin/interface.d.ts.map +1 -0
- package/dist/types/bitcoin/network.d.ts +2 -0
- package/dist/types/bitcoin/network.d.ts.map +1 -0
- package/dist/types/bitcoin/rest-client.d.ts +268 -0
- package/dist/types/bitcoin/rest-client.d.ts.map +1 -0
- package/dist/types/bitcoin/rpc-client.d.ts +506 -0
- package/dist/types/bitcoin/rpc-client.d.ts.map +1 -0
- package/dist/types/bitcoin/taproot.d.ts +34 -0
- package/dist/types/bitcoin/taproot.d.ts.map +1 -0
- package/dist/types/btcr2/beacon/aggregation/coordinator.d.ts +74 -0
- package/dist/types/btcr2/beacon/aggregation/coordinator.d.ts.map +1 -0
- package/dist/types/btcr2/beacon/aggregation/messages/advert.d.ts +22 -0
- package/dist/types/btcr2/beacon/aggregation/messages/advert.d.ts.map +1 -0
- package/dist/types/btcr2/beacon/aggregation/messages/base.d.ts +36 -0
- package/dist/types/btcr2/beacon/aggregation/messages/base.d.ts.map +1 -0
- package/dist/types/btcr2/beacon/aggregation/messages/cohort-set.d.ts +23 -0
- package/dist/types/btcr2/beacon/aggregation/messages/cohort-set.d.ts.map +1 -0
- package/dist/types/btcr2/beacon/aggregation/messages/keygen.d.ts +6 -0
- package/dist/types/btcr2/beacon/aggregation/messages/keygen.d.ts.map +1 -0
- package/dist/types/btcr2/beacon/aggregation/messages/opt-in.d.ts +22 -0
- package/dist/types/btcr2/beacon/aggregation/messages/opt-in.d.ts.map +1 -0
- package/dist/types/btcr2/beacon/aggregation/messages/sign.d.ts +5 -0
- package/dist/types/btcr2/beacon/aggregation/messages/sign.d.ts.map +1 -0
- package/dist/types/btcr2/beacon/aggregation/models/cohort/index.d.ts +77 -0
- package/dist/types/btcr2/beacon/aggregation/models/cohort/index.d.ts.map +1 -0
- package/dist/types/btcr2/beacon/aggregation/models/cohort/status.d.ts +7 -0
- package/dist/types/btcr2/beacon/aggregation/models/cohort/status.d.ts.map +1 -0
- package/dist/types/btcr2/beacon/aggregation/participant.d.ts +1 -0
- package/dist/types/btcr2/beacon/aggregation/participant.d.ts.map +1 -0
- package/dist/types/btcr2/beacon/aggregation/protocol/nostr.d.ts +36 -0
- package/dist/types/btcr2/beacon/aggregation/protocol/nostr.d.ts.map +1 -0
- package/dist/types/btcr2/beacon/aggregation/protocol/service.d.ts +6 -0
- package/dist/types/btcr2/beacon/aggregation/protocol/service.d.ts.map +1 -0
- package/dist/types/btcr2/beacon/cid-aggregate.d.ts +103 -0
- package/dist/types/btcr2/beacon/cid-aggregate.d.ts.map +1 -0
- package/dist/types/btcr2/beacon/factory.d.ts +17 -0
- package/dist/types/btcr2/beacon/factory.d.ts.map +1 -0
- package/dist/types/btcr2/beacon/singleton.d.ts +93 -0
- package/dist/types/btcr2/beacon/singleton.d.ts.map +1 -0
- package/dist/types/btcr2/beacon/smt-aggregate.d.ts +112 -0
- package/dist/types/btcr2/beacon/smt-aggregate.d.ts.map +1 -0
- package/dist/types/btcr2/crud/create.d.ts +92 -0
- package/dist/types/btcr2/crud/create.d.ts.map +1 -0
- package/dist/types/btcr2/crud/deactivate.d.ts +13 -0
- package/dist/types/btcr2/crud/deactivate.d.ts.map +1 -0
- package/dist/types/btcr2/crud/read.d.ts +341 -0
- package/dist/types/btcr2/crud/read.d.ts.map +1 -0
- package/dist/types/btcr2/crud/update.d.ts +83 -0
- package/dist/types/btcr2/crud/update.d.ts.map +1 -0
- package/dist/types/btcr2/key-manager/index.d.ts +145 -0
- package/dist/types/btcr2/key-manager/index.d.ts.map +1 -0
- package/dist/types/btcr2/key-manager/interface.d.ts +113 -0
- package/dist/types/btcr2/key-manager/interface.d.ts.map +1 -0
- package/dist/types/did-btcr2.d.ts +117 -0
- package/dist/types/did-btcr2.d.ts.map +1 -0
- package/dist/types/index.d.ts +26 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/interfaces/beacon.d.ts +57 -0
- package/dist/types/interfaces/beacon.d.ts.map +1 -0
- package/dist/types/interfaces/crud.d.ts +35 -0
- package/dist/types/interfaces/crud.d.ts.map +1 -0
- package/dist/types/interfaces/ibeacon.d.ts +66 -0
- package/dist/types/interfaces/ibeacon.d.ts.map +1 -0
- package/dist/types/types/bitcoin.d.ts +827 -0
- package/dist/types/types/bitcoin.d.ts.map +1 -0
- package/dist/types/types/crud.d.ts +38 -0
- package/dist/types/types/crud.d.ts.map +1 -0
- package/dist/types/utils/appendix.d.ts +118 -0
- package/dist/types/utils/appendix.d.ts.map +1 -0
- package/dist/types/utils/beacons.d.ts +156 -0
- package/dist/types/utils/beacons.d.ts.map +1 -0
- package/dist/types/utils/did-document-builder.d.ts +13 -0
- package/dist/types/utils/did-document-builder.d.ts.map +1 -0
- package/dist/types/utils/did-document.d.ts +211 -0
- package/dist/types/utils/did-document.d.ts.map +1 -0
- package/dist/types/utils/general.d.ts +85 -0
- package/dist/types/utils/general.d.ts.map +1 -0
- package/dist/types/utils/identifier.d.ts +59 -0
- package/dist/types/utils/identifier.d.ts.map +1 -0
- package/package.json +137 -0
- package/src/bitcoin/constants.ts +19 -0
- package/src/bitcoin/errors.ts +10 -0
- package/src/bitcoin/index.ts +154 -0
- package/src/bitcoin/interface.ts +160 -0
- package/src/bitcoin/network.ts +17 -0
- package/src/bitcoin/rest-client.ts +415 -0
- package/src/bitcoin/rpc-client.ts +888 -0
- package/src/bitcoin/taproot.ts +237 -0
- package/src/btcr2/beacon/aggregation/coordinator.ts +135 -0
- package/src/btcr2/beacon/aggregation/messages/advert.ts +36 -0
- package/src/btcr2/beacon/aggregation/messages/base.ts +59 -0
- package/src/btcr2/beacon/aggregation/messages/cohort-set.ts +37 -0
- package/src/btcr2/beacon/aggregation/messages/keygen.ts +8 -0
- package/src/btcr2/beacon/aggregation/messages/opt-in.ts +35 -0
- package/src/btcr2/beacon/aggregation/messages/sign.ts +7 -0
- package/src/btcr2/beacon/aggregation/models/cohort/index.ts +112 -0
- package/src/btcr2/beacon/aggregation/models/cohort/status.ts +7 -0
- package/src/btcr2/beacon/aggregation/participant.ts +0 -0
- package/src/btcr2/beacon/aggregation/protocol/nostr.ts +81 -0
- package/src/btcr2/beacon/aggregation/protocol/service.ts +6 -0
- package/src/btcr2/beacon/cid-aggregate.ts +154 -0
- package/src/btcr2/beacon/factory.ts +36 -0
- package/src/btcr2/beacon/singleton.ts +257 -0
- package/src/btcr2/beacon/smt-aggregate.ts +136 -0
- package/src/btcr2/crud/create.ts +160 -0
- package/src/btcr2/crud/deactivate.ts +13 -0
- package/src/btcr2/crud/read.ts +946 -0
- package/src/btcr2/crud/update.ts +277 -0
- package/src/btcr2/key-manager/index.ts +364 -0
- package/src/btcr2/key-manager/interface.ts +129 -0
- package/src/canonicalize.d.ts +6 -0
- package/src/did-btcr2.ts +288 -0
- package/src/index.ts +34 -0
- package/src/interfaces/beacon.ts +68 -0
- package/src/interfaces/crud.ts +36 -0
- package/src/interfaces/ibeacon.ts +76 -0
- package/src/types/bitcoin.ts +1028 -0
- package/src/types/crud.ts +41 -0
- package/src/utils/appendix.ts +257 -0
- package/src/utils/beacons.ts +276 -0
- package/src/utils/did-document-builder.ts +73 -0
- package/src/utils/did-document.ts +474 -0
- package/src/utils/general.ts +204 -0
- package/src/utils/identifier.ts +276 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { DidUpdatePayload, ProofBytes } from '@did-btcr2/common';
|
|
2
|
+
import { BeaconService } from '../interfaces/ibeacon.js';
|
|
3
|
+
import { Btc1DidDocument } from '../utils/did-document.js';
|
|
4
|
+
import { BlockV3 } from './bitcoin.js';
|
|
5
|
+
|
|
6
|
+
export type FindNextSignals = {
|
|
7
|
+
block: BlockV3;
|
|
8
|
+
beacons: BeaconService[]
|
|
9
|
+
};
|
|
10
|
+
export type Metadata = {
|
|
11
|
+
btc1Update: DidUpdatePayload;
|
|
12
|
+
proofs?: string;
|
|
13
|
+
};
|
|
14
|
+
export type SignalSidecarData = Metadata;
|
|
15
|
+
export interface Btc1SidecarData {
|
|
16
|
+
did: string;
|
|
17
|
+
}
|
|
18
|
+
export type SignalsMetadata = { [signalId: string]: Metadata; };
|
|
19
|
+
export interface SingletonSidecar extends Btc1SidecarData {
|
|
20
|
+
signalsMetadata: SignalsMetadata;
|
|
21
|
+
}
|
|
22
|
+
export interface CIDAggregateSidecar extends Btc1SidecarData {
|
|
23
|
+
initialDocument: Btc1DidDocument;
|
|
24
|
+
signalsMetadata: SignalsMetadata;
|
|
25
|
+
cidUpdates: Array<string>;
|
|
26
|
+
}
|
|
27
|
+
export interface SMTAggregateSidecar extends Btc1SidecarData {
|
|
28
|
+
initialDocument: Btc1DidDocument;
|
|
29
|
+
signalsMetadata: SignalsMetadata;
|
|
30
|
+
smtProof: ProofBytes;
|
|
31
|
+
}
|
|
32
|
+
export type BeaconSidecarData<T> =
|
|
33
|
+
T extends 'SingletonBeacon' ? SingletonSidecar :
|
|
34
|
+
T extends 'CIDAggregateBeacon' ? CIDAggregateSidecar :
|
|
35
|
+
T extends 'SMTAggregateBeacon' ? SMTAggregateSidecar :
|
|
36
|
+
T;
|
|
37
|
+
export type SidecarData = BeaconSidecarData<SingletonSidecar | CIDAggregateSidecar | SMTAggregateSidecar>;
|
|
38
|
+
export type GetSigningMethodParams = {
|
|
39
|
+
didDocument: Btc1DidDocument;
|
|
40
|
+
methodId?: string;
|
|
41
|
+
};
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import { Bytes, HashBytes, Logger, W3C_ZCAP_V1 } from '@did-btcr2/common';
|
|
2
|
+
import { strings } from '@helia/strings';
|
|
3
|
+
import {
|
|
4
|
+
DidDocument,
|
|
5
|
+
DidError,
|
|
6
|
+
DidErrorCode,
|
|
7
|
+
DidService,
|
|
8
|
+
DidVerificationMethod,
|
|
9
|
+
DidVerificationRelationship
|
|
10
|
+
} from '@web5/dids';
|
|
11
|
+
import { createHelia } from 'helia';
|
|
12
|
+
import { CID } from 'multiformats';
|
|
13
|
+
import { create as createDigest } from 'multiformats/hashes/digest';
|
|
14
|
+
import { Btc1RootCapability } from '../interfaces/crud.js';
|
|
15
|
+
import { Btc1VerificationMethod } from './did-document.js';
|
|
16
|
+
|
|
17
|
+
export interface DidComponents {
|
|
18
|
+
hrp: string;
|
|
19
|
+
idType: string;
|
|
20
|
+
version: number;
|
|
21
|
+
network: string;
|
|
22
|
+
genesisBytes: Bytes;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Implements {@link https://dcdpr.github.io/did-btcr2/#appendix | 9. Appendix} methods.
|
|
27
|
+
*
|
|
28
|
+
* @class Btc1Appendix
|
|
29
|
+
* @type {Btc1Appendix}
|
|
30
|
+
*/
|
|
31
|
+
export class Btc1Appendix {
|
|
32
|
+
/**
|
|
33
|
+
* Extracts a DID fragment from a given input
|
|
34
|
+
* @param {unknown} input The input to extract the DID fragment from
|
|
35
|
+
* @returns {string | undefined} The extracted DID fragment or undefined if not found
|
|
36
|
+
*/
|
|
37
|
+
public static extractDidFragment(input: unknown): string | undefined {
|
|
38
|
+
if (typeof input !== 'string') return undefined;
|
|
39
|
+
if (input.length === 0) return undefined;
|
|
40
|
+
return input;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Validates that the given object is a DidVerificationMethod
|
|
45
|
+
* @param {unknown} obj The object to validate
|
|
46
|
+
* @returns {boolean} A boolean indicating whether the object is a DidVerificationMethod
|
|
47
|
+
*/
|
|
48
|
+
public static isDidVerificationMethod(obj: unknown): obj is DidVerificationMethod {
|
|
49
|
+
// Validate that the given value is an object.
|
|
50
|
+
if (!obj || typeof obj !== 'object' || obj === null) return false;
|
|
51
|
+
|
|
52
|
+
// Validate that the object has the necessary properties of a DidVerificationMethod.
|
|
53
|
+
if (!('id' in obj && 'type' in obj && 'controller' in obj)) return false;
|
|
54
|
+
|
|
55
|
+
if (typeof obj.id !== 'string') return false;
|
|
56
|
+
if (typeof obj.type !== 'string') return false;
|
|
57
|
+
if (typeof obj.controller !== 'string') return false;
|
|
58
|
+
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Validates that the given object is a DidService
|
|
64
|
+
* @param {unknown} obj The object to validate
|
|
65
|
+
* @returns {boolean} A boolean indicating whether the object is a DidService
|
|
66
|
+
*/
|
|
67
|
+
public static isDidService(obj: unknown): obj is DidService {
|
|
68
|
+
// Validate that the given value is an object.
|
|
69
|
+
if (!obj || typeof obj !== 'object' || obj === null) return false;
|
|
70
|
+
// Validate that the object has the necessary properties of a DidService.
|
|
71
|
+
if (!('id' in obj && 'type' in obj && 'serviceEndpoint' in obj)) return false;
|
|
72
|
+
if (typeof obj.id !== 'string') return false;
|
|
73
|
+
if (typeof obj.type !== 'string') return false;
|
|
74
|
+
if (typeof obj.serviceEndpoint !== 'string') return false;
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Extracts the verification methods from a given DID Document
|
|
80
|
+
* @param {DidDocument} params.didDocument The DID Document to extract the verification methods from
|
|
81
|
+
* @returns {DidVerificationMethod[]} An array of DidVerificationMethod objects
|
|
82
|
+
* @throws {TypeError} if the didDocument is not provided
|
|
83
|
+
*/
|
|
84
|
+
public static getVerificationMethods(didDocument: DidDocument): Btc1VerificationMethod[] {
|
|
85
|
+
if (!didDocument) throw new TypeError(`Required parameter missing: 'didDocument'`);
|
|
86
|
+
const verificationMethods: DidVerificationMethod[] = [];
|
|
87
|
+
// Check the 'verificationMethod' array.
|
|
88
|
+
verificationMethods.push(...didDocument.verificationMethod?.filter(Btc1Appendix.isDidVerificationMethod) ?? []);
|
|
89
|
+
// Check verification relationship properties for embedded verification methods.
|
|
90
|
+
Object.keys(DidVerificationRelationship).forEach((relationship) => {
|
|
91
|
+
verificationMethods.push(
|
|
92
|
+
...(didDocument[relationship as keyof DidDocument] as (DidVerificationMethod)[])
|
|
93
|
+
?.filter(Btc1Appendix.isDidVerificationMethod) ?? []
|
|
94
|
+
);
|
|
95
|
+
});
|
|
96
|
+
return verificationMethods as Btc1VerificationMethod[];
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Implements {@link https://dcdpr.github.io/did-btcr2/#derive-root-capability-from-didbtc1-identifier | 9.4.1 Derive Root Capability from did:btcr2 Identifier}.
|
|
102
|
+
*
|
|
103
|
+
* The Derive Root Capability algorithm deterministically generates a ZCAP-LD root capability from a given did:btcr2
|
|
104
|
+
* identifier. Each root capability is unique to the identifier. This root capability is defined and understood by the
|
|
105
|
+
* did:btcr2 specification as the root capability to authorize updates to the specific did:btcr2 identifiers DID
|
|
106
|
+
* document. It takes in a did:btcr2 identifier and returns a rootCapability object. It returns the root capability.
|
|
107
|
+
*
|
|
108
|
+
* @param {string} identifier The did-btcr2 identifier to derive the root capability from
|
|
109
|
+
* @returns {Btc1RootCapability} The root capability object
|
|
110
|
+
* @example Root capability for updating the DID document for
|
|
111
|
+
* did:btcr2:k1q0rnnwf657vuu8trztlczvlmphjgc6q598h79cm6sp7c4fgqh0fkc0vzd9u
|
|
112
|
+
* ```
|
|
113
|
+
* {
|
|
114
|
+
* "@context": "https://w3id.org/zcap/v1",
|
|
115
|
+
* "id": "urn:zcap:root:did:btcr2:k1q0rnnwf657vuu8trztlczvlmphjgc6q598h79cm6sp7c4fgqh0fkc0vzd9u",
|
|
116
|
+
* "controller": "did:btcr2:k1q0rnnwf657vuu8trztlczvlmphjgc6q598h79cm6sp7c4fgqh0fkc0vzd9u",
|
|
117
|
+
* "invocationTarget": "did:btcr2:k1q0rnnwf657vuu8trztlczvlmphjgc6q598h79cm6sp7c4fgqh0fkc0vzd9u"
|
|
118
|
+
* }
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
public static deriveRootCapability(identifier: string): Btc1RootCapability {
|
|
122
|
+
// 1. Define rootCapability as an empty object.
|
|
123
|
+
const rootCapability = {} as Btc1RootCapability;
|
|
124
|
+
|
|
125
|
+
// 2. Set rootCapability.@context to ‘https://w3id.org/zcap/v1’.
|
|
126
|
+
rootCapability['@context'] = W3C_ZCAP_V1;
|
|
127
|
+
|
|
128
|
+
// 3. Set encodedIdentifier to result of calling algorithm encodeURIComponent(identifier).
|
|
129
|
+
const encodedIdentifier = encodeURIComponent(identifier);
|
|
130
|
+
|
|
131
|
+
// 4. Set rootCapability.id to urn:zcap:root:${encodedIdentifier}.
|
|
132
|
+
rootCapability.id = `urn:zcap:root:${encodedIdentifier}`;
|
|
133
|
+
|
|
134
|
+
// 5. Set rootCapability.controller to identifier.
|
|
135
|
+
rootCapability.controller = identifier;
|
|
136
|
+
|
|
137
|
+
// 6. Set rootCapability.invocationTarget to identifier.
|
|
138
|
+
rootCapability.invocationTarget = identifier;
|
|
139
|
+
|
|
140
|
+
// 7. Return rootCapability.
|
|
141
|
+
return rootCapability;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Implements {@link https://dcdpr.github.io/did-btcr2/#dereference-root-capability-identifier | 9.4.2 Dereference Root Capability Identifier}.
|
|
147
|
+
*
|
|
148
|
+
* This algorithm takes in capabilityId, a root capability identifier, and dereferences it to rootCapability, the root
|
|
149
|
+
* capability object.
|
|
150
|
+
*
|
|
151
|
+
* @param {string} capabilityId The root capability identifier to dereference.
|
|
152
|
+
* @returns {Btc1RootCapability} The root capability object.
|
|
153
|
+
* @example a didUpdatePayload with an invoked ZCAP-LD capability containing a patch defining how the DID document
|
|
154
|
+
* for did:btcr2:k1q0rnnwf657vuu8trztlczvlmphjgc6q598h79cm6sp7c4fgqh0fkc0vzd9u SHOULD be mutated.
|
|
155
|
+
* ```
|
|
156
|
+
* {
|
|
157
|
+
* "@context": [
|
|
158
|
+
* "https://w3id.org/zcap/v1",
|
|
159
|
+
* "https://w3id.org/security/data-integrity/v2",
|
|
160
|
+
* "https://w3id.org/json-ld-patch/v1"
|
|
161
|
+
* ],
|
|
162
|
+
* "patch": [
|
|
163
|
+
* {
|
|
164
|
+
* "op": "add",
|
|
165
|
+
* "path": "/service/4",
|
|
166
|
+
* "value": {
|
|
167
|
+
* "id": "#linked-domain",
|
|
168
|
+
* "type": "LinkedDomains",
|
|
169
|
+
* "serviceEndpoint": "https://contact-me.com"
|
|
170
|
+
* }
|
|
171
|
+
* }
|
|
172
|
+
* ],
|
|
173
|
+
* "proof": {
|
|
174
|
+
* "type": "DataIntegrityProof",
|
|
175
|
+
* "cryptosuite": "schnorr-secp256k1-jcs-2025",
|
|
176
|
+
* "verificationMethod": "did:btcr2:k1q0rnnwf657vuu8trztlczvlmphjgc6q598h79cm6sp7c4fgqh0fkc0vzd9u#initialKey",
|
|
177
|
+
* "invocationTarget": "did:btcr2:k1q0rnnwf657vuu8trztlczvlmphjgc6q598h79cm6sp7c4fgqh0fkc0vzd9u",
|
|
178
|
+
* "capability": "urn:zcap:root:did%3Abtc1%3Ak1q0rnnwf657vuu8trztlczvlmphjgc6q598h79cm6sp7c4fgqh0fkc0vzd9u",
|
|
179
|
+
* "capabilityAction": "Write",
|
|
180
|
+
* "proofPurpose": "assertionMethod",
|
|
181
|
+
* "proofValue": "z381yXYmxU8NudZ4HXY56DfMN6zfD8syvWcRXzT9xD9uYoQToo8QsXD7ahM3gXTzuay5WJbqTswt2BKaGWYn2hHhVFKJLXaDz"
|
|
182
|
+
* }
|
|
183
|
+
* }
|
|
184
|
+
*/
|
|
185
|
+
public static derefernceRootCapabilityIdentifier(capabilityId: string): Btc1RootCapability {
|
|
186
|
+
// 1. Set rootCapability to an empty object.
|
|
187
|
+
const rootCapability = {} as Btc1RootCapability;
|
|
188
|
+
|
|
189
|
+
// 2. Set components to the result of capabilityId.split(":").
|
|
190
|
+
const [urn, zcap, root, did] = capabilityId.split(':') ?? [];
|
|
191
|
+
|
|
192
|
+
// 3. Validate components:
|
|
193
|
+
// 1. Assert length of components is 4.
|
|
194
|
+
if ([urn, zcap, root, did].length !== 4) {
|
|
195
|
+
throw new DidError(DidErrorCode.InvalidDid, `Invalid capabilityId: ${capabilityId}`);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// 2. components[0] == urn.
|
|
199
|
+
if (!urn || urn !== 'urn') {
|
|
200
|
+
throw new DidError(DidErrorCode.InvalidDid, `Invalid capabilityId: ${capabilityId}`);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// 3. components[1] == zcap.
|
|
204
|
+
if (!zcap || zcap !== 'zcap') {
|
|
205
|
+
throw new DidError(DidErrorCode.InvalidDid, `Invalid capabilityId: ${capabilityId}`);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// 4. components[2] == root.
|
|
209
|
+
if (!root || root !== 'root') {
|
|
210
|
+
throw new DidError(DidErrorCode.InvalidDid, `Invalid capabilityId: ${capabilityId}`);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// 4. Set uriEncodedId to components[3].
|
|
214
|
+
const uriEncodedId = did;
|
|
215
|
+
|
|
216
|
+
// 5. Set btc1Identifier the result of decodeURIComponent(uriEncodedId).
|
|
217
|
+
const btc1Identifier = decodeURIComponent(uriEncodedId);
|
|
218
|
+
|
|
219
|
+
// 6. Set rootCapability.id to capabilityId.
|
|
220
|
+
rootCapability.id = capabilityId;
|
|
221
|
+
|
|
222
|
+
// 7. Set rootCapability.controller to btc1Identifier.
|
|
223
|
+
rootCapability.controller = btc1Identifier;
|
|
224
|
+
|
|
225
|
+
// 8. Set rootCapability.invocationTarget to btc1Identifier.
|
|
226
|
+
rootCapability.invocationTarget = btc1Identifier;
|
|
227
|
+
|
|
228
|
+
// 9. Return rootCapability.
|
|
229
|
+
return rootCapability;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Implements {@link https://dcdpr.github.io/did-btcr2/#fetch-content-from-addressable-storage | 9.3. Fetch Content from Addressable Storage}.
|
|
234
|
+
*
|
|
235
|
+
* The Fetch Content from Addressable Storage function takes in SHA256 hash of some content, hashBytes, converts these
|
|
236
|
+
* bytes to a IPFS v1 Content Identifier and attempts to retrieve the identified content from Content Addressable
|
|
237
|
+
* Storage (CAS). It returns the retrieved content or null.
|
|
238
|
+
*
|
|
239
|
+
* @param {HashBytes} hashBytes The SHA256 hash of the content to be fetched.
|
|
240
|
+
* @returns {string} The fetched content or null if not found.
|
|
241
|
+
*/
|
|
242
|
+
public static async fetchFromCas(hashBytes: HashBytes): Promise<string | undefined> {
|
|
243
|
+
// 1. Set cid to the result of converting hashBytes to an IPFS v1 CID.
|
|
244
|
+
const cid = CID.create(1, 1, createDigest(1, hashBytes));
|
|
245
|
+
|
|
246
|
+
// Create a Helia node connection to IPFS
|
|
247
|
+
const helia = strings(await createHelia());
|
|
248
|
+
|
|
249
|
+
// 2. Set content to the result of fetching the cid from a CAS system. Which CAS systems checked is up to implementation.
|
|
250
|
+
Logger.warn('// TODO: Is this right? Are implementations just supposed to check all CAS they trust?');
|
|
251
|
+
const content = await helia.get(cid, {});
|
|
252
|
+
|
|
253
|
+
// 3. If content for cid cannot be found, set content to null.
|
|
254
|
+
// 4. Return content.
|
|
255
|
+
return content;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import { Btcr2Error, DidBtcr2Error, Maybe, KeyBytes } from '@did-btcr2/common';
|
|
2
|
+
import { DidDocument, DidService } from '@web5/dids';
|
|
3
|
+
import { networks, payments } from 'bitcoinjs-lib';
|
|
4
|
+
import { BeaconFactory } from '../btcr2/beacon/factory.js';
|
|
5
|
+
import { BeaconService, BeaconServiceAddress } from '../interfaces/ibeacon.js';
|
|
6
|
+
import { Btc1Appendix } from './appendix.js';
|
|
7
|
+
import { Btc1DidDocument } from './did-document.js';
|
|
8
|
+
export interface GenerateBeaconParams {
|
|
9
|
+
identifier: string;
|
|
10
|
+
publicKey: KeyBytes;
|
|
11
|
+
network: networks.Network;
|
|
12
|
+
type: string;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Required parameters for generating Beacon Services.
|
|
16
|
+
* @interface GenerateBitcoinAddrsParams
|
|
17
|
+
* @type {GenerateBitcoinAddrsParams}
|
|
18
|
+
*/
|
|
19
|
+
export interface GenerateBitcoinAddrsParams {
|
|
20
|
+
publicKey: KeyBytes;
|
|
21
|
+
network: networks.Network;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Required parameters for generating Beacon Services.
|
|
26
|
+
* @interface GenerateBeaconServicesParams
|
|
27
|
+
* @type {GenerateBeaconServicesParams}
|
|
28
|
+
*/
|
|
29
|
+
export interface GenerateBeaconServicesParams {
|
|
30
|
+
publicKey: KeyBytes;
|
|
31
|
+
network: networks.Network
|
|
32
|
+
beaconType: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Static class of utility functions for the Beacon Service
|
|
37
|
+
* @class BeaconUtils
|
|
38
|
+
* @type {BeaconUtils}
|
|
39
|
+
*/
|
|
40
|
+
export class BeaconUtils {
|
|
41
|
+
/**
|
|
42
|
+
* Converts a BIP21 Bitcoin URI to a Bitcoin address
|
|
43
|
+
* @param {string} uri The BIP21 Bitcoin URI to convert
|
|
44
|
+
* @returns {string} The Bitcoin address extracted from the URI
|
|
45
|
+
* @throws {DidBtcr2Error} if the URI is not a valid Bitcoin URI
|
|
46
|
+
*/
|
|
47
|
+
public static parseBitcoinAddress(uri: string): string {
|
|
48
|
+
if (!uri.startsWith('bitcoin:')) {
|
|
49
|
+
throw new DidBtcr2Error('Invalid Bitcoin URI format', { type: 'BEACON_ERROR' });
|
|
50
|
+
}
|
|
51
|
+
return uri.replace('bitcoin:', '').split('?')[0]; // Extracts address from "bitcoin:<address>?params"
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Validates that the given object is a Beacon Service
|
|
56
|
+
* @param {BeaconService} obj The object to validate
|
|
57
|
+
* @returns {boolean} A boolean indicating whether the object is a Beacon Service
|
|
58
|
+
*/
|
|
59
|
+
public static isBeaconService(obj: Maybe<BeaconService>): boolean {
|
|
60
|
+
// Return false if the given obj is not a valid DidService.
|
|
61
|
+
if(!Btc1Appendix.isDidService(obj)) return false;
|
|
62
|
+
|
|
63
|
+
// Return false if the type is not a valid beacon service type.
|
|
64
|
+
if(!['SingletonBeacon', 'CIDAggregateBeacon', 'SMTAggregateBeacon'].includes(obj.type)) return false;
|
|
65
|
+
|
|
66
|
+
// Return false if the serviceEndpoint is not a valid BIP21 bitcoin address.
|
|
67
|
+
if ([obj.serviceEndpoint].flat().some(ep => typeof ep === 'string' && !ep.startsWith('bitcoin:'))) return false;
|
|
68
|
+
|
|
69
|
+
// Return false if the casType exists and is not a string.
|
|
70
|
+
if(obj.casType && typeof obj.casType !== 'string') return false;
|
|
71
|
+
|
|
72
|
+
// Else return true
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Extracts the services from a given DID Document
|
|
78
|
+
* @param {DidDocument} didDocument The DID Document to extract the services from
|
|
79
|
+
* @returns {DidService[]} An array of DidService objects
|
|
80
|
+
* @throws {TypeError} if the didDocument is not provided
|
|
81
|
+
*/
|
|
82
|
+
public static getBeaconServices(didDocument: DidDocument): BeaconService[] {
|
|
83
|
+
// Filter out any invalid did service objects.
|
|
84
|
+
const didServices: DidService[] = didDocument.service?.filter(Btc1Appendix.isDidService) ?? [];
|
|
85
|
+
// Filter for valid beacon service objects.
|
|
86
|
+
return (didServices.filter(this.isBeaconService) ?? []) as BeaconService[];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Generate all 3 Beacon Service Endpoints for a given public key.
|
|
91
|
+
* @param {GenerateBitcoinAddrsParams} params Required parameters for generating Beacon Services.
|
|
92
|
+
* @param {KeyBytes} params.publicKey Public key bytes used to generate the beacon object serviceEndpoint.
|
|
93
|
+
* @param {Network} params.network Bitcoin network interface from bitcoinlib-js.
|
|
94
|
+
* @returns {Array<Array<string>>} 2D Array of bitcoin addresses (p2pkh, p2wpkh, p2tr).
|
|
95
|
+
* @throws {DidBtcr2Error} if the bitcoin address is invalid.
|
|
96
|
+
*/
|
|
97
|
+
public static generateBeaconAddresses({ identifier, publicKey, network }: {
|
|
98
|
+
identifier: string;
|
|
99
|
+
publicKey: KeyBytes;
|
|
100
|
+
network: networks.Network;
|
|
101
|
+
}): Array<Array<string>> {
|
|
102
|
+
try {
|
|
103
|
+
const p2pkh = payments.p2pkh({ pubkey: publicKey, network }).address;
|
|
104
|
+
const p2wpkh = payments.p2wpkh({ pubkey: publicKey, network }).address;
|
|
105
|
+
const p2tr = payments.p2tr({ network, internalPubkey: publicKey.slice(1, 33) }).address;
|
|
106
|
+
if (!p2pkh || !p2wpkh || !p2tr) {
|
|
107
|
+
throw new DidBtcr2Error('Failed to generate bitcoin addresses');
|
|
108
|
+
}
|
|
109
|
+
return [
|
|
110
|
+
[`${identifier}#initialP2PKH`, p2pkh],
|
|
111
|
+
[`${identifier}#initialP2WPKH`, p2wpkh],
|
|
112
|
+
[`${identifier}#initialP2TR`, p2tr]
|
|
113
|
+
];
|
|
114
|
+
} catch (error) {
|
|
115
|
+
console.error(error);
|
|
116
|
+
process.exit(1);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Generate a set of Beacon Services for a given public key.
|
|
122
|
+
* @param {GenerateBeaconServicesParams} params Required parameters for generating Beacon Services.
|
|
123
|
+
* @param {KeyBytes} params.publicKey Public key bytes used to generate the beacon object serviceEndpoint.
|
|
124
|
+
* @param {Network} params.network Bitcoin network interface from bitcoinlib-js.
|
|
125
|
+
* @param {string} params.beaconType The type of beacon service to create.
|
|
126
|
+
* @param {string} params.addressType The type of address to create (p2pkh, p2wpkh, p2tr).
|
|
127
|
+
* @returns {BeaconService} A BeaconService object.
|
|
128
|
+
* @throws {DidBtcr2Error} if the bitcoin address is invalid.
|
|
129
|
+
*/
|
|
130
|
+
public static generateBeaconService({ id, publicKey: pubkey, network, addressType, type }: {
|
|
131
|
+
id: string;
|
|
132
|
+
publicKey: KeyBytes;
|
|
133
|
+
network: networks.Network;
|
|
134
|
+
addressType: 'p2pkh' | 'p2wpkh' | 'p2tr';
|
|
135
|
+
type: string;
|
|
136
|
+
}): BeaconService {
|
|
137
|
+
try {
|
|
138
|
+
if(!id.includes('#')) {
|
|
139
|
+
id = `${id}#initial${addressType.toUpperCase()}`;
|
|
140
|
+
}
|
|
141
|
+
const serviceEndpoint = `bitcoin:${payments[addressType]({ pubkey, network }).address}`;
|
|
142
|
+
return { id, type, serviceEndpoint, };
|
|
143
|
+
} catch (error) {
|
|
144
|
+
console.error(error);
|
|
145
|
+
process.exit(1);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Generate a custom Beacon Service.
|
|
151
|
+
* @param {GenerateBeaconServicesParams} params Required parameters for generating Beacon Services.
|
|
152
|
+
* @returns
|
|
153
|
+
*/
|
|
154
|
+
public static generateBeaconServiceCustom({ id, publicKey: pubkey, network, addressType, type }: {
|
|
155
|
+
id: string;
|
|
156
|
+
publicKey: KeyBytes;
|
|
157
|
+
network: networks.Network;
|
|
158
|
+
addressType: 'p2pkh' | 'p2wpkh' | 'p2tr';
|
|
159
|
+
type: string;
|
|
160
|
+
}): BeaconService {
|
|
161
|
+
try {
|
|
162
|
+
if(!id.includes('#')) {
|
|
163
|
+
throw new Btcr2Error(
|
|
164
|
+
'Invalid id format. It should include a fragment identifier (e.g., #initialP2PKH).',
|
|
165
|
+
'BEACON_ERROR',
|
|
166
|
+
{ id }
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
const serviceEndpoint = `bitcoin:${payments[addressType]({ pubkey, network }).address}`;
|
|
170
|
+
return { id, type, serviceEndpoint, };
|
|
171
|
+
} catch (error) {
|
|
172
|
+
console.error(error);
|
|
173
|
+
process.exit(1);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Generate beacon services.
|
|
179
|
+
* @param {GenerateBeaconServicesParams} params Required parameters for generating Beacon Services.
|
|
180
|
+
* @param {string} params.network The name of the Bitcoin network to use.
|
|
181
|
+
* @param {Uint8Array} params.publicKey Byte array representation of a public key used to generate a new btcr2 key-id-type.
|
|
182
|
+
* @param {string} params.beaconType Optional beacon type to use (default: SingletonBeacon).
|
|
183
|
+
* @returns {DidService[]} Array of DidService objects.
|
|
184
|
+
*/
|
|
185
|
+
public static generateBeaconServices({ identifier, network, type, publicKey }: {
|
|
186
|
+
identifier: string;
|
|
187
|
+
publicKey: KeyBytes;
|
|
188
|
+
network: networks.Network;
|
|
189
|
+
type: string;
|
|
190
|
+
}): Array<BeaconService> {
|
|
191
|
+
// Generate the bitcoin addresses
|
|
192
|
+
const bitcoinAddrs = this.generateBeaconAddresses({ identifier, publicKey, network, });
|
|
193
|
+
|
|
194
|
+
// Map the bitcoin addresses to the beacon service
|
|
195
|
+
return bitcoinAddrs.map(([id, address]) => {
|
|
196
|
+
// Convert the address to a BIP-21 URI
|
|
197
|
+
const serviceEndpoint = `bitcoin:${address}`;
|
|
198
|
+
// Create the beacon object
|
|
199
|
+
const beacon = BeaconFactory.establish({ id, type, serviceEndpoint });
|
|
200
|
+
// Return the beacon service
|
|
201
|
+
return beacon.service;
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Generate a single beacon service.
|
|
207
|
+
* @param {GenerateBeaconParams} params Required parameters for generating a single Beacon Service.
|
|
208
|
+
* @param {string} params.identifier The identifier for the beacon service.
|
|
209
|
+
* @param {string} params.network The name of the Bitcoin network to use.
|
|
210
|
+
* @param {Uint8Array} params.publicKey Byte array representation of a public key used to generate a new btcr2 key-id-type.
|
|
211
|
+
* @param {string} params.type The type of beacon service to create.
|
|
212
|
+
* @returns {BeaconService} A BeaconService object.
|
|
213
|
+
* @throws {DidBtcr2Error} if the bitcoin address is invalid.
|
|
214
|
+
*/
|
|
215
|
+
public static generateBeacon({ identifier, network, type, publicKey }: {
|
|
216
|
+
identifier: string;
|
|
217
|
+
publicKey: KeyBytes;
|
|
218
|
+
network: networks.Network;
|
|
219
|
+
type: string;
|
|
220
|
+
}): BeaconService {
|
|
221
|
+
// Generate the bitcoin addresses
|
|
222
|
+
const bitcoinAddrs = this.generateBeaconAddresses({ identifier, publicKey, network, });
|
|
223
|
+
|
|
224
|
+
// Map the bitcoin addresses to the beacon service
|
|
225
|
+
const beacon = bitcoinAddrs.map(([id, address]) => {
|
|
226
|
+
// Convert the address to a BIP-21 URI
|
|
227
|
+
const serviceEndpoint = `bitcoin:${address}`;
|
|
228
|
+
// Create the beacon object
|
|
229
|
+
const beacon = BeaconFactory.establish({ id, type, serviceEndpoint });
|
|
230
|
+
// Return the beacon service
|
|
231
|
+
return beacon.service;
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
return beacon[0];
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Manufacture a pre-filled Beacon using the BeaconFactory.
|
|
239
|
+
* @param {BeaconServicesParams} params Required parameters for generating a single Beacon Service.
|
|
240
|
+
* @param {string} params.serviceId The type of service being created (#initialP2PKH, #initialP2WPKH, #initialP2TR).
|
|
241
|
+
* @param {string} params.beaconType The type of beacon service being created (SingletonBeacon, SMTAggregatorBeacon).
|
|
242
|
+
* @param {BitcoinAddress} params.bitcoinAddress The bitcoin address to use for the service endpoint.
|
|
243
|
+
* @returns {BeaconService} One BeaconService object.
|
|
244
|
+
*/
|
|
245
|
+
public static manufactureBeacon(params: BeaconService): BeaconService {
|
|
246
|
+
return BeaconFactory.establish(params).service;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Convert beacon service endpoints from BIP-21 URIs to addresses.
|
|
251
|
+
* @param {Array<BeaconService>} beacons The list of beacon services.
|
|
252
|
+
* @returns {Array<BeaconServiceAddress>} An array of beacon services with address: bitcoinAddress.
|
|
253
|
+
*/
|
|
254
|
+
public static toBeaconServiceAddress(beacons: Array<BeaconService>): Array<BeaconServiceAddress> {
|
|
255
|
+
return beacons.map((beacon) => ({ ...beacon, address: beacon.serviceEndpoint.replace('bitcoin:', '')}));
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Create a map of address => beaconService with address field.
|
|
260
|
+
* @param {Array<BeaconService>} beacons The list of beacon services.
|
|
261
|
+
* @returns {Map<string, BeaconServiceAddress>} A map of address => beaconService.
|
|
262
|
+
*/
|
|
263
|
+
public static getBeaconServiceAddressMap(beacons: Array<BeaconService>): Map<string, BeaconServiceAddress> {
|
|
264
|
+
const beaconAddrs = this.toBeaconServiceAddress(beacons);
|
|
265
|
+
return new Map<string, BeaconServiceAddress>(beaconAddrs.map((beacon) => ([beacon.address, beacon])));
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Get the beacon service ids from a list of beacon services.
|
|
270
|
+
* @param {Btc1DidDocument} didDocument The DID Document to extract the services from.
|
|
271
|
+
* @returns {string[]} An array of beacon service ids.
|
|
272
|
+
*/
|
|
273
|
+
public static getBeaconServiceIds(didDocument: Btc1DidDocument): string[] {
|
|
274
|
+
return this.getBeaconServices(didDocument).map((beacon) => beacon.id);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { DidDocumentError, INVALID_DID_DOCUMENT } from '@did-btcr2/common';
|
|
2
|
+
import { BeaconService } from '../interfaces/ibeacon.js';
|
|
3
|
+
import { Btc1DidDocument, Btc1VerificationMethod } from './did-document.js';
|
|
4
|
+
|
|
5
|
+
export class Btc1DidDocumentBuilder {
|
|
6
|
+
private document: Partial<Btc1DidDocument> = {};
|
|
7
|
+
|
|
8
|
+
constructor(initialDocument: Partial<Btc1DidDocument>) {
|
|
9
|
+
if (!initialDocument.id) {
|
|
10
|
+
throw new DidDocumentError('Missing required "id" property', INVALID_DID_DOCUMENT, initialDocument);
|
|
11
|
+
}
|
|
12
|
+
this.document.id = initialDocument.id;
|
|
13
|
+
this.document.verificationMethod = initialDocument.verificationMethod ?? [];
|
|
14
|
+
|
|
15
|
+
if (initialDocument['@context']) {
|
|
16
|
+
this.document['@context'] = initialDocument['@context'];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
withController(controller?: Array<string>): this {
|
|
21
|
+
if (controller) {
|
|
22
|
+
this.document.controller = controller ?? [this.document.id!];
|
|
23
|
+
}
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
withAuthentication(authentication: Array<string | Btc1VerificationMethod>): this {
|
|
28
|
+
if (authentication) {
|
|
29
|
+
this.document.authentication = authentication;
|
|
30
|
+
}
|
|
31
|
+
return this;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
withAssertionMethod(assertionMethod: Array<string | Btc1VerificationMethod>): this {
|
|
35
|
+
if (assertionMethod) {
|
|
36
|
+
this.document.assertionMethod = assertionMethod;
|
|
37
|
+
}
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
withCapabilityInvocation(capabilityInvocation: Array<string | Btc1VerificationMethod>): this {
|
|
42
|
+
if (capabilityInvocation) {
|
|
43
|
+
this.document.capabilityInvocation = capabilityInvocation;
|
|
44
|
+
}
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
withCapabilityDelegation(capabilityDelegation: Array<string | Btc1VerificationMethod>): this {
|
|
49
|
+
if (capabilityDelegation) {
|
|
50
|
+
this.document.capabilityDelegation = capabilityDelegation;
|
|
51
|
+
}
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
withService(service: Array<BeaconService>): this {
|
|
56
|
+
if (service) {
|
|
57
|
+
this.document.service = service;
|
|
58
|
+
}
|
|
59
|
+
return this;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
build(): Btc1DidDocument {
|
|
63
|
+
const didDocument = new Btc1DidDocument(this.document as Btc1DidDocument);
|
|
64
|
+
|
|
65
|
+
for (const key of Object.keys(didDocument)) {
|
|
66
|
+
if (didDocument[key as keyof Btc1DidDocument] === undefined) {
|
|
67
|
+
delete didDocument[key as keyof Btc1DidDocument];
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return didDocument;
|
|
72
|
+
}
|
|
73
|
+
}
|