payload: adapt notary payloads to new hashing too

This commit is contained in:
Roman Khimov 2021-03-12 14:14:22 +03:00
parent 9d500b86fc
commit 7730aef0ec
2 changed files with 12 additions and 18 deletions

View file

@ -71,32 +71,27 @@ func (r *P2PNotaryRequest) GetSignedHash() util.Uint256 {
// GetSignedPart returns a part of the payload which must be signed. // GetSignedPart returns a part of the payload which must be signed.
func (r *P2PNotaryRequest) GetSignedPart() []byte { func (r *P2PNotaryRequest) GetSignedPart() []byte {
if r.hash.Equals(util.Uint256{}) {
if r.createHash() != nil {
panic("failed to compute hash!")
}
}
buf := io.NewBufBinWriter() buf := io.NewBufBinWriter()
buf.WriteU32LE(uint32(r.Network)) buf.WriteU32LE(uint32(r.Network))
r.encodeHashableFields(buf.BinWriter) buf.WriteBytes(r.hash[:])
if buf.Err != nil {
return nil
}
return buf.Bytes() return buf.Bytes()
} }
// createHash creates hash of the payload. // createHash creates hash of the payload.
func (r *P2PNotaryRequest) createHash() error { func (r *P2PNotaryRequest) createHash() error {
b := r.GetSignedPart() buf := io.NewBufBinWriter()
if b == nil { r.encodeHashableFields(buf.BinWriter)
return errors.New("failed to serialize hashable data") r.hash = hash.Sha256(buf.Bytes())
} signed := r.GetSignedPart()
r.updateHashes(b) r.signedHash = hash.Sha256(signed)
return nil 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. // DecodeBinaryUnsigned reads payload from w excluding signature.
func (r *P2PNotaryRequest) decodeHashableFields(br *io.BinReader) { func (r *P2PNotaryRequest) decodeHashableFields(br *io.BinReader) {
r.MainTransaction = &transaction.Transaction{Network: r.Network} r.MainTransaction = &transaction.Transaction{Network: r.Network}

View file

@ -6,7 +6,6 @@ import (
"github.com/nspcc-dev/neo-go/internal/random" "github.com/nspcc-dev/neo-go/internal/random"
"github.com/nspcc-dev/neo-go/pkg/config/netmode" "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/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/util"
"github.com/nspcc-dev/neo-go/pkg/vm/opcode" "github.com/nspcc-dev/neo-go/pkg/vm/opcode"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -182,8 +181,8 @@ func TestNotaryRequestBytesFromBytes(t *testing.T) {
VerificationScript: []byte{7, 8, 9}, VerificationScript: []byte{7, 8, 9},
}, },
} }
require.Equal(t, hash.Sha256(p.GetSignedHash().BytesBE()), p.Hash())
_ = p.Hash() // initialize hash caches
bytes, err := p.Bytes() bytes, err := p.Bytes()
require.NoError(t, err) require.NoError(t, err)
actual, err := NewP2PNotaryRequestFromBytes(netmode.UnitTestNet, bytes) actual, err := NewP2PNotaryRequestFromBytes(netmode.UnitTestNet, bytes)