forked from elastos/Elastos.ELA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attribute.go
95 lines (83 loc) · 2.19 KB
/
attribute.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package core
import (
"errors"
"fmt"
"io"
. "github.com/elastos/Elastos.ELA.Utility/common"
)
type AttributeUsage byte
const (
Nonce AttributeUsage = 0x00
Script AttributeUsage = 0x20
DescriptionUrl AttributeUsage = 0x81
Description AttributeUsage = 0x90
Memo AttributeUsage = 0x91
)
func (self AttributeUsage) Name() string {
switch self {
case Nonce:
return "Nonce"
case Script:
return "Script"
case DescriptionUrl:
return "DescriptionUrl"
case Description:
return "Description"
default:
return "Unknown"
}
}
func IsValidAttributeType(usage AttributeUsage) bool {
return usage == Nonce || usage == Script ||
usage == DescriptionUrl || usage == Description || usage == Memo
}
type Attribute struct {
Usage AttributeUsage
Data []byte
Size uint32
}
func (self Attribute) String() string {
return "Attribute: {\n\t\t" +
"Usage: " + self.Usage.Name() + "\n\t\t" +
"Data: " + BytesToHexString(self.Data) + "\n\t\t" +
"Size: " + fmt.Sprint(self.Size) + "\n\t" +
"}"
}
func NewAttribute(u AttributeUsage, d []byte) Attribute {
tx := Attribute{u, d, 0}
tx.Size = tx.GetSize()
return tx
}
func (u *Attribute) GetSize() uint32 {
if u.Usage == DescriptionUrl {
return uint32(len([]byte{(byte(0xff))}) + len([]byte{(byte(0xff))}) + len(u.Data))
}
return 0
}
func (tx *Attribute) Serialize(w io.Writer) error {
if err := WriteUint8(w, byte(tx.Usage)); err != nil {
return errors.New("Transaction attribute Usage serialization error.")
}
if !IsValidAttributeType(tx.Usage) {
return errors.New("[Attribute error] Unsupported attribute Description.")
}
if err := WriteVarBytes(w, tx.Data); err != nil {
return errors.New("Transaction attribute Data serialization error.")
}
return nil
}
func (tx *Attribute) Deserialize(r io.Reader) error {
val, err := ReadBytes(r, 1)
if err != nil {
return errors.New("Transaction attribute Usage deserialization error.")
}
tx.Usage = AttributeUsage(val[0])
if !IsValidAttributeType(tx.Usage) {
return errors.New("[Attribute error] Unsupported attribute Description.")
}
tx.Data, err = ReadVarBytes(r)
if err != nil {
return errors.New("Transaction attribute Data deserialization error.")
}
return nil
}