MCPcopy Create free account
hub / github.com/IceFireDB/IceFireDB / generateCID

Function generateCID

IceFireDB-SQLProxy/pkg/p2p/p2p.go:443–464  ·  view source on GitHub ↗

A function that generates a CID object for a given string and returns it. Uses SHA256 to hash the string and generate a multihash from it. The mulithash is then base58 encoded and then used to create the CID

(namestring string)

Source from the content-addressed store, hash-verified

441// Uses SHA256 to hash the string and generate a multihash from it.
442// The mulithash is then base58 encoded and then used to create the CID
443func generateCID(namestring string) cid.Cid {
444 // Hash the service content ID with SHA256
445 hash := sha256.Sum256([]byte(namestring))
446 // Append the hash with the hashing codec ID for SHA2-256 (0x12),
447 // the digest size (0x20) and the hash of the service content ID
448 finalhash := append([]byte{0x12, 0x20}, hash[:]...)
449 // Encode the fullhash to Base58
450 b58string := base58.Encode(finalhash)
451
452 // Generate a Multihash from the base58 string
453 mulhash, err := multihash.FromB58String(string(b58string))
454 if err != nil {
455 logrus.WithFields(logrus.Fields{
456 "error": err.Error(),
457 }).Fatalln("Failed to Generate Service CID!")
458 }
459
460 // Generate a CID from the Multihash
461 cidvalue := cid.NewCidV1(12, mulhash)
462 // Return the CID
463 return cidvalue
464}

Callers 1

AnnounceConnectMethod · 0.70

Calls 2

EncodeMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected