mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-26 09:43:49 +00:00
ddd1d92ff1
The idea here is to preserve the history of `dev` branch development and its code when merging with the `master`. Later this code could be moved into the masters code where appropriate.
61 lines
1.5 KiB
Go
61 lines
1.5 KiB
Go
package transaction
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/wire/util"
|
|
)
|
|
|
|
// Attribute represents a Transaction attribute.
|
|
type Attribute struct {
|
|
Usage AttrUsage
|
|
Data []byte
|
|
}
|
|
|
|
var errMaxData = errors.New("max Size of Attribute reached")
|
|
|
|
const maxAttrSize = 65535
|
|
|
|
// Encode encodes the given Attribute into the binary writer
|
|
func (a *Attribute) Encode(bw *util.BinWriter) {
|
|
if len(a.Data) > maxAttrSize {
|
|
bw.Err = errMaxData
|
|
return
|
|
}
|
|
bw.Write(uint8(a.Usage))
|
|
|
|
if a.Usage == ContractHash || a.Usage == Vote || (a.Usage >= Hash1 && a.Usage <= Hash15) {
|
|
bw.Write(a.Data[:32])
|
|
} else if a.Usage == ECDH02 || a.Usage == ECDH03 {
|
|
bw.Write(a.Data[1:33])
|
|
} else if a.Usage == Script {
|
|
bw.Write(a.Data[:20])
|
|
} else if a.Usage == DescriptionURL || a.Usage == Description || a.Usage >= Remark {
|
|
bw.VarUint(uint64(len(a.Data)))
|
|
bw.Write(a.Data)
|
|
} else {
|
|
bw.Write(a.Data)
|
|
}
|
|
|
|
}
|
|
|
|
// Decode decodes the binary reader into an Attribute object
|
|
func (a *Attribute) Decode(br *util.BinReader) {
|
|
br.Read(&a.Usage)
|
|
if a.Usage == ContractHash || a.Usage == Vote || a.Usage >= Hash1 && a.Usage <= Hash15 {
|
|
a.Data = make([]byte, 32)
|
|
br.Read(&a.Data)
|
|
} else if a.Usage == ECDH02 || a.Usage == ECDH03 {
|
|
a.Data = make([]byte, 32)
|
|
br.Read(&a.Data)
|
|
} else if a.Usage == Script {
|
|
a.Data = make([]byte, 20)
|
|
br.Read(&a.Data)
|
|
} else if a.Usage == DescriptionURL || a.Usage == Description || a.Usage >= Remark {
|
|
lenData := br.VarUint()
|
|
a.Data = make([]byte, lenData)
|
|
br.Read(&a.Data)
|
|
} else {
|
|
br.Read(&a.Data)
|
|
}
|
|
}
|