refactor: unify all encoding packages

This commit is contained in:
Derrick Hammer 2024-01-03 08:36:23 -05:00
parent 355de2b65f
commit 1b30048a75
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
7 changed files with 80 additions and 88 deletions

View File

@ -1,13 +1,11 @@
package cid package encoding
import ( import (
"bytes" "bytes"
"encoding/hex" "encoding/hex"
"errors" "errors"
"fmt" "fmt"
"git.lumeweb.com/LumeWeb/libs5-go/cid/multibase"
"git.lumeweb.com/LumeWeb/libs5-go/internal/bases" "git.lumeweb.com/LumeWeb/libs5-go/internal/bases"
"git.lumeweb.com/LumeWeb/libs5-go/multihash"
"git.lumeweb.com/LumeWeb/libs5-go/types" "git.lumeweb.com/LumeWeb/libs5-go/types"
"git.lumeweb.com/LumeWeb/libs5-go/utils" "git.lumeweb.com/LumeWeb/libs5-go/utils"
) )
@ -18,19 +16,19 @@ var (
) )
type CID struct { type CID struct {
multibase.Multibase Multibase
Type types.CIDType Type types.CIDType
Hash multihash.Multihash Hash Multihash
Size uint32 Size uint32
} }
func New(Type types.CIDType, Hash multihash.Multihash, Size uint32) *CID { func NewCID(Type types.CIDType, Hash Multihash, Size uint32) *CID {
c := &CID{ c := &CID{
Type: Type, Type: Type,
Hash: Hash, Hash: Hash,
Size: Size, Size: Size,
} }
m := multibase.New(c) m := NewMultibase(c)
c.Multibase = m c.Multibase = m
return c return c
@ -60,7 +58,7 @@ func (cid *CID) ToBytes() []byte {
} }
func Decode(cid string) (*CID, error) { func Decode(cid string) (*CID, error) {
decodedBytes, err := multibase.DecodeString(cid) decodedBytes, err := MultibaseDecodeString(cid)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -74,7 +72,7 @@ func Decode(cid string) (*CID, error) {
return cidInstance, nil return cidInstance, nil
} }
func FromRegistry(bytes []byte) (*CID, error) { func CIDFromRegistry(bytes []byte) (*CID, error) {
if len(bytes) == 0 { if len(bytes) == 0 {
return nil, errEmptyBytes return nil, errEmptyBytes
} }
@ -95,11 +93,11 @@ func FromRegistry(bytes []byte) (*CID, error) {
return cidInstance, nil return cidInstance, nil
} }
func FromBytes(bytes []byte) (*CID, error) { func CIDFromBytes(bytes []byte) (*CID, error) {
return initCID(bytes) return initCID(bytes)
} }
func FromHash(bytes interface{}, size uint32, cidType types.CIDType) (*CID, error) { func CIDFromHash(bytes interface{}, size uint32, cidType types.CIDType) (*CID, error) {
var ( var (
byteSlice []byte byteSlice []byte
err error err error
@ -121,10 +119,10 @@ func FromHash(bytes interface{}, size uint32, cidType types.CIDType) (*CID, erro
return nil, fmt.Errorf("invalid hash type %d", cidType) return nil, fmt.Errorf("invalid hash type %d", cidType)
} }
return New(cidType, *multihash.New(byteSlice), size), nil return NewCID(cidType, *NewMultihash(byteSlice), size), nil
} }
func Verify(bytes interface{}) bool { func CIDVerify(bytes interface{}) bool {
var ( var (
byteSlice []byte byteSlice []byte
err error err error
@ -132,7 +130,7 @@ func Verify(bytes interface{}) bool {
switch v := bytes.(type) { switch v := bytes.(type) {
case string: case string:
byteSlice, err = multibase.DecodeString(v) // Assuming MultibaseDecodeString function is defined byteSlice, err = MultibaseDecodeString(v) // Assuming MultibaseDecodeString function is defined
if err != nil { if err != nil {
return false return false
} }
@ -155,7 +153,7 @@ func (cid *CID) CopyWith(newType int, newSize uint32) (*CID, error) {
return nil, fmt.Errorf("invalid cid type %d", newType) return nil, fmt.Errorf("invalid cid type %d", newType)
} }
return New(types.CIDType(newType), cid.Hash, newSize), nil return NewCID(types.CIDType(newType), cid.Hash, newSize), nil
} }
func (cid *CID) ToRegistryEntry() []byte { func (cid *CID) ToRegistryEntry() []byte {
@ -197,8 +195,8 @@ func (cid *CID) HashCode() int {
int(fullBytes[3])<<24 int(fullBytes[3])<<24
} }
func FromRegistryPublicKey(pubkey interface{}) (*CID, error) { func CIDFromRegistryPublicKey(pubkey interface{}) (*CID, error) {
return FromHash(pubkey, 0, types.CIDTypeResolver) return CIDFromHash(pubkey, 0, types.CIDTypeResolver)
} }
func initCID(bytes []byte) (*CID, error) { func initCID(bytes []byte) (*CID, error) {
@ -208,12 +206,12 @@ func initCID(bytes []byte) (*CID, error) {
cidType := types.CIDType(bytes[0]) cidType := types.CIDType(bytes[0])
if cidType == types.CIDTypeBridge { if cidType == types.CIDTypeBridge {
hash := multihash.New(bytes[1:35]) hash := NewMultihash(bytes[1:35])
return New(cidType, *hash, 0), nil return NewCID(cidType, *hash, 0), nil
} }
hashBytes := bytes[1:34] hashBytes := bytes[1:34]
hash := multihash.New(hashBytes) hash := NewMultihash(hashBytes)
var size uint32 var size uint32
if len(bytes) > 34 { if len(bytes) > 34 {
@ -226,5 +224,5 @@ func initCID(bytes []byte) (*CID, error) {
return nil, fmt.Errorf("invalid cid type %d", cidType) return nil, fmt.Errorf("invalid cid type %d", cidType)
} }
return New(cidType, *hash, size), nil return NewCID(cidType, *hash, size), nil
} }

View File

@ -1,9 +1,7 @@
package cid package encoding
import ( import (
"git.lumeweb.com/LumeWeb/libs5-go/cid/multibase"
"git.lumeweb.com/LumeWeb/libs5-go/internal/testdata" "git.lumeweb.com/LumeWeb/libs5-go/internal/testdata"
"git.lumeweb.com/LumeWeb/libs5-go/multihash"
"git.lumeweb.com/LumeWeb/libs5-go/types" "git.lumeweb.com/LumeWeb/libs5-go/types"
"reflect" "reflect"
"testing" "testing"
@ -11,9 +9,9 @@ import (
func TestCID_CopyWith(t *testing.T) { func TestCID_CopyWith(t *testing.T) {
type fields struct { type fields struct {
Multibase multibase.Multibase Multibase Multibase
Type types.CIDType Type types.CIDType
Hash multihash.Multihash Hash Multihash
Size uint32 Size uint32
} }
type args struct { type args struct {
@ -51,9 +49,9 @@ func TestCID_CopyWith(t *testing.T) {
func TestCID_Equals(t *testing.T) { func TestCID_Equals(t *testing.T) {
type fields struct { type fields struct {
Multibase multibase.Multibase Multibase Multibase
Type types.CIDType Type types.CIDType
Hash multihash.Multihash Hash Multihash
Size uint32 Size uint32
} }
type args struct { type args struct {
@ -84,9 +82,9 @@ func TestCID_Equals(t *testing.T) {
func TestCID_HashCode(t *testing.T) { func TestCID_HashCode(t *testing.T) {
type fields struct { type fields struct {
Multibase multibase.Multibase Multibase Multibase
Type types.CIDType Type types.CIDType
Hash multihash.Multihash Hash Multihash
Size uint32 Size uint32
} }
tests := []struct { tests := []struct {
@ -112,14 +110,14 @@ func TestCID_HashCode(t *testing.T) {
} }
/*func TestCID_ToBytes(t *testing.T) { /*func TestCID_ToBytes(t *testing.T) {
FromHash(testdata.RawBase58CID) CIDFromHash(testdata.RawBase58CID)
println(len(testdata.RawCIDBytes)) println(len(testdata.RawCIDBytes))
println(utils.DecodeEndian(testdata.RawCIDBytes[35:])) println(utils.DecodeEndian(testdata.RawCIDBytes[35:]))
return return
type fields struct { type fields struct {
Type types.CIDType Type types.CIDType
Hash multihash.Multihash Hash Multihash
Size uint32 Size uint32
} }
tests := []struct { tests := []struct {
@ -131,7 +129,7 @@ func TestCID_HashCode(t *testing.T) {
name: "Bridge CID", name: "Bridge CID",
fields: fields{ fields: fields{
Type: types.CIDTypeBridge, Type: types.CIDTypeBridge,
Hash: multihash.New(), // Replace with a valid hash value Hash: NewMultibase(), // Replace with a valid hash value
}, },
want: , // Replace with the expected byte output for Bridge CID want: , // Replace with the expected byte output for Bridge CID
}, },
@ -139,7 +137,7 @@ func TestCID_HashCode(t *testing.T) {
name: "Raw CID with Non-Zero Size", name: "Raw CID with Non-Zero Size",
fields: fields{ fields: fields{
Type: types.CIDTypeRaw, Type: types.CIDTypeRaw,
Hash: *multihash.New(testdata.RawCIDBytes[1:34]), Hash: *NewMultibase(testdata.RawCIDBytes[1:34]),
Size: utils.DecodeEndian(testdata.RawCIDBytes[34:]), Size: utils.DecodeEndian(testdata.RawCIDBytes[34:]),
}, },
want: testdata.RawCIDBytes, want: testdata.RawCIDBytes,
@ -169,7 +167,7 @@ func TestCID_HashCode(t *testing.T) {
Hash: tt.fields.Hash, Hash: tt.fields.Hash,
Size: tt.fields.Size, Size: tt.fields.Size,
} }
m := multibase.New(cid) m := NewMultibase(cid)
cid.Multibase = m cid.Multibase = m
if got := cid.ToBytes(); !reflect.DeepEqual(got, tt.want) { if got := cid.ToBytes(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ToBytes() = %v, want %v", got, tt.want) t.Errorf("ToBytes() = %v, want %v", got, tt.want)
@ -180,9 +178,9 @@ func TestCID_HashCode(t *testing.T) {
func TestCID_ToRegistryCID(t *testing.T) { func TestCID_ToRegistryCID(t *testing.T) {
type fields struct { type fields struct {
Multibase multibase.Multibase Multibase Multibase
Type types.CIDType Type types.CIDType
Hash multihash.Multihash Hash Multihash
Size uint32 Size uint32
} }
tests := []struct { tests := []struct {
@ -215,9 +213,9 @@ func TestCID_ToRegistryCID(t *testing.T) {
func TestCID_ToRegistryEntry(t *testing.T) { func TestCID_ToRegistryEntry(t *testing.T) {
type fields struct { type fields struct {
Multibase multibase.Multibase Multibase Multibase
Type types.CIDType Type types.CIDType
Hash multihash.Multihash Hash Multihash
Size uint32 Size uint32
} }
tests := []struct { tests := []struct {
@ -244,9 +242,9 @@ func TestCID_ToRegistryEntry(t *testing.T) {
func TestCID_ToString(t *testing.T) { func TestCID_ToString(t *testing.T) {
type fields struct { type fields struct {
Multibase multibase.Multibase Multibase Multibase
Type types.CIDType Type types.CIDType
Hash multihash.Multihash Hash Multihash
Size uint32 Size uint32
} }
tests := []struct { tests := []struct {
@ -279,9 +277,9 @@ func TestCID_ToString(t *testing.T) {
func TestCID_getPrefixBytes(t *testing.T) { func TestCID_getPrefixBytes(t *testing.T) {
type fields struct { type fields struct {
Multibase multibase.Multibase Multibase Multibase
Type types.CIDType Type types.CIDType
Hash multihash.Multihash Hash Multihash
Size uint32 Size uint32
} }
tests := []struct { tests := []struct {
@ -326,19 +324,19 @@ func TestDecode(t *testing.T) {
{ {
name: "Valid Raw Base 58 CID", name: "Valid Raw Base 58 CID",
args: args{cid: testdata.RawBase58CID}, args: args{cid: testdata.RawBase58CID},
want: New(types.CIDTypeRaw, *multihash.New(testdata.RawCIDBytes[1:34]), testdata.RawCIDSize), want: NewCID(types.CIDTypeRaw, *NewMultihash(testdata.RawCIDBytes[1:34]), testdata.RawCIDSize),
wantErr: false, wantErr: false,
}, },
{ {
name: "Valid Media 58 CID", name: "Valid Media 58 CID",
args: args{cid: testdata.MediaBase58CID}, args: args{cid: testdata.MediaBase58CID},
want: New(types.CIDTypeMetadataMedia, *multihash.New(testdata.MediaCIDBytes[1:34]), testdata.MediaCIDSize), want: NewCID(types.CIDTypeMetadataMedia, *NewMultihash(testdata.MediaCIDBytes[1:34]), testdata.MediaCIDSize),
wantErr: false, wantErr: false,
}, },
{ {
name: "Valid Resolver CID", name: "Valid Resolver CID",
args: args{cid: testdata.ResolverBase58CID}, args: args{cid: testdata.ResolverBase58CID},
want: New(types.CIDTypeResolver, *multihash.New(testdata.ResolverCIDBytes[1:34]), testdata.ResolverCIDSize), want: NewCID(types.CIDTypeResolver, *NewMultihash(testdata.ResolverCIDBytes[1:34]), testdata.ResolverCIDSize),
wantErr: false, wantErr: false,
}, },
} }
@ -369,31 +367,31 @@ func TestFromBytes(t *testing.T) {
{ {
name: "Valid Raw Base 58 CID", name: "Valid Raw Base 58 CID",
args: args{bytes: testdata.RawCIDBytes}, args: args{bytes: testdata.RawCIDBytes},
want: New(types.CIDTypeRaw, *multihash.New(testdata.RawCIDBytes[1:34]), testdata.RawCIDSize), want: NewCID(types.CIDTypeRaw, *NewMultihash(testdata.RawCIDBytes[1:34]), testdata.RawCIDSize),
wantErr: false, wantErr: false,
}, },
{ {
name: "Valid Media 58 CID", name: "Valid Media 58 CID",
args: args{bytes: testdata.MediaCIDBytes}, args: args{bytes: testdata.MediaCIDBytes},
want: New(types.CIDTypeMetadataMedia, *multihash.New(testdata.MediaCIDBytes[1:34]), testdata.MediaCIDSize), want: NewCID(types.CIDTypeMetadataMedia, *NewMultihash(testdata.MediaCIDBytes[1:34]), testdata.MediaCIDSize),
wantErr: false, wantErr: false,
}, },
{ {
name: "Valid Resolver CID", name: "Valid Resolver CID",
args: args{bytes: testdata.ResolverCIDBytes}, args: args{bytes: testdata.ResolverCIDBytes},
want: New(types.CIDTypeResolver, *multihash.New(testdata.ResolverCIDBytes[1:34]), testdata.ResolverCIDSize), want: NewCID(types.CIDTypeResolver, *NewMultihash(testdata.ResolverCIDBytes[1:34]), testdata.ResolverCIDSize),
wantErr: false, wantErr: false,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
got, err := FromBytes(tt.args.bytes) got, err := CIDFromBytes(tt.args.bytes)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("FromBytes() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("CIDFromBytes() error = %v, wantErr %v", err, tt.wantErr)
return return
} }
if !reflect.DeepEqual(got, tt.want) { if !reflect.DeepEqual(got, tt.want) {
t.Errorf("FromBytes() got = %v, want %v", got, tt.want) t.Errorf("CIDFromBytes() got = %v, want %v", got, tt.want)
} }
}) })
} }
@ -414,31 +412,31 @@ func TestFromHash(t *testing.T) {
{ {
name: "Valid Raw Base 58 CID", name: "Valid Raw Base 58 CID",
args: args{bytes: testdata.RawCIDBytes[1:34], size: testdata.RawCIDSize, cidType: types.CIDTypeRaw}, args: args{bytes: testdata.RawCIDBytes[1:34], size: testdata.RawCIDSize, cidType: types.CIDTypeRaw},
want: New(types.CIDTypeRaw, *multihash.New(testdata.RawCIDBytes[1:34]), testdata.RawCIDSize), want: NewCID(types.CIDTypeRaw, *NewMultihash(testdata.RawCIDBytes[1:34]), testdata.RawCIDSize),
wantErr: false, wantErr: false,
}, },
{ {
name: "Valid Media 58 CID", name: "Valid Media 58 CID",
args: args{bytes: testdata.MediaCIDBytes[1:34], size: testdata.MediaCIDSize, cidType: types.CIDTypeMetadataMedia}, args: args{bytes: testdata.MediaCIDBytes[1:34], size: testdata.MediaCIDSize, cidType: types.CIDTypeMetadataMedia},
want: New(types.CIDTypeMetadataMedia, *multihash.New(testdata.MediaCIDBytes[1:34]), testdata.MediaCIDSize), want: NewCID(types.CIDTypeMetadataMedia, *NewMultihash(testdata.MediaCIDBytes[1:34]), testdata.MediaCIDSize),
wantErr: false, wantErr: false,
}, },
{ {
name: "Valid Resolver CID", name: "Valid Resolver CID",
args: args{bytes: testdata.ResolverCIDBytes[1:34], size: testdata.ResolverCIDSize, cidType: types.CIDTypeResolver}, args: args{bytes: testdata.ResolverCIDBytes[1:34], size: testdata.ResolverCIDSize, cidType: types.CIDTypeResolver},
want: New(types.CIDTypeResolver, *multihash.New(testdata.ResolverCIDBytes[1:34]), testdata.ResolverCIDSize), want: NewCID(types.CIDTypeResolver, *NewMultihash(testdata.ResolverCIDBytes[1:34]), testdata.ResolverCIDSize),
wantErr: false, wantErr: false,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
got, err := FromHash(tt.args.bytes, tt.args.size, tt.args.cidType) got, err := CIDFromHash(tt.args.bytes, tt.args.size, tt.args.cidType)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("FromHash() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("CIDFromHash() error = %v, wantErr %v", err, tt.wantErr)
return return
} }
if !reflect.DeepEqual(got, tt.want) { if !reflect.DeepEqual(got, tt.want) {
t.Errorf("FromHash() got = %v, want %v", got, tt.want) t.Errorf("CIDFromHash() got = %v, want %v", got, tt.want)
} }
}) })
} }
@ -457,19 +455,19 @@ func TestFromRegistry(t *testing.T) {
{ {
name: "Valid Resolver Data", name: "Valid Resolver Data",
args: args{bytes: testdata.ResolverDataBytes}, args: args{bytes: testdata.ResolverDataBytes},
want: New(types.CIDTypeMetadataWebapp, *multihash.New(testdata.ResolverDataBytes[2:35]), testdata.ResolverDataSize), want: NewCID(types.CIDTypeMetadataWebapp, *NewMultihash(testdata.ResolverDataBytes[2:35]), testdata.ResolverDataSize),
wantErr: false, wantErr: false,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
got, err := FromRegistry(tt.args.bytes) got, err := CIDFromRegistry(tt.args.bytes)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("FromRegistry() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("CIDFromRegistry() error = %v, wantErr %v", err, tt.wantErr)
return return
} }
if !reflect.DeepEqual(got, tt.want) { if !reflect.DeepEqual(got, tt.want) {
t.Errorf("FromRegistry() got = %v, want %v", got, tt.want) t.Errorf("CIDFromRegistry() got = %v, want %v", got, tt.want)
} }
}) })
} }

View File

@ -1,4 +1,4 @@
package multibase package encoding
import ( import (
"errors" "errors"
@ -30,11 +30,11 @@ type Multibase interface {
var _ Multibase = (*multibaseImpl)(nil) var _ Multibase = (*multibaseImpl)(nil)
func New(encoder Encoder) Multibase { func NewMultibase(encoder Encoder) Multibase {
return &multibaseImpl{encoder: encoder} return &multibaseImpl{encoder: encoder}
} }
func DecodeString(data string) (bytes []byte, err error) { func MultibaseDecodeString(data string) (bytes []byte, err error) {
if len(data) == 0 { if len(data) == 0 {
return nil, errMultibaseDecodeZeroLength return nil, errMultibaseDecodeZeroLength
} }

View File

@ -1,4 +1,4 @@
package multibase package encoding
import ( import (
"git.lumeweb.com/LumeWeb/libs5-go/internal/testdata" "git.lumeweb.com/LumeWeb/libs5-go/internal/testdata"
@ -17,7 +17,7 @@ func (e *encoder) ToBytes() []byte {
func newEncoder(data []byte) encoder { func newEncoder(data []byte) encoder {
e := &encoder{data: data} e := &encoder{data: data}
m := New(e) m := NewMultibase(e)
e.Multibase = m e.Multibase = m
return *e return *e
@ -84,13 +84,13 @@ func TestDecodeString(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
gotBytes, err := DecodeString(tt.args.data) gotBytes, err := MultibaseDecodeString(tt.args.data)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("DecodeString() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("MultibaseDecodeString() error = %v, wantErr %v", err, tt.wantErr)
return return
} }
if !reflect.DeepEqual(gotBytes, tt.wantBytes) { if !reflect.DeepEqual(gotBytes, tt.wantBytes) {
t.Errorf("DecodeString() gotBytes = %v, want %v", gotBytes, tt.wantBytes) t.Errorf("MultibaseDecodeString() gotBytes = %v, want %v", gotBytes, tt.wantBytes)
} }
}) })
} }

View File

@ -1,4 +1,4 @@
package multihash package encoding
import ( import (
"bytes" "bytes"
@ -21,7 +21,7 @@ type Multihash struct {
FullBytes []byte FullBytes []byte
} }
func New(fullBytes []byte) *Multihash { func NewMultihash(fullBytes []byte) *Multihash {
return &Multihash{FullBytes: fullBytes} return &Multihash{FullBytes: fullBytes}
} }
@ -33,7 +33,7 @@ func (m *Multihash) HashBytes() []byte {
return m.FullBytes[1:] return m.FullBytes[1:]
} }
func FromBase64Url(hash string) (*Multihash, error) { func MultihashFromBase64Url(hash string) (*Multihash, error) {
encoder, _ := multibase.EncoderByName("base64url") encoder, _ := multibase.EncoderByName("base64url")
encoding, err := getEncoding(hash) encoding, err := getEncoding(hash)
@ -45,7 +45,7 @@ func FromBase64Url(hash string) (*Multihash, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
return New(ret), nil return NewMultihash(ret), nil
} }
func (m *Multihash) ToBase64Url() (string, error) { func (m *Multihash) ToBase64Url() (string, error) {
@ -69,10 +69,6 @@ func (m *Multihash) Equals(other *Multihash) bool {
func (m *Multihash) HashCode() MultihashCode { func (m *Multihash) HashCode() MultihashCode {
return utils.HashCode(m.FullBytes[:4]) return utils.HashCode(m.FullBytes[:4])
return int(m.FullBytes[0]) +
int(m.FullBytes[1])<<8 +
int(m.FullBytes[2])<<16 +
int(m.FullBytes[3])<<24
} }
func getEncoding(hash string) (multibase.Encoding, error) { func getEncoding(hash string) (multibase.Encoding, error) {

View File

@ -1,4 +1,4 @@
package multihash package encoding
import ( import (
"git.lumeweb.com/LumeWeb/libs5-go/internal/testdata" "git.lumeweb.com/LumeWeb/libs5-go/internal/testdata"
@ -57,13 +57,13 @@ func TestFromBase64Url(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
got, err := FromBase64Url(tt.args.hash) got, err := MultihashFromBase64Url(tt.args.hash)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("FromBase64Url() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("MultihashFromBase64Url() error = %v, wantErr %v", err, tt.wantErr)
return return
} }
if !reflect.DeepEqual(got, tt.want) { if !reflect.DeepEqual(got, tt.want) {
t.Errorf("FromBase64Url() got = %v, want %v", got, tt.want) t.Errorf("MultihashFromBase64Url() got = %v, want %v", got, tt.want)
} }
}) })
} }
@ -203,8 +203,8 @@ func TestNewMultihash(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if got := New(tt.args.fullBytes); !reflect.DeepEqual(got, tt.want) { if got := NewMultihash(tt.args.fullBytes); !reflect.DeepEqual(got, tt.want) {
t.Errorf("New() = %v, want %v", got, tt.want) t.Errorf("NewMultihash() = %v, want %v", got, tt.want)
} }
}) })
} }

View File

@ -1,4 +1,4 @@
package nodeid package encoding
import ( import (
"bytes" "bytes"
@ -16,11 +16,11 @@ type NodeId struct {
Bytes []byte Bytes []byte
} }
func New(bytes []byte) *NodeId { func NewNodeId(bytes []byte) *NodeId {
return &NodeId{Bytes: bytes} return &NodeId{Bytes: bytes}
} }
func Decode(nodeId string) (*NodeId, error) { func NodeIdDecode(nodeId string) (*NodeId, error) {
encoding, ret, err := multibase.Decode(nodeId) encoding, ret, err := multibase.Decode(nodeId)
if err != nil { if err != nil {
return nil, err return nil, err
@ -30,7 +30,7 @@ func Decode(nodeId string) (*NodeId, error) {
return nil, errorNotBase58BTC return nil, errorNotBase58BTC
} }
return New(ret), nil return NewNodeId(ret), nil
} }
func (nodeId *NodeId) Equals(other interface{}) bool { func (nodeId *NodeId) Equals(other interface{}) bool {