forked from eoscanada/eos-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
privkey.go
121 lines (94 loc) · 2.73 KB
/
privkey.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
package ecc
import (
cryptorand "crypto/rand"
"encoding/json"
"fmt"
"io"
"strings"
"github.com/eoscanada/eos-go/btcsuite/btcd/btcec"
"github.com/eoscanada/eos-go/btcsuite/btcutil"
)
const PrivateKeyPrefix = "PVT_"
func NewRandomPrivateKey() (*PrivateKey, error) {
return newRandomPrivateKey(cryptorand.Reader)
}
func NewDeterministicPrivateKey(randSource io.Reader) (*PrivateKey, error) {
return newRandomPrivateKey(randSource)
}
func newRandomPrivateKey(randSource io.Reader) (*PrivateKey, error) {
rawPrivKey := make([]byte, 32)
written, err := io.ReadFull(randSource, rawPrivKey)
if err != nil {
return nil, fmt.Errorf("error feeding crypto-rand numbers to seed ephemeral private key: %s", err)
}
if written != 32 {
return nil, fmt.Errorf("couldn't write 32 bytes of randomness to seed ephemeral private key")
}
privKey, _ := btcec.PrivKeyFromBytes(btcec.S256(), rawPrivKey)
inner := &innerK1PrivateKey{privKey: privKey}
return &PrivateKey{Curve: CurveK1, inner: inner}, nil
}
func NewPrivateKey(wif string) (*PrivateKey, error) {
// Strip potential prefix, and set curve
var privKeyMaterial string
if strings.HasPrefix(wif, PrivateKeyPrefix) { // "PVT_"
privKeyMaterial = wif[len(PrivateKeyPrefix):]
curvePrefix := privKeyMaterial[:3]
privKeyMaterial = privKeyMaterial[3:] // remove "K1_"...
switch curvePrefix {
case "K1_":
wifObj, err := btcutil.DecodeWIF(privKeyMaterial)
if err != nil {
return nil, err
}
inner := &innerK1PrivateKey{privKey: wifObj.PrivKey}
return &PrivateKey{Curve: CurveK1, inner: inner}, nil
case "R1_":
inner := &innerR1PrivateKey{}
return &PrivateKey{Curve: CurveR1, inner: inner}, nil
default:
return nil, fmt.Errorf("unsupported curve prefix %q", curvePrefix)
}
} else { // no-prefix, like before
wifObj, err := btcutil.DecodeWIF(wif)
if err != nil {
return nil, err
}
inner := &innerK1PrivateKey{privKey: wifObj.PrivKey}
return &PrivateKey{Curve: CurveK1, inner: inner}, nil
}
}
type innerPrivateKey interface {
publicKey() PublicKey
sign(hash []byte) (out Signature, err error)
string() string
}
type PrivateKey struct {
Curve CurveID
inner innerPrivateKey
}
func (p *PrivateKey) PublicKey() PublicKey {
return p.inner.publicKey()
}
// Sign signs a 32 bytes SHA256 hash..
func (p *PrivateKey) Sign(hash []byte) (out Signature, err error) {
return p.inner.sign(hash)
}
func (p *PrivateKey) String() string {
return p.inner.string()
}
func (p *PrivateKey) MarshalJSON() ([]byte, error) {
return json.Marshal(p.String())
}
func (p *PrivateKey) UnmarshalJSON(v []byte) (err error) {
var s string
if err = json.Unmarshal(v, &s); err != nil {
return
}
newPrivKey, err := NewPrivateKey(s)
if err != nil {
return
}
*p = *newPrivKey
return
}