status-lib/status/provider.nim

58 lines
1.7 KiB
Nim
Raw Normal View History

2021-09-08 18:05:39 +00:00
import ens, wallet, permissions, utils
import ../eventemitter
import ./types/[setting, permission]
2021-09-08 18:05:39 +00:00
import utils
import statusgo_backend/accounts
import statusgo_backend/core
import statusgo_backend/settings as status_settings
2021-09-08 18:05:39 +00:00
import json, json_serialization, sets, strutils
import chronicles
import stew/byteutils
from stew/base32 import nil
from stew/base58 import nil
2021-09-08 18:05:39 +00:00
const HTTPS_SCHEME* = "https"
const IPFS_GATEWAY* = ".infura.status.im"
const SWARM_GATEWAY* = "swarm-gateways.net"
logScope:
topics = "provider-model"
type ProviderModel* = ref object
events*: EventEmitter
permissions*: PermissionsModel
2021-09-10 17:27:49 +00:00
wallet*: WalletModel
2021-09-08 18:05:39 +00:00
2021-09-10 17:27:49 +00:00
proc newProviderModel*(events: EventEmitter, permissions: PermissionsModel, wallet: WalletModel): ProviderModel =
2021-09-08 18:05:39 +00:00
result = ProviderModel()
result.events = events
result.permissions = permissions
2021-09-10 17:27:49 +00:00
result.wallet = wallet
2021-09-08 18:05:39 +00:00
proc ensResourceURL*(self: ProviderModel, ens: string, url: string):
(string, string, string, string, bool) =
2021-09-08 18:05:39 +00:00
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]:
2021-09-08 18:05:39 +00:00
of ENSType.IPFS:
let
base58bytes = base58.decode(base58.BTCBase58, decodedHash[1])
base32Hash = base32.encode(base32.Base32Lower, base58bytes)
2021-09-08 18:05:39 +00:00
result = (url, base32Hash & IPFS_GATEWAY, HTTPS_SCHEME, "", true)
2021-09-08 18:05:39 +00:00
of ENSType.SWARM:
result = (url, SWARM_GATEWAY, HTTPS_SCHEME,
"/bzz:/" & decodedHash[1] & "/", true)
2021-09-08 18:05:39 +00:00
of ENSType.IPNS:
result = (url, decodedHash[1], HTTPS_SCHEME, "", true)
else:
2021-09-08 18:05:39 +00:00
warn "Unknown content for", ens, contentHash