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

Function generateCID

IceFireDB-SQLite/pkg/p2p/p2p.go:442–463  ·  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

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

Callers 1

AnnounceConnectMethod · 0.70

Calls 2

EncodeMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected