@bsv/sdk 1.1.32 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/mod.js +4 -0
- package/dist/cjs/mod.js.map +1 -1
- package/dist/cjs/package.json +4 -3
- package/dist/cjs/src/auth/Certificate.js +163 -0
- package/dist/cjs/src/auth/Certificate.js.map +1 -0
- package/dist/cjs/src/auth/index.js +9 -0
- package/dist/cjs/src/auth/index.js.map +1 -0
- package/dist/cjs/src/compat/BSM.js +17 -7
- package/dist/cjs/src/compat/BSM.js.map +1 -1
- package/dist/cjs/src/compat/ECIES.js +17 -7
- package/dist/cjs/src/compat/ECIES.js.map +1 -1
- package/dist/cjs/src/compat/HD.js +17 -7
- package/dist/cjs/src/compat/HD.js.map +1 -1
- package/dist/cjs/src/compat/Mnemonic.js +17 -7
- package/dist/cjs/src/compat/Mnemonic.js.map +1 -1
- package/dist/cjs/src/compat/index.js +17 -7
- package/dist/cjs/src/compat/index.js.map +1 -1
- package/dist/cjs/src/messages/index.js +17 -7
- package/dist/cjs/src/messages/index.js.map +1 -1
- package/dist/cjs/src/overlay-tools/LookupResolver.js +170 -0
- package/dist/cjs/src/overlay-tools/LookupResolver.js.map +1 -0
- package/dist/cjs/src/overlay-tools/OverlayAdminTokenTemplate.js +69 -0
- package/dist/cjs/src/overlay-tools/OverlayAdminTokenTemplate.js.map +1 -0
- package/dist/cjs/src/overlay-tools/SHIPBroadcaster.js +336 -0
- package/dist/cjs/src/overlay-tools/SHIPBroadcaster.js.map +1 -0
- package/dist/cjs/src/overlay-tools/index.js +29 -0
- package/dist/cjs/src/overlay-tools/index.js.map +1 -0
- package/dist/cjs/src/primitives/PrivateKey.js +17 -7
- package/dist/cjs/src/primitives/PrivateKey.js.map +1 -1
- package/dist/cjs/src/primitives/TransactionSignature.js +17 -7
- package/dist/cjs/src/primitives/TransactionSignature.js.map +1 -1
- package/dist/cjs/src/primitives/index.js +17 -7
- package/dist/cjs/src/primitives/index.js.map +1 -1
- package/dist/cjs/src/script/Spend.js +17 -7
- package/dist/cjs/src/script/Spend.js.map +1 -1
- package/dist/cjs/src/script/templates/PushDrop.js +218 -0
- package/dist/cjs/src/script/templates/PushDrop.js.map +1 -0
- package/dist/cjs/src/script/templates/index.js +3 -1
- package/dist/cjs/src/script/templates/index.js.map +1 -1
- package/dist/cjs/src/transaction/Beef.js +35 -6
- package/dist/cjs/src/transaction/Beef.js.map +1 -1
- package/dist/cjs/src/transaction/Transaction.js +13 -4
- package/dist/cjs/src/transaction/Transaction.js.map +1 -1
- package/dist/cjs/src/transaction/http/DefaultHttpClient.js +1 -1
- package/dist/cjs/src/transaction/http/DefaultHttpClient.js.map +1 -1
- package/dist/cjs/src/wallet/CachedKeyDeriver.js +177 -0
- package/dist/cjs/src/wallet/CachedKeyDeriver.js.map +1 -0
- package/dist/cjs/src/wallet/KeyDeriver.js +174 -0
- package/dist/cjs/src/wallet/KeyDeriver.js.map +1 -0
- package/dist/cjs/src/wallet/ProtoWallet.js +245 -0
- package/dist/cjs/src/wallet/ProtoWallet.js.map +1 -0
- package/dist/cjs/src/wallet/Wallet.interfaces.js +3 -0
- package/dist/cjs/src/wallet/Wallet.interfaces.js.map +1 -0
- package/dist/cjs/src/wallet/WalletClient.js +181 -0
- package/dist/cjs/src/wallet/WalletClient.js.map +1 -0
- package/dist/cjs/src/wallet/WalletError.js +28 -0
- package/dist/cjs/src/wallet/WalletError.js.map +1 -0
- package/dist/cjs/src/wallet/index.js +34 -0
- package/dist/cjs/src/wallet/index.js.map +1 -0
- package/dist/cjs/src/wallet/substrates/HTTPWalletWire.js +45 -0
- package/dist/cjs/src/wallet/substrates/HTTPWalletWire.js.map +1 -0
- package/dist/cjs/src/wallet/substrates/WalletWire.js +3 -0
- package/dist/cjs/src/wallet/substrates/WalletWire.js.map +1 -0
- package/dist/cjs/src/wallet/substrates/WalletWireCalls.js +36 -0
- package/dist/cjs/src/wallet/substrates/WalletWireCalls.js.map +1 -0
- package/dist/cjs/src/wallet/substrates/WalletWireProcessor.js +1821 -0
- package/dist/cjs/src/wallet/substrates/WalletWireProcessor.js.map +1 -0
- package/dist/cjs/src/wallet/substrates/WalletWireTransceiver.js +1305 -0
- package/dist/cjs/src/wallet/substrates/WalletWireTransceiver.js.map +1 -0
- package/dist/cjs/src/wallet/substrates/XDM.js +130 -0
- package/dist/cjs/src/wallet/substrates/XDM.js.map +1 -0
- package/dist/cjs/src/wallet/substrates/index.js +33 -0
- package/dist/cjs/src/wallet/substrates/index.js.map +1 -0
- package/dist/cjs/src/wallet/substrates/window.CWI.js +102 -0
- package/dist/cjs/src/wallet/substrates/window.CWI.js.map +1 -0
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/esm/mod.js +4 -0
- package/dist/esm/mod.js.map +1 -1
- package/dist/esm/src/auth/Certificate.js +185 -0
- package/dist/esm/src/auth/Certificate.js.map +1 -0
- package/dist/esm/src/auth/index.js +2 -0
- package/dist/esm/src/auth/index.js.map +1 -0
- package/dist/esm/src/overlay-tools/LookupResolver.js +167 -0
- package/dist/esm/src/overlay-tools/LookupResolver.js.map +1 -0
- package/dist/esm/src/overlay-tools/OverlayAdminTokenTemplate.js +64 -0
- package/dist/esm/src/overlay-tools/OverlayAdminTokenTemplate.js.map +1 -0
- package/dist/esm/src/overlay-tools/SHIPBroadcaster.js +335 -0
- package/dist/esm/src/overlay-tools/SHIPBroadcaster.js.map +1 -0
- package/dist/esm/src/overlay-tools/index.js +6 -0
- package/dist/esm/src/overlay-tools/index.js.map +1 -0
- package/dist/esm/src/script/templates/PushDrop.js +215 -0
- package/dist/esm/src/script/templates/PushDrop.js.map +1 -0
- package/dist/esm/src/script/templates/index.js +1 -0
- package/dist/esm/src/script/templates/index.js.map +1 -1
- package/dist/esm/src/transaction/Beef.js +35 -6
- package/dist/esm/src/transaction/Beef.js.map +1 -1
- package/dist/esm/src/transaction/Transaction.js +13 -4
- package/dist/esm/src/transaction/Transaction.js.map +1 -1
- package/dist/esm/src/transaction/http/DefaultHttpClient.js +1 -1
- package/dist/esm/src/transaction/http/DefaultHttpClient.js.map +1 -1
- package/dist/esm/src/wallet/CachedKeyDeriver.js +174 -0
- package/dist/esm/src/wallet/CachedKeyDeriver.js.map +1 -0
- package/dist/esm/src/wallet/KeyDeriver.js +172 -0
- package/dist/esm/src/wallet/KeyDeriver.js.map +1 -0
- package/dist/esm/src/wallet/ProtoWallet.js +207 -0
- package/dist/esm/src/wallet/ProtoWallet.js.map +1 -0
- package/dist/esm/src/wallet/Wallet.interfaces.js +2 -0
- package/dist/esm/src/wallet/Wallet.interfaces.js.map +1 -0
- package/dist/esm/src/wallet/WalletClient.js +177 -0
- package/dist/esm/src/wallet/WalletClient.js.map +1 -0
- package/dist/esm/src/wallet/WalletError.js +25 -0
- package/dist/esm/src/wallet/WalletError.js.map +1 -0
- package/dist/esm/src/wallet/index.js +9 -0
- package/dist/esm/src/wallet/index.js.map +1 -0
- package/dist/esm/src/wallet/substrates/HTTPWalletWire.js +42 -0
- package/dist/esm/src/wallet/substrates/HTTPWalletWire.js.map +1 -0
- package/dist/esm/src/wallet/substrates/WalletWire.js +2 -0
- package/dist/esm/src/wallet/substrates/WalletWire.js.map +1 -0
- package/dist/esm/src/wallet/substrates/WalletWireCalls.js +34 -0
- package/dist/esm/src/wallet/substrates/WalletWireCalls.js.map +1 -0
- package/dist/esm/src/wallet/substrates/WalletWireProcessor.js +1816 -0
- package/dist/esm/src/wallet/substrates/WalletWireProcessor.js.map +1 -0
- package/dist/esm/src/wallet/substrates/WalletWireTransceiver.js +1300 -0
- package/dist/esm/src/wallet/substrates/WalletWireTransceiver.js.map +1 -0
- package/dist/esm/src/wallet/substrates/XDM.js +128 -0
- package/dist/esm/src/wallet/substrates/XDM.js.map +1 -0
- package/dist/esm/src/wallet/substrates/index.js +8 -0
- package/dist/esm/src/wallet/substrates/index.js.map +1 -0
- package/dist/esm/src/wallet/substrates/window.CWI.js +100 -0
- package/dist/esm/src/wallet/substrates/window.CWI.js.map +1 -0
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/types/mod.d.ts +4 -0
- package/dist/types/mod.d.ts.map +1 -1
- package/dist/types/src/auth/Certificate.d.ts +76 -0
- package/dist/types/src/auth/Certificate.d.ts.map +1 -0
- package/dist/types/src/auth/index.d.ts +2 -0
- package/dist/types/src/auth/index.d.ts.map +1 -0
- package/dist/types/src/overlay-tools/LookupResolver.d.ts +71 -0
- package/dist/types/src/overlay-tools/LookupResolver.d.ts.map +1 -0
- package/dist/types/src/overlay-tools/OverlayAdminTokenTemplate.d.ts +44 -0
- package/dist/types/src/overlay-tools/OverlayAdminTokenTemplate.d.ts.map +1 -0
- package/dist/types/src/overlay-tools/SHIPBroadcaster.d.ts +90 -0
- package/dist/types/src/overlay-tools/SHIPBroadcaster.d.ts.map +1 -0
- package/dist/types/src/overlay-tools/index.d.ts +6 -0
- package/dist/types/src/overlay-tools/index.d.ts.map +1 -0
- package/dist/types/src/script/templates/PushDrop.d.ts +53 -0
- package/dist/types/src/script/templates/PushDrop.d.ts.map +1 -0
- package/dist/types/src/script/templates/index.d.ts +1 -0
- package/dist/types/src/script/templates/index.d.ts.map +1 -1
- package/dist/types/src/transaction/Beef.d.ts +16 -1
- package/dist/types/src/transaction/Beef.d.ts.map +1 -1
- package/dist/types/src/transaction/Transaction.d.ts.map +1 -1
- package/dist/types/src/wallet/CachedKeyDeriver.d.ts +92 -0
- package/dist/types/src/wallet/CachedKeyDeriver.d.ts.map +1 -0
- package/dist/types/src/wallet/KeyDeriver.d.ts +72 -0
- package/dist/types/src/wallet/KeyDeriver.d.ts.map +1 -0
- package/dist/types/src/wallet/ProtoWallet.d.ts +415 -0
- package/dist/types/src/wallet/ProtoWallet.d.ts.map +1 -0
- package/dist/types/src/wallet/Wallet.interfaces.d.ts +996 -0
- package/dist/types/src/wallet/Wallet.interfaces.d.ts.map +1 -0
- package/dist/types/src/wallet/WalletClient.d.ts +182 -0
- package/dist/types/src/wallet/WalletClient.d.ts.map +1 -0
- package/dist/types/src/wallet/WalletError.d.ts +14 -0
- package/dist/types/src/wallet/WalletError.d.ts.map +1 -0
- package/dist/types/src/wallet/index.d.ts +9 -0
- package/dist/types/src/wallet/index.d.ts.map +1 -0
- package/dist/types/src/wallet/substrates/HTTPWalletWire.d.ts +9 -0
- package/dist/types/src/wallet/substrates/HTTPWalletWire.d.ts.map +1 -0
- package/dist/types/src/wallet/substrates/WalletWire.d.ts +7 -0
- package/dist/types/src/wallet/substrates/WalletWire.d.ts.map +1 -0
- package/dist/types/src/wallet/substrates/WalletWireCalls.d.ts +33 -0
- package/dist/types/src/wallet/substrates/WalletWireCalls.d.ts.map +1 -0
- package/dist/types/src/wallet/substrates/WalletWireProcessor.d.ts +18 -0
- package/dist/types/src/wallet/substrates/WalletWireProcessor.d.ts.map +1 -0
- package/dist/types/src/wallet/substrates/WalletWireTransceiver.d.ts +196 -0
- package/dist/types/src/wallet/substrates/WalletWireTransceiver.d.ts.map +1 -0
- package/dist/types/src/wallet/substrates/XDM.d.ts +412 -0
- package/dist/types/src/wallet/substrates/XDM.d.ts.map +1 -0
- package/dist/types/src/wallet/substrates/index.d.ts +8 -0
- package/dist/types/src/wallet/substrates/index.d.ts.map +1 -0
- package/dist/types/src/wallet/substrates/window.CWI.d.ts +410 -0
- package/dist/types/src/wallet/substrates/window.CWI.d.ts.map +1 -0
- package/dist/types/tsconfig.types.tsbuildinfo +1 -1
- package/dist/umd/bundle.js +1 -1
- package/docs/overlay-tools.md +551 -0
- package/docs/script.md +135 -0
- package/docs/totp.md +119 -0
- package/docs/transaction.md +25 -0
- package/docs/wallet-substrates.md +10 -0
- package/docs/wallet.md +4182 -0
- package/mod.ts +5 -1
- package/package.json +44 -3
- package/src/auth/Certificate.ts +233 -0
- package/src/auth/__tests/Certificate.test.ts +282 -0
- package/src/auth/index.ts +1 -0
- package/src/overlay-tools/LookupResolver.ts +228 -0
- package/src/overlay-tools/OverlayAdminTokenTemplate.ts +79 -0
- package/src/overlay-tools/SHIPBroadcaster.ts +405 -0
- package/src/overlay-tools/__tests/LookupResolver.test.ts +1403 -0
- package/src/overlay-tools/__tests/OverlayAdminTokenTemplate.test.ts +69 -0
- package/src/overlay-tools/__tests/SHIPBroadcaster.test.ts +904 -0
- package/src/overlay-tools/index.ts +5 -0
- package/src/script/templates/PushDrop.ts +246 -0
- package/src/script/templates/__tests/PushDrop.test.ts +158 -0
- package/src/script/templates/index.ts +1 -0
- package/src/transaction/Beef.ts +36 -6
- package/src/transaction/Transaction.ts +13 -4
- package/src/transaction/__tests/Beef.test.ts +20 -6
- package/src/transaction/http/DefaultHttpClient.ts +1 -1
- package/src/wallet/CachedKeyDeriver.ts +193 -0
- package/src/wallet/KeyDeriver.ts +178 -0
- package/src/wallet/ProtoWallet.ts +732 -0
- package/src/wallet/Wallet.interfaces.ts +1170 -0
- package/src/wallet/WalletClient.ts +201 -0
- package/src/wallet/WalletError.ts +27 -0
- package/src/wallet/__tests/CachedKeyDeriver.test.ts +322 -0
- package/src/wallet/__tests/KeyDeriver.test.ts +118 -0
- package/src/wallet/__tests/ProtoWallet.test.ts +543 -0
- package/src/wallet/index.ts +8 -0
- package/src/wallet/substrates/HTTPWalletWire.ts +47 -0
- package/src/wallet/substrates/WalletWire.ts +6 -0
- package/src/wallet/substrates/WalletWireCalls.ts +34 -0
- package/src/wallet/substrates/WalletWireProcessor.ts +2046 -0
- package/src/wallet/substrates/WalletWireTransceiver.ts +1454 -0
- package/src/wallet/substrates/XDM.ts +157 -0
- package/src/wallet/substrates/__tests/WalletWire.integration.test.ts +2194 -0
- package/src/wallet/substrates/__tests/XDM.test.ts +659 -0
- package/src/wallet/substrates/index.ts +7 -0
- package/src/wallet/substrates/window.CWI.ts +133 -0
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { PrivateKey, PublicKey, SymmetricKey } from '../primitives/index.js'
|
|
2
|
+
import KeyDeriver from './KeyDeriver.js'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A cached version of KeyDeriver that caches the results of key derivation methods.
|
|
6
|
+
* This is useful for optimizing performance when the same keys are derived multiple times.
|
|
7
|
+
* It supports configurable cache size with sane defaults and maintains cache entries using LRU (Least Recently Used) eviction policy.
|
|
8
|
+
*/
|
|
9
|
+
export default class CachedKeyDeriver {
|
|
10
|
+
private readonly keyDeriver: KeyDeriver
|
|
11
|
+
private readonly cache: Map<string, any>
|
|
12
|
+
private readonly maxCacheSize: number
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Initializes the CachedKeyDeriver instance with a root private key and optional cache settings.
|
|
16
|
+
* @param {PrivateKey | 'anyone'} rootKey - The root private key or the string 'anyone'.
|
|
17
|
+
* @param {Object} [options] - Optional settings for the cache.
|
|
18
|
+
* @param {number} [options.maxCacheSize=1000] - The maximum number of entries to store in the cache.
|
|
19
|
+
*/
|
|
20
|
+
constructor(rootKey: PrivateKey | 'anyone', options?: { maxCacheSize?: number }) {
|
|
21
|
+
this.keyDeriver = new KeyDeriver(rootKey)
|
|
22
|
+
this.cache = new Map<string, any>()
|
|
23
|
+
this.maxCacheSize = options?.maxCacheSize || 1000
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Derives a public key based on protocol ID, key ID, and counterparty.
|
|
28
|
+
* Caches the result for future calls with the same parameters.
|
|
29
|
+
* @param {[0 | 1 | 2, string]} protocolID - The protocol ID including a security level and protocol name.
|
|
30
|
+
* @param {string} keyID - The key identifier.
|
|
31
|
+
* @param {PublicKey | string | 'self' | 'anyone'} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
|
|
32
|
+
* @param {boolean} [forSelf=false] - Whether deriving for self.
|
|
33
|
+
* @returns {PublicKey} - The derived public key.
|
|
34
|
+
*/
|
|
35
|
+
derivePublicKey(
|
|
36
|
+
protocolID: [0 | 1 | 2, string],
|
|
37
|
+
keyID: string,
|
|
38
|
+
counterparty: PublicKey | string | 'self' | 'anyone',
|
|
39
|
+
forSelf: boolean = false
|
|
40
|
+
): PublicKey {
|
|
41
|
+
const cacheKey = this.generateCacheKey('derivePublicKey', protocolID, keyID, counterparty, forSelf)
|
|
42
|
+
if (this.cache.has(cacheKey)) {
|
|
43
|
+
return this.cacheGet(cacheKey)
|
|
44
|
+
} else {
|
|
45
|
+
const result = this.keyDeriver.derivePublicKey(protocolID, keyID, counterparty, forSelf)
|
|
46
|
+
this.cacheSet(cacheKey, result)
|
|
47
|
+
return result
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Derives a private key based on protocol ID, key ID, and counterparty.
|
|
53
|
+
* Caches the result for future calls with the same parameters.
|
|
54
|
+
* @param {[0 | 1 | 2, string]} protocolID - The protocol ID including a security level and protocol name.
|
|
55
|
+
* @param {string} keyID - The key identifier.
|
|
56
|
+
* @param {PublicKey | string | 'self' | 'anyone'} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
|
|
57
|
+
* @returns {PrivateKey} - The derived private key.
|
|
58
|
+
*/
|
|
59
|
+
derivePrivateKey(
|
|
60
|
+
protocolID: [0 | 1 | 2, string],
|
|
61
|
+
keyID: string,
|
|
62
|
+
counterparty: PublicKey | string | 'self' | 'anyone'
|
|
63
|
+
): PrivateKey {
|
|
64
|
+
const cacheKey = this.generateCacheKey('derivePrivateKey', protocolID, keyID, counterparty)
|
|
65
|
+
if (this.cache.has(cacheKey)) {
|
|
66
|
+
return this.cacheGet(cacheKey)
|
|
67
|
+
} else {
|
|
68
|
+
const result = this.keyDeriver.derivePrivateKey(protocolID, keyID, counterparty)
|
|
69
|
+
this.cacheSet(cacheKey, result)
|
|
70
|
+
return result
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Derives a symmetric key based on protocol ID, key ID, and counterparty.
|
|
76
|
+
* Caches the result for future calls with the same parameters.
|
|
77
|
+
* @param {[0 | 1 | 2, string]} protocolID - The protocol ID including a security level and protocol name.
|
|
78
|
+
* @param {string} keyID - The key identifier.
|
|
79
|
+
* @param {PublicKey | string | 'self' | 'anyone'} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
|
|
80
|
+
* @returns {SymmetricKey} - The derived symmetric key.
|
|
81
|
+
* @throws {Error} - Throws an error if attempting to derive a symmetric key for 'anyone'.
|
|
82
|
+
*/
|
|
83
|
+
deriveSymmetricKey(
|
|
84
|
+
protocolID: [0 | 1 | 2, string],
|
|
85
|
+
keyID: string,
|
|
86
|
+
counterparty: PublicKey | string | 'self' | 'anyone'
|
|
87
|
+
): SymmetricKey {
|
|
88
|
+
const cacheKey = this.generateCacheKey('deriveSymmetricKey', protocolID, keyID, counterparty)
|
|
89
|
+
if (this.cache.has(cacheKey)) {
|
|
90
|
+
return this.cacheGet(cacheKey)
|
|
91
|
+
} else {
|
|
92
|
+
const result = this.keyDeriver.deriveSymmetricKey(protocolID, keyID, counterparty)
|
|
93
|
+
this.cacheSet(cacheKey, result)
|
|
94
|
+
return result
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Reveals the shared secret between the root key and the counterparty.
|
|
100
|
+
* Caches the result for future calls with the same parameters.
|
|
101
|
+
* @param {PublicKey | string | 'self' | 'anyone'} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
|
|
102
|
+
* @returns {number[]} - The shared secret as a number array.
|
|
103
|
+
* @throws {Error} - Throws an error if attempting to reveal a shared secret for 'self'.
|
|
104
|
+
*/
|
|
105
|
+
revealCounterpartySecret(counterparty: PublicKey | string | 'self' | 'anyone'): number[] {
|
|
106
|
+
const cacheKey = this.generateCacheKey('revealCounterpartySecret', counterparty)
|
|
107
|
+
if (this.cache.has(cacheKey)) {
|
|
108
|
+
return this.cacheGet(cacheKey)
|
|
109
|
+
} else {
|
|
110
|
+
const result = this.keyDeriver.revealCounterpartySecret(counterparty)
|
|
111
|
+
this.cacheSet(cacheKey, result)
|
|
112
|
+
return result
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Reveals the specific key association for a given protocol ID, key ID, and counterparty.
|
|
118
|
+
* Caches the result for future calls with the same parameters.
|
|
119
|
+
* @param {PublicKey | string | 'self' | 'anyone'} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
|
|
120
|
+
* @param {[0 | 1 | 2, string]} protocolID - The protocol ID including a security level and protocol name.
|
|
121
|
+
* @param {string} keyID - The key identifier.
|
|
122
|
+
* @returns {number[]} - The specific key association as a number array.
|
|
123
|
+
*/
|
|
124
|
+
revealSpecificSecret(
|
|
125
|
+
counterparty: PublicKey | string | 'self' | 'anyone',
|
|
126
|
+
protocolID: [0 | 1 | 2, string],
|
|
127
|
+
keyID: string
|
|
128
|
+
): number[] {
|
|
129
|
+
const cacheKey = this.generateCacheKey('revealSpecificSecret', counterparty, protocolID, keyID)
|
|
130
|
+
if (this.cache.has(cacheKey)) {
|
|
131
|
+
return this.cacheGet(cacheKey)
|
|
132
|
+
} else {
|
|
133
|
+
const result = this.keyDeriver.revealSpecificSecret(counterparty, protocolID, keyID)
|
|
134
|
+
this.cacheSet(cacheKey, result)
|
|
135
|
+
return result
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Generates a unique cache key based on the method name and input parameters.
|
|
141
|
+
* @param {string} methodName - The name of the method.
|
|
142
|
+
* @param {...any} args - The arguments passed to the method.
|
|
143
|
+
* @returns {string} - The generated cache key.
|
|
144
|
+
*/
|
|
145
|
+
private generateCacheKey(methodName: string, ...args: any[]): string {
|
|
146
|
+
const serializedArgs = args.map((arg) => this.serializeArgument(arg)).join('|')
|
|
147
|
+
return `${methodName}|${serializedArgs}`
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Serializes an argument to a string for use in a cache key.
|
|
152
|
+
* @param {any} arg - The argument to serialize.
|
|
153
|
+
* @returns {string} - The serialized argument.
|
|
154
|
+
*/
|
|
155
|
+
private serializeArgument(arg: any): string {
|
|
156
|
+
if (arg instanceof PublicKey || arg instanceof PrivateKey) {
|
|
157
|
+
return arg.toString()
|
|
158
|
+
} else if (Array.isArray(arg)) {
|
|
159
|
+
return arg.map((item) => this.serializeArgument(item)).join(',')
|
|
160
|
+
} else if (typeof arg === 'object' && arg !== null) {
|
|
161
|
+
return JSON.stringify(arg)
|
|
162
|
+
} else {
|
|
163
|
+
return String(arg)
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Retrieves an item from the cache and updates its position to reflect recent use.
|
|
169
|
+
* @param {string} cacheKey - The key of the cached item.
|
|
170
|
+
* @returns {any} - The cached value.
|
|
171
|
+
*/
|
|
172
|
+
private cacheGet(cacheKey: string): any {
|
|
173
|
+
const value = this.cache.get(cacheKey)
|
|
174
|
+
// Update the entry to reflect recent use
|
|
175
|
+
this.cache.delete(cacheKey)
|
|
176
|
+
this.cache.set(cacheKey, value)
|
|
177
|
+
return value
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Adds an item to the cache and evicts the least recently used item if necessary.
|
|
182
|
+
* @param {string} cacheKey - The key of the item to cache.
|
|
183
|
+
* @param {any} value - The value to cache.
|
|
184
|
+
*/
|
|
185
|
+
private cacheSet(cacheKey: string, value: any): void {
|
|
186
|
+
if (this.cache.size >= this.maxCacheSize) {
|
|
187
|
+
// Evict the least recently used item (first item in Map)
|
|
188
|
+
const firstKey = this.cache.keys().next().value
|
|
189
|
+
this.cache.delete(firstKey)
|
|
190
|
+
}
|
|
191
|
+
this.cache.set(cacheKey, value)
|
|
192
|
+
}
|
|
193
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { PrivateKey, PublicKey, SymmetricKey, Hash, Utils } from '../primitives/index.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Class responsible for deriving various types of keys using a root private key.
|
|
5
|
+
* It supports deriving public and private keys, symmetric keys, and revealing key linkages.
|
|
6
|
+
*/
|
|
7
|
+
export default class KeyDeriver {
|
|
8
|
+
rootKey: PrivateKey
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Initializes the KeyDeriver instance with a root private key.
|
|
12
|
+
* @param {PrivateKey | 'anyone'} rootKey - The root private key or the string 'anyone'.
|
|
13
|
+
*/
|
|
14
|
+
constructor(rootKey: PrivateKey | 'anyone') {
|
|
15
|
+
if (rootKey === 'anyone') {
|
|
16
|
+
this.rootKey = new PrivateKey(1)
|
|
17
|
+
} else {
|
|
18
|
+
this.rootKey = rootKey
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Derives a public key based on protocol ID, key ID, and counterparty.
|
|
24
|
+
* @param {[0 | 1 | 2, string]} protocolID - The protocol ID including a security level and protocol name.
|
|
25
|
+
* @param {string} keyID - The key identifier.
|
|
26
|
+
* @param {PublicKey | string | 'self' | 'anyone'} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
|
|
27
|
+
* @param {boolean} [forSelf=false] - Whether deriving for self.
|
|
28
|
+
* @returns {PublicKey} - The derived public key.
|
|
29
|
+
*/
|
|
30
|
+
derivePublicKey(protocolID: [0 | 1 | 2, string], keyID: string, counterparty: PublicKey | string | 'self' | 'anyone', forSelf: boolean = false): PublicKey {
|
|
31
|
+
counterparty = this.normalizeCounterparty(counterparty)
|
|
32
|
+
if (forSelf) {
|
|
33
|
+
return this.rootKey.deriveChild(counterparty, this.computeInvoiceNumber(protocolID, keyID)).toPublicKey()
|
|
34
|
+
} else {
|
|
35
|
+
return counterparty.deriveChild(this.rootKey, this.computeInvoiceNumber(protocolID, keyID))
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Derives a private key based on protocol ID, key ID, and counterparty.
|
|
41
|
+
* @param {[0 | 1 | 2, string]} protocolID - The protocol ID including a security level and protocol name.
|
|
42
|
+
* @param {string} keyID - The key identifier.
|
|
43
|
+
* @param {PublicKey | string | 'self' | 'anyone'} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
|
|
44
|
+
* @returns {PrivateKey} - The derived private key.
|
|
45
|
+
*/
|
|
46
|
+
derivePrivateKey(protocolID: [0 | 1 | 2, string], keyID: string, counterparty: PublicKey | string | 'self' | 'anyone'): PrivateKey {
|
|
47
|
+
counterparty = this.normalizeCounterparty(counterparty)
|
|
48
|
+
return this.rootKey.deriveChild(counterparty, this.computeInvoiceNumber(protocolID, keyID))
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Derives a symmetric key based on protocol ID, key ID, and counterparty.
|
|
53
|
+
* Note: Symmetric keys should not be derivable by everyone due to security risks.
|
|
54
|
+
* @param {[0 | 1 | 2, string]} protocolID - The protocol ID including a security level and protocol name.
|
|
55
|
+
* @param {string} keyID - The key identifier.
|
|
56
|
+
* @param {PublicKey | string | 'self' | 'anyone'} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
|
|
57
|
+
* @returns {SymmetricKey} - The derived symmetric key.
|
|
58
|
+
* @throws {Error} - Throws an error if attempting to derive a symmetric key for 'anyone'.
|
|
59
|
+
*/
|
|
60
|
+
deriveSymmetricKey(protocolID: [0 | 1 | 2, string], keyID: string, counterparty: PublicKey | string | 'self' | 'anyone'): SymmetricKey {
|
|
61
|
+
if (counterparty === 'anyone') {
|
|
62
|
+
throw new Error(
|
|
63
|
+
'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.'
|
|
64
|
+
)
|
|
65
|
+
}
|
|
66
|
+
counterparty = this.normalizeCounterparty(counterparty)
|
|
67
|
+
const derivedPublicKey = this.derivePublicKey(protocolID, keyID, counterparty)
|
|
68
|
+
const derivedPrivateKey = this.derivePrivateKey(protocolID, keyID, counterparty)
|
|
69
|
+
return new SymmetricKey(derivedPrivateKey.deriveSharedSecret(derivedPublicKey).x.toArray())
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Reveals the shared secret between the root key and the counterparty.
|
|
74
|
+
* Note: This should not be used for 'self'.
|
|
75
|
+
* @param {PublicKey | string | 'self' | 'anyone'} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
|
|
76
|
+
* @returns {number[]} - The shared secret as a number array.
|
|
77
|
+
* @throws {Error} - Throws an error if attempting to reveal a shared secret for 'self'.
|
|
78
|
+
*/
|
|
79
|
+
revealCounterpartySecret(counterparty: PublicKey | string | 'self' | 'anyone'): number[] {
|
|
80
|
+
if (counterparty === 'self') {
|
|
81
|
+
throw new Error('Counterparty secrets cannot be revealed for counterparty=self.')
|
|
82
|
+
}
|
|
83
|
+
counterparty = this.normalizeCounterparty(counterparty)
|
|
84
|
+
|
|
85
|
+
// Double-check to ensure not revealing the secret for 'self'
|
|
86
|
+
const self = this.rootKey.toPublicKey()
|
|
87
|
+
const keyDerivedBySelf = this.rootKey.deriveChild(self, 'test').toHex()
|
|
88
|
+
const keyDerivedByCounterparty = this.rootKey.deriveChild(counterparty, 'test').toHex()
|
|
89
|
+
|
|
90
|
+
if (keyDerivedBySelf === keyDerivedByCounterparty) {
|
|
91
|
+
throw new Error('Counterparty secrets cannot be revealed for counterparty=self.')
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return this.rootKey.deriveSharedSecret(counterparty).encode(true) as number[]
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Reveals the specific key association for a given protocol ID, key ID, and counterparty.
|
|
99
|
+
* @param {PublicKey | string | 'self' | 'anyone'} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
|
|
100
|
+
* @param {[0 | 1 | 2, string]} protocolID - The protocol ID including a security level and protocol name.
|
|
101
|
+
* @param {string} keyID - The key identifier.
|
|
102
|
+
* @returns {number[]} - The specific key association as a number array.
|
|
103
|
+
*/
|
|
104
|
+
revealSpecificSecret(counterparty: PublicKey | string | 'self' | 'anyone', protocolID: [0 | 1 | 2, string], keyID: string): number[] {
|
|
105
|
+
counterparty = this.normalizeCounterparty(counterparty)
|
|
106
|
+
const sharedSecret = this.rootKey.deriveSharedSecret(counterparty)
|
|
107
|
+
const invoiceNumberBin = Utils.toArray(this.computeInvoiceNumber(protocolID, keyID), 'utf8')
|
|
108
|
+
return Hash.sha256hmac(sharedSecret.encode(true), invoiceNumberBin)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Normalizes the counterparty to a public key.
|
|
113
|
+
* @param {PublicKey | string | 'self' | 'anyone'} counterparty - The counterparty's public key or a predefined value ('self' or 'anyone').
|
|
114
|
+
* @returns {PublicKey} - The normalized counterparty public key.
|
|
115
|
+
* @throws {Error} - Throws an error if the counterparty is invalid.
|
|
116
|
+
*/
|
|
117
|
+
private normalizeCounterparty(counterparty: PublicKey | string | 'self' | 'anyone'): PublicKey {
|
|
118
|
+
if (!counterparty) {
|
|
119
|
+
throw new Error('counterparty must be self, anyone or a public key!')
|
|
120
|
+
} else if (counterparty === 'self') {
|
|
121
|
+
return this.rootKey.toPublicKey()
|
|
122
|
+
} else if (counterparty === 'anyone') {
|
|
123
|
+
return new PrivateKey(1).toPublicKey()
|
|
124
|
+
} else if (typeof counterparty === 'string') {
|
|
125
|
+
return PublicKey.fromString(counterparty)
|
|
126
|
+
} else {
|
|
127
|
+
return counterparty
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Computes the invoice number based on the protocol ID and key ID.
|
|
133
|
+
* @param {[0 | 1 | 2, string]} protocolID - The protocol ID including a security level and protocol name.
|
|
134
|
+
* @param {string} keyID - The key identifier.
|
|
135
|
+
* @returns {string} - The computed invoice number.
|
|
136
|
+
* @throws {Error} - Throws an error if protocol ID or key ID are invalid.
|
|
137
|
+
*/
|
|
138
|
+
private computeInvoiceNumber(protocolID: [0 | 1 | 2, string], keyID: string): string {
|
|
139
|
+
const securityLevel = protocolID[0]
|
|
140
|
+
if (!Number.isInteger(securityLevel) || securityLevel < 0 || securityLevel > 2) {
|
|
141
|
+
throw new Error('Protocol security level must be 0, 1, or 2')
|
|
142
|
+
}
|
|
143
|
+
const protocolName = protocolID[1].toLowerCase().trim()
|
|
144
|
+
if (keyID.length > 800) {
|
|
145
|
+
throw new Error('Key IDs must be 800 characters or less')
|
|
146
|
+
}
|
|
147
|
+
if (keyID.length < 1) {
|
|
148
|
+
throw new Error('Key IDs must be 1 character or more')
|
|
149
|
+
}
|
|
150
|
+
if (protocolName.length > 400) {
|
|
151
|
+
// Specific linkage revelation is the only protocol ID that can contain another protocol ID.
|
|
152
|
+
// Therefore, we allow it to be long enough to encapsulate the target protocol
|
|
153
|
+
if (protocolName.startsWith('specific linkage revelation ')) {
|
|
154
|
+
// The format is: 'specific linkage revelation x YYYYY'
|
|
155
|
+
// Where: x is the security level and YYYYY is the target protocol
|
|
156
|
+
// Thus, the max acceptable length is 30 + 400 = 430 bytes
|
|
157
|
+
if (protocolName.length > 430) {
|
|
158
|
+
throw new Error('Specific linkage revelation protocol names must be 430 characters or less')
|
|
159
|
+
}
|
|
160
|
+
} else {
|
|
161
|
+
throw new Error('Protocol names must be 400 characters or less')
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
if (protocolName.length < 5) {
|
|
165
|
+
throw new Error('Protocol names must be 5 characters or more')
|
|
166
|
+
}
|
|
167
|
+
if (protocolName.includes(' ')) {
|
|
168
|
+
throw new Error('Protocol names cannot contain multiple consecutive spaces (" ")')
|
|
169
|
+
}
|
|
170
|
+
if (!/^[a-z0-9 ]+$/g.test(protocolName)) {
|
|
171
|
+
throw new Error('Protocol names can only contain letters, numbers and spaces')
|
|
172
|
+
}
|
|
173
|
+
if (protocolName.endsWith(' protocol')) {
|
|
174
|
+
throw new Error('No need to end your protocol name with " protocol"')
|
|
175
|
+
}
|
|
176
|
+
return `${securityLevel}-${protocolName}-${keyID}`
|
|
177
|
+
}
|
|
178
|
+
}
|