-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.go
142 lines (120 loc) · 4.27 KB
/
utils.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
package rosetta
import (
"encoding/json"
"fmt"
"strconv"
"time"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/timestamppb"
v1beta1 "cosmossdk.io/api/cosmos/base/v1beta1"
txv1beta1 "cosmossdk.io/api/cosmos/tx/v1beta1"
signing2 "cosmossdk.io/x/tx/signing"
sdk "github.com/cosmos/cosmos-sdk/types"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
crgerrs "github.com/cosmos/rosetta/lib/errors"
)
// timeToMilliseconds converts time to milliseconds timestamp
func timeToMilliseconds(t time.Time) int64 {
return t.UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond))
}
// unmarshalMetadata unmarshals the given meta to the target
func unmarshalMetadata(meta map[string]interface{}, target interface{}) error {
b, err := json.Marshal(meta)
if err != nil {
return crgerrs.WrapError(crgerrs.ErrCodec, fmt.Sprintf("marshaling meta on json %s", err.Error()))
}
err = json.Unmarshal(b, target)
if err != nil {
return crgerrs.WrapError(crgerrs.ErrCodec, fmt.Sprintf("unmarshaling json target %s", err.Error()))
}
return nil
}
// marshalMetadata marshals the given interface to map[string]interface{}
func marshalMetadata(o interface{}) (meta map[string]interface{}, err error) {
b, err := json.Marshal(o)
if err != nil {
return nil, crgerrs.WrapError(crgerrs.ErrCodec, fmt.Sprintf("marshaling object into json %s", err.Error()))
}
meta = make(map[string]interface{})
err = json.Unmarshal(b, &meta)
if err != nil {
return nil, crgerrs.WrapError(crgerrs.ErrCodec, fmt.Sprintf("unmarshaling json into meta %s", err.Error()))
}
return
}
func parseSignerData(signerData authsigning.SignerData) signing2.SignerData {
parsedSignerDataPublicKey := anypb.Any{
TypeUrl: sdk.MsgTypeURL(signerData.PubKey),
Value: signerData.PubKey.Bytes(),
}
return signing2.SignerData{Address: strconv.FormatUint(signerData.AccountNumber, 10), ChainID: signerData.ChainID, AccountNumber: signerData.AccountNumber, Sequence: signerData.Sequence, PubKey: &parsedSignerDataPublicKey}
}
func parseSignerInfo(signerData signing2.SignerData) []*txv1beta1.SignerInfo {
parsedSignerInfo := []*txv1beta1.SignerInfo{}
signerInfo := &txv1beta1.SignerInfo{
PublicKey: signerData.PubKey,
ModeInfo: nil,
Sequence: signerData.Sequence,
}
parsedSignerInfo = append(parsedSignerInfo, signerInfo)
return parsedSignerInfo
}
func parseTxMessages(tx authsigning.Tx) ([]*anypb.Any, error) {
var parsedTxMsgs []*anypb.Any
txPubKeys, err := tx.GetPubKeys()
if err != nil {
return nil, crgerrs.WrapError(crgerrs.ErrBadArgument, fmt.Sprintf("Getting pub keys from tx %s", err.Error()))
}
for _, txPubKey := range txPubKeys {
parsedPubKey := anypb.Any{
TypeUrl: sdk.MsgTypeURL(txPubKey),
Value: txPubKey.Bytes(),
}
parsedTxMsgs = append(parsedTxMsgs, &parsedPubKey)
}
return parsedTxMsgs, nil
}
func parseFeeAmount(tx authsigning.Tx) []*v1beta1.Coin {
parsedFeeAmount := []*v1beta1.Coin{}
for _, denom := range tx.GetFee().Denoms() {
parsedFeeAmount = append(parsedFeeAmount, &v1beta1.Coin{
Denom: denom,
Amount: tx.GetFee().AmountOf(denom).String(),
})
}
return parsedFeeAmount
}
func parseAuthInfo(tx authsigning.Tx, signerData signing2.SignerData) *txv1beta1.AuthInfo {
parsedFeeAmount := parseFeeAmount(tx)
parsedSignerInfo := parseSignerInfo(signerData)
return &txv1beta1.AuthInfo{
SignerInfos: parsedSignerInfo,
Fee: &txv1beta1.Fee{
Amount: parsedFeeAmount,
GasLimit: tx.GetGas(),
Payer: string(tx.FeePayer()),
Granter: string(tx.FeeGranter()),
},
}
}
func parseTxData(tx authsigning.Tx, signerData signing2.SignerData) (*signing2.TxData, error) {
parsedTxMsgs, err := parseTxMessages(tx)
if err != nil {
return nil, crgerrs.WrapError(crgerrs.ErrCodec, fmt.Sprintf("parsing tx data %s", err.Error()))
}
// todo: timeouthecigh
txData := signing2.TxData{
Body: &txv1beta1.TxBody{
Messages: parsedTxMsgs,
Memo: tx.GetMemo(),
TimeoutTimestamp: timestamppb.New(tx.GetTimeoutTimeStamp()),
ExtensionOptions: nil,
NonCriticalExtensionOptions: nil,
},
AuthInfo: parseAuthInfo(tx, signerData),
BodyBytes: nil,
AuthInfoBytes: nil,
BodyHasUnknownNonCriticals: false,
}
return &txData, nil
}