-
Notifications
You must be signed in to change notification settings - Fork 1
/
profiles.go
104 lines (88 loc) · 2.48 KB
/
profiles.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
package profilestesting
import (
"encoding/hex"
"fmt"
"time"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/bech32/legacybech32"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/gogo/protobuf/proto"
"github.com/gridiron-zone/huddle/x/profiles/types"
)
func NewAny(value proto.Message) *codectypes.Any {
any, err := codectypes.NewAnyWithValue(value)
if err != nil {
panic(err)
}
return any
}
func AssertNoProfileError(profile *types.Profile, err error) *types.Profile {
if err != nil {
panic(err)
}
return profile
}
func AccountFromAddr(addr string) authtypes.AccountI {
address, err := sdk.AccAddressFromBech32(addr)
if err != nil {
panic(err)
}
return authtypes.NewBaseAccountWithAddress(address)
}
func PubKeyFromBech32(pubKey string) cryptotypes.PubKey {
publicKey, err := legacybech32.UnmarshalPubKey(legacybech32.AccPK, pubKey)
if err != nil {
panic(err)
}
return publicKey
}
func PubKeyFromJSON(cdc codec.Codec, pubKey string) cryptotypes.PubKey {
var publicKey cryptotypes.PubKey
err := cdc.UnmarshalInterfaceJSON([]byte(pubKey), &publicKey)
if err != nil {
panic(err)
}
return publicKey
}
func ProfileFromAddr(address string) *types.Profile {
profile, err := types.NewProfile(
fmt.Sprintf("%s-dtag", address),
"",
"",
types.NewPictures("", ""),
time.Date(2020, 1, 1, 00, 00, 00, 000, time.UTC),
AccountFromAddr(address),
)
if err != nil {
panic(err)
}
return profile
}
// SingleSignatureFromHex convert the hex-encoded string of the single signature to CosmosSignatureData
func SingleSignatureFromHex(hexEncodedSignature string) types.Signature {
sig, err := hex.DecodeString(hexEncodedSignature)
if err != nil {
panic(err)
}
return types.NewSingleSignature(types.SIGNATURE_VALUE_TYPE_RAW, sig)
}
// MultiCosmosSignatureFromHex convert the hex-encoded string of the MultiSignature Any value to CosmosSignatureData
func MultiCosmosSignatureFromHex(unpacker codectypes.AnyUnpacker, hexEncodedSignatureData string) types.Signature {
sig, err := hex.DecodeString(hexEncodedSignatureData)
if err != nil {
panic(err)
}
var multisigAny codectypes.Any
err = multisigAny.Unmarshal(sig)
if err != nil {
panic(err)
}
var signature types.Signature
if err = unpacker.UnpackAny(&multisigAny, &signature); err != nil {
panic(err)
}
return signature
}