forked from coming-chat/wallet-SDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
124 lines (100 loc) · 2.92 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package doge
import (
"errors"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/coming-chat/wallet-SDK/core/base"
"github.com/cosmos/go-bip39"
)
type Account struct {
privateKey []byte
publicKey []byte
address string
Chainnet string
}
func NewAccountWithMnemonic(mnemonic, chainnet string) (*Account, error) {
seed, err := bip39.NewSeedWithErrorChecking(mnemonic, "")
if err != nil {
return nil, err
}
pri, pub := btcec.PrivKeyFromBytes(seed)
priData := pri.Serialize()
pubData := pub.SerializeUncompressed()
address, err := EncodePublicDataToAddress(pubData, chainnet)
if err != nil {
return nil, err
}
return &Account{
privateKey: priData,
publicKey: pubData,
address: address,
Chainnet: chainnet,
}, nil
}
func AccountWithPrivateKey(prikey string, chainnet string) (*Account, error) {
seed, err := types.HexDecodeString(prikey)
if err != nil {
return nil, err
}
pri, pub := btcec.PrivKeyFromBytes(seed)
priData := pri.Serialize()
pubData := pub.SerializeUncompressed()
address, err := EncodePublicDataToAddress(pubData, chainnet)
if err != nil {
return nil, err
}
return &Account{
privateKey: priData,
publicKey: pubData,
address: address,
Chainnet: chainnet,
}, nil
}
// MARK - Implement the protocol Account
// @return privateKey data
func (a *Account) PrivateKey() ([]byte, error) {
return a.privateKey, nil
}
// @return privateKey string that will start with 0x.
func (a *Account) PrivateKeyHex() (string, error) {
return types.HexEncodeToString(a.privateKey), nil
}
// @return publicKey data
func (a *Account) PublicKey() []byte {
return a.publicKey
}
// @return publicKey string that will start with 0x.
func (a *Account) PublicKeyHex() string {
return types.HexEncodeToString(a.publicKey)
}
// @return default is the mainnet address
func (a *Account) Address() string {
return a.address
}
// TODO: function not implement yet.
func (a *Account) Sign(message []byte, password string) ([]byte, error) {
return nil, errors.New("TODO: function not implement yet.")
}
// TODO: function not implement yet.
func (a *Account) SignHex(messageHex string, password string) (*base.OptionalString, error) {
return nil, errors.New("TODO: function not implement yet.")
}
// MARK - Implement the protocol AddressUtil
// @param publicKey can start with 0x or not.
func (a *Account) EncodePublicKeyToAddress(publicKey string) (string, error) {
return EncodePublicKeyToAddress(publicKey, a.Chainnet)
}
// @return publicKey that will start with 0x.
func (a *Account) DecodeAddressToPublicKey(address string) (string, error) {
return "", errors.New("Dogecoin cannot support decode address to public key")
}
func (a *Account) IsValidAddress(address string) bool {
return IsValidAddress(address, a.Chainnet)
}
func AsDogecoinAccount(account base.Account) *Account {
if r, ok := account.(*Account); ok {
return r
} else {
return nil
}
}