2020-11-27 10:55:48 +00:00
|
|
|
package payload
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
2022-10-05 07:45:52 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2020-11-27 10:55:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
|
|
|
)
|
|
|
|
|
|
|
|
// P2PNotaryRequest contains main and fallback transactions for the Notary service.
|
|
|
|
type P2PNotaryRequest struct {
|
2021-05-28 11:55:06 +00:00
|
|
|
MainTransaction *transaction.Transaction `json:"maintx"`
|
|
|
|
FallbackTransaction *transaction.Transaction `json:"fallbacktx"`
|
2020-11-27 10:55:48 +00:00
|
|
|
|
|
|
|
Witness transaction.Witness
|
|
|
|
|
2021-03-25 19:18:47 +00:00
|
|
|
hash util.Uint256
|
2020-11-27 10:55:48 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NewP2PNotaryRequestFromBytes decodes a P2PNotaryRequest from the given bytes.
|
2021-03-25 19:18:47 +00:00
|
|
|
func NewP2PNotaryRequestFromBytes(b []byte) (*P2PNotaryRequest, error) {
|
|
|
|
req := &P2PNotaryRequest{}
|
2021-02-08 09:26:02 +00:00
|
|
|
br := io.NewBinReaderFromBuf(b)
|
|
|
|
req.DecodeBinary(br)
|
|
|
|
if br.Err != nil {
|
|
|
|
return nil, br.Err
|
|
|
|
}
|
2021-08-20 19:08:09 +00:00
|
|
|
if br.Len() != 0 {
|
2021-02-08 09:26:02 +00:00
|
|
|
return nil, errors.New("additional data after the payload")
|
|
|
|
}
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bytes returns serialized P2PNotaryRequest payload.
|
|
|
|
func (r *P2PNotaryRequest) Bytes() ([]byte, error) {
|
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
r.EncodeBinary(buf.BinWriter)
|
|
|
|
if buf.Err != nil {
|
|
|
|
return nil, buf.Err
|
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2020-11-27 10:55:48 +00:00
|
|
|
// Hash returns payload's hash.
|
|
|
|
func (r *P2PNotaryRequest) Hash() util.Uint256 {
|
|
|
|
if r.hash.Equals(util.Uint256{}) {
|
|
|
|
if r.createHash() != nil {
|
|
|
|
panic("failed to compute hash!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r.hash
|
|
|
|
}
|
|
|
|
|
|
|
|
// createHash creates hash of the payload.
|
|
|
|
func (r *P2PNotaryRequest) createHash() error {
|
2021-03-12 11:14:22 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
r.encodeHashableFields(buf.BinWriter)
|
|
|
|
r.hash = hash.Sha256(buf.Bytes())
|
2020-11-27 10:55:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// DecodeBinaryUnsigned reads payload from the w excluding signature.
|
2020-11-27 10:55:48 +00:00
|
|
|
func (r *P2PNotaryRequest) decodeHashableFields(br *io.BinReader) {
|
2021-03-25 16:18:01 +00:00
|
|
|
r.MainTransaction = &transaction.Transaction{}
|
|
|
|
r.FallbackTransaction = &transaction.Transaction{}
|
2020-11-27 10:55:48 +00:00
|
|
|
r.MainTransaction.DecodeBinary(br)
|
|
|
|
r.FallbackTransaction.DecodeBinary(br)
|
|
|
|
if br.Err == nil {
|
|
|
|
br.Err = r.isValid()
|
|
|
|
}
|
|
|
|
if br.Err == nil {
|
|
|
|
br.Err = r.createHash()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// DecodeBinary implements the io.Serializable interface.
|
2020-11-27 10:55:48 +00:00
|
|
|
func (r *P2PNotaryRequest) DecodeBinary(br *io.BinReader) {
|
|
|
|
r.decodeHashableFields(br)
|
|
|
|
if br.Err == nil {
|
|
|
|
r.Witness.DecodeBinary(br)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// encodeHashableFields writes payload to the w excluding signature.
|
2020-11-27 10:55:48 +00:00
|
|
|
func (r *P2PNotaryRequest) encodeHashableFields(bw *io.BinWriter) {
|
|
|
|
r.MainTransaction.EncodeBinary(bw)
|
|
|
|
r.FallbackTransaction.EncodeBinary(bw)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// EncodeBinary implements the Serializable interface.
|
2020-11-27 10:55:48 +00:00
|
|
|
func (r *P2PNotaryRequest) EncodeBinary(bw *io.BinWriter) {
|
|
|
|
r.encodeHashableFields(bw)
|
|
|
|
r.Witness.EncodeBinary(bw)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *P2PNotaryRequest) isValid() error {
|
|
|
|
nKeysMain := r.MainTransaction.GetAttributes(transaction.NotaryAssistedT)
|
|
|
|
if len(nKeysMain) == 0 {
|
|
|
|
return errors.New("main transaction should have NotaryAssisted attribute")
|
|
|
|
}
|
|
|
|
if nKeysMain[0].Value.(*transaction.NotaryAssisted).NKeys == 0 {
|
|
|
|
return errors.New("main transaction should have NKeys > 0")
|
|
|
|
}
|
|
|
|
if len(r.FallbackTransaction.Signers) != 2 {
|
|
|
|
return errors.New("fallback transaction should have two signers")
|
|
|
|
}
|
|
|
|
if len(r.FallbackTransaction.Scripts[0].InvocationScript) != 66 ||
|
|
|
|
len(r.FallbackTransaction.Scripts[0].VerificationScript) != 0 ||
|
2022-10-05 07:45:52 +00:00
|
|
|
!bytes.HasPrefix(r.FallbackTransaction.Scripts[0].InvocationScript, []byte{byte(opcode.PUSHDATA1), keys.SignatureLen}) {
|
2020-11-27 10:55:48 +00:00
|
|
|
return errors.New("fallback transaction has invalid dummy Notary witness")
|
|
|
|
}
|
|
|
|
if !r.FallbackTransaction.HasAttribute(transaction.NotValidBeforeT) {
|
|
|
|
return errors.New("fallback transactions should have NotValidBefore attribute")
|
|
|
|
}
|
|
|
|
conflicts := r.FallbackTransaction.GetAttributes(transaction.ConflictsT)
|
|
|
|
if len(conflicts) != 1 {
|
|
|
|
return errors.New("fallback transaction should have one Conflicts attribute")
|
|
|
|
}
|
|
|
|
if conflicts[0].Value.(*transaction.Conflicts).Hash != r.MainTransaction.Hash() {
|
2023-02-20 08:08:07 +00:00
|
|
|
return errors.New("fallback transaction does not conflict with the main transaction")
|
2020-11-27 10:55:48 +00:00
|
|
|
}
|
|
|
|
nKeysFallback := r.FallbackTransaction.GetAttributes(transaction.NotaryAssistedT)
|
|
|
|
if len(nKeysFallback) == 0 {
|
|
|
|
return errors.New("fallback transaction should have NotaryAssisted attribute")
|
|
|
|
}
|
|
|
|
if nKeysFallback[0].Value.(*transaction.NotaryAssisted).NKeys != 0 {
|
|
|
|
return errors.New("fallback transaction should have NKeys = 0")
|
|
|
|
}
|
|
|
|
if r.MainTransaction.ValidUntilBlock != r.FallbackTransaction.ValidUntilBlock {
|
|
|
|
return errors.New("both main and fallback transactions should have the same ValidUntil value")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|