Skip to content

Commit

Permalink
Add sig validation for open blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
frankh committed Jan 17, 2018
1 parent 21b338e commit 08fa984
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
6 changes: 6 additions & 0 deletions blocks/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ func (*ReceiveBlock) Type() BlockType {
return Receive
}

func (b *OpenBlock) VerifySignature() (bool, error) {
pub, _ := address.AddressToPub(b.Account)
res := ed25519.Verify(pub, b.Hash().ToBytes(), b.Signature.ToBytes())
return res, nil
}

type RawBlock struct {
Type BlockType
Source rai.BlockHash
Expand Down
14 changes: 13 additions & 1 deletion node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var publishReceive, _ = hex.DecodeString("5243050501030003233FF43F2ADE055D4D4BCC
var publishOpen, _ = hex.DecodeString("5243040501030004FBC1F34CF9EF42FB137A909873BD3FDEC047CB8A6D4448B43C0610931E268F012298FAB7C61058E77EA554CB93EDEEDA0692CBFCC540AB213B2836B29029E23A0A3E8B35979AC58F7A0AB42656B28294F5968EB059749EA36BC372DDCDFDBB0134086DB608D63F4A086FD92E0BB4AC6A05926CEC84E4D7D99A86F81D90EA9669A9E02B4E907D5E09491206D76E4787F6F2C26B8FD9932315B10EC005A8B4F60DDA9D288B1C14A4CB")
var publishChange, _ = hex.DecodeString("5243050501030005611A6FA8736497E6C1BD9AE42090F0F646F56B32B6E02F804C2295B3888A2FEDE196157A3B52034755CA905AD0C365B192A40203D8983E077093BCD6C9757A64A772CD1736F8DF3C6E382BDC7EED1D48628A65263CE50B12A603B6782D2C3E5EE2280B3C97ACEA67FF003CA3690B2BBEE160E375D0CAA220109D63ED35BBAD0F1DE013836D3471C1")
var publishWrongMagic, _ = hex.DecodeString("5242050501030005611A6FA8736497E6C1BD9AE42090F0F646F56B32B6E02F804C2295B3888A2FEDE196157A3B52034755CA905AD0C365B192A40203D8983E077093BCD6C9757A64A772CD1736F8DF3C6E382BDC7EED1D48628A65263CE50B12A603B6782D2C3E5EE2280B3C97ACEA67FF003CA3690B2BBEE160E375D0CAA220109D63ED35BBAD0F1DE013836D3471C1")
var publishWrongSig, _ = hex.DecodeString("5242050501030005611A6FA8736497E6C1BD9AE42090F0F646F56B32B6E02F804C2295B3888A2FEDE196157A3B52034755CA905AD0C365B192A40203D8983E077093BCD6C9757A64A772CD1736F8DF3C6E382BDC7EED1D48628A65263CE50B12A603B6782D2C3E5EE2280B3C97ACEA67FF003CA3690B2BBEE160E375D0CAA220109D63ED34BBAD0F1DE013836D3471C1")
var publishWrongSig, _ = hex.DecodeString("5243040501030004FBC1F34CF9EF42FB137A909873BD3FDEC047CB8A6D4448B43C0610931E268F012298FAB7C61058E77EA554CB93EDEEDA0692CBFCC540AB213B2836B29029E23A0A3E8B35979AC58F7A0AB42656B28294F5968EB059749EA36BC372DDCDFDBB0134086DB608D63F4A086FD92E0BB4AC6A05926CEC84E4D7D99A86F81D90EA9669A9E02B4E907D5E09491206D76E4787F6F2C26B8FD9932315B10EC015A8B4F60DDA9D288B1C14A4CB")
var publishWrongWork, _ = hex.DecodeString("5242050501030005611A6FA8736497E6C1BD9AE42090F0F646F56B32B6E02F804C2295B3888A2FEDE196157A3B52034755CA905AD0C365B192A40203D8983E077093BCD6C9757A64A772CD1736F8DF3C6E382BDC7EED1D48628A65263CE50B12A603B6782D2C3E5EE2280B3C97ACEA67FF003CA3690B2BBEE160E375D0CAA220109D63ED34BBAD0F1DE013836D3471C0")

func TestReadWriteMessageBody(t *testing.T) {
Expand Down Expand Up @@ -65,6 +65,12 @@ func validateTestBlock(t *testing.T, b blocks.Block, expectedHash rai.BlockHash)
if !blocks.ValidateBlockWork(b) {
t.Errorf("Bad PoW")
}
if b.Type() == blocks.Open {
passed, _ := b.(*blocks.OpenBlock).VerifySignature()
if !passed {
t.Errorf("Failed to verify signature")
}
}
}

func TestReadPublish(t *testing.T) {
Expand Down Expand Up @@ -96,6 +102,12 @@ func TestReadPublish(t *testing.T) {
if blocks.ValidateBlockWork(m.ToBlock()) {
t.Errorf("Invalid work should fail")
}

m, err = readMessagePublish(bytes.NewBuffer(publishWrongSig))
passed, _ := m.ToBlock().(*blocks.OpenBlock).VerifySignature()
if passed {
t.Errorf("Invalid signature should fail")
}
}

func TestReadWriteHeader(t *testing.T) {
Expand Down
8 changes: 8 additions & 0 deletions rai.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ func (hash BlockHash) ToBytes() []byte {
return bytes
}

func (sig Signature) ToBytes() []byte {
bytes, err := hex.DecodeString(string(sig))
if err != nil {
panic(err)
}
return bytes
}

func (hash BlockHash) Sign(private_key ed25519.PrivateKey) Signature {
sig := hex.EncodeToString(ed25519.Sign(private_key, hash.ToBytes()))
return Signature(strings.ToUpper(sig))
Expand Down

0 comments on commit 08fa984

Please sign in to comment.