@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
|
@@ -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,UAAU,GAAG,UAAU;IAkBxF;;;;;OAKG;IACH,MAAM,CAAC,oBAAoB,CAAC,OAAO,EAAE,UAAU,GAAG,UAAU;IAiB5D;;;;;OAKG;IACH,MAAM,CAAC,GAAG,CAAC,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,UAAU,GAAG,UAAU;IAQ7D;;;;;;OAMG;IACH,MAAM,CAAC,IAAI,CAAC,cAAc,EAAE,UAAU,EAAE,IAAI,EAAE,UAAU,GAAG,UAAU;IAQrE;;;;;;OAMG;WACU,0BAA0B,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC;IAe5F;;;;;;OAMG;IACH,MAAM,CAAC,cAAc,CAAC,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,GAAG,KAAK;CAUzD"}
|
package/src/utils/general.ts
DELETED
|
@@ -1,204 +0,0 @@
|
|
|
1
|
-
import { KeyBytes, BIP340_PUBLIC_KEY_MULTIBASE_PREFIX, HdWallet } 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
|
-
/**
|
|
10
|
-
* Static class of general utility functions for the did-btcr2 spec implementation
|
|
11
|
-
* @class GeneralUtils
|
|
12
|
-
* @type {GeneralUtils}
|
|
13
|
-
*/
|
|
14
|
-
export class GeneralUtils {
|
|
15
|
-
/**
|
|
16
|
-
* Helper function to encode a secp256k1 key in SchnorrSecp256k1 Multikey Format
|
|
17
|
-
* @param {KeyBytes} xOnlyKeyBytes
|
|
18
|
-
* @returns {PublicKeyMultibase}
|
|
19
|
-
*/
|
|
20
|
-
public static encode(xOnlyKeyBytes: KeyBytes): string {
|
|
21
|
-
if (xOnlyKeyBytes.length !== 32) {
|
|
22
|
-
throw new Error('x-only public key must be 32 bytes');
|
|
23
|
-
}
|
|
24
|
-
const prefix = Array.from(BIP340_PUBLIC_KEY_MULTIBASE_PREFIX);
|
|
25
|
-
const x = Array.from(xOnlyKeyBytes);
|
|
26
|
-
// Set the prefix and the public key bytes
|
|
27
|
-
const multikeyBytes = new Uint8Array([...prefix, ...x]);
|
|
28
|
-
// Encode the public key as a multibase base58btc string
|
|
29
|
-
return base58btc.encode(multikeyBytes);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Converts a bigint to a buffer
|
|
34
|
-
* @param {bigint} value The bigint to convert
|
|
35
|
-
* @returns {Buffer} The buffer representation of the bigint
|
|
36
|
-
*/
|
|
37
|
-
static bigintToBuffer(value: bigint): Buffer {
|
|
38
|
-
const hex = value.toString(16).padStart(64, '0');
|
|
39
|
-
return Buffer.from(hex, 'hex');
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Generates a new mnemonic phrase and HD wallet
|
|
44
|
-
* @returns {HdWallet} Promise resolving to a new hdwallet object w/ mnemonic and hdkey
|
|
45
|
-
* @throws {Error} if the public key bytes cannot be derived
|
|
46
|
-
*/
|
|
47
|
-
static async generateHdWallet(): Promise<HdWallet> {
|
|
48
|
-
// Generate random mnemonic phrase.
|
|
49
|
-
const mnemonic = generateMnemonic(wordlist, 128);
|
|
50
|
-
// Generate seed from random mnemonic phrase.
|
|
51
|
-
const seed = await mnemonicToSeed(mnemonic);
|
|
52
|
-
// Generate HDKey from seed.
|
|
53
|
-
const hdkey = HDKey.fromMasterSeed(seed);
|
|
54
|
-
// Ensure HDKey returns valid
|
|
55
|
-
if (!hdkey) {
|
|
56
|
-
throw new Error('Failed to derive hd wallet');
|
|
57
|
-
}
|
|
58
|
-
return { mnemonic, hdkey };
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
static generateCompressedSecp256k1KeyPair(){
|
|
62
|
-
const privateKey = utils.randomPrivateKey();
|
|
63
|
-
if(!utils.isValidPrivateKey(privateKey)) {
|
|
64
|
-
throw new Error('Invalid private key');
|
|
65
|
-
}
|
|
66
|
-
return { privateKey, publicKey: getPublicKey(privateKey, true) };
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Recovers an HDKey from a mnemonic phrase
|
|
71
|
-
* @param {string} mnemonic The mnemonic phrase to recover the HDKey from
|
|
72
|
-
* @param {Uint8Array} seed Optional seed to recover the HDKey from
|
|
73
|
-
* @returns {HDKey} Promise resolving to the recovered HDKey
|
|
74
|
-
* @throws Error if the HDKey cannot be recovered
|
|
75
|
-
*/
|
|
76
|
-
static async recoverHdWallet(mnemonic: string, seed?: Uint8Array): Promise<HDKey> {
|
|
77
|
-
seed ??= await mnemonicToSeed(mnemonic);
|
|
78
|
-
// Generate HDKey from seed.
|
|
79
|
-
const hdkey = HDKey.fromMasterSeed(seed);
|
|
80
|
-
// Ensure HDKey returns valid
|
|
81
|
-
if (!hdkey) {
|
|
82
|
-
throw new Error('Failed to recover hdkey');
|
|
83
|
-
}
|
|
84
|
-
// Return the HDKey
|
|
85
|
-
return hdkey;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Recovers a secp256k1 privateKey from its original entropy
|
|
90
|
-
* @param {Uint8Array} xorEntropy The original entropy to recover the privateKey from
|
|
91
|
-
* @param {Uint8Array} salt The salt used to tweak the privateKey
|
|
92
|
-
* @returns {Uint8Array} The recovered privateKey
|
|
93
|
-
* @throws {Error} if the privateKey cannot be recovered
|
|
94
|
-
*/
|
|
95
|
-
static recoverTweakedRawPrivateKey(xorEntropy: Uint8Array, salt: Uint8Array): Uint8Array {
|
|
96
|
-
// If entropy is not 32 bytes, hash it to get a deterministic 32-byte private key
|
|
97
|
-
if (xorEntropy.length !== 32) {
|
|
98
|
-
xorEntropy = sha256(xorEntropy);
|
|
99
|
-
}
|
|
100
|
-
const entropy = this.XNOR(xorEntropy, salt);
|
|
101
|
-
// Convert entropy to hex
|
|
102
|
-
const hexEntropy = Buffer.from(entropy).toString('hex');
|
|
103
|
-
// Convert hexEntropy to BigInt
|
|
104
|
-
const privateKey = BigInt(`0x${hexEntropy}`);
|
|
105
|
-
// Ensure private key is in valid secp256k1 range1
|
|
106
|
-
if (privateKey < BigInt(1) || privateKey >= CURVE.n) {
|
|
107
|
-
throw new Error('Invalid private key derived from entropy');
|
|
108
|
-
}
|
|
109
|
-
// The valid 32-byte private key
|
|
110
|
-
return entropy;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Recovers a secp256k1 privateKey from its original entropy
|
|
115
|
-
* @param {Uint8Array} entropy The entropy to recover the privateKey from
|
|
116
|
-
* @returns {Uint8Array} The recovered privateKey
|
|
117
|
-
* @throws {Error} if the privateKey cannot be recovered
|
|
118
|
-
*/
|
|
119
|
-
static recoverRawPrivateKey(entropy: Uint8Array): Uint8Array {
|
|
120
|
-
// If entropy is not 32 bytes, hash it to get a deterministic 32-byte private key
|
|
121
|
-
if (entropy.length !== 32) {
|
|
122
|
-
entropy = sha256(entropy);
|
|
123
|
-
}
|
|
124
|
-
// Convert entropy to hex
|
|
125
|
-
const hexEntropy = Buffer.from(entropy).toString('hex');
|
|
126
|
-
// Convert hexEntropy to BigInt
|
|
127
|
-
const privateKey = BigInt(`0x${hexEntropy}`);
|
|
128
|
-
// Ensure private key is in valid secp256k1 range1
|
|
129
|
-
if (privateKey < BigInt(1) || privateKey >= CURVE.n) {
|
|
130
|
-
throw new Error('Invalid private key derived from entropy');
|
|
131
|
-
}
|
|
132
|
-
// The valid 32-byte private key
|
|
133
|
-
return entropy;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Tweak the entropy with a salt using XOR
|
|
138
|
-
* @param {Uint8Array} entropy The entropy to tweak
|
|
139
|
-
* @param {Uint8Array} salt The salt to tweak the entropy with
|
|
140
|
-
* @returns {Uint8Array} The tweaked entropy
|
|
141
|
-
*/
|
|
142
|
-
static XOR(entropy: Uint8Array, salt: Uint8Array): Uint8Array {
|
|
143
|
-
const tweaked = new Uint8Array(entropy.length);
|
|
144
|
-
for (let i = 0; i < entropy.length; i++) {
|
|
145
|
-
tweaked[i] = entropy[i] ^ salt[i % salt.length]; // XOR with repeating salt
|
|
146
|
-
}
|
|
147
|
-
return tweaked;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* Untweak the entropy with a salt using XNOR
|
|
152
|
-
*
|
|
153
|
-
* @param {Uint8Array} tweakedEntropy The tweaked entropy to untweak
|
|
154
|
-
* @param {Uint8Array} salt The salt to untweak the entropy with
|
|
155
|
-
* @returns {Uint8Array} The original entropy
|
|
156
|
-
*/
|
|
157
|
-
static XNOR(tweakedEntropy: Uint8Array, salt: Uint8Array): Uint8Array {
|
|
158
|
-
const originalEntropy = new Uint8Array(tweakedEntropy.length);
|
|
159
|
-
for (let i = 0; i < tweakedEntropy.length; i++) {
|
|
160
|
-
originalEntropy[i] = tweakedEntropy[i] ^ salt[i % salt.length]; // XOR with salt again
|
|
161
|
-
}
|
|
162
|
-
return originalEntropy;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Recovers an HDKey from a mnemonic phrase
|
|
167
|
-
* @param {string} mnemonic The mnemonic phrase to recover the HDKey from
|
|
168
|
-
* @param {string} path The path to derive the child key from
|
|
169
|
-
* @returns {Uint8Array} Promise resolving to the recovered private key bytes
|
|
170
|
-
* @throws {Error} if the HDKey cannot be recovered
|
|
171
|
-
*/
|
|
172
|
-
static async recoverHdChildFromMnemonic(mnemonic: string, path: string): Promise<Uint8Array> {
|
|
173
|
-
// Generate HDKey from seed.
|
|
174
|
-
const hdkey = await this.recoverHdWallet(mnemonic);
|
|
175
|
-
// Ensure HDKey returns valid
|
|
176
|
-
if (!hdkey) {
|
|
177
|
-
throw new Error('Failed to recover hdkey');
|
|
178
|
-
}
|
|
179
|
-
// Return the privateKey of the derived childKey
|
|
180
|
-
const childPrivKeyBytes = hdkey.derive(path).privateKey;
|
|
181
|
-
if (!childPrivKeyBytes) {
|
|
182
|
-
throw new Error('Failed to recover child private key');
|
|
183
|
-
}
|
|
184
|
-
return childPrivKeyBytes;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* Derives a child key from an HDKey
|
|
189
|
-
* @param {HDKey} hdkey The HDKey to derive the child key from
|
|
190
|
-
* @param {string} path The path to derive the child key from
|
|
191
|
-
* @returns {HDKey} A Promise resolving to the child key
|
|
192
|
-
* @throws {Error} Error if the child key cannot be derived
|
|
193
|
-
*/
|
|
194
|
-
static deriveChildKey(hdkey: HDKey, path: string): HDKey {
|
|
195
|
-
// Derive child key from HDKey.
|
|
196
|
-
const childKey = hdkey.derive(path);
|
|
197
|
-
// Ensure child key returns valid
|
|
198
|
-
if (!childKey) {
|
|
199
|
-
throw new Error(`Failed to derive child key`);
|
|
200
|
-
}
|
|
201
|
-
// Return the child key
|
|
202
|
-
return childKey;
|
|
203
|
-
}
|
|
204
|
-
}
|