diff options
| author | Adam Soltys <[email protected]> | 2022-09-30 15:07:30 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2022-09-30 15:09:55 -0700 |
| commit | c3efae9d1720c6f9eb6f826c4f1c6d5f3e259407 (patch) | |
| tree | 2ac3b89ba3c0febe7d45e25eb4515d42e7a27188 /lib/utils.js | |
| parent | 6dee51ad1c2a74bfb0b57417dae9bce6931cd9f1 (diff) | |
| download | coinos-server-c3efae9d1720c6f9eb6f826c4f1c6d5f3e259407.tar.gz coinos-server-c3efae9d1720c6f9eb6f826c4f1c6d5f3e259407.zip | |
derive address/payreq server side
Diffstat (limited to 'lib/utils.js')
| -rw-r--r-- | lib/utils.js | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/lib/utils.js b/lib/utils.js index 1ff24c67..036ccd6d 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,5 +1,13 @@ +import config from "$config"; import db from "$db"; import sequelize from "@sequelize/core"; +import { fromBase58 } from "bip32"; +import { payments as bcPayments, networks as bcNetworks } from "bitcoinjs-lib"; +import { payments as lqPayments, networks as lqNetworks } from "liquidjs-lib"; +import { createInvoice } from "lightning"; +import lnd from "$lib/lnd"; +import ln from "$lib/ln"; + const { Op } = sequelize; export const getUser = async (username, transaction) => { @@ -90,3 +98,79 @@ export const getBlockHeight = message => { readUInt32(); return readUInt32(); }; + +export const deriveAddress = async (account, type) => { + let { index, pubkey, path, network } = account; + let n = { bitcoin: bcNetworks, liquid: lqNetworks }[network][ + prod ? "bitcoin" : "regtest" + ]; + let p = { bitcoin: bcPayments, liquid: lqPayments }[network]; + let root = fromBase58(pubkey, n); + let parts = path.split("/"); + let hd = root.derive(parseInt(parts[parts.length - 1])); + + type = { + bech32: "p2wpkh", + "p2sh-segwit": "p2sh", + legacy: "p2pkh" + }[type]; + + console.log("TYPE", type); + + let r; + if (type !== "p2sh") { + r = p[type]({ + pubkey: hd.publicKey, + network: n + }); + } else { + r = p[type]({ + redeem: p.p2wpkh({ + pubkey: hd.publicKey, + network: n + }), + network: n + }); + } + + index++; + account.index = index; + account.address = r.address; + await account.save(); + + return r; +}; + +export const bip21 = ({ address, amount, memo, tip, network }, { asset }) => { + if (network === "liquid") network = "liquidnetwork"; + + let url = amount || memo ? `${network}:${address}?` : address; + if (amount) + url += `amount=${((amount + tip) / SATS).toFixed(8)}${memo ? "&" : ""}`; + if (memo) url += `message=${memo}`; + + if (network === "liquidnetwork" && amount) { + url += `&asset=${asset}`; + } + + return url; +}; + +export const derivePayRequest = async ({ amount, memo, tip }) => { + if (!tip) tip = 0; + let value = amount + tip; + + if (config.lna.clightning) { + if (!memo) memo = "coinos"; + return (await ln.invoice(value ? `${value}sat` : "any", new Date(), memo, 360)) + .bolt11; + } else { + return ( + await createInvoice({ + lnd, + tokens: value, + description: memo + }) + ).request; + } +}; |
