From 0eddfa2b4ab521d712be6ff219ab2e3978999ddf Mon Sep 17 00:00:00 2001 From: Sale Djenic Date: Fri, 8 Oct 2021 11:58:39 +0200 Subject: [PATCH] added new backend for refactoring purpose, contacts added --- status/statusgo_backend_new/contacts.nim | 41 +++++++++++++++++++ status/statusgo_backend_new/core.nim | 41 +++++++++++++++++++ status/statusgo_backend_new/response_type.nim | 24 +++++++++++ status/statusgo_backend_new/utils.nim | 7 ++++ 4 files changed, 113 insertions(+) create mode 100644 status/statusgo_backend_new/contacts.nim create mode 100644 status/statusgo_backend_new/core.nim create mode 100644 status/statusgo_backend_new/response_type.nim create mode 100644 status/statusgo_backend_new/utils.nim diff --git a/status/statusgo_backend_new/contacts.nim b/status/statusgo_backend_new/contacts.nim new file mode 100644 index 0000000..a1baa5c --- /dev/null +++ b/status/statusgo_backend_new/contacts.nim @@ -0,0 +1,41 @@ +import json, strmisc +import core, utils +import response_type + +export response_type + +proc getContacts*(): RpcResponse[JsonNode] {.raises: [Exception].} = + let payload = %* [] + result = callPrivateRPC("contacts".prefix, payload) + +proc getContactById*(id: string): RpcResponse[JsonNode] {.raises: [Exception].} = + let payload = %* [] + result = callPrivateRPC("getContactByID".prefix, payload) + +proc saveContact*(id: string, ensVerified: bool, ensName: string, alias: string, + identicon: string, thumbnail: string, largeImage: string, added: bool, + blocked: bool, hasAddedUs: bool, localNickname: string) + {.raises: [Exception].} = + let payload = %* [{ + "id": id, + "name": ensName, + "ensVerified": ensVerified, + "alias": alias, + "identicon": identicon, + "images": { + "thumbnail": {"Payload": thumbnail.partition(",")[2]}, + "large": {"Payload": largeImage.partition(",")[2]} + }, + "added": added, + "blocked": blocked, + "hasAddedUs": hasAddedUs, + "localNickname": localNickname + }] + + discard callPrivateRPC("saveContact".prefix, payload) + +proc sendContactUpdate*(publicKey, ensName, thumbnail: string) + {.raises: [Exception].} = + let payload = %* [publicKey, ensName, thumbnail] + discard callPrivateRPC("sendContactUpdate".prefix, payload) + \ No newline at end of file diff --git a/status/statusgo_backend_new/core.nim b/status/statusgo_backend_new/core.nim new file mode 100644 index 0000000..bf79654 --- /dev/null +++ b/status/statusgo_backend_new/core.nim @@ -0,0 +1,41 @@ +import json, json_serialization, strformat, chronicles +import status_go +import response_type + +export response_type + +logScope: + topics = "rpc" + +proc callRPC*(inputJSON: string): string = + return $status_go.callRPC(inputJSON) + +proc callPrivateRPCRaw*(inputJSON: string): string {.raises: [].} = + result = $status_go.callPrivateRPC(inputJSON) + +proc callPrivateRPC*(methodName: string, payload = %* []): RpcResponse[JsonNode] + {.raises: [RpcException, ValueError, Defect, SerializationError].} = + try: + let inputJSON = %* { + "jsonrpc": "2.0", + "method": methodName, + "params": %payload + } + debug "NewBE_callPrivateRPC", rpc_method=methodName + let rpcResponseRaw = status_go.callPrivateRPC($inputJSON) + result = Json.decode(rpcResponseRaw, RpcResponse[JsonNode]) + + if(not result.error.isNil): + var err = "\nstatus-go error [" + err &= fmt"methodName:{methodName}, " + err &= fmt"code:{result.error.code}, " + err &= fmt"message:{result.error.message} " + err &= "]\n" + error "rpc response error", err + raise newException(ValueError, err) + + except RpcException as e: + error "error doing rpc request", methodName = methodName, exception=e.msg + raise newException(RpcException, e.msg) + + diff --git a/status/statusgo_backend_new/response_type.nim b/status/statusgo_backend_new/response_type.nim new file mode 100644 index 0000000..3544cdc --- /dev/null +++ b/status/statusgo_backend_new/response_type.nim @@ -0,0 +1,24 @@ +{.used.} + +import strformat + +type + RpcException* = object of CatchableError + +type + RpcError* = ref object + code*: int + message*: string + +type + RpcResponse*[T] = object + jsonrpc*: string + result*: T + id*: int + error*: RpcError + +proc `$`*(self: RpcError): string = + result = fmt"""RpcError( + code: {self.code}, + message: {self.message}, + ]""" \ No newline at end of file diff --git a/status/statusgo_backend_new/utils.nim b/status/statusgo_backend_new/utils.nim new file mode 100644 index 0000000..bae0160 --- /dev/null +++ b/status/statusgo_backend_new/utils.nim @@ -0,0 +1,7 @@ +proc isWakuEnabled(): bool = + true # TODO: + +proc prefix*(methodName: string, isExt:bool = true): string = + result = if isWakuEnabled(): "waku" else: "shh" + result = result & (if isExt: "ext_" else: "_") + result = result & methodName