-
Notifications
You must be signed in to change notification settings - Fork 44
/
sample.go
70 lines (58 loc) · 1.64 KB
/
sample.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
package sample
import (
"crypto/rand"
"encoding/hex"
"github.com/cometbft/cometbft/crypto/tmhash"
"github.com/cosmos/cosmos-sdk/crypto/keys/eth/ethsecp256k1"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/prysmaticlabs/prysm/crypto/bls"
)
func RandAccAddress() sdk.AccAddress {
pk, err := ethsecp256k1.GenPrivKey()
if err != nil {
panic(err)
}
return sdk.AccAddress(pk.PubKey().Address())
}
func RandAccAddressHex() string {
pk, err := ethsecp256k1.GenPrivKey()
if err != nil {
panic(err)
}
return sdk.AccAddress(pk.PubKey().Address()).String()
}
func RandSignBytes() (addr sdk.AccAddress, signBytes, sig []byte) {
signBytes = RandStr(256)
privKey, _ := ethsecp256k1.GenPrivKey()
sig, _ = privKey.Sign(sdk.Keccak256(signBytes))
pk := privKey.PubKey()
addr = sdk.AccAddress(pk.Address())
return addr, signBytes, sig
}
func Checksum() []byte {
return sdk.Keccak256(RandStr(256))
}
func RandStr(length int) []byte {
randBytes := make([]byte, length/2)
// #nosec
_, _ = rand.Read(randBytes)
return randBytes
}
func RandBlsPubKey() []byte {
blsPrivKey, _ := bls.RandKey()
return blsPrivKey.PublicKey().Marshal()
}
func RandBlsPubKeyHex() string {
blsPrivKey, _ := bls.RandKey()
return hex.EncodeToString(blsPrivKey.PublicKey().Marshal())
}
func RandBlsPubKeyAndBlsProofBz() ([]byte, []byte) {
blsPriv, _ := bls.RandKey()
blsPubKeyBz := blsPriv.PublicKey().Marshal()
blsProofBz := blsPriv.Sign(tmhash.Sum(blsPubKeyBz)).Marshal()
return blsPubKeyBz, blsProofBz
}
func RandBlsPubKeyAndBlsProof() (string, string) {
blsPubKey, proof := RandBlsPubKeyAndBlsProofBz()
return hex.EncodeToString(blsPubKey), hex.EncodeToString(proof)
}