-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
98 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const assert = require('assert') | ||
const { secp256k1: bsecp256k1, BN, SHA256 } = require('bcrypto') | ||
|
||
const lightning = require('./src/index.js') | ||
|
||
const LND_SOCKET = 'localhost:10009' // should be socket where your node is running | ||
const LND_MACAROON = 'admin.macaroon' // should be absolute path to your macaroon file | ||
const LND_CERT = 'tls.cert' // abs path to cert file | ||
|
||
lightning.setCredentials(LND_SOCKET, LND_MACAROON, LND_CERT) | ||
|
||
let client = lightning.lightning() | ||
let wallet = lightning.wallet() | ||
|
||
function hash(data) { | ||
return SHA256.digest(data) | ||
} | ||
|
||
function pubKeyToPoint(pubKey) { | ||
const s = BN.fromBuffer(pubKey).isOdd() | ||
const x = BN.fromBuffer(pubKey.slice(1, 33)) | ||
return bsecp256k1.curve.pointFromX(x, s) | ||
} | ||
|
||
;(async function() { | ||
try { | ||
const { identity_pubkey } = await client.getInfoAsync({}) | ||
|
||
let pubKeyN = Buffer.from(identity_pubkey, 'hex') | ||
let pubKeyNPoint = pubKeyToPoint(pubKeyN) | ||
|
||
// get ephemeral keys for generating shared secret | ||
let ephemeralPriv = bsecp256k1.privateKeyGenerate() | ||
let ephemeralPub = bsecp256k1.publicKeyCreate(ephemeralPriv, true) | ||
let ephemeralPubBuff = bsecp256k1.publicKeyConvert(ephemeralPub, true) | ||
|
||
// multiply node's public key by our ephemeral private key to generate shared pub key | ||
let sharedPubKey = pubKeyNPoint.mul(BN.fromBuffer(ephemeralPriv)) | ||
sharedPubKey = sharedPubKey.encode(true) | ||
|
||
// generate shared key by using our chosen KDF (Key Derivation Function) | ||
const sharedKey = hash(sharedPubKey) | ||
|
||
// get shared key from node with ephemeral pubkey | ||
const { shared_key: derivedKey } = await wallet.deriveSharedKeyAsync({ ephemeral_pubkey: ephemeralPubBuff }) | ||
|
||
// assert they are equal | ||
assert(sharedKey.toString('hex') === derivedKey.toString('hex')) | ||
console.log('Success! Shared key:', sharedKey.toString('hex')) | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters