refactor: remove nimbaser and nim-base32 dependencies, use stew modules in their place (#58)

Closes issue #56.
This commit is contained in:
Michael Bradley 2021-09-29 08:42:34 -05:00 committed by GitHub
parent da366ae1cf
commit b2ecbda9e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 55 additions and 48 deletions

6
.gitmodules vendored
View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)

1
vendor/nbaser vendored

@ -1 +0,0 @@
Subproject commit 0c989e0d9580b4a6c14e11976dba3a0d503ecfcd

1
vendor/nim-base32 vendored

@ -1 +0,0 @@
Subproject commit 660680c1cbdf6b1294b669fdb8b6e9c89794cb5b