-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
keys.go
61 lines (47 loc) · 2.02 KB
/
keys.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
package types
import (
"fmt"
"regexp"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
host "github.com/cosmos/cosmos-sdk/x/ibc/core/24-host"
)
const (
// SubModuleName defines the IBC connection name
SubModuleName = "connection"
// StoreKey is the store key string for IBC connections
StoreKey = SubModuleName
// RouterKey is the message route for IBC connections
RouterKey = SubModuleName
// QuerierRoute is the querier route for IBC connections
QuerierRoute = SubModuleName
// KeyNextConnectionSequence is the key used to store the next connection sequence in
// the keeper.
KeyNextConnectionSequence = "nextConnectionSequence"
// ConnectionPrefix is the prefix used when creating a connection identifier
ConnectionPrefix = "connection-"
)
// FormatConnectionIdentifier returns the connection identifier with the sequence appended.
// This is a SDK specific format not enforced by IBC protocol.
func FormatConnectionIdentifier(sequence uint64) string {
return fmt.Sprintf("%s%d", ConnectionPrefix, sequence)
}
// IsConnectionIDFormat checks if a connectionID is in the format required on the SDK for
// parsing connection identifiers. The connection identifier must be in the form: `connection-{N}
var IsConnectionIDFormat = regexp.MustCompile(`^connection-[0-9]{1,20}$`).MatchString
// IsValidConnectionID checks if the connection identifier is valid and can be parsed to
// the connection identifier format.
func IsValidConnectionID(connectionID string) bool {
_, err := ParseConnectionSequence(connectionID)
return err == nil
}
// ParseConnectionSequence parses the connection sequence from the connection identifier.
func ParseConnectionSequence(connectionID string) (uint64, error) {
if !IsConnectionIDFormat(connectionID) {
return 0, sdkerrors.Wrap(host.ErrInvalidID, "connection identifier is not in the format: `connection-{N}`")
}
sequence, err := host.ParseIdentifier(connectionID, ConnectionPrefix)
if err != nil {
return 0, sdkerrors.Wrap(err, "invalid connection identifier")
}
return sequence, nil
}