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

Accept contract path from client

parent cf6a4be7
No related branches found
No related tags found
No related merge requests found
...@@ -10,13 +10,13 @@ import ( ...@@ -10,13 +10,13 @@ import (
) )
// SignAndTransmit creates a Client object & transmits commitment info to the server // SignAndTransmit creates a Client object & transmits commitment info to the server
func SignAndTransmit(pk, idfBytes []byte, wallet string, h *connect.Host, s Sender) error { func SignAndTransmit(pk, idfBytes, contractBytes []byte, wallet string, h *connect.Host, s Sender) error {
key, err := rsa.LoadPrivateKeyFromPem(pk) key, err := rsa.LoadPrivateKeyFromPem(pk)
if err != nil { if err != nil {
return errors.WithMessage(err, "Failed to load private key") return errors.WithMessage(err, "Failed to load private key")
} }
hashed, hash, err := utils.HashNodeInfo(wallet, idfBytes) hashed, hash, err := utils.HashNodeInfo(wallet, idfBytes, contractBytes)
if err != nil { if err != nil {
return errors.WithMessage(err, "Failed to hash node info") return errors.WithMessage(err, "Failed to hash node info")
} }
...@@ -29,6 +29,7 @@ func SignAndTransmit(pk, idfBytes []byte, wallet string, h *connect.Host, s Send ...@@ -29,6 +29,7 @@ func SignAndTransmit(pk, idfBytes []byte, wallet string, h *connect.Host, s Send
err = s.TransmitSignature(h, &messages.Commitment{ err = s.TransmitSignature(h, &messages.Commitment{
PrivateKey: pk, PrivateKey: pk,
IDF: idfBytes, IDF: idfBytes,
Contract: contractBytes,
Wallet: wallet, Wallet: wallet,
Signature: sig, Signature: sig,
}) })
......
...@@ -30,7 +30,7 @@ func StartClient(key, cert, salt []byte, id *id.ID) (*Client, error) { ...@@ -30,7 +30,7 @@ func StartClient(key, cert, salt []byte, id *id.ID) (*Client, error) {
}, nil }, nil
} }
// SignAndTransmit func sends a Commitment message to the mainnet-commitments server // TransmitSignature func sends a Commitment message to the mainnet-commitments server
func (c *Client) TransmitSignature(host *connect.Host, message *messages.Commitment) error { func (c *Client) TransmitSignature(host *connect.Host, message *messages.Commitment) error {
f := func(conn *grpc.ClientConn) (*any.Any, error) { f := func(conn *grpc.ClientConn) (*any.Any, error) {
// Set up the context // Set up the context
......
...@@ -60,7 +60,8 @@ func TestSignAndTransmit(t *testing.T) { ...@@ -60,7 +60,8 @@ func TestSignAndTransmit(t *testing.T) {
t.Errorf("Failed to marshal IDF: %+v", err) t.Errorf("Failed to marshal IDF: %+v", err)
} }
err = SignAndTransmit(rsa.CreatePrivateKeyPem(pk), idfBytes, "wallet", nil, &MockSender{t, nid.Bytes(), rsa.CreatePublicKeyPem(pk.GetPublic())}) contractBytes := []byte("I solemnly swear that I am up to no good")
err = SignAndTransmit(rsa.CreatePrivateKeyPem(pk), idfBytes, contractBytes, "wallet", nil, &MockSender{t, nid.Bytes(), rsa.CreatePublicKeyPem(pk.GetPublic())})
if err != nil { if err != nil {
t.Errorf("Failed to sign & transmit: %+v", err) t.Errorf("Failed to sign & transmit: %+v", err)
} }
......
...@@ -24,16 +24,17 @@ func main() { ...@@ -24,16 +24,17 @@ func main() {
} }
// SignAndTransmit signs & transmits info to the commitments server // SignAndTransmit signs & transmits info to the commitments server
// Accepts args nodeCertPath, nodeKeyPath, idfPath, wallet, commitmentsAddress, commitmentsCertPath // Accepts args nodeCertPath, nodeKeyPath, idfPath, contractPath, wallet, commitmentsAddress, commitmentsCertPath
func SignAndTransmit(this js.Value, inputs []js.Value) interface{} { func SignAndTransmit(this js.Value, inputs []js.Value) interface{} {
certPath := inputs[0].String() certPath := inputs[0].String()
keyPath := inputs[1].String() keyPath := inputs[1].String()
idfPath := inputs[2].String() idfPath := inputs[2].String()
wallet := inputs[3].String() contractPath := inputs[3].String()
address := inputs[4].String() wallet := inputs[4].String()
commitmentsCertPath := inputs[5].String() address := inputs[5].String()
commitmentsCertPath := inputs[6].String()
var cert, key, idfBytes, commitmentCert []byte var cert, key, idfBytes, commitmentCert, contractBytes []byte
var err error var err error
var ep string var ep string
// Read certificate file // Read certificate file
...@@ -66,6 +67,15 @@ func SignAndTransmit(this js.Value, inputs []js.Value) interface{} { ...@@ -66,6 +67,15 @@ func SignAndTransmit(this js.Value, inputs []js.Value) interface{} {
return map[string]interface{}{"Error": err.Error()} return map[string]interface{}{"Error": err.Error()}
} }
if ep, err = utils.ExpandPath(contractPath); err == nil {
contractBytes, err = utils.ReadFile(ep)
if err != nil {
return map[string]interface{}{"Error": err.Error()}
}
} else {
return map[string]interface{}{"Error": err.Error()}
}
if ep, err = utils.ExpandPath(commitmentsCertPath); err == nil { if ep, err = utils.ExpandPath(commitmentsCertPath); err == nil {
commitmentCert, err = utils.ReadFile(ep) commitmentCert, err = utils.ReadFile(ep)
if err != nil { if err != nil {
...@@ -97,7 +107,7 @@ func SignAndTransmit(this js.Value, inputs []js.Value) interface{} { ...@@ -97,7 +107,7 @@ func SignAndTransmit(this js.Value, inputs []js.Value) interface{} {
} }
// Sign & transmit information // Sign & transmit information
err = client.SignAndTransmit(key, idfBytes, wallet, h, cl) err = client.SignAndTransmit(key, idfBytes, contractBytes, wallet, h, cl)
if err != nil { if err != nil {
return map[string]interface{}{"Error": err.Error()} return map[string]interface{}{"Error": err.Error()}
} }
......
...@@ -66,8 +66,9 @@ func (m *CommitmentResponse) GetError() string { ...@@ -66,8 +66,9 @@ func (m *CommitmentResponse) GetError() string {
type Commitment struct { type Commitment struct {
PrivateKey []byte `protobuf:"bytes,1,opt,name=PrivateKey,proto3" json:"PrivateKey,omitempty"` PrivateKey []byte `protobuf:"bytes,1,opt,name=PrivateKey,proto3" json:"PrivateKey,omitempty"`
IDF []byte `protobuf:"bytes,2,opt,name=IDF,proto3" json:"IDF,omitempty"` IDF []byte `protobuf:"bytes,2,opt,name=IDF,proto3" json:"IDF,omitempty"`
Wallet string `protobuf:"bytes,3,opt,name=Wallet,proto3" json:"Wallet,omitempty"` Contract []byte `protobuf:"bytes,3,opt,name=Contract,proto3" json:"Contract,omitempty"`
Signature []byte `protobuf:"bytes,4,opt,name=Signature,proto3" json:"Signature,omitempty"` Wallet string `protobuf:"bytes,4,opt,name=Wallet,proto3" json:"Wallet,omitempty"`
Signature []byte `protobuf:"bytes,5,opt,name=Signature,proto3" json:"Signature,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"` XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"` XXX_sizecache int32 `json:"-"`
...@@ -112,6 +113,13 @@ func (m *Commitment) GetIDF() []byte { ...@@ -112,6 +113,13 @@ func (m *Commitment) GetIDF() []byte {
return nil return nil
} }
func (m *Commitment) GetContract() []byte {
if m != nil {
return m.Contract
}
return nil
}
func (m *Commitment) GetWallet() string { func (m *Commitment) GetWallet() string {
if m != nil { if m != nil {
return m.Wallet return m.Wallet
...@@ -136,22 +144,23 @@ func init() { ...@@ -136,22 +144,23 @@ func init() {
} }
var fileDescriptor_02948687e8213eef = []byte{ var fileDescriptor_02948687e8213eef = []byte{
// 228 bytes of a gzipped FileDescriptorProto // 245 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0x4c, 0xce, 0xcf, 0xcd, 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x90, 0x4d, 0x4b, 0x03, 0x31,
0xcd, 0x2c, 0xc9, 0x4d, 0xcd, 0x2b, 0x89, 0xcf, 0x4d, 0x2d, 0x2e, 0x4e, 0x4c, 0x4f, 0x2d, 0xd6, 0x10, 0x86, 0x5d, 0x6b, 0x97, 0x76, 0xf4, 0x20, 0x43, 0x91, 0x58, 0x8a, 0x1f, 0x7b, 0x12, 0x0f,
0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x80, 0xf1, 0x95, 0xb4, 0xb8, 0x84, 0x9c, 0xe1, 0xca, 0x5b, 0xd0, 0xbb, 0x07, 0xab, 0x82, 0x88, 0x20, 0x2b, 0x28, 0x78, 0x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00, 0xff, 0xff, 0x95, 0xa7, 0xd1,
0xa9, 0x5e, 0x01, 0x00, 0x00,
} }
// Reference imports to suppress errors if they are not otherwise used. // Reference imports to suppress errors if they are not otherwise used.
......
...@@ -21,6 +21,7 @@ message CommitmentResponse { ...@@ -21,6 +21,7 @@ message CommitmentResponse {
message Commitment { message Commitment {
bytes PrivateKey = 1; bytes PrivateKey = 1;
bytes IDF = 2; bytes IDF = 2;
string Wallet = 3; bytes Contract = 3;
bytes Signature = 4; string Wallet = 4;
bytes Signature = 5;
} }
...@@ -57,7 +57,7 @@ func (i *Impl) Verify(_ context.Context, msg *messages.Commitment) (*messages.Co ...@@ -57,7 +57,7 @@ func (i *Impl) Verify(_ context.Context, msg *messages.Commitment) (*messages.Co
} }
// Hash node info from message // Hash node info from message
hashed, hash, err := utils.HashNodeInfo(msg.Wallet, msg.IDF) hashed, hash, err := utils.HashNodeInfo(msg.Wallet, msg.IDF, msg.Contract)
if err != nil { if err != nil {
return nil, errors.WithMessage(err, "Failed to hash node info") return nil, errors.WithMessage(err, "Failed to hash node info")
} }
...@@ -83,6 +83,7 @@ func (i *Impl) Verify(_ context.Context, msg *messages.Commitment) (*messages.Co ...@@ -83,6 +83,7 @@ func (i *Impl) Verify(_ context.Context, msg *messages.Commitment) (*messages.Co
// Insert commitment info to the database once verified // Insert commitment info to the database once verified
err = i.s.InsertCommitment(storage.Commitment{ err = i.s.InsertCommitment(storage.Commitment{
Id: m.Id, Id: m.Id,
Contract: msg.Contract,
Wallet: msg.Wallet, Wallet: msg.Wallet,
Signature: msg.Signature, Signature: msg.Signature,
}) })
......
...@@ -31,6 +31,7 @@ type Member struct { ...@@ -31,6 +31,7 @@ type Member struct {
type Commitment struct { type Commitment struct {
Id []byte `gorm:"primary_key"` Id []byte `gorm:"primary_key"`
Contract []byte `gorm:"not null"`
Wallet string `gorm:"not null"` Wallet string `gorm:"not null"`
Signature []byte `gorm:"not null"` Signature []byte `gorm:"not null"`
CreatedAt time.Time CreatedAt time.Time
......
...@@ -5,13 +5,17 @@ import ( ...@@ -5,13 +5,17 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
) )
func HashNodeInfo(wallet string, idfBytes []byte) ([]byte, crypto.Hash, error) { func HashNodeInfo(wallet string, idfBytes, contractBytes []byte) ([]byte, crypto.Hash, error) {
h := crypto.BLAKE2b_512 // Define & return this here so we aren't defining hash type in 3 places for sign/verify calls h := crypto.BLAKE2b_512 // Define & return this here so we aren't defining hash type in 3 places for sign/verify calls
hasher := h.New() hasher := h.New()
_, err := hasher.Write(idfBytes) _, err := hasher.Write(idfBytes)
if err != nil { if err != nil {
return nil, h, errors.WithMessage(err, "Failed to write IDF to hash") return nil, h, errors.WithMessage(err, "Failed to write IDF to hash")
} }
_, err = hasher.Write(contractBytes)
if err != nil {
return nil, h, errors.WithMessage(err, "Failed to write contract to hash")
}
_, err = hasher.Write([]byte(wallet)) _, err = hasher.Write([]byte(wallet))
if err != nil { if err != nil {
return nil, h, errors.WithMessage(err, "Failed to write wallet to hash") return nil, h, errors.WithMessage(err, "Failed to write wallet to hash")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment