/
kava.go
81 lines (68 loc) · 1.89 KB
/
kava.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
package kava
import (
"bytes"
"errors"
"fmt"
"strings"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/go-bip39"
"github.com/xtaci/safebox/plugins/atom"
"github.com/xtaci/safebox/qrcode"
)
type KavaExporter struct{}
func (exp *KavaExporter) Name() string {
return "Kava"
}
func (exp *KavaExporter) Desc() string {
return "A fully integrated suite of financial products for crypto. Kava brings together everything that's required to do frictionless decentralized finance"
}
func (exp *KavaExporter) KeySize() int {
return 32
}
func (exp *KavaExporter) Export(key []byte) ([]byte, error) {
if len(key) != 32 {
return nil, errors.New("invalid key length")
}
mnemonic, err := bip39.NewMnemonic(key)
if err != nil {
return nil, err
}
configuration := types.GetConfig()
configuration.SetBech32PrefixForAccount(Bech32MainPrefix, Bech32MainPrefix+types.PrefixPublic)
configuration.SetCoinType(Bip44CoinType)
hdPath := hd.NewFundraiserParams(0, types.GetConfig().GetCoinType(), 0).String()
account, err := atom.NewAccount(mnemonic, hdPath)
if err != nil {
return nil, err
}
//Check Address
_, err = types.AccAddressFromBech32(account.GetAddress().String())
if err != nil {
return nil, err
}
mnemonicSlice := strings.Split(mnemonic, " ")
var out bytes.Buffer
fmt.Fprintf(&out,
`Kava Account: %v
HD derivation path: %v
Address QR Code:
%v
Mnemonic: %v
Mnemonic QR Code(part1):
%v
Mnemonic QR Code(part2):
%v`,
account.GetAddress().String(),
hdPath,
qrcode.GenerateQRCode(account.GetAddress().String()),
mnemonic,
qrcode.GenerateQRCode(strings.Join(mnemonicSlice[0:len(mnemonicSlice)/2], " ")),
qrcode.GenerateQRCode(strings.Join(mnemonicSlice[len(mnemonicSlice)/2:], " ")),
)
return out.Bytes(), nil
}
const (
Bech32MainPrefix = "kava"
Bip44CoinType = 459 // see https://github.com/satoshilabs/slips/blob/master/slip-0044.md
)