fix: hash calculation of message to include timestamp (#959)

This commit is contained in:
kaichao 2023-12-13 22:46:23 +08:00 committed by GitHub
parent c403388ec2
commit 5d1477d5b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 6 deletions

View File

@ -24,7 +24,7 @@ func TestEnvelope(t *testing.T) {
require.Equal(
t,
[]uint8{70, 218, 246, 174, 188, 127, 199, 220, 111, 30, 61, 218, 238, 60, 83, 3, 179, 98, 85, 35, 7, 107, 188, 138, 32, 70, 170, 126, 55, 21, 71, 70},
[]byte{0x91, 0x0, 0xe4, 0xa5, 0xcf, 0xf7, 0x19, 0x27, 0x49, 0x81, 0x66, 0xb3, 0xdf, 0xc7, 0xa6, 0x31, 0xf0, 0x87, 0xc7, 0x29, 0xb4, 0x28, 0x83, 0xb9, 0x5c, 0x31, 0x25, 0x33, 0x3, 0xc9, 0x7, 0x95},
hash,
)
}

View File

@ -1,10 +1,18 @@
package pb
import (
"encoding/binary"
"github.com/waku-org/go-waku/waku/v2/hash"
)
// Hash calculates the hash of a waku message
func (msg *WakuMessage) Hash(pubsubTopic string) []byte {
return hash.SHA256([]byte(pubsubTopic), msg.Payload, []byte(msg.ContentTopic), msg.Meta)
return hash.SHA256([]byte(pubsubTopic), msg.Payload, []byte(msg.ContentTopic), msg.Meta, toBytes(msg.GetTimestamp()))
}
func toBytes(i int64) []byte {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, uint64(i))
return b
}

View File

@ -22,7 +22,7 @@ func TestEnvelopeHash(t *testing.T) {
msg.Timestamp = proto.Int64(123456789123456789)
msg.Version = proto.Uint32(1)
expected := []byte{0xee, 0xcf, 0xf5, 0xb7, 0xdd, 0x54, 0x2d, 0x68, 0x9e, 0x7d, 0x64, 0xa3, 0xb8, 0x50, 0x8b, 0xba, 0xc, 0xf1, 0xac, 0xb6, 0xf7, 0x1c, 0x9f, 0xf2, 0x32, 0x7, 0x5b, 0xfd, 0x90, 0x5c, 0xe5, 0xa1}
expected := []byte{0xb6, 0x59, 0x60, 0x7f, 0x2a, 0xae, 0x18, 0x84, 0x8d, 0xca, 0xa7, 0xd5, 0x1c, 0xb3, 0x7e, 0x6c, 0xc6, 0xfc, 0x33, 0x40, 0x2c, 0x70, 0x4f, 0xf0, 0xc0, 0x16, 0x33, 0x7d, 0x83, 0xad, 0x61, 0x50}
result := msg.Hash("test")
require.Equal(t, expected, result)
}
@ -39,7 +39,7 @@ func TestEmptyMeta(t *testing.T) {
messageHash := msg.Hash(pubsubTopic)
require.Equal(t, "87619d05e563521d9126749b45bd4cc2430df0607e77e23572d874ed9c1aaa62", hex.EncodeToString(messageHash))
require.Equal(t, "f0183c2e370e473ff471bbe1028d0d8a940949c02f3007a1ccd21fed356852a0", hex.EncodeToString(messageHash))
}
func Test13ByteMeta(t *testing.T) {
@ -48,11 +48,12 @@ func Test13ByteMeta(t *testing.T) {
msg.ContentTopic = "/waku/2/default-content/proto"
msg.Payload = []byte("\x01\x02\x03\x04TEST\x05\x06\x07\x08")
msg.Meta = []byte("\x73\x75\x70\x65\x72\x2d\x73\x65\x63\x72\x65\x74")
msg.Timestamp = proto.Int64(123456789123456789)
msg.Version = proto.Uint32(1)
messageHash := msg.Hash(pubsubTopic)
require.Equal(t, "4fdde1099c9f77f6dae8147b6b3179aba1fc8e14a7bf35203fc253ee479f135f", hex.EncodeToString(messageHash))
require.Equal(t, "f673cd2c9c973d685b52ca74c2559e001733a3a31a49ffc7b6e8713decba5a55", hex.EncodeToString(messageHash))
}
func TestZeroLenPayload(t *testing.T) {
@ -61,9 +62,31 @@ func TestZeroLenPayload(t *testing.T) {
msg.ContentTopic = "/waku/2/default-content/proto"
msg.Payload = []byte{}
msg.Meta = []byte("\x73\x75\x70\x65\x72\x2d\x73\x65\x63\x72\x65\x74")
msg.Timestamp = proto.Int64(123456789123456789)
msg.Version = proto.Uint32(1)
messageHash := msg.Hash(pubsubTopic)
require.Equal(t, "e1a9596237dbe2cc8aaf4b838c46a7052df6bc0d42ba214b998a8bfdbe8487d6", hex.EncodeToString(messageHash))
require.Equal(t, "978ccc9a665029f9829d42d84e3a49ad3a4791cce53fb5a8b581ef43ad6b4d2f", hex.EncodeToString(messageHash))
}
func TestHashWithTimestamp(t *testing.T) {
pubsubTopic := "/waku/2/default-waku/proto"
msg := new(WakuMessage)
msg.ContentTopic = "/waku/2/default-content/proto"
msg.Payload = []byte{}
msg.Meta = []byte("\x73\x75\x70\x65\x72\x2d\x73\x65\x63\x72\x65\x74")
msg.Version = proto.Uint32(1)
messageHash := msg.Hash(pubsubTopic)
require.Equal(t, "58e2fc032a82c4adeb967a8b87086d0d6fb304912f120d4404e6236add8f1f56", hex.EncodeToString(messageHash))
msg.Timestamp = proto.Int64(123456789123456789)
messageHash = msg.Hash(pubsubTopic)
require.Equal(t, "978ccc9a665029f9829d42d84e3a49ad3a4791cce53fb5a8b581ef43ad6b4d2f", hex.EncodeToString(messageHash))
}
func TestIntToBytes(t *testing.T) {
require.Equal(t, []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x27, 0x10}, toBytes(10000))
require.Equal(t, []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x98, 0x96, 0x80}, toBytes(10000000))
}