-
Notifications
You must be signed in to change notification settings - Fork 44
/
builder.go
68 lines (58 loc) · 2.09 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
67
68
package single
import (
"encoding/hex"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/desmos-labs/desmos/v5/app/desmos/cmd/chainlink/getter"
"github.com/desmos-labs/desmos/v5/app/desmos/cmd/chainlink/types"
"github.com/desmos-labs/desmos/v5/x/profiles/client/utils"
profilestypes "github.com/desmos-labs/desmos/v5/x/profiles/types"
)
const (
KeyName = "desmos_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(cdc codec.Codec, 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(cdc)
_, 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)
accAddr, _ := key.GetAddress()
addr, _ := sdk.Bech32ifyAddressBytes(chain.Prefix, accAddr)
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
}