/
wallet.go
58 lines (49 loc) · 1.15 KB
/
wallet.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
package wallet
import (
"github.com/icon-project/icon-bridge/common"
"github.com/icon-project/icon-bridge/common/crypto"
)
type Wallet interface {
Address() string
Sign(data []byte) ([]byte, error)
PublicKey() []byte
ECDH(pubKey []byte) ([]byte, error)
}
type softwareWallet struct {
skey *crypto.PrivateKey
pkey *crypto.PublicKey
}
func (w *softwareWallet) Address() string {
return common.NewAccountAddressFromPublicKey(w.pkey).String()
}
func (w *softwareWallet) Sign(data []byte) ([]byte, error) {
sig, err := crypto.NewSignature(data, w.skey)
if err != nil {
return nil, err
}
return sig.SerializeRSV()
}
func (w *softwareWallet) PublicKey() []byte {
return w.pkey.SerializeCompressed()
}
func (w *softwareWallet) ECDH(pubKey []byte) ([]byte, error) {
pkey, err := crypto.ParsePublicKey(pubKey)
if err != nil {
return nil, err
}
return w.skey.ECDH(pkey), nil
}
func New() *softwareWallet {
sk, pk := crypto.GenerateKeyPair()
return &softwareWallet{
skey: sk,
pkey: pk,
}
}
func NewIcxWalletFromPrivateKey(sk *crypto.PrivateKey) (*softwareWallet, error) {
pk := sk.PublicKey()
return &softwareWallet{
skey: sk,
pkey: pk,
}, nil
}