@bsv/sdk 1.3.2 → 1.3.4
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/dist/cjs/package.json +1 -1
- package/dist/cjs/src/auth/certificates/Certificate.js +3 -4
- package/dist/cjs/src/auth/certificates/Certificate.js.map +1 -1
- package/dist/cjs/src/auth/certificates/MasterCertificate.js.map +1 -1
- package/dist/cjs/src/auth/utils/certificateHelpers.js +2 -2
- package/dist/cjs/src/auth/utils/certificateHelpers.js.map +1 -1
- package/dist/cjs/src/wallet/CachedKeyDeriver.js +11 -14
- package/dist/cjs/src/wallet/CachedKeyDeriver.js.map +1 -1
- package/dist/cjs/src/wallet/KeyDeriver.js +14 -11
- package/dist/cjs/src/wallet/KeyDeriver.js.map +1 -1
- package/dist/cjs/src/wallet/ProtoWallet.js +53 -111
- package/dist/cjs/src/wallet/ProtoWallet.js.map +1 -1
- package/dist/cjs/src/wallet/WalletClient.js.map +1 -1
- package/dist/cjs/src/wallet/substrates/WalletWireTransceiver.js +1 -0
- package/dist/cjs/src/wallet/substrates/WalletWireTransceiver.js.map +1 -1
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/esm/src/auth/certificates/Certificate.js +4 -5
- package/dist/esm/src/auth/certificates/Certificate.js.map +1 -1
- package/dist/esm/src/auth/certificates/MasterCertificate.js.map +1 -1
- package/dist/esm/src/auth/utils/certificateHelpers.js +2 -2
- package/dist/esm/src/auth/utils/certificateHelpers.js.map +1 -1
- package/dist/esm/src/wallet/CachedKeyDeriver.js +10 -10
- package/dist/esm/src/wallet/CachedKeyDeriver.js.map +1 -1
- package/dist/esm/src/wallet/KeyDeriver.js +15 -12
- package/dist/esm/src/wallet/KeyDeriver.js.map +1 -1
- package/dist/esm/src/wallet/ProtoWallet.js +53 -76
- package/dist/esm/src/wallet/ProtoWallet.js.map +1 -1
- package/dist/esm/src/wallet/WalletClient.js.map +1 -1
- package/dist/esm/src/wallet/substrates/WalletWireTransceiver.js +1 -0
- package/dist/esm/src/wallet/substrates/WalletWireTransceiver.js.map +1 -1
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/types/src/auth/certificates/Certificate.d.ts +2 -2
- package/dist/types/src/auth/certificates/Certificate.d.ts.map +1 -1
- package/dist/types/src/auth/certificates/MasterCertificate.d.ts +2 -2
- package/dist/types/src/auth/certificates/MasterCertificate.d.ts.map +1 -1
- package/dist/types/src/auth/utils/certificateHelpers.d.ts +5 -5
- package/dist/types/src/auth/utils/certificateHelpers.d.ts.map +1 -1
- package/dist/types/src/wallet/CachedKeyDeriver.d.ts +16 -15
- package/dist/types/src/wallet/CachedKeyDeriver.d.ts.map +1 -1
- package/dist/types/src/wallet/KeyDeriver.d.ts +74 -18
- package/dist/types/src/wallet/KeyDeriver.d.ts.map +1 -1
- package/dist/types/src/wallet/ProtoWallet.d.ts +31 -407
- package/dist/types/src/wallet/ProtoWallet.d.ts.map +1 -1
- package/dist/types/src/wallet/Wallet.interfaces.d.ts +201 -169
- package/dist/types/src/wallet/Wallet.interfaces.d.ts.map +1 -1
- package/dist/types/src/wallet/WalletClient.d.ts +2 -3
- package/dist/types/src/wallet/WalletClient.d.ts.map +1 -1
- package/dist/types/src/wallet/substrates/HTTPWalletJSON.d.ts +1 -1
- package/dist/types/src/wallet/substrates/HTTPWalletJSON.d.ts.map +1 -1
- package/dist/types/src/wallet/substrates/WalletWireTransceiver.d.ts +1 -1
- package/dist/types/src/wallet/substrates/WalletWireTransceiver.d.ts.map +1 -1
- package/dist/types/src/wallet/substrates/XDM.d.ts +2 -2
- package/dist/types/src/wallet/substrates/XDM.d.ts.map +1 -1
- package/dist/types/src/wallet/substrates/window.CWI.d.ts +2 -2
- package/dist/types/src/wallet/substrates/window.CWI.d.ts.map +1 -1
- package/dist/types/tsconfig.types.tsbuildinfo +1 -1
- package/dist/umd/bundle.js +1 -1
- package/docs/auth.md +12 -12
- package/docs/wallet-substrates.md +6 -6
- package/docs/wallet.md +897 -983
- package/package.json +1 -1
- package/src/auth/certificates/Certificate.ts +6 -6
- package/src/auth/certificates/MasterCertificate.ts +3 -2
- package/src/auth/utils/certificateHelpers.ts +5 -5
- package/src/wallet/CachedKeyDeriver.ts +22 -21
- package/src/wallet/KeyDeriver.ts +89 -22
- package/src/wallet/ProtoWallet.ts +105 -550
- package/src/wallet/Wallet.interfaces.ts +259 -176
- package/src/wallet/WalletClient.ts +2 -1
- package/src/wallet/__tests/ProtoWallet.test.ts +0 -53
- package/src/wallet/substrates/HTTPWalletJSON.ts +1 -1
- package/src/wallet/substrates/WalletWireTransceiver.ts +2 -1
- package/src/wallet/substrates/XDM.ts +2 -2
- package/src/wallet/substrates/__tests/WalletWire.integration.test.ts +22 -22
- package/src/wallet/substrates/window.CWI.ts +2 -2
package/package.json
CHANGED
|
@@ -6,7 +6,8 @@ import {
|
|
|
6
6
|
HexString,
|
|
7
7
|
OutpointString,
|
|
8
8
|
CertificateFieldNameUnder50Bytes,
|
|
9
|
-
ProtoWallet
|
|
9
|
+
ProtoWallet,
|
|
10
|
+
Signature
|
|
10
11
|
} from '../../../mod.js'
|
|
11
12
|
|
|
12
13
|
/**
|
|
@@ -128,7 +129,6 @@ export default class Certificate {
|
|
|
128
129
|
// Write signature if included
|
|
129
130
|
if (includeSignature && this.signature && this.signature.length > 0) {
|
|
130
131
|
const signatureBytes = Utils.toArray(this.signature, 'hex')
|
|
131
|
-
writer.writeVarIntNum(signatureBytes.length)
|
|
132
132
|
writer.write(signatureBytes)
|
|
133
133
|
}
|
|
134
134
|
|
|
@@ -186,9 +186,9 @@ export default class Certificate {
|
|
|
186
186
|
// Read signature if present
|
|
187
187
|
let signature: string | undefined
|
|
188
188
|
if (!reader.eof()) {
|
|
189
|
-
const
|
|
190
|
-
const
|
|
191
|
-
signature =
|
|
189
|
+
const signatureBytes = reader.read()
|
|
190
|
+
const sig = Signature.fromDER(signatureBytes)
|
|
191
|
+
signature = sig.toString('hex') as string
|
|
192
192
|
}
|
|
193
193
|
|
|
194
194
|
return new Certificate(
|
|
@@ -228,7 +228,7 @@ export default class Certificate {
|
|
|
228
228
|
* @param {Wallet} certifier - The wallet representing the certifier.
|
|
229
229
|
* @returns {Promise<void>}
|
|
230
230
|
*/
|
|
231
|
-
async sign(certifier:
|
|
231
|
+
async sign(certifier: ProtoWallet): Promise<void> {
|
|
232
232
|
const preimage = this.toBinary(false) // Exclude the signature when signing
|
|
233
233
|
const { signature } = await certifier.createSignature({
|
|
234
234
|
data: preimage,
|
|
@@ -6,7 +6,8 @@ import {
|
|
|
6
6
|
HexString,
|
|
7
7
|
OutpointString,
|
|
8
8
|
PubKeyHex,
|
|
9
|
-
Wallet
|
|
9
|
+
Wallet,
|
|
10
|
+
ProtoWallet
|
|
10
11
|
} from '../../../mod.js'
|
|
11
12
|
import Certificate from './Certificate.js'
|
|
12
13
|
|
|
@@ -58,7 +59,7 @@ export class MasterCertificate extends Certificate {
|
|
|
58
59
|
* - fieldsToReveal is empty or a field in `fieldsToReveal` does not exist in the certificate.
|
|
59
60
|
* - The decrypted master field key fails to decrypt the corresponding field (indicating an invalid key).
|
|
60
61
|
*/
|
|
61
|
-
async createKeyringForVerifier(subjectWallet:
|
|
62
|
+
async createKeyringForVerifier(subjectWallet: ProtoWallet, verifierIdentityKey: string, fieldsToReveal: string[], originator?: string): Promise<Record<CertificateFieldNameUnder50Bytes, string>> {
|
|
62
63
|
if (!Array.isArray(fieldsToReveal)) {
|
|
63
64
|
throw new Error('fieldsToReveal must be an array of strings')
|
|
64
65
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { PrivateKey, SymmetricKey, Utils,
|
|
1
|
+
import { PrivateKey, SymmetricKey, Utils, ProtoWallet } from "../../../mod.js"
|
|
2
2
|
import { MasterCertificate } from "../certificates/MasterCertificate.js"
|
|
3
3
|
import { VerifiableCertificate } from "../certificates/VerifiableCertificate.js"
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Creates a Master Certificate by encrypting provided fields and generating a master keyring.
|
|
7
7
|
*
|
|
8
|
-
* @param {
|
|
8
|
+
* @param {ProtoWallet} wallet - The wallet instance used for encryption and public key retrieval.
|
|
9
9
|
* @param {Record<string, string>} fields - The certificate fields to encrypt.
|
|
10
10
|
* @param {string} certificateType - The type of the certificate being created.
|
|
11
11
|
* @param {string} certificateSerialNumber - The serial number of the certificate.
|
|
@@ -13,7 +13,7 @@ import { VerifiableCertificate } from "../certificates/VerifiableCertificate.js"
|
|
|
13
13
|
* @returns {Promise<MasterCertificate>} A promise resolving to the created Master Certificate.
|
|
14
14
|
*/
|
|
15
15
|
export async function createMasterCertificate(
|
|
16
|
-
wallet:
|
|
16
|
+
wallet: ProtoWallet,
|
|
17
17
|
fields: Record<string, string>,
|
|
18
18
|
certificateType: string,
|
|
19
19
|
certificateSerialNumber: string,
|
|
@@ -51,7 +51,7 @@ export async function createMasterCertificate(
|
|
|
51
51
|
* Creates a Verifiable Certificate by signing a Master Certificate and generating a keyring for a verifier.
|
|
52
52
|
*
|
|
53
53
|
* @param {MasterCertificate} masterCertificate - The master certificate to convert into a verifiable certificate.
|
|
54
|
-
* @param {
|
|
54
|
+
* @param {ProtoWallet} wallet - The wallet instance used for generating a keyring for the verifier.
|
|
55
55
|
* @param {string} verifierIdentityKey - The identity key of the verifier.
|
|
56
56
|
* @param {string[]} fieldsToReveal - The list of fields to reveal to the verifier.
|
|
57
57
|
* @param {PrivateKey} certifierPrivateKey - The private key of the certifier for signing the certificate.
|
|
@@ -59,7 +59,7 @@ export async function createMasterCertificate(
|
|
|
59
59
|
*/
|
|
60
60
|
export async function createVerifiableCertificate(
|
|
61
61
|
masterCertificate: MasterCertificate,
|
|
62
|
-
wallet:
|
|
62
|
+
wallet: ProtoWallet,
|
|
63
63
|
verifierIdentityKey: string,
|
|
64
64
|
fieldsToReveal: string[],
|
|
65
65
|
certifierPrivateKey: PrivateKey
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { PrivateKey, PublicKey, SymmetricKey } from '../primitives/index.js'
|
|
2
|
-
import KeyDeriver from './KeyDeriver.js'
|
|
3
|
-
import {
|
|
2
|
+
import { Counterparty, KeyDeriver } from './KeyDeriver.js'
|
|
3
|
+
import { WalletProtocol } from './Wallet.interfaces.js'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* A cached version of KeyDeriver that caches the results of key derivation methods.
|
|
@@ -24,19 +24,20 @@ export default class CachedKeyDeriver {
|
|
|
24
24
|
this.maxCacheSize = options?.maxCacheSize || 1000
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
|
|
27
28
|
/**
|
|
28
29
|
* Derives a public key based on protocol ID, key ID, and counterparty.
|
|
29
30
|
* Caches the result for future calls with the same parameters.
|
|
30
|
-
* @param {
|
|
31
|
+
* @param {WalletProtocol} protocolID - The protocol ID including a security level and protocol name.
|
|
31
32
|
* @param {string} keyID - The key identifier.
|
|
32
|
-
* @param {
|
|
33
|
+
* @param {Counterparty} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
|
|
33
34
|
* @param {boolean} [forSelf=false] - Whether deriving for self.
|
|
34
35
|
* @returns {PublicKey} - The derived public key.
|
|
35
36
|
*/
|
|
36
37
|
derivePublicKey (
|
|
37
|
-
protocolID:
|
|
38
|
+
protocolID: WalletProtocol,
|
|
38
39
|
keyID: string,
|
|
39
|
-
counterparty:
|
|
40
|
+
counterparty: Counterparty,
|
|
40
41
|
forSelf: boolean = false
|
|
41
42
|
): PublicKey {
|
|
42
43
|
const cacheKey = this.generateCacheKey('derivePublicKey', protocolID, keyID, counterparty, forSelf)
|
|
@@ -52,15 +53,15 @@ export default class CachedKeyDeriver {
|
|
|
52
53
|
/**
|
|
53
54
|
* Derives a private key based on protocol ID, key ID, and counterparty.
|
|
54
55
|
* Caches the result for future calls with the same parameters.
|
|
55
|
-
* @param {
|
|
56
|
+
* @param {WalletProtocol} protocolID - The protocol ID including a security level and protocol name.
|
|
56
57
|
* @param {string} keyID - The key identifier.
|
|
57
|
-
* @param {
|
|
58
|
+
* @param {Counterparty} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
|
|
58
59
|
* @returns {PrivateKey} - The derived private key.
|
|
59
60
|
*/
|
|
60
61
|
derivePrivateKey (
|
|
61
|
-
protocolID:
|
|
62
|
+
protocolID: WalletProtocol,
|
|
62
63
|
keyID: string,
|
|
63
|
-
counterparty:
|
|
64
|
+
counterparty: Counterparty
|
|
64
65
|
): PrivateKey {
|
|
65
66
|
const cacheKey = this.generateCacheKey('derivePrivateKey', protocolID, keyID, counterparty)
|
|
66
67
|
if (this.cache.has(cacheKey)) {
|
|
@@ -75,16 +76,16 @@ export default class CachedKeyDeriver {
|
|
|
75
76
|
/**
|
|
76
77
|
* Derives a symmetric key based on protocol ID, key ID, and counterparty.
|
|
77
78
|
* Caches the result for future calls with the same parameters.
|
|
78
|
-
* @param {
|
|
79
|
+
* @param {WalletProtocol} protocolID - The protocol ID including a security level and protocol name.
|
|
79
80
|
* @param {string} keyID - The key identifier.
|
|
80
|
-
* @param {
|
|
81
|
+
* @param {Counterparty} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
|
|
81
82
|
* @returns {SymmetricKey} - The derived symmetric key.
|
|
82
83
|
* @throws {Error} - Throws an error if attempting to derive a symmetric key for 'anyone'.
|
|
83
84
|
*/
|
|
84
85
|
deriveSymmetricKey (
|
|
85
|
-
protocolID:
|
|
86
|
+
protocolID: WalletProtocol,
|
|
86
87
|
keyID: string,
|
|
87
|
-
counterparty:
|
|
88
|
+
counterparty: Counterparty
|
|
88
89
|
): SymmetricKey {
|
|
89
90
|
const cacheKey = this.generateCacheKey('deriveSymmetricKey', protocolID, keyID, counterparty)
|
|
90
91
|
if (this.cache.has(cacheKey)) {
|
|
@@ -99,11 +100,11 @@ export default class CachedKeyDeriver {
|
|
|
99
100
|
/**
|
|
100
101
|
* Reveals the shared secret between the root key and the counterparty.
|
|
101
102
|
* Caches the result for future calls with the same parameters.
|
|
102
|
-
* @param {
|
|
103
|
+
* @param {Counterparty} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
|
|
103
104
|
* @returns {number[]} - The shared secret as a number array.
|
|
104
105
|
* @throws {Error} - Throws an error if attempting to reveal a shared secret for 'self'.
|
|
105
106
|
*/
|
|
106
|
-
revealCounterpartySecret (counterparty:
|
|
107
|
+
revealCounterpartySecret (counterparty: Counterparty): number[] {
|
|
107
108
|
const cacheKey = this.generateCacheKey('revealCounterpartySecret', counterparty)
|
|
108
109
|
if (this.cache.has(cacheKey)) {
|
|
109
110
|
return this.cacheGet(cacheKey)
|
|
@@ -117,14 +118,14 @@ export default class CachedKeyDeriver {
|
|
|
117
118
|
/**
|
|
118
119
|
* Reveals the specific key association for a given protocol ID, key ID, and counterparty.
|
|
119
120
|
* Caches the result for future calls with the same parameters.
|
|
120
|
-
* @param {
|
|
121
|
-
* @param {
|
|
121
|
+
* @param {Counterparty} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
|
|
122
|
+
* @param {WalletProtocol} protocolID - The protocol ID including a security level and protocol name.
|
|
122
123
|
* @param {string} keyID - The key identifier.
|
|
123
124
|
* @returns {number[]} - The specific key association as a number array.
|
|
124
125
|
*/
|
|
125
126
|
revealSpecificSecret (
|
|
126
|
-
counterparty:
|
|
127
|
-
protocolID:
|
|
127
|
+
counterparty: Counterparty,
|
|
128
|
+
protocolID: WalletProtocol,
|
|
128
129
|
keyID: string
|
|
129
130
|
): number[] {
|
|
130
131
|
const cacheKey = this.generateCacheKey('revealSpecificSecret', counterparty, protocolID, keyID)
|
|
@@ -187,7 +188,7 @@ export default class CachedKeyDeriver {
|
|
|
187
188
|
if (this.cache.size >= this.maxCacheSize) {
|
|
188
189
|
// Evict the least recently used item (first item in Map)
|
|
189
190
|
const firstKey = this.cache.keys().next().value
|
|
190
|
-
this.cache.delete(firstKey)
|
|
191
|
+
this.cache.delete(firstKey!)
|
|
191
192
|
}
|
|
192
193
|
this.cache.set(cacheKey, value)
|
|
193
194
|
}
|
package/src/wallet/KeyDeriver.ts
CHANGED
|
@@ -1,34 +1,99 @@
|
|
|
1
1
|
import { PrivateKey, PublicKey, SymmetricKey, Hash, Utils } from '../primitives/index.js'
|
|
2
|
-
import {
|
|
2
|
+
import { WalletProtocol, PubKeyHex } from './Wallet.interfaces.js'
|
|
3
|
+
|
|
4
|
+
export type Counterparty = PublicKey | PubKeyHex | 'self' | 'anyone'
|
|
5
|
+
|
|
6
|
+
export interface KeyDeriverApi {
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The root key from which all other keys are derived.
|
|
10
|
+
*/
|
|
11
|
+
rootKey: PrivateKey
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The identity of this key deriver which is normally the public key associated with the `rootKey`
|
|
15
|
+
*/
|
|
16
|
+
identityKey: string
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Derives a public key based on protocol ID, key ID, and counterparty.
|
|
20
|
+
* @param {WalletProtocol} protocolID - The protocol ID including a security level and protocol name.
|
|
21
|
+
* @param {string} keyID - The key identifier.
|
|
22
|
+
* @param {Counterparty} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
|
|
23
|
+
* @param {boolean} [forSelf=false] - Optional. false if undefined. Whether deriving for self.
|
|
24
|
+
* @returns {PublicKey} - The derived public key.
|
|
25
|
+
*/
|
|
26
|
+
derivePublicKey(protocolID: WalletProtocol, keyID: string, counterparty: Counterparty, forSelf?: boolean): PublicKey
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Derives a private key based on protocol ID, key ID, and counterparty.
|
|
30
|
+
* @param {WalletProtocol} protocolID - The protocol ID including a security level and protocol name.
|
|
31
|
+
* @param {string} keyID - The key identifier.
|
|
32
|
+
* @param {Counterparty} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
|
|
33
|
+
* @returns {PrivateKey} - The derived private key.
|
|
34
|
+
*/
|
|
35
|
+
derivePrivateKey(protocolID: WalletProtocol, keyID: string, counterparty: Counterparty): PrivateKey
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Derives a symmetric key based on protocol ID, key ID, and counterparty.
|
|
39
|
+
* Note: Symmetric keys should not be derivable by everyone due to security risks.
|
|
40
|
+
* @param {WalletProtocol} protocolID - The protocol ID including a security level and protocol name.
|
|
41
|
+
* @param {string} keyID - The key identifier.
|
|
42
|
+
* @param {Counterparty} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
|
|
43
|
+
* @returns {SymmetricKey} - The derived symmetric key.
|
|
44
|
+
* @throws {Error} - Throws an error if attempting to derive a symmetric key for 'anyone'.
|
|
45
|
+
*/
|
|
46
|
+
deriveSymmetricKey(protocolID: WalletProtocol, keyID: string, counterparty: Counterparty): SymmetricKey
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Reveals the shared secret between the root key and the counterparty.
|
|
50
|
+
* Note: This should not be used for 'self'.
|
|
51
|
+
* @param {Counterparty} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
|
|
52
|
+
* @returns {number[]} - The shared secret as a number array.
|
|
53
|
+
* @throws {Error} - Throws an error if attempting to reveal a shared secret for 'self'.
|
|
54
|
+
*/
|
|
55
|
+
revealCounterpartySecret(counterparty: Counterparty): number[]
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Reveals the specific key association for a given protocol ID, key ID, and counterparty.
|
|
59
|
+
* @param {Counterparty} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
|
|
60
|
+
* @param {WalletProtocol} protocolID - The protocol ID including a security level and protocol name.
|
|
61
|
+
* @param {string} keyID - The key identifier.
|
|
62
|
+
* @returns {number[]} - The specific key association as a number array.
|
|
63
|
+
*/
|
|
64
|
+
revealSpecificSecret(counterparty: Counterparty, protocolID: WalletProtocol, keyID: string): number[]
|
|
65
|
+
}
|
|
3
66
|
|
|
4
67
|
/**
|
|
5
68
|
* Class responsible for deriving various types of keys using a root private key.
|
|
6
69
|
* It supports deriving public and private keys, symmetric keys, and revealing key linkages.
|
|
7
70
|
*/
|
|
8
|
-
export
|
|
71
|
+
export class KeyDeriver implements KeyDeriverApi {
|
|
9
72
|
rootKey: PrivateKey
|
|
73
|
+
identityKey: string
|
|
10
74
|
|
|
11
75
|
/**
|
|
12
76
|
* Initializes the KeyDeriver instance with a root private key.
|
|
13
77
|
* @param {PrivateKey | 'anyone'} rootKey - The root private key or the string 'anyone'.
|
|
14
78
|
*/
|
|
15
|
-
constructor
|
|
79
|
+
constructor(rootKey: PrivateKey | 'anyone') {
|
|
16
80
|
if (rootKey === 'anyone') {
|
|
17
81
|
this.rootKey = new PrivateKey(1)
|
|
18
82
|
} else {
|
|
19
83
|
this.rootKey = rootKey
|
|
20
84
|
}
|
|
85
|
+
this.identityKey = this.rootKey.toPublicKey().toString()
|
|
21
86
|
}
|
|
22
87
|
|
|
23
88
|
/**
|
|
24
89
|
* Derives a public key based on protocol ID, key ID, and counterparty.
|
|
25
|
-
* @param {
|
|
90
|
+
* @param {WalletProtocol} protocolID - The protocol ID including a security level and protocol name.
|
|
26
91
|
* @param {string} keyID - The key identifier.
|
|
27
|
-
* @param {
|
|
92
|
+
* @param {Counterparty} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
|
|
28
93
|
* @param {boolean} [forSelf=false] - Whether deriving for self.
|
|
29
94
|
* @returns {PublicKey} - The derived public key.
|
|
30
95
|
*/
|
|
31
|
-
derivePublicKey
|
|
96
|
+
derivePublicKey(protocolID: WalletProtocol, keyID: string, counterparty: Counterparty, forSelf: boolean = false): PublicKey {
|
|
32
97
|
counterparty = this.normalizeCounterparty(counterparty)
|
|
33
98
|
if (forSelf) {
|
|
34
99
|
return this.rootKey.deriveChild(counterparty, this.computeInvoiceNumber(protocolID, keyID)).toPublicKey()
|
|
@@ -39,12 +104,12 @@ export default class KeyDeriver {
|
|
|
39
104
|
|
|
40
105
|
/**
|
|
41
106
|
* Derives a private key based on protocol ID, key ID, and counterparty.
|
|
42
|
-
* @param {
|
|
107
|
+
* @param {WalletProtocol} protocolID - The protocol ID including a security level and protocol name.
|
|
43
108
|
* @param {string} keyID - The key identifier.
|
|
44
|
-
* @param {
|
|
109
|
+
* @param {Counterparty} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
|
|
45
110
|
* @returns {PrivateKey} - The derived private key.
|
|
46
111
|
*/
|
|
47
|
-
derivePrivateKey
|
|
112
|
+
derivePrivateKey(protocolID: WalletProtocol, keyID: string, counterparty: Counterparty): PrivateKey {
|
|
48
113
|
counterparty = this.normalizeCounterparty(counterparty)
|
|
49
114
|
return this.rootKey.deriveChild(counterparty, this.computeInvoiceNumber(protocolID, keyID))
|
|
50
115
|
}
|
|
@@ -52,13 +117,13 @@ export default class KeyDeriver {
|
|
|
52
117
|
/**
|
|
53
118
|
* Derives a symmetric key based on protocol ID, key ID, and counterparty.
|
|
54
119
|
* Note: Symmetric keys should not be derivable by everyone due to security risks.
|
|
55
|
-
* @param {
|
|
120
|
+
* @param {WalletProtocol} protocolID - The protocol ID including a security level and protocol name.
|
|
56
121
|
* @param {string} keyID - The key identifier.
|
|
57
|
-
* @param {
|
|
122
|
+
* @param {Counterparty} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
|
|
58
123
|
* @returns {SymmetricKey} - The derived symmetric key.
|
|
59
124
|
* @throws {Error} - Throws an error if attempting to derive a symmetric key for 'anyone'.
|
|
60
125
|
*/
|
|
61
|
-
deriveSymmetricKey
|
|
126
|
+
deriveSymmetricKey(protocolID: WalletProtocol, keyID: string, counterparty: Counterparty): SymmetricKey {
|
|
62
127
|
if (counterparty === 'anyone') {
|
|
63
128
|
throw new Error(
|
|
64
129
|
'Symmetric keys (such as encryption keys or HMAC keys) should not be derivable by everyone, because messages would be decryptable by anyone who knows the identity public key of the user, and HMACs would be similarly forgeable.'
|
|
@@ -67,17 +132,17 @@ export default class KeyDeriver {
|
|
|
67
132
|
counterparty = this.normalizeCounterparty(counterparty)
|
|
68
133
|
const derivedPublicKey = this.derivePublicKey(protocolID, keyID, counterparty)
|
|
69
134
|
const derivedPrivateKey = this.derivePrivateKey(protocolID, keyID, counterparty)
|
|
70
|
-
return new SymmetricKey(derivedPrivateKey.deriveSharedSecret(derivedPublicKey).x
|
|
135
|
+
return new SymmetricKey(derivedPrivateKey.deriveSharedSecret(derivedPublicKey).x!.toArray())
|
|
71
136
|
}
|
|
72
137
|
|
|
73
138
|
/**
|
|
74
139
|
* Reveals the shared secret between the root key and the counterparty.
|
|
75
140
|
* Note: This should not be used for 'self'.
|
|
76
|
-
* @param {
|
|
141
|
+
* @param {Counterparty} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
|
|
77
142
|
* @returns {number[]} - The shared secret as a number array.
|
|
78
143
|
* @throws {Error} - Throws an error if attempting to reveal a shared secret for 'self'.
|
|
79
144
|
*/
|
|
80
|
-
revealCounterpartySecret
|
|
145
|
+
revealCounterpartySecret(counterparty: Counterparty): number[] {
|
|
81
146
|
if (counterparty === 'self') {
|
|
82
147
|
throw new Error('Counterparty secrets cannot be revealed for counterparty=self.')
|
|
83
148
|
}
|
|
@@ -97,12 +162,12 @@ export default class KeyDeriver {
|
|
|
97
162
|
|
|
98
163
|
/**
|
|
99
164
|
* Reveals the specific key association for a given protocol ID, key ID, and counterparty.
|
|
100
|
-
* @param {
|
|
101
|
-
* @param {
|
|
165
|
+
* @param {Counterparty} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
|
|
166
|
+
* @param {WalletProtocol} protocolID - The protocol ID including a security level and protocol name.
|
|
102
167
|
* @param {string} keyID - The key identifier.
|
|
103
168
|
* @returns {number[]} - The specific key association as a number array.
|
|
104
169
|
*/
|
|
105
|
-
revealSpecificSecret
|
|
170
|
+
revealSpecificSecret(counterparty: Counterparty, protocolID: WalletProtocol, keyID: string): number[] {
|
|
106
171
|
counterparty = this.normalizeCounterparty(counterparty)
|
|
107
172
|
const sharedSecret = this.rootKey.deriveSharedSecret(counterparty)
|
|
108
173
|
const invoiceNumberBin = Utils.toArray(this.computeInvoiceNumber(protocolID, keyID), 'utf8')
|
|
@@ -111,11 +176,11 @@ export default class KeyDeriver {
|
|
|
111
176
|
|
|
112
177
|
/**
|
|
113
178
|
* Normalizes the counterparty to a public key.
|
|
114
|
-
* @param {
|
|
179
|
+
* @param {Counterparty} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
|
|
115
180
|
* @returns {PublicKey} - The normalized counterparty public key.
|
|
116
181
|
* @throws {Error} - Throws an error if the counterparty is invalid.
|
|
117
182
|
*/
|
|
118
|
-
private normalizeCounterparty
|
|
183
|
+
private normalizeCounterparty(counterparty: Counterparty): PublicKey {
|
|
119
184
|
if (!counterparty) {
|
|
120
185
|
throw new Error('counterparty must be self, anyone or a public key!')
|
|
121
186
|
} else if (counterparty === 'self') {
|
|
@@ -131,12 +196,12 @@ export default class KeyDeriver {
|
|
|
131
196
|
|
|
132
197
|
/**
|
|
133
198
|
* Computes the invoice number based on the protocol ID and key ID.
|
|
134
|
-
* @param {
|
|
199
|
+
* @param {WalletProtocol} protocolID - The protocol ID including a security level and protocol name.
|
|
135
200
|
* @param {string} keyID - The key identifier.
|
|
136
201
|
* @returns {string} - The computed invoice number.
|
|
137
202
|
* @throws {Error} - Throws an error if protocol ID or key ID are invalid.
|
|
138
203
|
*/
|
|
139
|
-
private computeInvoiceNumber
|
|
204
|
+
private computeInvoiceNumber(protocolID: WalletProtocol, keyID: string): string {
|
|
140
205
|
const securityLevel = protocolID[0]
|
|
141
206
|
if (!Number.isInteger(securityLevel) || securityLevel < 0 || securityLevel > 2) {
|
|
142
207
|
throw new Error('Protocol security level must be 0, 1, or 2')
|
|
@@ -177,3 +242,5 @@ export default class KeyDeriver {
|
|
|
177
242
|
return `${securityLevel}-${protocolName}-${keyID}`
|
|
178
243
|
}
|
|
179
244
|
}
|
|
245
|
+
|
|
246
|
+
export default KeyDeriver
|