neoneo-go/pkg/core/transaction/attribute.go
Evgenii Stratonikov 2661ebd295 transaction: add HighPriority attribute
HighPriority attributes specifies that transaction was
signed by a committee.
2020-08-23 09:39:46 +03:00

77 lines
1.6 KiB
Go

package transaction
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"github.com/nspcc-dev/neo-go/pkg/io"
)
// Attribute represents a Transaction attribute.
type Attribute struct {
Type AttrType
Data []byte
}
// attrJSON is used for JSON I/O of Attribute.
type attrJSON struct {
Type string `json:"type"`
Data string `json:"data"`
}
// DecodeBinary implements Serializable interface.
func (attr *Attribute) DecodeBinary(br *io.BinReader) {
attr.Type = AttrType(br.ReadB())
var datasize uint64
switch attr.Type {
case HighPriority:
default:
br.Err = fmt.Errorf("failed decoding TX attribute usage: 0x%2x", int(attr.Type))
return
}
attr.Data = make([]byte, datasize)
br.ReadBytes(attr.Data)
}
// EncodeBinary implements Serializable interface.
func (attr *Attribute) EncodeBinary(bw *io.BinWriter) {
bw.WriteB(byte(attr.Type))
switch attr.Type {
case HighPriority:
default:
bw.Err = fmt.Errorf("failed encoding TX attribute usage: 0x%2x", attr.Type)
}
}
// MarshalJSON implements the json Marshaller interface.
func (attr *Attribute) MarshalJSON() ([]byte, error) {
return json.Marshal(attrJSON{
Type: attr.Type.String(),
Data: base64.StdEncoding.EncodeToString(attr.Data),
})
}
// 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
}
binData, err := base64.StdEncoding.DecodeString(aj.Data)
if err != nil {
return err
}
switch aj.Type {
case "HighPriority":
attr.Type = HighPriority
default:
return errors.New("wrong Type")
}
attr.Data = binData
return nil
}