-
Notifications
You must be signed in to change notification settings - Fork 28
/
test_helpers.go
131 lines (110 loc) · 3.49 KB
/
test_helpers.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
package simapp
import (
"bytes"
"crypto/rand"
"encoding/base64"
"encoding/hex"
"fmt"
"math"
"os"
"path/filepath"
"github.com/golang-jwt/jwt"
"github.com/spf13/cast"
sdkerrors "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrtypes "github.com/cosmos/cosmos-sdk/types/errors"
)
// PKs is a slice of public keys for test
var PKs = createTestPubKeys(500)
func CreateJwtTicket(claim jwt.MapClaims) (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodEdDSA, claim)
return token.SignedString(TestOVMPrivateKeys[0])
}
// createIncrementalAccounts is a strategy used by addTestAddrs() in order to generated addresses in ascending order.
func createIncrementalAccounts(accNum int) []sdk.AccAddress {
var addresses []sdk.AccAddress
var buffer bytes.Buffer
// start at 100 so we can make up to 999 test addresses with valid test addresses
for i := 100; i < (accNum + 100); i++ {
numString := cast.ToString(i)
buffer.WriteString("A58856F0FD53BF058B4909A21AEC019107BA6") // base address string
buffer.WriteString(numString) // adding on final two digits to make addresses unique
res, _ := sdk.AccAddressFromHexUnsafe(buffer.String())
bech := res.String()
addr, _ := testAddr(buffer.String(), bech)
addresses = append(addresses, addr)
buffer.Reset()
}
return addresses
}
// testAddr returns sample account address
func testAddr(addr, bech string) (sdk.AccAddress, error) {
res, err := sdk.AccAddressFromHexUnsafe(addr)
if err != nil {
return nil, err
}
bechexpected := res.String()
if bech != bechexpected {
return nil, fmt.Errorf("bech encoding doesn't match reference")
}
bechres, err := sdk.AccAddressFromBech32(bech)
if err != nil {
return nil, err
}
if !bytes.Equal(bechres, res) {
return nil, err
}
return res, nil
}
// createTestPubKeys returns a total of numPubKeys public keys in ascending order.
func createTestPubKeys(numPubKeys int) []cryptotypes.PubKey {
var publicKeys []cryptotypes.PubKey
var buffer bytes.Buffer
// start at 10 to avoid changing 1 to 01, 2 to 02, etc
for i := 100; i < (numPubKeys + 100); i++ {
numString := cast.ToString(i)
buffer.WriteString(
"0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AF",
) // base pubkey string
buffer.WriteString(
numString,
) // adding on final two digits to make pubkeys unique
publicKeys = append(publicKeys, newPubKeyFromHex(buffer.String()))
buffer.Reset()
}
return publicKeys
}
// newPubKeyFromHex returns a PubKey from a hex string.
func newPubKeyFromHex(pk string) (res cryptotypes.PubKey) {
pkBytes, err := hex.DecodeString(pk)
if err != nil {
panic(err)
}
if len(pkBytes) != ed25519.PubKeySize {
panic(sdkerrors.Wrap(sdkerrtypes.ErrInvalidPubKey, "invalid pubkey size"))
}
return &ed25519.PubKey{Key: pkBytes}
}
func RandomString(length int) string {
buff := make([]byte, int(math.Ceil(float64(length)/float64(1.33333333333))))
_, err := rand.Read(buff)
if err != nil {
panic(err)
}
str := base64.RawURLEncoding.EncodeToString(buff)
return str[:length] // strip 1 extra character we get from odd length results
}
func WriteKeyringFile(name string, dir string, contents []byte) error {
file := filepath.Join(dir, name)
//#nosec
if err := os.MkdirAll(dir, 0o755); err != nil {
return fmt.Errorf("could not create directory %q: %w", dir, err)
}
//#nosec
if err := os.WriteFile(file, contents, 0o644); err != nil {
return err
}
return nil
}