@did-btcr2/method 0.22.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 +35708 -37764
- package/dist/browser.mjs +35707 -37763
- package/dist/cjs/core/beacon/aggregation/cohort/index.js +13 -2
- package/dist/cjs/core/beacon/aggregation/cohort/index.js.map +1 -1
- package/dist/cjs/core/beacon/aggregation/cohort/messages/base.js +7 -2
- package/dist/cjs/core/beacon/aggregation/cohort/messages/base.js.map +1 -1
- package/dist/cjs/core/beacon/aggregation/communication/adapter/did-comm.js +6 -11
- package/dist/cjs/core/beacon/aggregation/communication/adapter/did-comm.js.map +1 -1
- package/dist/cjs/core/beacon/aggregation/communication/adapter/nostr.js +5 -8
- 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 +18 -7
- package/dist/cjs/core/beacon/aggregation/session/index.js.map +1 -1
- package/dist/cjs/core/beacon/cas-beacon.js +1 -1
- package/dist/cjs/core/beacon/cas-beacon.js.map +1 -1
- package/dist/cjs/core/beacon/signal-discovery.js +6 -6
- package/dist/cjs/core/beacon/signal-discovery.js.map +1 -1
- package/dist/cjs/core/beacon/singleton.js +18 -17
- package/dist/cjs/core/beacon/singleton.js.map +1 -1
- 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 +11 -13
- package/dist/cjs/core/identifier.js.map +1 -1
- package/dist/cjs/core/resolve.js +25 -22
- 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 +11 -16
- 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 +22 -21
- package/dist/cjs/utils/did-document.js.map +1 -1
- package/dist/esm/core/beacon/aggregation/cohort/index.js +13 -2
- package/dist/esm/core/beacon/aggregation/cohort/index.js.map +1 -1
- package/dist/esm/core/beacon/aggregation/cohort/messages/base.js +7 -2
- package/dist/esm/core/beacon/aggregation/cohort/messages/base.js.map +1 -1
- package/dist/esm/core/beacon/aggregation/communication/adapter/did-comm.js +6 -11
- package/dist/esm/core/beacon/aggregation/communication/adapter/did-comm.js.map +1 -1
- package/dist/esm/core/beacon/aggregation/communication/adapter/nostr.js +5 -8
- 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 +18 -7
- package/dist/esm/core/beacon/aggregation/session/index.js.map +1 -1
- package/dist/esm/core/beacon/cas-beacon.js +1 -1
- package/dist/esm/core/beacon/cas-beacon.js.map +1 -1
- package/dist/esm/core/beacon/signal-discovery.js +6 -6
- package/dist/esm/core/beacon/signal-discovery.js.map +1 -1
- package/dist/esm/core/beacon/singleton.js +18 -17
- package/dist/esm/core/beacon/singleton.js.map +1 -1
- 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 +11 -13
- package/dist/esm/core/identifier.js.map +1 -1
- package/dist/esm/core/resolve.js +25 -22
- 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 +11 -16
- 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 +22 -21
- package/dist/esm/utils/did-document.js.map +1 -1
- package/dist/types/core/beacon/aggregation/cohort/index.d.ts +2 -1
- package/dist/types/core/beacon/aggregation/cohort/index.d.ts.map +1 -1
- package/dist/types/core/beacon/aggregation/cohort/messages/base.d.ts +2 -1
- package/dist/types/core/beacon/aggregation/cohort/messages/base.d.ts.map +1 -1
- 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 +2 -1
- 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 +4 -3
- package/dist/types/core/beacon/beacon.d.ts.map +1 -1
- package/dist/types/core/beacon/cas-beacon.d.ts +4 -3
- 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 +6 -5
- package/dist/types/core/beacon/signal-discovery.d.ts.map +1 -1
- package/dist/types/core/beacon/singleton.d.ts +4 -4
- package/dist/types/core/beacon/singleton.d.ts.map +1 -1
- package/dist/types/core/beacon/smt-beacon.d.ts +4 -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 +18 -21
- package/dist/types/core/identifier.d.ts.map +1 -1
- package/dist/types/core/interfaces.d.ts +3 -2
- package/dist/types/core/interfaces.d.ts.map +1 -1
- package/dist/types/core/resolve.d.ts +4 -3
- 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 +3 -2
- package/dist/types/core/update.d.ts.map +1 -1
- package/dist/types/did-btcr2.d.ts +5 -4
- 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 +5 -7
- package/dist/types/utils/did-document.d.ts.map +1 -1
- package/package.json +5 -6
- package/src/core/beacon/aggregation/cohort/index.ts +13 -2
- package/src/core/beacon/aggregation/cohort/messages/base.ts +7 -2
- package/src/core/beacon/aggregation/cohort/messages/keygen/cohort-ready.ts +1 -1
- package/src/core/beacon/aggregation/communication/adapter/did-comm.ts +9 -15
- package/src/core/beacon/aggregation/communication/adapter/nostr.ts +9 -11
- 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 +18 -7
- package/src/core/beacon/beacon.ts +3 -3
- package/src/core/beacon/cas-beacon.ts +3 -3
- package/src/core/beacon/signal-discovery.ts +9 -9
- package/src/core/beacon/singleton.ts +20 -20
- package/src/core/beacon/smt-beacon.ts +3 -3
- package/src/core/identifier.ts +31 -28
- package/src/core/interfaces.ts +2 -2
- package/src/core/resolve.ts +32 -26
- package/src/core/update.ts +6 -6
- package/src/did-btcr2.ts +21 -21
- package/src/index.ts +0 -1
- package/src/utils/did-document.ts +24 -20
- 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
|
@@ -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;
|
|
@@ -99,6 +103,7 @@ export interface Btcr2DidDocument extends W3CDidDocument {
|
|
|
99
103
|
capabilityInvocation?: Array<string | DidVerificationMethod>;
|
|
100
104
|
capabilityDelegation?: Array<string | DidVerificationMethod>;
|
|
101
105
|
service: Array<BeaconService>;
|
|
106
|
+
deactivated?: boolean;
|
|
102
107
|
}
|
|
103
108
|
|
|
104
109
|
/**
|
|
@@ -114,12 +119,13 @@ export interface Btcr2DidDocument extends W3CDidDocument {
|
|
|
114
119
|
* @property {Array<string | DidVerificationMethod>} [capabilityInvocation] - The capability invocation methods of the DID Document.
|
|
115
120
|
* @property {Array<string | DidVerificationMethod>} [capabilityDelegation] - The capability delegation methods of the DID Document.
|
|
116
121
|
* @property {Array<BeaconService>} service - The services of the DID Document.
|
|
122
|
+
* @property {boolean} [deactivated] - Indicates if the DID Document is deactivated.
|
|
117
123
|
*/
|
|
118
124
|
export class DidDocument implements Btcr2DidDocument {
|
|
119
125
|
id: string;
|
|
120
126
|
'@context'?: Array<string | JSONObject> = [
|
|
121
|
-
'https://www.w3.org/
|
|
122
|
-
'https://btcr2.dev/context/v1'
|
|
127
|
+
'https://www.w3.org/ns/did/v1.1',
|
|
128
|
+
'https://btcr2.dev/context/v1',
|
|
123
129
|
];
|
|
124
130
|
verificationMethod: Array<DidVerificationMethod>;
|
|
125
131
|
authentication?: Array<string | DidVerificationMethod>;
|
|
@@ -164,7 +170,7 @@ export class DidDocument implements Btcr2DidDocument {
|
|
|
164
170
|
this.verificationMethod = document.verificationMethod || [];
|
|
165
171
|
this.service = document.service || [];
|
|
166
172
|
this['@context'] = document['@context'] || [
|
|
167
|
-
'https://www.w3.org/
|
|
173
|
+
'https://www.w3.org/ns/did/v1.1',
|
|
168
174
|
'https://btcr2.dev/context/v1'
|
|
169
175
|
];
|
|
170
176
|
|
|
@@ -199,8 +205,18 @@ export class DidDocument implements Btcr2DidDocument {
|
|
|
199
205
|
* Convert the DidDocument to a JSON object.
|
|
200
206
|
* @returns {DidDocument} The JSON representation of the DidDocument.
|
|
201
207
|
*/
|
|
202
|
-
public
|
|
203
|
-
return
|
|
208
|
+
public toJSON(): DidDocumentLike {
|
|
209
|
+
return {
|
|
210
|
+
id : this.id,
|
|
211
|
+
'@context' : this['@context'],
|
|
212
|
+
verificationMethod : this.verificationMethod,
|
|
213
|
+
authentication : this.authentication,
|
|
214
|
+
assertionMethod : this.assertionMethod,
|
|
215
|
+
capabilityInvocation : this.capabilityInvocation,
|
|
216
|
+
capabilityDelegation : this.capabilityDelegation,
|
|
217
|
+
service : this.service,
|
|
218
|
+
deactivated : this.deactivated
|
|
219
|
+
};
|
|
204
220
|
}
|
|
205
221
|
|
|
206
222
|
/**
|
|
@@ -309,18 +325,6 @@ export class DidDocument implements Btcr2DidDocument {
|
|
|
309
325
|
}
|
|
310
326
|
}
|
|
311
327
|
|
|
312
|
-
/**
|
|
313
|
-
* Validates that the controller exists and is correctly formatted.
|
|
314
|
-
* @param {Array<string>} controller The controller to validate.
|
|
315
|
-
* @returns {boolean} True if the controller is valid.
|
|
316
|
-
*/
|
|
317
|
-
private static isValidController(controller: Array<string>): boolean {
|
|
318
|
-
if(!controller) return false;
|
|
319
|
-
if(!Array.isArray(controller)) return false;
|
|
320
|
-
if(!controller.every(c => typeof c === 'string')) return false;
|
|
321
|
-
return true;
|
|
322
|
-
}
|
|
323
|
-
|
|
324
328
|
/**
|
|
325
329
|
* Validates that verification methods exist and are correctly formatted.
|
|
326
330
|
* @private
|
|
@@ -525,6 +529,6 @@ export class GenesisDocument extends DidDocument {
|
|
|
525
529
|
* @returns {Bytes} The genesis bytes.
|
|
526
530
|
*/
|
|
527
531
|
static toGenesisBytes(genesisDocument: GenesisDocumentLike): HashBytes {
|
|
528
|
-
return
|
|
532
|
+
return hash(canonicalize(genesisDocument));
|
|
529
533
|
}
|
|
530
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,C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,CAAC,CAAC,sBAAsB;QACxF,CAAC;QACD,OAAO,eAAe,CAAC;IACzB,CAAC;IAED;;;;;;OAMG;IACH,MAAM,CAAC,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,OAA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I,KAAK,CAAC,4BAA4B,CAAC,CAAC;QAChD,CAAC;QACD,uBAAuB;QACvB,OAAO,QAAQ,CAAC;IAClB,CAAC;CACF"}
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import { KeyBytes, HdWallet } from '@did-btcr2/common';
|
|
2
|
-
import { HDKey } from '@scure/bip32';
|
|
3
|
-
/**
|
|
4
|
-
* Static class of general utility functions for the did-btcr2 spec implementation
|
|
5
|
-
* @class GeneralUtils
|
|
6
|
-
* @type {GeneralUtils}
|
|
7
|
-
*/
|
|
8
|
-
export declare class GeneralUtils {
|
|
9
|
-
/**
|
|
10
|
-
* Helper function to encode a secp256k1 key in SchnorrSecp256k1 Multikey Format
|
|
11
|
-
* @param {KeyBytes} xOnlyKeyBytes
|
|
12
|
-
* @returns {PublicKeyMultibase}
|
|
13
|
-
*/
|
|
14
|
-
static encode(xOnlyKeyBytes: KeyBytes): string;
|
|
15
|
-
/**
|
|
16
|
-
* Converts a bigint to a buffer
|
|
17
|
-
* @param {bigint} value The bigint to convert
|
|
18
|
-
* @returns {Buffer} The buffer representation of the bigint
|
|
19
|
-
*/
|
|
20
|
-
static bigintToBuffer(value: bigint): Buffer;
|
|
21
|
-
/**
|
|
22
|
-
* Generates a new mnemonic phrase and HD wallet
|
|
23
|
-
* @returns {HdWallet} Promise resolving to a new hdwallet object w/ mnemonic and hdkey
|
|
24
|
-
* @throws {Error} if the public key bytes cannot be derived
|
|
25
|
-
*/
|
|
26
|
-
static generateHdWallet(): Promise<HdWallet>;
|
|
27
|
-
static generateCompressedSecp256k1KeyPair(): {
|
|
28
|
-
privateKey: import("@noble/secp256k1").Bytes;
|
|
29
|
-
publicKey: import("@noble/secp256k1").Bytes;
|
|
30
|
-
};
|
|
31
|
-
/**
|
|
32
|
-
* Recovers an HDKey from a mnemonic phrase
|
|
33
|
-
* @param {string} mnemonic The mnemonic phrase to recover the HDKey from
|
|
34
|
-
* @param {Uint8Array} seed Optional seed to recover the HDKey from
|
|
35
|
-
* @returns {HDKey} Promise resolving to the recovered HDKey
|
|
36
|
-
* @throws Error if the HDKey cannot be recovered
|
|
37
|
-
*/
|
|
38
|
-
static recoverHdWallet(mnemonic: string, seed?: Uint8Array): Promise<HDKey>;
|
|
39
|
-
/**
|
|
40
|
-
* Recovers a secp256k1 privateKey from its original entropy
|
|
41
|
-
* @param {Uint8Array} xorEntropy The original entropy to recover the privateKey from
|
|
42
|
-
* @param {Uint8Array} salt The salt used to tweak the privateKey
|
|
43
|
-
* @returns {Uint8Array} The recovered privateKey
|
|
44
|
-
* @throws {Error} if the privateKey cannot be recovered
|
|
45
|
-
*/
|
|
46
|
-
static recoverTweakedRawPrivateKey(xorEntropy: Uint8Array, salt: Uint8Array): Uint8Array;
|
|
47
|
-
/**
|
|
48
|
-
* Recovers a secp256k1 privateKey from its original entropy
|
|
49
|
-
* @param {Uint8Array} entropy The entropy to recover the privateKey from
|
|
50
|
-
* @returns {Uint8Array} The recovered privateKey
|
|
51
|
-
* @throws {Error} if the privateKey cannot be recovered
|
|
52
|
-
*/
|
|
53
|
-
static recoverRawPrivateKey(entropy: Uint8Array): Uint8Array;
|
|
54
|
-
/**
|
|
55
|
-
* Tweak the entropy with a salt using XOR
|
|
56
|
-
* @param {Uint8Array} entropy The entropy to tweak
|
|
57
|
-
* @param {Uint8Array} salt The salt to tweak the entropy with
|
|
58
|
-
* @returns {Uint8Array} The tweaked entropy
|
|
59
|
-
*/
|
|
60
|
-
static XOR(entropy: Uint8Array, salt: Uint8Array): Uint8Array;
|
|
61
|
-
/**
|
|
62
|
-
* Untweak the entropy with a salt using XNOR
|
|
63
|
-
*
|
|
64
|
-
* @param {Uint8Array} tweakedEntropy The tweaked entropy to untweak
|
|
65
|
-
* @param {Uint8Array} salt The salt to untweak the entropy with
|
|
66
|
-
* @returns {Uint8Array} The original entropy
|
|
67
|
-
*/
|
|
68
|
-
static XNOR(tweakedEntropy: Uint8Array, salt: Uint8Array): Uint8Array;
|
|
69
|
-
/**
|
|
70
|
-
* Recovers an HDKey from a mnemonic phrase
|
|
71
|
-
* @param {string} mnemonic The mnemonic phrase to recover the HDKey from
|
|
72
|
-
* @param {string} path The path to derive the child key from
|
|
73
|
-
* @returns {Uint8Array} Promise resolving to the recovered private key bytes
|
|
74
|
-
* @throws {Error} if the HDKey cannot be recovered
|
|
75
|
-
*/
|
|
76
|
-
static recoverHdChildFromMnemonic(mnemonic: string, path: string): Promise<Uint8Array>;
|
|
77
|
-
/**
|
|
78
|
-
* Derives a child key from an HDKey
|
|
79
|
-
* @param {HDKey} hdkey The HDKey to derive the child key from
|
|
80
|
-
* @param {string} path The path to derive the child key from
|
|
81
|
-
* @returns {HDKey} A Promise resolving to the child key
|
|
82
|
-
* @throws {Error} Error if the child key cannot be derived
|
|
83
|
-
*/
|
|
84
|
-
static deriveChildKey(hdkey: HDKey, path: string): HDKey;
|
|
85
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"general.d.ts","sourceRoot":"","sources":["../../../src/utils/general.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAsC,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAG3F,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAKrC;;;;GAIG;AACH,qBAAa,YAAY;IACvB;;;;OAIG;WACW,MAAM,CAAC,aAAa,EAAE,QAAQ,GAAG,MAAM;IAYrD;;;;OAIG;IACH,MAAM,CAAC,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAK5C;;;;OAIG;WACU,gBAAgB,IAAI,OAAO,CAAC,QAAQ,CAAC;IAclD,MAAM,CAAC,kCAAkC;;;;IAQzC;;;;;;OAMG;WACU,eAAe,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,UAAU,GAAG,OAAO,CAAC,KAAK,CAAC;IAYjF;;;;;;OAMG;IACH,MAAM,CAAC,2BAA2B,CAAC,UAAU,EAAE,UAAU,EAAE,IAAI,EAAE,UA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