From 7730aef0eca80df92a1692154fbba19dd0141f68 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 12 Mar 2021 14:14:22 +0300 Subject: [PATCH] payload: adapt notary payloads to new hashing too --- pkg/network/payload/notary_request.go | 27 +++++++++------------- pkg/network/payload/notary_request_test.go | 3 +-- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/pkg/network/payload/notary_request.go b/pkg/network/payload/notary_request.go index 7c4095bc7..8dcd5e1ad 100644 --- a/pkg/network/payload/notary_request.go +++ b/pkg/network/payload/notary_request.go @@ -71,32 +71,27 @@ func (r *P2PNotaryRequest) GetSignedHash() util.Uint256 { // GetSignedPart returns a part of the payload which must be signed. func (r *P2PNotaryRequest) GetSignedPart() []byte { + if r.hash.Equals(util.Uint256{}) { + if r.createHash() != nil { + panic("failed to compute hash!") + } + } buf := io.NewBufBinWriter() buf.WriteU32LE(uint32(r.Network)) - r.encodeHashableFields(buf.BinWriter) - if buf.Err != nil { - return nil - } + buf.WriteBytes(r.hash[:]) return buf.Bytes() } // createHash creates hash of the payload. func (r *P2PNotaryRequest) createHash() error { - b := r.GetSignedPart() - if b == nil { - return errors.New("failed to serialize hashable data") - } - r.updateHashes(b) + buf := io.NewBufBinWriter() + r.encodeHashableFields(buf.BinWriter) + r.hash = hash.Sha256(buf.Bytes()) + signed := r.GetSignedPart() + r.signedHash = hash.Sha256(signed) return nil } -// updateHashes updates Payload's hashes based on the given buffer which should -// be a signable data slice. -func (r *P2PNotaryRequest) updateHashes(b []byte) { - r.signedHash = hash.Sha256(b) - r.hash = hash.Sha256(r.signedHash.BytesBE()) -} - // DecodeBinaryUnsigned reads payload from w excluding signature. func (r *P2PNotaryRequest) decodeHashableFields(br *io.BinReader) { r.MainTransaction = &transaction.Transaction{Network: r.Network} diff --git a/pkg/network/payload/notary_request_test.go b/pkg/network/payload/notary_request_test.go index 5c29bb294..7f82175cb 100644 --- a/pkg/network/payload/notary_request_test.go +++ b/pkg/network/payload/notary_request_test.go @@ -6,7 +6,6 @@ import ( "github.com/nspcc-dev/neo-go/internal/random" "github.com/nspcc-dev/neo-go/pkg/config/netmode" "github.com/nspcc-dev/neo-go/pkg/core/transaction" - "github.com/nspcc-dev/neo-go/pkg/crypto/hash" "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/vm/opcode" "github.com/stretchr/testify/require" @@ -182,8 +181,8 @@ func TestNotaryRequestBytesFromBytes(t *testing.T) { VerificationScript: []byte{7, 8, 9}, }, } - require.Equal(t, hash.Sha256(p.GetSignedHash().BytesBE()), p.Hash()) + _ = p.Hash() // initialize hash caches bytes, err := p.Bytes() require.NoError(t, err) actual, err := NewP2PNotaryRequestFromBytes(netmode.UnitTestNet, bytes)