2018-03-04 13:56:49 +00:00
|
|
|
package transaction
|
|
|
|
|
|
|
|
import (
|
2020-03-23 14:31:28 +00:00
|
|
|
"encoding/json"
|
2020-01-29 13:38:58 +00:00
|
|
|
"errors"
|
2020-06-05 13:07:04 +00:00
|
|
|
"math/rand"
|
2019-11-27 10:12:04 +00:00
|
|
|
|
2020-06-18 09:00:51 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
2020-03-23 14:31:28 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2018-03-04 13:56:49 +00:00
|
|
|
)
|
|
|
|
|
Implement rpc server method: sendrawtransaction (#174)
* Added new config attributes: 'SecondsPerBlock','LowPriorityThreshold'
* Added new files:
* Added new method: CompareTo
* Fixed empty Slice case
* Added new methods: LessThan, GreaterThan, Equal, CompareTo
* Added new method: InputIntersection
* Added MaxTransactionSize, GroupOutputByAssetID
* Added ned method: ScriptHash
* Added new method: IsDoubleSpend
* Refactor blockchainer, Added Feer interface, Verify and GetMemPool method
* 1) Added MemPool
2) Added new methods to satisfy the blockchainer interface: IsLowPriority, Verify, GetMemPool
* Added new methods: RelayTxn, RelayDirectly
* Fixed tests
* Implemented RPC server method sendrawtransaction
* Refactor getrawtransaction, sendrawtransaction in separate methods
* Moved 'secondsPerBlock' to config file
* Implemented Kim suggestions:
1) Fixed data race issues
2) refactor Verify method
3) Get rid of unused InputIntersection method due to refactoring Verify method
4) Fixed bug in https://github.com/CityOfZion/neo-go/pull/174#discussion_r264108135
5) minor simplications of the code
* Fixed minor issues related to
1) space
2) getter methods do not need pointer on the receiver
3) error message
4) refactoring CompareTo method in uint256.go
* Fixed small issues
* Use sync.RWMutex instead of sync.Mutex
* Refined (R)Lock/(R)Unlock
* return error instead of bool in Verify methods
2019-03-20 12:30:05 +00:00
|
|
|
const (
|
|
|
|
// MaxTransactionSize is the upper limit size in bytes that a transaction can reach. It is
|
|
|
|
// set to be 102400.
|
|
|
|
MaxTransactionSize = 102400
|
2020-04-15 06:50:13 +00:00
|
|
|
// MaxValidUntilBlockIncrement is the upper increment size of blockhain height in blocs after
|
|
|
|
// exceeding that a transaction should fail validation. It is set to be 2102400.
|
|
|
|
MaxValidUntilBlockIncrement = 2102400
|
2020-04-30 13:00:33 +00:00
|
|
|
// MaxCosigners is maximum number of cosigners that can be contained within a transaction.
|
|
|
|
// It is set to be 16.
|
|
|
|
MaxCosigners = 16
|
Implement rpc server method: sendrawtransaction (#174)
* Added new config attributes: 'SecondsPerBlock','LowPriorityThreshold'
* Added new files:
* Added new method: CompareTo
* Fixed empty Slice case
* Added new methods: LessThan, GreaterThan, Equal, CompareTo
* Added new method: InputIntersection
* Added MaxTransactionSize, GroupOutputByAssetID
* Added ned method: ScriptHash
* Added new method: IsDoubleSpend
* Refactor blockchainer, Added Feer interface, Verify and GetMemPool method
* 1) Added MemPool
2) Added new methods to satisfy the blockchainer interface: IsLowPriority, Verify, GetMemPool
* Added new methods: RelayTxn, RelayDirectly
* Fixed tests
* Implemented RPC server method sendrawtransaction
* Refactor getrawtransaction, sendrawtransaction in separate methods
* Moved 'secondsPerBlock' to config file
* Implemented Kim suggestions:
1) Fixed data race issues
2) refactor Verify method
3) Get rid of unused InputIntersection method due to refactoring Verify method
4) Fixed bug in https://github.com/CityOfZion/neo-go/pull/174#discussion_r264108135
5) minor simplications of the code
* Fixed minor issues related to
1) space
2) getter methods do not need pointer on the receiver
3) error message
4) refactoring CompareTo method in uint256.go
* Fixed small issues
* Use sync.RWMutex instead of sync.Mutex
* Refined (R)Lock/(R)Unlock
* return error instead of bool in Verify methods
2019-03-20 12:30:05 +00:00
|
|
|
)
|
|
|
|
|
2018-03-04 13:56:49 +00:00
|
|
|
// Transaction is a process recorded in the NEO blockchain.
|
|
|
|
type Transaction struct {
|
|
|
|
// The trading version which is currently 0.
|
2020-03-23 14:31:28 +00:00
|
|
|
Version uint8
|
2018-03-04 13:56:49 +00:00
|
|
|
|
2020-04-10 10:41:49 +00:00
|
|
|
// Random number to avoid hash collision.
|
|
|
|
Nonce uint32
|
|
|
|
|
2020-04-16 14:10:42 +00:00
|
|
|
// Address signed the transaction.
|
|
|
|
Sender util.Uint160
|
|
|
|
|
2020-05-08 17:54:24 +00:00
|
|
|
// Fee to be burned.
|
2020-06-23 14:15:35 +00:00
|
|
|
SystemFee int64
|
2020-05-08 17:54:24 +00:00
|
|
|
|
|
|
|
// Fee to be distributed to consensus nodes.
|
2020-06-23 14:15:35 +00:00
|
|
|
NetworkFee int64
|
2020-05-08 17:54:24 +00:00
|
|
|
|
2020-04-15 06:50:13 +00:00
|
|
|
// Maximum blockchain height exceeding which
|
|
|
|
// transaction should fail verification.
|
|
|
|
ValidUntilBlock uint32
|
|
|
|
|
2020-06-05 13:07:04 +00:00
|
|
|
// Code to run in NeoVM for this transaction.
|
|
|
|
Script []byte
|
2018-03-04 13:56:49 +00:00
|
|
|
|
|
|
|
// Transaction attributes.
|
2020-03-23 14:31:28 +00:00
|
|
|
Attributes []Attribute
|
2018-03-04 13:56:49 +00:00
|
|
|
|
2020-04-30 13:00:33 +00:00
|
|
|
// Transaction cosigners (not include Sender).
|
|
|
|
Cosigners []Cosigner
|
|
|
|
|
2018-03-04 13:56:49 +00:00
|
|
|
// The scripts that comes with this transaction.
|
|
|
|
// Scripts exist out of the verification script
|
|
|
|
// and invocation script.
|
2020-03-23 14:31:28 +00:00
|
|
|
Scripts []Witness
|
2018-03-14 09:36:59 +00:00
|
|
|
|
2020-06-18 09:00:51 +00:00
|
|
|
// Network magic number. This one actually is not a part of the
|
|
|
|
// wire-representation of Transaction, but it's absolutely necessary
|
|
|
|
// for correct signing/verification.
|
|
|
|
Network netmode.Magic
|
|
|
|
|
2020-07-03 14:37:01 +00:00
|
|
|
// feePerByte is the ratio of NetworkFee and tx size, used for calculating tx priority.
|
|
|
|
feePerByte int64
|
|
|
|
|
2019-09-23 17:13:44 +00:00
|
|
|
// Hash of the transaction (double SHA256).
|
2018-03-14 09:36:59 +00:00
|
|
|
hash util.Uint256
|
2018-03-17 11:53:21 +00:00
|
|
|
|
2019-09-23 17:13:44 +00:00
|
|
|
// Hash of the transaction used to verify it (single SHA256).
|
|
|
|
verificationHash util.Uint256
|
|
|
|
|
2018-03-17 11:53:21 +00:00
|
|
|
// Trimmed indicates this is a transaction from trimmed
|
|
|
|
// data.
|
2020-03-23 14:31:28 +00:00
|
|
|
Trimmed bool
|
2018-03-17 11:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewTrimmedTX returns a trimmed transaction with only its hash
|
|
|
|
// and Trimmed to true.
|
|
|
|
func NewTrimmedTX(hash util.Uint256) *Transaction {
|
|
|
|
return &Transaction{
|
|
|
|
hash: hash,
|
|
|
|
Trimmed: true,
|
|
|
|
}
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 13:07:04 +00:00
|
|
|
// New returns a new transaction to execute given script and pay given system
|
|
|
|
// fee.
|
2020-06-23 14:15:35 +00:00
|
|
|
func New(network netmode.Magic, script []byte, gas int64) *Transaction {
|
2020-06-05 13:07:04 +00:00
|
|
|
return &Transaction{
|
|
|
|
Version: 0,
|
|
|
|
Nonce: rand.Uint32(),
|
|
|
|
Script: script,
|
|
|
|
SystemFee: gas,
|
|
|
|
Attributes: []Attribute{},
|
|
|
|
Cosigners: []Cosigner{},
|
|
|
|
Scripts: []Witness{},
|
2020-06-18 09:00:51 +00:00
|
|
|
Network: network,
|
2020-06-05 13:07:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// Hash returns the hash of the transaction.
|
2018-03-14 09:36:59 +00:00
|
|
|
func (t *Transaction) Hash() util.Uint256 {
|
2018-03-25 10:45:54 +00:00
|
|
|
if t.hash.Equals(util.Uint256{}) {
|
2019-09-23 17:26:53 +00:00
|
|
|
if t.createHash() != nil {
|
|
|
|
panic("failed to compute hash!")
|
|
|
|
}
|
2018-03-25 10:45:54 +00:00
|
|
|
}
|
2018-03-14 09:36:59 +00:00
|
|
|
return t.hash
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 17:13:44 +00:00
|
|
|
// VerificationHash returns the hash of the transaction used to verify it.
|
|
|
|
func (t *Transaction) VerificationHash() util.Uint256 {
|
|
|
|
if t.verificationHash.Equals(util.Uint256{}) {
|
2019-09-23 17:26:53 +00:00
|
|
|
if t.createHash() != nil {
|
|
|
|
panic("failed to compute hash!")
|
|
|
|
}
|
2019-09-23 17:13:44 +00:00
|
|
|
}
|
|
|
|
return t.verificationHash
|
|
|
|
}
|
|
|
|
|
2020-06-18 08:39:47 +00:00
|
|
|
// decodeHashableFields decodes the fields that are used for signing the
|
|
|
|
// transaction, which are all fields except the scripts.
|
|
|
|
func (t *Transaction) decodeHashableFields(br *io.BinReader) {
|
2019-12-12 17:17:50 +00:00
|
|
|
t.Version = uint8(br.ReadB())
|
2020-06-05 13:07:04 +00:00
|
|
|
if t.Version > 0 {
|
|
|
|
br.Err = errors.New("only version 0 is supported")
|
|
|
|
return
|
|
|
|
}
|
2020-04-10 10:41:49 +00:00
|
|
|
t.Nonce = br.ReadU32LE()
|
2020-05-04 07:35:56 +00:00
|
|
|
t.Sender.DecodeBinary(br)
|
2020-06-23 14:15:35 +00:00
|
|
|
t.SystemFee = int64(br.ReadU64LE())
|
2020-05-08 17:54:24 +00:00
|
|
|
if t.SystemFee < 0 {
|
|
|
|
br.Err = errors.New("negative system fee")
|
|
|
|
return
|
|
|
|
}
|
2020-06-23 14:15:35 +00:00
|
|
|
t.NetworkFee = int64(br.ReadU64LE())
|
2020-05-08 17:54:24 +00:00
|
|
|
if t.NetworkFee < 0 {
|
|
|
|
br.Err = errors.New("negative network fee")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if t.NetworkFee+t.SystemFee < t.SystemFee {
|
|
|
|
br.Err = errors.New("too big fees: int 64 overflow")
|
|
|
|
return
|
|
|
|
}
|
2020-04-15 06:50:13 +00:00
|
|
|
t.ValidUntilBlock = br.ReadU32LE()
|
2018-03-04 13:56:49 +00:00
|
|
|
|
2019-11-14 07:50:03 +00:00
|
|
|
br.ReadArray(&t.Attributes)
|
2020-04-30 13:00:33 +00:00
|
|
|
|
|
|
|
br.ReadArray(&t.Cosigners, MaxCosigners)
|
|
|
|
for i := 0; i < len(t.Cosigners); i++ {
|
|
|
|
for j := i + 1; j < len(t.Cosigners); j++ {
|
|
|
|
if t.Cosigners[i].Account.Equals(t.Cosigners[j].Account) {
|
|
|
|
br.Err = errors.New("transaction cosigners should be unique")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-05 13:07:04 +00:00
|
|
|
t.Script = br.ReadVarBytes()
|
|
|
|
if br.Err == nil && len(t.Script) == 0 {
|
|
|
|
br.Err = errors.New("no script")
|
|
|
|
return
|
|
|
|
}
|
2020-06-18 08:39:47 +00:00
|
|
|
}
|
2020-06-05 13:07:04 +00:00
|
|
|
|
2020-06-18 08:39:47 +00:00
|
|
|
// DecodeBinary implements Serializable interface.
|
|
|
|
func (t *Transaction) DecodeBinary(br *io.BinReader) {
|
|
|
|
t.decodeHashableFields(br)
|
|
|
|
if br.Err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-11-14 07:50:03 +00:00
|
|
|
br.ReadArray(&t.Scripts)
|
2018-03-04 13:56:49 +00:00
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
// Create the hash of the transaction at decode, so we dont need
|
|
|
|
// to do it anymore.
|
2019-09-16 16:31:49 +00:00
|
|
|
if br.Err == nil {
|
|
|
|
br.Err = t.createHash()
|
|
|
|
}
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 16:31:49 +00:00
|
|
|
// EncodeBinary implements Serializable interface.
|
|
|
|
func (t *Transaction) EncodeBinary(bw *io.BinWriter) {
|
|
|
|
t.encodeHashableFields(bw)
|
2019-11-13 07:36:29 +00:00
|
|
|
bw.WriteArray(t.Scripts)
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// encodeHashableFields encodes the fields that are not used for
|
2018-03-14 09:36:59 +00:00
|
|
|
// signing the transaction, which are all fields except the scripts.
|
2019-09-16 16:31:49 +00:00
|
|
|
func (t *Transaction) encodeHashableFields(bw *io.BinWriter) {
|
2020-06-05 13:07:04 +00:00
|
|
|
if len(t.Script) == 0 {
|
|
|
|
bw.Err = errors.New("transaction has no script")
|
2020-01-29 13:38:58 +00:00
|
|
|
return
|
|
|
|
}
|
2019-12-12 17:17:50 +00:00
|
|
|
bw.WriteB(byte(t.Version))
|
2020-04-10 10:41:49 +00:00
|
|
|
bw.WriteU32LE(t.Nonce)
|
2020-05-04 07:35:56 +00:00
|
|
|
t.Sender.EncodeBinary(bw)
|
2020-06-23 14:15:35 +00:00
|
|
|
bw.WriteU64LE(uint64(t.SystemFee))
|
|
|
|
bw.WriteU64LE(uint64(t.NetworkFee))
|
2020-04-15 06:50:13 +00:00
|
|
|
bw.WriteU32LE(t.ValidUntilBlock)
|
2018-03-14 09:36:59 +00:00
|
|
|
|
2018-03-04 13:56:49 +00:00
|
|
|
// Attributes
|
2019-11-13 07:36:29 +00:00
|
|
|
bw.WriteArray(t.Attributes)
|
2018-03-04 13:56:49 +00:00
|
|
|
|
2020-04-30 13:00:33 +00:00
|
|
|
// Cosigners
|
|
|
|
bw.WriteArray(t.Cosigners)
|
|
|
|
|
2020-06-05 13:07:04 +00:00
|
|
|
bw.WriteVarBytes(t.Script)
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
|
|
|
|
2018-03-25 10:45:54 +00:00
|
|
|
// createHash creates the hash of the transaction.
|
|
|
|
func (t *Transaction) createHash() error {
|
2020-06-18 09:00:51 +00:00
|
|
|
b := t.GetSignedPart()
|
|
|
|
if b == nil {
|
|
|
|
return errors.New("failed to serialize hashable data")
|
2018-03-25 10:45:54 +00:00
|
|
|
}
|
2020-06-18 09:00:51 +00:00
|
|
|
t.updateHashes(b)
|
|
|
|
return nil
|
|
|
|
}
|
2018-03-25 10:45:54 +00:00
|
|
|
|
2020-06-18 09:00:51 +00:00
|
|
|
// updateHashes updates Transaction's hashes based on the given buffer which should
|
|
|
|
// be a signable data slice.
|
|
|
|
func (t *Transaction) updateHashes(b []byte) {
|
2019-09-23 17:13:44 +00:00
|
|
|
t.verificationHash = hash.Sha256(b)
|
2019-12-12 15:01:30 +00:00
|
|
|
t.hash = hash.Sha256(t.verificationHash.BytesBE())
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
2018-03-21 16:11:04 +00:00
|
|
|
|
2020-02-28 07:05:13 +00:00
|
|
|
// GetSignedPart returns a part of the transaction which must be signed.
|
|
|
|
func (t *Transaction) GetSignedPart() []byte {
|
|
|
|
buf := io.NewBufBinWriter()
|
2020-06-18 09:00:51 +00:00
|
|
|
buf.WriteU32LE(uint32(t.Network))
|
2020-02-28 07:05:13 +00:00
|
|
|
t.encodeHashableFields(buf.BinWriter)
|
|
|
|
if buf.Err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return buf.Bytes()
|
|
|
|
}
|
|
|
|
|
2020-06-18 08:39:47 +00:00
|
|
|
// DecodeSignedPart decodes a part of transaction from GetSignedPart data.
|
|
|
|
func (t *Transaction) DecodeSignedPart(buf []byte) error {
|
|
|
|
r := io.NewBinReaderFromBuf(buf)
|
2020-06-18 09:00:51 +00:00
|
|
|
t.Network = netmode.Magic(r.ReadU32LE())
|
2020-06-18 08:39:47 +00:00
|
|
|
t.decodeHashableFields(r)
|
|
|
|
if r.Err != nil {
|
|
|
|
return r.Err
|
|
|
|
}
|
|
|
|
// Ensure all the data was read.
|
|
|
|
_ = r.ReadB()
|
|
|
|
if r.Err == nil {
|
|
|
|
return errors.New("additional data after the signed part")
|
|
|
|
}
|
|
|
|
t.Scripts = make([]Witness, 0)
|
2020-06-18 09:00:51 +00:00
|
|
|
t.updateHashes(buf)
|
2020-06-18 08:39:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// Bytes converts the transaction to []byte
|
2019-02-20 17:39:32 +00:00
|
|
|
func (t *Transaction) Bytes() []byte {
|
2019-09-16 09:18:13 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
2019-09-16 16:31:49 +00:00
|
|
|
t.EncodeBinary(buf.BinWriter)
|
|
|
|
if buf.Err != nil {
|
2019-02-20 17:39:32 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return buf.Bytes()
|
|
|
|
}
|
2020-03-23 14:31:28 +00:00
|
|
|
|
2020-04-13 09:02:18 +00:00
|
|
|
// NewTransactionFromBytes decodes byte array into *Transaction
|
2020-06-18 09:00:51 +00:00
|
|
|
func NewTransactionFromBytes(network netmode.Magic, b []byte) (*Transaction, error) {
|
|
|
|
tx := &Transaction{Network: network}
|
2020-04-13 09:02:18 +00:00
|
|
|
r := io.NewBinReaderFromBuf(b)
|
|
|
|
tx.DecodeBinary(r)
|
|
|
|
if r.Err != nil {
|
|
|
|
return nil, r.Err
|
|
|
|
}
|
2020-07-03 14:52:44 +00:00
|
|
|
_ = r.ReadB()
|
|
|
|
if r.Err == nil {
|
|
|
|
return nil, errors.New("additional data after the transaction")
|
|
|
|
}
|
2020-07-03 14:40:03 +00:00
|
|
|
tx.feePerByte = tx.NetworkFee / int64(len(b))
|
2020-04-13 09:02:18 +00:00
|
|
|
return tx, nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:54:24 +00:00
|
|
|
// FeePerByte returns NetworkFee of the transaction divided by
|
|
|
|
// its size
|
2020-06-23 14:15:35 +00:00
|
|
|
func (t *Transaction) FeePerByte() int64 {
|
2020-07-03 14:37:01 +00:00
|
|
|
if t.feePerByte != 0 {
|
|
|
|
return t.feePerByte
|
|
|
|
}
|
|
|
|
t.feePerByte = t.NetworkFee / int64(io.GetVarSize(t))
|
|
|
|
return t.feePerByte
|
2020-05-08 17:54:24 +00:00
|
|
|
}
|
|
|
|
|
2020-03-23 14:31:28 +00:00
|
|
|
// transactionJSON is a wrapper for Transaction and
|
|
|
|
// used for correct marhalling of transaction.Data
|
|
|
|
type transactionJSON struct {
|
2020-06-24 06:03:10 +00:00
|
|
|
TxID util.Uint256 `json:"hash"`
|
2020-04-15 06:50:13 +00:00
|
|
|
Size int `json:"size"`
|
|
|
|
Version uint8 `json:"version"`
|
|
|
|
Nonce uint32 `json:"nonce"`
|
2020-04-16 14:10:42 +00:00
|
|
|
Sender string `json:"sender"`
|
2020-07-09 13:47:50 +00:00
|
|
|
SystemFee int64 `json:"sysfee,string"`
|
|
|
|
NetworkFee int64 `json:"netfee,string"`
|
|
|
|
ValidUntilBlock uint32 `json:"validuntilblock"`
|
2020-04-15 06:50:13 +00:00
|
|
|
Attributes []Attribute `json:"attributes"`
|
2020-04-30 13:00:33 +00:00
|
|
|
Cosigners []Cosigner `json:"cosigners"`
|
2020-06-05 12:50:26 +00:00
|
|
|
Script []byte `json:"script"`
|
2020-07-09 13:47:50 +00:00
|
|
|
Scripts []Witness `json:"witnesses"`
|
2020-03-23 14:31:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements json.Marshaler interface.
|
|
|
|
func (t *Transaction) MarshalJSON() ([]byte, error) {
|
|
|
|
tx := transactionJSON{
|
2020-04-15 06:50:13 +00:00
|
|
|
TxID: t.Hash(),
|
|
|
|
Size: io.GetVarSize(t),
|
|
|
|
Version: t.Version,
|
|
|
|
Nonce: t.Nonce,
|
2020-04-16 14:10:42 +00:00
|
|
|
Sender: address.Uint160ToString(t.Sender),
|
2020-04-15 06:50:13 +00:00
|
|
|
ValidUntilBlock: t.ValidUntilBlock,
|
|
|
|
Attributes: t.Attributes,
|
2020-04-30 13:00:33 +00:00
|
|
|
Cosigners: t.Cosigners,
|
2020-06-05 12:50:26 +00:00
|
|
|
Script: t.Script,
|
2020-04-15 06:50:13 +00:00
|
|
|
Scripts: t.Scripts,
|
2020-05-08 17:54:24 +00:00
|
|
|
SystemFee: t.SystemFee,
|
|
|
|
NetworkFee: t.NetworkFee,
|
2020-03-23 14:31:28 +00:00
|
|
|
}
|
|
|
|
return json.Marshal(tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements json.Unmarshaler interface.
|
|
|
|
func (t *Transaction) UnmarshalJSON(data []byte) error {
|
|
|
|
tx := new(transactionJSON)
|
|
|
|
if err := json.Unmarshal(data, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t.Version = tx.Version
|
2020-04-10 10:41:49 +00:00
|
|
|
t.Nonce = tx.Nonce
|
2020-04-15 06:50:13 +00:00
|
|
|
t.ValidUntilBlock = tx.ValidUntilBlock
|
2020-03-23 14:31:28 +00:00
|
|
|
t.Attributes = tx.Attributes
|
2020-04-30 13:00:33 +00:00
|
|
|
t.Cosigners = tx.Cosigners
|
2020-03-23 14:31:28 +00:00
|
|
|
t.Scripts = tx.Scripts
|
2020-05-08 17:54:24 +00:00
|
|
|
t.SystemFee = tx.SystemFee
|
|
|
|
t.NetworkFee = tx.NetworkFee
|
2020-04-16 14:10:42 +00:00
|
|
|
sender, err := address.StringToUint160(tx.Sender)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("cannot unmarshal tx: bad sender")
|
|
|
|
}
|
|
|
|
t.Sender = sender
|
2020-06-05 12:50:26 +00:00
|
|
|
t.Script = tx.Script
|
2020-03-23 14:31:28 +00:00
|
|
|
if t.Hash() != tx.TxID {
|
|
|
|
return errors.New("txid doesn't match transaction hash")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|