Created
September 15, 2025 12:17
-
-
Save alanshaw/173a8818e3350f1747619c0884b49f90 to your computer and use it in GitHub Desktop.
Find the genesis of an IPNI chain given it is stored in an S3 bucket you have access to.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { GetObjectCommand, ListObjectsV2Command, S3Client } from '@aws-sdk/client-s3' | |
import Ora from 'ora' | |
import * as dagJSON from '@ipld/dag-json' | |
const spinner = Ora() | |
const client = new S3Client({ region: 'us-west-2' }) | |
const bucket = 'ipfs-advertisement' | |
let oldest | |
let cursor | |
let total = 0 | |
spinner.start() | |
while (cursor || !oldest) { | |
const cmd = new ListObjectsV2Command({ Bucket: bucket, ContinuationToken: cursor }) | |
const res = await client.send(cmd) | |
total += res.Contents.length | |
for (const item of res.Contents) { | |
if (item.Key === 'head') continue | |
if (!oldest || item.LastModified.getTime() < oldest.LastModified.getTime()) { | |
oldest = item | |
const cmd = new GetObjectCommand({ Bucket: bucket, Key: item.Key }) | |
const res = await client.send(cmd) | |
const data = await res.Body.transformToString() | |
const adOrEntries = dagJSON.parse(data) | |
if (adOrEntries.ContextID && !adOrEntries.PreviousID) { | |
spinner.succeed(`genesis: ${item.Key} ${item.LastModified.toISOString()}`) | |
break | |
} | |
spinner.fail(`not genesis: ${item.Key} ${item.LastModified.toISOString()}`) | |
spinner.start() | |
} | |
spinner.text = `oldest of ${total.toLocaleString()}: ${oldest.Key} ${oldest.LastModified.toISOString()}` | |
} | |
cursor = res.NextContinuationToken | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment