Skip to content

Instantly share code, notes, and snippets.

@simonw
Created January 6, 2025 17:57
Show Gist options
  • Save simonw/ab7280cc31a92aab3f68e6ad69cfd17a to your computer and use it in GitHub Desktop.
Save simonw/ab7280cc31a92aab3f68e6ad69cfd17a to your computer and use it in GitHub Desktop.

Bluesky Handle to DID Lookup

human (Jan 6, 2025, 08:47 AM)

let me input a bluesky handle and show me the DID

assistant (Jan 6, 2025, 08:48 AM)

create Bluesky DID resolver

<!DOCTYPE html>
<html>
<head>
  <style>
  * {
    box-sizing: border-box;
  }

  body {
    font-family: Helvetica, Arial, sans-serif;
    max-width: 800px;
    margin: 20px auto;
    padding: 0 20px;
  }

  input {
    display: block;
    width: 100%;
    padding: 8px;
    margin: 8px 0;
    font-size: 16px;
    border: 1px solid #ccc;
    border-radius: 4px;
  }

  button {
    background: #0070ff;
    color: white;
    border: none;
    padding: 8px 16px;
    border-radius: 4px;
    cursor: pointer;
  }

  button:hover {
    background: #0060df;
  }

  #result {
    margin-top: 20px;
    padding: 16px;
    background: #f5f5f5;
    border-radius: 4px;
    word-break: break-all;
  }

  .error {
    color: #d00;
  }
  </style>
</head>
<body>
  <h1>Resolve Bluesky handle to DID</h1>
  
  <input type="text" id="handle" placeholder="Enter Bluesky handle (e.g. jay.bsky.social)">
  <button onclick="resolveDid()">Resolve</button>
  
  <div id="result"></div>

<script type="module">
const API_URL = 'https://plc.directory'
const result = document.getElementById('result')
const handleInput = document.getElementById('handle')

async function resolveDid() {
  const handle = handleInput.value.trim()
  if (!handle) {
    result.innerHTML = '<span class="error">Please enter a handle</span>'
    return
  }

  try {
    const response = await fetch(`${API_URL}/${handle}`)
    if (!response.ok) throw new Error('Handle not found')
    const data = await response.json()
    result.textContent = data.did
  } catch (err) {
    result.innerHTML = `<span class="error">Error: ${err.message}</span>`
  }
}

// Allow Enter key to submit
handleInput.addEventListener('keypress', (e) => {
  if (e.key === 'Enter') resolveDid()
})
</script>
</body>
</html>

Enter a Bluesky handle to see their DID. Press Enter or click Resolve.

human (Jan 6, 2025, 09:51 AM)

use com.atproto.identity.resolveHandle

assistant (Jan 6, 2025, 09:51 AM)

rewrite Untitled

<!DOCTYPE html>
<html>
<head>
  <style>
  * {
    box-sizing: border-box;
  }

  body {
    font-family: Helvetica, Arial, sans-serif;
    max-width: 800px;
    margin: 20px auto;
    padding: 0 20px;
  }

  input {
    display: block;
    width: 100%;
    padding: 8px;
    margin: 8px 0;
    font-size: 16px;
    border: 1px solid #ccc;
    border-radius: 4px;
  }

  button {
    background: #0070ff;
    color: white;
    border: none;
    padding: 8px 16px;
    border-radius: 4px;
    cursor: pointer;
  }

  button:hover {
    background: #0060df;
  }

  #result {
    margin-top: 20px;
    padding: 16px;
    background: #f5f5f5;
    border-radius: 4px;
    word-break: break-all;
  }

  .error {
    color: #d00;
  }
  </style>
</head>
<body>
  <h1>Resolve Bluesky handle to DID</h1>
  
  <input type="text" id="handle" placeholder="Enter Bluesky handle (e.g. jay.bsky.social)">
  <button onclick="resolveDid()">Resolve</button>
  
  <div id="result"></div>

<script type="module">
const BSKY_URL = 'https://bsky.social/xrpc/com.atproto.identity.resolveHandle'
const result = document.getElementById('result')
const handleInput = document.getElementById('handle')

async function resolveDid() {
  const handle = handleInput.value.trim()
  if (!handle) {
    result.innerHTML = '<span class="error">Please enter a handle</span>'
    return
  }

  try {
    const response = await fetch(`${BSKY_URL}?handle=${encodeURIComponent(handle)}`)
    if (!response.ok) throw new Error('Handle not found')
    const data = await response.json()
    result.textContent = data.did
  } catch (err) {
    result.innerHTML = `<span class="error">Error: ${err.message}</span>`
  }
}

handleInput.addEventListener('keypress', (e) => {
  if (e.key === 'Enter') resolveDid()
})
</script>
</body>
</html>

