forked from mapprotocol/atlas
/
accounts.go
119 lines (106 loc) · 3.34 KB
/
accounts.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
package account
import (
"crypto/ecdsa"
"fmt"
"github.com/Alexfordev/atlas/helper/bls"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"io/ioutil"
)
// Account represents a atlas Account
type Account struct {
Address common.Address
PrivateKey *ecdsa.PrivateKey
}
// MustBLSProofOfPossession variant of BLSProofOfPossession that panics on error
func (a *Account) MustBLSProofOfPossession() []byte {
pop, err := a.BLSProofOfPossession()
if err != nil {
log.Error("MustBLSProofOfPossession", "err", err)
}
return pop
}
// BLSProofOfPossession generates bls proof of possession
func (a *Account) BLSProofOfPossession() ([]byte, error) {
privateKey, err := bls.CryptoType().ECDSAToBLS(a.PrivateKey)
if err != nil {
privdata := crypto.FromECDSA(a.PrivateKey)
log.Error("ECDSAToBLS", "privdata", hexutil.Encode(privdata))
return nil, err
}
key, err := bls.DeserializePrivateKey(privateKey)
if err != nil {
log.Error("DeserializePrivateKey", "err", err)
return nil, err
}
// pkbytes, err := bls.CryptoType().PrivateToPublic(privateKey)
// if err != nil {
// privdata := crypto.FromECDSA(a.PrivateKey)
// log.Error("PrivateToPublic", "err", err, "privdata", hexutil.Encode(privdata), "address", a.Address.String())
// return nil, err
// }
// pubkey, err := bls.UnmarshalPk(pkbytes[:])
// if err != nil {
// log.Error("bn256.UnmarshalPk", "err", err)
// return nil, err
// }
signature, err := bls.UnsafeSign2(key, a.Address.Bytes())
if err != nil {
log.Error("bn256.Sign", "err", err)
return nil, err
}
return signature.Marshal(), nil
}
// BLSPublicKey returns the bls public key
func (a *Account) BLSPublicKey() (bls.SerializedPublicKey, error) {
privateKey, err := bls.CryptoType().ECDSAToBLS(a.PrivateKey)
if err != nil {
return bls.SerializedPublicKey{}, err
}
return bls.CryptoType().PrivateToPublic(privateKey)
}
// BLSG1PublicKey returns the bls G1 public key
func (a *Account) BLSG1PublicKey() (bls.SerializedG1PublicKey, error) {
privateKey, err := bls.CryptoType().ECDSAToBLS(a.PrivateKey)
if err != nil {
return bls.SerializedG1PublicKey{}, err
}
return bls.CryptoType().PrivateToG1Public(privateKey)
}
// PublicKeyHex hex representation of the public key
func (a *Account) PublicKey() []byte {
return crypto.FromECDSAPub(&a.PrivateKey.PublicKey)
}
// PrivateKeyHex hex representation of the private key
func (a *Account) PrivateKeyHex() string {
return common.Bytes2Hex(crypto.FromECDSA(a.PrivateKey))
}
func (a *Account) String() string {
return fmt.Sprintf("{ address: %s\tprivateKey: %s }",
a.Address.Hex(),
a.PrivateKeyHex(),
)
}
func LoadAccount(path string, password string) (*Account, error) {
keyjson, err := ioutil.ReadFile(path)
if err != nil {
log.Error("LoadAccount", "msg", fmt.Errorf("failed to read the keyfile at '%s': %v", path, err))
return nil, err
}
key, err := keystore.DecryptKey(keyjson, password)
if err != nil {
log.Error("LoadAccount", "msg", fmt.Errorf("error decrypting key: %v", err))
return nil, err
}
priKey1 := key.PrivateKey
publicAddr := crypto.PubkeyToAddress(priKey1.PublicKey)
var addr common.Address
addr.SetBytes(publicAddr.Bytes())
return &Account{
Address: addr,
PrivateKey: priKey1,
}, nil
}