2024-01-03 13:36:23 +00:00
|
|
|
package encoding
|
2024-01-03 08:47:47 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2024-01-04 09:19:24 +00:00
|
|
|
"encoding/json"
|
2024-01-03 08:47:47 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/internal/bases"
|
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
2024-01-03 13:27:04 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/utils"
|
2024-01-03 08:47:47 +00:00
|
|
|
"github.com/multiformats/go-multibase"
|
|
|
|
"unicode/utf8"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errorNotBase64Url = errors.New("not a base64url string")
|
|
|
|
)
|
|
|
|
|
2024-01-03 13:20:03 +00:00
|
|
|
type MultihashCode = int
|
|
|
|
|
2024-01-03 08:47:47 +00:00
|
|
|
type Multihash struct {
|
|
|
|
FullBytes []byte
|
|
|
|
}
|
|
|
|
|
2024-01-04 09:19:24 +00:00
|
|
|
var _ json.Marshaler = (*Multihash)(nil)
|
|
|
|
var _ json.Unmarshaler = (*Multihash)(nil)
|
|
|
|
|
2024-01-03 13:36:23 +00:00
|
|
|
func NewMultihash(fullBytes []byte) *Multihash {
|
2024-01-03 08:47:47 +00:00
|
|
|
return &Multihash{FullBytes: fullBytes}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Multihash) FunctionType() types.HashType {
|
|
|
|
return types.HashType(m.FullBytes[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Multihash) HashBytes() []byte {
|
|
|
|
return m.FullBytes[1:]
|
|
|
|
}
|
|
|
|
|
2024-01-03 13:36:23 +00:00
|
|
|
func MultihashFromBase64Url(hash string) (*Multihash, error) {
|
2024-01-03 08:47:47 +00:00
|
|
|
encoder, _ := multibase.EncoderByName("base64url")
|
|
|
|
encoding, err := getEncoding(hash)
|
|
|
|
|
|
|
|
if encoding != encoder.Encoding() {
|
|
|
|
return nil, errorNotBase64Url
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ret, err := multibase.Decode(hash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-01-03 13:36:23 +00:00
|
|
|
return NewMultihash(ret), nil
|
2024-01-03 08:47:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Multihash) ToBase64Url() (string, error) {
|
|
|
|
return bases.ToBase64Url(m.FullBytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Multihash) ToBase32() (string, error) {
|
|
|
|
return bases.ToBase32(m.FullBytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Multihash) ToString() (string, error) {
|
|
|
|
if m.FunctionType() == types.HashType(types.CIDTypeBridge) {
|
|
|
|
return string(m.FullBytes), nil // Assumes the bytes are valid UTF-8
|
|
|
|
}
|
|
|
|
return m.ToBase64Url()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Multihash) Equals(other *Multihash) bool {
|
|
|
|
return bytes.Equal(m.FullBytes, other.FullBytes)
|
|
|
|
}
|
|
|
|
|
2024-01-03 13:20:03 +00:00
|
|
|
func (m *Multihash) HashCode() MultihashCode {
|
2024-01-03 13:27:04 +00:00
|
|
|
return utils.HashCode(m.FullBytes[:4])
|
2024-01-03 08:47:47 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 09:19:24 +00:00
|
|
|
func (b *Multihash) UnmarshalJSON(data []byte) error {
|
|
|
|
decodedData, err := MultibaseDecodeString(string(data))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
b.FullBytes = decodedData
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (b Multihash) MarshalJSON() ([]byte, error) {
|
|
|
|
url, err := b.ToBase64Url()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return []byte(url), nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-01-03 08:47:47 +00:00
|
|
|
func getEncoding(hash string) (multibase.Encoding, error) {
|
|
|
|
r, _ := utf8.DecodeRuneInString(hash)
|
|
|
|
enc := multibase.Encoding(r)
|
|
|
|
|
|
|
|
_, ok := multibase.EncodingToStr[enc]
|
|
|
|
if !ok {
|
|
|
|
return -1, fmt.Errorf("unsupported multibase encoding: %d", enc)
|
|
|
|
|
|
|
|
}
|
|
|
|
return enc, nil
|
|
|
|
}
|