-
Notifications
You must be signed in to change notification settings - Fork 1
/
builder.go
66 lines (56 loc) · 2 KB
/
builder.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
package single
import (
"encoding/hex"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/gridiron-zone/huddle/app/huddle/cmd/chainlink/getter"
"github.com/gridiron-zone/huddle/app/huddle/cmd/chainlink/types"
"github.com/gridiron-zone/huddle/x/profiles/client/utils"
profilestypes "github.com/gridiron-zone/huddle/x/profiles/types"
)
const (
KeyName = "huddle_chain_link_account"
)
// AccountChainLinkJSONBuilder implements the ChainLinkJSONBuilder for single signature accounts
type AccountChainLinkJSONBuilder struct {
owner string
getter getter.SingleSignatureAccountReferenceGetter
}
// NewAccountChainLinkJSONBuilder returns a new AccountChainLinkJSONBuilder instance
func NewAccountChainLinkJSONBuilder(owner string, getter getter.SingleSignatureAccountReferenceGetter) *AccountChainLinkJSONBuilder {
return &AccountChainLinkJSONBuilder{
owner: owner,
getter: getter,
}
}
// BuildChainLinkJSON implements ChainLinkJSONBuilder
func (b *AccountChainLinkJSONBuilder) BuildChainLinkJSON(chain types.Chain) (utils.ChainLinkJSON, error) {
mnemonic, err := b.getter.GetMnemonic()
if err != nil {
return utils.ChainLinkJSON{}, err
}
// Create an in-memory keybase for signing
keyBase := keyring.NewInMemory()
_, err = keyBase.NewAccount(KeyName, mnemonic, "", chain.DerivationPath, hd.Secp256k1)
if err != nil {
return utils.ChainLinkJSON{}, err
}
// Generate the proof signing it with the key
key, _ := keyBase.Key(KeyName)
addr, _ := sdk.Bech32ifyAddressBytes(chain.Prefix, key.GetAddress())
value := []byte(b.owner)
sig, pubkey, err := keyBase.Sign(KeyName, value)
if err != nil {
return utils.ChainLinkJSON{}, err
}
return utils.NewChainLinkJSON(
profilestypes.NewBech32Address(addr, chain.Prefix),
profilestypes.NewProof(
pubkey,
profilestypes.NewSingleSignature(profilestypes.SIGNATURE_VALUE_TYPE_RAW, sig),
hex.EncodeToString(value),
),
profilestypes.NewChainConfig(chain.Name),
), nil
}