Guest User

Untitled

a guest
Sep 14th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. const lightningPayReq = require('bolt11');
  2. const bitcoin = require('bitcoinjs-lib');
  3.  
  4.  
  5. class LightningWallet {
  6.  
  7. constructor(privateKey, coinType) {
  8. const network = bitcoin.networks[coinType];
  9. const keyPair = bitcoin.ECPair.fromWIF(privateKey, network);
  10. const pubKey = keyPair.getPublicKeyBuffer();
  11.  
  12. const scriptPubKey = bitcoin.script.witnessPubKeyHash.output.encode(bitcoin.crypto.hash160(pubKey));
  13. const address = bitcoin.address.fromOutputScript(scriptPubKey, network);
  14.  
  15.  
  16. this.privateKey = privateKey;
  17. this.coinType = coinType;
  18. this.address = address;
  19. }
  20.  
  21. sign(encoded) { return lightningPayReq.sign(encoded, this.privateKey) }
  22.  
  23. decode(invoice) { return lightningPayReq.decode(invoice) }
  24.  
  25. encode(satoshis) {
  26. const encoded = lightningPayReq.encode({
  27. "coinType": this.coinType,
  28. "satoshis": satoshis,
  29. "tags": [
  30. {
  31. "tagName": "payment_hash",
  32. "data": bitcoin.crypto.sha256(this.coinType + satoshis + new Date().toUTCString()).toString("hex")
  33. },
  34. {
  35. "tagName": "fallback_address",
  36. "data": {
  37. "address": this.address
  38. }
  39. }
  40. ]
  41. })
  42.  
  43. return encoded
  44. }
  45. }
  46.  
  47. module.exports = LightningWallet
Add Comment
Please, Sign In to add comment