Skip to content

feat(statics): add middleware public key field to LightningNetwork #6310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions modules/abstract-lightning/src/lightning/lightningUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,23 @@ export function deriveLightningServiceSharedSecret(coinName: 'lnbtc' | 'tlnbtc',
const userAuthHdNode = utxolib.bip32.fromBase58(userAuthXprv);
return sdkcore.getSharedSecret(userAuthHdNode, publicKey);
}

/**
* Derives the shared secret for the middleware using the user's auth extended private key and the middleware's public key.
* This is used for secure communication between the middleware and the user.
*/
export function deriveMiddlewareSharedSecret(coinName: 'lnbtc' | 'tlnbtc', userXprv: string): Buffer {
const publicKey = Buffer.from(getStaticsLightningNetwork(coinName).middlewarePubKey, 'hex');
const userAuthHdNode = utxolib.bip32.fromBase58(userXprv);
return sdkcore.getSharedSecret(userAuthHdNode, publicKey);
}

/**
* Derives the shared secret for TAT service using the user's private key and the TAT public key.
* This is used for secure communication with the TAT service and the user.
*/
export function deriveTatSharedSecret(coinName: 'lnbtc' | 'tlnbtc', userXprv: string): Buffer {
const publicKey = Buffer.from(getStaticsLightningNetwork(coinName).tatPubKey, 'hex');
const userAuthHdNode = utxolib.bip32.fromBase58(userXprv);
return sdkcore.getSharedSecret(userAuthHdNode, publicKey);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
createWatchOnly,
addIPCaveatToMacaroon,
deriveLightningServiceSharedSecret,
deriveMiddlewareSharedSecret,
deriveTatSharedSecret,
} from '../../../src/lightning';

import * as sdkcore from '@bitgo/sdk-core';
Expand Down Expand Up @@ -99,4 +101,28 @@ describe('lightning utils', function () {

assert.deepStrictEqual(secret, expectedSecret);
});

it(`deriveMiddlewareSharedSecret`, function () {
const userAuthXprv =
'xprv9s21ZrQH143K4NPkV8riiTnFf72MRyQDVHMmmpekGF1w5QkS2MfTei9KXYvrZVMop4zQ4arnzSF7TRp3Cy73AWaDdADiYMCi5qpYW1bUa5m';
const middlewarePubKey = getStaticsLightningNetwork('tlnbtc').middlewarePubKey;

const expectedSecret = sdkcore.getSharedSecret(
utxolib.bip32.fromBase58(userAuthXprv),
Buffer.from(middlewarePubKey, 'hex')
);

const secret = deriveMiddlewareSharedSecret('tlnbtc', userAuthXprv);

assert.deepStrictEqual(secret, expectedSecret);
});

it(`deriveTatSharedSecret`, function () {
const userXprv =
'xprv9s21ZrQH143K4NPkV8riiTnFf72MRyQDVHMmmpekGF1w5QkS2MfTei9KXYvrZVMop4zQ4arnzSF7TRp3Cy73AWaDdADiYMCi5qpYW1bUa5m';
const tatPubKey = getStaticsLightningNetwork('tlnbtc').tatPubKey;
const expectedSecret = sdkcore.getSharedSecret(utxolib.bip32.fromBase58(userXprv), Buffer.from(tatPubKey, 'hex'));
const secret = deriveTatSharedSecret('tlnbtc', userXprv);
assert.deepStrictEqual(secret, expectedSecret);
});
});
16 changes: 16 additions & 0 deletions modules/statics/src/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ export interface LightningNetwork extends UtxoNetwork {
* by enabling the creation of a shared secret for encryption and decryption of data.
*/
lightningServicePubKey: string;
/**
* The public key of the middleware service, used for deriving the shared Elliptic Curve Diffie-Hellman (ECDH) secret
* between the user's extended private key and the middleware service.
*/
middlewarePubKey: string;
/**
* The public key of the TAT service, used for deriving the shared Elliptic Curve Diffie-Hellman (ECDH) secret
* between the user's extended private key and the TAT service.
*/
tatPubKey: string;
}

export interface AdaNetwork extends BaseNetwork {
Expand Down Expand Up @@ -321,6 +331,10 @@ class LightningBitcoin extends Mainnet implements LightningNetwork {
utxolibName = 'bitcoin';
explorerUrl = 'https://mempool.space/lightning';
lightningServicePubKey = '0338508686f978ceffd7ce05404041b1a5b4f75a39bc92a6d355240ccc081f763e';
// TODO - BTC-2202
middlewarePubKey = '';
// TODO - BTC-2211
tatPubKey = '';
}

class LightningBitcoinTestnet extends Testnet implements LightningNetwork {
Expand All @@ -329,6 +343,8 @@ class LightningBitcoinTestnet extends Testnet implements LightningNetwork {
utxolibName = 'testnet';
explorerUrl = 'https://mempool.space/testnet/lightning';
lightningServicePubKey = '024055021db1e7f019ebb783ab0b0810c21a819207d4cb1ec4a6e2150ac07f1482';
middlewarePubKey = '027cb3bc6b49fc385d282b42a7be232a94ffcbaffc7818b603b17722582bbf539b';
tatPubKey = '02e747c99c371eac9c14fb19913bec8a0e3e46e35ab1a45878e5b9afbb69899c1e';
}

class Bitcoin extends Mainnet implements UtxoNetwork {
Expand Down