-
Notifications
You must be signed in to change notification settings - Fork 18
/
auth.go
49 lines (38 loc) · 1.35 KB
/
auth.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
package types
import (
"fmt"
"github.com/cometbft/cometbft/crypto"
sdkcodec "github.com/cosmos/cosmos-sdk/codec"
)
func Sign(signableData sdkcodec.ProtoMarshaler, seq uint64, privKey crypto.PrivKey) ([]byte, error) {
return privKey.Sign(mustGetSignBytesWithSeq(signableData, seq))
}
func Verify(signature []byte, signableData sdkcodec.ProtoMarshaler, seq uint64, pubKey crypto.PubKey) (uint64, bool) {
signBytes := mustGetSignBytesWithSeq(signableData, seq)
if !pubKey.VerifySignature(signBytes, signature) {
return 0, false
}
return nextSequence(seq), true
}
// mustGetSignBytesWithSeq returns a byte slice which is the combination of data and seq.
// The return value is deterministic, so that it can be used for signing.
func mustGetSignBytesWithSeq(signableData sdkcodec.ProtoMarshaler, seq uint64) []byte {
dAtA, err := signableData.Marshal()
if err != nil {
panic(fmt.Sprintf("marshal failed: %s, signableData: %s", err.Error(), signableData))
}
dataWithSeq := DataWithSeq{
Data: dAtA,
Sequence: seq,
}
dAtA, err = dataWithSeq.Marshal()
if err != nil {
panic(fmt.Sprintf("marshal failed: %s, dataWithSeq: %v", err.Error(), dataWithSeq))
}
return dAtA
}
// Sequence is a preventative measure to distinguish replayed transactions (replay attack).
const InitialSequence uint64 = 0
func nextSequence(seq uint64) uint64 {
return seq + 1
}