aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/bitcoin.js2
-rw-r--r--lib/rpc.js26
-rw-r--r--lib/utils.js9
3 files changed, 36 insertions, 1 deletions
diff --git a/lib/bitcoin.js b/lib/bitcoin.js
index eda8b4b0..a811dc14 100644
--- a/lib/bitcoin.js
+++ b/lib/bitcoin.js
@@ -1,3 +1,3 @@
import config from "$config";
-import rpc from "./rpc";
+import rpc from "$lib/rpc";
export default rpc(config.bitcoin);
diff --git a/lib/rpc.js b/lib/rpc.js
new file mode 100644
index 00000000..b8a2d0dd
--- /dev/null
+++ b/lib/rpc.js
@@ -0,0 +1,26 @@
+import got from "got";
+
+export default ({ host, port, wallet, username, password }) =>
+ new Proxy(
+ {},
+ {
+ get: (target, prop) => (...params) =>
+ ((method, ...params) => {
+ let url = `http://${host}:${port}/wallet/${wallet}`;
+ return got
+ .post(url, {
+ json: {
+ method,
+ params
+ },
+ headers: {
+ authorization: `Basic ${Buffer.from(
+ `${username}:${password}`
+ ).toString("base64")}`
+ }
+ })
+ .json()
+ .then(({ result }) => result);
+ })(prop.toLowerCase(), ...params)
+ }
+ );
diff --git a/lib/utils.js b/lib/utils.js
index 92b34be5..e653f235 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -28,3 +28,12 @@ export const toSats = n => Math.round(n * SATS);
export const uniq = (a, k) => [...new Map(a.map(x => [k(x), x])).values()];
export const pick = (O, K) =>
K.reduce((o, k) => (typeof O[k] !== "undefined" && (o[k] = O[k]), o), {});
+
+export const bip21 = (address, { amount, memo, tip }) => {
+ let url = amount || memo ? `bitcoin:${address}?` : address;
+ if (amount)
+ url += `amount=${((amount + tip) / SATS).toFixed(8)}${memo ? "&" : ""}`;
+ if (memo) url += `message=${memo}`;
+
+ return url;
+};