Skip to content
Snippets Groups Projects
Commit 3b400f34 authored by Hugo Trentesaux's avatar Hugo Trentesaux
Browse files

improve ipns view

parent c47c5710
Branches
No related tags found
No related merge requests found
...@@ -14,4 +14,4 @@ export const KEYSIZE = (64 * Math.log(2)) / Math.log(BASE) ...@@ -14,4 +14,4 @@ export const KEYSIZE = (64 * Math.log(2)) / Math.log(BASE)
export const EMPTY_NODE_CID = CID.parse('bafyreicvlp2p65agkxpzcboedba7zit55us4zvtyyq2wesvsdedy6irwfy') export const EMPTY_NODE_CID = CID.parse('bafyreicvlp2p65agkxpzcboedba7zit55us4zvtyyq2wesvsdedy6irwfy')
// document kind of old cesium plus profile imported in the indexer // document kind of old cesium plus profile imported in the indexer
export const CESIUM_PLUS_PROFILE_IMPORT = CID.parse('/ipfs/bafkreiawtammeqc55cssr2zepfpaxbmp7kquhikkagipvtefeadsw4mqvq') export const CESIUM_PLUS_PROFILE_IMPORT = CID.parse('bafkreiawtammeqc55cssr2zepfpaxbmp7kquhikkagipvtefeadsw4mqvq')
<script setup lang="ts"> <script setup lang="ts">
import { kubo } from '@/kubo' import { kubo } from '@/kubo'
import { emptyInode, type IndexHist } from '../types' import { emptyInode, type IndexHist } from '../types'
import { IPNS, IPNS_HIST } from '../consts' import { IPNS, IPNS_HIST, EMPTY_NODE_CID } from '../consts'
import { CID } from 'multiformats' import { CID } from 'multiformats'
import { ref, type Ref, computed, onMounted } from 'vue' import { ref, type Ref, computed, onMounted } from 'vue'
const msg = ref('') const targetMsg = ref('')
const rootnodeipns = ref('') const targetCID: Ref<CID | null> = ref(null)
const historyipns = ref('') const histCID: Ref<CID | null> = ref(null)
const ipnsTarget = ref('')
const targetCid: Ref<CID | null> = ref(null)
const histCid: Ref<CID | null> = ref(null)
const isValid = computed(() => { const isValid = computed(() => {
try { try {
CID.parse(msg.value) CID.parse(targetMsg.value)
return true return true
} catch { } catch {
return false return false
...@@ -23,99 +20,87 @@ const isValid = computed(() => { ...@@ -23,99 +20,87 @@ const isValid = computed(() => {
// inits indexing and publishes ipns entry for it // inits indexing and publishes ipns entry for it
async function initIndex() { async function initIndex() {
// initialize index to empty inode targetCID.value = EMPTY_NODE_CID
const rootnode = emptyInode()
const rootcid = await kubo.dag.put(rootnode)
// publish result to ipns // publish result to ipns
const res = await kubo.name.publish(rootcid, { ttl: '1s' }) kubo.name.publish(EMPTY_NODE_CID, { ttl: '1s' }).then(console.log)
console.log(res) // track it on the history
rootnodeipns.value = res.name updateHist(EMPTY_NODE_CID)
// resolve again to confirm publish result
resolve()
// update history accordingly
updateHist(rootcid as CID)
} }
// inits indexing and publishes ipns entry for it // inits indexing and publishes ipns entry for it
async function initIndexHist() { async function initIndexHist() {
const firstHist: IndexHist = { const firstHist: IndexHist = {
last_history: null, last_history: null,
current_index: targetCid.value!, current_index: targetCID.value!,
number: 0, number: 0,
timestamp: Date.now() timestamp: Date.now()
} }
const histcid = await kubo.dag.put(firstHist) const firstHistCID = await kubo.dag.put(firstHist)
const res = await kubo.name.publish(histcid, { ttl: '1s', key: 'index_history' }) kubo.name.publish(firstHistCID, { ttl: '1s', key: 'index_history' }).then(console.log)
console.log(res)
historyipns.value = res.name
} }
async function resolve() { // resolve given ipns
for await (const name of kubo.name.resolve(IPNS, { nocache: true })) { async function resolveIPNS(ipns: string): Promise<CID> {
ipnsTarget.value = name
targetCid.value = CID.parse(name.slice(6))
}
}
async function resolveHist(): Promise<CID> {
let cid = null let cid = null
for await (const name of kubo.name.resolve(IPNS_HIST, { nocache: true })) { for await (const name of kubo.name.resolve(ipns, { nocache: true })) {
cid = CID.parse(name.slice(6)) cid = CID.parse(name.slice(6))
histCid.value = cid
} }
return cid! return cid!
} }
// update history chain with new cid
async function updateHist(cid: CID) { async function updateHist(cid: CID) {
const lastHist = await kubo.dag.get(cid) const lastHistCID = await resolveIPNS(IPNS_HIST)
const hist: IndexHist = { const lastHist = (await kubo.dag.get(lastHistCID)).value
last_history: await resolveHist(), const newHist: IndexHist = {
last_history: lastHistCID,
current_index: cid, current_index: cid,
number: lastHist.value.number + 1, number: lastHist.number + 1,
timestamp: Date.now() timestamp: Date.now()
} }
const histcid = await kubo.dag.put(hist) const newHistCID = await kubo.dag.put(newHist)
const res = await kubo.name.publish(histcid, { ttl: '1s', key: 'index_history' }) kubo.name.publish(newHistCID, { ttl: '1s', key: 'index_history' })
} }
// publish cid to IPNS
function setTargetCid() { function setTargetCid() {
if (isValid.value) { if (isValid.value) {
const cid = CID.parse(msg.value) const cid = CID.parse(targetMsg.value)
targetCid.value = cid targetCID.value = cid
kubo.name.publish(cid, { ttl: '1s' }).then((_) => resolve()) kubo.name.publish(cid, { ttl: '1s' })
} else { } else {
targetCid.value = null targetCID.value = null
} }
} }
onMounted(() => { onMounted(() => {
resolve() resolveIPNS(IPNS).then((c) => (targetCID.value = c))
resolveHist() resolveIPNS(IPNS_HIST).then((c) => (histCID.value = c))
}) })
</script> </script>
<template> <template>
<div> <div>
<h1>IPNS (TODO improve)</h1> <h1>IPNS</h1>
<p>Info about IPNS entries</p> <p>Info about IPNS entries</p>
<p>initialize empty index: <button @click="initIndex">reinitialize ⚠️</button></p> <h2>Index root</h2>
<p> <p>Root node IPNS</p>
root node ipns: <span class="mono">/ipns/{{ rootnodeipns }}</span>
</p>
<p>You can use it to configure root node instead of hardcoded one:</p>
<p class="mono">{{ IPNS }}</p> <p class="mono">{{ IPNS }}</p>
<p>which is pointing to <button @click="resolve">resolve</button></p> <p>is pointing to</p>
<p class="mono">{{ ipnsTarget }}</p> <p class="mono">{{ targetCID }}</p>
<p>initialize empty index: <button @click="initIndex">reinitialize ⚠️</button></p>
<p> <p>
set IPNS target cid or set to custom CID <br />
<input v-model="msg" @keyup.enter="setTargetCid" /> <input v-model="targetMsg" @keyup.enter="setTargetCid" size="50" />
<button v-on:click="setTargetCid">Set ⚠️</button> <button v-on:click="setTargetCid">Set ⚠️</button>
</p> </p>
<h2>Index history</h2> <h2>Index history</h2>
<p> <p>History IPNS</p>
initialize <button @click="initIndexHist">reinitialize history ⚠️</button> <p class="mono">{{ IPNS_HIST }}</p>
<span class="mono">&nbsp;/ipns/{{ historyipns }}</span> <p>is pointing to</p>
</p> <p class="mono">{{ histCID }}</p>
<p>reinitialize history to current target cid <button @click="initIndexHist">reinitialize ⚠️</button></p>
<p>and got to <RouterLink to="/index">index</RouterLink> to see more about index history</p>
</div> </div>
</template> </template>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment