/
transaction_attribute.go
97 lines (84 loc) · 2.74 KB
/
transaction_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
96
97
/*
* Copyright (C) 2021 The poly network Authors
* This file is part of The poly network library.
*
* The poly network is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The poly network is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the poly network. If not, see <http://www.gnu.org/licenses/>.
*/
package types
import (
"bytes"
"errors"
"fmt"
"io"
"github.com/polynetwork/poly/common/serialization"
)
type TransactionAttributeUsage byte
const (
Nonce TransactionAttributeUsage = 0x00
Script TransactionAttributeUsage = 0x20
DescriptionUrl TransactionAttributeUsage = 0x81
Description TransactionAttributeUsage = 0x90
)
func IsValidAttributeType(usage TransactionAttributeUsage) bool {
return usage == Nonce || usage == Script ||
usage == DescriptionUrl || usage == Description
}
type TxAttribute struct {
Usage TransactionAttributeUsage
Data []byte
Size uint32
}
func NewTxAttribute(u TransactionAttributeUsage, d []byte) TxAttribute {
tx := TxAttribute{u, d, 0}
tx.Size = tx.GetSize()
return tx
}
func (u *TxAttribute) GetSize() uint32 {
if u.Usage == DescriptionUrl {
return uint32(len([]byte{(byte(0xff))}) + len([]byte{(byte(0xff))}) + len(u.Data))
}
return 0
}
func (tx *TxAttribute) Serialize(w io.Writer) error {
if err := serialization.WriteUint8(w, byte(tx.Usage)); err != nil {
return fmt.Errorf("Transaction attribute Usage serialization error: %s", err)
}
if !IsValidAttributeType(tx.Usage) {
return errors.New("Unsupported attribute Description.")
}
if err := serialization.WriteVarBytes(w, tx.Data); err != nil {
return fmt.Errorf("Transaction attribute Data serialization error: %s", err)
}
return nil
}
func (tx *TxAttribute) Deserialize(r io.Reader) error {
val, err := serialization.ReadBytes(r, 1)
if err != nil {
return fmt.Errorf("Transaction attribute Usage deserialization error: %s", err)
}
tx.Usage = TransactionAttributeUsage(val[0])
if !IsValidAttributeType(tx.Usage) {
return errors.New("[TxAttribute] Unsupported attribute Description.")
}
tx.Data, err = serialization.ReadVarBytes(r)
if err != nil {
return fmt.Errorf("Transaction attribute Data deserialization error: %s", err)
}
return nil
}
func (tx *TxAttribute) ToArray() []byte {
bf := new(bytes.Buffer)
tx.Serialize(bf)
return bf.Bytes()
}