Skip to content

Instantly share code, notes, and snippets.

@alanshaw
Created July 1, 2025 08:53
Show Gist options
  • Save alanshaw/1d1691e0631b9df6071a27aac56065ea to your computer and use it in GitHub Desktop.
Save alanshaw/1d1691e0631b9df6071a27aac56065ea to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"strconv"
"time"
"github.com/multiformats/go-multibase"
"github.com/multiformats/go-multicodec"
"github.com/multiformats/go-multihash"
"github.com/storacha/go-libstoracha/capabilities/assert"
"github.com/storacha/go-libstoracha/metadata"
"github.com/storacha/go-ucanto/core/delegation"
"github.com/storacha/go-ucanto/did"
"github.com/storacha/indexing-service/pkg/providerresults"
)
var key = "\x12 \x81\x8b\x11\x11\xfd\x9ap\x12\x80\xe8\x9d[\xd7\xcb\x99\x06\x8d\xba\x16\xcc\xb1\xc8\xcd\x7f\xbe\xdc\xaa\x95bi6="
var data = []string{
"\x83X\"\x12 \x81\x8b\x11\x11\xfd\x9ap\x12\x80\xe8\x9d[\xd7\xcb\x99\x06\x8d\xba\x16\xcc\xb1\xc8\xcd\x7f\xbe\xdc\xaa\x95bi6=Y\x02\xf6\x82\x80\xf8\x01\xa4ac\xd8*Y\x02\xad\x00\x01\x82\x04\x00\xa6\x05:\xa2eroots\x81\xd8*X%\x00\x01q\x12 \x1fl\x87\xaa\xb6.\x9d\xd0\xc2\x89\xf1N\xa8\\g\xea\x1b\x15\xc2\xde\n@\"\xb4\xaei\xc5\xa6\xba.\xf1\xe8gversion\x01Y\x01q\x12 \x1fl\x87\xaa\xb6.\x9d\xd0\xc2\x89\xf1N\xa8\\g\xea\x1b\x15\xc2\xde\n@\"\xb4\xaei\xc5\xa6\xba.\xf1\xe8\[email protected]\xd8*X%\x00\x01q\x12 6\x93\x8d\xae\xb5\xea\xfb\xd8\x8e\xe4ar\xe6\xaf\xeb\x0f\xe3\x94\x93\x9b'\x0f\xd4m\xa8\x16\x8f`\x85Y\x9e\xc2\x8f\x04\x01q\x12 6\x93\x8d\xae\xb5\xea\xfb\xd8\x8e\xe4ar\xe6\xaf\xeb\x0f\xe3\x94\x93\x9b'\x0f\xd4m\xa8\x16\x8f`\x85Y\x9e\xc2\xa7asXD\xed\xa1\x03@\x06\xf3c(H\x81\xeb\xc1\x1bi\xef=\xa6\x1c\xbe1|\xa8-\xcaf\x9fH\x8b3\x9c\xfd6\xb5g\x01y\xf8\x90Sf\xe9[\xefc\xb75\xcc\x04\xa4\x04tw\xc6F\xa4\xfd\x04\xe4\x93\\P\x0b\xaeQ^\x80'\x06ave0.9.1catt\x81\xa3bnb\xa3erange\xa2flength\x19\x04`foffset\x1a\x00[\xe7Lgcontent\xa1fdigestX\"\x12 \x81\x8b\x11\x11\xfd\x9ap\x12\x80\xe8\x9d[\xd7\xcb\x99\x06\x8d\xba\x16\xcc\xb1\xc8\xcd\x7f\xbe\xdc\xaa\x95bi6=hlocation\x81x\xa2https://carpark-prod-0.r2.w3s.link/bagbaierafhliybiitwrfjqvyklkw7wdhpa66xpl6ffbhn77k2cu7752wr4wq/bagbaierafhliybiitwrfjqvyklkw7wdhpa66xpl6ffbhn77k2cu7752wr4wq.carccanoassert/locationdwithx did:web:indexer.storacha.networkcaudX\x1e\x9d\x1aweb:indexer.storacha.networkcexp\x1ah[\xc6)cissX\x1e\x9d\x1aweb:indexer.storacha.networkcprf\x80ae\x1ah[\xc6)ar\x82\x1a\x00[\xe7L\x19\x04`as\xd8*X%\x00\x01U\x12 \x81\x8b\x11\x11\xfd\x9ap\x12\x80\xe8\x9d[\xd7\xcb\x99\x06\x8d\xba\x16\xcc\xb1\xc8\xcd\x7f\xbe\xdc\xaa\x95bi6=\x82X&\x00$\b\x01\x12 \xa4\n\xfc \xa1\xa6\x19\xb3\xda\xa6HvL\xddh,<\xd2\x1f\xde\xeb\xbb\x95\x10%\x01^D\xdaT\x9b\xb9\x82X\xa25\x1acarpark-prod-0.r2.w3s.link\xbb\x03\xe1\x03\x80\x01/bagbaierafhliybiitwrfjqvyklkw7wdhpa66xpl6ffbhn77k2cu7752wr4wq/bagbaierafhliybiitwrfjqvyklkw7wdhpa66xpl6ffbhn77k2cu7752wr4wq.carXt5Yprod-content-claims-bucket-claimsv1bucketefd46802-1mqz6d8o7xw8.s3.us-west-2.amazonaws.com\xbb\x03\xe1\x03\x14/{claim}/{claim}.car",
"\x83X\"\x12 \x81\x8b\x11\x11\xfd\x9ap\x12\x80\xe8\x9d[\xd7\xcb\x99\x06\x8d\xba\x16\xcc\xb1\xc8\xcd\x7f\xbe\xdc\xaa\x95bi6=Y\x02\xf6\x82\x80\xf8\x01\xa4ac\xd8*Y\x02\xad\x00\x01\x82\x04\x00\xa6\x05:\xa2eroots\x81\xd8*X%\x00\x01q\x12 \x99\x01\x92q\x19\xd3\x9b\x9e\xb2r\x1a\xe01\xc6\x14\xe7\x0bD\xc2v\xc6\xa6\xa5Q\xabY\xfaj\xf5+\x9c\x13gversion\x01Y\x01q\x12 \x99\x01\x92q\x19\xd3\x9b\x9e\xb2r\x1a\xe01\xc6\x14\xe7\x0bD\xc2v\xc6\xa6\xa5Q\xabY\xfaj\xf5+\x9c\x13\[email protected]\xd8*X%\x00\x01q\x12 2_\xf5\x10\xf0tY3NY\a\xfea\xd8\xca/\xafA\xac\xd8\xe7x\x10f\xe6\xfc\xe3uA\xc8\xbc\x14\x8f\x04\x01q\x12 2_\xf5\x10\xf0tY3NY\a\xfea\xd8\xca/\xafA\xac\xd8\xe7x\x10f\xe6\xfc\xe3uA\xc8\xbc\x14\xa7asXD\xed\xa1\x03@@T\x03\x1b,\xddC\x9ec/\xdc)\xec^\x189\x9c\xc2N4MG\x1d\x17( \xde\xa72\xa1,\x96I\xf6\xa5?,!\xffH\x13\xfb|\xbe\"\xab\x82U\xad\xca\xbd\xc7\xd9}\xfb\xa5\x8d6\xce\xc6\xa0/Z\x0eave0.9.1catt\x81\xa3bnb\xa3erange\xa2flength\x19\x04`foffset\x1a\x00x\xe52gcontent\xa1fdigestX\"\x12 \x81\x8b\x11\x11\xfd\x9ap\x12\x80\xe8\x9d[\xd7\xcb\x99\x06\x8d\xba\x16\xcc\xb1\xc8\xcd\x7f\xbe\xdc\xaa\x95bi6=hlocation\x81x\xa2https://carpark-prod-0.r2.w3s.link/bagbaierad5wknrmi4wdej5nueyxwtbupt4wszaxv7qypthazz7knkcrzaaqa/bagbaierad5wknrmi4wdej5nueyxwtbupt4wszaxv7qypthazz7knkcrzaaqa.carccanoassert/locationdwithx did:web:indexer.storacha.networkcaudX\x1e\x9d\x1aweb:indexer.storacha.networkcexp\x1ah[\xc6\x1ecissX\x1e\x9d\x1aweb:indexer.storacha.networkcprf\x80ae\x1ah[\xc6\x1ear\x82\x1a\x00x\xe52\x19\x04`as\xd8*X%\x00\x01U\x12 \x81\x8b\x11\x11\xfd\x9ap\x12\x80\xe8\x9d[\xd7\xcb\x99\x06\x8d\xba\x16\xcc\xb1\xc8\xcd\x7f\xbe\xdc\xaa\x95bi6=\x82X&\x00$\b\x01\x12 \xa4\n\xfc \xa1\xa6\x19\xb3\xda\xa6HvL\xddh,<\xd2\x1f\xde\xeb\xbb\x95\x10%\x01^D\xdaT\x9b\xb9\x82X\xa25\x1acarpark-prod-0.r2.w3s.link\xbb\x03\xe1\x03\x80\x01/bagbaierad5wknrmi4wdej5nueyxwtbupt4wszaxv7qypthazz7knkcrzaaqa/bagbaierad5wknrmi4wdej5nueyxwtbupt4wszaxv7qypthazz7knkcrzaaqa.carXt5Yprod-content-claims-bucket-claimsv1bucketefd46802-1mqz6d8o7xw8.s3.us-west-2.amazonaws.com\xbb\x03\xe1\x03\x14/{claim}/{claim}.car",
}
func main() {
for _, d := range data {
fmt.Printf("Key: %s\n", FormatDigest(multihash.Multihash(key)))
r, err := providerresults.UnmarshalCBOR([]byte(d))
if err != nil {
panic(err)
}
fmt.Printf("ContextID: %#x\n", r.ContextID)
md := metadata.MetadataContext.New()
err = md.UnmarshalBinary(r.Metadata)
if err != nil {
panic(err)
}
fmt.Println("Metadata:")
// the provider may list one or more protocols for this CID
// in our case, the protocols are just differnt types of content claims
for _, code := range md.Protocols() {
protocol := md.Get(code)
// make sure this is some kind of claim protocol, ignore if not
hasClaim, ok := protocol.(metadata.HasClaim)
if !ok {
fmt.Println("UNKNOWN METADATA")
continue
}
protoName := "unknown"
if protocol.ID() == metadata.LocationCommitmentID {
protoName = "location commitment"
} else if protocol.ID() == metadata.EqualsClaimID {
protoName = "equivalency claim"
} else if protocol.ID() == metadata.IndexClaimID {
protoName = "index claim"
}
fmt.Printf(" ID: 0x%s (%s)\n", strconv.FormatUint(uint64(protocol.ID()), 16), protoName)
fmt.Printf(" Claim: %s\n", hasClaim.GetClaim())
if hasClaim.GetClaim().Prefix().MhType == uint64(multicodec.Identity) {
dmh, err := multihash.Decode(hasClaim.GetClaim().Hash())
if err != nil {
panic(err)
}
dlg, err := delegation.Extract(dmh.Digest)
if err != nil {
panic(err)
}
fmt.Printf(" Can: %s\n", dlg.Capabilities()[0].Can())
fmt.Printf(" With: %s\n", dlg.Capabilities()[0].With())
if dlg.Capabilities()[0].Can() == assert.LocationAbility {
fmt.Println(" Nb:")
nb, err := assert.LocationCaveatsReader.Read(dlg.Capabilities()[0].Nb())
if err != nil {
panic(err)
}
fmt.Printf(" Content: %s\n", FormatDigest(nb.Content.Hash()))
fmt.Println(" Location:")
for _, l := range nb.Location {
fmt.Printf(" %s\n", l.String())
}
if nb.Range != nil {
if nb.Range.Length != nil {
fmt.Printf(" Range: %d-%d (%d bytes)\n", nb.Range.Offset, nb.Range.Offset+*nb.Range.Length, *nb.Range.Length)
} else {
fmt.Printf(" Range: %d-\n", nb.Range.Offset)
}
}
if nb.Space != did.Undef {
fmt.Printf(" Space: %s\n", nb.Space.String())
}
} else if dlg.Capabilities()[0].Can() == assert.IndexAbility {
fmt.Println(" Nb:")
nb, err := assert.IndexCaveatsReader.Read(dlg.Capabilities()[0].Nb())
if err != nil {
panic(err)
}
fmt.Printf(" Content: %s\n", nb.Content)
fmt.Printf(" Index: %s\n", nb.Index)
}
if dlg.Expiration() != nil {
fmt.Printf(" Expiration: %s\n", time.Unix(int64(*dlg.Expiration()), 0).String())
}
}
switch typedProtocol := protocol.(type) {
case *metadata.EqualsClaimMetadata:
fmt.Printf(" Equals: %s\n", typedProtocol.Equals)
fmt.Printf(" Expiration: %s\n", time.Unix(typedProtocol.Expiration, 0).String())
case *metadata.IndexClaimMetadata:
fmt.Printf(" Index: %s\n", typedProtocol.Index)
fmt.Printf(" Expiration: %s\n", time.Unix(typedProtocol.Expiration, 0).String())
case *metadata.LocationCommitmentMetadata:
if typedProtocol.Shard != nil {
fmt.Printf(" Shard: %s\n", typedProtocol.Shard)
}
if typedProtocol.Range != nil {
if typedProtocol.Range.Length != nil {
fmt.Printf(" Range: %d-%d (%d bytes)\n", typedProtocol.Range.Offset, typedProtocol.Range.Offset+*typedProtocol.Range.Length, *typedProtocol.Range.Length)
} else {
fmt.Printf(" Range: %d-\n", typedProtocol.Range.Offset)
}
}
fmt.Printf(" Expiration: %s\n", time.Unix(typedProtocol.Expiration, 0).String())
}
fmt.Println("Provider:")
fmt.Printf(" Peer: %s\n", r.Provider.ID)
fmt.Println(" Addresses:")
for _, a := range r.Provider.Addrs {
fmt.Printf(" %s\n", a)
}
fmt.Println("---")
}
}
}
func FormatDigest(digest multihash.Multihash) string {
key, _ := multibase.Encode(multibase.Base58BTC, digest)
return key
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment