libs5-go/encoding/multibase.go

85 lines
1.7 KiB
Go
Raw Normal View History

2024-01-03 13:36:23 +00:00
package encoding
2024-01-03 08:48:19 +00:00
import (
"encoding/json"
2024-01-03 08:48:19 +00:00
"errors"
"git.lumeweb.com/LumeWeb/libs5-go/internal/bases"
"github.com/multiformats/go-multibase"
)
var (
errMultibaseEncodingNotSupported = errors.New("multibase encoding not supported")
errMultibaseDecodeZeroLength = errors.New("cannot decode multibase for zero length string")
)
type Encoder interface {
ToBytes() []byte
}
type multibaseImpl struct {
Multibase
encoder Encoder
}
var _ json.Marshaler = (*multibaseImpl)(nil)
2024-01-03 08:48:19 +00:00
type Multibase interface {
ToHex() (string, error)
ToBase32() (string, error)
ToBase64Url() (string, error)
ToBase58() (string, error)
ToString() (string, error)
}
var _ Multibase = (*multibaseImpl)(nil)
2024-01-03 13:36:23 +00:00
func NewMultibase(encoder Encoder) Multibase {
2024-01-03 08:48:19 +00:00
return &multibaseImpl{encoder: encoder}
}
2024-01-03 13:36:23 +00:00
func MultibaseDecodeString(data string) (bytes []byte, err error) {
2024-01-03 08:48:19 +00:00
if len(data) == 0 {
return nil, errMultibaseDecodeZeroLength
}
switch data[0] {
case 'z', 'f', 'u', 'b':
_, bytes, err = multibase.Decode(data)
case ':':
bytes = []byte(data)
default:
err = errMultibaseEncodingNotSupported
}
return bytes, err
}
func (m *multibaseImpl) ToHex() (string, error) {
return bases.ToHex(m.encoder.ToBytes())
}
func (m *multibaseImpl) ToBase32() (string, error) {
return bases.ToBase32(m.encoder.ToBytes())
}
func (m *multibaseImpl) ToBase64Url() (string, error) {
return bases.ToBase64Url(m.encoder.ToBytes())
}
func (m *multibaseImpl) ToBase58() (string, error) {
return bases.ToBase58BTC(m.encoder.ToBytes())
}
func (m *multibaseImpl) ToString() (string, error) {
return m.ToBase58()
}
func (b multibaseImpl) MarshalJSON() ([]byte, error) {
url, err := b.ToBase64Url()
if err != nil {
return nil, err
}
return []byte(url), nil
}