mirror of
https://github.com/pezkuwichain/pezkuwi-wallet-android.git
synced 2026-11-20 01:26:11 +00:00
Resolve and serve .kurd sites from verified content, never via DNS (#37)
* Resolve .kurd names on chain and keep them away from DNS and search The Navnisan pallet is live on Zagros People. The wallet now reads a name's record and every ancestor's at one finalized block, checks that each record sits under the parent its name implies, and judges validity the way the pallet does (expiry, 30-day grace for leases, a subname valid only under the registration it was set against). Records are decoded by hand, so a lookup does not depend on the chain's runtime being loaded; the decoder is tested against records read from the live chain, and namehash against the vectors the pallet pins. Names are read through the light client, which proves each value. The chain's own connection is RPC first, so it is not used for names. When the light client cannot answer, two RPC nodes on different hosts must return the same records; an answer reached that way is marked unverified, and an official, institutional, reserved or zone name is then refused rather than opened. The light client tracked its users with one set, so the RPC fallback releasing a chain would stop it under any other reader. Each holder now keeps its own lease. A .kurd host never reaches system DNS or the search engine: the search box offers no web address or search for it, and the DApp browser answers every request to such a host itself, including loadUrl and resources a page embeds. The DuckDuckGo query is now URL-encoded. * Verify .kurd site content block by block A name's content is an IPFS CID on chain; mirrors serve it as a CAR. Every block in the CAR is checked against its CID while it is read, and one block that does not match refuses the whole CAR, so a mirror can withhold content but cannot change it. Files are then assembled only by following CIDs taken from blocks already checked, starting from the CID the chain gave rather than the roots the CAR claims. Supported: CIDv1 and CIDv0, dag-pb and raw, sha2-256 and inlined identity blocks; UnixFS files of any chunking, plain directories and HAMT-sharded directories (murmur3-x64-64, as Kubo builds them). An ipns contenthash, a symlink, an escaping path segment or a file over 32 MiB is refused. Tested against CARs Kubo produced, taken unchanged from ipfs/gateway-conformance (MIT / Apache-2.0), including a 238-shard HAMT, a CAR with a missing block and nodes with their fields in either order; murmur3 also against Guava's reference vector. * Serve .kurd sites from verified content under their own origin The DApp browser now answers https://<name>.kurd itself: the name is resolved on chain, its content root fetched from IPFS trustless gateways as a CAR for the requested path, verified block by block, and only then served. The page's origin is the full name, so cookies, storage and permissions belong to that name alone, and every response carries a policy the wallet writes: nothing loads or connects outside the name, no workers, no framing. Gateways are tried in order and one that fails, withholds or changes a block is skipped. A gateway saying "not found" is believed only once the parent directory, verified, shows the name is absent. A directory is served through its index.html. The gateway list is the signed kurd/gateways.json from the config repository when it can be read, otherwise the gateways measured reachable from Turkiye; entries can only be https origins. Bad paths (dot segments, encoded slashes, malformed escapes, non-UTF-8) are refused before any lookup. A refused, missing or unavailable name, ipns content and feeds get their own status; a name's resolution is reused for a minute so one page does not resolve it once per resource. * Give the runtime component the app's HTTP client for .kurd content The site server fetches CARs over the shared client, so gateway requests use the same DNS-over-HTTPS-first resolver as the rest of the app. * Hear the DApp bridge only from the top frame of the page shown The polkadot-js and MetaMask bridges were exposed with addJavascriptInterface, which every frame of a page can call. A third-party frame inside an authorised DApp -- an ad, an embed -- could ask for the account list or a signature, and the request was shown in the DApp's name, because authorisation follows the page the tab shows. The bridges now use a web message listener: the WebView reports each message's origin and frame, and only a message from the top frame whose origin is the page shown is heard. On a WebView too old to report them, the bridge stays off instead of trusting the page. The bridge is also added before the tab's first load, since it reaches only documents loaded after it. * Let .kurd pages reach the chain through the wallet's connection A .kurd page may only connect to its own name, so it cannot open a node connection of its own. The provider half of the polkadot-js extension API (pub(rpc.listProviders, startProvider, send, subscribe, subscribeConnected)) now answers it over the wallet's connection to Pezkuwi and Zagros relay, Asset Hub and People, which falls back to the light client. Offered only when the tab shows a .kurd page, judged by the WebView's page rather than the message. Only calls that read state, estimate fees or submit an extrinsic the user already signed pass; key, peer and offchain methods do not. A subscription ends when the tab leaves the page's origin. Calls are answered off the request queue so a slow node does not hold up the page, and params are forwarded as the page wrote them rather than reparsed (a block number must not become 123.0). * Offer a typed .kurd name in DApp search as the name itself Typing a valid .kurd name now shows it under its own heading, with the shield and "resolved on chain, never sent to a search engine", and opens https://<name>/ in the browser. An invalid one is offered nowhere: not as a web address, not as a search. * Let runtime unit tests log a warning with its cause The test stand-in for android.util.Log had no w(tag, msg, throwable), so any code under test that logged a failure that way stopped with NoSuchMethodError instead of carrying on: the .kurd fetcher logs each gateway it skips. * Show what was verified about a .kurd page, and stop at what was not The address bar of a .kurd page carries a badge in place of the padlock: Official for a name Parliament gave, Registered for a bought one, Not verified for one the reader chose to open on two RPC nodes' word. Tapping it shows the block the name was proven at, the owner (a state institution or an account), the content CID, and a button to clear the site's storage, cookies and permissions. A page that did not open is replaced by a notice instead of a blank page: an official name that cannot be verified now (retry, never open), a name read through RPC nodes only (go back, or open anyway), an unregistered name, a name without a site, and content no mirror could deliver verifiably. Behind it the site server keeps each name's page state, set only by the page's own document, and holds back a name read through RPC nodes until the reader chooses to open it. Texts in Kurmanji, English and Turkish. * Warn before opening a .kurd name that imitates an official one Zone names are institutional by construction, and the commonest imitation folds the zone into the label: serok-gov.kurd or serokgov.kurd for serok.gov.kurd. When a typed name has such a candidate and the candidate resolves, proven, as an official name, search asks first: go to the official name, or open the typed one anyway. The check runs only for names with a candidate and gives up after a few seconds; the opened page still shows by its badge whether it is official. * Say on the signing screen what kind of .kurd name asks A signature requested from a .kurd page now names the page by its .kurd name with its kind: Official, Bought (not official) or Not verified. For a name that is not official, the reader is told to check the full name, since the state did not give it; for one opened without proof, to sign only when sure whose site it is. * Judge a DApp request by the page that sent it, not the page shown by then Bridge requests are handled one at a time, after they arrive. A page could send a request and navigate at once, and its request was then authorised, and named on the signing screen, as the next page's: a .kurd page could have its signature request shown as Official by moving to an official name. Every request now carries the origin the WebView reported for its message (the polkadot-js url the page writes is no longer read). A request whose origin is not the page shown when it is handled is refused, and the signing screen is built from the asking origin's own .kurd state, refusing as well if the tab has moved on. * Serve .kurd feeds from their publisher's newest signed release A feed name (a site that changes many times a day) carries its publisher's key and an anchor: the newest release the chain was told of. The wallet asks the gateways for the publisher's IPNS record, verifies each against the key the chain gives (never the key in the record or its name), refuses expired ones, V1-only ones and ones whose legacy fields contradict the signed data, and serves the newest release at or past the anchor. When none is confirmed, it serves the anchor itself and the browser says that a newer version could not be found; the release the chain already names is always served from the chain's root. Verification is tested against records Kubo signed, taken unchanged from ipfs/gateway-conformance (MIT / Apache-2.0).
This commit is contained in:
1 parent
cf74df7cf4
commit
425079281c
106 files changed
+4786
-97
No files matched your search
@@ -28,6 +28,15 @@ import io.novafoundation.nova.runtime.multiNetwork.connection.Web3ApiPool
|
||||
import io.novafoundation.nova.runtime.multiNetwork.connection.autobalance.NodeAutobalancer
|
||||
import io.novafoundation.nova.runtime.multiNetwork.connection.light.LightClientSpecApi
|
||||
import io.novafoundation.nova.runtime.multiNetwork.connection.light.LightClients
|
||||
import io.novafoundation.nova.runtime.names.KurdNameResolver
|
||||
import io.novafoundation.nova.runtime.names.RealKurdNameResolver
|
||||
import io.novafoundation.nova.runtime.names.content.KurdContentFetcher
|
||||
import io.novafoundation.nova.runtime.names.content.KurdFeedResolver
|
||||
import io.novafoundation.nova.runtime.names.content.KurdGatewayListApi
|
||||
import io.novafoundation.nova.runtime.names.content.KurdSiteServer
|
||||
import io.novafoundation.nova.runtime.names.content.OkHttpCarSource
|
||||
import io.novafoundation.nova.runtime.names.content.OkHttpIpnsRecordSource
|
||||
import io.novafoundation.nova.runtime.names.content.RealKurdGateways
|
||||
import io.novafoundation.nova.runtime.multiNetwork.connection.autobalance.strategy.NodeSelectionStrategyProvider
|
||||
import io.novafoundation.nova.runtime.multiNetwork.connection.node.healthState.NodeHealthStateTesterFactory
|
||||
import io.novafoundation.nova.runtime.multiNetwork.runtime.AsyncChainSyncDispatcher
|
||||
@@ -190,6 +199,35 @@ class ChainRegistryModule {
|
||||
networkApiCreator: NetworkApiCreator,
|
||||
) = LightClients(context, encryptedFirstDns, networkApiCreator.create(LightClientSpecApi::class.java))
|
||||
|
||||
@Provides
|
||||
@ApplicationScope
|
||||
fun provideKurdNameResolver(
|
||||
lightClients: LightClients,
|
||||
socketProvider: Provider<SocketService>,
|
||||
chainRegistry: ChainRegistry,
|
||||
connectionSecrets: ConnectionSecrets,
|
||||
): KurdNameResolver = RealKurdNameResolver(
|
||||
// Names live on the testnet until the mainnet People chain is launched with the pallet
|
||||
chainId = LightClients.ZAGROS_PEOPLE,
|
||||
lightClients = lightClients,
|
||||
socketProvider = socketProvider,
|
||||
chainRegistry = chainRegistry,
|
||||
connectionSecrets = connectionSecrets,
|
||||
)
|
||||
|
||||
@Provides
|
||||
@ApplicationScope
|
||||
fun provideKurdSiteServer(
|
||||
kurdNameResolver: KurdNameResolver,
|
||||
networkApiCreator: NetworkApiCreator,
|
||||
okHttpClient: OkHttpClient,
|
||||
): KurdSiteServer {
|
||||
val gateways = RealKurdGateways(networkApiCreator.create(KurdGatewayListApi::class.java))
|
||||
val fetcher = KurdContentFetcher(gateways, OkHttpCarSource(okHttpClient))
|
||||
val feeds = KurdFeedResolver(gateways, OkHttpIpnsRecordSource(okHttpClient))
|
||||
return KurdSiteServer(kurdNameResolver, fetcher, feeds)
|
||||
}
|
||||
|
||||
@Provides
|
||||
@ApplicationScope
|
||||
fun provideChainConnectionFactory(
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package io.novafoundation.nova.runtime.di
|
||||
|
||||
import io.novafoundation.nova.runtime.names.KurdNameResolver
|
||||
import io.novafoundation.nova.runtime.names.content.KurdSiteServer
|
||||
import com.google.gson.Gson
|
||||
import io.novafoundation.nova.core.storage.StorageCache
|
||||
import io.novafoundation.nova.runtime.call.MultiChainRuntimeCallsApi
|
||||
@@ -62,6 +64,10 @@ interface RuntimeApi {
|
||||
|
||||
fun chainRegistry(): ChainRegistry
|
||||
|
||||
fun kurdNameResolver(): KurdNameResolver
|
||||
|
||||
fun kurdSiteServer(): KurdSiteServer
|
||||
|
||||
fun rpcCalls(): RpcCalls
|
||||
|
||||
@ExtrinsicSerialization
|
||||
|
||||
@@ -11,6 +11,7 @@ import io.novafoundation.nova.core_db.dao.ChainAssetDao
|
||||
import io.novafoundation.nova.core_db.dao.ChainDao
|
||||
import io.novafoundation.nova.core_db.dao.StorageDao
|
||||
import io.novasama.substrate_sdk_android.wsrpc.SocketService
|
||||
import okhttp3.OkHttpClient
|
||||
|
||||
interface RuntimeDependencies {
|
||||
|
||||
@@ -35,4 +36,6 @@ interface RuntimeDependencies {
|
||||
fun assetsIconModeService(): AssetsIconModeRepository
|
||||
|
||||
fun encryptedFirstDns(): EncryptedFirstDns
|
||||
|
||||
fun okHttpClient(): OkHttpClient
|
||||
}
|
||||
+2
-2
@@ -208,7 +208,7 @@ class ChainConnection internal constructor(
|
||||
policy.onLightUnavailable(rpcNodeCount())
|
||||
emit(latestRpc)
|
||||
} else if (policy.onRpcFailure(rpcNodeCount(), System.currentTimeMillis())) {
|
||||
val url = lightClients.url(chain.value.id)
|
||||
val url = lightClients.url(chain.value.id, holder = this@ChainConnection)
|
||||
Log.d(LIGHT_FALLBACK_TAG, "${chain.value.name}: light client url $url")
|
||||
if (url == null) {
|
||||
policy.onLightUnavailable(rpcNodeCount())
|
||||
@@ -229,7 +229,7 @@ class ChainConnection internal constructor(
|
||||
if (policy.shouldReleaseLight(now)) {
|
||||
Log.d(LIGHT_FALLBACK_TAG, "${chain.value.name}: RPC is stable, releasing the light client")
|
||||
light = null
|
||||
lightClients.release(chain.value.id)
|
||||
lightClients.release(chain.value.id, holder = this@ChainConnection)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package io.novafoundation.nova.runtime.multiNetwork.connection.light
|
||||
|
||||
import io.novafoundation.nova.runtime.multiNetwork.chain.model.ChainId
|
||||
|
||||
/**
|
||||
* Who is using which light client chain. A chain is wanted by more than one reader at a time -- a
|
||||
* connection that fell back to it, the `.kurd` resolver -- so one reader letting go must not stop
|
||||
* it under another. Holders are compared by identity, so asking twice and releasing once balances.
|
||||
*/
|
||||
internal class LightClientLeases(private val relayOf: (ChainId) -> ChainId?) {
|
||||
|
||||
private val holders = mutableMapOf<ChainId, MutableSet<Any>>()
|
||||
|
||||
fun acquire(chainId: ChainId, holder: Any) {
|
||||
holders.getOrPut(chainId) { mutableSetOf() } += holder
|
||||
}
|
||||
|
||||
fun release(chainId: ChainId, holder: Any) {
|
||||
val chainHolders = holders[chainId] ?: return
|
||||
chainHolders -= holder
|
||||
if (chainHolders.isEmpty()) holders -= chainId
|
||||
}
|
||||
|
||||
fun isHeld(chainId: ChainId): Boolean = holders[chainId].orEmpty().isNotEmpty()
|
||||
|
||||
/** One of [registered] that nothing holds and that is not the relay of a registered chain. */
|
||||
fun nextUnused(registered: Set<ChainId>): ChainId? = registered.firstOrNull { key ->
|
||||
!isHeld(key) && registered.none { relayOf(it) == key }
|
||||
}
|
||||
}
|
||||
+19
-15
@@ -43,18 +43,21 @@ class LightClients(
|
||||
private val mutex = Mutex()
|
||||
private var baseUrl: String? = null
|
||||
private val registered = mutableSetOf<ChainId>()
|
||||
private val inUse = mutableSetOf<ChainId>()
|
||||
private val leases = LightClientLeases(relayOf = { chains.getValue(it).relay })
|
||||
|
||||
fun supports(chainId: ChainId): Boolean = chainId in chains
|
||||
|
||||
/** Loopback JSON-RPC URL of [chainId], or null when the light client cannot serve it. */
|
||||
suspend fun url(chainId: ChainId): String? = mutex.withLock {
|
||||
/**
|
||||
* Loopback JSON-RPC URL of [chainId], or null when the light client cannot serve it. The chain
|
||||
* keeps syncing until every [holder] that asked for it has [release]d it.
|
||||
*/
|
||||
suspend fun url(chainId: ChainId, holder: Any): String? = mutex.withLock {
|
||||
withContext(Dispatchers.IO) {
|
||||
runCatching {
|
||||
if (!supports(chainId)) return@runCatching null
|
||||
val base = baseUrl ?: PezkuwiLight.start(File(context.filesDir, DATABASE_DIR), resolver()).also { baseUrl = it }
|
||||
register(chainId)
|
||||
inUse += chainId
|
||||
leases.acquire(chainId, holder)
|
||||
PezkuwiLight.chainUrl(base, chainId)
|
||||
}.onFailure {
|
||||
Log.w(TAG, "Light client unavailable for $chainId", it)
|
||||
@@ -63,14 +66,15 @@ class LightClients(
|
||||
}
|
||||
|
||||
/**
|
||||
* [chainId] no longer needs the light client, so it stops syncing: a synced chain costs data
|
||||
* and CPU while nothing reads it. A relay chain goes once none of its teyrchains is in use.
|
||||
* [holder] no longer needs [chainId]. Once no holder does, the chain stops syncing: a synced
|
||||
* chain costs data and CPU while nothing reads it. A relay chain goes once none of its
|
||||
* teyrchains is in use.
|
||||
*/
|
||||
suspend fun release(chainId: ChainId) = mutex.withLock {
|
||||
suspend fun release(chainId: ChainId, holder: Any) = mutex.withLock {
|
||||
withContext(Dispatchers.IO) {
|
||||
inUse -= chainId
|
||||
leases.release(chainId, holder)
|
||||
while (true) {
|
||||
val unused = registered.firstOrNull { key -> key !in inUse && registered.none { chains.getValue(it).relay == key } } ?: break
|
||||
val unused = leases.nextUnused(registered) ?: break
|
||||
// Still registered on failure, so a later url() does not register it twice
|
||||
runCatching { PezkuwiLight.removeChain(unused) }
|
||||
.onSuccess { registered -= unused }
|
||||
@@ -132,15 +136,15 @@ class LightClients(
|
||||
dns.lookup(host).mapNotNull { it.hostAddress }.toTypedArray()
|
||||
}
|
||||
|
||||
private companion object {
|
||||
companion object {
|
||||
const val ZAGROS: ChainId = "1b0b4727bbb5e44ed587d1056b4ad537ffb330034509a58866a088d77e792cd5"
|
||||
const val ZAGROS_PEOPLE: ChainId = "c72b38f36affaeb2661018b201c2be02e8c09fc699640525d39200a10fd77878"
|
||||
|
||||
// Not LOG_TAG: called inside a coroutine, it gives the coroutine class name
|
||||
const val TAG = "LightClients"
|
||||
const val DATABASE_DIR = "light-client"
|
||||
const val SPECS_DIR = "light-client-specs"
|
||||
const val BUNDLED_DIR = "light-client/"
|
||||
const val SPEC_SOURCE = "https://raw.githubusercontent.com/pezkuwichain/pezkuwi-wallet-utils/master/light-client/"
|
||||
private const val TAG = "LightClients"
|
||||
private const val DATABASE_DIR = "light-client"
|
||||
private const val SPECS_DIR = "light-client-specs"
|
||||
private const val BUNDLED_DIR = "light-client/"
|
||||
private const val SPEC_SOURCE = "https://raw.githubusercontent.com/pezkuwichain/pezkuwi-wallet-utils/master/light-client/"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package io.novafoundation.nova.runtime.names
|
||||
|
||||
/**
|
||||
* Official names a typed one could be mistaken for. Zone names are institutional by construction
|
||||
* (`*.gov.kurd`, `*.edu.kurd`, …), and the commonest imitation flattens the zone into the label:
|
||||
* `serok-gov.kurd` or `serokgov.kurd` for `serok.gov.kurd`. Candidates are only guesses: a warning
|
||||
* is shown once one of them resolves, proven, as an official name.
|
||||
*/
|
||||
object KurdLookalikes {
|
||||
|
||||
/** Zones created at genesis (decisions D6, D13). */
|
||||
val ZONES = listOf("gov", "edu", "mil", "int", "org", "net")
|
||||
|
||||
fun candidates(name: KurdName): List<KurdName> {
|
||||
val top = name.top
|
||||
if (top in ZONES) return emptyList()
|
||||
|
||||
return ZONES.mapNotNull { zone ->
|
||||
val prefix = when {
|
||||
top.endsWith("-$zone") -> top.removeSuffix("-$zone")
|
||||
top.endsWith(zone) -> top.removeSuffix(zone)
|
||||
else -> return@mapNotNull null
|
||||
}
|
||||
if (!KurdName.isValidLabel(prefix)) return@mapNotNull null
|
||||
|
||||
KurdName.parse((name.labels.dropLast(1) + prefix + zone).joinToString(".") + KurdName.SUFFIX)
|
||||
}.distinct()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package io.novafoundation.nova.runtime.names
|
||||
|
||||
import io.novasama.substrate_sdk_android.hash.Hasher.blake2b256
|
||||
|
||||
/**
|
||||
* A `.kurd` name in the canonical form the Navnisan pallet keys records by.
|
||||
*
|
||||
* The chain does not normalise: it accepts canonical bytes or refuses them. So the wallet lower-cases
|
||||
* ASCII first and then applies the pallet's label rules (`label::validate`, `Use::Sub`); input that
|
||||
* fails them is never looked up anywhere, and in particular never handed to a search engine.
|
||||
*/
|
||||
class KurdName private constructor(
|
||||
/** Labels from the leftmost down to the one directly under `.kurd`: `dks.gov.kurd` is `[dks, gov]`. */
|
||||
val labels: List<String>,
|
||||
) {
|
||||
|
||||
val name: String = labels.joinToString(".") + SUFFIX
|
||||
|
||||
/** The top-level label: `gov` for `dks.gov.kurd`. */
|
||||
val top: String get() = labels.last()
|
||||
|
||||
/** `node(name)`, as `pezpallet_navnisan::Pallet::node_of` computes it. */
|
||||
val node: ByteArray by lazy { labels.foldRight(KURD_NODE) { label, parent -> childNode(parent, label) } }
|
||||
|
||||
/** Nodes of this name and each ancestor, this name first, the top-level name last. */
|
||||
fun nodesUpward(): List<ByteArray> = labels.indices.map { from -> KurdName(labels.drop(from)).node }
|
||||
|
||||
override fun equals(other: Any?) = other is KurdName && other.name == name
|
||||
|
||||
override fun hashCode() = name.hashCode()
|
||||
|
||||
override fun toString() = name
|
||||
|
||||
companion object {
|
||||
|
||||
const val SUFFIX = ".kurd"
|
||||
|
||||
const val MAX_LABEL = 63
|
||||
const val MAX_DEPTH = 4
|
||||
const val MAX_NAME = 253
|
||||
|
||||
/** `node("kurd")`: never stored, every top-level name's parent. */
|
||||
val KURD_NODE: ByteArray = (ByteArray(32) + "kurd".toByteArray().blake2b256()).blake2b256()
|
||||
|
||||
/** True for `kurd` and any host under it, valid name or not: none of them may reach system DNS. */
|
||||
fun isKurdHost(host: String?): Boolean {
|
||||
val normalized = host?.lowercase(java.util.Locale.ROOT)?.trimEnd('.') ?: return false
|
||||
return normalized == "kurd" || normalized.endsWith(SUFFIX)
|
||||
}
|
||||
|
||||
/**
|
||||
* True when [input] names a `.kurd` host, valid name or not, with or without a scheme or path:
|
||||
* such input is never treated as a web address or a search query.
|
||||
*/
|
||||
fun mentionsKurd(input: String): Boolean {
|
||||
val afterScheme = input.trim().substringAfter("://")
|
||||
val authority = afterScheme.takeWhile { it !in "/?#" }
|
||||
val host = authority.substringAfterLast('@').substringBefore(':')
|
||||
return isKurdHost(host)
|
||||
}
|
||||
|
||||
/**
|
||||
* The name in [input], or null when it is not a valid `.kurd` name. Accepts surrounding
|
||||
* whitespace, a trailing dot or slash and an `https://` prefix; anything else around the
|
||||
* name (a path, a port, user info) makes it invalid rather than being ignored.
|
||||
*/
|
||||
fun parse(input: String): KurdName? {
|
||||
var text = input.trim()
|
||||
if (text.startsWith("https://", ignoreCase = true)) text = text.substring("https://".length)
|
||||
text = text.trimEnd('/').trimEnd('.')
|
||||
if (text.any { it.code > 0x7f }) return null
|
||||
text = text.lowercase(java.util.Locale.ROOT)
|
||||
|
||||
if (text.length > MAX_NAME || !text.endsWith(SUFFIX)) return null
|
||||
val labels = text.removeSuffix(SUFFIX).split('.')
|
||||
if (labels.size > MAX_DEPTH || !labels.all(::isValidLabel)) return null
|
||||
|
||||
return KurdName(labels)
|
||||
}
|
||||
|
||||
/** `a-z`, `0-9` and `-`, 1 to 63 bytes, no hyphen at either end and no `--` anywhere. */
|
||||
fun isValidLabel(label: String): Boolean {
|
||||
if (label.isEmpty() || label.length > MAX_LABEL) return false
|
||||
if (!label.all { it in 'a'..'z' || it in '0'..'9' || it == '-' }) return false
|
||||
if (label.first() == '-' || label.last() == '-') return false
|
||||
return "--" !in label
|
||||
}
|
||||
|
||||
fun childNode(parent: ByteArray, label: String): ByteArray = (parent + label.toByteArray().blake2b256()).blake2b256()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
package io.novafoundation.nova.runtime.names
|
||||
|
||||
import android.util.Log
|
||||
import io.novafoundation.nova.common.data.network.runtime.calls.GetFinalizedHeadRequest
|
||||
import io.novafoundation.nova.common.utils.awaitConnected
|
||||
import io.novafoundation.nova.runtime.multiNetwork.ChainRegistry
|
||||
import io.novafoundation.nova.runtime.multiNetwork.chain.model.Chain
|
||||
import io.novafoundation.nova.runtime.multiNetwork.chain.model.ChainId
|
||||
import io.novafoundation.nova.runtime.multiNetwork.connection.ConnectionSecrets
|
||||
import io.novafoundation.nova.runtime.multiNetwork.connection.light.LightClients
|
||||
import io.novafoundation.nova.runtime.multiNetwork.connection.saturateNodeUrl
|
||||
import io.novasama.substrate_sdk_android.extensions.fromHex
|
||||
import io.novasama.substrate_sdk_android.extensions.toHexString
|
||||
import io.novasama.substrate_sdk_android.wsrpc.SocketService
|
||||
import io.novasama.substrate_sdk_android.wsrpc.executeAsync
|
||||
import io.novasama.substrate_sdk_android.wsrpc.mappers.nonNull
|
||||
import io.novasama.substrate_sdk_android.wsrpc.mappers.pojo
|
||||
import io.novasama.substrate_sdk_android.wsrpc.request.runtime.RuntimeRequest
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.awaitAll
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import kotlinx.coroutines.withTimeout
|
||||
import java.net.URI
|
||||
import javax.inject.Provider
|
||||
|
||||
/**
|
||||
* Resolves `.kurd` names from `Navnisan` on [chainId]: through the light client first, so a name
|
||||
* is proven rather than reported, and only when it cannot answer through two RPC nodes that must
|
||||
* agree (architecture §5.2).
|
||||
*
|
||||
* The chain's own connection is RPC first and moves to the light client only once every node
|
||||
* fails, so it is not used here: a name read through it would be the node's word.
|
||||
*/
|
||||
class RealKurdNameResolver(
|
||||
private val chainId: ChainId,
|
||||
private val lightClients: LightClients,
|
||||
private val socketProvider: Provider<SocketService>,
|
||||
private val chainRegistry: ChainRegistry,
|
||||
private val connectionSecrets: ConnectionSecrets,
|
||||
) : KurdNameResolver {
|
||||
|
||||
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
||||
private val leaseMutex = Mutex()
|
||||
private var leaseRelease: Job? = null
|
||||
|
||||
override suspend fun resolve(name: KurdName): KurdResolution {
|
||||
val light = runCatching { readThroughLightClient(name) }
|
||||
.onFailure { Log.w(TAG, "Light client could not resolve $name", it) }
|
||||
.getOrNull()
|
||||
if (light != null) return judge(name, light, verified = true)
|
||||
|
||||
val urls = rpcUrls()
|
||||
if (urls.size < AGREEING_NODES) return refuseOrGiveUp(name)
|
||||
|
||||
val lookups = runCatching {
|
||||
coroutineScope {
|
||||
urls.take(AGREEING_NODES).map { url -> async { withSocket(url) { NameLookup.read(SocketStorageReader(it), name) } } }.awaitAll()
|
||||
}
|
||||
}.onFailure { Log.w(TAG, "RPC nodes could not resolve $name", it) }.getOrNull() ?: return refuseOrGiveUp(name)
|
||||
|
||||
val first = lookups.first()
|
||||
if (!lookups.all { it.agreesWith(first) }) return refuseOrGiveUp(name)
|
||||
|
||||
return judge(name, first, verified = false)
|
||||
}
|
||||
|
||||
private suspend fun readThroughLightClient(name: KurdName): NameLookup {
|
||||
val url = holdLightClient() ?: error("Light client unavailable")
|
||||
return withTimeout(LIGHT_TIMEOUT_MILLIS) {
|
||||
withSocket(url) { NameLookup.read(SocketStorageReader(it), name) }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Keeps the names chain syncing between lookups, so a second name does not wait for a cold
|
||||
* start, and lets it go once nothing has been resolved for a while.
|
||||
*/
|
||||
private suspend fun holdLightClient(): String? = leaseMutex.withLock {
|
||||
leaseRelease?.cancel()
|
||||
val url = lightClients.url(chainId, holder = this)
|
||||
leaseRelease = scope.launch {
|
||||
delay(LIGHT_IDLE_MILLIS)
|
||||
lightClients.release(chainId, holder = this@RealKurdNameResolver)
|
||||
}
|
||||
url
|
||||
}
|
||||
|
||||
/** RPC node URLs of the names chain, at most one per host, so agreement is not one server twice. */
|
||||
private suspend fun rpcUrls(): List<String> {
|
||||
val chain = chainRegistry.getChain(chainId)
|
||||
return chain.nodes.nodes
|
||||
.filter { it.connectionType == Chain.Node.ConnectionType.WSS }
|
||||
.mapNotNull { it.saturateNodeUrl(connectionSecrets)?.saturatedUrl }
|
||||
.distinctBy { runCatching { URI(it).host }.getOrNull() ?: it }
|
||||
}
|
||||
|
||||
private suspend fun <T> withSocket(url: String, block: suspend (SocketService) -> T): T {
|
||||
val socket = socketProvider.get()
|
||||
socket.start(url)
|
||||
return try {
|
||||
socket.awaitConnected()
|
||||
block(socket)
|
||||
} finally {
|
||||
socket.stop()
|
||||
}
|
||||
}
|
||||
|
||||
private class SocketStorageReader(private val socket: SocketService) : NameStorageReader {
|
||||
|
||||
override suspend fun finalizedHead(): String = socket.executeAsync(GetFinalizedHeadRequest, mapper = pojo<String>().nonNull())
|
||||
|
||||
override suspend fun storage(key: ByteArray, at: String): ByteArray? {
|
||||
val request = RuntimeRequest(method = "state_getStorage", params = listOf(key.toHexString(withPrefix = true), at))
|
||||
return socket.executeAsync(request, mapper = pojo<String>()).result?.fromHex()
|
||||
}
|
||||
|
||||
override suspend fun blockNumber(at: String): Long? {
|
||||
val header = socket.executeAsync(RuntimeRequest(method = "chain_getHeader", params = listOf(at))).result as? Map<*, *>
|
||||
return (header?.get("number") as? String)?.removePrefix("0x")?.toLongOrNull(16)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
/**
|
||||
* D18: a proven answer stands; an unproven one opens only for a name nobody could take for an
|
||||
* institution's, and with a warning.
|
||||
*/
|
||||
internal fun judge(name: KurdName, lookup: NameLookup, verified: Boolean): KurdResolution = when (lookup) {
|
||||
NameLookup.NotFound, NameLookup.Lapsed -> KurdResolution.NotFound(name, verified)
|
||||
is NameLookup.Found -> when {
|
||||
verified -> KurdResolution.Resolved(name, lookup.record, lookup.status, verified = true, block = lookup.block)
|
||||
mustBeVerified(name, lookup) -> KurdResolution.Refused(name)
|
||||
else -> KurdResolution.Resolved(name, lookup.record, lookup.status, verified = false, block = lookup.block)
|
||||
}
|
||||
}
|
||||
|
||||
/** No answer at all: a zone name is known to be institutional without one. */
|
||||
internal fun refuseOrGiveUp(name: KurdName) = if (name.top in ZONES) KurdResolution.Refused(name) else KurdResolution.Unavailable(name)
|
||||
|
||||
private fun mustBeVerified(name: KurdName, found: NameLookup.Found): Boolean {
|
||||
return name.top in ZONES || found.locked || found.record.isOfficial || found.record.isInstitutional
|
||||
}
|
||||
|
||||
/** Zones created at genesis: every name under them belongs to an institution (decisions D6, D13). */
|
||||
val ZONES = setOf("gov", "edu", "mil", "int", "org", "net")
|
||||
|
||||
private const val AGREEING_NODES = 2
|
||||
|
||||
/** A cold light client within the acceptance target (architecture §1, criterion 2). */
|
||||
private const val LIGHT_TIMEOUT_MILLIS = 30_000L
|
||||
private const val LIGHT_IDLE_MILLIS = 5 * 60_000L
|
||||
|
||||
private const val TAG = "KurdNameResolver"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package io.novafoundation.nova.runtime.names
|
||||
|
||||
sealed interface KurdResolution {
|
||||
|
||||
val name: KurdName
|
||||
|
||||
/**
|
||||
* [verified]: read through the light client, which checks every value against the chain's
|
||||
* finalized state. Otherwise two independent RPC nodes returned the same records, and the
|
||||
* name is shown with a "not verified" warning.
|
||||
*/
|
||||
class Resolved(
|
||||
override val name: KurdName,
|
||||
val record: NameRecord,
|
||||
val status: NameStatus,
|
||||
val verified: Boolean,
|
||||
/** The finalized block the name was read at, when known. */
|
||||
val block: Long? = null,
|
||||
) : KurdResolution {
|
||||
|
||||
/** "Official" badge: only granted names and their descendants; a bought name never shows it. */
|
||||
val official: Boolean get() = record.isOfficial
|
||||
}
|
||||
|
||||
/** Not registered, expired, or orphaned by its parent's re-registration. */
|
||||
class NotFound(override val name: KurdName, val verified: Boolean) : KurdResolution
|
||||
|
||||
/**
|
||||
* Not verified, and a name that must never open unverified: official, institutional, reserved
|
||||
* or under a zone (decision D18).
|
||||
*/
|
||||
class Refused(override val name: KurdName) : KurdResolution
|
||||
|
||||
/** Neither the light client nor two agreeing RPC nodes could answer. */
|
||||
class Unavailable(override val name: KurdName) : KurdResolution
|
||||
}
|
||||
|
||||
interface KurdNameResolver {
|
||||
|
||||
suspend fun resolve(name: KurdName): KurdResolution
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
package io.novafoundation.nova.runtime.names
|
||||
|
||||
import io.novasama.substrate_sdk_android.extensions.toHexString
|
||||
import io.novasama.substrate_sdk_android.hash.Hasher.xxHash128
|
||||
import java.math.BigInteger
|
||||
|
||||
/** Raw storage of the chain that keeps `.kurd` names, all read at one block. */
|
||||
interface NameStorageReader {
|
||||
|
||||
suspend fun finalizedHead(): String
|
||||
|
||||
/** The raw value at [key] in block [at], or null when nothing is stored there. */
|
||||
suspend fun storage(key: ByteArray, at: String): ByteArray?
|
||||
|
||||
/** The number of block [at], shown to the reader as where a name was read. */
|
||||
suspend fun blockNumber(at: String): Long?
|
||||
}
|
||||
|
||||
enum class NameStatus {
|
||||
ACTIVE,
|
||||
|
||||
/** The lease has ended; the site still resolves and only the last owner may renew. */
|
||||
GRACE,
|
||||
}
|
||||
|
||||
/** What one source says about a name. */
|
||||
sealed interface NameLookup {
|
||||
|
||||
/** Two sources agree when they return the same records byte for byte and judge them the same. */
|
||||
fun agreesWith(other: NameLookup): Boolean = when (this) {
|
||||
is Found -> other is Found && rawChain == other.rawChain && status == other.status && locked == other.locked
|
||||
else -> this === other
|
||||
}
|
||||
|
||||
object NotFound : NameLookup
|
||||
|
||||
/** Registered once but no longer valid: expired, or an ancestor was registered anew. */
|
||||
object Lapsed : NameLookup
|
||||
|
||||
data class Found(
|
||||
val record: NameRecord,
|
||||
val status: NameStatus,
|
||||
/** Every record from this name up to its top-level name, raw, for comparing sources. */
|
||||
val rawChain: List<String>,
|
||||
/** Some label of the name is in `Navnisan::Locked`: reserved, never for sale. */
|
||||
val locked: Boolean,
|
||||
/** The finalized block it was read at, when the source said; not part of agreement. */
|
||||
val block: Long? = null,
|
||||
) : NameLookup
|
||||
|
||||
companion object {
|
||||
|
||||
private val NAMES_PREFIX = "Navnisan".toByteArray().xxHash128() + "Names".toByteArray().xxHash128()
|
||||
private val LOCKED_PREFIX = "Navnisan".toByteArray().xxHash128() + "Locked".toByteArray().xxHash128()
|
||||
private val TIMESTAMP_NOW = "Timestamp".toByteArray().xxHash128() + "Now".toByteArray().xxHash128()
|
||||
|
||||
/** `NavnisanGracePeriod`: 30 days after a lease ends the name still resolves (decision D11). */
|
||||
const val GRACE_PERIOD_MILLIS = 30L * 24 * 60 * 60 * 1000
|
||||
|
||||
fun namesKey(node: ByteArray) = NAMES_PREFIX + node
|
||||
|
||||
fun lockedKey(node: ByteArray) = LOCKED_PREFIX + node
|
||||
|
||||
val timestampNowKey: ByteArray get() = TIMESTAMP_NOW.copyOf()
|
||||
|
||||
/**
|
||||
* Reads [name] the way `Pallet::effective_status` judges it, all at one finalized block: the
|
||||
* record, then each ancestor. Each record must sit under the parent its name implies and
|
||||
* carry the label the name has, so a source cannot answer for one name with another's record.
|
||||
*/
|
||||
suspend fun read(reader: NameStorageReader, name: KurdName): NameLookup {
|
||||
val at = reader.finalizedHead()
|
||||
val now = reader.storage(TIMESTAMP_NOW, at)?.let(::decodeMoment) ?: error("Chain time is missing")
|
||||
|
||||
val nodes = name.nodesUpward()
|
||||
val parents = nodes.drop(1) + listOf(KurdName.KURD_NODE)
|
||||
val records = mutableListOf<Pair<NameRecord, String>>()
|
||||
|
||||
for (level in nodes.indices) {
|
||||
val raw = reader.storage(namesKey(nodes[level]), at) ?: return if (level == 0) NotFound else Lapsed
|
||||
val record = NameRecord.decode(raw)
|
||||
check(record.label == name.labels[level] && record.parent.contentEquals(parents[level])) {
|
||||
"Record of ${name.labels[level]} does not belong to $name"
|
||||
}
|
||||
records += record to raw.toHexString()
|
||||
}
|
||||
|
||||
val status = effectiveStatus(records.map { it.first }, now) ?: return Lapsed
|
||||
val locked = nodes.any { reader.storage(lockedKey(it), at) != null }
|
||||
|
||||
val block = runCatching { reader.blockNumber(at) }.getOrNull()
|
||||
|
||||
return Found(records.first().first, status, records.map { it.second }, locked, block)
|
||||
}
|
||||
|
||||
/**
|
||||
* [chain] is a name's record followed by every ancestor's. A subname is valid only while
|
||||
* each ancestor is, under the registration it was set against; a grace period anywhere
|
||||
* above makes the whole name graced.
|
||||
*/
|
||||
fun effectiveStatus(chain: List<NameRecord>, now: Long): NameStatus? {
|
||||
var status = ownStatus(chain.first(), now) ?: return null
|
||||
for (index in 0 until chain.lastIndex) {
|
||||
val record = chain[index]
|
||||
if (record.kind != NameRecord.Kind.SUB) return status
|
||||
|
||||
val parent = chain[index + 1]
|
||||
if (parent.reg != record.parentReg) return null
|
||||
if ((ownStatus(parent, now) ?: return null) == NameStatus.GRACE) status = NameStatus.GRACE
|
||||
}
|
||||
|
||||
return status.takeIf { chain.last().kind != NameRecord.Kind.SUB }
|
||||
}
|
||||
|
||||
fun ownStatus(record: NameRecord, now: Long): NameStatus? {
|
||||
val end = record.expiry ?: return NameStatus.ACTIVE
|
||||
return when {
|
||||
now < end -> NameStatus.ACTIVE
|
||||
record.kind == NameRecord.Kind.LEASED && now < end + GRACE_PERIOD_MILLIS -> NameStatus.GRACE
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
private fun decodeMoment(raw: ByteArray): Long {
|
||||
require(raw.size == 8) { "Chain time is not a u64" }
|
||||
return BigInteger(1, raw.reversedArray()).toLong()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
package io.novafoundation.nova.runtime.names
|
||||
|
||||
import java.math.BigInteger
|
||||
|
||||
/**
|
||||
* `pezpallet_navnisan::NameRecord`, decoded from the raw value of `Navnisan::Names[node]`.
|
||||
*
|
||||
* Decoded by hand rather than through runtime metadata: the resolver reads it through the light
|
||||
* client, before and independently of the chain's runtime being loaded.
|
||||
*/
|
||||
class NameRecord(
|
||||
val label: String,
|
||||
val parent: ByteArray,
|
||||
val parentReg: Long,
|
||||
val reg: Long,
|
||||
val owner: Owner,
|
||||
val kind: Kind,
|
||||
val lockedTransfer: Boolean,
|
||||
/** Milliseconds, chain time. Null for names that never expire. */
|
||||
val expiry: Long?,
|
||||
val account: ByteArray?,
|
||||
val content: Content?,
|
||||
) {
|
||||
|
||||
sealed interface Owner {
|
||||
class Account(val accountId: ByteArray) : Owner
|
||||
class Office(val tiki: Int) : Owner
|
||||
object Governance : Owner
|
||||
}
|
||||
|
||||
enum class Kind { LEASED, GRANTED, SUB }
|
||||
|
||||
sealed interface Content {
|
||||
/** An EIP-1577 contenthash: `0xe3` then a CIDv1. */
|
||||
class Ipfs(val contenthash: ByteArray) : Content
|
||||
|
||||
class Feed(val publisher: ByteArray, val anchorer: ByteArray, val anchor: ByteArray, val seq: BigInteger) : Content
|
||||
}
|
||||
|
||||
/** `NameRecord::is_official`: granted, or descended from a granted name. */
|
||||
val isOfficial: Boolean get() = lockedTransfer || kind == Kind.GRANTED
|
||||
|
||||
/** `NameRecord::is_institutional`: changes to it wait out the institution delay. */
|
||||
val isInstitutional: Boolean get() = lockedTransfer || owner !is Owner.Account
|
||||
|
||||
companion object {
|
||||
|
||||
private const val ACCOUNT_ID = 32
|
||||
|
||||
fun decode(bytes: ByteArray): NameRecord {
|
||||
val reader = ScaleReader(bytes)
|
||||
val record = with(reader) {
|
||||
NameRecord(
|
||||
label = String(byteVec(), Charsets.US_ASCII),
|
||||
parent = bytes(32),
|
||||
parentReg = u64().toLong(),
|
||||
reg = u64().toLong(),
|
||||
owner = when (val index = u8()) {
|
||||
0 -> Owner.Account(bytes(ACCOUNT_ID))
|
||||
1 -> Owner.Office(u8())
|
||||
2 -> Owner.Governance
|
||||
else -> error("Unknown owner variant $index")
|
||||
},
|
||||
kind = when (val index = u8()) {
|
||||
0 -> Kind.LEASED
|
||||
1 -> Kind.GRANTED
|
||||
2 -> Kind.SUB
|
||||
else -> error("Unknown kind variant $index")
|
||||
},
|
||||
lockedTransfer = bool(),
|
||||
expiry = option { u64().toLong() },
|
||||
account = option { bytes(ACCOUNT_ID) },
|
||||
content = option {
|
||||
when (val index = u8()) {
|
||||
0 -> Content.Ipfs(byteVec())
|
||||
1 -> Content.Feed(publisher = bytes(32), anchorer = bytes(ACCOUNT_ID), anchor = byteVec(), seq = u64())
|
||||
else -> error("Unknown content variant $index")
|
||||
}
|
||||
},
|
||||
).also {
|
||||
// deposit: Option<(AccountId, Balance)>, read only so a trailing byte is noticed
|
||||
option { bytes(ACCOUNT_ID + 16) }
|
||||
}
|
||||
}
|
||||
require(reader.isExhausted) { "Trailing bytes after a name record" }
|
||||
|
||||
return record
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** The few SCALE shapes a name record uses. */
|
||||
internal class ScaleReader(private val data: ByteArray) {
|
||||
|
||||
private var position = 0
|
||||
|
||||
val isExhausted: Boolean get() = position == data.size
|
||||
|
||||
fun u8(): Int {
|
||||
require(position < data.size) { "Unexpected end of SCALE input" }
|
||||
return data[position++].toInt() and 0xff
|
||||
}
|
||||
|
||||
fun bytes(count: Int): ByteArray {
|
||||
require(count >= 0 && position + count <= data.size) { "Unexpected end of SCALE input" }
|
||||
return data.copyOfRange(position, position + count).also { position += count }
|
||||
}
|
||||
|
||||
fun bool(): Boolean = when (val byte = u8()) {
|
||||
0 -> false
|
||||
1 -> true
|
||||
else -> error("Invalid bool byte $byte")
|
||||
}
|
||||
|
||||
fun u64(): BigInteger = BigInteger(1, bytes(8).reversedArray())
|
||||
|
||||
fun compact(): Int {
|
||||
val first = u8()
|
||||
return when (first and 0b11) {
|
||||
0 -> first ushr 2
|
||||
1 -> (first or (u8() shl 8)) ushr 2
|
||||
2 -> ((first or (u8() shl 8) or (u8() shl 16) or (u8() shl 24)) ushr 2).also { require(it >= 0) }
|
||||
else -> error("Big compact lengths are not used by name records")
|
||||
}
|
||||
}
|
||||
|
||||
fun byteVec(): ByteArray = bytes(compact())
|
||||
|
||||
fun <T> option(read: ScaleReader.() -> T): T? = when (val tag = u8()) {
|
||||
0 -> null
|
||||
1 -> read()
|
||||
else -> error("Invalid option tag $tag")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
package io.novafoundation.nova.runtime.names.content
|
||||
|
||||
import java.security.MessageDigest
|
||||
|
||||
/**
|
||||
* A content identifier, limited to what `.kurd` sites are built from: CIDv1 or CIDv0, codec dag-pb
|
||||
* or raw, hash sha2-256 or identity (inlined tiny blocks). Anything else is refused when parsed,
|
||||
* so a block can only ever be accepted by checking its bytes against the digest.
|
||||
*/
|
||||
class Cid private constructor(
|
||||
/** The CID's binary form, as it appears in links and in a CAR. */
|
||||
val bytes: ByteArray,
|
||||
val codec: Codec,
|
||||
private val hash: Hash,
|
||||
private val digest: ByteArray,
|
||||
) {
|
||||
|
||||
enum class Codec(val code: Int) { DAG_PB(0x70), RAW(0x55) }
|
||||
|
||||
private enum class Hash(val code: Int) { IDENTITY(0x00), SHA2_256(0x12) }
|
||||
|
||||
/** True when [block] is the content this CID names. */
|
||||
fun matches(block: ByteArray): Boolean = when (hash) {
|
||||
Hash.IDENTITY -> MessageDigest.isEqual(digest, block)
|
||||
Hash.SHA2_256 -> MessageDigest.isEqual(digest, MessageDigest.getInstance("SHA-256").digest(block))
|
||||
}
|
||||
|
||||
/** An identity CID carries its block; nothing needs to be fetched for it. */
|
||||
val inlineBlock: ByteArray? get() = digest.copyOf().takeIf { hash == Hash.IDENTITY }
|
||||
|
||||
/** Multibase base32 (`b…`), the form gateways take in a path. */
|
||||
override fun toString(): String = "b" + base32(bytes)
|
||||
|
||||
override fun equals(other: Any?) = other is Cid && other.bytes.contentEquals(bytes)
|
||||
|
||||
override fun hashCode() = bytes.contentHashCode()
|
||||
|
||||
companion object {
|
||||
|
||||
private const val SHA2_256_LENGTH = 32
|
||||
|
||||
/** An inlined block is meant for a few bytes; a larger one is not something a publisher makes. */
|
||||
private const val MAX_IDENTITY_LENGTH = 128
|
||||
|
||||
/**
|
||||
* The CID in an EIP-1577 contenthash as `Navnisan` stores it, or null when it is not an
|
||||
* `ipfs-ns` (`0xe3 0x01`) hash of a supported CID. `ipns-ns` is not content but a pointer
|
||||
* that needs its own signed record, so it is not accepted here.
|
||||
*/
|
||||
fun fromContenthash(contenthash: ByteArray): Cid? {
|
||||
if (contenthash.size < 2 || contenthash[0] != 0xe3.toByte() || contenthash[1] != 0x01.toByte()) return null
|
||||
val reader = ByteReader(contenthash, 2)
|
||||
return runCatching { read(reader) }.getOrNull()?.takeIf { reader.isExhausted && it.hash == Hash.SHA2_256 }
|
||||
}
|
||||
|
||||
/** The CID that fills [bytes] exactly. */
|
||||
fun parse(bytes: ByteArray): Cid {
|
||||
val reader = ByteReader(bytes, 0)
|
||||
return read(reader).also { require(reader.isExhausted) { "Trailing bytes after a CID" } }
|
||||
}
|
||||
|
||||
/** Reads one CID at the reader's position: CIDv0 is a bare sha2-256 multihash (`0x12 0x20`). */
|
||||
internal fun read(reader: ByteReader): Cid {
|
||||
val start = reader.position
|
||||
if (reader.peek() == Hash.SHA2_256.code) {
|
||||
val digest = readMultihashDigest(reader, Hash.SHA2_256)
|
||||
return Cid(reader.slice(start), Codec.DAG_PB, Hash.SHA2_256, digest)
|
||||
}
|
||||
|
||||
require(reader.varint() == 1L) { "Unsupported CID version" }
|
||||
val codecCode = reader.varint()
|
||||
val codec = Codec.values().firstOrNull { it.code.toLong() == codecCode } ?: throw IllegalArgumentException("Unsupported codec $codecCode")
|
||||
val hashCode = reader.varint()
|
||||
val hash = Hash.values().firstOrNull { it.code.toLong() == hashCode } ?: throw IllegalArgumentException("Unsupported hash $hashCode")
|
||||
val digest = readDigest(reader, hash)
|
||||
|
||||
return Cid(reader.slice(start), codec, hash, digest)
|
||||
}
|
||||
|
||||
private fun readMultihashDigest(reader: ByteReader, expected: Hash): ByteArray {
|
||||
require(reader.varint() == expected.code.toLong()) { "Unexpected hash" }
|
||||
return readDigest(reader, expected)
|
||||
}
|
||||
|
||||
private fun readDigest(reader: ByteReader, hash: Hash): ByteArray {
|
||||
val length = reader.varint()
|
||||
when (hash) {
|
||||
Hash.SHA2_256 -> require(length == SHA2_256_LENGTH.toLong()) { "Bad sha2-256 length" }
|
||||
Hash.IDENTITY -> require(length in 0..MAX_IDENTITY_LENGTH) { "Identity block too long" }
|
||||
}
|
||||
return reader.bytes(length.toInt())
|
||||
}
|
||||
|
||||
private const val BASE32 = "abcdefghijklmnopqrstuvwxyz234567"
|
||||
|
||||
private fun base32(data: ByteArray): String {
|
||||
val out = StringBuilder()
|
||||
var buffer = 0
|
||||
var bits = 0
|
||||
for (byte in data) {
|
||||
buffer = (buffer shl 8) or (byte.toInt() and 0xff)
|
||||
bits += 8
|
||||
while (bits >= 5) {
|
||||
out.append(BASE32[(buffer shr (bits - 5)) and 31])
|
||||
bits -= 5
|
||||
}
|
||||
}
|
||||
if (bits > 0) out.append(BASE32[(buffer shl (5 - bits)) and 31])
|
||||
return out.toString()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Bounded reads over bytes that came from the network: every read checks it stays inside. */
|
||||
internal class ByteReader(private val data: ByteArray, start: Int = 0, private val end: Int = data.size) {
|
||||
|
||||
var position = start
|
||||
private set
|
||||
|
||||
val isExhausted: Boolean get() = position == end
|
||||
|
||||
val remaining: Int get() = end - position
|
||||
|
||||
fun peek(): Int {
|
||||
require(position < end) { "Unexpected end of input" }
|
||||
return data[position].toInt() and 0xff
|
||||
}
|
||||
|
||||
fun byte(): Int = peek().also { position++ }
|
||||
|
||||
fun bytes(count: Int): ByteArray {
|
||||
require(count in 0..remaining) { "Unexpected end of input" }
|
||||
return data.copyOfRange(position, position + count).also { position += count }
|
||||
}
|
||||
|
||||
/** Unsigned LEB128, at most 63 bits and minimally encoded, as multiformats require. */
|
||||
fun varint(): Long {
|
||||
var value = 0L
|
||||
var shift = 0
|
||||
while (true) {
|
||||
val byte = byte()
|
||||
require(shift < 63) { "Varint too long" }
|
||||
value = value or ((byte and 0x7f).toLong() shl shift)
|
||||
if (byte and 0x80 == 0) {
|
||||
require(byte != 0 || shift == 0) { "Varint not minimally encoded" }
|
||||
return value
|
||||
}
|
||||
shift += 7
|
||||
}
|
||||
}
|
||||
|
||||
/** A reader over the next [count] bytes; this reader moves past them. */
|
||||
fun sub(count: Int): ByteReader {
|
||||
require(count in 0..remaining) { "Unexpected end of input" }
|
||||
return ByteReader(data, position, position + count).also { position += count }
|
||||
}
|
||||
|
||||
fun slice(from: Int): ByteArray = data.copyOfRange(from, position)
|
||||
}
|
||||
@@ -0,0 +1,213 @@
|
||||
package io.novafoundation.nova.runtime.names.content
|
||||
|
||||
import org.bouncycastle.crypto.params.Ed25519PublicKeyParameters
|
||||
import org.bouncycastle.crypto.signers.Ed25519Signer
|
||||
import java.math.BigInteger
|
||||
import java.time.Instant
|
||||
|
||||
/** A feed release: the content root its publisher signed, and the release's number. */
|
||||
class IpnsRecord(val root: Cid, val sequence: Long)
|
||||
|
||||
/**
|
||||
* Verifies IPNS records (ipfs/specs `ipns-record`, V2) against a feed's publisher key as the chain
|
||||
* gives it. The key in the record or in its name is never used: a mirror cannot answer for a feed
|
||||
* with a record signed by someone else.
|
||||
*/
|
||||
object IpnsRecords {
|
||||
|
||||
/** The spec's ceiling: a larger record is refused unread. */
|
||||
const val MAX_RECORD = 10 * 1024
|
||||
|
||||
private val SIGNATURE_PREFIX = "ipns-signature:".toByteArray()
|
||||
|
||||
/**
|
||||
* The release in [record], or null when it is not one [publisherKey] (ed25519) signed, has
|
||||
* expired by [now], or points anywhere but at an immutable `/ipfs/` root.
|
||||
*/
|
||||
fun verify(record: ByteArray, publisherKey: ByteArray, now: Instant): IpnsRecord? = runCatching {
|
||||
require(record.size <= MAX_RECORD && publisherKey.size == 32)
|
||||
val entry = IpnsEntry.decode(record)
|
||||
val data = entry.data ?: return null
|
||||
val signature = entry.signatureV2 ?: return null
|
||||
|
||||
val signer = Ed25519Signer().apply {
|
||||
init(false, Ed25519PublicKeyParameters(publisherKey, 0))
|
||||
update(SIGNATURE_PREFIX, 0, SIGNATURE_PREFIX.size)
|
||||
update(data, 0, data.size)
|
||||
}
|
||||
if (!signer.verifySignature(signature)) return null
|
||||
|
||||
val signed = Cbor.decodeMap(data)
|
||||
val value = signed["Value"] as? ByteArray ?: return null
|
||||
val validity = signed["Validity"] as? ByteArray ?: return null
|
||||
val validityType = signed["ValidityType"] as? BigInteger ?: return null
|
||||
val sequence = signed["Sequence"] as? BigInteger ?: return null
|
||||
|
||||
// Legacy copies outside the signed data must say the same, or they would be read instead of it
|
||||
entry.value?.let { if (!it.contentEquals(value)) return null }
|
||||
entry.validity?.let { if (!it.contentEquals(validity)) return null }
|
||||
entry.validityType?.let { if (it != validityType.toLong()) return null }
|
||||
entry.sequence?.let { if (it != sequence.toLong()) return null }
|
||||
|
||||
// EOL is the only validity type there is
|
||||
if (validityType != BigInteger.ZERO) return null
|
||||
if (!Instant.parse(String(validity, Charsets.US_ASCII)).isAfter(now)) return null
|
||||
|
||||
val path = String(value, Charsets.UTF_8)
|
||||
if (!path.startsWith("/ipfs/")) return null
|
||||
val root = CidText.parse(path.removePrefix("/ipfs/").substringBefore('/')) ?: return null
|
||||
|
||||
IpnsRecord(root, sequence.longValueExact())
|
||||
}.getOrNull()
|
||||
|
||||
/** The IPNS name of an ed25519 key: `k`-base36 CIDv1, codec libp2p-key, identity multihash of its protobuf form. */
|
||||
fun nameOf(publisherKey: ByteArray): String {
|
||||
val protobufKey = byteArrayOf(0x08, 0x01, 0x12, 0x20) + publisherKey
|
||||
val cid = byteArrayOf(0x01, 0x72, 0x00, protobufKey.size.toByte()) + protobufKey
|
||||
return "k" + base36(cid)
|
||||
}
|
||||
|
||||
private fun base36(data: ByteArray): String {
|
||||
val zeros = data.takeWhile { it == 0.toByte() }.size
|
||||
val digits = BigInteger(1, data).toString(36).takeUnless { it == "0" }.orEmpty()
|
||||
return "0".repeat(zeros) + digits
|
||||
}
|
||||
|
||||
private class IpnsEntry(
|
||||
val value: ByteArray?,
|
||||
val validityType: Long?,
|
||||
val validity: ByteArray?,
|
||||
val sequence: Long?,
|
||||
val signatureV2: ByteArray?,
|
||||
val data: ByteArray?,
|
||||
) {
|
||||
companion object {
|
||||
fun decode(bytes: ByteArray): IpnsEntry {
|
||||
var value: ByteArray? = null
|
||||
var validityType: Long? = null
|
||||
var validity: ByteArray? = null
|
||||
var sequence: Long? = null
|
||||
var signatureV2: ByteArray? = null
|
||||
var data: ByteArray? = null
|
||||
|
||||
val reader = ByteReader(bytes)
|
||||
while (!reader.isExhausted) {
|
||||
val key = reader.varint()
|
||||
when (key and 7) {
|
||||
0L -> {
|
||||
val number = reader.varint()
|
||||
when (key ushr 3) {
|
||||
3L -> validityType = number
|
||||
5L -> sequence = number
|
||||
}
|
||||
}
|
||||
2L -> {
|
||||
val length = reader.varint()
|
||||
require(length in 0..reader.remaining.toLong())
|
||||
val field = reader.bytes(length.toInt())
|
||||
when (key ushr 3) {
|
||||
1L -> value = field
|
||||
4L -> validity = field
|
||||
8L -> signatureV2 = field
|
||||
9L -> data = field
|
||||
}
|
||||
}
|
||||
else -> throw IllegalArgumentException("Unsupported wire type")
|
||||
}
|
||||
}
|
||||
return IpnsEntry(value, validityType, validity, sequence, signatureV2, data)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** The DAG-CBOR an IPNS record signs: one map of text keys to byte strings, text and unsigned integers. */
|
||||
internal object Cbor {
|
||||
|
||||
fun decodeMap(bytes: ByteArray): Map<String, Any> {
|
||||
val reader = ByteReader(bytes)
|
||||
val (major, count) = head(reader)
|
||||
require(major == 5) { "Not a map" }
|
||||
val map = mutableMapOf<String, Any>()
|
||||
repeat(count.toInt()) {
|
||||
val key = item(reader) as? String ?: throw IllegalArgumentException("Map key is not text")
|
||||
map[key] = item(reader)
|
||||
}
|
||||
require(reader.isExhausted) { "Trailing bytes after the map" }
|
||||
return map
|
||||
}
|
||||
|
||||
private fun item(reader: ByteReader): Any {
|
||||
val (major, argument) = head(reader)
|
||||
return when (major) {
|
||||
0 -> argument
|
||||
2 -> reader.bytes(length(reader, argument))
|
||||
3 -> String(reader.bytes(length(reader, argument)), Charsets.UTF_8)
|
||||
else -> throw IllegalArgumentException("Unsupported CBOR major type $major")
|
||||
}
|
||||
}
|
||||
|
||||
private fun length(reader: ByteReader, argument: BigInteger): Int {
|
||||
require(argument <= BigInteger.valueOf(reader.remaining.toLong())) { "Length past the end" }
|
||||
return argument.toInt()
|
||||
}
|
||||
|
||||
private fun head(reader: ByteReader): Pair<Int, BigInteger> {
|
||||
val initial = reader.byte()
|
||||
val major = initial ushr 5
|
||||
val info = initial and 0x1f
|
||||
val argument = when {
|
||||
info < 24 -> BigInteger.valueOf(info.toLong())
|
||||
info == 24 -> BigInteger.valueOf(reader.byte().toLong())
|
||||
info == 25 -> BigInteger(1, reader.bytes(2))
|
||||
info == 26 -> BigInteger(1, reader.bytes(4))
|
||||
info == 27 -> BigInteger(1, reader.bytes(8))
|
||||
else -> throw IllegalArgumentException("Indefinite or reserved CBOR length")
|
||||
}
|
||||
return major to argument
|
||||
}
|
||||
}
|
||||
|
||||
/** A CID as text in a path: multibase base32 (`b…`) or a CIDv0 in base58btc (`Qm…`). */
|
||||
internal object CidText {
|
||||
|
||||
private const val BASE32 = "abcdefghijklmnopqrstuvwxyz234567"
|
||||
private const val BASE58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
|
||||
|
||||
fun parse(text: String): Cid? = runCatching {
|
||||
when {
|
||||
text.startsWith("Qm") && text.length == 46 -> Cid.parse(base58(text))
|
||||
text.startsWith("b") -> Cid.parse(base32(text.substring(1)))
|
||||
else -> null
|
||||
}
|
||||
}.getOrNull()
|
||||
|
||||
private fun base32(text: String): ByteArray {
|
||||
val out = java.io.ByteArrayOutputStream()
|
||||
var buffer = 0
|
||||
var bits = 0
|
||||
for (char in text) {
|
||||
val value = BASE32.indexOf(char)
|
||||
require(value >= 0) { "Not base32" }
|
||||
buffer = (buffer shl 5) or value
|
||||
bits += 5
|
||||
if (bits >= 8) {
|
||||
out.write((buffer shr (bits - 8)) and 0xff)
|
||||
bits -= 8
|
||||
}
|
||||
}
|
||||
return out.toByteArray()
|
||||
}
|
||||
|
||||
private fun base58(text: String): ByteArray {
|
||||
var number = BigInteger.ZERO
|
||||
for (char in text) {
|
||||
val value = BASE58.indexOf(char)
|
||||
require(value >= 0) { "Not base58" }
|
||||
number = number.multiply(BigInteger.valueOf(58)).add(BigInteger.valueOf(value.toLong()))
|
||||
}
|
||||
val bytes = number.toByteArray().dropWhile { it == 0.toByte() }.toByteArray()
|
||||
val zeros = text.takeWhile { it == '1' }.length
|
||||
return ByteArray(zeros) + bytes
|
||||
}
|
||||
}
|
||||
+151
@@ -0,0 +1,151 @@
|
||||
package io.novafoundation.nova.runtime.names.content
|
||||
|
||||
import android.util.Log
|
||||
import retrofit2.http.GET
|
||||
import retrofit2.http.Url
|
||||
|
||||
/** Where `.kurd` content is fetched from: IPFS trustless gateways that answer `?format=car`. */
|
||||
interface KurdGateways {
|
||||
|
||||
/** Base URLs, `https://host`, in the order they are tried. */
|
||||
suspend fun list(): List<String>
|
||||
}
|
||||
|
||||
interface KurdGatewayListApi {
|
||||
|
||||
/** A file of the config repository, checked against its signed targets by the app's HTTP client. */
|
||||
@GET
|
||||
suspend fun gateways(@Url url: String): GatewayListRemote
|
||||
}
|
||||
|
||||
class GatewayListRemote(val gateways: List<GatewayRemote>?) {
|
||||
|
||||
class GatewayRemote(val url: String?)
|
||||
}
|
||||
|
||||
/**
|
||||
* The signed list from the config repository when it can be read, the built-in one otherwise. The
|
||||
* list can only add or reorder gateways: whatever a gateway sends is verified block by block, so a
|
||||
* bad entry costs time, never content.
|
||||
*/
|
||||
class RealKurdGateways(
|
||||
private val api: KurdGatewayListApi,
|
||||
private val refreshMillis: Long = 30 * 60_000L,
|
||||
private val clock: () -> Long = System::currentTimeMillis,
|
||||
) : KurdGateways {
|
||||
|
||||
@Volatile
|
||||
private var remote: Pair<Long, List<String>>? = null
|
||||
|
||||
override suspend fun list(): List<String> {
|
||||
val cached = remote
|
||||
if (cached != null && clock() - cached.first < refreshMillis) return cached.second
|
||||
|
||||
val fetched = runCatching { api.gateways(LIST_URL).gateways.orEmpty().mapNotNull { it.url?.let(::normalized) } }
|
||||
.onFailure { Log.w(TAG, "Signed gateway list unavailable, using the built-in one", it) }
|
||||
.getOrNull()
|
||||
?.takeIf { it.isNotEmpty() }
|
||||
|
||||
if (fetched != null) remote = clock() to fetched
|
||||
return fetched ?: cached?.second ?: BUILT_IN
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
const val LIST_URL = "https://raw.githubusercontent.com/pezkuwichain/pezkuwi-wallet-utils/master/kurd/gateways.json"
|
||||
|
||||
/** Measured reachable from Türkiye (architecture, M0); our own mirrors join through the signed list. */
|
||||
val BUILT_IN = listOf("https://ipfs.io", "https://dweb.link")
|
||||
|
||||
private const val TAG = "KurdGateways"
|
||||
|
||||
/** `https://host[:port]` only: a path or query in an entry would change what is asked for. */
|
||||
internal fun normalized(url: String): String? {
|
||||
val match = Regex("^https://([a-z0-9.-]+)(:[0-9]{1,5})?/?$").matchEntire(url.trim().lowercase()) ?: return null
|
||||
return "https://" + match.groupValues[1] + match.groupValues[2]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** One gateway's answer for a CAR request: the bytes, or null when it has none (404 and the like). */
|
||||
fun interface CarSource {
|
||||
|
||||
suspend fun car(gateway: String, root: Cid, path: List<String>): ByteArray?
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches a path under a content root from the gateways, in order, and keeps the first answer that
|
||||
* verifies. A gateway that fails, times out, withholds a block or sends a changed one is skipped.
|
||||
*/
|
||||
class KurdContentFetcher(
|
||||
private val gateways: KurdGateways,
|
||||
private val source: CarSource,
|
||||
private val maxFileSize: Int = UnixFsReader.DEFAULT_MAX_FILE,
|
||||
) {
|
||||
|
||||
sealed interface Result {
|
||||
|
||||
class File(val bytes: ByteArray, val path: List<String>) : Result
|
||||
|
||||
/** Proven absent: the parent directory was verified and does not have the name. */
|
||||
object NotFound : Result
|
||||
|
||||
/** No gateway gave a verifiable answer. */
|
||||
object Unavailable : Result
|
||||
}
|
||||
|
||||
/** The file at [path] under [root]; a directory gives its `index.html`. */
|
||||
suspend fun fetch(root: Cid, path: List<String>): Result {
|
||||
return when (val entry = entry(root, path)) {
|
||||
is Lookup.Found -> when (val found = entry.entry) {
|
||||
is UnixFsReader.Entry.File -> Result.File(found.bytes, path)
|
||||
is UnixFsReader.Entry.Directory -> when (val index = entry(root, path + INDEX)) {
|
||||
is Lookup.Found -> (index.entry as? UnixFsReader.Entry.File)?.let { Result.File(it.bytes, path + INDEX) } ?: Result.NotFound
|
||||
Lookup.Absent -> Result.NotFound
|
||||
Lookup.Unavailable -> Result.Unavailable
|
||||
}
|
||||
}
|
||||
Lookup.Absent -> Result.NotFound
|
||||
Lookup.Unavailable -> Result.Unavailable
|
||||
}
|
||||
}
|
||||
|
||||
private sealed interface Lookup {
|
||||
class Found(val entry: UnixFsReader.Entry) : Lookup
|
||||
object Absent : Lookup
|
||||
object Unavailable : Lookup
|
||||
}
|
||||
|
||||
private suspend fun entry(root: Cid, path: List<String>): Lookup {
|
||||
for (gateway in gateways.list()) {
|
||||
val entry = runCatching { read(gateway, root, path) }
|
||||
.onFailure { Log.w(TAG, "Gateway $gateway gave no verifiable answer for $root", it) }
|
||||
.getOrNull()
|
||||
if (entry != null) return entry
|
||||
}
|
||||
return Lookup.Unavailable
|
||||
}
|
||||
|
||||
/** Null when this gateway could not answer; [Lookup.Absent] only once absence is verified. */
|
||||
private suspend fun read(gateway: String, root: Cid, path: List<String>): Lookup? {
|
||||
val car = source.car(gateway, root, path)
|
||||
if (car != null) {
|
||||
val entry = UnixFsReader(VerifiedBlocks.fromCar(car), maxFileSize).read(root, path) ?: return null
|
||||
return Lookup.Found(entry)
|
||||
}
|
||||
|
||||
// A gateway that says "not found" may be withholding: check the parent directory says so
|
||||
if (path.isEmpty()) return null
|
||||
val parentCar = source.car(gateway, root, path.dropLast(1)) ?: return null
|
||||
val reader = UnixFsReader(VerifiedBlocks.fromCar(parentCar), maxFileSize)
|
||||
if (reader.read(root, path.dropLast(1)) !is UnixFsReader.Entry.Directory) return null
|
||||
return if (reader.read(root, path) == null) Lookup.Absent else null
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
const val INDEX = "index.html"
|
||||
|
||||
private const val TAG = "KurdContentFetcher"
|
||||
}
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package io.novafoundation.nova.runtime.names.content
|
||||
|
||||
import io.novafoundation.nova.runtime.names.NameRecord
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.awaitAll
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.withContext
|
||||
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import java.time.Instant
|
||||
|
||||
/** One gateway's IPNS record for a name, or null when it has none. */
|
||||
fun interface IpnsRecordSource {
|
||||
|
||||
suspend fun record(gateway: String, name: String): ByteArray?
|
||||
}
|
||||
|
||||
class OkHttpIpnsRecordSource(private val client: OkHttpClient) : IpnsRecordSource {
|
||||
|
||||
override suspend fun record(gateway: String, name: String): ByteArray? = withContext(Dispatchers.IO) {
|
||||
val url = gateway.toHttpUrlOrNull()?.newBuilder()
|
||||
?.addPathSegment("ipns")
|
||||
?.addPathSegment(name)
|
||||
?.addQueryParameter("format", "ipns-record")
|
||||
?.build()
|
||||
?: return@withContext null
|
||||
val request = Request.Builder().url(url).header("Accept", "application/vnd.ipfs.ipns-record").build()
|
||||
|
||||
client.newCall(request).execute().use { response ->
|
||||
if (!response.isSuccessful) return@withContext null
|
||||
val source = response.body?.source() ?: return@withContext null
|
||||
if (source.request(IpnsRecords.MAX_RECORD + 1L)) return@withContext null
|
||||
source.buffer.readByteArray()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The release a feed name serves: the newest one its publisher signed that any gateway has, never
|
||||
* older than the release the chain was last told of (its anchor), and the anchor itself when no
|
||||
* gateway has a record to show.
|
||||
*/
|
||||
class KurdFeedResolver(
|
||||
private val gateways: KurdGateways,
|
||||
private val source: IpnsRecordSource,
|
||||
private val clock: () -> Instant = Instant::now,
|
||||
) {
|
||||
|
||||
/** [onChainVersion]: no gateway confirmed a release at or past the anchor, so newer ones may exist. */
|
||||
class Release(val root: Cid, val onChainVersion: Boolean)
|
||||
|
||||
suspend fun resolve(feed: NameRecord.Content.Feed): Release? {
|
||||
val anchor = Cid.fromContenthash(feed.anchor) ?: return null
|
||||
val floor = feed.seq
|
||||
val name = IpnsRecords.nameOf(feed.publisher)
|
||||
val now = clock()
|
||||
|
||||
val records = coroutineScope {
|
||||
gateways.list().map { gateway ->
|
||||
async { runCatching { source.record(gateway, name) }.getOrNull()?.let { IpnsRecords.verify(it, feed.publisher, now) } }
|
||||
}.awaitAll()
|
||||
}
|
||||
val newest = records.filterNotNull().filter { it.sequence.toBigInteger() >= floor }.maxByOrNull { it.sequence }
|
||||
|
||||
return when {
|
||||
newest == null -> Release(anchor, onChainVersion = true)
|
||||
// The release the chain already names: its root is the chain's word, not a gateway's
|
||||
newest.sequence.toBigInteger() == floor -> Release(anchor, onChainVersion = false)
|
||||
else -> Release(newest.root, onChainVersion = false)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
package io.novafoundation.nova.runtime.names.content
|
||||
|
||||
import io.novafoundation.nova.runtime.names.KurdName
|
||||
import io.novafoundation.nova.runtime.names.KurdNameResolver
|
||||
import io.novafoundation.nova.runtime.names.KurdResolution
|
||||
import io.novafoundation.nova.runtime.names.NameRecord
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
/**
|
||||
* Answers the DApp browser's requests to `https://<name>.kurd/...`: the name is resolved on chain,
|
||||
* its content root fetched from gateways and verified, and only then served. The page's origin is
|
||||
* the full name, so cookies, storage and permissions belong to that name alone.
|
||||
*/
|
||||
class KurdSiteServer(
|
||||
private val resolver: KurdNameResolver,
|
||||
private val fetcher: KurdContentFetcher,
|
||||
private val feeds: KurdFeedResolver? = null,
|
||||
private val resolutionTtlMillis: Long = 60_000L,
|
||||
private val clock: () -> Long = System::currentTimeMillis,
|
||||
) {
|
||||
|
||||
class Response(val status: Int, val reason: String, val mimeType: String, val body: ByteArray) {
|
||||
|
||||
/** The policy the wallet writes for every `.kurd` page: nothing loads or connects outside the name. */
|
||||
val headers: Map<String, String> = mapOf(
|
||||
"Content-Security-Policy" to CONTENT_SECURITY_POLICY,
|
||||
"X-Content-Type-Options" to "nosniff",
|
||||
"Referrer-Policy" to "no-referrer",
|
||||
"Cache-Control" to "no-store",
|
||||
)
|
||||
}
|
||||
|
||||
/** How the browser shows a `.kurd` page: set by its top-level document, read by the address bar and notices. */
|
||||
sealed interface PageState {
|
||||
|
||||
/**
|
||||
* Content verified and served. [resolution] says how the name itself was read; for a feed,
|
||||
* [onChainVersion] means no gateway confirmed a release past the one the chain names.
|
||||
*/
|
||||
class Open(val resolution: KurdResolution.Resolved, val root: Cid, val onChainVersion: Boolean = false) : PageState
|
||||
|
||||
/** Read through RPC nodes only; the content waits until the reader chooses to open it. */
|
||||
class NeedsConsent(val resolution: KurdResolution.Resolved) : PageState
|
||||
|
||||
/** Official or institutional and not verifiable now: never opened (decision D18). */
|
||||
object Refused : PageState
|
||||
|
||||
object NotRegistered : PageState
|
||||
|
||||
/** Registered, but no site: no content, or content of a kind this wallet does not serve. */
|
||||
object NoSite : PageState
|
||||
|
||||
object Unavailable : PageState
|
||||
}
|
||||
|
||||
private val resolutions = ConcurrentHashMap<KurdName, Pair<Long, KurdResolution>>()
|
||||
private val consented: MutableSet<KurdName> = ConcurrentHashMap.newKeySet()
|
||||
|
||||
private val _pageStates = MutableStateFlow<Map<KurdName, PageState>>(emptyMap())
|
||||
val pageStates: StateFlow<Map<KurdName, PageState>> = _pageStates
|
||||
|
||||
/**
|
||||
* The reader chose to open [name] although only RPC nodes vouched for it; holds until the app
|
||||
* restarts. Never offered for a name that must be verified, which is [PageState.Refused].
|
||||
*/
|
||||
fun allowUnverified(name: KurdName) {
|
||||
consented += name
|
||||
resolutions -= name
|
||||
}
|
||||
|
||||
/** Forgets [name]'s resolution, so a retry reads the chain again. */
|
||||
fun forget(name: KurdName) {
|
||||
resolutions -= name
|
||||
}
|
||||
|
||||
/** [mainFrame]: the page's own document, whose outcome becomes the name's [PageState]. */
|
||||
suspend fun serve(host: String, encodedPath: String, mainFrame: Boolean = false): Response {
|
||||
val name = KurdName.parse(host) ?: return status(404, "Not Found")
|
||||
val segments = decodePath(encodedPath) ?: return status(400, "Bad Request")
|
||||
|
||||
val (state, response) = handle(name, segments)
|
||||
if (mainFrame) _pageStates.update { it + (name to state) }
|
||||
return response
|
||||
}
|
||||
|
||||
private suspend fun handle(name: KurdName, segments: List<String>): Pair<PageState, Response> {
|
||||
val resolution = when (val resolved = resolve(name)) {
|
||||
is KurdResolution.Resolved -> resolved
|
||||
is KurdResolution.NotFound -> return PageState.NotRegistered to status(404, "Not Found")
|
||||
is KurdResolution.Refused -> return PageState.Refused to status(403, "Forbidden")
|
||||
is KurdResolution.Unavailable -> return PageState.Unavailable to status(502, "Bad Gateway")
|
||||
}
|
||||
|
||||
if (resolution.record.content == null) return PageState.NoSite to status(404, "Not Found")
|
||||
if (!resolution.verified && name !in consented) return PageState.NeedsConsent(resolution) to status(428, "Precondition Required")
|
||||
|
||||
val (root, onChainVersion) = when (val content = resolution.record.content) {
|
||||
is NameRecord.Content.Ipfs -> {
|
||||
val cid = Cid.fromContenthash(content.contenthash) ?: return PageState.NoSite to status(415, "Unsupported Media Type")
|
||||
cid to false
|
||||
}
|
||||
is NameRecord.Content.Feed -> {
|
||||
val release = feeds?.resolve(content) ?: return PageState.NoSite to status(415, "Unsupported Media Type")
|
||||
release.root to release.onChainVersion
|
||||
}
|
||||
null -> return PageState.NoSite to status(404, "Not Found")
|
||||
}
|
||||
|
||||
val open = PageState.Open(resolution, root, onChainVersion)
|
||||
return when (val result = fetcher.fetch(root, segments)) {
|
||||
is KurdContentFetcher.Result.File -> open to Response(200, "OK", mimeTypeOf(result.path.lastOrNull()), result.bytes)
|
||||
KurdContentFetcher.Result.NotFound -> open to status(404, "Not Found")
|
||||
KurdContentFetcher.Result.Unavailable -> PageState.Unavailable to status(502, "Bad Gateway")
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun resolve(name: KurdName): KurdResolution {
|
||||
val now = clock()
|
||||
resolutions[name]?.let { (at, resolution) -> if (now - at < resolutionTtlMillis) return resolution }
|
||||
return resolver.resolve(name).also { resolutions[name] = now to it }
|
||||
}
|
||||
|
||||
private fun status(code: Int, reason: String) = Response(code, reason, "text/plain", ByteArray(0))
|
||||
|
||||
companion object {
|
||||
|
||||
const val CONTENT_SECURITY_POLICY = "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; " +
|
||||
"img-src 'self' data: blob:; media-src 'self' blob:; font-src 'self' data:; connect-src 'self'; " +
|
||||
"worker-src 'none'; object-src 'none'; frame-ancestors 'none'; base-uri 'self'; form-action 'self'"
|
||||
|
||||
/**
|
||||
* Path segments, percent-decoded one by one. Null for a malformed escape, a non-UTF-8 byte
|
||||
* sequence, or a segment that is `.`, `..` or holds an encoded `/`: none names a file.
|
||||
*/
|
||||
fun decodePath(encodedPath: String): List<String>? {
|
||||
val segments = encodedPath.substringBefore('?').substringBefore('#').split('/').filter { it.isNotEmpty() }
|
||||
return segments.map { encoded ->
|
||||
percentDecode(encoded)?.takeIf { it != "." && it != ".." && '/' !in it && it.isNotEmpty() } ?: return null
|
||||
}
|
||||
}
|
||||
|
||||
private fun percentDecode(segment: String): String? {
|
||||
val out = java.io.ByteArrayOutputStream()
|
||||
var index = 0
|
||||
while (index < segment.length) {
|
||||
val char = segment[index]
|
||||
if (char == '%') {
|
||||
if (index + 3 > segment.length) return null
|
||||
val high = hexValue(segment[index + 1])
|
||||
val low = hexValue(segment[index + 2])
|
||||
if (high < 0 || low < 0) return null
|
||||
out.write(high * 16 + low)
|
||||
index += 3
|
||||
} else {
|
||||
out.write(char.toString().toByteArray(Charsets.UTF_8))
|
||||
index++
|
||||
}
|
||||
}
|
||||
val bytes = out.toByteArray()
|
||||
val decoder = Charsets.UTF_8.newDecoder()
|
||||
return runCatching { decoder.decode(java.nio.ByteBuffer.wrap(bytes)).toString() }.getOrNull()
|
||||
}
|
||||
|
||||
/** ASCII hex only: `Character.digit` would also take other scripts' digits. */
|
||||
private fun hexValue(char: Char): Int = when (char) {
|
||||
in '0'..'9' -> char - '0'
|
||||
in 'a'..'f' -> char - 'a' + 10
|
||||
in 'A'..'F' -> char - 'A' + 10
|
||||
else -> -1
|
||||
}
|
||||
|
||||
private val MIME_TYPES = mapOf(
|
||||
"html" to "text/html", "htm" to "text/html", "css" to "text/css", "js" to "text/javascript", "mjs" to "text/javascript",
|
||||
"json" to "application/json", "map" to "application/json", "txt" to "text/plain", "xml" to "application/xml",
|
||||
"svg" to "image/svg+xml", "png" to "image/png", "jpg" to "image/jpeg", "jpeg" to "image/jpeg", "gif" to "image/gif",
|
||||
"webp" to "image/webp", "avif" to "image/avif", "ico" to "image/x-icon", "woff" to "font/woff", "woff2" to "font/woff2",
|
||||
"ttf" to "font/ttf", "otf" to "font/otf", "wasm" to "application/wasm", "mp3" to "audio/mpeg", "ogg" to "audio/ogg",
|
||||
"mp4" to "video/mp4", "webm" to "video/webm", "pdf" to "application/pdf",
|
||||
)
|
||||
|
||||
fun mimeTypeOf(fileName: String?): String {
|
||||
val extension = fileName?.substringAfterLast('.', missingDelimiterValue = "")?.lowercase().orEmpty()
|
||||
return MIME_TYPES[extension] ?: "application/octet-stream"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package io.novafoundation.nova.runtime.names.content
|
||||
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import java.io.IOException
|
||||
|
||||
/**
|
||||
* Asks a trustless gateway for the blocks of one path as a CAR (`dag-scope=entity`: the path's
|
||||
* directories and the whole file or directory at its end), never more than [maxCarBytes].
|
||||
*/
|
||||
class OkHttpCarSource(
|
||||
private val client: OkHttpClient,
|
||||
private val maxCarBytes: Long = UnixFsReader.DEFAULT_MAX_FILE + 8L * 1024 * 1024,
|
||||
) : CarSource {
|
||||
|
||||
override suspend fun car(gateway: String, root: Cid, path: List<String>): ByteArray? = withContext(Dispatchers.IO) {
|
||||
val url = gateway.toHttpUrlOrNull()?.newBuilder()
|
||||
?.addPathSegment("ipfs")
|
||||
?.addPathSegment(root.toString())
|
||||
?.apply { path.forEach(::addPathSegment) }
|
||||
?.addQueryParameter("format", "car")
|
||||
?.addQueryParameter("dag-scope", "entity")
|
||||
?.build()
|
||||
?: return@withContext null
|
||||
|
||||
val request = Request.Builder()
|
||||
.url(url)
|
||||
.header("Accept", "application/vnd.ipld.car;version=1;order=dfs;dups=y")
|
||||
.build()
|
||||
|
||||
client.newCall(request).execute().use { response ->
|
||||
if (!response.isSuccessful) return@withContext null
|
||||
val body = response.body ?: return@withContext null
|
||||
if (body.contentLength() > maxCarBytes) throw IOException("CAR larger than $maxCarBytes bytes")
|
||||
|
||||
val source = body.source()
|
||||
if (source.request(maxCarBytes + 1)) throw IOException("CAR larger than $maxCarBytes bytes")
|
||||
source.buffer.readByteArray()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package io.novafoundation.nova.runtime.names.content
|
||||
|
||||
/** A dag-pb node: `PBNode { repeated PBLink Links = 2; optional bytes Data = 1; }`, fields in any order. */
|
||||
internal class PbNode(val links: List<PbLink>, val data: ByteArray?) {
|
||||
|
||||
class PbLink(val cid: Cid, val name: String?, val size: Long?)
|
||||
|
||||
companion object {
|
||||
|
||||
fun decode(block: ByteArray): PbNode {
|
||||
val links = mutableListOf<PbLink>()
|
||||
var data: ByteArray? = null
|
||||
val reader = ByteReader(block)
|
||||
while (!reader.isExhausted) {
|
||||
when (val key = reader.varint()) {
|
||||
// Data
|
||||
(1L shl 3) or 2 -> {
|
||||
require(data == null) { "Data twice" }
|
||||
data = reader.bytes(lengthOf(reader))
|
||||
}
|
||||
// Links
|
||||
(2L shl 3) or 2 -> links += decodeLink(reader.sub(lengthOf(reader)))
|
||||
else -> throw IllegalArgumentException("Unknown dag-pb field $key")
|
||||
}
|
||||
}
|
||||
return PbNode(links, data)
|
||||
}
|
||||
|
||||
private fun decodeLink(reader: ByteReader): PbLink {
|
||||
var cid: Cid? = null
|
||||
var name: String? = null
|
||||
var size: Long? = null
|
||||
while (!reader.isExhausted) {
|
||||
when (val key = reader.varint()) {
|
||||
(1L shl 3) or 2 -> cid = Cid.parse(reader.bytes(lengthOf(reader)))
|
||||
(2L shl 3) or 2 -> name = String(reader.bytes(lengthOf(reader)), Charsets.UTF_8)
|
||||
(3L shl 3) or 0 -> size = reader.varint()
|
||||
else -> throw IllegalArgumentException("Unknown dag-pb link field $key")
|
||||
}
|
||||
}
|
||||
return PbLink(cid ?: throw IllegalArgumentException("Link without a hash"), name, size)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** UnixFS `Data`, the payload of a dag-pb node that is part of a file system. */
|
||||
internal class UnixFsData(
|
||||
val type: Type,
|
||||
val data: ByteArray?,
|
||||
val fileSize: Long?,
|
||||
val blockSizes: List<Long>,
|
||||
val hashType: Long?,
|
||||
val fanout: Long?,
|
||||
) {
|
||||
|
||||
enum class Type { RAW, DIRECTORY, FILE, METADATA, SYMLINK, HAMT_SHARD }
|
||||
|
||||
companion object {
|
||||
|
||||
fun decode(bytes: ByteArray): UnixFsData {
|
||||
var type: Type? = null
|
||||
var data: ByteArray? = null
|
||||
var fileSize: Long? = null
|
||||
val blockSizes = mutableListOf<Long>()
|
||||
var hashType: Long? = null
|
||||
var fanout: Long? = null
|
||||
|
||||
val reader = ByteReader(bytes)
|
||||
while (!reader.isExhausted) {
|
||||
val key = reader.varint()
|
||||
val field = key ushr 3
|
||||
when (key and 7) {
|
||||
0L -> {
|
||||
val value = reader.varint()
|
||||
when (field) {
|
||||
1L -> type = Type.values().getOrNull(value.toInt()) ?: throw IllegalArgumentException("Unknown UnixFS type $value")
|
||||
3L -> fileSize = value
|
||||
4L -> blockSizes += value
|
||||
5L -> hashType = value
|
||||
6L -> fanout = value
|
||||
// mode (7) and other scalar metadata do not change what is served
|
||||
}
|
||||
}
|
||||
2L -> {
|
||||
val value = reader.bytes(lengthOf(reader))
|
||||
when (field) {
|
||||
2L -> data = value
|
||||
// packed blocksizes
|
||||
4L -> ByteReader(value).let { packed -> while (!packed.isExhausted) blockSizes += packed.varint() }
|
||||
// mtime (8) and other nested metadata do not change what is served
|
||||
}
|
||||
}
|
||||
else -> throw IllegalArgumentException("Unsupported UnixFS wire type")
|
||||
}
|
||||
}
|
||||
|
||||
return UnixFsData(type ?: throw IllegalArgumentException("UnixFS without a type"), data, fileSize, blockSizes, hashType, fanout)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun lengthOf(reader: ByteReader): Int {
|
||||
val length = reader.varint()
|
||||
require(length in 0..reader.remaining.toLong()) { "Length past the end" }
|
||||
return length.toInt()
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
package io.novafoundation.nova.runtime.names.content
|
||||
|
||||
/**
|
||||
* Walks a UnixFS tree held in [blocks] and assembles files from it. Every block was checked
|
||||
* against its CID when the CAR was read, and every step here follows a CID taken from a block
|
||||
* already checked, so what comes out is exactly what the root CID names.
|
||||
*/
|
||||
class UnixFsReader(private val blocks: VerifiedBlocks, private val maxFileSize: Int = DEFAULT_MAX_FILE) {
|
||||
|
||||
sealed interface Entry {
|
||||
class File(val bytes: ByteArray) : Entry
|
||||
class Directory(val cid: Cid) : Entry
|
||||
}
|
||||
|
||||
/** The entry at [path] (segments, already split and decoded) under [root]. Null when nothing is there. */
|
||||
fun read(root: Cid, path: List<String>): Entry? = verify {
|
||||
var cid = root
|
||||
for (segment in path) {
|
||||
require(segment.isNotEmpty() && segment != "." && segment != ".." && '/' !in segment) { "Bad path segment" }
|
||||
cid = child(cid, segment) ?: return@verify null
|
||||
}
|
||||
entry(cid)
|
||||
}
|
||||
|
||||
private fun entry(cid: Cid): Entry {
|
||||
if (cid.codec == Cid.Codec.RAW) return Entry.File(blocks.get(cid).also(::checkSize))
|
||||
|
||||
val node = PbNode.decode(blocks.get(cid))
|
||||
val unixFs = UnixFsData.decode(node.data ?: throw IllegalArgumentException("dag-pb node without UnixFS data"))
|
||||
return when (unixFs.type) {
|
||||
UnixFsData.Type.FILE, UnixFsData.Type.RAW -> Entry.File(assemble(node, unixFs))
|
||||
UnixFsData.Type.DIRECTORY, UnixFsData.Type.HAMT_SHARD -> Entry.Directory(cid)
|
||||
UnixFsData.Type.SYMLINK, UnixFsData.Type.METADATA -> throw ContentVerificationException("Unsupported UnixFS entry ${unixFs.type}")
|
||||
}
|
||||
}
|
||||
|
||||
private fun child(directory: Cid, name: String): Cid? {
|
||||
if (directory.codec != Cid.Codec.DAG_PB) return null
|
||||
val node = PbNode.decode(blocks.get(directory))
|
||||
val unixFs = UnixFsData.decode(node.data ?: throw IllegalArgumentException("dag-pb node without UnixFS data"))
|
||||
|
||||
return when (unixFs.type) {
|
||||
UnixFsData.Type.DIRECTORY -> node.links.singleOrNull { it.name == name }?.cid
|
||||
UnixFsData.Type.HAMT_SHARD -> shardLookup(node, unixFs, name, HashBits(murmur3x64_64(name.toByteArray(Charsets.UTF_8))), depth = 0)
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* HAMT directory lookup, as `go-unixfs`/`boxo` shard: each level consumes log2(fanout) bits of
|
||||
* the name's murmur3-x64-64 hash, most significant first; a link named by the bucket index
|
||||
* alone (upper-case hex) is a child shard, one named by index plus the entry name is the entry.
|
||||
*/
|
||||
private fun shardLookup(node: PbNode, unixFs: UnixFsData, name: String, bits: HashBits, depth: Int): Cid? {
|
||||
require(unixFs.hashType == MURMUR3_X64_64) { "Unsupported HAMT hash" }
|
||||
val fanout = unixFs.fanout ?: throw IllegalArgumentException("HAMT without fanout")
|
||||
require(fanout in 2..MAX_FANOUT && fanout and (fanout - 1) == 0L) { "Bad HAMT fanout" }
|
||||
val width = java.lang.Long.numberOfTrailingZeros(fanout)
|
||||
require(depth < 64 / width) { "HAMT deeper than its hash" }
|
||||
|
||||
val padLength = (fanout - 1).toString(16).length
|
||||
val prefix = bits.next(width).toString(16).uppercase().padStart(padLength, '0')
|
||||
|
||||
for (link in node.links) {
|
||||
val linkName = link.name ?: continue
|
||||
if (!linkName.startsWith(prefix)) continue
|
||||
if (linkName.length == padLength) {
|
||||
val shard = PbNode.decode(blocks.get(link.cid))
|
||||
val shardData = UnixFsData.decode(shard.data ?: throw IllegalArgumentException("Shard without data"))
|
||||
require(shardData.type == UnixFsData.Type.HAMT_SHARD && shardData.fanout == fanout) { "Inconsistent HAMT shard" }
|
||||
return shardLookup(shard, shardData, name, bits, depth + 1)
|
||||
}
|
||||
if (linkName.substring(padLength) == name) return link.cid
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
private fun assemble(node: PbNode, unixFs: UnixFsData): ByteArray {
|
||||
val out = java.io.ByteArrayOutputStream()
|
||||
appendFile(node, unixFs, out, depth = 0)
|
||||
return out.toByteArray()
|
||||
}
|
||||
|
||||
/** A file node's own data comes first, then its children in link order. */
|
||||
private fun appendFile(node: PbNode, unixFs: UnixFsData, out: java.io.ByteArrayOutputStream, depth: Int) {
|
||||
require(depth < MAX_FILE_DEPTH) { "File tree too deep" }
|
||||
unixFs.data?.let { append(out, it) }
|
||||
|
||||
for (link in node.links) {
|
||||
if (link.cid.codec == Cid.Codec.RAW) {
|
||||
append(out, blocks.get(link.cid))
|
||||
continue
|
||||
}
|
||||
val child = PbNode.decode(blocks.get(link.cid))
|
||||
val childData = UnixFsData.decode(child.data ?: throw IllegalArgumentException("File part without data"))
|
||||
require(childData.type == UnixFsData.Type.FILE || childData.type == UnixFsData.Type.RAW) { "File part is not a file" }
|
||||
appendFile(child, childData, out, depth + 1)
|
||||
}
|
||||
}
|
||||
|
||||
private fun append(out: java.io.ByteArrayOutputStream, bytes: ByteArray) {
|
||||
checkSize(out.size().toLong() + bytes.size)
|
||||
out.write(bytes)
|
||||
}
|
||||
|
||||
private fun checkSize(bytes: ByteArray) = checkSize(bytes.size.toLong())
|
||||
|
||||
private fun checkSize(size: Long) {
|
||||
if (size > maxFileSize) throw ContentVerificationException("File larger than $maxFileSize bytes")
|
||||
}
|
||||
|
||||
private inline fun <T> verify(block: () -> T): T = try {
|
||||
block()
|
||||
} catch (e: IllegalArgumentException) {
|
||||
throw ContentVerificationException("Malformed UnixFS: ${e.message}")
|
||||
}
|
||||
|
||||
/** The bits of a hash, most significant first. */
|
||||
private class HashBits(private val hash: Long) {
|
||||
|
||||
private var used = 0
|
||||
|
||||
fun next(count: Int): Long {
|
||||
require(used + count <= 64) { "HAMT hash exhausted" }
|
||||
val value = (hash ushr (64 - used - count)) and ((1L shl count) - 1)
|
||||
used += count
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
const val DEFAULT_MAX_FILE = 32 * 1024 * 1024
|
||||
|
||||
/** Multicodec `murmur3-x64-64`, the only hash UnixFS shards use. */
|
||||
private const val MURMUR3_X64_64 = 0x22L
|
||||
private const val MAX_FANOUT = 1024L
|
||||
private const val MAX_FILE_DEPTH = 32
|
||||
|
||||
/** The first 64 bits of MurmurHash3 x64 128 with seed 0 (`murmur3.New64` in Go). */
|
||||
internal fun murmur3x64_64(data: ByteArray): Long {
|
||||
val c1 = -0x783c846eeebdac2bL // 0x87c37b91114253d5
|
||||
val c2 = 0x4cf5ad432745937fL
|
||||
var h1 = 0L
|
||||
var h2 = 0L
|
||||
|
||||
fun littleEndian(offset: Int, count: Int): Long {
|
||||
var value = 0L
|
||||
for (i in count - 1 downTo 0) value = (value shl 8) or (data[offset + i].toLong() and 0xff)
|
||||
return value
|
||||
}
|
||||
|
||||
val blocks = data.size / 16
|
||||
for (block in 0 until blocks) {
|
||||
var k1 = littleEndian(block * 16, 8)
|
||||
var k2 = littleEndian(block * 16 + 8, 8)
|
||||
|
||||
k1 *= c1; k1 = java.lang.Long.rotateLeft(k1, 31); k1 *= c2; h1 = h1 xor k1
|
||||
h1 = java.lang.Long.rotateLeft(h1, 27); h1 += h2; h1 = h1 * 5 + 0x52dce729
|
||||
|
||||
k2 *= c2; k2 = java.lang.Long.rotateLeft(k2, 33); k2 *= c1; h2 = h2 xor k2
|
||||
h2 = java.lang.Long.rotateLeft(h2, 31); h2 += h1; h2 = h2 * 5 + 0x38495ab5
|
||||
}
|
||||
|
||||
val tail = blocks * 16
|
||||
val tailLength = data.size - tail
|
||||
if (tailLength > 8) {
|
||||
var k2 = littleEndian(tail + 8, tailLength - 8)
|
||||
k2 *= c2; k2 = java.lang.Long.rotateLeft(k2, 33); k2 *= c1; h2 = h2 xor k2
|
||||
}
|
||||
if (tailLength > 0) {
|
||||
var k1 = littleEndian(tail, minOf(tailLength, 8))
|
||||
k1 *= c1; k1 = java.lang.Long.rotateLeft(k1, 31); k1 *= c2; h1 = h1 xor k1
|
||||
}
|
||||
|
||||
h1 = h1 xor data.size.toLong()
|
||||
h2 = h2 xor data.size.toLong()
|
||||
h1 += h2
|
||||
h2 += h1
|
||||
h1 = fmix(h1)
|
||||
h2 = fmix(h2)
|
||||
h1 += h2
|
||||
|
||||
return h1
|
||||
}
|
||||
|
||||
private fun fmix(value: Long): Long {
|
||||
var k = value
|
||||
k = k xor (k ushr 33)
|
||||
k *= -0xae502812aa7333L // 0xff51afd7ed558ccd
|
||||
k = k xor (k ushr 33)
|
||||
k *= -0x3b314601e57a13adL // 0xc4ceb9fe1a85ec53
|
||||
k = k xor (k ushr 33)
|
||||
return k
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package io.novafoundation.nova.runtime.names.content
|
||||
|
||||
class ContentVerificationException(message: String) : Exception(message)
|
||||
|
||||
/**
|
||||
* Blocks whose bytes have each been checked against their CID. Built only from a CAR the network
|
||||
* sent, so a mirror can leave blocks out (the read then fails) but cannot change one.
|
||||
*/
|
||||
class VerifiedBlocks private constructor(private val blocks: Map<Cid, ByteArray>) {
|
||||
|
||||
/** The block named by [cid], or a failure when no mirror sent it. */
|
||||
fun get(cid: Cid): ByteArray = cid.inlineBlock ?: blocks[cid] ?: throw ContentVerificationException("Missing block $cid")
|
||||
|
||||
val size: Int get() = blocks.size
|
||||
|
||||
companion object {
|
||||
|
||||
/** A block larger than this is not one IPFS tooling produces (its limit is 1 MiB, plus framing). */
|
||||
const val MAX_BLOCK = 2 * 1024 * 1024
|
||||
|
||||
private const val MAX_HEADER = 1024
|
||||
|
||||
/**
|
||||
* Reads a CARv1 stream. Every block must match its CID or the whole CAR is refused: a mirror
|
||||
* that changes one byte is not a mirror. The header's roots are not trusted; the caller
|
||||
* starts from the CID the chain gave.
|
||||
*/
|
||||
fun fromCar(car: ByteArray): VerifiedBlocks {
|
||||
val reader = ByteReader(car)
|
||||
try {
|
||||
val headerLength = reader.varint()
|
||||
require(headerLength in 1..MAX_HEADER) { "Bad CAR header length" }
|
||||
requireCarV1(reader.bytes(headerLength.toInt()))
|
||||
|
||||
val blocks = mutableMapOf<Cid, ByteArray>()
|
||||
while (!reader.isExhausted) {
|
||||
val sectionLength = reader.varint()
|
||||
require(sectionLength in 1..(MAX_BLOCK + 128).toLong()) { "Bad CAR section length" }
|
||||
val section = reader.sub(sectionLength.toInt())
|
||||
val cid = Cid.read(section)
|
||||
val block = section.bytes(section.remaining)
|
||||
if (!cid.matches(block)) throw ContentVerificationException("Block does not match $cid")
|
||||
blocks[cid] = block
|
||||
}
|
||||
|
||||
return VerifiedBlocks(blocks)
|
||||
} catch (e: IllegalArgumentException) {
|
||||
throw ContentVerificationException("Malformed CAR: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
/** Header is the dag-cbor map `{roots: [...], version: 1}`; only the version is read. */
|
||||
private fun requireCarV1(header: ByteArray) {
|
||||
val versionKey = byteArrayOf(0x67) + "version".toByteArray()
|
||||
val at = indexOf(header, versionKey)
|
||||
require(at >= 0 && at + versionKey.size < header.size && header[at + versionKey.size] == 0x01.toByte()) { "Not a CARv1" }
|
||||
}
|
||||
|
||||
private fun indexOf(haystack: ByteArray, needle: ByteArray): Int {
|
||||
outer@ for (start in 0..haystack.size - needle.size) {
|
||||
for (offset in needle.indices) if (haystack[start + offset] != needle[offset]) continue@outer
|
||||
return start
|
||||
}
|
||||
return -1
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,11 @@ public class Log {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static int w(String tag, String msg, Throwable tr) {
|
||||
System.out.println("WARN: " + tag + ": " + msg + ": " + tr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static int e(String tag, String msg) {
|
||||
System.out.println("ERROR: " + tag + ": " + msg);
|
||||
return 0;
|
||||
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package io.novafoundation.nova.runtime.multiNetwork.connection.light
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Test
|
||||
|
||||
class LightClientLeasesTest {
|
||||
|
||||
private val leases = LightClientLeases(relayOf = { if (it == PEOPLE) RELAY else null })
|
||||
private val registered = setOf(RELAY, PEOPLE)
|
||||
|
||||
@Test
|
||||
fun `a chain stays while any holder still uses it`() {
|
||||
val connection = Any()
|
||||
val resolver = Any()
|
||||
leases.acquire(PEOPLE, connection)
|
||||
leases.acquire(PEOPLE, resolver)
|
||||
|
||||
leases.release(PEOPLE, connection)
|
||||
assertNull(leases.nextUnused(registered))
|
||||
|
||||
leases.release(PEOPLE, resolver)
|
||||
assertEquals(PEOPLE, leases.nextUnused(registered))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `asking twice and releasing once frees the chain`() {
|
||||
val connection = Any()
|
||||
leases.acquire(PEOPLE, connection)
|
||||
leases.acquire(PEOPLE, connection)
|
||||
|
||||
leases.release(PEOPLE, connection)
|
||||
|
||||
assertEquals(PEOPLE, leases.nextUnused(registered))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a relay goes only after its teyrchains`() {
|
||||
assertEquals(PEOPLE, leases.nextUnused(registered))
|
||||
assertEquals(RELAY, leases.nextUnused(setOf(RELAY)))
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val RELAY = "relay"
|
||||
const val PEOPLE = "people"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package io.novafoundation.nova.runtime.names
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
|
||||
class KurdLookalikesTest {
|
||||
|
||||
@Test
|
||||
fun `a zone flattened into the label suggests the zone name`() {
|
||||
assertEquals(listOf("serok.gov.kurd"), candidates("serok-gov.kurd"))
|
||||
assertEquals(listOf("serok.gov.kurd"), candidates("serokgov.kurd"))
|
||||
assertEquals(listOf("zanko.edu.kurd"), candidates("zanko-edu.kurd"))
|
||||
assertEquals(listOf("nuce.darayi.gov.kurd"), candidates("nuce.darayi-gov.kurd"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `ordinary and official names suggest nothing`() {
|
||||
assertEquals(emptyList<String>(), candidates("dks.kurd"))
|
||||
assertEquals(emptyList<String>(), candidates("serok.gov.kurd"))
|
||||
assertEquals(emptyList<String>(), candidates("gov.kurd"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a guess that would not be a valid name is dropped`() {
|
||||
// five labels are too deep
|
||||
assertEquals(emptyList<String>(), candidates("a.b.c.d-gov.kurd"))
|
||||
}
|
||||
|
||||
private fun candidates(name: String): List<String> = KurdName.parse(name)?.let { KurdLookalikes.candidates(it).map(KurdName::name) } ?: emptyList()
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package io.novafoundation.nova.runtime.names
|
||||
|
||||
import io.novasama.substrate_sdk_android.extensions.toHexString
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Test
|
||||
|
||||
class KurdNameTest {
|
||||
|
||||
// The vectors pinned by pezpallet-navnisan's `namehash_vectors_are_pinned`
|
||||
@Test
|
||||
fun `nodes match the pallet's pinned vectors`() {
|
||||
assertEquals("011fc4e97a5ef275bd399ccbe8fca90e5f7a907c010663259364a9fb4b1d64d9", KurdName.KURD_NODE.toHexString())
|
||||
assertEquals("a10d2f1372dde3c6b1853b3f14f883fdd6a3ca08944d24e85b56c71249441ed5", node("ahmet.kurd"))
|
||||
assertEquals("c5c21a8e620900b2c976bc0919a91167b427de09f025bb24bf46f91a39a62907", node("gov.kurd"))
|
||||
assertEquals("3569b4cc564de96e00781bee7ad6cda51a65006529208b32e99d3be61ac7708e", node("dks.gov.kurd"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `nodes upward run from the name to its top-level name`() {
|
||||
val nodes = KurdName.parse("dks.gov.kurd")!!.nodesUpward().map { it.toHexString() }
|
||||
|
||||
assertEquals(listOf(node("dks.gov.kurd"), node("gov.kurd")), nodes)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `input is lower-cased and may carry a scheme, a trailing dot or slash`() {
|
||||
assertEquals("dks.kurd", KurdName.parse(" DKS.Kurd ")!!.name)
|
||||
assertEquals("serok.gov.kurd", KurdName.parse("https://Serok.Gov.Kurd/")!!.name)
|
||||
assertEquals("dks.kurd", KurdName.parse("dks.kurd.")!!.name)
|
||||
assertEquals(listOf("serok", "gov"), KurdName.parse("serok.gov.kurd")!!.labels)
|
||||
assertEquals("gov", KurdName.parse("serok.gov.kurd")!!.top)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `names the pallet refuses are never parsed`() {
|
||||
listOf(
|
||||
"uni-x--edu.kurd", // `--` anywhere
|
||||
"-dks.kurd",
|
||||
"dks-.kurd",
|
||||
"dks.gov.com",
|
||||
"dks",
|
||||
".kurd",
|
||||
"a..kurd",
|
||||
"dks_news.kurd",
|
||||
"dêrsim.kurd", // not ASCII: never lower-cased into something else
|
||||
"a.b.c.d.e.kurd", // deeper than four labels
|
||||
"dks.kurd/path",
|
||||
"dks.kurd:443",
|
||||
"http://dks.kurd",
|
||||
"user@dks.kurd",
|
||||
"a".repeat(64) + ".kurd",
|
||||
).forEach { assertNull(it, KurdName.parse(it)) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a label of 63 bytes and a name of four labels are accepted`() {
|
||||
assertEquals(63, KurdName.parse("a".repeat(63) + ".kurd")!!.labels.single().length)
|
||||
assertEquals(4, KurdName.parse("a.b.c.d.kurd")!!.labels.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `any input naming a kurd host is recognised, valid name or not`() {
|
||||
listOf(
|
||||
"Uni-X--Edu.KURD/",
|
||||
"dks_news.kurd",
|
||||
"user@dks.kurd",
|
||||
"dks.kurd/path?q=1",
|
||||
"http://dks.kurd",
|
||||
"https://serok.gov.kurd:443/x",
|
||||
"dks.kurd.",
|
||||
).forEach {
|
||||
assertEquals(it, true, KurdName.mentionsKurd(it))
|
||||
}
|
||||
listOf("kurd.com", "https://kurd.com/a.kurd/", "dkskurd", "duckduckgo.com/?q=dks").forEach {
|
||||
assertEquals(it, false, KurdName.mentionsKurd(it))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `kurd hosts include the bare suffix and a trailing dot`() {
|
||||
listOf("kurd", "dks.kurd", "DKS.KURD.", "a.b.gov.kurd").forEach { assertEquals(it, true, KurdName.isKurdHost(it)) }
|
||||
listOf(null, "kurd.com", "notkurd", "dks.kurd.com").forEach { assertEquals(it.toString(), false, KurdName.isKurdHost(it)) }
|
||||
}
|
||||
|
||||
private fun node(name: String) = KurdName.parse(name)!!.node.toHexString()
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package io.novafoundation.nova.runtime.names
|
||||
|
||||
import io.novafoundation.nova.runtime.names.RealKurdNameResolver.Companion.judge
|
||||
import io.novafoundation.nova.runtime.names.RealKurdNameResolver.Companion.refuseOrGiveUp
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class KurdResolutionJudgeTest {
|
||||
|
||||
private val dks = KurdName.parse("dks.kurd")!!
|
||||
private val ahmet = KurdName.parse("ahmet.kurd")!!
|
||||
|
||||
@Test
|
||||
fun `a proven official name resolves as official`() {
|
||||
val resolution = judge(dks, found(NameFixtures.DKS), verified = true) as KurdResolution.Resolved
|
||||
|
||||
assertTrue(resolution.verified)
|
||||
assertTrue(resolution.official)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an unproven official, institutional, reserved or zone name never opens`() {
|
||||
assertTrue(judge(dks, found(NameFixtures.DKS), verified = false) is KurdResolution.Refused)
|
||||
assertTrue(judge(ahmet, found(leased(), locked = true), verified = false) is KurdResolution.Refused)
|
||||
assertTrue(judge(KurdName.parse("ahmet.edu.kurd")!!, found(leased()), verified = false) is KurdResolution.Refused)
|
||||
assertTrue(judge(ahmet, found(leased(owner = NameRecord.Owner.Governance)), verified = false) is KurdResolution.Refused)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an unproven bought name opens with the warning and never as official`() {
|
||||
val resolution = judge(ahmet, found(leased()), verified = false) as KurdResolution.Resolved
|
||||
|
||||
assertFalse(resolution.verified)
|
||||
assertFalse(resolution.official)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `with no answer a zone name is refused and any other is unavailable`() {
|
||||
assertTrue(refuseOrGiveUp(KurdName.parse("serok.gov.kurd")!!) is KurdResolution.Refused)
|
||||
assertTrue(refuseOrGiveUp(ahmet) is KurdResolution.Unavailable)
|
||||
}
|
||||
|
||||
private fun found(raw: ByteArray, locked: Boolean = false) = found(NameRecord.decode(raw), locked)
|
||||
|
||||
private fun found(record: NameRecord, locked: Boolean = false) = NameLookup.Found(record, NameStatus.ACTIVE, listOf("raw"), locked)
|
||||
|
||||
private fun leased(owner: NameRecord.Owner = NameRecord.Owner.Account(ByteArray(32))) = NameRecord(
|
||||
label = "ahmet", parent = KurdName.KURD_NODE, parentReg = 0, reg = 9, owner = owner, kind = NameRecord.Kind.LEASED,
|
||||
lockedTransfer = false, expiry = Long.MAX_VALUE, account = null, content = null,
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package io.novafoundation.nova.runtime.names
|
||||
|
||||
import io.novasama.substrate_sdk_android.extensions.fromHex
|
||||
|
||||
/** Raw `Navnisan::Names` values read from Zagros People (spec 1020024) on 2026-11-05. */
|
||||
internal object NameFixtures {
|
||||
|
||||
val GOV = "0x0c676f76011fc4e97a5ef275bd399ccbe8fca90e5f7a907c010663259364a9fb4b1d64d90000000000000000010000000000000002010100000000".fromHex()
|
||||
|
||||
val DKS = "0x0c646b73011fc4e97a5ef275bd399ccbe8fca90e5f7a907c010663259364a9fb4b1d64d90000000000000000190000000000000002010100000000".fromHex()
|
||||
|
||||
val SEROK_GOV = hex(
|
||||
"0x147365726f6bc5c21a8e620900b2c976bc0919a91167b427de09f025bb24bf46f91a39a629070100000000000000070000000000000001030201000192b5",
|
||||
"e877ed42d604c921154f456dd2effe85991d07c1577b9709beb7dee411190000",
|
||||
)
|
||||
|
||||
val DARAYI_GOV = hex(
|
||||
"0x18646172617969c5c21a8e620900b2c976bc0919a91167b427de09f025bb24bf46f91a39a6290701000000000000000b00000000000000012502010001",
|
||||
"eaa3a59fbd6a4795ac240a00d00b4f6c1cc288934679cb9a1e6557be4ae972480000",
|
||||
)
|
||||
|
||||
/** `Timestamp::Now` at the block the records were read from. */
|
||||
const val NOW = 0x01_a1_ac_c3_49_d0L
|
||||
|
||||
private fun hex(vararg parts: String) = parts.joinToString("").fromHex()
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
package io.novafoundation.nova.runtime.names
|
||||
|
||||
import io.novasama.substrate_sdk_android.extensions.toHexString
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertSame
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class NameLookupTest {
|
||||
|
||||
private val gov = KurdName.parse("gov.kurd")!!
|
||||
private val serok = KurdName.parse("serok.gov.kurd")!!
|
||||
|
||||
@Test
|
||||
fun `a ministry subname resolves with its zone under the live records`() = runBlocking {
|
||||
val reader = FakeReader().put(gov, NameFixtures.GOV).put(serok, NameFixtures.SEROK_GOV)
|
||||
|
||||
val found = NameLookup.read(reader, serok) as NameLookup.Found
|
||||
|
||||
assertEquals("serok", found.record.label)
|
||||
assertEquals(NameStatus.ACTIVE, found.status)
|
||||
assertEquals(2, found.rawChain.size)
|
||||
assertFalse(found.locked)
|
||||
assertEquals(1_284_551L, found.block)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `every read is made at the finalized block`() = runBlocking {
|
||||
val reader = FakeReader().put(gov, NameFixtures.GOV).put(serok, NameFixtures.SEROK_GOV)
|
||||
|
||||
NameLookup.read(reader, serok)
|
||||
|
||||
assertEquals(setOf(FakeReader.HEAD), reader.blocksRead)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an unregistered name is not found and a missing ancestor lapses it`() = runBlocking {
|
||||
assertSame(NameLookup.NotFound, NameLookup.read(FakeReader(), serok))
|
||||
assertSame(NameLookup.Lapsed, NameLookup.read(FakeReader().put(serok, NameFixtures.SEROK_GOV), serok))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a subname set against an earlier registration of its parent lapses`() = runBlocking {
|
||||
val reRegisteredGov = NameFixtures.GOV.copyOf().also { it[1 + 3 + 32 + 8] = 2 } // reg 1 -> 2
|
||||
val reader = FakeReader().put(gov, reRegisteredGov).put(serok, NameFixtures.SEROK_GOV)
|
||||
|
||||
assertSame(NameLookup.Lapsed, NameLookup.read(reader, serok))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a record stored under another name's key is refused`() = runBlocking {
|
||||
val dks = KurdName.parse("dks.kurd")!!
|
||||
val reader = FakeReader().put(dks, NameFixtures.GOV)
|
||||
|
||||
assertTrue(runCatching { NameLookup.read(reader, dks) }.isFailure)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a reserved label is reported`() = runBlocking {
|
||||
val dks = KurdName.parse("dks.kurd")!!
|
||||
val reader = FakeReader().put(dks, NameFixtures.DKS).lock(dks)
|
||||
|
||||
assertTrue((NameLookup.read(reader, dks) as NameLookup.Found).locked)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a lease is graced for thirty days after it ends, a grant is not`() {
|
||||
val end = 1_000L
|
||||
val leased = record(kind = NameRecord.Kind.LEASED, expiry = end)
|
||||
val granted = record(kind = NameRecord.Kind.GRANTED, expiry = end)
|
||||
|
||||
assertEquals(NameStatus.ACTIVE, NameLookup.ownStatus(leased, end - 1))
|
||||
assertEquals(NameStatus.GRACE, NameLookup.ownStatus(leased, end))
|
||||
assertEquals(NameStatus.GRACE, NameLookup.ownStatus(leased, end + NameLookup.GRACE_PERIOD_MILLIS - 1))
|
||||
assertEquals(null, NameLookup.ownStatus(leased, end + NameLookup.GRACE_PERIOD_MILLIS))
|
||||
assertEquals(null, NameLookup.ownStatus(granted, end))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a parent in grace graces its subname, an expired parent lapses it`() {
|
||||
val sub = record(kind = NameRecord.Kind.SUB, expiry = null, parentReg = 7)
|
||||
val parent = { expiry: Long -> record(kind = NameRecord.Kind.LEASED, expiry = expiry, reg = 7) }
|
||||
|
||||
assertEquals(NameStatus.GRACE, NameLookup.effectiveStatus(listOf(sub, parent(500)), now = 600))
|
||||
assertEquals(null, NameLookup.effectiveStatus(listOf(sub, parent(500)), now = 500 + NameLookup.GRACE_PERIOD_MILLIS))
|
||||
assertEquals(NameStatus.ACTIVE, NameLookup.effectiveStatus(listOf(sub, parent(700)), now = 600))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a chain that ends in a subname is not valid`() {
|
||||
val sub = record(kind = NameRecord.Kind.SUB, expiry = null)
|
||||
|
||||
assertEquals(null, NameLookup.effectiveStatus(listOf(sub), now = 0))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `sources agree only on identical records`() = runBlocking {
|
||||
val one = FakeReader().put(gov, NameFixtures.GOV).put(serok, NameFixtures.SEROK_GOV)
|
||||
val other = FakeReader().put(gov, NameFixtures.GOV).put(serok, NameFixtures.SEROK_GOV.copyOf())
|
||||
val differs = FakeReader().put(gov, NameFixtures.GOV).put(serok, NameFixtures.SEROK_GOV.copyOf().also { it[it.size - 3] = 0x55 })
|
||||
|
||||
val first = NameLookup.read(one, serok)
|
||||
assertTrue(first.agreesWith(NameLookup.read(other, serok)))
|
||||
assertFalse(first.agreesWith(NameLookup.read(differs, serok)))
|
||||
assertFalse(first.agreesWith(NameLookup.NotFound))
|
||||
}
|
||||
|
||||
private fun record(kind: NameRecord.Kind, expiry: Long?, reg: Long = 1, parentReg: Long = 1) = NameRecord(
|
||||
label = "x", parent = ByteArray(32), parentReg = parentReg, reg = reg, owner = NameRecord.Owner.Governance, kind = kind,
|
||||
lockedTransfer = false, expiry = expiry, account = null, content = null,
|
||||
)
|
||||
|
||||
private class FakeReader : NameStorageReader {
|
||||
|
||||
private val values = mutableMapOf<String, ByteArray>()
|
||||
val blocksRead = mutableSetOf<String>()
|
||||
|
||||
init {
|
||||
values[NameLookup.timestampNowKey.toHexString()] = ByteArray(8) { (NameFixtures.NOW ushr (8 * it)).toByte() }
|
||||
}
|
||||
|
||||
fun put(name: KurdName, record: ByteArray) = apply { values[NameLookup.namesKey(name.node).toHexString()] = record }
|
||||
|
||||
fun lock(name: KurdName) = apply { values[NameLookup.lockedKey(name.node).toHexString()] = byteArrayOf(0) }
|
||||
|
||||
override suspend fun finalizedHead() = HEAD
|
||||
|
||||
override suspend fun blockNumber(at: String): Long = 1_284_551
|
||||
|
||||
override suspend fun storage(key: ByteArray, at: String): ByteArray? {
|
||||
blocksRead += at
|
||||
return values[key.toHexString()]
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val HEAD = "0xfinalized"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package io.novafoundation.nova.runtime.names
|
||||
|
||||
import io.novasama.substrate_sdk_android.extensions.toHexString
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class NameRecordTest {
|
||||
|
||||
@Test
|
||||
fun `a zone granted at genesis decodes as governance owned and official`() {
|
||||
val gov = NameRecord.decode(NameFixtures.GOV)
|
||||
|
||||
assertEquals("gov", gov.label)
|
||||
assertEquals(KurdName.KURD_NODE.toHexString(), gov.parent.toHexString())
|
||||
assertEquals(1L, gov.reg)
|
||||
assertTrue(gov.owner is NameRecord.Owner.Governance)
|
||||
assertEquals(NameRecord.Kind.GRANTED, gov.kind)
|
||||
assertTrue(gov.lockedTransfer)
|
||||
assertNull(gov.expiry)
|
||||
assertNull(gov.account)
|
||||
assertNull(gov.content)
|
||||
assertTrue(gov.isOfficial)
|
||||
assertTrue(gov.isInstitutional)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a ministry subname decodes as office owned with its account`() {
|
||||
val serok = NameRecord.decode(NameFixtures.SEROK_GOV)
|
||||
|
||||
assertEquals("serok", serok.label)
|
||||
assertEquals(KurdName.parse("gov.kurd")!!.node.toHexString(), serok.parent.toHexString())
|
||||
assertEquals(1L, serok.parentReg)
|
||||
assertEquals(3, (serok.owner as NameRecord.Owner.Office).tiki)
|
||||
assertEquals(NameRecord.Kind.SUB, serok.kind)
|
||||
assertEquals(32, serok.account!!.size)
|
||||
assertTrue(serok.isOfficial)
|
||||
|
||||
assertEquals(0x25, (NameRecord.decode(NameFixtures.DARAYI_GOV).owner as NameRecord.Owner.Office).tiki)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a trailing byte is refused rather than ignored`() {
|
||||
val result = runCatching { NameRecord.decode(NameFixtures.DKS + byteArrayOf(0)) }
|
||||
|
||||
assertFalse(result.isSuccess)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `content variants decode`() {
|
||||
val cid = byteArrayOf(0xe3.toByte(), 1, 2, 3)
|
||||
val withIpfs = NameFixtures.DKS.copyOf(NameFixtures.DKS.size - 2) + byteArrayOf(1, 0, (cid.size shl 2).toByte()) + cid + byteArrayOf(0)
|
||||
|
||||
val content = NameRecord.decode(withIpfs).content as NameRecord.Content.Ipfs
|
||||
|
||||
assertEquals(cid.toHexString(), content.contenthash.toHexString())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package io.novafoundation.nova.runtime.names.content
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class CidTest {
|
||||
|
||||
private val root = cid("bafybeietjm63oynimmv5yyqay33nui4y4wx6u3peezwetxgiwvfmelutzu")
|
||||
|
||||
@Test
|
||||
fun `base32 form round trips`() {
|
||||
assertEquals("bafybeietjm63oynimmv5yyqay33nui4y4wx6u3peezwetxgiwvfmelutzu", root.toString())
|
||||
assertEquals(Cid.Codec.DAG_PB, root.codec)
|
||||
assertEquals(Cid.Codec.RAW, cid("bafkreifjjcie6lypi6ny7amxnfftagclbuxndqonfipmb64f2km2devei4").codec)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an ipfs contenthash as Navnisan stores it gives its CID`() {
|
||||
assertEquals(root, Cid.fromContenthash(byteArrayOf(0xe3.toByte(), 0x01) + root.bytes))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `other contenthashes are not content`() {
|
||||
assertNull("ipns", Cid.fromContenthash(byteArrayOf(0xe5.toByte(), 0x01) + root.bytes))
|
||||
assertNull("trailing byte", Cid.fromContenthash(byteArrayOf(0xe3.toByte(), 0x01) + root.bytes + 0))
|
||||
assertNull("truncated", Cid.fromContenthash(byteArrayOf(0xe3.toByte(), 0x01) + root.bytes.copyOf(10)))
|
||||
// An identity CID would put the site in the record itself; a root must be a real hash
|
||||
assertNull("identity root", Cid.fromContenthash(byteArrayOf(0xe3.toByte(), 0x01, 0x01, 0x55, 0x00, 0x02, 0x68, 0x69)))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a block matches only its own CID`() {
|
||||
val hello = cid("bafkreifjjcie6lypi6ny7amxnfftagclbuxndqonfipmb64f2km2devei4")
|
||||
|
||||
assertTrue(hello.matches("hello world\n".toByteArray()))
|
||||
assertFalse(hello.matches("hello world!".toByteArray()))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `unsupported codecs and hashes are refused`() {
|
||||
// dag-cbor (0x71)
|
||||
assertTrue(runCatching { Cid.parse(byteArrayOf(0x01, 0x71, 0x12, 0x20) + ByteArray(32)) }.isFailure)
|
||||
// sha2-512 (0x13)
|
||||
assertTrue(runCatching { Cid.parse(byteArrayOf(0x01, 0x55, 0x13, 0x40) + ByteArray(64)) }.isFailure)
|
||||
// sha2-256 with a short digest
|
||||
assertTrue(runCatching { Cid.parse(byteArrayOf(0x01, 0x55, 0x12, 0x10) + ByteArray(16)) }.isFailure)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an inlined block needs no fetch and is its own content`() {
|
||||
val inline = Cid.parse(byteArrayOf(0x01, 0x55, 0x00, 0x02, 0x68, 0x69))
|
||||
|
||||
assertEquals("hi", String(inline.inlineBlock!!))
|
||||
assertTrue(inline.matches("hi".toByteArray()))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package io.novafoundation.nova.runtime.names.content
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Test
|
||||
import java.math.BigInteger
|
||||
import java.time.Instant
|
||||
|
||||
/** Against records Kubo signed (`src/test/resources/kurd-ipns`, from ipfs/gateway-conformance). */
|
||||
class IpnsRecordTest {
|
||||
|
||||
private val now = Instant.parse("2026-11-05T00:00:00Z")
|
||||
|
||||
@Test
|
||||
fun `a V2 record and one carrying matching V1 copies verify`() {
|
||||
val v2 = verify("k51qzi5uqu5dit2ku9mutlfgwyz8u730on38kd10m97m36bjt66my99hb6103f_v2")!!
|
||||
assertEquals("bafkqadtwgiww63tmpeqhezldn5zgi", v2.root.toString())
|
||||
|
||||
assertEquals("bafkqaddwgevxmmraojswg33smq", verify("k51qzi5uqu5dlkw8pxuw9qmqayfdeh4kfebhmreauqdc6a7c3y7d5i9fi8mk9w_v1-v2")!!.root.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a broken legacy signature does not matter, the V2 one does`() {
|
||||
assertNotNull(verify("k51qzi5uqu5dilgf7gorsh9vcqqq4myo6jd4zmqkuy9pxyxi5fua3uf7axph4y_v1-v2-broken-signature-v1"))
|
||||
assertNull(verify("k51qzi5uqu5diamp7qnnvs1p1gzmku3eijkeijs3418j23j077zrkok63xdm8c_v1-v2-broken-signature-v2"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a V1-only record or one whose legacy value disagrees is refused`() {
|
||||
assertNull(verify("k51qzi5uqu5dm4tm0wt8srkg9h9suud4wuiwjimndrkydqm81cqtlb5ak6p7ku_v1"))
|
||||
assertNull(verify("k51qzi5uqu5dlmit2tuwdvnx4sbnyqgmvbxftl0eo3f33wwtb9gr7yozae9kpw_v1-v2-broken-v1-value"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `only the chain's publisher key is accepted`() {
|
||||
val record = resource("k51qzi5uqu5dit2ku9mutlfgwyz8u730on38kd10m97m36bjt66my99hb6103f_v2")
|
||||
val otherKey = keyOf("k51qzi5uqu5dlkw8pxuw9qmqayfdeh4kfebhmreauqdc6a7c3y7d5i9fi8mk9w")
|
||||
|
||||
assertNull(IpnsRecords.verify(record, otherKey, now))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an expired record is refused`() {
|
||||
val name = "k51qzi5uqu5dit2ku9mutlfgwyz8u730on38kd10m97m36bjt66my99hb6103f"
|
||||
|
||||
assertNull(IpnsRecords.verify(resource("${name}_v2"), keyOf(name), Instant.parse("2123-08-14T12:17:04Z")))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the name of a key round trips`() {
|
||||
val name = "k51qzi5uqu5dit2ku9mutlfgwyz8u730on38kd10m97m36bjt66my99hb6103f"
|
||||
|
||||
assertEquals(name, IpnsRecords.nameOf(keyOf(name)))
|
||||
}
|
||||
|
||||
private fun verify(file: String): IpnsRecord? = IpnsRecords.verify(resource(file), keyOf(file.substringBefore('_')), now)
|
||||
|
||||
private fun resource(file: String): ByteArray = IpnsRecordTest::class.java.getResourceAsStream("/kurd-ipns/$file.ipns-record")!!.use { it.readBytes() }
|
||||
|
||||
/** The ed25519 key inside a `k`-base36 IPNS name. */
|
||||
private fun keyOf(name: String): ByteArray {
|
||||
val bytes = BigInteger(name.substring(1), 36).toByteArray().dropWhile { it == 0.toByte() }.toByteArray()
|
||||
return bytes.copyOfRange(bytes.size - 32, bytes.size)
|
||||
}
|
||||
}
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
package io.novafoundation.nova.runtime.names.content
|
||||
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertSame
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class KurdContentFetcherTest {
|
||||
|
||||
private val root = cid("bafybeietjm63oynimmv5yyqay33nui4y4wx6u3peezwetxgiwvfmelutzu")
|
||||
private val car = resource("subdir-with-two-single-block-files.car")
|
||||
private val tampered = car.copyOf().also { it[it.size - 2] = (it[it.size - 2] + 1).toByte() }
|
||||
|
||||
@Test
|
||||
fun `a gateway that changed a block is skipped for the next one`() = runBlocking {
|
||||
val asked = mutableListOf<String>()
|
||||
val source = CarSource { gateway, _, _ ->
|
||||
asked += gateway
|
||||
if (gateway == "https://bad") tampered else car
|
||||
}
|
||||
|
||||
val result = KurdContentFetcher(gateways("https://bad", "https://good"), source).fetch(root, listOf("subdir", "hello.txt"))
|
||||
|
||||
assertEquals("hello world\n", String((result as KurdContentFetcher.Result.File).bytes))
|
||||
assertEquals(listOf("https://bad", "https://good"), asked)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `not found is believed only when the parent directory proves it`() = runBlocking {
|
||||
// Withholds the file although its directory lists it: not an answer, so the next gateway is asked
|
||||
val withholding = CarSource { gateway, _, path -> if (gateway == "https://withholding" && path.size == 2) null else car }
|
||||
|
||||
val result = KurdContentFetcher(gateways("https://withholding", "https://good"), withholding).fetch(root, listOf("subdir", "hello.txt"))
|
||||
assertTrue(result is KurdContentFetcher.Result.File)
|
||||
|
||||
val honest = CarSource { _, _, path -> if (path.size == 2) null else car }
|
||||
assertSame(KurdContentFetcher.Result.NotFound, KurdContentFetcher(gateways("https://one"), honest).fetch(root, listOf("subdir", "missing.txt")))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a directory is served through its index and without one is not found`() = runBlocking {
|
||||
val source = CarSource { _, _, path -> if (path.lastOrNull() == KurdContentFetcher.INDEX) null else car }
|
||||
|
||||
assertSame(KurdContentFetcher.Result.NotFound, KurdContentFetcher(gateways("https://one"), source).fetch(root, listOf("subdir")))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `no verifiable answer anywhere is unavailable`() = runBlocking {
|
||||
val result = KurdContentFetcher(gateways("https://a", "https://b"), CarSource { _, _, _ -> tampered }).fetch(root, listOf("subdir", "hello.txt"))
|
||||
|
||||
assertSame(KurdContentFetcher.Result.Unavailable, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `gateway entries are origins only`() {
|
||||
assertEquals("https://ipfs.io", RealKurdGateways.normalized("https://IPFS.io/"))
|
||||
assertEquals("https://mirror.example:8443", RealKurdGateways.normalized("https://mirror.example:8443"))
|
||||
listOf("http://ipfs.io", "https://ipfs.io/ipfs/", "https://ipfs.io?x=1", "https://user@ipfs.io", "ipfs.io").forEach {
|
||||
assertEquals(it, null, RealKurdGateways.normalized(it))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the built-in list stands when the signed one cannot be read`() = runBlocking {
|
||||
val failing = object : KurdGatewayListApi {
|
||||
override suspend fun gateways(url: String): GatewayListRemote = throw java.io.IOException("offline")
|
||||
}
|
||||
val signed = object : KurdGatewayListApi {
|
||||
override suspend fun gateways(url: String) = GatewayListRemote(
|
||||
listOf(GatewayListRemote.GatewayRemote("https://mirror.pezkuwi.example"), GatewayListRemote.GatewayRemote("http://plain"))
|
||||
)
|
||||
}
|
||||
|
||||
assertEquals(RealKurdGateways.BUILT_IN, RealKurdGateways(failing).list())
|
||||
assertEquals(listOf("https://mirror.pezkuwi.example"), RealKurdGateways(signed).list())
|
||||
}
|
||||
}
|
||||
+139
@@ -0,0 +1,139 @@
|
||||
package io.novafoundation.nova.runtime.names.content
|
||||
|
||||
import io.novafoundation.nova.runtime.names.NameRecord
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.bouncycastle.crypto.generators.Ed25519KeyPairGenerator
|
||||
import org.bouncycastle.crypto.params.Ed25519KeyGenerationParameters
|
||||
import org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters
|
||||
import org.bouncycastle.crypto.params.Ed25519PublicKeyParameters
|
||||
import org.bouncycastle.crypto.signers.Ed25519Signer
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import java.math.BigInteger
|
||||
import java.security.SecureRandom
|
||||
import java.time.Instant
|
||||
|
||||
class KurdFeedResolverTest {
|
||||
|
||||
private val keys = Ed25519KeyPairGenerator().apply { init(Ed25519KeyGenerationParameters(SecureRandom())) }.generateKeyPair()
|
||||
private val publisher = (keys.public as Ed25519PublicKeyParameters).encoded
|
||||
private val anchor = cid("bafkreifjjcie6lypi6ny7amxnfftagclbuxndqonfipmb64f2km2devei4")
|
||||
private val newer = cid("bafkreifkam6ns4aoolg3wedr4uzrs3kvq5m6wzyijeswb3htqhmc6itmi4")
|
||||
private val now = Instant.parse("2026-11-05T00:00:00Z")
|
||||
|
||||
@Test
|
||||
fun `a newer release a gateway has is served`() = runBlocking {
|
||||
val release = resolve(floor = 5, records = mapOf("https://a" to record(seq = 7, newer)))!!
|
||||
|
||||
assertEquals(newer, release.root)
|
||||
assertFalse(release.onChainVersion)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the newest of several gateways wins`() = runBlocking {
|
||||
val records = mapOf("https://a" to record(seq = 6, anchor), "https://b" to record(seq = 9, newer))
|
||||
|
||||
assertEquals(newer, resolve(floor = 5, records = records)!!.root)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a release older than the chain's anchor is never served`() = runBlocking {
|
||||
val release = resolve(floor = 5, records = mapOf("https://a" to record(seq = 4, newer)))!!
|
||||
|
||||
assertEquals(anchor, release.root)
|
||||
assertTrue(release.onChainVersion)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the anchored release itself is the chain's word`() = runBlocking {
|
||||
val release = resolve(floor = 5, records = mapOf("https://a" to record(seq = 5, newer)))!!
|
||||
|
||||
assertEquals(anchor, release.root)
|
||||
assertFalse(release.onChainVersion)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a record signed by another key is ignored`() = runBlocking {
|
||||
val stranger = Ed25519KeyPairGenerator().apply { init(Ed25519KeyGenerationParameters(SecureRandom())) }.generateKeyPair()
|
||||
val forged = record(seq = 99, newer, signer = stranger.private as Ed25519PrivateKeyParameters)
|
||||
|
||||
assertTrue(resolve(floor = 5, records = mapOf("https://a" to forged))!!.onChainVersion)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `no gateway answering leaves the chain's version`() = runBlocking {
|
||||
val release = resolve(floor = 5, records = emptyMap())!!
|
||||
|
||||
assertEquals(anchor, release.root)
|
||||
assertTrue(release.onChainVersion)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the feed is asked for by its publisher's IPNS name`() = runBlocking {
|
||||
val asked = mutableListOf<String>()
|
||||
KurdFeedResolver(gateways("https://a"), { _, name -> asked += name; null }, clock = { now }).resolve(feed(floor = 1))
|
||||
|
||||
assertEquals(listOf(IpnsRecords.nameOf(publisher)), asked)
|
||||
}
|
||||
|
||||
private suspend fun resolve(floor: Long, records: Map<String, ByteArray>): KurdFeedResolver.Release? {
|
||||
val source = IpnsRecordSource { gateway, _ -> records[gateway] }
|
||||
return KurdFeedResolver(gateways(*records.keys.ifEmpty { setOf("https://a") }.toTypedArray()), source, clock = { now }).resolve(feed(floor))
|
||||
}
|
||||
|
||||
private fun feed(floor: Long) = NameRecord.Content.Feed(
|
||||
publisher = publisher,
|
||||
anchorer = ByteArray(32),
|
||||
anchor = byteArrayOf(0xe3.toByte(), 0x01) + anchor.bytes,
|
||||
seq = BigInteger.valueOf(floor),
|
||||
)
|
||||
|
||||
/** A V2 IPNS record, built here only to exercise release selection; signing is tested against Kubo's records. */
|
||||
private fun record(seq: Long, root: Cid, signer: Ed25519PrivateKeyParameters = keys.private as Ed25519PrivateKeyParameters): ByteArray {
|
||||
val value = "/ipfs/$root".toByteArray()
|
||||
val validity = "2030-01-01T00:00:00Z".toByteArray()
|
||||
val data = cborMap(
|
||||
"Sequence" to cborUint(seq),
|
||||
"TTL" to cborUint(0),
|
||||
"Validity" to cborBytes(validity),
|
||||
"ValidityType" to cborUint(0),
|
||||
"Value" to cborBytes(value),
|
||||
)
|
||||
val signature = Ed25519Signer().run {
|
||||
init(true, signer)
|
||||
val message = "ipns-signature:".toByteArray() + data
|
||||
update(message, 0, message.size)
|
||||
generateSignature()
|
||||
}
|
||||
return protobufBytes(8, signature) + protobufBytes(9, data)
|
||||
}
|
||||
|
||||
private fun protobufBytes(field: Int, bytes: ByteArray) = byteArrayOf(((field shl 3) or 2).toByte()) + varint(bytes.size.toLong()) + bytes
|
||||
|
||||
private fun varint(value: Long): ByteArray {
|
||||
val out = java.io.ByteArrayOutputStream()
|
||||
var rest = value
|
||||
while (rest >= 0x80) {
|
||||
out.write(((rest and 0x7f) or 0x80).toInt())
|
||||
rest = rest ushr 7
|
||||
}
|
||||
out.write(rest.toInt())
|
||||
return out.toByteArray()
|
||||
}
|
||||
|
||||
private fun cborHead(major: Int, value: Long): ByteArray = when {
|
||||
value < 24 -> byteArrayOf(((major shl 5) or value.toInt()).toByte())
|
||||
value < 256 -> byteArrayOf(((major shl 5) or 24).toByte(), value.toByte())
|
||||
else -> byteArrayOf(((major shl 5) or 27).toByte()) + ByteArray(8) { (value ushr (56 - 8 * it)).toByte() }
|
||||
}
|
||||
|
||||
private fun cborUint(value: Long) = cborHead(0, value)
|
||||
|
||||
private fun cborBytes(bytes: ByteArray) = cborHead(2, bytes.size.toLong()) + bytes
|
||||
|
||||
private fun cborMap(vararg entries: Pair<String, ByteArray>): ByteArray {
|
||||
return entries.fold(cborHead(5, entries.size.toLong())) { acc, (key, value) -> acc + cborHead(3, key.length.toLong()) + key.toByteArray() + value }
|
||||
}
|
||||
}
|
||||
+158
@@ -0,0 +1,158 @@
|
||||
package io.novafoundation.nova.runtime.names.content
|
||||
|
||||
import io.novafoundation.nova.runtime.names.KurdName
|
||||
import io.novafoundation.nova.runtime.names.KurdNameResolver
|
||||
import io.novafoundation.nova.runtime.names.KurdResolution
|
||||
import io.novafoundation.nova.runtime.names.NameRecord
|
||||
import io.novafoundation.nova.runtime.names.NameStatus
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertSame
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class KurdSiteServerTest {
|
||||
|
||||
private val siteRoot = cid("bafybeietjm63oynimmv5yyqay33nui4y4wx6u3peezwetxgiwvfmelutzu")
|
||||
private val siteCar = resource("subdir-with-two-single-block-files.car")
|
||||
|
||||
@Test
|
||||
fun `a file of a resolved name is served with the wallet's policy`() = runBlocking {
|
||||
val response = server(resolved(ipfs(siteRoot))).serve("dks.kurd", "/subdir/hello.txt")
|
||||
|
||||
assertEquals(200, response.status)
|
||||
assertEquals("text/plain", response.mimeType)
|
||||
assertEquals("hello world\n", String(response.body))
|
||||
assertEquals(KurdSiteServer.CONTENT_SECURITY_POLICY, response.headers["Content-Security-Policy"])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `each resolution outcome has its status`() = runBlocking {
|
||||
val name = KurdName.parse("dks.kurd")!!
|
||||
|
||||
assertEquals(404, server { KurdResolution.NotFound(it, verified = true) }.serve("dks.kurd", "/").status)
|
||||
assertEquals(403, server { KurdResolution.Refused(it) }.serve("dks.kurd", "/").status)
|
||||
assertEquals(502, server { KurdResolution.Unavailable(it) }.serve("dks.kurd", "/").status)
|
||||
assertEquals(404, server(resolved(content = null)).serve(name.name, "/").status)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `content the server cannot verify is not served`() = runBlocking {
|
||||
val ipns = NameRecord.Content.Ipfs(byteArrayOf(0xe5.toByte(), 0x01) + siteRoot.bytes)
|
||||
val feed = NameRecord.Content.Feed(ByteArray(32), ByteArray(32), siteRoot.bytes, java.math.BigInteger.ONE)
|
||||
|
||||
assertEquals(415, server(resolved(ipns)).serve("dks.kurd", "/").status)
|
||||
assertEquals(415, server(resolved(feed)).serve("dks.kurd", "/").status)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an invalid name or path is refused before any lookup`() = runBlocking {
|
||||
val server = server { error("must not resolve") }
|
||||
|
||||
assertEquals(404, server.serve("uni-x--edu.kurd", "/").status)
|
||||
assertEquals(400, server.serve("dks.kurd", "/%2e%2e/secret").status)
|
||||
assertEquals(400, server.serve("dks.kurd", "/a%2Fb").status)
|
||||
assertEquals(400, server.serve("dks.kurd", "/%zz").status)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a name is resolved once while its resolution is fresh`() = runBlocking {
|
||||
var calls = 0
|
||||
var now = 0L
|
||||
val server = KurdSiteServer(
|
||||
resolver = resolverOf { calls++; resolved(ipfs(siteRoot))(it) },
|
||||
fetcher = fetcher(CarSource { _, _, _ -> siteCar }),
|
||||
resolutionTtlMillis = 1_000,
|
||||
clock = { now },
|
||||
)
|
||||
|
||||
server.serve("dks.kurd", "/subdir/hello.txt")
|
||||
server.serve("dks.kurd", "/subdir/ascii.txt")
|
||||
assertEquals(1, calls)
|
||||
|
||||
now = 1_000
|
||||
server.serve("dks.kurd", "/subdir/hello.txt")
|
||||
assertEquals(2, calls)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a name read through RPC only waits for the reader's choice`() = runBlocking {
|
||||
val server = server(resolved(ipfs(siteRoot), verified = false))
|
||||
val name = KurdName.parse("ahmet.kurd")!!
|
||||
|
||||
assertEquals(428, server.serve("ahmet.kurd", "/subdir/hello.txt", mainFrame = true).status)
|
||||
assertTrue(server.pageStates.value[name] is KurdSiteServer.PageState.NeedsConsent)
|
||||
|
||||
server.allowUnverified(name)
|
||||
|
||||
assertEquals(200, server.serve("ahmet.kurd", "/subdir/hello.txt", mainFrame = true).status)
|
||||
assertTrue(server.pageStates.value[name] is KurdSiteServer.PageState.Open)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `only the page's own document sets its state`() = runBlocking {
|
||||
val server = server(resolved(ipfs(siteRoot)))
|
||||
val name = KurdName.parse("dks.kurd")!!
|
||||
|
||||
server.serve("dks.kurd", "/subdir/hello.txt", mainFrame = false)
|
||||
assertNull(server.pageStates.value[name])
|
||||
|
||||
server.serve("dks.kurd", "/subdir/hello.txt", mainFrame = true)
|
||||
assertEquals(siteRoot, (server.pageStates.value[name] as KurdSiteServer.PageState.Open).root)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `each outcome sets the page state the browser shows`() = runBlocking {
|
||||
val name = KurdName.parse("dks.kurd")!!
|
||||
suspend fun stateFor(resolve: (KurdName) -> KurdResolution): KurdSiteServer.PageState? {
|
||||
val server = server(resolve)
|
||||
server.serve("dks.kurd", "/", mainFrame = true)
|
||||
return server.pageStates.value[name]
|
||||
}
|
||||
|
||||
assertSame(KurdSiteServer.PageState.Refused, stateFor { KurdResolution.Refused(it) })
|
||||
assertSame(KurdSiteServer.PageState.NotRegistered, stateFor { KurdResolution.NotFound(it, verified = true) })
|
||||
assertSame(KurdSiteServer.PageState.Unavailable, stateFor { KurdResolution.Unavailable(it) })
|
||||
assertSame(KurdSiteServer.PageState.NoSite, stateFor(resolved(content = null)))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `paths decode per segment and only as UTF-8`() {
|
||||
assertEquals(listOf("a b", "ç.html"), KurdSiteServer.decodePath("/a%20b/%C3%A7.html?x=1#top"))
|
||||
assertEquals(emptyList<String>(), KurdSiteServer.decodePath("/"))
|
||||
assertNull(KurdSiteServer.decodePath("/%C3"))
|
||||
assertNull(KurdSiteServer.decodePath("/%+f"))
|
||||
assertNull(KurdSiteServer.decodePath("/%٣٣"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `types follow the extension`() {
|
||||
assertEquals("text/html", KurdSiteServer.mimeTypeOf("index.HTML"))
|
||||
assertEquals("text/javascript", KurdSiteServer.mimeTypeOf("app.mjs"))
|
||||
assertEquals("application/octet-stream", KurdSiteServer.mimeTypeOf("README"))
|
||||
assertEquals("application/octet-stream", KurdSiteServer.mimeTypeOf(null))
|
||||
}
|
||||
|
||||
private fun server(resolve: (KurdName) -> KurdResolution) = KurdSiteServer(resolverOf(resolve), fetcher(CarSource { _, _, _ -> siteCar }))
|
||||
|
||||
private fun resolverOf(resolve: (KurdName) -> KurdResolution) = object : KurdNameResolver {
|
||||
override suspend fun resolve(name: KurdName) = resolve(name)
|
||||
}
|
||||
|
||||
private fun fetcher(source: CarSource) = KurdContentFetcher(gateways("https://one"), source)
|
||||
|
||||
private fun ipfs(cid: Cid) = NameRecord.Content.Ipfs(byteArrayOf(0xe3.toByte(), 0x01) + cid.bytes)
|
||||
|
||||
private fun resolved(content: NameRecord.Content?, verified: Boolean = true): (KurdName) -> KurdResolution = { name ->
|
||||
val record = NameRecord(
|
||||
label = name.labels.first(), parent = KurdName.KURD_NODE, parentReg = 0, reg = 1, owner = NameRecord.Owner.Governance,
|
||||
kind = NameRecord.Kind.GRANTED, lockedTransfer = true, expiry = null, account = null, content = content,
|
||||
)
|
||||
KurdResolution.Resolved(name, record, NameStatus.ACTIVE, verified = verified)
|
||||
}
|
||||
}
|
||||
|
||||
internal fun gateways(vararg urls: String) = object : KurdGateways {
|
||||
override suspend fun list() = urls.toList()
|
||||
}
|
||||
+139
@@ -0,0 +1,139 @@
|
||||
package io.novafoundation.nova.runtime.names.content
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
/** Against CARs Kubo produced (`src/test/resources/kurd-content`, from ipfs/gateway-conformance). */
|
||||
class UnixFsReaderTest {
|
||||
|
||||
@Test
|
||||
fun `files in a nested directory are read`() {
|
||||
val reader = reader("subdir-with-two-single-block-files.car")
|
||||
val root = cid("bafybeietjm63oynimmv5yyqay33nui4y4wx6u3peezwetxgiwvfmelutzu")
|
||||
|
||||
assertEquals("hello world\n", text(reader.read(root, listOf("subdir", "hello.txt"))))
|
||||
assertEquals("hello application/vnd.ipld.car\n", text(reader.read(root, listOf("subdir", "ascii.txt"))))
|
||||
assertTrue(reader.read(root, listOf("subdir")) is UnixFsReader.Entry.Directory)
|
||||
assertTrue(reader.read(root, emptyList()) is UnixFsReader.Entry.Directory)
|
||||
assertNull(reader.read(root, listOf("subdir", "missing.txt")))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a file chunked into several blocks is assembled in order`() {
|
||||
val reader = reader("dir-with-files.car")
|
||||
val root = cid("bafybeihchr7vmgjaasntayyatmp5sv6xza57iy2h4xj7g46bpjij6yhrmy")
|
||||
|
||||
val text = text(reader.read(root, listOf("multiblock.txt")))
|
||||
|
||||
assertEquals(1026, text.length)
|
||||
assertTrue(text.startsWith("Lorem ipsum dolor sit amet, consectetur adipiscing elit."))
|
||||
assertTrue(text.endsWith("Pellentesque habitant amet."))
|
||||
assertEquals(text(reader.read(root, listOf("ascii.txt"))), text(reader.read(root, listOf("ascii-copy.txt"))))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a sharded directory is looked up by the name's hash`() {
|
||||
val reader = reader("single-layer-hamt-with-multi-block-files.car")
|
||||
val root = cid("bafybeidbclfqleg2uojchspzd4bob56dqetqjsj27gy2cq3klkkgxtpn4i")
|
||||
|
||||
listOf("470.txt", "742.txt", "448.txt", "499.txt").forEach { name ->
|
||||
assertTrue(name, text(reader.read(root, listOf(name))).startsWith("Lorem ipsum"))
|
||||
}
|
||||
assertNull(reader.read(root, listOf("6E470.txt")))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `node fields in either order and shards with one entry read the same`() {
|
||||
val reader = reader("pbnode-field-orders.car")
|
||||
|
||||
listOf(
|
||||
"bafybeigqvyloizmfcdy6scaxnyltftzptaruqa3hnnplfzsbf4sqteiwlm",
|
||||
"bafybeigdcg7pksx2zk5336vrfsktjodlr4rbfz37qr3koc5xboxe5ekv24",
|
||||
"bafybeicwgy2rlqmqqu3yy2tqvm2wbgdvy3snu4sbbv4wqpvpnoplpzxz74",
|
||||
"bafybeicjwkfslu7gwyywffvqgse5kiibojtktxcdqhgv7ldj5fjdacuceq",
|
||||
).forEach { root -> assertEquals(root, "hello\n", text(reader.read(cid(root), listOf("hello.txt")))) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a file with a block no mirror sent is not served`() {
|
||||
val reader = reader("file-3k-and-3-blocks-missing-block.car")
|
||||
val root = Cid.parse(hex("122099fd9f8119c50b421e8e87d7047f6bb7cc4d4d5cfecea65813fb4bfef5049b79"))
|
||||
|
||||
assertTrue(runCatching { reader.read(root, emptyList()) }.exceptionOrNull() is ContentVerificationException)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a CAR with any block changed is refused whole`() {
|
||||
val car = resource("subdir-with-two-single-block-files.car")
|
||||
val tampered = car.copyOf().also { it[it.size - 2] = (it[it.size - 2] + 1).toByte() } // inside "hello world\n"
|
||||
|
||||
assertTrue(runCatching { VerifiedBlocks.fromCar(tampered) }.exceptionOrNull() is ContentVerificationException)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a truncated CAR is refused`() {
|
||||
val car = resource("subdir-with-two-single-block-files.car")
|
||||
|
||||
assertTrue(runCatching { VerifiedBlocks.fromCar(car.copyOf(car.size - 3)) }.exceptionOrNull() is ContentVerificationException)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a file larger than the limit is not assembled`() {
|
||||
val reader = UnixFsReader(VerifiedBlocks.fromCar(resource("dir-with-files.car")), maxFileSize = 1000)
|
||||
val root = cid("bafybeihchr7vmgjaasntayyatmp5sv6xza57iy2h4xj7g46bpjij6yhrmy")
|
||||
|
||||
assertTrue(runCatching { reader.read(root, listOf("multiblock.txt")) }.exceptionOrNull() is ContentVerificationException)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `path segments that could escape are refused`() {
|
||||
val reader = reader("subdir-with-two-single-block-files.car")
|
||||
val root = cid("bafybeietjm63oynimmv5yyqay33nui4y4wx6u3peezwetxgiwvfmelutzu")
|
||||
|
||||
listOf("..", ".", "", "subdir/hello.txt").forEach { segment ->
|
||||
assertTrue(segment, runCatching { reader.read(root, listOf(segment)) }.exceptionOrNull() is ContentVerificationException)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `murmur3 matches the shard prefix Kubo gave hello txt`() {
|
||||
assertEquals(0xDF, (UnixFsReader.murmur3x64_64("hello.txt".toByteArray()) ushr 56).toInt())
|
||||
}
|
||||
|
||||
// Guava's Murmur3_128 vector: x64_128 = 6c1b07bc7bbc4be3 47939ac4a93c437a, h1 written little-endian.
|
||||
// 43 bytes, so both 16-byte blocks and both tail halves are exercised.
|
||||
@Test
|
||||
fun `murmur3 matches the reference vector over blocks and tail`() {
|
||||
val h1 = UnixFsReader.murmur3x64_64("The quick brown fox jumps over the lazy dog".toByteArray())
|
||||
|
||||
assertEquals("e34bbc7bbc071b6c", java.lang.Long.toHexString(h1))
|
||||
}
|
||||
|
||||
private fun reader(name: String) = UnixFsReader(VerifiedBlocks.fromCar(resource(name)))
|
||||
|
||||
private fun text(entry: UnixFsReader.Entry?) = String((entry as UnixFsReader.Entry.File).bytes, Charsets.UTF_8)
|
||||
}
|
||||
|
||||
internal fun resource(name: String): ByteArray = UnixFsReaderTest::class.java.getResourceAsStream("/kurd-content/$name")!!.use { it.readBytes() }
|
||||
|
||||
internal fun hex(value: String): ByteArray = ByteArray(value.length / 2) { value.substring(2 * it, 2 * it + 2).toInt(16).toByte() }
|
||||
|
||||
/** Multibase base32 (`b…`) to a CID, for writing fixtures' CIDs as they are published. */
|
||||
internal fun cid(value: String): Cid {
|
||||
require(value.startsWith("b"))
|
||||
val alphabet = "abcdefghijklmnopqrstuvwxyz234567"
|
||||
val out = java.io.ByteArrayOutputStream()
|
||||
var buffer = 0
|
||||
var bits = 0
|
||||
for (char in value.substring(1)) {
|
||||
buffer = (buffer shl 5) or alphabet.indexOf(char).also { require(it >= 0) }
|
||||
bits += 5
|
||||
if (bits >= 8) {
|
||||
out.write((buffer shr (bits - 8)) and 0xff)
|
||||
bits -= 8
|
||||
}
|
||||
}
|
||||
return Cid.parse(out.toByteArray())
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
CAR fixtures from [ipfs/gateway-conformance](https://github.com/ipfs/gateway-conformance)
|
||||
at commit `eaca278518dc977db10bfec4acd79c92c4ee11ac` (`fixtures/trustless_gateway_car/` and
|
||||
`fixtures/path_gateway_unixfs/`), produced by Kubo. Dual licensed MIT / Apache-2.0 by their authors.
|
||||
Unmodified; used to check the wallet's CAR and UnixFS verifier against the reference implementation.
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,4 @@
|
||||
IPNS record fixtures from [ipfs/gateway-conformance](https://github.com/ipfs/gateway-conformance)
|
||||
at commit `eaca278518dc977db10bfec4acd79c92c4ee11ac` (`fixtures/ipns_records/`), produced by Kubo's
|
||||
record library. Dual licensed MIT / Apache-2.0 by their authors. Unmodified; each file is named by
|
||||
the IPNS name of the key that signed it.
|
||||
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
Reference in new issue
Block a user