diff --git a/src/consts.ts b/src/consts.ts index f7d9dd75bc0f9be76bc783e2ffe1b688c0a0a364..b7deec7af52433cf431a75e3e1c1421844756b15 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -14,4 +14,4 @@ export const KEYSIZE = (64 * Math.log(2)) / Math.log(BASE) export const EMPTY_NODE_CID = CID.parse('bafyreicvlp2p65agkxpzcboedba7zit55us4zvtyyq2wesvsdedy6irwfy') // 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') diff --git a/src/views/IpnsView.vue b/src/views/IpnsView.vue index 49e17da8b982d7e50719946ff36596dc96683f08..8ae2b416b7a98e941bc82bf8a16ed3d24053ce24 100644 --- a/src/views/IpnsView.vue +++ b/src/views/IpnsView.vue @@ -1,20 +1,17 @@ <script setup lang="ts"> import { kubo } from '@/kubo' 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 { ref, type Ref, computed, onMounted } from 'vue' -const msg = ref('') -const rootnodeipns = ref('') -const historyipns = ref('') -const ipnsTarget = ref('') -const targetCid: Ref<CID | null> = ref(null) -const histCid: Ref<CID | null> = ref(null) +const targetMsg = ref('') +const targetCID: Ref<CID | null> = ref(null) +const histCID: Ref<CID | null> = ref(null) const isValid = computed(() => { try { - CID.parse(msg.value) + CID.parse(targetMsg.value) return true } catch { return false @@ -23,99 +20,87 @@ const isValid = computed(() => { // inits indexing and publishes ipns entry for it async function initIndex() { - // initialize index to empty inode - const rootnode = emptyInode() - const rootcid = await kubo.dag.put(rootnode) + targetCID.value = EMPTY_NODE_CID // publish result to ipns - const res = await kubo.name.publish(rootcid, { ttl: '1s' }) - console.log(res) - rootnodeipns.value = res.name - // resolve again to confirm publish result - resolve() - // update history accordingly - updateHist(rootcid as CID) + kubo.name.publish(EMPTY_NODE_CID, { ttl: '1s' }).then(console.log) + // track it on the history + updateHist(EMPTY_NODE_CID) } // inits indexing and publishes ipns entry for it async function initIndexHist() { const firstHist: IndexHist = { last_history: null, - current_index: targetCid.value!, + current_index: targetCID.value!, number: 0, timestamp: Date.now() } - const histcid = await kubo.dag.put(firstHist) - const res = await kubo.name.publish(histcid, { ttl: '1s', key: 'index_history' }) - console.log(res) - historyipns.value = res.name + const firstHistCID = await kubo.dag.put(firstHist) + kubo.name.publish(firstHistCID, { ttl: '1s', key: 'index_history' }).then(console.log) } -async function resolve() { - for await (const name of kubo.name.resolve(IPNS, { nocache: true })) { - ipnsTarget.value = name - targetCid.value = CID.parse(name.slice(6)) - } -} - -async function resolveHist(): Promise<CID> { +// resolve given ipns +async function resolveIPNS(ipns: string): Promise<CID> { 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)) - histCid.value = cid } return cid! } +// update history chain with new cid async function updateHist(cid: CID) { - const lastHist = await kubo.dag.get(cid) - const hist: IndexHist = { - last_history: await resolveHist(), + const lastHistCID = await resolveIPNS(IPNS_HIST) + const lastHist = (await kubo.dag.get(lastHistCID)).value + const newHist: IndexHist = { + last_history: lastHistCID, current_index: cid, - number: lastHist.value.number + 1, + number: lastHist.number + 1, timestamp: Date.now() } - const histcid = await kubo.dag.put(hist) - const res = await kubo.name.publish(histcid, { ttl: '1s', key: 'index_history' }) + const newHistCID = await kubo.dag.put(newHist) + kubo.name.publish(newHistCID, { ttl: '1s', key: 'index_history' }) } +// publish cid to IPNS function setTargetCid() { if (isValid.value) { - const cid = CID.parse(msg.value) - targetCid.value = cid - kubo.name.publish(cid, { ttl: '1s' }).then((_) => resolve()) + const cid = CID.parse(targetMsg.value) + targetCID.value = cid + kubo.name.publish(cid, { ttl: '1s' }) } else { - targetCid.value = null + targetCID.value = null } } onMounted(() => { - resolve() - resolveHist() + resolveIPNS(IPNS).then((c) => (targetCID.value = c)) + resolveIPNS(IPNS_HIST).then((c) => (histCID.value = c)) }) </script> <template> <div> - <h1>IPNS (TODO improve)</h1> + <h1>IPNS</h1> <p>Info about IPNS entries</p> - <p>initialize empty index: <button @click="initIndex">reinitialize ⚠️</button></p> - <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> + <h2>Index root</h2> + <p>Root node IPNS</p> <p class="mono">{{ IPNS }}</p> - <p>which is pointing to <button @click="resolve">resolve</button></p> - <p class="mono">{{ ipnsTarget }}</p> + <p>is pointing to</p> + <p class="mono">{{ targetCID }}</p> + <p>initialize empty index: <button @click="initIndex">reinitialize ⚠️</button></p> <p> - set IPNS target cid - <input v-model="msg" @keyup.enter="setTargetCid" /> + or set to custom CID <br /> + <input v-model="targetMsg" @keyup.enter="setTargetCid" size="50" /> <button v-on:click="setTargetCid">Set ⚠️</button> </p> <h2>Index history</h2> - <p> - initialize <button @click="initIndexHist">reinitialize history ⚠️</button> - <span class="mono"> /ipns/{{ historyipns }}</span> - </p> + <p>History IPNS</p> + <p class="mono">{{ IPNS_HIST }}</p> + <p>is pointing to</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> </template>