|
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,32 @@ 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 | + * Derives the shared secret for the middleware using a private key and the middleware's public key. |
212 | 213 | * This is used for secure communication between the middleware and the user.
|
213 | 214 | */
|
214 |
| -export function deriveMiddlewareSharedSecret(coinName: 'lnbtc' | 'tlnbtc', userXprv: string): Buffer { |
| 215 | +export function deriveMiddlewareSharedSecret(coinName: 'lnbtc' | 'tlnbtc', xprv: string): Buffer { |
215 | 216 | const publicKey = Buffer.from(getStaticsLightningNetwork(coinName).middlewarePubKey, 'hex');
|
216 |
| - const userAuthHdNode = utxolib.bip32.fromBase58(userXprv); |
| 217 | + const userAuthHdNode = utxolib.bip32.fromBase58(xprv); |
217 | 218 | return sdkcore.getSharedSecret(userAuthHdNode, publicKey);
|
218 | 219 | }
|
219 | 220 |
|
220 | 221 | /**
|
221 |
| - * Derives the shared secret for TAT service using the user's private key and the TAT public key. |
| 222 | + * Derives the shared secret for TAT service using ta private key and the TAT public key. |
222 | 223 | * This is used for secure communication with the TAT service and the user.
|
223 | 224 | */
|
224 |
| -export function deriveTatSharedSecret(coinName: 'lnbtc' | 'tlnbtc', userXprv: string): Buffer { |
| 225 | +export function deriveTatSharedSecret(coinName: 'lnbtc' | 'tlnbtc', xprv: string): Buffer { |
225 | 226 | const publicKey = Buffer.from(getStaticsLightningNetwork(coinName).tatPubKey, 'hex');
|
226 |
| - const userAuthHdNode = utxolib.bip32.fromBase58(userXprv); |
| 227 | + const userAuthHdNode = utxolib.bip32.fromBase58(xprv); |
227 | 228 | return sdkcore.getSharedSecret(userAuthHdNode, publicKey);
|
228 | 229 | }
|
229 | 230 |
|
230 | 231 | /**
|
231 | 232 | * Given a seed, compute a BIP32 derivation index.
|
232 | 233 | * 0 <= index < 4294967295 (largest 4 byte number)
|
233 |
| - * @param seed |
| 234 | + * @param seed (optional) If nothing provided, we will generate one randomly |
234 | 235 | */
|
235 |
| -export function computeBip32DerivationIndexFromSeed(seed: string): number { |
236 |
| - return Buffer.from(utxolib.crypto.sha256(Buffer.from(seed, 'utf8'))).readUint32BE(0); |
| 236 | +export function computeBip32DerivationIndexFromSeed(seed?: string): number { |
| 237 | + return Buffer.from(utxolib.crypto.sha256(Buffer.from(seed ?? randomBytes(32).toString('hex'), 'utf8'))).readUint32BE( |
| 238 | + 0 |
| 239 | + ); |
237 | 240 | }
|
0 commit comments