-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
147d82d
commit 36c20d2
Showing
5 changed files
with
324 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
syntax = "proto3"; | ||
|
||
option go_package = "./pb"; | ||
package inactivity; | ||
|
||
message ClaimSignatureMessage { | ||
uint32 senderID = 1; | ||
bytes claimHash = 2; | ||
bytes signature = 3; | ||
bytes publicKey = 4; | ||
string sessionID = 5; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,57 @@ | ||
package inactivity | ||
|
||
import ( | ||
"fmt" | ||
|
||
"google.golang.org/protobuf/proto" | ||
|
||
"github.com/keep-network/keep-core/pkg/protocol/group" | ||
"github.com/keep-network/keep-core/pkg/tecdsa/inactivity/gen/pb" | ||
) | ||
|
||
func validateMemberIndex(protoIndex uint32) error { | ||
// Protobuf does not have uint8 type, so we are using uint32. When | ||
// unmarshalling message, we need to make sure we do not overflow. | ||
if protoIndex > group.MaxMemberIndex { | ||
return fmt.Errorf("invalid member index value: [%v]", protoIndex) | ||
} | ||
return nil | ||
} | ||
|
||
// Marshal converts this claimSignatureMessage to a byte array suitable | ||
// for network communication. | ||
func (csm *claimSignatureMessage) Marshal() ([]byte, error) { | ||
// TODO: Implement | ||
return nil, nil | ||
return proto.Marshal(&pb.ClaimSignatureMessage{ | ||
SenderID: uint32(csm.senderID), | ||
ClaimHash: csm.claimHash[:], | ||
Signature: csm.signature, | ||
PublicKey: csm.publicKey, | ||
SessionID: csm.sessionID, | ||
}) | ||
} | ||
|
||
// Unmarshal converts a byte array produced by Marshal to a | ||
// claimSignatureMessage. | ||
func (csm *claimSignatureMessage) Unmarshal(bytes []byte) error { | ||
// TODO: Implement | ||
pbMsg := pb.ClaimSignatureMessage{} | ||
if err := proto.Unmarshal(bytes, &pbMsg); err != nil { | ||
return err | ||
} | ||
|
||
if err := validateMemberIndex(pbMsg.SenderID); err != nil { | ||
return err | ||
} | ||
csm.senderID = group.MemberIndex(pbMsg.SenderID) | ||
|
||
claimHash, err := ClaimSignatureHashFromBytes(pbMsg.ClaimHash) | ||
if err != nil { | ||
return err | ||
} | ||
csm.claimHash = claimHash | ||
|
||
csm.signature = pbMsg.Signature | ||
csm.publicKey = pbMsg.PublicKey | ||
csm.sessionID = pbMsg.SessionID | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package inactivity | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
fuzz "github.com/google/gofuzz" | ||
|
||
"github.com/keep-network/keep-core/pkg/internal/pbutils" | ||
"github.com/keep-network/keep-core/pkg/protocol/group" | ||
) | ||
|
||
func TestClaimSignatureMessage_MarshalingRoundtrip(t *testing.T) { | ||
msg := &claimSignatureMessage{ | ||
senderID: 123, | ||
claimHash: [32]byte{0: 11, 10: 22, 31: 33}, | ||
signature: []byte("signature"), | ||
publicKey: []byte("pubkey"), | ||
sessionID: "session-1", | ||
} | ||
unmarshaled := &claimSignatureMessage{} | ||
|
||
err := pbutils.RoundTrip(msg, unmarshaled) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if !reflect.DeepEqual(msg, unmarshaled) { | ||
t.Fatalf("unexpected content of unmarshaled message") | ||
} | ||
} | ||
|
||
func TestFuzzClaimSignatureMessage_MarshalingRoundtrip(t *testing.T) { | ||
for i := 0; i < 10; i++ { | ||
var ( | ||
senderID group.MemberIndex | ||
claimHash ClaimSignatureHash | ||
signature []byte | ||
publicKey []byte | ||
sessionID string | ||
) | ||
|
||
f := fuzz.New().NilChance(0.1).NumElements(0, 512) | ||
|
||
f.Fuzz(&senderID) | ||
f.Fuzz(&claimHash) | ||
f.Fuzz(&signature) | ||
f.Fuzz(&publicKey) | ||
f.Fuzz(&sessionID) | ||
|
||
message := &claimSignatureMessage{ | ||
senderID: senderID, | ||
claimHash: claimHash, | ||
signature: signature, | ||
publicKey: publicKey, | ||
sessionID: sessionID, | ||
} | ||
|
||
_ = pbutils.RoundTrip(message, &claimSignatureMessage{}) | ||
} | ||
} | ||
|
||
func TestFuzzClaimSignatureMessage_Unmarshaler(t *testing.T) { | ||
pbutils.FuzzUnmarshaler(&claimSignatureMessage{}) | ||
} |