2021-09-08 18:05:39 +00:00
|
|
|
import ens, provider
|
|
|
|
import stew/byteutils
|
2021-09-29 13:42:34 +00:00
|
|
|
from stew/base32 import nil
|
|
|
|
from stew/base58 import nil
|
2021-09-08 18:05:39 +00:00
|
|
|
import chronicles, httpclient, net
|
2021-09-29 13:42:34 +00:00
|
|
|
import strutils
|
2021-09-08 18:05:39 +00:00
|
|
|
import semver
|
|
|
|
import constants
|
|
|
|
|
|
|
|
|
|
|
|
type
|
|
|
|
VersionInfo* = object
|
|
|
|
version*: string
|
|
|
|
url*: string
|
|
|
|
|
|
|
|
proc getLatestVersion*(): VersionInfo =
|
2021-09-29 13:42:34 +00:00
|
|
|
let contentHash = contenthash(APP_UPDATES_ENS)
|
|
|
|
if contentHash == "":
|
|
|
|
raise newException(ValueError, "ENS does not have a content hash")
|
2021-09-08 18:05:39 +00:00
|
|
|
|
2021-09-29 13:42:34 +00:00
|
|
|
var url: string = ""
|
2021-09-08 18:05:39 +00:00
|
|
|
|
2021-09-29 13:42:34 +00:00
|
|
|
let decodedHash = contentHash.decodeENSContentHash()
|
|
|
|
|
|
|
|
case decodedHash[0]:
|
2021-09-08 18:05:39 +00:00
|
|
|
of ENSType.IPFS:
|
2021-09-29 13:42:34 +00:00
|
|
|
let
|
|
|
|
base58bytes = base58.decode(base58.BTCBase58, decodedHash[1])
|
|
|
|
base32Hash = base32.encode(base32.Base32Lower, base58bytes)
|
|
|
|
|
2021-09-08 18:05:39 +00:00
|
|
|
url = "https://" & base32Hash & IPFS_GATEWAY
|
2021-09-29 13:42:34 +00:00
|
|
|
|
2021-09-08 18:05:39 +00:00
|
|
|
of ENSType.SWARM:
|
|
|
|
url = "https://" & SWARM_GATEWAY & "/bzz:/" & decodedHash[1]
|
2021-09-29 13:42:34 +00:00
|
|
|
|
2021-09-08 18:05:39 +00:00
|
|
|
of ENSType.IPNS:
|
|
|
|
url = "https://" & decodedHash[1]
|
2021-09-29 13:42:34 +00:00
|
|
|
|
|
|
|
else:
|
2021-09-08 18:05:39 +00:00
|
|
|
warn "Unknown content for", contentHash
|
|
|
|
raise newException(ValueError, "Unknown content for " & contentHash)
|
|
|
|
|
2021-09-29 13:42:34 +00:00
|
|
|
# Read version from folder
|
|
|
|
let secureSSLContext = newContext()
|
|
|
|
let client = newHttpClient(sslContext = secureSSLContext, timeout = CHECK_VERSION_TIMEOUT_MS)
|
|
|
|
result.version = client.getContent(url & "/VERSION").strip()
|
|
|
|
result.url = url
|
2021-09-08 18:05:39 +00:00
|
|
|
|
|
|
|
proc isNewer*(currentVersion, versionToCheck: string): bool =
|
|
|
|
let lastVersion = parseVersion(versionToCheck)
|
|
|
|
let currVersion = parseVersion(currentVersion)
|
|
|
|
result = lastVersion > currVersion
|