This repository has been archived by the owner on Dec 5, 2022. It is now read-only.
/
account.go
65 lines (51 loc) · 1.47 KB
/
account.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
package panacea
import (
"fmt"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/bech32"
"github.com/medibloc/panacea-doracle/crypto"
log "github.com/sirupsen/logrus"
)
const prefix = "panacea"
type OracleAccount struct {
privKey cryptotypes.PrivKey
pubKey cryptotypes.PubKey
}
// NewOracleAccount returns an oracle account from mnemonic, account number, and index
func NewOracleAccount(mnemonic string, accNum, index uint32) (*OracleAccount, error) {
if len(mnemonic) == 0 {
return &OracleAccount{}, fmt.Errorf("mnemonic is empty")
}
key, err := crypto.GeneratePrivateKeyFromMnemonic(mnemonic, CoinType, accNum, index)
if err != nil {
return &OracleAccount{}, err
}
pk := &secp256k1.PrivKey{
Key: key.Bytes(),
}
return &OracleAccount{
privKey: pk,
pubKey: pk.PubKey(),
}, nil
}
func (oa OracleAccount) GetAddress() string {
address, err := bech32.ConvertAndEncode(HRP, oa.pubKey.Address().Bytes())
if err != nil {
log.Panic(err)
}
return address
}
func (oa OracleAccount) AccAddressFromBech32() sdk.AccAddress {
return oa.pubKey.Bytes()
}
func (oa OracleAccount) GetPrivKey() cryptotypes.PrivKey {
return oa.privKey
}
func (oa OracleAccount) GetPubKey() cryptotypes.PubKey {
return oa.pubKey
}
func GetAccAddressFromBech32(address string) (addr sdk.AccAddress, err error) {
return sdk.GetFromBech32(address, prefix)
}