mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-26 19:42:23 +00:00
payload: adapt notary payloads to new hashing too
This commit is contained in:
parent
9d500b86fc
commit
7730aef0ec
2 changed files with 12 additions and 18 deletions
|
@ -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}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue