From 3172def440e268ddaf03394a86c6041cbae2db38 Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Tue, 31 Dec 2024 18:24:48 +0200 Subject: [PATCH] add string methods where they needed, add method to get list of keys from key store --- crypto/lux_key.go | 25 +++++++++++++++++++------ host/lux_host.go | 2 ++ proto/lux_id.go | 4 ++++ proto/lux_type.go | 11 +++++++++++ 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/crypto/lux_key.go b/crypto/lux_key.go index 0d424e8..b6b63ad 100644 --- a/crypto/lux_key.go +++ b/crypto/lux_key.go @@ -2,6 +2,7 @@ package crypto import ( "crypto/rand" + "fmt" "lux/proto" "os" ) @@ -66,6 +67,10 @@ func (key *LuxKey) Write(wd *proto.LuxBuffer) { wd.WriteBytes(key.IV) } +func (key *LuxKey) String() string { + return fmt.Sprintf("%s %s", key.Type.String(), key.Id.String()) +} + const LUX_KEYSTORE_FILEMODE = os.FileMode(int(0600)) type LuxKeyStore struct { @@ -111,12 +116,9 @@ func (ks *LuxKeyStore) Save() error { return os.WriteFile(ks.filePath, wd.AllBytes(), LUX_KEYSTORE_FILEMODE) } -func (ks *LuxKeyStore) Get(id proto.LuxID) *LuxKey { - if key, ok := ks.keys[id]; ok { - return &key - } else { - return nil - } +func (ks *LuxKeyStore) Get(id proto.LuxID) (LuxKey, bool) { + key, ok := ks.keys[id] + return key, ok } func (ks *LuxKeyStore) Put(key LuxKey) error { @@ -127,3 +129,14 @@ func (ks *LuxKeyStore) Put(key LuxKey) error { return nil } + +func (ks *LuxKeyStore) Keys() []LuxKey { + values := make([]LuxKey, len(ks.keys)) + + i := 0 + for _, value := range ks.keys { + values[i] = value + } + + return values +} diff --git a/host/lux_host.go b/host/lux_host.go index 20d512c..2d22db6 100644 --- a/host/lux_host.go +++ b/host/lux_host.go @@ -45,5 +45,7 @@ func LuxHostEntry(configPath string) error { log.Fatal(err) } + fmt.Println(key.String()) + return nil } diff --git a/proto/lux_id.go b/proto/lux_id.go index 2f84f13..97bb115 100644 --- a/proto/lux_id.go +++ b/proto/lux_id.go @@ -28,3 +28,7 @@ func (id *LuxID) Read(rd *LuxBuffer) error { func (id *LuxID) Write(wd *LuxBuffer) { wd.WriteBytes(id.UUID[:]) } + +func (id *LuxID) String() string { + return id.UUID.String() +} diff --git a/proto/lux_type.go b/proto/lux_type.go index b5a72a7..20b9de0 100644 --- a/proto/lux_type.go +++ b/proto/lux_type.go @@ -21,3 +21,14 @@ func (luxType *LuxType) Read(rd *LuxBuffer) error { func (luxType *LuxType) Write(wd *LuxBuffer) { wd.WriteUint16(uint16(*luxType)) } + +func (luxType *LuxType) String() string { + switch *luxType { + case LuxTypeHost: + return "host" + case LuxTypeNode: + return "node" + default: + return "unknown" + } +}