Skip to content

Commit 579a9a3

Browse files
feat(statics): add middleware public key field to LightningNetwork
Add a new field to LightningNetwork interface that stores the middleware public key used for deriving shared ECDH secrets between user's extended private key and the middleware service. BTC-2202 Co-authored-by: llm-git <[email protected]> TICKET: BTC-2202
1 parent 68ebe71 commit 579a9a3

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

modules/abstract-lightning/src/lightning/lightningUtils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,13 @@ export function deriveLightningServiceSharedSecret(coinName: 'lnbtc' | 'tlnbtc',
206206
const userAuthHdNode = utxolib.bip32.fromBase58(userAuthXprv);
207207
return sdkcore.getSharedSecret(userAuthHdNode, publicKey);
208208
}
209+
210+
/**
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's key.
213+
*/
214+
export function deriveMiddlewareSharedSecret(coinName: 'lnbtc' | 'tlnbtc', userXprv: string): Buffer {
215+
const publicKey = Buffer.from(getStaticsLightningNetwork(coinName).middlewarePubKey, 'hex');
216+
const userAuthHdNode = utxolib.bip32.fromBase58(userXprv);
217+
return sdkcore.getSharedSecret(userAuthHdNode, publicKey);
218+
}

modules/abstract-lightning/test/unit/lightning/lightningUtils.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
createWatchOnly,
1414
addIPCaveatToMacaroon,
1515
deriveLightningServiceSharedSecret,
16+
deriveMiddlewareSharedSecret,
1617
} from '../../../src/lightning';
1718

1819
import * as sdkcore from '@bitgo/sdk-core';
@@ -99,4 +100,19 @@ describe('lightning utils', function () {
99100

100101
assert.deepStrictEqual(secret, expectedSecret);
101102
});
103+
104+
it(`deriveMiddlewareSharedSecret`, function () {
105+
const userAuthXprv =
106+
'xprv9s21ZrQH143K4NPkV8riiTnFf72MRyQDVHMmmpekGF1w5QkS2MfTei9KXYvrZVMop4zQ4arnzSF7TRp3Cy73AWaDdADiYMCi5qpYW1bUa5m';
107+
const middlewarePubKey = getStaticsLightningNetwork('tlnbtc').middlewarePubKey;
108+
109+
const expectedSecret = sdkcore.getSharedSecret(
110+
utxolib.bip32.fromBase58(userAuthXprv),
111+
Buffer.from(middlewarePubKey, 'hex')
112+
);
113+
114+
const secret = deriveMiddlewareSharedSecret('tlnbtc', userAuthXprv);
115+
116+
assert.deepStrictEqual(secret, expectedSecret);
117+
});
102118
});

modules/statics/src/networks.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ export interface LightningNetwork extends UtxoNetwork {
2525
* by enabling the creation of a shared secret for encryption and decryption of data.
2626
*/
2727
lightningServicePubKey: string;
28+
/**
29+
* The public key of the middleware service, used for deriving the shared Elliptic Curve Diffie-Hellman (ECDH) secret
30+
* between the user's extended private key and the middleware service.
31+
*/
32+
middlewarePubKey: string;
2833
}
2934

3035
export interface AdaNetwork extends BaseNetwork {
@@ -321,6 +326,8 @@ class LightningBitcoin extends Mainnet implements LightningNetwork {
321326
utxolibName = 'bitcoin';
322327
explorerUrl = 'https://mempool.space/lightning';
323328
lightningServicePubKey = '0338508686f978ceffd7ce05404041b1a5b4f75a39bc92a6d355240ccc081f763e';
329+
// TODO - BTC-2202
330+
middlewarePubKey = '';
324331
}
325332

326333
class LightningBitcoinTestnet extends Testnet implements LightningNetwork {
@@ -329,6 +336,7 @@ class LightningBitcoinTestnet extends Testnet implements LightningNetwork {
329336
utxolibName = 'testnet';
330337
explorerUrl = 'https://mempool.space/testnet/lightning';
331338
lightningServicePubKey = '024055021db1e7f019ebb783ab0b0810c21a819207d4cb1ec4a6e2150ac07f1482';
339+
middlewarePubKey = '027cb3bc6b49fc385d282b42a7be232a94ffcbaffc7818b603b17722582bbf539b';
332340
}
333341

334342
class Bitcoin extends Mainnet implements UtxoNetwork {

0 commit comments

Comments
 (0)