{"type":"text","text":""}

human (Jan 6, 2025, 09:52 AM)

use a form with an onsubmit handler

assistant (Jan 6, 2025, 09:52 AM)

rewrite Untitled

<!DOCTYPE html>
<html>
<head>
  <style>
  * {
    box-sizing: border-box;
  }

  body {
    font-family: Helvetica, Arial, sans-serif;
    max-width: 800px;
    margin: 20px auto;
    padding: 0 20px;
  }

  input {
    display: block;
    width: 100%;
    padding: 8px;
    margin: 8px 0;
    font-size: 16px;
    border: 1px solid #ccc;
    border-radius: 4px;
  }

  button {
    background: #0070ff;
    color: white;
    border: none;
    padding: 8px 16px;
    border-radius: 4px;
    cursor: pointer;
  }

  button:hover {
    background: #0060df;
  }

  #result {
    margin-top: 20px;
    padding: 16px;
    background: #f5f5f5;
    border-radius: 4px;
    word-break: break-all;
  }

  .error {
    color: #d00;
  }
  </style>
</head>
<body>
  <h1>Resolve Bluesky handle to DID</h1>
  
  <form onsubmit="return resolveDid(event)">
    <input type="text" id="handle" name="handle" placeholder="Enter Bluesky handle (e.g. jay.bsky.social)" required>
    <button type="submit">Resolve</button>
  </form>
  
  <div id="result"></div>

<script type="module">
const BSKY_URL = 'https://bsky.social/xrpc/com.atproto.identity.resolveHandle'
const result = document.getElementById('result')

async function resolveDid(event) {
  event.preventDefault()
  const handle = event.target.handle.value.trim()

  try {
    const response = await fetch(`${BSKY_URL}?handle=${encodeURIComponent(handle)}`)
    if (!response.ok) throw new Error('Handle not found')
    const data = await response.json()
    result.textContent = data.did
  } catch (err) {
    result.innerHTML = `<span class="error">Error: ${err.message}</span>`
  }
  return false
}
</script>
</body>
</html>

{"type":"text","text":""}

human (Jan 6, 2025, 09:53 AM)

be sure to cancel the submission

assistant (Jan 6, 2025, 09:53 AM)

update bluesky-resolver

Find this:

async function resolveDid(event) {
  event.preventDefault()

Replace with this:

async function resolveDid(event) {
  if (event) event.preventDefault()
  if (event?.target?.tagName !== 'FORM') return false

{"type":"text","text":""}

human (Jan 6, 2025, 09:53 AM)

don't use an onsubmit= attribute, build it better than that

assistant (Jan 6, 2025, 09:54 AM)

rewrite Untitled

<!DOCTYPE html>
<html>
<head>
  <style>
  * {
    box-sizing: border-box;
  }

  body {
    font-family: Helvetica, Arial, sans-serif;
    max-width: 800px;
    margin: 20px auto;
    padding: 0 20px;
  }

  input {
    display: block;
    width: 100%;
    padding: 8px;
    margin: 8px 0;
    font-size: 16px;
    border: 1px solid #ccc;
    border-radius: 4px;
  }

  button {
    background: #0070ff;
    color: white;
    border: none;
    padding: 8px 16px;
    border-radius: 4px;
    cursor: pointer;
  }

  button:hover {
    background: #0060df;
  }

  #result {
    margin-top: 20px;
    padding: 16px;
    background: #f5f5f5;
    border-radius: 4px;
    word-break: break-all;
  }

  .error {
    color: #d00;
  }
  </style>
</head>
<body>
  <h1>Resolve Bluesky handle to DID</h1>
  
  <form id="resolve-form">
    <input type="text" id="handle" name="handle" placeholder="Enter Bluesky handle (e.g. jay.bsky.social)" required>
    <button type="submit">Resolve</button>
  </form>
  
  <div id="result"></div>

<script type="module">
const BSKY_URL = 'https://bsky.social/xrpc/com.atproto.identity.resolveHandle'
const form = document.getElementById('resolve-form')
const result = document.getElementById('result')

async function resolveDid(event) {
  if (!event?.target?.tagName === 'FORM') return
  event.preventDefault()
  
  const handle = event.target.handle.value.trim()

  try {
    const response = await fetch(`${BSKY_URL}?handle=${encodeURIComponent(handle)}`)
    if (!response.ok) throw new Error('Handle not found')
    const data = await response.json()
    result.textContent = data.did
  } catch (err) {
    result.innerHTML = `<span class="error">Error: ${err.message}</span>`
  }
  return false
}

form.addEventListener('submit', resolveDid)
</script>
</body>
</html>

{"type":"text","text":""}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment