/
signedtransaction.go
201 lines (160 loc) · 4.97 KB
/
signedtransaction.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package types
//go:generate ffjson $GOFILE
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"time"
"github.com/denkhaus/bitshares/config"
"github.com/denkhaus/bitshares/util"
"github.com/denkhaus/logging"
"github.com/pquerna/ffjson/ffjson"
"github.com/juju/errors"
)
const (
TxExpirationDefault = 30 * time.Second
)
//TODO: implement
// @property
// def id(self):
// """ The transaction id of this transaction
// """
// # Store signatures temporarily since they are not part of
// # transaction id
// sigs = self.data["signatures"]
// self.data.pop("signatures", None)
// # Generage Hash of the seriliazed version
// h = hashlib.sha256(bytes(self)).digest()
// # recover signatures
// self.data["signatures"] = sigs
// # Return properly truncated tx hash
// return hexlify(h[:20]).decode("ascii")
type SignedTransactions []SignedTransaction
type SignedTransaction struct {
Transaction
Signatures Signatures `json:"signatures"`
}
func (p SignedTransaction) Marshal(enc *util.TypeEncoder) error {
if err := enc.Encode(p.Transaction); err != nil {
return errors.Annotate(err, "encode Transaction")
}
if err := enc.Encode(p.Signatures); err != nil {
return errors.Annotate(err, "encode Signatures")
}
return nil
}
//SerializeTrx serializes the transaction wihout signatures.
func (p SignedTransaction) SerializeTrx() ([]byte, error) {
var b bytes.Buffer
enc := util.NewTypeEncoder(&b)
if err := enc.Encode(p.Transaction); err != nil {
return nil, errors.Annotate(err, "encode Transaction")
}
return b.Bytes(), nil
}
//ToHex returns th hex representation of the underlying transaction + signatures.
func (p SignedTransaction) ToHex() (string, error) {
var b bytes.Buffer
enc := util.NewTypeEncoder(&b)
if err := enc.Encode(p); err != nil {
return "", errors.Annotate(err, "encode SignedTransaction")
}
return hex.EncodeToString(b.Bytes()), nil
}
//Digest calculates ths sha256 hash of the transaction.
func (tx SignedTransaction) Digest(chain *config.ChainConfig) ([]byte, error) {
if chain == nil {
return nil, ErrChainConfigIsUndefined
}
writer := sha256.New()
rawChainID, err := hex.DecodeString(chain.ID)
if err != nil {
return nil, errors.Annotatef(err, "failed to decode chain ID: %v", chain.ID)
}
// digestChainID := sha256.Sum256(rawChainID)
// util.Dump("digest chainID", hex.EncodeToString(digestChainID[:]))
if _, err := writer.Write(rawChainID); err != nil {
return nil, errors.Annotate(err, "Write [chainID]")
}
rawTrx, err := tx.SerializeTrx()
if err != nil {
return nil, errors.Annotatef(err, "Serialize")
}
// digestTrx := sha256.Sum256(rawTrx)
// util.Dump("digest trx", hex.EncodeToString(digestTrx[:]))
if _, err := writer.Write(rawTrx); err != nil {
return nil, errors.Annotate(err, "Write [trx]")
}
digest := writer.Sum(nil)
// util.Dump("digest trx all", hex.EncodeToString(digest[:]))
return digest[:], nil
}
//NewSignedTransactionWithBlockData creates a new SignedTransaction and initializes
//relevant Blockdata fields and expiration.
func NewSignedTransactionWithBlockData(props *DynamicGlobalProperties) (*SignedTransaction, error) {
prefix, err := props.RefBlockPrefix()
if err != nil {
return nil, errors.Annotate(err, "RefBlockPrefix")
}
tx := SignedTransaction{
Transaction: Transaction{
Extensions: Extensions{},
RefBlockNum: props.RefBlockNum(),
Expiration: props.Time.Add(TxExpirationDefault),
RefBlockPrefix: prefix,
},
Signatures: Signatures{},
}
return &tx, nil
}
//NewSignedTransaction creates an new SignedTransaction
func NewSignedTransaction() *SignedTransaction {
tm := time.Now().UTC().Add(TxExpirationDefault)
tx := SignedTransaction{
Transaction: Transaction{
Extensions: Extensions{},
Expiration: Time{tm},
},
Signatures: Signatures{},
}
return &tx
}
type SignedTransactionWithTransactionId struct {
TransactionId string
SignedTransaction SignedTransaction
}
func (p SignedTransactionWithTransactionId) Marshal(enc *util.TypeEncoder) error {
// type is marshaled by operation
if err := enc.Encode(p); err != nil {
return errors.Annotate(err, "Encode SignedTransactionWithTransactionId")
}
return nil
}
func (p SignedTransactionWithTransactionId) MarshalJSON() ([]byte, error) {
return ffjson.Marshal([]interface{}{
p.TransactionId,
p.SignedTransaction,
})
}
func (p *SignedTransactionWithTransactionId) UnmarshalJSON(data []byte) error {
raw := make([]json.RawMessage, 2)
if err := ffjson.Unmarshal(data, &raw); err != nil {
return errors.Annotate(err, "Unmarshal [raw]")
}
if len(raw) != 2 {
return ErrInvalidInputLength
}
if err := ffjson.Unmarshal(raw[0], &p.TransactionId); err != nil {
return errors.Annotate(err, "Unmarshal [TransactionId]")
}
if err := ffjson.Unmarshal(raw[1], &p.SignedTransaction); err != nil {
logging.DDumpUnmarshaled(
fmt.Sprintf("TransactionId %s", p.TransactionId),
raw[1],
)
return errors.Annotatef(err, "Unmarshal SignedTransaction %v", p.SignedTransaction)
}
return nil
}