Skip to content

Commit a8a9102

Browse files
Merge pull request #6310 from BitGo/BTC-2202.middleware-encryption-key
feat(statics): add middleware public key field to LightningNetwork
2 parents 8baf791 + b151135 commit a8a9102

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as statics from '@bitgo/statics';
22
import * as utxolib from '@bitgo/utxo-lib';
3+
import { randomBytes } from 'crypto';
34
import { importMacaroon, bytesToBase64 } from 'macaroon';
45
import * as bs58check from 'bs58check';
56
import * as sdkcore from '@bitgo/sdk-core';
@@ -206,3 +207,32 @@ export function deriveLightningServiceSharedSecret(coinName: 'lnbtc' | 'tlnbtc',
206207
const userAuthHdNode = utxolib.bip32.fromBase58(userAuthXprv);
207208
return sdkcore.getSharedSecret(userAuthHdNode, publicKey);
208209
}
210+
211+
/**
212+
* Derives the shared secret for the middleware using a private key and the middleware's public key.
213+
*/
214+
export function deriveMiddlewareSharedSecret(coinName: 'lnbtc' | 'tlnbtc', xprv: string): Buffer {
215+
const publicKey = Buffer.from(getStaticsLightningNetwork(coinName).middlewarePubKey, 'hex');
216+
const userAuthHdNode = utxolib.bip32.fromBase58(xprv);
217+
return sdkcore.getSharedSecret(userAuthHdNode, publicKey);
218+
}
219+
220+
/**
221+
* Derives the shared secret for TAT service using ta private key and the TAT public key.
222+
*/
223+
export function deriveTatSharedSecret(coinName: 'lnbtc' | 'tlnbtc', xprv: string): Buffer {
224+
const publicKey = Buffer.from(getStaticsLightningNetwork(coinName).tatPubKey, 'hex');
225+
const userAuthHdNode = utxolib.bip32.fromBase58(xprv);
226+
return sdkcore.getSharedSecret(userAuthHdNode, publicKey);
227+
}
228+
229+
/**
230+
* Given a seed, compute a BIP32 derivation index.
231+
* 0 <= index < 4294967295 (largest 4 byte number)
232+
* @param seed (optional) If nothing provided, we will generate one randomly
233+
*/
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+
);
238+
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import {
1313
createWatchOnly,
1414
addIPCaveatToMacaroon,
1515
deriveLightningServiceSharedSecret,
16+
deriveMiddlewareSharedSecret,
17+
deriveTatSharedSecret,
18+
computeBip32DerivationIndexFromSeed,
1619
} from '../../../src/lightning';
1720

1821
import * as sdkcore from '@bitgo/sdk-core';
@@ -99,4 +102,34 @@ describe('lightning utils', function () {
99102

100103
assert.deepStrictEqual(secret, expectedSecret);
101104
});
105+
106+
it(`deriveMiddlewareSharedSecret`, function () {
107+
const userAuthXprv =
108+
'xprv9s21ZrQH143K4NPkV8riiTnFf72MRyQDVHMmmpekGF1w5QkS2MfTei9KXYvrZVMop4zQ4arnzSF7TRp3Cy73AWaDdADiYMCi5qpYW1bUa5m';
109+
const middlewarePubKey = getStaticsLightningNetwork('tlnbtc').middlewarePubKey;
110+
111+
const expectedSecret = sdkcore.getSharedSecret(
112+
utxolib.bip32.fromBase58(userAuthXprv),
113+
Buffer.from(middlewarePubKey, 'hex')
114+
);
115+
116+
const secret = deriveMiddlewareSharedSecret('tlnbtc', userAuthXprv);
117+
118+
assert.deepStrictEqual(secret, expectedSecret);
119+
});
120+
121+
it(`deriveTatSharedSecret`, function () {
122+
const userXprv =
123+
'xprv9s21ZrQH143K4NPkV8riiTnFf72MRyQDVHMmmpekGF1w5QkS2MfTei9KXYvrZVMop4zQ4arnzSF7TRp3Cy73AWaDdADiYMCi5qpYW1bUa5m';
124+
const tatPubKey = getStaticsLightningNetwork('tlnbtc').tatPubKey;
125+
const expectedSecret = sdkcore.getSharedSecret(utxolib.bip32.fromBase58(userXprv), Buffer.from(tatPubKey, 'hex'));
126+
const secret = deriveTatSharedSecret('tlnbtc', userXprv);
127+
assert.deepStrictEqual(secret, expectedSecret);
128+
});
129+
130+
it(`computeBip32DerivationIndexFromSeed`, function () {
131+
const seed1 = 'The Times 03/Jan/2009 Chancellor on brink of second bailout for banks.';
132+
const seed2 = 'The Times 04/Jan/2009 Chancellor on brink of second bailout for banks.';
133+
assert.notDeepStrictEqual(computeBip32DerivationIndexFromSeed(seed1), computeBip32DerivationIndexFromSeed(seed2));
134+
});
102135
});

modules/statics/src/networks.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ 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;
33+
/**
34+
* The public key of the TAT service, used for deriving the shared Elliptic Curve Diffie-Hellman (ECDH) secret
35+
* between the user's extended private key and the TAT service.
36+
*/
37+
tatPubKey: string;
2838
}
2939

3040
export interface AdaNetwork extends BaseNetwork {
@@ -321,6 +331,8 @@ class LightningBitcoin extends Mainnet implements LightningNetwork {
321331
utxolibName = 'bitcoin';
322332
explorerUrl = 'https://mempool.space/lightning';
323333
lightningServicePubKey = '0338508686f978ceffd7ce05404041b1a5b4f75a39bc92a6d355240ccc081f763e';
334+
middlewarePubKey = '020d52f6458b17b565bc03516568d1a65c8cbd131ce03f2207567e4cd3322e82f3';
335+
tatPubKey = '03953da20068c096858d3cca60f8c5b0e5a2f5d0383ecf9b6ab392d71e4f3e72df';
324336
}
325337

326338
class LightningBitcoinTestnet extends Testnet implements LightningNetwork {
@@ -329,6 +341,8 @@ class LightningBitcoinTestnet extends Testnet implements LightningNetwork {
329341
utxolibName = 'testnet';
330342
explorerUrl = 'https://mempool.space/testnet/lightning';
331343
lightningServicePubKey = '024055021db1e7f019ebb783ab0b0810c21a819207d4cb1ec4a6e2150ac07f1482';
344+
middlewarePubKey = '027cb3bc6b49fc385d282b42a7be232a94ffcbaffc7818b603b17722582bbf539b';
345+
tatPubKey = '02e747c99c371eac9c14fb19913bec8a0e3e46e35ab1a45878e5b9afbb69899c1e';
332346
}
333347

334348
class Bitcoin extends Mainnet implements UtxoNetwork {

0 commit comments

Comments
 (0)