forked from OffchainLabs/nitro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkey_utils.go
125 lines (109 loc) · 3.55 KB
/
key_utils.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
122
123
124
125
// Copyright 2021-2022, Offchain Labs, Inc.
// For license information, see https://github.com/nitro/blob/master/LICENSE
package das
import (
"bytes"
"encoding/base64"
"encoding/hex"
"io"
"os"
"github.com/ethereum/go-ethereum/crypto"
"github.com/offchainlabs/nitro/blsSignatures"
)
// Note for Decode functions
// Ethereum's BLS library doesn't like the byte slice containing the BLS keys to be
// any larger than necessary, so we need to create a Decoder to avoid returning any padding.
func DecodeBase64BLSPublicKey(pubKeyEncodedBytes []byte) (*blsSignatures.PublicKey, error) {
pubKeyDecoder := base64.NewDecoder(base64.StdEncoding, bytes.NewReader(pubKeyEncodedBytes))
pubKeyBytes, err := io.ReadAll(pubKeyDecoder)
if err != nil {
return nil, err
}
pubKey, err := blsSignatures.PublicKeyFromBytes(pubKeyBytes, false)
if err != nil {
return nil, err
}
return &pubKey, nil
}
func DecodeBase64BLSPrivateKey(privKeyEncodedBytes []byte) (blsSignatures.PrivateKey, error) {
privKeyDecoder := base64.NewDecoder(base64.StdEncoding, bytes.NewReader(privKeyEncodedBytes))
privKeyBytes, err := io.ReadAll(privKeyDecoder)
if err != nil {
return nil, err
}
privKey, err := blsSignatures.PrivateKeyFromBytes(privKeyBytes)
if err != nil {
return nil, err
}
return privKey, nil
}
const DefaultPubKeyFilename = "das_bls.pub"
const DefaultPrivKeyFilename = "das_bls"
func GenerateAndStoreKeys(keyDir string) (*blsSignatures.PublicKey, *blsSignatures.PrivateKey, error) {
pubKey, privKey, err := blsSignatures.GenerateKeys()
if err != nil {
return nil, nil, err
}
pubKeyPath := keyDir + "/" + DefaultPubKeyFilename
pubKeyBytes := blsSignatures.PublicKeyToBytes(pubKey)
encodedPubKey := make([]byte, base64.StdEncoding.EncodedLen(len(pubKeyBytes)))
base64.StdEncoding.Encode(encodedPubKey, pubKeyBytes)
err = os.WriteFile(pubKeyPath, encodedPubKey, 0o600)
if err != nil {
return nil, nil, err
}
privKeyPath := keyDir + "/" + DefaultPrivKeyFilename
privKeyBytes := blsSignatures.PrivateKeyToBytes(privKey)
encodedPrivKey := make([]byte, base64.StdEncoding.EncodedLen(len(privKeyBytes)))
base64.StdEncoding.Encode(encodedPrivKey, privKeyBytes)
err = os.WriteFile(privKeyPath, encodedPrivKey, 0o600)
if err != nil {
return nil, nil, err
}
return &pubKey, &privKey, nil
}
func ReadKeysFromFile(keyDir string) (*blsSignatures.PublicKey, blsSignatures.PrivateKey, error) {
pubKey, err := ReadPubKeyFromFile(keyDir + "/" + DefaultPubKeyFilename)
if err != nil {
return nil, nil, err
}
privKey, err := ReadPrivKeyFromFile(keyDir + "/" + DefaultPrivKeyFilename)
if err != nil {
return nil, nil, err
}
return pubKey, privKey, nil
}
func ReadPubKeyFromFile(pubKeyPath string) (*blsSignatures.PublicKey, error) {
pubKeyEncodedBytes, err := os.ReadFile(pubKeyPath)
if err != nil {
return nil, err
}
pubKey, err := DecodeBase64BLSPublicKey(pubKeyEncodedBytes)
if err != nil {
return nil, err
}
return pubKey, nil
}
func ReadPrivKeyFromFile(privKeyPath string) (blsSignatures.PrivateKey, error) {
privKeyEncodedBytes, err := os.ReadFile(privKeyPath)
if err != nil {
return nil, err
}
privKey, err := DecodeBase64BLSPrivateKey(privKeyEncodedBytes)
if err != nil {
return nil, err
}
return privKey, nil
}
func GenerateAndStoreECDSAKeys(dir string) error {
privateKey, err := crypto.GenerateKey()
if err != nil {
return err
}
err = crypto.SaveECDSA(dir+"/ecdsa", privateKey)
if err != nil {
return err
}
encodedPubKey := hex.EncodeToString(crypto.FromECDSAPub(&privateKey.PublicKey))
return os.WriteFile(dir+"/ecdsa.pub", []byte(encodedPubKey), 0o600)
}