aboutsummaryrefslogtreecommitdiffstats
path: root/lib/rpc.ts
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rpc.ts')
-rw-r--r--lib/rpc.ts43
1 files changed, 17 insertions, 26 deletions
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 });
+};