/
stdtx_adapter.go
195 lines (169 loc) · 5.23 KB
/
stdtx_adapter.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
package types
import (
"encoding/json"
"errors"
"fmt"
"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec"
sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/tx/signing"
"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/exported"
"github.com/gogo/protobuf/proto"
)
type IbcTx struct {
*StdTx
AuthInfoBytes []byte
BodyBytes []byte
SignMode []signing.SignMode
SigFee IbcFee
SigMsgs []sdk.Msg
Sequences []uint64
TxBodyHasUnknownNonCriticals bool
HasExtensionOpt bool
Payer string
ValidateParams func() error
}
type StdIBCSignDoc struct {
AccountNumber uint64 `json:"account_number" yaml:"account_number"`
Sequence uint64 `json:"sequence" yaml:"sequence"`
TimeoutHeight uint64 `json:"timeout_height,omitempty" yaml:"timeout_height"`
ChainID string `json:"chain_id" yaml:"chain_id"`
Memo string `json:"memo" yaml:"memo"`
Fee json.RawMessage `json:"fee" yaml:"fee"`
Msgs []json.RawMessage `json:"msgs" yaml:"msgs"`
}
type IbcFee struct {
Amount sdk.CoinAdapters `json:"amount" yaml:"amount"`
Gas uint64 `json:"gas" yaml:"gas"`
}
const (
aminoNonCriticalFieldsError = "protobuf transaction contains unknown non-critical fields. This is a transaction malleability issue and SIGN_MODE_LEGACY_AMINO_JSON cannot be used."
aminoExtentionError = "SIGN_MODE_LEGACY_AMINO_JSON does not support protobuf extension options."
)
func (tx *IbcTx) GetSignBytes(ctx sdk.Context, index int, acc exported.Account) []byte {
genesis := ctx.BlockHeight() == 0
chainID := ctx.ChainID()
var accNum uint64
if !genesis {
accNum = acc.GetAccountNumber()
}
if index > len(tx.SignMode) {
panic(fmt.Sprintf("GetSignBytes index %d is upper than tx.SignMode Length %d", index, len(tx.SignMode)))
}
switch tx.SignMode[index] {
case signing.SignMode_SIGN_MODE_DIRECT:
return IbcDirectSignBytes(
chainID, accNum, acc.GetSequence(), tx.Fee, tx.Msgs, tx.Memo, tx.AuthInfoBytes, tx.BodyBytes,
)
case signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON:
if tx.TxBodyHasUnknownNonCriticals {
panic(aminoNonCriticalFieldsError)
}
if tx.HasExtensionOpt {
panic(aminoExtentionError)
}
return IbcAminoSignBytes(
chainID, accNum, acc.GetSequence(), tx.SigFee, tx.SigMsgs, tx.Memo, 0,
)
case signing.SignMode_SIGN_MODE_UNSPECIFIED:
//Compatible with cosmojs for simulating tx
return nil
default:
//does not other mode
panic(fmt.Sprintf("ibctx not support sign mode: %s", tx.SignMode[index].String()))
}
}
func (tx *IbcTx) ValidateBasic() error {
err := tx.StdTx.ValidateBasic()
if err != nil {
return err
}
err = tx.ValidateParams()
if err != nil {
return err
}
return nil
}
func (tx *IbcTx) VerifySequence(index int, acc exported.Account) error {
//check
if index > len(tx.Sequences) {
return errors.New("verify sequence error index not fit")
}
seq := tx.Sequences[index]
if seq != acc.GetSequence() {
return fmt.Errorf("verify sequence error expected:%d,got:%d", acc.GetSequence(), seq)
}
return nil
}
func IbcAminoSignBytes(chainID string, accNum uint64,
sequence uint64, fee IbcFee, msgs []sdk.Msg,
memo string, height uint64) []byte {
msgsBytes := make([]json.RawMessage, 0, len(msgs))
for _, msg := range msgs {
msgsBytes = append(msgsBytes, json.RawMessage(msg.GetSignBytes()))
}
bz, err := ModuleCdc.MarshalJSON(StdIBCSignDoc{
AccountNumber: accNum,
ChainID: chainID,
Fee: ModuleCdc.MustMarshalJSON(fee),
Memo: memo,
Msgs: msgsBytes,
Sequence: sequence,
TimeoutHeight: height,
})
if err != nil {
return nil
}
return sdk.MustSortJSON(bz)
}
// IbcDirectSignBytes returns the bytes to sign for a transaction.
func IbcDirectSignBytes(chainID string, accnum uint64,
sequence uint64, fee StdFee, msgs []sdk.Msg,
memo string, authInfoBytes []byte, bodyBytes []byte) []byte {
signDoc := SignDoc{
BodyBytes: bodyBytes,
AuthInfoBytes: authInfoBytes,
ChainId: chainID,
AccountNumber: accnum,
}
r, err := signDoc.Marshal()
if err != nil {
return nil
}
return r
}
//////
// /////////
type ProtobufViewMsg struct {
TypeStr string `json:"type"`
Data string `json:"data"`
}
func NewProtobufViewMsg(typeStr string, data string) *ProtobufViewMsg {
return &ProtobufViewMsg{TypeStr: typeStr, Data: data}
}
func (b ProtobufViewMsg) Route() string {
return ""
}
func (b ProtobufViewMsg) Type() string {
return b.TypeStr
}
func (b ProtobufViewMsg) ValidateBasic() error {
return nil
}
func (b ProtobufViewMsg) GetSignBytes() []byte {
return nil
}
func (b ProtobufViewMsg) GetSigners() []sdk.AccAddress {
return nil
}
func FromProtobufTx(cdc *codec.CodecProxy, tx *IbcTx) (*StdTx, error) {
msgs := make([]sdk.Msg, 0)
for _, msg := range tx.GetMsgs() {
m := (interface{})(msg).(sdk.MsgAdapter)
data, err := cdc.GetProtocMarshal().MarshalJSON(m)
if nil != err {
return nil, err
}
msgs = append(msgs, NewProtobufViewMsg("/"+proto.MessageName(m), string(data)))
}
return NewStdTx(msgs, tx.Fee, tx.Signatures, tx.Memo), nil
}