Skip to content
Snippets Groups Projects
Commit 570bc8c5 authored by Jonah Husson's avatar Jonah Husson
Browse files

basic testing

parent 81f5904e
Branches
Tags
No related merge requests found
......@@ -3,39 +3,15 @@ package client
import (
"crypto"
"crypto/sha256"
"encoding/json"
"git.xx.network/elixxir/mainnet-commitments/messages"
"github.com/pkg/errors"
jww "github.com/spf13/jwalterweatherman"
"gitlab.com/xx_network/comms/connect"
"gitlab.com/xx_network/crypto/csprng"
"gitlab.com/xx_network/crypto/signature/rsa"
"gitlab.com/xx_network/primitives/id"
"gitlab.com/xx_network/primitives/id/idf"
)
// SignAndTransmit creates a Client object & transmits commitment info to the server
func SignAndTransmit(pk, cert, idfBytes, commitmentCert []byte, wallet, address string) error {
idfStruct := &idf.IdFile{}
err := json.Unmarshal(idfBytes, idfStruct)
if err != nil {
return err
}
nodeID, err := id.Unmarshal(idfStruct.IdBytes[:])
if err != nil {
return errors.WithMessage(err, "Failed to unmarshal ID from IDF")
}
h, err := connect.NewHost(nil, address, commitmentCert, connect.GetDefaultHostParams())
if err != nil {
return err
}
cl, err := StartClient(pk, cert, idfStruct.Salt[:], nodeID)
if err != nil {
jww.FATAL.Panicf("Failed to start client: %+v", err)
}
func SignAndTransmit(pk, idfBytes []byte, wallet string, h *connect.Host, s Sender) error {
key, err := rsa.LoadPrivateKeyFromPem(pk)
if err != nil {
jww.FATAL.Panicf("Failed to load private key: %+v", err)
......@@ -54,12 +30,13 @@ func SignAndTransmit(pk, cert, idfBytes, commitmentCert []byte, wallet, address
jww.FATAL.Panicf("Failed to sign node info: %+v", err)
}
err = cl.SignAndTransmit(h, &messages.Commitment{
err = s.SignAndTransmit(h, &messages.Commitment{
PrivateKey: pk,
IDF: idfBytes,
Wallet: wallet,
Signature: sig,
})
if err != nil {
jww.FATAL.Panicf("Error in registering commitment: %+v", err)
}
......
......@@ -10,6 +10,10 @@ import (
"google.golang.org/grpc"
)
type Sender interface {
SignAndTransmit(host *connect.Host, message *messages.Commitment) error
}
// Client struct implements the GRPC client call to mainnet-commitments servers
type Client struct {
pc *connect.ProtoComms
......
package client
import "testing"
import (
"encoding/json"
"git.xx.network/elixxir/mainnet-commitments/messages"
"git.xx.network/elixxir/mainnet-commitments/server"
"git.xx.network/elixxir/mainnet-commitments/storage"
"gitlab.com/xx_network/comms/connect"
"gitlab.com/xx_network/crypto/csprng"
"gitlab.com/xx_network/crypto/signature/rsa"
"gitlab.com/xx_network/primitives/id"
"gitlab.com/xx_network/primitives/id/idf"
"testing"
)
type MockSender struct {
t *testing.T
id, cert []byte
}
func (ms *MockSender) SignAndTransmit(host *connect.Host, message *messages.Commitment) error {
s, err := storage.NewStorage(storage.Params{})
if err != nil {
ms.t.Error("Failed to init storage for mock server")
}
err = s.InsertMembers([]storage.Member{{
Id: ms.id,
Cert: ms.cert,
},
})
if err != nil {
ms.t.Errorf("Failed to insert members: %+v", err)
}
impl := server.Impl{}
impl.SetStorage(ms.t, s)
_, err = impl.Verify(nil, message)
if err != nil {
ms.t.Errorf("Failed to verify: %+v", err)
}
return nil
}
func TestSignAndTransmit(t *testing.T) {
pk, err := rsa.GenerateKey(csprng.NewSystemRNG(), 2048)
if err != nil {
t.Errorf("Failed to gen key: %+v", err)
}
nid := id.NewIdFromString("zezima", id.Node, t)
idb := [33]byte{}
copy(idb[:], nid.Marshal())
idFile := idf.IdFile{
ID: nid.String(),
Type: nid.GetType().String(),
Salt: [32]byte{},
IdBytes: idb,
HexNodeID: nid.HexEncode(),
}
idfBytes, err := json.Marshal(idFile)
if err != nil {
t.Errorf("Failed to marshal IDF: %+v", err)
}
err = SignAndTransmit(rsa.CreatePrivateKeyPem(pk), idfBytes, "wallet", nil, &MockSender{t, nid.Bytes(), rsa.CreatePublicKeyPem(pk.GetPublic())})
if err != nil {
t.Errorf("Failed to sign & transmit: %+v", err)
}
}
......@@ -4,7 +4,11 @@
package main
import (
"encoding/json"
"git.xx.network/elixxir/mainnet-commitments/client"
"gitlab.com/xx_network/comms/connect"
"gitlab.com/xx_network/primitives/id"
"gitlab.com/xx_network/primitives/id/idf"
"gitlab.com/xx_network/primitives/utils"
"syscall/js"
)
......@@ -27,7 +31,7 @@ func SignAndTransmit(this js.Value, inputs []js.Value) interface{} {
wallet := inputs[4].String()
address := inputs[5].String()
var cert, key, idf, commitmentCert []byte
var cert, key, idfBytes, commitmentCert []byte
var err error
var ep string
// Read certificate file
......@@ -52,7 +56,7 @@ func SignAndTransmit(this js.Value, inputs []js.Value) interface{} {
// Read id file
if ep, err = utils.ExpandPath(idfPath); err == nil {
idf, err = utils.ReadFile(ep)
idfBytes, err = utils.ReadFile(ep)
if err != nil {
return map[string]interface{}{"Error": err.Error()}
}
......@@ -69,8 +73,29 @@ func SignAndTransmit(this js.Value, inputs []js.Value) interface{} {
return map[string]interface{}{"Error": err.Error()}
}
idfStruct := &idf.IdFile{}
err = json.Unmarshal(idfBytes, idfStruct)
if err != nil {
return map[string]interface{}{"Error": err.Error()}
}
nodeID, err := id.Unmarshal(idfStruct.IdBytes[:])
if err != nil {
return map[string]interface{}{"Error": err.Error()}
}
cl, err := client.StartClient(key, cert, idfStruct.Salt[:], nodeID)
if err != nil {
return map[string]interface{}{"Error": err.Error()}
}
h, err := connect.NewHost(&id.Permissioning, address, commitmentCert, connect.GetDefaultHostParams())
if err != nil {
return err
}
// Sign & transmit information
err = client.SignAndTransmit(key, cert, idf, commitmentCert, wallet, address)
err = client.SignAndTransmit(key, idfBytes, wallet, h, cl)
if err != nil {
return map[string]interface{}{"Error": err.Error()}
}
......
......@@ -12,6 +12,7 @@ import (
"gitlab.com/xx_network/crypto/signature/rsa"
"gitlab.com/xx_network/primitives/id"
"gitlab.com/xx_network/primitives/id/idf"
"testing"
)
// Params struct holds data needed to create a server Impl
......@@ -93,3 +94,14 @@ func (i *Impl) Verify(_ context.Context, msg *messages.Commitment) (*messages.Co
func (i *Impl) Stop() {
i.pc.Shutdown()
}
func (i *Impl) GetStorage() *storage.Storage {
return i.s
}
func (i *Impl) SetStorage(t *testing.T, s *storage.Storage) {
if t == nil {
panic("Cannot set storage on impl outside of testing")
}
i.s = s
}
package storage
import "gorm.io/gorm/clause"
func (db *DatabaseImpl) InsertMembers(members []Member) error {
return db.db.Create(&members).Error
}
func (db *DatabaseImpl) InsertCommitment(commitment Commitment) error {
return db.db.Create(&commitment).Error
return db.db.Clauses(clause.OnConflict{UpdateAll: true}).Create(&commitment).Error
}
func (db *DatabaseImpl) GetMember(id []byte) (*Member, error) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment