/
denoms.go
84 lines (73 loc) · 2.9 KB
/
denoms.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
package types
import (
fmt "fmt"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
)
const (
ModuleDenomPrefix = "factory"
// See the TokenFactory readme for a derivation of these.
// TL;DR, MaxSubdenomLength + MaxHrpLength = 60 comes from SDK max denom length = 128
// and the structure of tokenfactory denoms.
MaxSubdenomLength = 44
MaxHrpLength = 16
// MaxCreatorLength = 59 + MaxHrpLength
MaxCreatorLength = 59 + MaxHrpLength
)
// GetTokenDenom constructs a denom string for tokens created by tokenfactory
// based on an input creator address and a subdenom
// The denom constructed is factory/{creator}/{subdenom}
func GetTokenDenom(creator, subdenom string) (string, error) {
if len(subdenom) > MaxSubdenomLength {
return "", ErrSubdenomTooLong
}
if len(creator) > MaxCreatorLength {
return "", ErrCreatorTooLong
}
if strings.Contains(creator, "/") {
return "", ErrInvalidCreator
}
denom := strings.Join([]string{ModuleDenomPrefix, creator, subdenom}, "/")
return denom, sdk.ValidateDenom(denom)
}
// DeconstructDenom takes a token denom string and verifies that it is a valid
// denom of the tokenfactory module, and is of the form `factory/{creator}/{subdenom}`
// If valid, it returns the creator address and subdenom
func DeconstructDenom(denom string) (creator string, subdenom string, err error) {
err = sdk.ValidateDenom(denom)
if err != nil {
return "", "", err
}
strParts := strings.Split(denom, "/")
if len(strParts) < 3 {
return "", "", sdkerrors.Wrapf(ErrInvalidDenom, "not enough parts of denom %s", denom)
}
if strParts[0] != ModuleDenomPrefix {
return "", "", sdkerrors.Wrapf(ErrInvalidDenom, "denom prefix is incorrect. Is: %s. Should be: %s", strParts[0], ModuleDenomPrefix)
}
creator = strParts[1]
creatorAddr, err := sdk.AccAddressFromBech32(creator)
if err != nil {
return "", "", sdkerrors.Wrapf(ErrInvalidDenom, "Invalid creator address (%s)", err)
}
// Handle the case where a denom has a slash in its subdenom. For example,
// when we did the split, we'd turn factory/accaddr/atomderivative/testy into ["factory", "accaddr", "atomderivative", "testy"]
// So we have to join [2:] with a "/" as the delimiter to get back the correct subdenom which should be "atomderivative/testy"
subdenom = strings.Join(strParts[2:], "/")
return creatorAddr.String(), subdenom, nil
}
// NewTokenFactoryDenomMintCoinsRestriction creates and returns a MintingRestrictionFn that only allows minting of
// valid tokenfactory denoms
func NewTokenFactoryDenomMintCoinsRestriction() bankkeeper.MintingRestrictionFn {
return func(ctx sdk.Context, coinsToMint sdk.Coins) error {
for _, coin := range coinsToMint {
_, _, err := DeconstructDenom(coin.Denom)
if err != nil {
return fmt.Errorf("does not have permission to mint %s", coin.Denom)
}
}
return nil
}
}