2018-03-04 13:56:49 +00:00
|
|
|
package transaction
|
|
|
|
|
|
|
|
import (
|
2020-07-10 17:40:27 +00:00
|
|
|
"encoding/base64"
|
2019-02-20 17:39:32 +00:00
|
|
|
"encoding/json"
|
2020-08-19 13:20:48 +00:00
|
|
|
"errors"
|
2018-03-17 11:53:21 +00:00
|
|
|
"fmt"
|
2018-03-04 13:56:49 +00:00
|
|
|
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2018-03-04 13:56:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Attribute represents a Transaction attribute.
|
|
|
|
type Attribute struct {
|
2020-08-11 17:58:56 +00:00
|
|
|
Type AttrType
|
|
|
|
Data []byte
|
2020-05-13 21:17:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// attrJSON is used for JSON I/O of Attribute.
|
|
|
|
type attrJSON struct {
|
2020-08-11 17:58:56 +00:00
|
|
|
Type string `json:"type"`
|
|
|
|
Data string `json:"data"`
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 16:31:49 +00:00
|
|
|
// DecodeBinary implements Serializable interface.
|
|
|
|
func (attr *Attribute) DecodeBinary(br *io.BinReader) {
|
2020-08-11 17:58:56 +00:00
|
|
|
attr.Type = AttrType(br.ReadB())
|
2019-08-28 16:27:06 +00:00
|
|
|
|
|
|
|
var datasize uint64
|
2020-08-11 17:58:56 +00:00
|
|
|
switch attr.Type {
|
2020-08-19 13:20:48 +00:00
|
|
|
case HighPriority:
|
2019-08-28 16:27:06 +00:00
|
|
|
default:
|
2020-08-11 17:58:56 +00:00
|
|
|
br.Err = fmt.Errorf("failed decoding TX attribute usage: 0x%2x", int(attr.Type))
|
2019-09-16 16:31:49 +00:00
|
|
|
return
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
2019-08-28 16:27:06 +00:00
|
|
|
attr.Data = make([]byte, datasize)
|
2019-12-06 15:37:46 +00:00
|
|
|
br.ReadBytes(attr.Data)
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 16:31:49 +00:00
|
|
|
// EncodeBinary implements Serializable interface.
|
|
|
|
func (attr *Attribute) EncodeBinary(bw *io.BinWriter) {
|
2020-08-11 17:58:56 +00:00
|
|
|
bw.WriteB(byte(attr.Type))
|
|
|
|
switch attr.Type {
|
2020-08-19 13:20:48 +00:00
|
|
|
case HighPriority:
|
2019-08-28 16:27:06 +00:00
|
|
|
default:
|
2020-08-11 17:58:56 +00:00
|
|
|
bw.Err = fmt.Errorf("failed encoding TX attribute usage: 0x%2x", attr.Type)
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-20 17:39:32 +00:00
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// MarshalJSON implements the json Marshaller interface.
|
2019-02-20 17:39:32 +00:00
|
|
|
func (attr *Attribute) MarshalJSON() ([]byte, error) {
|
2020-05-13 21:17:39 +00:00
|
|
|
return json.Marshal(attrJSON{
|
2020-08-19 13:20:48 +00:00
|
|
|
Type: attr.Type.String(),
|
2020-08-11 17:58:56 +00:00
|
|
|
Data: base64.StdEncoding.EncodeToString(attr.Data),
|
2019-02-20 17:39:32 +00:00
|
|
|
})
|
|
|
|
}
|
2020-05-13 21:17:39 +00:00
|
|
|
|
|
|
|
// UnmarshalJSON implements the json.Unmarshaller interface.
|
|
|
|
func (attr *Attribute) UnmarshalJSON(data []byte) error {
|
|
|
|
aj := new(attrJSON)
|
|
|
|
err := json.Unmarshal(data, aj)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-10 17:40:27 +00:00
|
|
|
binData, err := base64.StdEncoding.DecodeString(aj.Data)
|
2020-05-13 21:17:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-11 17:58:56 +00:00
|
|
|
switch aj.Type {
|
2020-08-19 13:20:48 +00:00
|
|
|
case "HighPriority":
|
|
|
|
attr.Type = HighPriority
|
2020-05-13 21:17:39 +00:00
|
|
|
default:
|
2020-08-11 17:58:56 +00:00
|
|
|
return errors.New("wrong Type")
|
2020-05-13 21:17:39 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
attr.Data = binData
|
|
|
|
return nil
|
|
|
|
}
|