From b2ecbda9e7a43d2a663da327f1a284b00b7d9c65 Mon Sep 17 00:00:00 2001 From: Michael Bradley Date: Wed, 29 Sep 2021 08:42:34 -0500 Subject: [PATCH] refactor: remove nimbaser and nim-base32 dependencies, use stew modules in their place (#58) Closes issue #56. --- .gitmodules | 6 ----- status/eth/stickers.nim | 7 +++--- status/provider.nim | 50 ++++++++++++++++++++++++----------------- status/updates.nim | 38 ++++++++++++++++++------------- vendor/nbaser | 1 - vendor/nim-base32 | 1 - 6 files changed, 55 insertions(+), 48 deletions(-) delete mode 160000 vendor/nbaser delete mode 160000 vendor/nim-base32 diff --git a/.gitmodules b/.gitmodules index 2e427f3..a06d797 100644 --- a/.gitmodules +++ b/.gitmodules @@ -76,12 +76,6 @@ [submodule "vendor/nim-task-runner"] path = vendor/nim-task-runner url = https://github.com/status-im/nim-task-runner.git -[submodule "vendor/nbaser"] - path = vendor/nbaser - url = https://github.com/D-Nice/nbaser/ -[submodule "vendor/nim-base32"] - path = vendor/nim-base32 - url = https://github.com/elktree/nim-base32 [submodule "vendor/edn.nim"] path = vendor/edn.nim url = https://github.com/status-im/edn.nim diff --git a/status/eth/stickers.nim b/status/eth/stickers.nim index 3984a8a..7b200ce 100644 --- a/status/eth/stickers.nim +++ b/status/eth/stickers.nim @@ -5,7 +5,6 @@ import # std libs json, json_serialization, chronicles, web3/ethtypes, stint, strutils from strutils import parseHexInt, parseInt -import nbaser import # vendor libs json_serialization, chronicles, libp2p/[multihash, multicodec, cid], stint, @@ -13,10 +12,10 @@ import # vendor libs from nimcrypto import fromHex import # status-desktop libs - ../types/[sticker, setting, rpc_response, network_type], - ../statusgo_backend/[edn_helpers, settings], + ../types/[sticker, setting, rpc_response, network_type], + ../statusgo_backend/[edn_helpers, settings], ../utils, - ./transactions as transactions, + ./transactions as transactions, ./contracts # Retrieves number of sticker packs owned by user diff --git a/status/provider.nim b/status/provider.nim index b2f163a..3cd9a0a 100644 --- a/status/provider.nim +++ b/status/provider.nim @@ -7,16 +7,14 @@ import statusgo_backend/core import statusgo_backend/settings as status_settings import json, json_serialization, sets, strutils import chronicles -import nbaser import stew/byteutils -from base32 import nil +from stew/base32 import nil +from stew/base58 import nil const HTTPS_SCHEME* = "https" const IPFS_GATEWAY* = ".infura.status.im" const SWARM_GATEWAY* = "swarm-gateways.net" -const base58* = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" - logScope: topics = "provider-model" @@ -64,7 +62,7 @@ proc newProviderModel*(events: EventEmitter, permissions: PermissionsModel, wall result.permissions = permissions result.wallet = wallet -proc requestType(message: string): RequestTypes = +proc requestType(message: string): RequestTypes = let data = message.parseJson result = RequestTypes.Unknown try: @@ -84,7 +82,7 @@ proc toWeb3SendAsyncReadOnly(message: string): Web3SendAsyncReadOnly = ) ) -proc toAPIRequest(message: string): APIRequest = +proc toAPIRequest(message: string): APIRequest = let data = message.parseJson result = APIRequest( @@ -103,7 +101,7 @@ proc process(self: ProviderModel, data: Web3SendAsyncReadOnly): string = "code": 4100 } } - + if data.payload.rpcMethod == "eth_sendTransaction": try: let request = data.request.parseJson @@ -170,7 +168,7 @@ proc process(self: ProviderModel, data: Web3SendAsyncReadOnly): string = } if SIGN_METHODS.contains(data.payload.rpcMethod): - try: + try: let request = data.request.parseJson var params = request["params"] let password = hashPassword(request["password"].getStr()) @@ -186,7 +184,7 @@ proc process(self: ProviderModel, data: Web3SendAsyncReadOnly): string = "password": password, "account": dappAddress }) - + let jsonRpcResult = rpcResult.parseJson let success: bool = not jsonRpcResult.hasKey("error") let errorMessage = if success: "" else: jsonRpcResult["error"]{"message"}.getStr() @@ -215,7 +213,7 @@ proc process(self: ProviderModel, data: Web3SendAsyncReadOnly): string = } - + if ACC_METHODS.contains(data.payload.rpcMethod): let dappAddress = status_settings.getSetting[string](Setting.DappsAddress) return $ %* { @@ -227,7 +225,7 @@ proc process(self: ProviderModel, data: Web3SendAsyncReadOnly): string = "result": if data.payload.rpcMethod == "eth_coinbase": newJString(dappAddress) else: %*[dappAddress] } } - + let rpcResult = callRPC(data.request) return $ %* { @@ -237,7 +235,7 @@ proc process(self: ProviderModel, data: Web3SendAsyncReadOnly): string = "result": rpcResult.parseJson } -proc process*(self: ProviderModel, data: APIRequest): string = +proc process*(self: ProviderModel, data: APIRequest): string = var value:JsonNode = case data.permission of Permission.Web3: %* [status_settings.getSetting[string](Setting.DappsAddress, "0x0000000000000000000000000000000000000000")] of Permission.ContactCode: %* status_settings.getSetting[string](Setting.PublicKey, "0x0") @@ -264,19 +262,29 @@ proc postMessage*(self: ProviderModel, message: string): string = of RequestTypes.APIRequest: self.process(message.toAPIRequest()) else: """{"type":"TODO-IMPLEMENT-THIS"}""" ##################### TODO: -proc ensResourceURL*(self: ProviderModel, ens: string, url: string): (string, string, string, string, bool) = - let contentHash = contenthash(ens) - if contentHash == "": # ENS does not have a content hash - return (url, url, HTTPS_SCHEME, "", false) +proc ensResourceURL*(self: ProviderModel, ens: string, url: string): + (string, string, string, string, bool) = - let decodedHash = contentHash.decodeENSContentHash() - case decodedHash[0]: + let contentHash = contenthash(ens) + if contentHash == "": # ENS does not have a content hash + return (url, url, HTTPS_SCHEME, "", false) + + let decodedHash = contentHash.decodeENSContentHash() + + case decodedHash[0]: of ENSType.IPFS: - let base32Hash = base32.encode(string.fromBytes(base58.decode(decodedHash[1]))).toLowerAscii().replace("=", "") + let + base58bytes = base58.decode(base58.BTCBase58, decodedHash[1]) + base32Hash = base32.encode(base32.Base32Lower, base58bytes) + result = (url, base32Hash & IPFS_GATEWAY, HTTPS_SCHEME, "", true) + of ENSType.SWARM: - result = (url, SWARM_GATEWAY, HTTPS_SCHEME, "/bzz:/" & decodedHash[1] & "/", true) + result = (url, SWARM_GATEWAY, HTTPS_SCHEME, + "/bzz:/" & decodedHash[1] & "/", true) + of ENSType.IPNS: result = (url, decodedHash[1], HTTPS_SCHEME, "", true) - else: + + else: warn "Unknown content for", ens, contentHash diff --git a/status/updates.nim b/status/updates.nim index 3868f13..d597a18 100644 --- a/status/updates.nim +++ b/status/updates.nim @@ -1,8 +1,9 @@ import ens, provider import stew/byteutils -from base32 import nil +from stew/base32 import nil +from stew/base58 import nil import chronicles, httpclient, net -import nbaser, strutils +import strutils import semver import constants @@ -13,30 +14,37 @@ type url*: string proc getLatestVersion*(): VersionInfo = - let contentHash = contenthash(APP_UPDATES_ENS) - if contentHash == "": - raise newException(ValueError, "ENS does not have a content hash") + let contentHash = contenthash(APP_UPDATES_ENS) + if contentHash == "": + raise newException(ValueError, "ENS does not have a content hash") - var url: string = "" + var url: string = "" - let decodedHash = contentHash.decodeENSContentHash() - case decodedHash[0]: + let decodedHash = contentHash.decodeENSContentHash() + + case decodedHash[0]: of ENSType.IPFS: - let base32Hash = base32.encode(string.fromBytes(base58.decode(decodedHash[1]))).toLowerAscii().replace("=", "") + let + base58bytes = base58.decode(base58.BTCBase58, decodedHash[1]) + base32Hash = base32.encode(base32.Base32Lower, base58bytes) + url = "https://" & base32Hash & IPFS_GATEWAY + of ENSType.SWARM: url = "https://" & SWARM_GATEWAY & "/bzz:/" & decodedHash[1] + of ENSType.IPNS: url = "https://" & decodedHash[1] - else: + + else: warn "Unknown content for", contentHash raise newException(ValueError, "Unknown content for " & contentHash) - # Read version from folder - let secureSSLContext = newContext() - let client = newHttpClient(sslContext = secureSSLContext, timeout = CHECK_VERSION_TIMEOUT_MS) - result.version = client.getContent(url & "/VERSION").strip() - result.url = url + # Read version from folder + let secureSSLContext = newContext() + let client = newHttpClient(sslContext = secureSSLContext, timeout = CHECK_VERSION_TIMEOUT_MS) + result.version = client.getContent(url & "/VERSION").strip() + result.url = url proc isNewer*(currentVersion, versionToCheck: string): bool = let lastVersion = parseVersion(versionToCheck) diff --git a/vendor/nbaser b/vendor/nbaser deleted file mode 160000 index 0c989e0..0000000 --- a/vendor/nbaser +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 0c989e0d9580b4a6c14e11976dba3a0d503ecfcd diff --git a/vendor/nim-base32 b/vendor/nim-base32 deleted file mode 160000 index 660680c..0000000 --- a/vendor/nim-base32 +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 660680c1cbdf6b1294b669fdb8b6e9c89794cb5b