feat: use base32 + status infura ipfs for ens contenthash

This commit is contained in:
Richard Ramos 2020-09-30 13:46:51 -04:00 committed by Iuri Matias
parent a9d9aeb0d3
commit e8183f3a9f
5 changed files with 23 additions and 6 deletions

6
.gitmodules vendored
View File

@ -85,3 +85,9 @@
[submodule "vendor/nim-eth"]
path = vendor/nim-eth
url = https://github.com/status-im/nim-eth
[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

View File

@ -5,14 +5,17 @@ import ../../status/libstatus/core
import ../../status/libstatus/settings as status_settings
import json, json_serialization, sets, strutils
import chronicles
import nbaser
from base32 import nil
const AUTH_METHODS = toHashSet(["eth_accounts", "eth_coinbase", "eth_sendTransaction", "eth_sign", "keycard_signTypedData", "eth_signTypedData", "personal_sign", "personal_ecRecover"])
const SIGN_METHODS = toHashSet(["eth_sendTransaction", "personal_sign", "eth_signTypedData", "eth_signTypedData_v3"])
const ACC_METHODS = toHashSet(["eth_accounts", "eth_coinbase"])
const IPFS_SCHEME = "https"
const IPFS_GATEWAY = "ipfs.status.im"
const IPFS_PATH_PREFIX = "/ipfs/"
const IPFS_GATEWAY = ".infura.status.im"
const base58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
logScope:
topics = "provider-view"
@ -165,15 +168,21 @@ QtObject:
QtProperty[int] networkId:
read = getNetworkId
proc toString(bytes: openarray[byte]): string =
result = newString(bytes.len)
copyMem(result[0].addr, bytes[0].unsafeAddr, bytes.len)
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 == "0x": # ENS does not have a content hash
if contentHash == "": # ENS does not have a content hash
return url_replaceHostAndAddPath(url, IPFS_SCHEME, url_host(url), "")
let path = IPFS_PATH_PREFIX & contentHash.decodeContentHash() & "/"
result = url_replaceHostAndAddPath(url, IPFS_SCHEME, IPFS_GATEWAY, path)
let decodedContentHash = contentHash.decodeContentHash()
let base32Hash = base32.encode(base58.decode(decodedContentHash).toString()).toLowerAscii().replace("=", "")
result = url_replaceHostAndAddPath(url, IPFS_SCHEME, base32Hash & IPFS_GATEWAY)
proc getHost*(self: Web3ProviderView, url: string): string {.slot.} =
result = url_host(url)

View File

@ -141,7 +141,7 @@ proc contenthash*(ensAddr: string): string =
let response = callPrivateRPC("eth_call", payload)
let bytesResponse = response.parseJson["result"].getStr;
if bytesResponse == "0x":
return "0x"
return ""
let size = fromHex(Stuint[256], bytesResponse[66..129]).toInt
result = bytesResponse[130..129+size*2]

1
vendor/nbaser vendored Submodule

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

1
vendor/nim-base32 vendored Submodule

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