Skip to content

Commit

Permalink
update node registrar client to accept private key instead of seed
Browse files Browse the repository at this point in the history
  • Loading branch information
Eslam-Nawara committed Feb 26, 2025
1 parent dc11912 commit b0566d6
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 54 deletions.
29 changes: 15 additions & 14 deletions node-registrar/client/account_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"encoding/base64"
"net/http"
"net/http/httptest"
"net/url"
Expand All @@ -14,9 +15,9 @@ func TestCreateAccount(t *testing.T) {
var count int
require := require.New(t)

_, seed, publicKeyBase64, err := aliceKeys()
publicKey, privateKey, err := aliceKeys()
require.NoError(err)
account.PublicKey = publicKeyBase64
account.PublicKey = base64.StdEncoding.EncodeToString(publicKey)

testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
statusCode, body := serverHandler(r, request, count, require)
Expand All @@ -31,7 +32,7 @@ func TestCreateAccount(t *testing.T) {
require.NoError(err)

request = newClientWithNoAccount
c, err := NewRegistrarClient(baseURL, seed)
c, err := NewRegistrarClient(baseURL, privateKey)
require.NoError(err)

t.Run("test create account created successfully", func(t *testing.T) {
Expand All @@ -47,9 +48,9 @@ func TestUpdateAccount(t *testing.T) {
var count int
require := require.New(t)

pk, seed, publicKeyBase64, err := aliceKeys()
publicKey, privateKey, err := aliceKeys()
require.NoError(err)
account.PublicKey = publicKeyBase64
account.PublicKey = base64.StdEncoding.EncodeToString(publicKey)

testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
statusCode, body := serverHandler(r, request, count, require)
Expand All @@ -67,11 +68,11 @@ func TestUpdateAccount(t *testing.T) {
t.Run("test update account updated successfully", func(t *testing.T) {
count = 0
request = newClientWithAccountNoNode
c, err := NewRegistrarClient(baseURL, seed)
c, err := NewRegistrarClient(baseURL, privateKey)

require.NoError(err)
require.Equal(c.twinID, account.TwinID)
require.Equal([]byte(c.keyPair.publicKey), pk)
require.Equal(c.keyPair.publicKey, publicKey)

request = updateAccountWithStatusOK
relays := []string{"relay1"}
Expand All @@ -81,10 +82,10 @@ func TestUpdateAccount(t *testing.T) {

t.Run("test update account account not found", func(t *testing.T) {
request = newClientWithNoAccount
c, err := NewRegistrarClient(baseURL, seed)
c, err := NewRegistrarClient(baseURL, privateKey)

require.NoError(err)
require.Equal([]byte(c.keyPair.publicKey), pk)
require.Equal(c.keyPair.publicKey, publicKey)

request = updateAccountWithNoAccount
relays := []string{"relay1"}
Expand All @@ -98,9 +99,9 @@ func TestGetAccount(t *testing.T) {
var count int
require := require.New(t)

pk, seed, publicKeyBase64, err := aliceKeys()
publicKey, privateKey, err := aliceKeys()
require.NoError(err)
account.PublicKey = publicKeyBase64
account.PublicKey = base64.StdEncoding.EncodeToString(publicKey)

testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
statusCode, body := serverHandler(r, request, count, require)
Expand All @@ -116,10 +117,10 @@ func TestGetAccount(t *testing.T) {

count = 0
request = newClientWithAccountNoNode
c, err := NewRegistrarClient(baseURL, seed)
c, err := NewRegistrarClient(baseURL, privateKey)
require.NoError(err)
require.Equal(c.twinID, account.TwinID)
require.Equal([]byte(c.keyPair.publicKey), pk)
require.Equal(account.TwinID, c.twinID)
require.Equal(publicKey, c.keyPair.publicKey)

t.Run("test get account with id account not found", func(t *testing.T) {
request = getAccountWithIDStatusNotFount
Expand Down
3 changes: 1 addition & 2 deletions node-registrar/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ type RegistrarClient struct {
baseURL string
}

func NewRegistrarClient(baseURL string, sk []byte) (cli RegistrarClient, err error) {
func NewRegistrarClient(baseURL string, privateKey ed25519.PrivateKey) (cli RegistrarClient, err error) {
client := http.DefaultClient

privateKey := ed25519.NewKeyFromSeed(sk)
publicKey, ok := privateKey.Public().(ed25519.PublicKey)
if !ok {
return cli, errors.Wrap(err, "failed to get public key of provided private key")
Expand Down
17 changes: 9 additions & 8 deletions node-registrar/client/client_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"encoding/base64"
"net/http"
"net/http/httptest"
"net/url"
Expand All @@ -14,9 +15,9 @@ func TestNewRegistrarClient(t *testing.T) {
var count int
require := require.New(t)

pk, seed, publicKeyBase64, err := aliceKeys()
publicKey, privateKey, err := aliceKeys()
require.NoError(err)
account.PublicKey = publicKeyBase64
account.PublicKey = base64.StdEncoding.EncodeToString(publicKey)

testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
statusCode, body := serverHandler(r, request, count, require)
Expand All @@ -33,29 +34,29 @@ func TestNewRegistrarClient(t *testing.T) {
t.Run("test new registrar client with no account", func(t *testing.T) {
count = 0
request = newClientWithNoAccount
c, err := NewRegistrarClient(baseURL, seed)
c, err := NewRegistrarClient(baseURL, privateKey)
require.NoError(err)
require.Equal(uint64(0), c.twinID)
require.Equal(uint64(0), c.nodeID)
require.Equal([]byte(c.keyPair.publicKey), pk)
require.Equal(publicKey, c.keyPair.publicKey)
})

t.Run("test new registrar client with account and no node", func(t *testing.T) {
count = 0
request = newClientWithAccountNoNode
c, err := NewRegistrarClient(baseURL, seed)
c, err := NewRegistrarClient(baseURL, privateKey)
require.NoError(err)
require.Equal(account.TwinID, c.twinID)
require.Equal(uint64(0), c.nodeID)
require.Equal(pk, []byte(c.keyPair.publicKey))
require.Equal(publicKey, c.keyPair.publicKey)
})
t.Run("test new registrar client with account and node", func(t *testing.T) {
count = 0
request = newClientWithAccountAndNode
c, err := NewRegistrarClient(baseURL, seed)
c, err := NewRegistrarClient(baseURL, privateKey)
require.NoError(err)
require.Equal(account.TwinID, c.twinID)
require.Equal(nodeID, c.nodeID)
require.Equal(pk, []byte(c.keyPair.publicKey))
require.Equal(publicKey, c.keyPair.publicKey)
})
}
21 changes: 11 additions & 10 deletions node-registrar/client/farm_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"encoding/base64"
"net/http"
"net/http/httptest"
"net/url"
Expand All @@ -14,9 +15,9 @@ func TestCreateFarm(t *testing.T) {
var count int
require := require.New(t)

_, seed, publicKeyBase64, err := aliceKeys()
publicKey, privateKey, err := aliceKeys()
require.NoError(err)
account.PublicKey = publicKeyBase64
account.PublicKey = base64.StdEncoding.EncodeToString(publicKey)

testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
statusCode, body := serverHandler(r, request, count, require)
Expand All @@ -31,7 +32,7 @@ func TestCreateFarm(t *testing.T) {
require.NoError(err)

request = newClientWithAccountNoNode
c, err := NewRegistrarClient(baseURL, seed)
c, err := NewRegistrarClient(baseURL, privateKey)
require.NoError(err)

t.Run("test create farm with status conflict", func(t *testing.T) {
Expand All @@ -53,9 +54,9 @@ func TestUpdateFarm(t *testing.T) {
var count int
require := require.New(t)

_, seed, publicKeyBase64, err := aliceKeys()
publicKey, privateKey, err := aliceKeys()
require.NoError(err)
account.PublicKey = publicKeyBase64
account.PublicKey = base64.StdEncoding.EncodeToString(publicKey)

testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
statusCode, body := serverHandler(r, request, count, require)
Expand All @@ -71,7 +72,7 @@ func TestUpdateFarm(t *testing.T) {

t.Run("test update farm with status unauthorzed", func(t *testing.T) {
request = newClientWithNoAccount
c, err := NewRegistrarClient(baseURL, seed)
c, err := NewRegistrarClient(baseURL, privateKey)
require.NoError(err)

request = updateFarmWithStatusUnauthorized
Expand All @@ -82,7 +83,7 @@ func TestUpdateFarm(t *testing.T) {
t.Run("test update farm with status ok", func(t *testing.T) {
count = 0
request = newClientWithAccountNoNode
c, err := NewRegistrarClient(baseURL, seed)
c, err := NewRegistrarClient(baseURL, privateKey)
require.NoError(err)

request = updateFarmWithStatusOK
Expand All @@ -96,9 +97,9 @@ func TestGetFarm(t *testing.T) {
var count int
require := require.New(t)

_, seed, publicKeyBase64, err := aliceKeys()
publicKey, privateKey, err := aliceKeys()
require.NoError(err)
account.PublicKey = publicKeyBase64
account.PublicKey = base64.StdEncoding.EncodeToString(publicKey)

testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
statusCode, body := serverHandler(r, request, count, require)
Expand All @@ -114,7 +115,7 @@ func TestGetFarm(t *testing.T) {

count = 0
request = newClientWithAccountNoNode
c, err := NewRegistrarClient(baseURL, seed)
c, err := NewRegistrarClient(baseURL, privateKey)
require.NoError(err)

t.Run("test get farm with status not found", func(t *testing.T) {
Expand Down
23 changes: 12 additions & 11 deletions node-registrar/client/node_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"encoding/base64"
"net/http"
"net/http/httptest"
"net/url"
Expand All @@ -15,9 +16,9 @@ func TestRegistarNode(t *testing.T) {
var count int
require := require.New(t)

_, seed, publicKeyBase64, err := aliceKeys()
publicKey, privateKey, err := aliceKeys()
require.NoError(err)
account.PublicKey = publicKeyBase64
account.PublicKey = base64.StdEncoding.EncodeToString(publicKey)

testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
statusCode, body := serverHandler(r, request, count, require)
Expand All @@ -33,7 +34,7 @@ func TestRegistarNode(t *testing.T) {

t.Run("test registar node no account", func(t *testing.T) {
request = newClientWithNoAccount
c, err := NewRegistrarClient(baseURL, seed)
c, err := NewRegistrarClient(baseURL, privateKey)
require.NoError(err)

request = registerNodeWithNoAccount
Expand All @@ -44,7 +45,7 @@ func TestRegistarNode(t *testing.T) {
t.Run("test registar node, node already exist", func(t *testing.T) {
count = 0
request = newClientWithAccountAndNode
c, err := NewRegistrarClient(baseURL, seed)
c, err := NewRegistrarClient(baseURL, privateKey)
require.NoError(err)

count = 0
Expand All @@ -56,7 +57,7 @@ func TestRegistarNode(t *testing.T) {
t.Run("test registar node, created successfully", func(t *testing.T) {
count = 0
request = newClientWithAccountNoNode
c, err := NewRegistrarClient(baseURL, seed)
c, err := NewRegistrarClient(baseURL, privateKey)
require.NoError(err)

count = 0
Expand All @@ -72,9 +73,9 @@ func TestUpdateNode(t *testing.T) {
var count int
require := require.New(t)

_, seed, publicKeyBase64, err := aliceKeys()
publicKey, privateKey, err := aliceKeys()
require.NoError(err)
account.PublicKey = publicKeyBase64
account.PublicKey = base64.StdEncoding.EncodeToString(publicKey)

testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
statusCode, body := serverHandler(r, request, count, require)
Expand All @@ -89,7 +90,7 @@ func TestUpdateNode(t *testing.T) {
require.NoError(err)

request = newClientWithAccountAndNode
c, err := NewRegistrarClient(baseURL, seed)
c, err := NewRegistrarClient(baseURL, privateKey)
require.NoError(err)

t.Run("test update node with status ok", func(t *testing.T) {
Expand All @@ -116,9 +117,9 @@ func TestGetNode(t *testing.T) {
var count int
require := require.New(t)

_, seed, publicKeyBase64, err := aliceKeys()
publicKey, privateKey, err := aliceKeys()
require.NoError(err)
account.PublicKey = publicKeyBase64
account.PublicKey = base64.StdEncoding.EncodeToString(publicKey)

testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
statusCode, body := serverHandler(r, request, count, require)
Expand All @@ -133,7 +134,7 @@ func TestGetNode(t *testing.T) {
require.NoError(err)

request = newClientWithAccountAndNode
c, err := NewRegistrarClient(baseURL, seed)
c, err := NewRegistrarClient(baseURL, privateKey)
require.NoError(err)

t.Run("test get node status not found", func(t *testing.T) {
Expand Down
15 changes: 6 additions & 9 deletions node-registrar/client/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package client

import (
"crypto/ed25519"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -238,19 +237,17 @@ func serverHandler(r *http.Request, request, count int, require *require.Asserti
return http.StatusNotAcceptable, nil
}

func aliceKeys() (pk, seed []byte, pkBase64 string, err error) {
seed, err = hex.DecodeString(aliceSeed)
func aliceKeys() (publicKey ed25519.PublicKey, privateKey ed25519.PrivateKey, err error) {
seed, err := hex.DecodeString(aliceSeed)
if err != nil {
return
}

privateKey := ed25519.NewKeyFromSeed(seed)
pk, ok := privateKey.Public().(ed25519.PublicKey)
privateKey = ed25519.NewKeyFromSeed(seed)
publicKey, ok := privateKey.Public().(ed25519.PublicKey)
if !ok {
return pk, seed, pkBase64, fmt.Errorf("failed to get public key of provided private key")
return publicKey, privateKey, fmt.Errorf("failed to get public key of provided private key")
}

pkBase64 = base64.StdEncoding.EncodeToString(pk)

return pk, seed, pkBase64, nil
return
}

0 comments on commit b0566d6

Please sign in to comment.