status-lib/status/updates.nim

53 lines
1.5 KiB
Nim
Raw Permalink Normal View History

2021-09-08 18:05:39 +00:00
import ens, provider
import stew/byteutils
from stew/base32 import nil
from stew/base58 import nil
2021-09-08 18:05:39 +00:00
import chronicles, httpclient, net
import strutils
2021-09-08 18:05:39 +00:00
import semver
import constants
type
VersionInfo* = object
version*: string
url*: string
proc getLatestVersion*(): VersionInfo =
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
var url: string = ""
2021-09-08 18:05:39 +00:00
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
url = "https://" & base32Hash & IPFS_GATEWAY
2021-09-08 18:05:39 +00:00
of ENSType.SWARM:
url = "https://" & SWARM_GATEWAY & "/bzz:/" & decodedHash[1]
2021-09-08 18:05:39 +00:00
of ENSType.IPNS:
url = "https://" & decodedHash[1]
else:
2021-09-08 18:05:39 +00:00
warn "Unknown content for", contentHash
raise newException(ValueError, "Unknown content for " & contentHash)
# 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