From f5f253aa8165ae907ff910365690bb182460b8f0 Mon Sep 17 00:00:00 2001 From: i-norden Date: Thu, 12 May 2022 21:10:31 -0500 Subject: [PATCH] multihash key gen func --- pkg/shared/functions.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 pkg/shared/functions.go diff --git a/pkg/shared/functions.go b/pkg/shared/functions.go new file mode 100644 index 0000000..1ef9620 --- /dev/null +++ b/pkg/shared/functions.go @@ -0,0 +1,19 @@ +package shared + +import ( + blockstore "github.com/ipfs/go-ipfs-blockstore" + dshelp "github.com/ipfs/go-ipfs-ds-help" + "github.com/multiformats/go-multihash" +) + +const SSZ_SHA2_256_PREFIX int = 0xb501 + +// MultihashKeyFromSSZRoot converts a SSZ-SHA2-256 root hash into a blockstore prefixed multihash key +func MultihashKeyFromSSZRoot(root []byte) (string, error) { + mh, err := multihash.Encode(root, SSZ_SHA2_256_PREFIX) + if err != nil { + return "", err + } + dbKey := dshelp.MultihashToDsKey(mh) + return blockstore.BlockPrefix.String() + dbKey.String(), nil +}