<div id="app">
  <div>
    <h1>Addressing by CID with Helia and dag-cbor</h1>
    <div class="card">
      <textarea id="user-input" name="user-input">{ "hello": "world" }</textarea>
      <br />
      <button id="get-cid">Get CID</button>
    </div>
    
    <p id="output" class="output"></p>
  </div>
</div>
import { createHeliaHTTP } from 'https://esm.sh/@helia/http?bundle-deps'
import { dagCbor } from 'https://esm.sh/@helia/dag-cbor?bundle-deps'

const output = document.getElementById('output')
const input = document.getElementById('user-input')
const button = document.getElementById('get-cid')
const helia = await createHeliaHTTP()
const dcbor = dagCbor(helia)
    globalThis.helia = helia
button.addEventListener('click', handleGetCid)

async function handleGetCid(event) {
  try {
    const object = JSON.parse(input.value)
    
    // Encode the file with dag-cbor
    const cid = await dcbor.add(object)
    
    // Display the CID
    output.innerHTML = `Object addressed by CID: <a href="https://cid.ipfs.tech/#${cid.toString()}">${cid.toString()}</a>`
    
  } catch (err) {
    console.error(err)
    document.getElementById('output').textContent = 
      `Error: ${err.message}`
  }
}

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.