forked from ChainSafe/chainbridge-utils
/
secp256k1.go
105 lines (82 loc) · 2.23 KB
/
secp256k1.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
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: LGPL-3.0-only
package secp256k1
import (
"crypto/ecdsa"
"github.com/UltronFoundationDev/chainbridge-utils/crypto"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
secp256k1 "github.com/ethereum/go-ethereum/crypto"
)
var _ crypto.Keypair = &Keypair{}
const PrivateKeyLength = 32
type Keypair struct {
public *ecdsa.PublicKey
private *ecdsa.PrivateKey
}
func NewKeypairFromPrivateKey(priv []byte) (*Keypair, error) {
pk, err := secp256k1.ToECDSA(priv)
if err != nil {
return nil, err
}
return &Keypair{
public: pk.Public().(*ecdsa.PublicKey),
private: pk,
}, nil
}
// NewKeypairFromPrivateKey parses a string for a hex private key. Must be at least
// PrivateKeyLength long.
func NewKeypairFromString(priv string) (*Keypair, error) {
pk, err := secp256k1.HexToECDSA(priv)
if err != nil {
return nil, err
}
return &Keypair{
public: pk.Public().(*ecdsa.PublicKey),
private: pk,
}, nil
}
func NewKeypair(pk ecdsa.PrivateKey) *Keypair {
pub := pk.Public()
return &Keypair{
public: pub.(*ecdsa.PublicKey),
private: &pk,
}
}
func GenerateKeypair() (*Keypair, error) {
priv, err := secp256k1.GenerateKey()
if err != nil {
return nil, err
}
return NewKeypair(*priv), nil
}
// Encode dumps the private key as bytes
func (kp *Keypair) Encode() []byte {
return secp256k1.FromECDSA(kp.private)
}
// Decode initializes the keypair using the input
func (kp *Keypair) Decode(in []byte) error {
key, err := secp256k1.ToECDSA(in)
if err != nil {
return err
}
kp.public = key.Public().(*ecdsa.PublicKey)
kp.private = key
return nil
}
// Address returns the Ethereum address format
func (kp *Keypair) Address() string {
return secp256k1.PubkeyToAddress(*kp.public).String()
}
// CommonAddress returns the Ethereum address in the common.Address Format
func (kp *Keypair) CommonAddress() common.Address {
return secp256k1.PubkeyToAddress(*kp.public)
}
// PublicKey returns the public key hex encoded
func (kp *Keypair) PublicKey() string {
return hexutil.Encode(secp256k1.CompressPubkey(kp.public))
}
// PrivateKey returns the keypair's private key
func (kp *Keypair) PrivateKey() *ecdsa.PrivateKey {
return kp.private
}