feat(ens): new backend
This commit is contained in:
parent
d639219b94
commit
8bc678dacd
|
@ -67,5 +67,4 @@ QtObject:
|
||||||
|
|
||||||
proc ensResourceURL*(self: View, ens: string, url: string): string {.slot.} =
|
proc ensResourceURL*(self: View, ens: string, url: string): string {.slot.} =
|
||||||
let (url, base, http_scheme, path_prefix, hasContentHash) = self.delegate.ensResourceURL(ens, url)
|
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)
|
||||||
|
|
|
@ -175,6 +175,9 @@ var NODE_CONFIG* = %* {
|
||||||
"WalletConfig": {
|
"WalletConfig": {
|
||||||
"Enabled": true
|
"Enabled": true
|
||||||
},
|
},
|
||||||
|
"EnsConfig": {
|
||||||
|
"Enabled": true
|
||||||
|
},
|
||||||
"Networks": [
|
"Networks": [
|
||||||
{
|
{
|
||||||
"chainId": 1,
|
"chainId": 1,
|
||||||
|
|
|
@ -1,8 +1,5 @@
|
||||||
import Tables, json, chronicles, strutils
|
import Tables, json, chronicles
|
||||||
import sets
|
import chronicles
|
||||||
import options
|
|
||||||
import chronicles, libp2p/[multihash, multicodec, cid]
|
|
||||||
import nimcrypto
|
|
||||||
include ../../common/json_utils
|
include ../../common/json_utils
|
||||||
import service_interface
|
import service_interface
|
||||||
import status/statusgo_backend_new/ens as status_go
|
import status/statusgo_backend_new/ens as status_go
|
||||||
|
@ -23,53 +20,10 @@ proc newService*(): Service =
|
||||||
method init*(self: Service) =
|
method init*(self: Service) =
|
||||||
discard
|
discard
|
||||||
|
|
||||||
method getContentHash*(self: Service, ens: string): Option[string] =
|
method resourceUrl*(self: Service, username: string): (string, string, string) =
|
||||||
try:
|
try:
|
||||||
let contentHash = status_go.contenthash(ens)
|
let response = status_go.resourceURL(username)
|
||||||
if contentHash != "":
|
return (response.result{"Scheme"}.getStr, response.result{"Host"}.getStr, response.result{"Path"}.getStr)
|
||||||
return some(contentHash)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
let errDescription = e.msg
|
error "Error getting ENS resourceUrl", username=username, exception=e.msg
|
||||||
error "error: ", errDescription
|
raise
|
||||||
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, "")
|
|
|
@ -17,8 +17,5 @@ method delete*(self: ServiceInterface) {.base.} =
|
||||||
method init*(self: ServiceInterface) {.base.} =
|
method init*(self: ServiceInterface) {.base.} =
|
||||||
raise newException(ValueError, "No implementation available")
|
raise newException(ValueError, "No implementation available")
|
||||||
|
|
||||||
method getContentHash*(self: ServiceInterface, ens: string): Option[string] {.base.} =
|
method resourceUrl*(self: ServiceInterface, username: string): (string, string, string) {.base.} =
|
||||||
raise newException(ValueError, "No implementation available")
|
|
||||||
|
|
||||||
method decodeENSContentHash*(self: ServiceInterface, value: string): tuple[ensType: ENSType, output: string] =
|
|
||||||
raise newException(ValueError, "No implementation available")
|
raise newException(ValueError, "No implementation available")
|
||||||
|
|
|
@ -10,8 +10,6 @@ import service_interface
|
||||||
import status/statusgo_backend_new/permissions as status_go_permissions
|
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/accounts as status_go_accounts
|
||||||
import status/statusgo_backend_new/core as status_go_core
|
import status/statusgo_backend_new/core as status_go_core
|
||||||
from stew/base32 import nil
|
|
||||||
from stew/base58 import nil
|
|
||||||
import stew/byteutils
|
import stew/byteutils
|
||||||
export service_interface
|
export service_interface
|
||||||
|
|
||||||
|
@ -19,8 +17,6 @@ logScope:
|
||||||
topics = "provider-service"
|
topics = "provider-service"
|
||||||
|
|
||||||
const HTTPS_SCHEME* = "https"
|
const HTTPS_SCHEME* = "https"
|
||||||
const IPFS_GATEWAY* = ".infura.status.im"
|
|
||||||
const SWARM_GATEWAY* = "swarm-gateways.net"
|
|
||||||
|
|
||||||
type
|
type
|
||||||
RequestTypes {.pure.} = enum
|
RequestTypes {.pure.} = enum
|
||||||
|
@ -298,27 +294,9 @@ method postMessage*(self: Service, message: string): string =
|
||||||
of RequestTypes.APIRequest: self.process(message.toAPIRequest())
|
of RequestTypes.APIRequest: self.process(message.toAPIRequest())
|
||||||
else: """{"type":"TODO-IMPLEMENT-THIS"}""" ##################### TODO:
|
else: """{"type":"TODO-IMPLEMENT-THIS"}""" ##################### TODO:
|
||||||
|
|
||||||
method ensResourceURL*(self: Service, ens: string, url: string): (string, string, string, string, bool) =
|
method ensResourceURL*(self: Service, username: string, url: string): (string, string, string, string, bool) =
|
||||||
let contentHash = self.ensService.getContentHash(ens)
|
let (scheme, host, path) = self.ensService.resourceUrl(username)
|
||||||
if contentHash.isNone(): # ENS does not have a content hash
|
if host == "":
|
||||||
return (url, url, HTTPS_SCHEME, "", false)
|
return (url, url, HTTPS_SCHEME, "", false)
|
||||||
|
|
||||||
let decodedHash = self.ensService.decodeENSContentHash(contentHash.get())
|
return (url, host, scheme, path, true)
|
||||||
|
|
||||||
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
|
|
|
@ -1 +1 @@
|
||||||
Subproject commit f0c1ad50878dde9c0c5c72f6421d29231bea94b5
|
Subproject commit e13f9041216553dfe2fdc2abff25619e6711d695
|
Loading…
Reference in New Issue