-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
pubkey.go
44 lines (34 loc) · 1.1 KB
/
pubkey.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
package bitcoin
import (
"encoding/hex"
"github.com/libsv/go-bk/bec"
)
// PubKeyFromPrivateKeyString will derive a pubKey (hex encoded) from a given private key
func PubKeyFromPrivateKeyString(privateKey string, compressed bool) (string, error) {
rawKey, err := PrivateKeyFromString(privateKey)
if err != nil {
return "", err
}
return PubKeyFromPrivateKey(rawKey, compressed), nil
}
// PubKeyFromPrivateKey will derive a pubKey (hex encoded) from a given private key
func PubKeyFromPrivateKey(privateKey *bec.PrivateKey, compressed bool) string {
if compressed {
return hex.EncodeToString(privateKey.PubKey().SerialiseCompressed())
}
return hex.EncodeToString(privateKey.PubKey().SerialiseUncompressed())
}
// PubKeyFromString will convert a pubKey (string) into a pubkey (*bec.PublicKey)
func PubKeyFromString(pubKey string) (*bec.PublicKey, error) {
// Invalid pubKey
if len(pubKey) == 0 {
return nil, ErrMissingPubKey
}
// Decode from hex string
decoded, err := hex.DecodeString(pubKey)
if err != nil {
return nil, err
}
// Parse into a pubKey
return bec.ParsePubKey(decoded, bec.S256())
}