@did-btcr2/method 0.23.0 → 0.25.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 +96 -50
- package/dist/browser.js +36332 -37280
- package/dist/browser.mjs +36331 -37279
- package/dist/cjs/core/beacon/aggregation/communication/adapter/did-comm.js +1 -1
- package/dist/cjs/core/beacon/aggregation/communication/adapter/did-comm.js.map +1 -1
- package/dist/cjs/core/beacon/aggregation/communication/adapter/nostr.js +1 -1
- package/dist/cjs/core/beacon/aggregation/communication/adapter/nostr.js.map +1 -1
- package/dist/cjs/core/beacon/aggregation/coordinator.js +40 -44
- package/dist/cjs/core/beacon/aggregation/coordinator.js.map +1 -1
- package/dist/cjs/core/beacon/aggregation/participant.js +35 -38
- package/dist/cjs/core/beacon/aggregation/participant.js.map +1 -1
- package/dist/cjs/core/beacon/aggregation/session/index.js +3 -4
- package/dist/cjs/core/beacon/aggregation/session/index.js.map +1 -1
- package/dist/cjs/core/beacon/beacon.js.map +1 -1
- package/dist/cjs/core/beacon/cas-beacon.js +119 -7
- package/dist/cjs/core/beacon/cas-beacon.js.map +1 -1
- package/dist/cjs/core/beacon/factory.js +1 -1
- package/dist/cjs/core/beacon/factory.js.map +1 -1
- package/dist/cjs/core/beacon/{singleton.js → singleton-beacon.js} +19 -27
- package/dist/cjs/core/beacon/singleton-beacon.js.map +1 -0
- package/dist/cjs/core/beacon/smt-beacon.js +1 -1
- package/dist/cjs/core/beacon/smt-beacon.js.map +1 -1
- package/dist/cjs/core/identifier.js +1 -1
- package/dist/cjs/core/identifier.js.map +1 -1
- package/dist/{esm/core/resolve.js → cjs/core/resolver.js} +244 -92
- package/dist/cjs/core/resolver.js.map +1 -0
- package/dist/cjs/core/update.js +7 -7
- package/dist/cjs/core/update.js.map +1 -1
- package/dist/cjs/did-btcr2.js +34 -94
- package/dist/cjs/did-btcr2.js.map +1 -1
- package/dist/cjs/index.js +2 -3
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/utils/did-document.js +9 -19
- package/dist/cjs/utils/did-document.js.map +1 -1
- package/dist/esm/core/beacon/aggregation/communication/adapter/did-comm.js +1 -1
- package/dist/esm/core/beacon/aggregation/communication/adapter/did-comm.js.map +1 -1
- package/dist/esm/core/beacon/aggregation/communication/adapter/nostr.js +1 -1
- package/dist/esm/core/beacon/aggregation/communication/adapter/nostr.js.map +1 -1
- package/dist/esm/core/beacon/aggregation/coordinator.js +40 -44
- package/dist/esm/core/beacon/aggregation/coordinator.js.map +1 -1
- package/dist/esm/core/beacon/aggregation/participant.js +35 -38
- package/dist/esm/core/beacon/aggregation/participant.js.map +1 -1
- package/dist/esm/core/beacon/aggregation/session/index.js +3 -4
- package/dist/esm/core/beacon/aggregation/session/index.js.map +1 -1
- package/dist/esm/core/beacon/beacon.js.map +1 -1
- package/dist/esm/core/beacon/cas-beacon.js +119 -7
- package/dist/esm/core/beacon/cas-beacon.js.map +1 -1
- package/dist/esm/core/beacon/factory.js +1 -1
- package/dist/esm/core/beacon/factory.js.map +1 -1
- package/dist/esm/core/beacon/{singleton.js → singleton-beacon.js} +19 -27
- package/dist/esm/core/beacon/singleton-beacon.js.map +1 -0
- package/dist/esm/core/beacon/smt-beacon.js +1 -1
- package/dist/esm/core/beacon/smt-beacon.js.map +1 -1
- package/dist/esm/core/identifier.js +1 -1
- package/dist/esm/core/identifier.js.map +1 -1
- package/dist/{cjs/core/resolve.js → esm/core/resolver.js} +244 -92
- package/dist/esm/core/resolver.js.map +1 -0
- package/dist/esm/core/update.js +7 -7
- package/dist/esm/core/update.js.map +1 -1
- package/dist/esm/did-btcr2.js +34 -94
- package/dist/esm/did-btcr2.js.map +1 -1
- package/dist/esm/index.js +2 -3
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/utils/did-document.js +9 -19
- package/dist/esm/utils/did-document.js.map +1 -1
- package/dist/types/core/beacon/aggregation/cohort/index.d.ts +1 -0
- package/dist/types/core/beacon/aggregation/cohort/messages/base.d.ts +1 -0
- package/dist/types/core/beacon/aggregation/cohort/messages/constants.d.ts +1 -0
- package/dist/types/core/beacon/aggregation/cohort/messages/index.d.ts +1 -0
- package/dist/types/core/beacon/aggregation/cohort/messages/keygen/cohort-advert.d.ts +1 -0
- package/dist/types/core/beacon/aggregation/cohort/messages/keygen/cohort-ready.d.ts +2 -2
- package/dist/types/core/beacon/aggregation/cohort/messages/keygen/cohort-ready.d.ts.map +1 -1
- package/dist/types/core/beacon/aggregation/cohort/messages/keygen/opt-in-accept.d.ts +1 -0
- package/dist/types/core/beacon/aggregation/cohort/messages/keygen/opt-in.d.ts +1 -0
- package/dist/types/core/beacon/aggregation/cohort/messages/keygen/subscribe.d.ts +1 -0
- package/dist/types/core/beacon/aggregation/cohort/messages/sign/aggregated-nonce.d.ts +1 -0
- package/dist/types/core/beacon/aggregation/cohort/messages/sign/authorization-request.d.ts +1 -0
- package/dist/types/core/beacon/aggregation/cohort/messages/sign/nonce-contribution.d.ts +1 -0
- package/dist/types/core/beacon/aggregation/cohort/messages/sign/request-signature.d.ts +1 -0
- package/dist/types/core/beacon/aggregation/cohort/messages/sign/signature-authorization.d.ts +1 -0
- package/dist/types/core/beacon/aggregation/cohort/status.d.ts +1 -0
- package/dist/types/core/beacon/aggregation/communication/adapter/did-comm.d.ts +4 -3
- package/dist/types/core/beacon/aggregation/communication/adapter/did-comm.d.ts.map +1 -1
- package/dist/types/core/beacon/aggregation/communication/adapter/nostr.d.ts +5 -3
- package/dist/types/core/beacon/aggregation/communication/adapter/nostr.d.ts.map +1 -1
- package/dist/types/core/beacon/aggregation/communication/error.d.ts +1 -0
- package/dist/types/core/beacon/aggregation/communication/factory.d.ts +1 -0
- package/dist/types/core/beacon/aggregation/communication/service.d.ts +3 -2
- package/dist/types/core/beacon/aggregation/communication/service.d.ts.map +1 -1
- package/dist/types/core/beacon/aggregation/coordinator.d.ts +1 -0
- package/dist/types/core/beacon/aggregation/coordinator.d.ts.map +1 -1
- package/dist/types/core/beacon/aggregation/participant.d.ts +1 -0
- package/dist/types/core/beacon/aggregation/participant.d.ts.map +1 -1
- package/dist/types/core/beacon/aggregation/session/index.d.ts +1 -0
- package/dist/types/core/beacon/aggregation/session/index.d.ts.map +1 -1
- package/dist/types/core/beacon/aggregation/session/status.d.ts +1 -0
- package/dist/types/core/beacon/beacon.d.ts +10 -4
- package/dist/types/core/beacon/beacon.d.ts.map +1 -1
- package/dist/types/core/beacon/cas-beacon.d.ts +27 -7
- package/dist/types/core/beacon/cas-beacon.d.ts.map +1 -1
- package/dist/types/core/beacon/error.d.ts +1 -0
- package/dist/types/core/beacon/factory.d.ts +1 -0
- package/dist/types/core/beacon/interfaces.d.ts +1 -0
- package/dist/types/core/beacon/signal-discovery.d.ts +1 -0
- package/dist/types/core/beacon/{singleton.d.ts → singleton-beacon.d.ts} +7 -5
- package/dist/types/core/beacon/singleton-beacon.d.ts.map +1 -0
- package/dist/types/core/beacon/smt-beacon.d.ts +5 -3
- package/dist/types/core/beacon/smt-beacon.d.ts.map +1 -1
- package/dist/types/core/beacon/utils.d.ts +1 -0
- package/dist/types/core/identifier.d.ts +1 -0
- package/dist/types/core/interfaces.d.ts +6 -15
- package/dist/types/core/interfaces.d.ts.map +1 -1
- package/dist/types/core/resolver.d.ts +167 -0
- package/dist/types/core/resolver.d.ts.map +1 -0
- package/dist/types/core/types.d.ts +1 -0
- package/dist/types/core/update.d.ts +4 -3
- package/dist/types/core/update.d.ts.map +1 -1
- package/dist/types/did-btcr2.d.ts +17 -16
- package/dist/types/did-btcr2.d.ts.map +1 -1
- package/dist/types/index.d.ts +3 -3
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/utils/appendix.d.ts +1 -0
- package/dist/types/utils/did-document-builder.d.ts +1 -0
- package/dist/types/utils/did-document.d.ts +2 -6
- package/dist/types/utils/did-document.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/core/beacon/aggregation/cohort/messages/keygen/cohort-ready.ts +1 -1
- package/src/core/beacon/aggregation/communication/adapter/did-comm.ts +4 -3
- package/src/core/beacon/aggregation/communication/adapter/nostr.ts +4 -3
- package/src/core/beacon/aggregation/communication/service.ts +2 -2
- package/src/core/beacon/aggregation/coordinator.ts +40 -44
- package/src/core/beacon/aggregation/participant.ts +38 -40
- package/src/core/beacon/aggregation/session/index.ts +3 -4
- package/src/core/beacon/beacon.ts +9 -5
- package/src/core/beacon/cas-beacon.ts +156 -10
- package/src/core/beacon/factory.ts +1 -1
- package/src/core/beacon/{singleton.ts → singleton-beacon.ts} +20 -36
- package/src/core/beacon/smt-beacon.ts +4 -3
- package/src/core/identifier.ts +1 -1
- package/src/core/interfaces.ts +5 -16
- package/src/core/resolver.ts +706 -0
- package/src/core/update.ts +9 -9
- package/src/did-btcr2.ts +37 -130
- package/src/index.ts +2 -3
- package/src/utils/did-document.ts +10 -18
- package/dist/cjs/core/beacon/singleton.js.map +0 -1
- package/dist/cjs/core/resolve.js.map +0 -1
- package/dist/cjs/utils/general.js +0 -195
- package/dist/cjs/utils/general.js.map +0 -1
- package/dist/esm/core/beacon/singleton.js.map +0 -1
- package/dist/esm/core/resolve.js.map +0 -1
- package/dist/esm/utils/general.js +0 -195
- package/dist/esm/utils/general.js.map +0 -1
- package/dist/types/core/beacon/singleton.d.ts.map +0 -1
- package/dist/types/core/resolve.d.ts +0 -92
- package/dist/types/core/resolve.d.ts.map +0 -1
- package/dist/types/utils/general.d.ts +0 -85
- package/dist/types/utils/general.d.ts.map +0 -1
- package/src/core/resolve.ts +0 -474
- package/src/utils/general.ts +0 -204
package/src/core/update.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
2
|
+
canonicalHash,
|
|
3
3
|
INVALID_DID_UPDATE,
|
|
4
4
|
JSONPatch,
|
|
5
5
|
KeyBytes,
|
|
@@ -37,14 +37,14 @@ export class Update {
|
|
|
37
37
|
* @param {Btcr2DidDocument} sourceDocument The source DID document to be updated.
|
|
38
38
|
* @param {PatchOperation[]} patches The array of JSON Patch operations to apply to the sourceDocument.
|
|
39
39
|
* @param {number} sourceVersionId The version ID of the source document.
|
|
40
|
-
* @returns {
|
|
40
|
+
* @returns {UnsignedBTCR2Update} The constructed UnsignedBTCR2Update object.
|
|
41
41
|
* @throws {UpdateError} InvalidDid if sourceDocument.id does not match identifier.
|
|
42
42
|
*/
|
|
43
|
-
static
|
|
43
|
+
static construct(
|
|
44
44
|
sourceDocument: Btcr2DidDocument,
|
|
45
45
|
patches: PatchOperation[],
|
|
46
46
|
sourceVersionId: number,
|
|
47
|
-
):
|
|
47
|
+
): UnsignedBTCR2Update {
|
|
48
48
|
// Initialize an unsigned update conformant to btcr2 spec.
|
|
49
49
|
const unsignedUpdate: UnsignedBTCR2Update = {
|
|
50
50
|
'@context' : [
|
|
@@ -56,7 +56,7 @@ export class Update {
|
|
|
56
56
|
patch : patches,
|
|
57
57
|
targetHash : '',
|
|
58
58
|
targetVersionId : sourceVersionId + 1,
|
|
59
|
-
sourceHash :
|
|
59
|
+
sourceHash : canonicalHash(sourceDocument),
|
|
60
60
|
};
|
|
61
61
|
|
|
62
62
|
// Apply all JSON patches to sourceDocument.
|
|
@@ -73,8 +73,8 @@ export class Update {
|
|
|
73
73
|
);
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
// Set the targetHash by canonicalizing the targetDocument and encoding it in
|
|
77
|
-
unsignedUpdate.targetHash =
|
|
76
|
+
// Set the targetHash by canonicalizing the targetDocument and encoding it in base64url.
|
|
77
|
+
unsignedUpdate.targetHash = canonicalHash(targetDocument);
|
|
78
78
|
|
|
79
79
|
// Return unsignedUpdate.
|
|
80
80
|
return unsignedUpdate;
|
|
@@ -90,12 +90,12 @@ export class Update {
|
|
|
90
90
|
* @returns {SignedBTCR2Update} Did update payload secured with a proof => SignedBTCR2Update
|
|
91
91
|
* @throws {UpdateError} if the privateKeyBytes are invalid
|
|
92
92
|
*/
|
|
93
|
-
static
|
|
93
|
+
static sign(
|
|
94
94
|
did: string,
|
|
95
95
|
unsignedUpdate: UnsignedBTCR2Update,
|
|
96
96
|
verificationMethod: DidVerificationMethod,
|
|
97
97
|
secretKey: KeyBytes,
|
|
98
|
-
):
|
|
98
|
+
): SignedBTCR2Update {
|
|
99
99
|
// Parse the controller from the verificationMethod
|
|
100
100
|
const controller = verificationMethod.controller;
|
|
101
101
|
// Parse the fragment from the vmId
|
package/src/did-btcr2.ts
CHANGED
|
@@ -8,10 +8,7 @@ import {
|
|
|
8
8
|
KeyBytes,
|
|
9
9
|
METHOD_NOT_SUPPORTED,
|
|
10
10
|
MethodError,
|
|
11
|
-
MISSING_RESOLUTION_OPTIONS,
|
|
12
|
-
MISSING_UPDATE_DATA,
|
|
13
11
|
PatchOperation,
|
|
14
|
-
ResolveError,
|
|
15
12
|
UpdateError
|
|
16
13
|
} from '@did-btcr2/common';
|
|
17
14
|
import { SignedBTCR2Update } from '@did-btcr2/cryptosuite';
|
|
@@ -20,22 +17,17 @@ import {
|
|
|
20
17
|
DidError,
|
|
21
18
|
DidErrorCode,
|
|
22
19
|
DidMethod,
|
|
23
|
-
DidResolutionResult,
|
|
24
|
-
EMPTY_DID_RESOLUTION_RESULT
|
|
25
20
|
} from '@web5/dids';
|
|
26
21
|
import { initEccLib } from 'bitcoinjs-lib';
|
|
27
22
|
import * as tinysecp from 'tiny-secp256k1';
|
|
28
23
|
import { BeaconService } from './core/beacon/interfaces.js';
|
|
29
|
-
import { BeaconUtils } from './core/beacon/utils.js';
|
|
30
24
|
import { Identifier } from './core/identifier.js';
|
|
31
25
|
import { ResolutionOptions } from './core/interfaces.js';
|
|
32
|
-
import {
|
|
26
|
+
import { Resolver } from './core/resolver.js';
|
|
33
27
|
import { Update } from './core/update.js';
|
|
34
28
|
import { Appendix } from './utils/appendix.js';
|
|
35
29
|
import { Btcr2DidDocument, DidVerificationMethod } from './utils/did-document.js';
|
|
36
30
|
|
|
37
|
-
export type Btcr2Identifier = string;
|
|
38
|
-
|
|
39
31
|
export interface DidCreateOptions {
|
|
40
32
|
/** Type of identifier to create (key or external) */
|
|
41
33
|
idType: string;
|
|
@@ -45,7 +37,6 @@ export interface DidCreateOptions {
|
|
|
45
37
|
network?: string;
|
|
46
38
|
}
|
|
47
39
|
|
|
48
|
-
// TODO: convert to API driver?
|
|
49
40
|
/** Initialize tiny secp256k1 */
|
|
50
41
|
initEccLib(tinysecp);
|
|
51
42
|
|
|
@@ -76,7 +67,7 @@ export class DidBtcr2 implements DidMethod {
|
|
|
76
67
|
* @param {string} options.idType The type of identifier to create, either 'KEY' or 'EXTERNAL'. Defaults to 'KEY'.
|
|
77
68
|
* @param {number} options.version The version number of the did:btcr2 specification to use for creating the identifier. Defaults to 1.
|
|
78
69
|
* @param {string} options.network The Bitcoin network to use for the identifier, e.g. 'bitcoin', 'testnet', etc. Defaults to 'bitcoin'.
|
|
79
|
-
* @returns {Promise<
|
|
70
|
+
* @returns {Promise<string>} Promise resolving to an identifier string.
|
|
80
71
|
* @throws {MethodError} if any of the checks fail
|
|
81
72
|
* @example
|
|
82
73
|
* ```ts
|
|
@@ -84,7 +75,7 @@ export class DidBtcr2 implements DidMethod {
|
|
|
84
75
|
* const did = DidBtcr2.create(genesisBytes, { idType: 'KEY', network: 'regtest' });
|
|
85
76
|
* ```
|
|
86
77
|
*/
|
|
87
|
-
static create(genesisBytes: KeyBytes | DocumentBytes, options?: DidCreateOptions):
|
|
78
|
+
static create(genesisBytes: KeyBytes | DocumentBytes, options?: DidCreateOptions): string {
|
|
88
79
|
// Deconstruct the idType, version and network from the options, setting defaults if not given
|
|
89
80
|
const { idType, version = 1, network = 'bitcoin' } = options || {};
|
|
90
81
|
|
|
@@ -101,131 +92,48 @@ export class DidBtcr2 implements DidMethod {
|
|
|
101
92
|
|
|
102
93
|
/**
|
|
103
94
|
* Entry point for section {@link https://dcdpr.github.io/did-btcr2/operations/resolve.html | 7.2 Resolve}.
|
|
104
|
-
* See specification for the {@link https://dcdpr.github.io/did-btcr2/operations/resolve.html#process | Resolve Process}.
|
|
105
|
-
* See {@link Resolve | Resolve (class)} for class implementation.
|
|
106
95
|
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
* deterministically created from the DID or provided by Sidecar Data.
|
|
96
|
+
* Factory method that performs pure setup and returns a {@link Resolver} state machine.
|
|
97
|
+
* The caller drives resolution by calling `resolver.resolve()` and `resolver.provide()`.
|
|
98
|
+
* Analogous to Rust's `Document::read()`.
|
|
111
99
|
*
|
|
112
100
|
* @param {string} did The did:btcr2 identifier to be resolved.
|
|
113
101
|
* @param {ResolutionOptions} resolutionOptions Options used during the resolution process.
|
|
114
|
-
* @returns {
|
|
115
|
-
* @throws {ResolveError} If the resolution process fails at any step.
|
|
102
|
+
* @returns {Resolver} A sans-I/O state machine the caller drives to completion.
|
|
116
103
|
* @example
|
|
117
104
|
* ```ts
|
|
118
|
-
* const
|
|
119
|
-
*
|
|
120
|
-
* )
|
|
105
|
+
* const resolver = DidBtcr2.resolve(did, { sidecar });
|
|
106
|
+
* let state = resolver.resolve();
|
|
107
|
+
* while (state.status === 'action-required') {
|
|
108
|
+
* for (const need of state.needs) { ... provide data ... }
|
|
109
|
+
* state = resolver.resolve();
|
|
110
|
+
* }
|
|
111
|
+
* const { didDocument, metadata } = state.result;
|
|
121
112
|
* ```
|
|
122
113
|
*/
|
|
123
|
-
static
|
|
114
|
+
static resolve(
|
|
124
115
|
did: string,
|
|
125
116
|
resolutionOptions: ResolutionOptions = {}
|
|
126
|
-
):
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
// Process sidecar if provided
|
|
148
|
-
const sidecarData = Resolve.sidecarData(resolutionOptions.sidecar);
|
|
149
|
-
|
|
150
|
-
// Parse the genesis document from the resolution options if provided
|
|
151
|
-
const genesisDocument = resolutionOptions.sidecar?.genesisDocument;
|
|
152
|
-
|
|
153
|
-
// Since genesisDocument is optional, check if it exists
|
|
154
|
-
if(!genesisDocument) {
|
|
155
|
-
// If no genesisDocument and x HRP, throw MISSING_UPDATE_DATA error
|
|
156
|
-
if(didComponents.hrp === IdentifierHrp.x)
|
|
157
|
-
throw new ResolveError(
|
|
158
|
-
'External resolution requires genesisDocument',
|
|
159
|
-
MISSING_UPDATE_DATA, resolutionOptions
|
|
160
|
-
);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
// Establish the current document
|
|
164
|
-
const currentDocument = await Resolve.currentDocument(didComponents, genesisDocument);
|
|
165
|
-
|
|
166
|
-
// Extract all Beacon services from the current DID Document
|
|
167
|
-
const beaconServices = currentDocument.service
|
|
168
|
-
.filter(BeaconUtils.isBeaconService)
|
|
169
|
-
.map(BeaconUtils.parseBeaconServiceEndpoint);
|
|
170
|
-
|
|
171
|
-
// Check if bitcoin driver provided
|
|
172
|
-
if(!resolutionOptions?.drivers?.bitcoin) {
|
|
173
|
-
throw new ResolveError(
|
|
174
|
-
'Bitcoin connection required for resolve. Pass a configured driver via '
|
|
175
|
-
+ 'resolutionOptions.drivers.bitcoin or use DidBtcr2Api which injects it automatically.',
|
|
176
|
-
MISSING_RESOLUTION_OPTIONS, resolutionOptions
|
|
177
|
-
);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
// Get the bitcoin driver from the resolution options
|
|
181
|
-
const bitcoin = resolutionOptions.drivers.bitcoin;
|
|
182
|
-
|
|
183
|
-
// Process the Beacon Signals to get the required updates
|
|
184
|
-
const unsortedUpdates = await Resolve.beaconSignals(
|
|
185
|
-
beaconServices,
|
|
186
|
-
sidecarData,
|
|
187
|
-
bitcoin
|
|
188
|
-
);
|
|
189
|
-
|
|
190
|
-
// If no updates found, return the current document
|
|
191
|
-
if(!unsortedUpdates.length) {
|
|
192
|
-
// Set the didDocument in didResolutionResult based on currentDocument
|
|
193
|
-
didResolutionResult.didDocument = currentDocument;
|
|
194
|
-
// Set other required fields in the didResolutionResult
|
|
195
|
-
didResolutionResult.didDocumentMetadata.deactivated = !!currentDocument.deactivated;
|
|
196
|
-
didResolutionResult.didDocumentMetadata.versionId = versionId ?? '1';
|
|
197
|
-
|
|
198
|
-
// Return the didResolutionResult early
|
|
199
|
-
return didResolutionResult;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
// Process the updates to apply updates to bring the current DID Document to its more current state
|
|
203
|
-
const result = await Resolve.updates(
|
|
204
|
-
currentDocument,
|
|
205
|
-
unsortedUpdates,
|
|
206
|
-
resolutionOptions.versionTime,
|
|
207
|
-
versionId
|
|
208
|
-
);
|
|
209
|
-
|
|
210
|
-
// Set all of the required fields in the didResolutionResult
|
|
211
|
-
didResolutionResult.didDocument = result.didDocument;
|
|
212
|
-
didResolutionResult.didDocumentMetadata = result.metadata;
|
|
213
|
-
|
|
214
|
-
// Return didResolutionResult;
|
|
215
|
-
return didResolutionResult;
|
|
216
|
-
} catch (error: any) {
|
|
217
|
-
// Rethrow any unexpected errors that are not a `ResolveError`.
|
|
218
|
-
if (!(error instanceof ResolveError)) throw new Error(error.message ?? error, { cause: error });
|
|
219
|
-
|
|
220
|
-
// Return a DID Resolution Result with the appropriate error code.
|
|
221
|
-
return {
|
|
222
|
-
...EMPTY_DID_RESOLUTION_RESULT,
|
|
223
|
-
didResolutionMetadata : {
|
|
224
|
-
error : error.type,
|
|
225
|
-
...error.message && { errorMessage: error.message }
|
|
226
|
-
}
|
|
227
|
-
};
|
|
228
|
-
}
|
|
117
|
+
): Resolver {
|
|
118
|
+
// Decode the did to be resolved
|
|
119
|
+
const didComponents = Identifier.decode(did);
|
|
120
|
+
|
|
121
|
+
// Process sidecar if provided
|
|
122
|
+
const sidecarData = Resolver.sidecarData(resolutionOptions.sidecar);
|
|
123
|
+
|
|
124
|
+
// Establish the current document for KEY identifiers (pure, synchronous).
|
|
125
|
+
// For EXTERNAL identifiers, defer to the Resolver's GenesisDocument phase
|
|
126
|
+
// since validation (Resolve.external) is async.
|
|
127
|
+
const currentDocument = didComponents.hrp === IdentifierHrp.k
|
|
128
|
+
? Resolver.deterministic(didComponents)
|
|
129
|
+
: null;
|
|
130
|
+
|
|
131
|
+
// Return the sans-I/O state machine
|
|
132
|
+
return new Resolver(didComponents, sidecarData, currentDocument, {
|
|
133
|
+
versionId : resolutionOptions.versionId,
|
|
134
|
+
versionTime : resolutionOptions.versionTime,
|
|
135
|
+
genesisDocument : resolutionOptions.sidecar?.genesisDocument
|
|
136
|
+
});
|
|
229
137
|
}
|
|
230
138
|
|
|
231
139
|
/**
|
|
@@ -265,7 +173,6 @@ export class DidBtcr2 implements DidMethod {
|
|
|
265
173
|
signingMaterial?: KeyBytes | HexString;
|
|
266
174
|
bitcoin?: BitcoinConnection;
|
|
267
175
|
}): Promise<SignedBTCR2Update> {
|
|
268
|
-
// TODO: provide KMS as alternative
|
|
269
176
|
// If no signingMaterial provided, throw an UpdateError with INVALID_DID_UPDATE.
|
|
270
177
|
if (!signingMaterial) {
|
|
271
178
|
throw new UpdateError(
|
|
@@ -315,10 +222,10 @@ export class DidBtcr2 implements DidMethod {
|
|
|
315
222
|
}
|
|
316
223
|
|
|
317
224
|
// Construct an unsigned update following the BTCR2 Update construction algorithm
|
|
318
|
-
const update =
|
|
225
|
+
const update = Update.construct(sourceDocument, patches, sourceVersionId);
|
|
319
226
|
|
|
320
227
|
// Sign the unsigned update using the specified verification method
|
|
321
|
-
const signed =
|
|
228
|
+
const signed = Update.sign(sourceDocument.id, update, verificationMethod, secretKey);
|
|
322
229
|
|
|
323
230
|
// Filter sourceDocument services to get beaconServices matching beaconIds
|
|
324
231
|
const beaconService = sourceDocument.service
|
package/src/index.ts
CHANGED
|
@@ -33,19 +33,18 @@ export * from './core/beacon/error.js';
|
|
|
33
33
|
export * from './core/beacon/factory.js';
|
|
34
34
|
export * from './core/beacon/interfaces.js';
|
|
35
35
|
export * from './core/beacon/signal-discovery.js';
|
|
36
|
-
export * from './core/beacon/singleton.js';
|
|
36
|
+
export * from './core/beacon/singleton-beacon.js';
|
|
37
37
|
export * from './core/beacon/smt-beacon.js';
|
|
38
38
|
export * from './core/beacon/utils.js';
|
|
39
39
|
|
|
40
40
|
export * from './core/identifier.js';
|
|
41
41
|
export * from './core/interfaces.js';
|
|
42
|
-
export * from './core/
|
|
42
|
+
export * from './core/resolver.js';
|
|
43
43
|
export * from './core/types.js';
|
|
44
44
|
export * from './core/update.js';
|
|
45
45
|
|
|
46
46
|
export * from './utils/appendix.js';
|
|
47
47
|
export * from './utils/did-document-builder.js';
|
|
48
48
|
export * from './utils/did-document.js';
|
|
49
|
-
export * from './utils/general.js';
|
|
50
49
|
|
|
51
50
|
export * from './did-btcr2.js';
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { getNetwork } from '@did-btcr2/bitcoin';
|
|
2
2
|
import {
|
|
3
|
-
|
|
4
|
-
Canonicalization,
|
|
3
|
+
canonicalize,
|
|
5
4
|
DidDocumentError,
|
|
5
|
+
hash,
|
|
6
6
|
HashBytes,
|
|
7
7
|
IdentifierTypes,
|
|
8
8
|
INVALID_DID_DOCUMENT,
|
|
@@ -18,6 +18,10 @@ import { BeaconUtils } from '../core/beacon/utils.js';
|
|
|
18
18
|
import { Identifier } from '../core/identifier.js';
|
|
19
19
|
import { Appendix } from './appendix.js';
|
|
20
20
|
|
|
21
|
+
export const BTCR2_DID_DOCUMENT_CONTEXT = [
|
|
22
|
+
'https://www.w3.org/ns/did/v1.1',
|
|
23
|
+
'https://btcr2.dev/context/v1',
|
|
24
|
+
];
|
|
21
25
|
export const ID_PLACEHOLDER_VALUE = 'did:btcr2:_';
|
|
22
26
|
export const BECH32M_CHARS = '';
|
|
23
27
|
export const DID_REGEX = /did:btcr2:(x1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]*)/g;
|
|
@@ -120,8 +124,8 @@ export interface Btcr2DidDocument extends W3CDidDocument {
|
|
|
120
124
|
export class DidDocument implements Btcr2DidDocument {
|
|
121
125
|
id: string;
|
|
122
126
|
'@context'?: Array<string | JSONObject> = [
|
|
123
|
-
'https://www.w3.org/
|
|
124
|
-
'https://btcr2.dev/context/v1'
|
|
127
|
+
'https://www.w3.org/ns/did/v1.1',
|
|
128
|
+
'https://btcr2.dev/context/v1',
|
|
125
129
|
];
|
|
126
130
|
verificationMethod: Array<DidVerificationMethod>;
|
|
127
131
|
authentication?: Array<string | DidVerificationMethod>;
|
|
@@ -166,7 +170,7 @@ export class DidDocument implements Btcr2DidDocument {
|
|
|
166
170
|
this.verificationMethod = document.verificationMethod || [];
|
|
167
171
|
this.service = document.service || [];
|
|
168
172
|
this['@context'] = document['@context'] || [
|
|
169
|
-
'https://www.w3.org/
|
|
173
|
+
'https://www.w3.org/ns/did/v1.1',
|
|
170
174
|
'https://btcr2.dev/context/v1'
|
|
171
175
|
];
|
|
172
176
|
|
|
@@ -321,18 +325,6 @@ export class DidDocument implements Btcr2DidDocument {
|
|
|
321
325
|
}
|
|
322
326
|
}
|
|
323
327
|
|
|
324
|
-
/**
|
|
325
|
-
* Validates that the controller exists and is correctly formatted.
|
|
326
|
-
* @param {Array<string>} controller The controller to validate.
|
|
327
|
-
* @returns {boolean} True if the controller is valid.
|
|
328
|
-
*/
|
|
329
|
-
private static isValidController(controller: Array<string>): boolean {
|
|
330
|
-
if(!controller) return false;
|
|
331
|
-
if(!Array.isArray(controller)) return false;
|
|
332
|
-
if(!controller.every(c => typeof c === 'string')) return false;
|
|
333
|
-
return true;
|
|
334
|
-
}
|
|
335
|
-
|
|
336
328
|
/**
|
|
337
329
|
* Validates that verification methods exist and are correctly formatted.
|
|
338
330
|
* @private
|
|
@@ -537,6 +529,6 @@ export class GenesisDocument extends DidDocument {
|
|
|
537
529
|
* @returns {Bytes} The genesis bytes.
|
|
538
530
|
*/
|
|
539
531
|
static toGenesisBytes(genesisDocument: GenesisDocumentLike): HashBytes {
|
|
540
|
-
return
|
|
532
|
+
return hash(canonicalize(genesisDocument));
|
|
541
533
|
}
|
|
542
534
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"singleton.js","sourceRoot":"","sources":["../../../../src/core/beacon/singleton.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,oBAAoB,EAAY,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAE1G,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AACtD,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AAEtD,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAGlD;;;;;GAKG;AACH,MAAM,OAAO,eAAgB,SAAQ,MAAM;IAEzC;;;;OAIG;IACH,YAAY,OAAsB;QAChC,KAAK,CAAC,EAAE,GAAG,OAAO,EAAE,IAAI,EAAE,iBAAiB,EAAE,CAAC,CAAC;IACjD,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,cAAc,CAClB,OAA4B,EAC5B,OAAoB;QAEpB,6DAA6D;QAC7D,MAAM,OAAO,GAAG,IAAI,KAAK,EAAsC,CAAC;QAEhE,sCAAsC;QACtC,KAAI,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;YAC5B,oDAAoD;YACpD,MAAM,UAAU,GAAG,MAAM,CAAC,WAAW,CAAC;YAEtC,iFAAiF;YACjF,MAAM,YAAY,GAAG,OAAO,CAAC,SAAS,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;YAEvD,mFAAmF;YACnF,IAAG,CAAC,YAAY,EAAE,CAAC;gBACjB,MAAM,IAAI,oBAAoB,CAC5B,iDAAiD,UAAU,GAAG,EAC9D,mBAAmB,EAAE,MAAM,CAC5B,CAAC;YACJ,CAAC;YAED,8FAA8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YAAY,CACpB,oCAAoC,EACpC,kBAAkB,EAAE,kBAAkB,CACvC,CAAC;QACJ,CAAC;QAED,4EAA4E;QAC5E,MAAM,eAAe,GAAG,SAAS,CAAC,KAAK,CAAC,eAAe,EAAE,MAAM,CAAC,KAAK,CAAgB,CAAC;QAEtF,8DAA8D;QAC9D,WAAW,CAAC,QAAQ,CAAC,eAAe,CAAC,CAAC;QAEtC,4EAA4E;QAC5E,MAAM,mBAAmB,GAAG,gBAAgB,CAAC,OAAO,CAAC,eAAe,EAAE,EAAE,QAAQ,EAAE,WAAW,EAAE,CAAC,CAAC;QAEjG,yEAAyE;QACzE,MAAM,gBAAgB,GAAG,MAAM,CAAC,UAAU,CAAC;QAE3C,8DAA8D;QAC9D,IAAI,MAAM,CAAC,UAAU,KAAK,mBAAmB,EAAE,CAAC;YAC9C,wDAAwD;YACxD,MAAM,IAAI,YAAY,CACpB,2DAA2D,EAC3D,kBAAkB,EAAE,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,CAC9D,CAAC;QACJ,CAAC;QAED,iCAAiC;QACjC,OAAO,eAAe,CAAC;IACzB,CAAC;CACF"}
|
|
@@ -1,195 +0,0 @@
|
|
|
1
|
-
import { BIP340_PUBLIC_KEY_MULTIBASE_PREFIX } from '@did-btcr2/common';
|
|
2
|
-
import { sha256 } from '@noble/hashes/sha2';
|
|
3
|
-
import { CURVE, getPublicKey, utils } from '@noble/secp256k1';
|
|
4
|
-
import { HDKey } from '@scure/bip32';
|
|
5
|
-
import { generateMnemonic, mnemonicToSeed } from '@scure/bip39';
|
|
6
|
-
import { wordlist } from '@scure/bip39/wordlists/english';
|
|
7
|
-
import { base58btc } from 'multiformats/bases/base58';
|
|
8
|
-
/**
|
|
9
|
-
* Static class of general utility functions for the did-btcr2 spec implementation
|
|
10
|
-
* @class GeneralUtils
|
|
11
|
-
* @type {GeneralUtils}
|
|
12
|
-
*/
|
|
13
|
-
export class GeneralUtils {
|
|
14
|
-
/**
|
|
15
|
-
* Helper function to encode a secp256k1 key in SchnorrSecp256k1 Multikey Format
|
|
16
|
-
* @param {KeyBytes} xOnlyKeyBytes
|
|
17
|
-
* @returns {PublicKeyMultibase}
|
|
18
|
-
*/
|
|
19
|
-
static encode(xOnlyKeyBytes) {
|
|
20
|
-
if (xOnlyKeyBytes.length !== 32) {
|
|
21
|
-
throw new Error('x-only public key must be 32 bytes');
|
|
22
|
-
}
|
|
23
|
-
const prefix = Array.from(BIP340_PUBLIC_KEY_MULTIBASE_PREFIX);
|
|
24
|
-
const x = Array.from(xOnlyKeyBytes);
|
|
25
|
-
// Set the prefix and the public key bytes
|
|
26
|
-
const multikeyBytes = new Uint8Array([...prefix, ...x]);
|
|
27
|
-
// Encode the public key as a multibase base58btc string
|
|
28
|
-
return base58btc.encode(multikeyBytes);
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Converts a bigint to a buffer
|
|
32
|
-
* @param {bigint} value The bigint to convert
|
|
33
|
-
* @returns {Buffer} The buffer representation of the bigint
|
|
34
|
-
*/
|
|
35
|
-
static bigintToBuffer(value) {
|
|
36
|
-
const hex = value.toString(16).padStart(64, '0');
|
|
37
|
-
return Buffer.from(hex, 'hex');
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Generates a new mnemonic phrase and HD wallet
|
|
41
|
-
* @returns {HdWallet} Promise resolving to a new hdwallet object w/ mnemonic and hdkey
|
|
42
|
-
* @throws {Error} if the public key bytes cannot be derived
|
|
43
|
-
*/
|
|
44
|
-
static async generateHdWallet() {
|
|
45
|
-
// Generate random mnemonic phrase.
|
|
46
|
-
const mnemonic = generateMnemonic(wordlist, 128);
|
|
47
|
-
// Generate seed from random mnemonic phrase.
|
|
48
|
-
const seed = await mnemonicToSeed(mnemonic);
|
|
49
|
-
// Generate HDKey from seed.
|
|
50
|
-
const hdkey = HDKey.fromMasterSeed(seed);
|
|
51
|
-
// Ensure HDKey returns valid
|
|
52
|
-
if (!hdkey) {
|
|
53
|
-
throw new Error('Failed to derive hd wallet');
|
|
54
|
-
}
|
|
55
|
-
return { mnemonic, hdkey };
|
|
56
|
-
}
|
|
57
|
-
static generateCompressedSecp256k1KeyPair() {
|
|
58
|
-
const privateKey = utils.randomPrivateKey();
|
|
59
|
-
if (!utils.isValidPrivateKey(privateKey)) {
|
|
60
|
-
throw new Error('Invalid private key');
|
|
61
|
-
}
|
|
62
|
-
return { privateKey, publicKey: getPublicKey(privateKey, true) };
|
|
63
|
-
}
|
|
64
|
-
;
|
|
65
|
-
/**
|
|
66
|
-
* Recovers an HDKey from a mnemonic phrase
|
|
67
|
-
* @param {string} mnemonic The mnemonic phrase to recover the HDKey from
|
|
68
|
-
* @param {Uint8Array} seed Optional seed to recover the HDKey from
|
|
69
|
-
* @returns {HDKey} Promise resolving to the recovered HDKey
|
|
70
|
-
* @throws Error if the HDKey cannot be recovered
|
|
71
|
-
*/
|
|
72
|
-
static async recoverHdWallet(mnemonic, seed) {
|
|
73
|
-
seed ??= await mnemonicToSeed(mnemonic);
|
|
74
|
-
// Generate HDKey from seed.
|
|
75
|
-
const hdkey = HDKey.fromMasterSeed(seed);
|
|
76
|
-
// Ensure HDKey returns valid
|
|
77
|
-
if (!hdkey) {
|
|
78
|
-
throw new Error('Failed to recover hdkey');
|
|
79
|
-
}
|
|
80
|
-
// Return the HDKey
|
|
81
|
-
return hdkey;
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Recovers a secp256k1 privateKey from its original entropy
|
|
85
|
-
* @param {Uint8Array} xorEntropy The original entropy to recover the privateKey from
|
|
86
|
-
* @param {Uint8Array} salt The salt used to tweak the privateKey
|
|
87
|
-
* @returns {Uint8Array} The recovered privateKey
|
|
88
|
-
* @throws {Error} if the privateKey cannot be recovered
|
|
89
|
-
*/
|
|
90
|
-
static recoverTweakedRawPrivateKey(xorEntropy, salt) {
|
|
91
|
-
// If entropy is not 32 bytes, hash it to get a deterministic 32-byte private key
|
|
92
|
-
if (xorEntropy.length !== 32) {
|
|
93
|
-
xorEntropy = sha256(xorEntropy);
|
|
94
|
-
}
|
|
95
|
-
const entropy = this.XNOR(xorEntropy, salt);
|
|
96
|
-
// Convert entropy to hex
|
|
97
|
-
const hexEntropy = Buffer.from(entropy).toString('hex');
|
|
98
|
-
// Convert hexEntropy to BigInt
|
|
99
|
-
const privateKey = BigInt(`0x${hexEntropy}`);
|
|
100
|
-
// Ensure private key is in valid secp256k1 range1
|
|
101
|
-
if (privateKey < BigInt(1) || privateKey >= CURVE.n) {
|
|
102
|
-
throw new Error('Invalid private key derived from entropy');
|
|
103
|
-
}
|
|
104
|
-
// The valid 32-byte private key
|
|
105
|
-
return entropy;
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Recovers a secp256k1 privateKey from its original entropy
|
|
109
|
-
* @param {Uint8Array} entropy The entropy to recover the privateKey from
|
|
110
|
-
* @returns {Uint8Array} The recovered privateKey
|
|
111
|
-
* @throws {Error} if the privateKey cannot be recovered
|
|
112
|
-
*/
|
|
113
|
-
static recoverRawPrivateKey(entropy) {
|
|
114
|
-
// If entropy is not 32 bytes, hash it to get a deterministic 32-byte private key
|
|
115
|
-
if (entropy.length !== 32) {
|
|
116
|
-
entropy = sha256(entropy);
|
|
117
|
-
}
|
|
118
|
-
// Convert entropy to hex
|
|
119
|
-
const hexEntropy = Buffer.from(entropy).toString('hex');
|
|
120
|
-
// Convert hexEntropy to BigInt
|
|
121
|
-
const privateKey = BigInt(`0x${hexEntropy}`);
|
|
122
|
-
// Ensure private key is in valid secp256k1 range1
|
|
123
|
-
if (privateKey < BigInt(1) || privateKey >= CURVE.n) {
|
|
124
|
-
throw new Error('Invalid private key derived from entropy');
|
|
125
|
-
}
|
|
126
|
-
// The valid 32-byte private key
|
|
127
|
-
return entropy;
|
|
128
|
-
}
|
|
129
|
-
/**
|
|
130
|
-
* Tweak the entropy with a salt using XOR
|
|
131
|
-
* @param {Uint8Array} entropy The entropy to tweak
|
|
132
|
-
* @param {Uint8Array} salt The salt to tweak the entropy with
|
|
133
|
-
* @returns {Uint8Array} The tweaked entropy
|
|
134
|
-
*/
|
|
135
|
-
static XOR(entropy, salt) {
|
|
136
|
-
const tweaked = new Uint8Array(entropy.length);
|
|
137
|
-
for (let i = 0; i < entropy.length; i++) {
|
|
138
|
-
tweaked[i] = entropy[i] ^ salt[i % salt.length]; // XOR with repeating salt
|
|
139
|
-
}
|
|
140
|
-
return tweaked;
|
|
141
|
-
}
|
|
142
|
-
/**
|
|
143
|
-
* Untweak the entropy with a salt using XNOR
|
|
144
|
-
*
|
|
145
|
-
* @param {Uint8Array} tweakedEntropy The tweaked entropy to untweak
|
|
146
|
-
* @param {Uint8Array} salt The salt to untweak the entropy with
|
|
147
|
-
* @returns {Uint8Array} The original entropy
|
|
148
|
-
*/
|
|
149
|
-
static XNOR(tweakedEntropy, salt) {
|
|
150
|
-
const originalEntropy = new Uint8Array(tweakedEntropy.length);
|
|
151
|
-
for (let i = 0; i < tweakedEntropy.length; i++) {
|
|
152
|
-
originalEntropy[i] = tweakedEntropy[i] ^ salt[i % salt.length]; // XOR with salt again
|
|
153
|
-
}
|
|
154
|
-
return originalEntropy;
|
|
155
|
-
}
|
|
156
|
-
/**
|
|
157
|
-
* Recovers an HDKey from a mnemonic phrase
|
|
158
|
-
* @param {string} mnemonic The mnemonic phrase to recover the HDKey from
|
|
159
|
-
* @param {string} path The path to derive the child key from
|
|
160
|
-
* @returns {Uint8Array} Promise resolving to the recovered private key bytes
|
|
161
|
-
* @throws {Error} if the HDKey cannot be recovered
|
|
162
|
-
*/
|
|
163
|
-
static async recoverHdChildFromMnemonic(mnemonic, path) {
|
|
164
|
-
// Generate HDKey from seed.
|
|
165
|
-
const hdkey = await this.recoverHdWallet(mnemonic);
|
|
166
|
-
// Ensure HDKey returns valid
|
|
167
|
-
if (!hdkey) {
|
|
168
|
-
throw new Error('Failed to recover hdkey');
|
|
169
|
-
}
|
|
170
|
-
// Return the privateKey of the derived childKey
|
|
171
|
-
const childPrivKeyBytes = hdkey.derive(path).privateKey;
|
|
172
|
-
if (!childPrivKeyBytes) {
|
|
173
|
-
throw new Error('Failed to recover child private key');
|
|
174
|
-
}
|
|
175
|
-
return childPrivKeyBytes;
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
* Derives a child key from an HDKey
|
|
179
|
-
* @param {HDKey} hdkey The HDKey to derive the child key from
|
|
180
|
-
* @param {string} path The path to derive the child key from
|
|
181
|
-
* @returns {HDKey} A Promise resolving to the child key
|
|
182
|
-
* @throws {Error} Error if the child key cannot be derived
|
|
183
|
-
*/
|
|
184
|
-
static deriveChildKey(hdkey, path) {
|
|
185
|
-
// Derive child key from HDKey.
|
|
186
|
-
const childKey = hdkey.derive(path);
|
|
187
|
-
// Ensure child key returns valid
|
|
188
|
-
if (!childKey) {
|
|
189
|
-
throw new Error(`Failed to derive child key`);
|
|
190
|
-
}
|
|
191
|
-
// Return the child key
|
|
192
|
-
return childKey;
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
//# sourceMappingURL=general.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"general.js","sourceRoot":"","sources":["../../../src/utils/general.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,kCAAkC,EAAY,MAAM,mBAAmB,CAAC;AAC3F,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,KAAK,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAC9D,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AACrC,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAChE,OAAO,EAAE,QAAQ,EAAE,MAAM,gCAAgC,CAAC;AAC1D,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AAEtD;;;;GAIG;AACH,MAAM,OAAO,YAAY;IACvB;;;;OAIG;IACI,MAAM,CAAC,MAAM,CAAC,aAAuB;QAC1C,IAAI,aA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