From 5c105c8f4738ab1ead3c5b92f465f899cf0a7446 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Sun, 8 Sep 2024 20:57:06 -0700 Subject: refactor rpc helper, accounts endpoints --- lib/rpc.ts | 43 +++++++++++++++++-------------------------- 1 file changed, 17 insertions(+), 26 deletions(-) (limited to 'lib/rpc.ts') diff --git a/lib/rpc.ts b/lib/rpc.ts index 0346a3cd..909ec96f 100644 --- a/lib/rpc.ts +++ b/lib/rpc.ts @@ -1,28 +1,19 @@ import got from "got"; +import { fail } from "$lib/utils"; +import { err } from "$lib/logging"; -export default ({ host, port, wallet, username, password }) => - new Proxy( - {}, - { - get: - (_, 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 as any).toLowerCase(), ...params), - }, - ); +export default ({ host, port, wallet, username, password }): any => { + let url = `http://${host}:${port}/wallet/${wallet}`; + let token = btoa(`${username}:${password}`); + let headers = { authorization: `Basic ${token}` }; + let p = (json) => got.post(url, { json, headers }).json(); + + let get = + (_, prop: any) => + (...params) => + p({ method: prop.toLowerCase(), params }) + .then((res: any) => res.result) + .catch((e) => (err(e.message), fail(`Failed to call ${prop}`))); + + return new Proxy({}, { get }); +}; -- cgit v1.2.3