-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathstdsign.go
187 lines (161 loc) · 5.24 KB
/
stdsign.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
package legacytx
import (
"encoding/json"
"errors"
"fmt"
gogoprotoany "github.com/cosmos/gogoproto/types/any"
"sigs.k8s.io/yaml"
errorsmod "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/legacy"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/crypto/types/multisig"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
)
// LegacyMsg defines the old interface a message must fulfill,
// containing Amino signing method.
// Deprecated: Please use `Msg` instead.
type LegacyMsg interface {
sdk.Msg
// GetSignBytes get the canonical byte representation of the Msg.
GetSignBytes() []byte
}
// StdSignDoc is replay-prevention structure.
// It includes the result of msg.GetSignBytes(),
// as well as the ChainID (prevent cross chain replay)
// and the Sequence numbers for each signature (prevent
// inchain replay and enforce tx ordering per account).
type StdSignDoc 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"`
}
var RegressionTestingAminoCodec *codec.LegacyAmino
// Deprecated: please delete this code eventually.
func mustSortJSON(bz []byte) []byte {
var c any
err := json.Unmarshal(bz, &c)
if err != nil {
panic(err)
}
js, err := json.Marshal(c)
if err != nil {
panic(err)
}
return js
}
// StdSignBytes returns the bytes to sign for a transaction.
// Deprecated: Please use x/tx/signing/aminojson instead.
func StdSignBytes(chainID string, accnum, sequence, timeout uint64, fee StdFee, msgs []sdk.Msg, memo string) []byte {
if RegressionTestingAminoCodec == nil {
panic(errors.New("must set RegressionTestingAminoCodec before calling StdSignBytes"))
}
msgsBytes := make([]json.RawMessage, 0, len(msgs))
for _, msg := range msgs {
bz := RegressionTestingAminoCodec.MustMarshalJSON(msg)
msgsBytes = append(msgsBytes, mustSortJSON(bz))
}
bz, err := legacy.Cdc.MarshalJSON(StdSignDoc{
AccountNumber: accnum,
ChainID: chainID,
Fee: json.RawMessage(fee.Bytes()),
Memo: memo,
Msgs: msgsBytes,
Sequence: sequence,
TimeoutHeight: timeout,
})
if err != nil {
panic(err)
}
return mustSortJSON(bz)
}
// Deprecated: StdSignature represents a sig
type StdSignature struct {
cryptotypes.PubKey `json:"pub_key" yaml:"pub_key"` // optional
Signature []byte `json:"signature" yaml:"signature"`
}
// Deprecated: NewStdSignature returns a std signature
func NewStdSignature(pk cryptotypes.PubKey, sig []byte) StdSignature {
return StdSignature{PubKey: pk, Signature: sig}
}
// GetSignature returns the raw signature bytes.
func (ss StdSignature) GetSignature() []byte {
return ss.Signature
}
// GetPubKey returns the public key of a signature as a cryptotypes.PubKey using the
// Amino codec.
func (ss StdSignature) GetPubKey() cryptotypes.PubKey {
return ss.PubKey
}
// MarshalYAML returns the YAML representation of the signature.
func (ss StdSignature) MarshalYAML() (interface{}, error) {
pk := ""
if ss.PubKey != nil {
pk = ss.PubKey.String()
}
bz, err := yaml.Marshal(struct {
PubKey string `json:"pub_key"`
Signature string `json:"signature"`
}{
pk,
fmt.Sprintf("%X", ss.Signature),
})
if err != nil {
return nil, err
}
return string(bz), err
}
func (ss StdSignature) UnpackInterfaces(unpacker gogoprotoany.AnyUnpacker) error {
return codectypes.UnpackInterfaces(ss.PubKey, unpacker)
}
// StdSignatureToSignatureV2 converts a StdSignature to a SignatureV2
func StdSignatureToSignatureV2(cdc *codec.LegacyAmino, sig StdSignature) (signing.SignatureV2, error) {
pk := sig.GetPubKey()
data, err := pubKeySigToSigData(cdc, pk, sig.Signature)
if err != nil {
return signing.SignatureV2{}, err
}
return signing.SignatureV2{
PubKey: pk,
Data: data,
}, nil
}
func pubKeySigToSigData(cdc *codec.LegacyAmino, key cryptotypes.PubKey, sig []byte) (signing.SignatureData, error) {
multiPK, ok := key.(multisig.PubKey)
if !ok {
return &signing.SingleSignatureData{
SignMode: signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON,
Signature: sig,
}, nil
}
var multiSig multisig.AminoMultisignature
err := cdc.Unmarshal(sig, &multiSig)
if err != nil {
return nil, err
}
sigs := multiSig.Sigs
sigDatas := make([]signing.SignatureData, len(sigs))
pubKeys := multiPK.GetPubKeys()
bitArray := multiSig.BitArray
n := multiSig.BitArray.Count()
signatures := multisig.NewMultisig(n)
sigIdx := 0
for i := 0; i < n; i++ {
if bitArray.GetIndex(i) {
data, err := pubKeySigToSigData(cdc, pubKeys[i], multiSig.Sigs[sigIdx])
if err != nil {
return nil, errorsmod.Wrapf(err, "Unable to convert Signature to SigData %d", sigIdx)
}
sigDatas[sigIdx] = data
multisig.AddSignature(signatures, data, sigIdx)
sigIdx++
}
}
return signatures, nil
}