-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathkey.go
More file actions
99 lines (81 loc) · 1.98 KB
/
key.go
File metadata and controls
99 lines (81 loc) · 1.98 KB
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
package block_io_go
import (
"crypto/sha256"
"encoding/hex"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcutil"
"log"
)
type ECKey struct {
d *btcec.PrivateKey
Compressed bool
}
func NewECKey (d [32]byte, compressed bool) *ECKey {
privKey, _ := btcec.PrivKeyFromBytes(btcec.S256(), d[:])
return &ECKey{
d: privKey,
Compressed: compressed,
}
}
func (eck *ECKey) PrivateKey() []byte {
return eck.d.Serialize()
}
func (eck *ECKey) PrivateKeyHex() string {
return hex.EncodeToString(eck.PrivateKey())
}
func (eck *ECKey) PublicKey() []byte {
if (eck.Compressed) {
return eck.d.PubKey().SerializeCompressed()
}
return eck.d.PubKey().SerializeUncompressed()
}
func (eck *ECKey) PublicKeyHex() string {
return hex.EncodeToString(eck.PublicKey())
}
func (eck *ECKey) Sign(message []byte) ([]byte, error) {
signature, err := eck.d.Sign(message)
if (err != nil) {
return nil, err
}
return signature.Serialize(), nil
}
func (eck *ECKey) SignHex(message []byte) (string, error) {
signature, err := eck.Sign(message)
if (err != nil) {
return "", err
}
return hex.EncodeToString(signature), nil
}
func FromWIF(strWif string) (*ECKey, error) {
wif, err := btcutil.DecodeWIF(strWif)
if (err != nil) {
return nil, err
}
eckey := &ECKey{wif.PrivKey, wif.CompressPubKey}
return eckey, nil
}
func DeriveKeyFromHex(hexPass string) (*ECKey, error) {
unhexlified, err := hex.DecodeString(hexPass)
if err != nil {
return nil, err
}
hashed := sha256.Sum256(unhexlified)
return NewECKey(hashed, true), nil
}
func DeriveKeyFromString(pass string) *ECKey {
password := []byte(pass)
hashed := sha256.Sum256(password)
return NewECKey(hashed, true)
}
//DEPRECIATED: Please use DeriveKeyFromHex
func ExtractKeyFromPassphrase(hexPass string) *ECKey {
key, err := DeriveKeyFromHex(hexPass)
if err != nil {
log.Fatal(err)
}
return key
}
//DEPRECIATED: Please use DeriveKeyFromString
func ExtractKeyFromPassphraseString(pass string) *ECKey {
return DeriveKeyFromString(pass)
}