|
1 | 1 | import * as statics from '@bitgo/statics'; |
2 | 2 | import * as utxolib from '@bitgo/utxo-lib'; |
| 3 | +import { randomBytes } from 'crypto'; |
3 | 4 | import { importMacaroon, bytesToBase64 } from 'macaroon'; |
4 | 5 | import * as bs58check from 'bs58check'; |
5 | 6 | import * as sdkcore from '@bitgo/sdk-core'; |
@@ -208,30 +209,30 @@ export function deriveLightningServiceSharedSecret(coinName: 'lnbtc' | 'tlnbtc', |
208 | 209 | } |
209 | 210 |
|
210 | 211 | /** |
211 | | - * Derives the shared secret for the middleware using the user's auth extended private key and the middleware's public key. |
212 | | - * This is used for secure communication between the middleware and the user. |
| 212 | + * Derives the shared secret for the middleware using a private key and the middleware's public key. |
213 | 213 | */ |
214 | | -export function deriveMiddlewareSharedSecret(coinName: 'lnbtc' | 'tlnbtc', userXprv: string): Buffer { |
| 214 | +export function deriveMiddlewareSharedSecret(coinName: 'lnbtc' | 'tlnbtc', xprv: string): Buffer { |
215 | 215 | const publicKey = Buffer.from(getStaticsLightningNetwork(coinName).middlewarePubKey, 'hex'); |
216 | | - const userAuthHdNode = utxolib.bip32.fromBase58(userXprv); |
| 216 | + const userAuthHdNode = utxolib.bip32.fromBase58(xprv); |
217 | 217 | return sdkcore.getSharedSecret(userAuthHdNode, publicKey); |
218 | 218 | } |
219 | 219 |
|
220 | 220 | /** |
221 | | - * Derives the shared secret for TAT service using the user's private key and the TAT public key. |
222 | | - * This is used for secure communication with the TAT service and the user. |
| 221 | + * Derives the shared secret for TAT service using ta private key and the TAT public key. |
223 | 222 | */ |
224 | | -export function deriveTatSharedSecret(coinName: 'lnbtc' | 'tlnbtc', userXprv: string): Buffer { |
| 223 | +export function deriveTatSharedSecret(coinName: 'lnbtc' | 'tlnbtc', xprv: string): Buffer { |
225 | 224 | const publicKey = Buffer.from(getStaticsLightningNetwork(coinName).tatPubKey, 'hex'); |
226 | | - const userAuthHdNode = utxolib.bip32.fromBase58(userXprv); |
| 225 | + const userAuthHdNode = utxolib.bip32.fromBase58(xprv); |
227 | 226 | return sdkcore.getSharedSecret(userAuthHdNode, publicKey); |
228 | 227 | } |
229 | 228 |
|
230 | 229 | /** |
231 | 230 | * Given a seed, compute a BIP32 derivation index. |
232 | 231 | * 0 <= index < 4294967295 (largest 4 byte number) |
233 | | - * @param seed |
| 232 | + * @param seed (optional) If nothing provided, we will generate one randomly |
234 | 233 | */ |
235 | | -export function computeBip32DerivationIndexFromSeed(seed: string): number { |
236 | | - return Buffer.from(utxolib.crypto.sha256(Buffer.from(seed, 'utf8'))).readUint32BE(0); |
| 234 | +export function computeBip32DerivationIndexFromSeed(seed?: string): number { |
| 235 | + return Buffer.from(utxolib.crypto.sha256(Buffer.from(seed ?? randomBytes(32).toString('hex'), 'utf8'))).readUint32BE( |
| 236 | + 0 |
| 237 | + ); |
237 | 238 | } |
0 commit comments