42 lines
895 B
Go
42 lines
895 B
Go
package database
|
|
|
|
import (
|
|
"bytes"
|
|
"github.com/ipfs/go-cid"
|
|
"github.com/multiformats/go-multihash"
|
|
)
|
|
|
|
// HashIdentifier Main identifier
|
|
type HashIdentifier multihash.DecodedMultihash
|
|
|
|
func (i *HashIdentifier) IsKey() bool {
|
|
return i.Code == multihash.SHA2_256
|
|
}
|
|
func (i *HashIdentifier) Encode() []byte {
|
|
b, _ := multihash.Encode(i.Digest, i.Code)
|
|
return b
|
|
}
|
|
func (i *HashIdentifier) CID() cid.Cid {
|
|
return cid.NewCidV1(cid.Raw, i.Hash())
|
|
}
|
|
func (i *HashIdentifier) Hash() multihash.Multihash {
|
|
return i.Encode()
|
|
}
|
|
func (i *HashIdentifier) Equals(o *HashIdentifier) bool {
|
|
return i.Code == o.Code && bytes.Compare(i.Digest, o.Digest) == 0
|
|
}
|
|
|
|
func NewHashIdentifierFromMultihash(mh multihash.Multihash) *HashIdentifier {
|
|
d, err := multihash.Decode(mh)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
i := HashIdentifier(*d)
|
|
return &i
|
|
}
|
|
|
|
type HashEntry struct {
|
|
Value HashIdentifier
|
|
Kind string
|
|
}
|