aboutsummaryrefslogtreecommitdiffstats
path: root/lib/rpc.ts
blob: 909ec96fd2a25f07599fa202010779566597ac0f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import got from "got";
import { fail } from "$lib/utils";
import { err } from "$lib/logging";

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 });
};