forked from aergoio/aergo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base58addr.go
329 lines (297 loc) · 7.45 KB
/
base58addr.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package util
import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"math/big"
"strconv"
"time"
"github.com/aergoio/aergo/types"
"github.com/anaskhan96/base58check"
"github.com/mr-tron/base58/base58"
)
type InOutTx struct {
Hash string
Body *InOutTxBody
}
type InOutTxBody struct {
Nonce uint64
Account string
Recipient string
Amount string
Payload string
GasLimit uint64
GasPrice string
Type types.TxType
ChainIdHash string
Sign string
}
type InOutTxIdx struct {
BlockHash string
Idx int32
}
type InOutTxInBlock struct {
TxIdx *InOutTxIdx
Tx *InOutTx
}
type InOutBlockHeader struct {
ChainID string
PrevBlockHash string
BlockNo uint64
Timestamp int64
BlockRootHash string
TxRootHash string
ReceiptsRootHash string
Confirms uint64
PubKey string
Sign string
CoinbaseAccount string
}
type InOutBlockBody struct {
Txs []*InOutTx
}
type InOutBlock struct {
Hash string
Header InOutBlockHeader
Body InOutBlockBody
}
type InOutBlockIdx struct {
BlockHash string
BlockNo uint64
}
type InOutPeerAddress struct {
Address string
Port string
PeerId string
}
type InOutPeer struct {
Address InOutPeerAddress
BestBlock InOutBlockIdx
LastCheck time.Time
State string
Hidden bool
Self bool
}
func FillTxBody(source *InOutTxBody, target *types.TxBody) error {
var err error
if source == nil {
return errors.New("tx body is empty")
}
target.Nonce = source.Nonce
if source.Account != "" {
target.Account, err = types.DecodeAddress(source.Account)
if err != nil {
return err
}
}
if source.Recipient != "" {
target.Recipient, err = types.DecodeAddress(source.Recipient)
if err != nil {
return err
}
}
if source.Amount != "" {
amount, err := ParseUnit(source.Amount)
if err != nil {
return err
}
target.Amount = amount.Bytes()
}
if source.Payload != "" {
target.Payload, err = base58.Decode(source.Payload)
if err != nil {
return err
}
}
target.GasLimit = source.GasLimit
if source.GasPrice != "" {
price, err := ParseUnit(source.GasPrice)
if err != nil {
return err
}
target.GasPrice = price.Bytes()
}
if source.ChainIdHash != "" {
target.ChainIdHash, err = base58.Decode(source.ChainIdHash)
if err != nil {
return err
}
}
if source.Sign != "" {
target.Sign, err = base58.Decode(source.Sign)
if err != nil {
return err
}
}
target.Type = source.Type
return nil
}
func ParseBase58Tx(jsonTx []byte) ([]*types.Tx, error) {
var inputlist []InOutTx
err := json.Unmarshal([]byte(jsonTx), &inputlist)
if err != nil {
var input InOutTx
err = json.Unmarshal([]byte(jsonTx), &input)
if err != nil {
return nil, err
}
inputlist = append(inputlist, input)
}
txs := make([]*types.Tx, len(inputlist))
for i, in := range inputlist {
tx := &types.Tx{Body: &types.TxBody{}}
if in.Hash != "" {
tx.Hash, err = base58.Decode(in.Hash)
if err != nil {
return nil, err
}
}
err = FillTxBody(in.Body, tx.Body)
if err != nil {
return nil, err
}
txs[i] = tx
}
return txs, nil
}
func ParseBase58TxBody(jsonTx []byte) (*types.TxBody, error) {
body := &types.TxBody{}
in := &InOutTxBody{}
err := json.Unmarshal(jsonTx, in)
if err != nil {
return nil, err
}
err = FillTxBody(in, body)
if err != nil {
return nil, err
}
return body, nil
}
func ConvTx(tx *types.Tx) *InOutTx {
out := &InOutTx{Body: &InOutTxBody{}}
if tx == nil {
return out
}
out.Hash = base58.Encode(tx.Hash)
out.Body.Nonce = tx.Body.Nonce
if tx.Body.Account != nil {
out.Body.Account = types.EncodeAddress(tx.Body.Account)
}
if tx.Body.Recipient != nil {
out.Body.Recipient = types.EncodeAddress(tx.Body.Recipient)
}
out.Body.Amount = new(big.Int).SetBytes(tx.Body.Amount).String()
out.Body.Payload = base58.Encode(tx.Body.Payload)
out.Body.GasLimit = tx.Body.GasLimit
out.Body.GasPrice = new(big.Int).SetBytes(tx.Body.GasPrice).String()
out.Body.ChainIdHash = base58.Encode(tx.Body.ChainIdHash)
out.Body.Sign = base58.Encode(tx.Body.Sign)
out.Body.Type = tx.Body.Type
return out
}
func ConvTxInBlock(txInBlock *types.TxInBlock) *InOutTxInBlock {
out := &InOutTxInBlock{TxIdx: &InOutTxIdx{}, Tx: &InOutTx{}}
out.TxIdx.BlockHash = base58.Encode(txInBlock.GetTxIdx().GetBlockHash())
out.TxIdx.Idx = txInBlock.GetTxIdx().GetIdx()
out.Tx = ConvTx(txInBlock.GetTx())
return out
}
func ConvBlock(b *types.Block) *InOutBlock {
out := &InOutBlock{}
if b != nil {
out.Hash = base58.Encode(b.Hash)
out.Header.ChainID = base58.Encode(b.GetHeader().GetChainID())
out.Header.PrevBlockHash = base58.Encode(b.GetHeader().GetPrevBlockHash())
out.Header.BlockNo = b.GetHeader().GetBlockNo()
out.Header.Timestamp = b.GetHeader().GetTimestamp()
out.Header.BlockRootHash = base58.Encode(b.GetHeader().GetBlocksRootHash())
out.Header.TxRootHash = base58.Encode(b.GetHeader().GetTxsRootHash())
out.Header.ReceiptsRootHash = base58.Encode(b.GetHeader().GetReceiptsRootHash())
out.Header.Confirms = b.GetHeader().GetConfirms()
out.Header.PubKey = base58.Encode(b.GetHeader().GetPubKey())
out.Header.Sign = base58.Encode(b.GetHeader().GetSign())
out.Header.CoinbaseAccount = base58.Encode(b.GetHeader().GetCoinbaseAccount())
if b.Body != nil {
for _, tx := range b.Body.Txs {
out.Body.Txs = append(out.Body.Txs, ConvTx(tx))
}
}
}
return out
}
func ConvPeer(p *types.Peer) *InOutPeer {
out := &InOutPeer{}
out.Address.Address = p.GetAddress().GetAddress()
out.Address.Port = strconv.Itoa(int(p.GetAddress().GetPort()))
out.Address.PeerId = base58.Encode(p.GetAddress().GetPeerID())
out.LastCheck = time.Unix(0, p.GetLashCheck())
out.BestBlock.BlockNo = p.GetBestblock().GetBlockNo()
out.BestBlock.BlockHash = base58.Encode(p.GetBestblock().GetBlockHash())
out.State = types.PeerState(p.State).String()
out.Hidden = p.Hidden
out.Self = p.Selfpeer
return out
}
func ConvBlockchainStatus(in *types.BlockchainStatus) string {
out := &InOutBlockchainStatus{}
if in == nil {
return ""
}
out.Hash = base58.Encode(in.BestBlockHash)
out.Height = in.BestHeight
if len(in.ConsensusInfo) > 0 {
ci := json.RawMessage(in.ConsensusInfo)
out.ConsensusInfo = &ci
}
out.ChainIdHash = base58.Encode(in.BestChainIdHash)
jsonout, err := json.Marshal(out)
if err != nil {
return ""
}
return string(jsonout)
}
func TxConvBase58Addr(tx *types.Tx) string {
return toString(ConvTx(tx))
}
func TxInBlockConvBase58Addr(txInBlock *types.TxInBlock) string {
return toString(ConvTxInBlock(txInBlock))
}
func BlockConvBase58Addr(b *types.Block) string {
return toString(ConvBlock(b))
}
func PeerListToString(p *types.PeerList) string {
peers := []*InOutPeer{}
for _, peer := range p.GetPeers() {
peers = append(peers, ConvPeer(peer))
}
return toString(peers)
}
func toString(out interface{}) string {
jsonout, err := json.MarshalIndent(out, "", " ")
if err != nil {
return ""
}
return string(jsonout)
}
const CodeVersion = 0xC0
func EncodeCode(code []byte) string {
encoded, _ := base58check.Encode(fmt.Sprintf("%x", CodeVersion), hex.EncodeToString(code))
return encoded
}
func DecodeCode(encodedCode string) ([]byte, error) {
decodedString, err := base58check.Decode(encodedCode)
if err != nil {
return nil, err
}
decodedBytes, err := hex.DecodeString(decodedString)
if err != nil {
return nil, err
}
version := decodedBytes[0]
if version != CodeVersion {
return nil, errors.New("Invalid code version")
}
decoded := decodedBytes[1:]
return decoded, nil
}