@did-btcr2/method 0.23.0 → 0.24.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/README.md +96 -50
- package/dist/browser.js +35650 -36741
- package/dist/browser.mjs +35649 -36740
- 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/singleton.js +8 -8
- package/dist/cjs/core/beacon/singleton.js.map +1 -1
- package/dist/cjs/core/identifier.js +1 -1
- package/dist/cjs/core/identifier.js.map +1 -1
- package/dist/cjs/core/resolve.js +17 -13
- package/dist/cjs/core/resolve.js.map +1 -1
- package/dist/cjs/core/update.js +4 -4
- package/dist/cjs/core/update.js.map +1 -1
- package/dist/cjs/did-btcr2.js +0 -2
- package/dist/cjs/did-btcr2.js.map +1 -1
- package/dist/cjs/index.js +0 -1
- 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/singleton.js +8 -8
- package/dist/esm/core/beacon/singleton.js.map +1 -1
- package/dist/esm/core/identifier.js +1 -1
- package/dist/esm/core/identifier.js.map +1 -1
- package/dist/esm/core/resolve.js +17 -13
- package/dist/esm/core/resolve.js.map +1 -1
- package/dist/esm/core/update.js +4 -4
- package/dist/esm/core/update.js.map +1 -1
- package/dist/esm/did-btcr2.js +0 -2
- package/dist/esm/did-btcr2.js.map +1 -1
- package/dist/esm/index.js +0 -1
- 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 +1 -0
- package/dist/types/core/beacon/cas-beacon.d.ts +1 -0
- 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 +1 -1
- package/dist/types/core/beacon/singleton.d.ts.map +1 -1
- package/dist/types/core/beacon/smt-beacon.d.ts +1 -0
- 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 +1 -0
- package/dist/types/core/resolve.d.ts +1 -0
- package/dist/types/core/resolve.d.ts.map +1 -1
- package/dist/types/core/types.d.ts +1 -0
- package/dist/types/core/update.d.ts +1 -0
- package/dist/types/did-btcr2.d.ts +1 -0
- package/dist/types/did-btcr2.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -1
- 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/singleton.ts +8 -8
- package/src/core/identifier.ts +1 -1
- package/src/core/resolve.ts +21 -14
- package/src/core/update.ts +4 -4
- package/src/did-btcr2.ts +0 -2
- package/src/index.ts +0 -1
- package/src/utils/did-document.ts +10 -18
- package/dist/cjs/utils/general.js +0 -195
- package/dist/cjs/utils/general.js.map +0 -1
- package/dist/esm/utils/general.js +0 -195
- package/dist/esm/utils/general.js.map +0 -1
- package/dist/types/utils/general.d.ts +0 -85
- package/dist/types/utils/general.d.ts.map +0 -1
- package/src/utils/general.ts +0 -204
package/src/core/resolve.ts
CHANGED
|
@@ -3,7 +3,8 @@ import {
|
|
|
3
3
|
getNetwork
|
|
4
4
|
} from '@did-btcr2/bitcoin';
|
|
5
5
|
import {
|
|
6
|
-
|
|
6
|
+
canonicalHash,
|
|
7
|
+
canonicalize,
|
|
7
8
|
DateUtils,
|
|
8
9
|
IdentifierHrp,
|
|
9
10
|
INVALID_DID,
|
|
@@ -23,7 +24,7 @@ import {
|
|
|
23
24
|
UnsignedBTCR2Update
|
|
24
25
|
} from '@did-btcr2/cryptosuite';
|
|
25
26
|
import { CompressedSecp256k1PublicKey } from '@did-btcr2/keypair';
|
|
26
|
-
import {
|
|
27
|
+
import { hex } from '@scure/base';
|
|
27
28
|
import { DidBtcr2 } from '../did-btcr2.js';
|
|
28
29
|
import { Appendix } from '../utils/appendix.js';
|
|
29
30
|
import { DidDocument, ID_PLACEHOLDER_VALUE } from '../utils/did-document.js';
|
|
@@ -104,15 +105,19 @@ export class Resolve {
|
|
|
104
105
|
didComponents: DidComponents,
|
|
105
106
|
genesisDocument: object,
|
|
106
107
|
): Promise<DidDocument> {
|
|
107
|
-
//
|
|
108
|
-
const hashBytes = Canonicalization.process(genesisDocument, { encoding: 'hex' });
|
|
109
|
-
|
|
108
|
+
// Get the genesis bytes from the did components
|
|
110
109
|
const { genesisBytes } = didComponents;
|
|
111
110
|
|
|
111
|
+
// Convert the genesis bytes to a hex string
|
|
112
|
+
const genesisHex = hex.encode(genesisBytes);
|
|
113
|
+
|
|
114
|
+
// Canonicalize and sha256 hash the currentDocument
|
|
115
|
+
const hashBytes = canonicalHash(genesisDocument);
|
|
116
|
+
|
|
112
117
|
// If the genesisBytes do not match the hashBytes, throw an error
|
|
113
|
-
if (
|
|
118
|
+
if (genesisHex !== hashBytes) {
|
|
114
119
|
throw new ResolveError(
|
|
115
|
-
`Initial document mismatch: genesisBytes ${
|
|
120
|
+
`Initial document mismatch: genesisBytes ${genesisHex} !== hashBytes ${hashBytes}`,
|
|
116
121
|
INVALID_DID_DOCUMENT, { genesisBytes, hashBytes }
|
|
117
122
|
);
|
|
118
123
|
}
|
|
@@ -139,14 +144,14 @@ export class Resolve {
|
|
|
139
144
|
const updateMap = new Map<string, SignedBTCR2Update>();
|
|
140
145
|
if(sidecar.updates?.length)
|
|
141
146
|
for(const update of sidecar.updates) {
|
|
142
|
-
updateMap.set(
|
|
147
|
+
updateMap.set(canonicalHash(update, { encoding: 'hex' }), update);
|
|
143
148
|
}
|
|
144
149
|
|
|
145
150
|
// CAS Announcements map
|
|
146
151
|
const casMap = new Map<string, CASAnnouncement>();
|
|
147
152
|
if(sidecar.casUpdates?.length)
|
|
148
153
|
for(const update of sidecar.casUpdates) {
|
|
149
|
-
casMap.set(
|
|
154
|
+
casMap.set(canonicalHash(update, { encoding: 'hex' }), update);
|
|
150
155
|
}
|
|
151
156
|
|
|
152
157
|
// SMT Proofs map
|
|
@@ -275,11 +280,13 @@ export class Resolve {
|
|
|
275
280
|
// Iterate over each (update block) pair
|
|
276
281
|
for(const [update, block] of updates) {
|
|
277
282
|
// Get the hash of the current document
|
|
278
|
-
const currentDocumentHash =
|
|
283
|
+
const currentDocumentHash = canonicalHash(response.didDocument, { encoding: 'base64url' });
|
|
279
284
|
|
|
280
285
|
// Safely convert block.time to timestamp
|
|
281
286
|
const blocktime = DateUtils.blocktimeToTimestamp(block.time);
|
|
282
287
|
|
|
288
|
+
// TODO: How to detect if block is unconfirmed and exit gracefully or return without it
|
|
289
|
+
|
|
283
290
|
// Set the updated field to the blocktime of the current update
|
|
284
291
|
response.metadata.updated = DateUtils.toISOStringNonFractional(blocktime);
|
|
285
292
|
|
|
@@ -317,7 +324,7 @@ export class Resolve {
|
|
|
317
324
|
// Create unsigned_update by removing the proof property from update.
|
|
318
325
|
const unsignedUpdate = JSONUtils.deleteKeys(update, ['proof']) as UnsignedBTCR2Update;
|
|
319
326
|
// Push the canonicalized unsigned update hash to the updateHashHistory
|
|
320
|
-
updateHashHistory.push(
|
|
327
|
+
updateHashHistory.push(canonicalHash(unsignedUpdate, { encoding: 'base64url' }));
|
|
321
328
|
}
|
|
322
329
|
|
|
323
330
|
// If update.targetVersionId > currentVersionId + 1, throw LATE_PUBLISHING error
|
|
@@ -366,7 +373,7 @@ export class Resolve {
|
|
|
366
373
|
const { proof: _, ...unsignedUpdate } = update;
|
|
367
374
|
|
|
368
375
|
// Hash unsignedUpdate with JSON Document Hashing algorithm
|
|
369
|
-
const unsignedUpdateHash =
|
|
376
|
+
const unsignedUpdateHash = canonicalHash(unsignedUpdate);
|
|
370
377
|
|
|
371
378
|
// Let historicalUpdateHash equal updateHashHistory[updateHashIndex].
|
|
372
379
|
const historicalUpdateHash = updateHashHistory[update.targetVersionId - 2];
|
|
@@ -431,7 +438,7 @@ export class Resolve {
|
|
|
431
438
|
const cryptosuite = new BIP340Cryptosuite(multikey);
|
|
432
439
|
|
|
433
440
|
// Canonicalize the update
|
|
434
|
-
const canonicalUpdate =
|
|
441
|
+
const canonicalUpdate = canonicalize(update);
|
|
435
442
|
|
|
436
443
|
// Construct a DataIntegrityProof with the cryptosuite
|
|
437
444
|
const diProof = new BIP340DataIntegrityProof(cryptosuite);
|
|
@@ -454,7 +461,7 @@ export class Resolve {
|
|
|
454
461
|
DidDocument.validate(updatedDocument);
|
|
455
462
|
|
|
456
463
|
// Canonicalize and hash the updatedDocument to get the currentDocumentHash.
|
|
457
|
-
const currentDocumentHash =
|
|
464
|
+
const currentDocumentHash = canonicalHash(updatedDocument, { encoding: 'base64url' });
|
|
458
465
|
|
|
459
466
|
// Prepare the update targetHash for comparison with currentDocumentHash.
|
|
460
467
|
const updateTargetHash = update.targetHash;
|
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,
|
|
@@ -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, { encoding: 'base64url' }),
|
|
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, { encoding: 'base64url' });
|
|
78
78
|
|
|
79
79
|
// Return unsignedUpdate.
|
|
80
80
|
return unsignedUpdate;
|
package/src/did-btcr2.ts
CHANGED
|
@@ -45,7 +45,6 @@ export interface DidCreateOptions {
|
|
|
45
45
|
network?: string;
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
// TODO: convert to API driver?
|
|
49
48
|
/** Initialize tiny secp256k1 */
|
|
50
49
|
initEccLib(tinysecp);
|
|
51
50
|
|
|
@@ -265,7 +264,6 @@ export class DidBtcr2 implements DidMethod {
|
|
|
265
264
|
signingMaterial?: KeyBytes | HexString;
|
|
266
265
|
bitcoin?: BitcoinConnection;
|
|
267
266
|
}): Promise<SignedBTCR2Update> {
|
|
268
|
-
// TODO: provide KMS as alternative
|
|
269
267
|
// If no signingMaterial provided, throw an UpdateError with INVALID_DID_UPDATE.
|
|
270
268
|
if (!signingMaterial) {
|
|
271
269
|
throw new UpdateError(
|
package/src/index.ts
CHANGED
|
@@ -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,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,aAAa,CAAC,MAAM,KAAK,EAAE,EAAE,CAAC;YAChC,MAAM,IAAI,KAAK,CAAC,oCAAoC,CAAC,CAAC;QACxD,CAAC;QACD,MAAM,MAAM,GAAG,KAAK,CAAC,IAAI,CAAC,kCAAkC,CAAC,CAAC;QAC9D,MAAM,CAAC,GAAG,KAAK,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;QACpC,0CAA0C;QAC1C,MAAM,aAAa,GAAG,IAAI,UAAU,CAAC,CAAC,GAAG,MAAM,EAAE,GAAG,CAAC,CA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,KAAK,CAAC,0BAA0B,CAAC,QAAgB,EAAE,IAAY;QACpE,4BAA4B;QAC5B,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,eAAe,CAAC,QAAQ,CAAC,CAAC;QACnD,6BAA6B;QAC7B,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;QAC7C,CAAC;QACD,gDAAgD;QAChD,MAAM,iBAAiB,GAAG,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,UAAU,CAAC;QACxD,IAAI,CAAC,iBAAiB,EAAE,CAAC;YACvB,MAAM,IAAI,KAAK,CAAC,qCAAqC,CAAC,CAAC;QACzD,CAAC;QACD,OAAO,iBAAiB,CAAC;IAC3B,CAAC;IAED;;;;;;OAMG;IACH,MAAM,CAAC,cAAc,CAAC,KAAY,EAAE,IAAY;QAC9C,+BAA+B;QAC/B,MAAM,QAAQ,GAAG,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACpC,iCAAiC;QACjC,IAAI,CAAC,QAAQ,EAAE,CAAC;YACd,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAC;QAChD,CAAC;QACD,uBAAuB;QACvB,OAAO,QAAQ,CAAC;IAClB,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;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