feat(ens): new backend

This commit is contained in:
Anthony Laibe 2021-12-20 11:52:09 +01:00 committed by Anthony Laibe
parent d639219b94
commit 8bc678dacd
6 changed files with 17 additions and 86 deletions

View File

@ -67,5 +67,4 @@ QtObject:
proc ensResourceURL*(self: View, ens: string, url: string): string {.slot.} =
let (url, base, http_scheme, path_prefix, hasContentHash) = self.delegate.ensResourceURL(ens, url)
result = url_replaceHostAndAddPath(url, (if hasContentHash: base else: url_host(base)), http_scheme, path_prefix)
result = url_replaceHostAndAddPath(url, (if hasContentHash: base else: url_host(base)), http_scheme, path_prefix)

View File

@ -175,6 +175,9 @@ var NODE_CONFIG* = %* {
"WalletConfig": {
"Enabled": true
},
"EnsConfig": {
"Enabled": true
},
"Networks": [
{
"chainId": 1,

View File

@ -1,8 +1,5 @@
import Tables, json, chronicles, strutils
import sets
import options
import chronicles, libp2p/[multihash, multicodec, cid]
import nimcrypto
import Tables, json, chronicles
import chronicles
include ../../common/json_utils
import service_interface
import status/statusgo_backend_new/ens as status_go
@ -23,53 +20,10 @@ proc newService*(): Service =
method init*(self: Service) =
discard
method getContentHash*(self: Service, ens: string): Option[string] =
method resourceUrl*(self: Service, username: string): (string, string, string) =
try:
let contentHash = status_go.contenthash(ens)
if contentHash != "":
return some(contentHash)
let response = status_go.resourceURL(username)
return (response.result{"Scheme"}.getStr, response.result{"Host"}.getStr, response.result{"Path"}.getStr)
except Exception as e:
let errDescription = e.msg
error "error: ", errDescription
return none(string)
method decodeENSContentHash*(self: Service, value: string): tuple[ensType: ENSType, output: string] =
if value == "":
return (ENSType.UNKNOWN, "")
if value[0..5] == "e40101":
return (ENSType.SWARM, value.split("1b20")[1])
if value[0..7] == "e3010170":
try:
let defaultCodec = parseHexInt("70") #dag-pb
var codec = defaultCodec # no codec specified
var codecStartIdx = 2 # idx of where codec would start if it was specified
# handle the case when starts with 0xe30170 instead of 0xe3010170
if value[2..5] == "0101":
codecStartIdx = 6
codec = parseHexInt(value[6..7])
elif value[2..3] == "01" and value[4..5] != "12":
codecStartIdx = 4
codec = parseHexInt(value[4..5])
# strip the info we no longer need
var multiHashStr = value[codecStartIdx + 2..<value.len]
# The rest of the hash identifies the multihash algo, length, and digest
# More info: https://multiformats.io/multihash/
# 12 = identifies sha2-256 hash
# 20 = multihash length = 32
# ...rest = multihash digest
let
multiHash = MultiHash.init(nimcrypto.fromHex(multiHashStr)).get()
decoded = Cid.init(CIDv0, MultiCodec.codec(codec), multiHash).get()
return (ENSType.IPFS, $decoded)
except Exception as e:
error "Error decoding ENS contenthash", hash=value, exception=e.msg
raise
if value[0..8] == "e50101700":
return (ENSType.IPNS, parseHexStr(value[12..value.len-1]))
return (ENSType.UNKNOWN, "")
error "Error getting ENS resourceUrl", username=username, exception=e.msg
raise

View File

@ -17,8 +17,5 @@ method delete*(self: ServiceInterface) {.base.} =
method init*(self: ServiceInterface) {.base.} =
raise newException(ValueError, "No implementation available")
method getContentHash*(self: ServiceInterface, ens: string): Option[string] {.base.} =
raise newException(ValueError, "No implementation available")
method decodeENSContentHash*(self: ServiceInterface, value: string): tuple[ensType: ENSType, output: string] =
method resourceUrl*(self: ServiceInterface, username: string): (string, string, string) {.base.} =
raise newException(ValueError, "No implementation available")

View File

@ -10,8 +10,6 @@ import service_interface
import status/statusgo_backend_new/permissions as status_go_permissions
import status/statusgo_backend_new/accounts as status_go_accounts
import status/statusgo_backend_new/core as status_go_core
from stew/base32 import nil
from stew/base58 import nil
import stew/byteutils
export service_interface
@ -19,8 +17,6 @@ logScope:
topics = "provider-service"
const HTTPS_SCHEME* = "https"
const IPFS_GATEWAY* = ".infura.status.im"
const SWARM_GATEWAY* = "swarm-gateways.net"
type
RequestTypes {.pure.} = enum
@ -298,27 +294,9 @@ method postMessage*(self: Service, message: string): string =
of RequestTypes.APIRequest: self.process(message.toAPIRequest())
else: """{"type":"TODO-IMPLEMENT-THIS"}""" ##################### TODO:
method ensResourceURL*(self: Service, ens: string, url: string): (string, string, string, string, bool) =
let contentHash = self.ensService.getContentHash(ens)
if contentHash.isNone(): # ENS does not have a content hash
method ensResourceURL*(self: Service, username: string, url: string): (string, string, string, string, bool) =
let (scheme, host, path) = self.ensService.resourceUrl(username)
if host == "":
return (url, url, HTTPS_SCHEME, "", false)
let decodedHash = self.ensService.decodeENSContentHash(contentHash.get())
case decodedHash[0]:
of ENSType.IPFS:
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)
of ENSType.IPNS:
result = (url, decodedHash[1], HTTPS_SCHEME, "", true)
else:
warn "Unknown content for", ens, contentHash
return (url, host, scheme, path, true)

2
vendor/status-lib vendored

@ -1 +1 @@
Subproject commit f0c1ad50878dde9c0c5c72f6421d29231bea94b5
Subproject commit e13f9041216553dfe2fdc2abff25619e6711d695