feat: add NodeId

This commit is contained in:
Derrick Hammer 2024-01-03 08:21:48 -05:00
parent 56be9082c3
commit 3a251479e1
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 58 additions and 0 deletions

58
nodeid/nodeid.go Normal file
View File

@ -0,0 +1,58 @@
package nodeid
import (
"bytes"
"errors"
"git.lumeweb.com/LumeWeb/libs5-go/internal/bases"
"github.com/multiformats/go-multibase"
)
var (
errorNotBase58BTC = errors.New("not a base58btc string")
)
type NodeId struct {
Bytes []byte
}
func New(bytes []byte) *NodeId {
return &NodeId{Bytes: bytes}
}
func Decode(nodeId string) (*NodeId, error) {
encoding, ret, err := multibase.Decode(nodeId)
if err != nil {
return nil, err
}
if encoding != multibase.Base58BTC {
return nil, errorNotBase58BTC
}
return New(ret), nil
}
func (nodeId *NodeId) Equals(other interface{}) bool {
if otherNodeId, ok := other.(*NodeId); ok {
return bytes.Equal(nodeId.Bytes, otherNodeId.Bytes)
}
return false
}
func (nodeId *NodeId) HashCode() int {
if len(nodeId.Bytes) < 4 {
return 0
}
return int(nodeId.Bytes[0]) +
int(nodeId.Bytes[1])<<8 +
int(nodeId.Bytes[2])<<16 +
int(nodeId.Bytes[3])<<24
}
func (nodeId *NodeId) ToBase58() (string, error) {
return bases.ToBase58BTC(nodeId.Bytes)
}
func (nodeId *NodeId) ToString() (string, error) {
return nodeId.ToBase58()
}