diff --git a/src/app/provider/view.nim b/src/app/provider/view.nim index 3d816cce50..29671e28e9 100644 --- a/src/app/provider/view.nim +++ b/src/app/provider/view.nim @@ -14,8 +14,9 @@ const AUTH_METHODS = toHashSet(["eth_accounts", "eth_coinbase", "eth_sendTransac const SIGN_METHODS = toHashSet(["eth_sign", "personal_sign", "eth_signTypedData", "eth_signTypedData_v3"]) const ACC_METHODS = toHashSet(["eth_accounts", "eth_coinbase"]) -const IPFS_SCHEME = "https" +const HTTPS_SCHEME = "https" const IPFS_GATEWAY = ".infura.status.im" +const SWARM_GATEWAY = "swarm-gateways.net" const base58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" @@ -279,15 +280,21 @@ QtObject: self.status.permissions.clearPermissions() proc ensResourceURL*(self: Web3ProviderView, ens: string, url: string): string {.slot.} = - # TODO: add support for swarm: "swarm-gateways.net/bzz:/...." and ipns - let contentHash = contenthash(ens) if contentHash == "": # ENS does not have a content hash - return url_replaceHostAndAddPath(url, url_host(url), IPFS_SCHEME) + return url_replaceHostAndAddPath(url, url_host(url), HTTPS_SCHEME) - let decodedContentHash = contentHash.decodeContentHash() - let base32Hash = base32.encode(string.fromBytes(base58.decode(decodedContentHash))).toLowerAscii().replace("=", "") - result = url_replaceHostAndAddPath(url, base32Hash & IPFS_GATEWAY, IPFS_SCHEME) + let decodedHash = contentHash.decodeENSContentHash() + case decodedHash[0]: + of ENSType.IPFS: + let base32Hash = base32.encode(string.fromBytes(base58.decode(decodedHash[1]))).toLowerAscii().replace("=", "") + result = url_replaceHostAndAddPath(url, base32Hash & IPFS_GATEWAY, HTTPS_SCHEME) + of ENSType.SWARM: + result = url_replaceHostAndAddPath(url, SWARM_GATEWAY, HTTPS_SCHEME, "/bzz:/" & decodedHash[1] & "/") + of ENSType.IPNS: + result = url_replaceHostAndAddPath(url, decodedHash[1], HTTPS_SCHEME) + else: + warn "Unknown content for", ens, contentHash proc replaceHostByENS*(self: Web3ProviderView, url: string, ens: string): string {.slot.} = result = url_replaceHostAndAddPath(url, ens) diff --git a/src/status/ens.nim b/src/status/ens.nim index 674ecd55b5..42f7970ee7 100644 --- a/src/status/ens.nim +++ b/src/status/ens.nim @@ -15,6 +15,7 @@ import transactions import algorithm import web3/[ethtypes, conversions], stew/byteutils, stint import libstatus/eth/contracts +import chronicles, sequtils, httpclient, libp2p/[multihash, multicodec, cid] const domain* = ".stateofus.eth" @@ -253,3 +254,50 @@ proc setPubKey*(username, pubKey, address, gas, gasPrice, password: string, succ proc statusRegistrarAddress*():string = result = $contracts.getContract("ens-usernames").address + + +type + ENSType* {.pure.} = enum + IPFS, + SWARM, + IPNS, + UNKNOWN + +proc decodeENSContentHash*(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..