add logic for signing and signature verification

This commit is contained in:
Callum Waters
2022-11-14 15:38:39 +01:00
parent 38897de6d7
commit 662517b0db
3 changed files with 95 additions and 20 deletions
-3
View File
@@ -1,7 +1,6 @@
package orderbook
import (
"crypto/ed25519"
"fmt"
"github.com/cosmos/gogoproto/proto"
@@ -308,8 +307,6 @@ func (sm *StateMachine) ProcessProposal(req types.RequestProcessProposal) types.
// ed25519.Verify(bidOwner.PublicKey, )
}
default:
return rejectProposal()
+2 -1
View File
@@ -56,7 +56,8 @@ func TestCheckTx(t *testing.T) {
Pair: testPair,
}},
responseCode: orderbook.StatusOK,
}
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
+93 -16
View File
@@ -1,12 +1,36 @@
package orderbook
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"math"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/ed25519"
)
func NewMsgBid(pair *Pair, maxPrice, maxQuantity float64, ownerId uint64) *MsgBid {
return &MsgBid{
Pair: pair,
BidOrder: &OrderBid{
MaxPrice: maxPrice,
MaxQuantity: maxQuantity,
OwnerId: ownerId,
},
}
}
func (msg *MsgBid) Sign(pk crypto.PrivKey) error {
sig, err := pk.Sign(msg.BidOrder.DeterministicSignatureBytes(msg.Pair))
if err != nil {
return err
}
msg.BidOrder.Signature = sig
return nil
}
func (msg *MsgBid) ValidateBasic() error {
if err := msg.BidOrder.ValidateBasic(); err != nil {
return err
@@ -20,9 +44,26 @@ func (msg *MsgBid) ValidateBasic() error {
return errors.New("invalid signature size")
}
//quantity to be more than 0
// price to not be 0
return nil
}
func NewMsgAsk(pair *Pair, askPrice, quantity float64, ownerId uint64) *MsgAsk {
return &MsgAsk{
Pair: pair,
AskOrder: &OrderAsk{
AskPrice: askPrice,
Quantity: quantity,
OwnerId: ownerId,
},
}
}
func (msg *MsgAsk) Sign(pk crypto.PrivKey) error {
sig, err := pk.Sign(msg.AskOrder.DeterministicSignatureBytes(msg.Pair))
if err != nil {
return err
}
msg.AskOrder.Signature = sig
return nil
}
@@ -35,16 +76,17 @@ func (msg *MsgAsk) ValidateBasic() error {
return err
}
if len(msg.AskOrder.Signature) != ed25519.SignatureSize {
return errors.New("invalid signature size")
}
//quantity to be more than 0
// price to not be 0
return nil
}
func NewMsgCreateAccount(commodities ...*Commodity) (*MsgCreateAccount, crypto.PrivKey) {
pk := ed25519.GenPrivKey()
return &MsgCreateAccount{
PublicKey: pk.PubKey().Bytes(),
Commodities: commodities,
}, pk
}
func (msg *MsgCreateAccount) ValidateBasic() error {
if len(msg.PublicKey) != ed25519.PubKeySize {
return errors.New("invalid pub key size")
@@ -65,6 +107,10 @@ func (msg *MsgCreateAccount) ValidateBasic() error {
return nil
}
func NewMsgRegisterPair(pair *Pair) *MsgRegisterPair {
return &MsgRegisterPair{Pair: pair}
}
func (msg *MsgRegisterPair) ValidateBasic() error {
return msg.Pair.ValidateBasic()
}
@@ -98,19 +144,36 @@ func (o *OrderBid) ValidateBasic() error {
return errors.New("min price must be greater than 0")
}
if len(o.Signature) != ed25519.SignatureSize {
return errors.New("invalid signature size")
}
return nil
}
// check signatures are valid
func (o *OrderBid) ValidateSignature(pk crypto.PubKey, pair *Pair) bool {
return pk.VerifySignature(o.DeterministicSignatureBytes(pair), o.Signature)
}
func (o *OrderBid) DeterministicSignatureBytes(pair *Pair) []byte {
buf := bytes.NewBuffer(nil)
buf.WriteString(pair.SellersDenomination)
buf.WriteString(pair.BuyersDenomination)
bz := buf.Bytes()
binary.BigEndian.PutUint64(bz, math.Float64bits(o.MaxQuantity))
binary.BigEndian.PutUint64(bz, math.Float64bits(o.MaxPrice))
return bz
}
func (m *MatchedOrder) ValidateBasic() error {
if len(m.OrderAsk.Signature) != ed25519.SignatureSize {
return errors.New("invalid signature size")
if err := m.OrderAsk.ValidateBasic(); err != nil {
return err
}
if len(m.OrderBid.Signature) != ed25519.SignatureSize {
return errors.New("invalid signature size")
if err := m.OrderBid.ValidateBasic(); err != nil {
return err
}
return nil
}
@@ -140,12 +203,26 @@ func (o *OrderAsk) ValidateBasic() error {
if o.AskPrice <= 0 {
return errors.New("min price must be greater than 0")
}
if len(o.Signature) != ed25519.SignatureSize {
return errors.New("invalid signature size")
}
return nil
}
func (o *OrderAsk) ValidateSignature(pk crypto.PubKey, pair *Pair) bool {
return pk.VerifySignature(o.DeterministicSignatureBytes(pair), o.Signature)
}
func (o *OrderAsk) DeterministicSignatureBytes(pair *Pair) []byte {
// buf := bytes.NewBuffer()
return nil
buf := bytes.NewBuffer(nil)
buf.WriteString(pair.BuyersDenomination)
buf.WriteString(pair.SellersDenomination)
bz := buf.Bytes()
binary.BigEndian.PutUint64(bz, math.Float64bits(o.Quantity))
binary.BigEndian.PutUint64(bz, math.Float64bits(o.AskPrice))
return bz
}
func (a *Account) FindCommidity(denom string) *Commodity {