Last active
September 14, 2018 22:28
-
-
Save tiero/c60e32432ec0eca8d82b60624858b1cf to your computer and use it in GitHub Desktop.
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
const lightningPayReq = require('bolt11'); | |
const bitcoin = require('bitcoinjs-lib'); | |
class LightningWallet { | |
constructor(privateKey, coinType) { | |
const network = bitcoin.networks[coinType]; | |
const keyPair = bitcoin.ECPair.fromWIF(privateKey, network); | |
const pubKey = keyPair.getPublicKeyBuffer(); | |
const scriptPubKey = bitcoin.script.witnessPubKeyHash.output.encode(bitcoin.crypto.hash160(pubKey)); | |
const address = bitcoin.address.fromOutputScript(scriptPubKey, network); | |
this.privateKey = privateKey; | |
this.coinType = coinType; | |
this.address = address; | |
} | |
sign(encoded) { return lightningPayReq.sign(encoded, this.privateKey) } | |
decode(invoice) { return lightningPayReq.decode(invoice) } | |
encode(satoshis) { | |
const encoded = lightningPayReq.encode({ | |
"coinType": this.coinType, | |
"satoshis": satoshis, | |
"tags": [ | |
{ | |
"tagName": "payment_hash", | |
"data": bitcoin.crypto.sha256(this.coinType + satoshis + new Date().toUTCString()).toString("hex") | |
}, | |
{ | |
"tagName": "fallback_address", | |
"data": { | |
"address": this.address | |
} | |
} | |
] | |
}) | |
return encoded | |
} | |
} | |
module.exports = LightningWallet |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment