Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Add Comment
Please, Sign In to add comment