/
signature.go
100 lines (88 loc) · 3.22 KB
/
signature.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
package signing
import (
"fmt"
"github.com/tendermint/tendermint/crypto"
)
// SignatureV2 is a convenience type that is easier to use in application logic
// than the protobuf SignerInfo's and raw signature bytes. It goes beyond the
// first sdk.Signature types by supporting sign modes and explicitly nested
// multi-signatures. It is intended to be used for both building and verifying
// signatures.
type SignatureV2 struct {
// PubKey is the public key to use for verifying the signature
PubKey crypto.PubKey
// Data is the actual data of the signature which includes SignMode's and
// the signatures themselves for either single or multi-signatures.
Data SignatureData
// Sequence is the sequence of this account. Only populated in
// SIGN_MODE_DIRECT.
Sequence uint64
// Ugly flag to keep backwards-compatibility with Amino StdSignatures.
// In SIGN_MODE_DIRECT, sequence is in AuthInfo, and will thus be populated
// in the Sequence field above. The ante handler then checks this Sequence
// with the actual sequence on-chain.
// In SIGN_MODE_LEGACY_AMINO_JSON, sequence is signed via StdSignDoc, and
// checked during signature verification. It's not populated in the
// Sequence field above. This flag indicates that the Sequence field should
// be skipped in ante handlers.
// TLDR;
// - false (by default) in SIGN_MODE_DIRECT
// - true in SIGN_MODE_LEGACY_AMINO_JSON
// ref: https://github.com/irisnet/irishub-sdk-go/issues/7229
SkipSequenceCheck bool
}
// SignatureDataToProto converts a SignatureData to SignatureDescriptor_Data.
// SignatureDescriptor_Data is considered an encoding type whereas SignatureData is used for
// business logic.
func SignatureDataToProto(data SignatureData) *SignatureDescriptor_Data {
switch data := data.(type) {
case *SingleSignatureData:
return &SignatureDescriptor_Data{
Sum: &SignatureDescriptor_Data_Single_{
Single: &SignatureDescriptor_Data_Single{
Mode: data.SignMode,
Signature: data.Signature,
},
},
}
case *MultiSignatureData:
descDatas := make([]*SignatureDescriptor_Data, len(data.Signatures))
for j, d := range data.Signatures {
descDatas[j] = SignatureDataToProto(d)
}
return &SignatureDescriptor_Data{
Sum: &SignatureDescriptor_Data_Multi_{
Multi: &SignatureDescriptor_Data_Multi{
Bitarray: data.BitArray,
Signatures: descDatas,
},
},
}
default:
panic(fmt.Errorf("unexpected case %+v", data))
}
}
// SignatureDataFromProto converts a SignatureDescriptor_Data to SignatureData.
// SignatureDescriptor_Data is considered an encoding type whereas SignatureData is used for
// business logic.
func SignatureDataFromProto(descData *SignatureDescriptor_Data) SignatureData {
switch descData := descData.Sum.(type) {
case *SignatureDescriptor_Data_Single_:
return &SingleSignatureData{
SignMode: descData.Single.Mode,
Signature: descData.Single.Signature,
}
case *SignatureDescriptor_Data_Multi_:
multi := descData.Multi
datas := make([]SignatureData, len(multi.Signatures))
for j, d := range multi.Signatures {
datas[j] = SignatureDataFromProto(d)
}
return &MultiSignatureData{
BitArray: multi.Bitarray,
Signatures: datas,
}
default:
panic(fmt.Errorf("unexpected case %+v", descData))
}
}