forked from atticlab/wormhole
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor all node key management into nodekeys.go
- Loading branch information
Leo
committed
Aug 21, 2020
1 parent
5b7b80d
commit 935411c
Showing
3 changed files
with
77 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package main | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
|
||
ethcrypto "github.com/ethereum/go-ethereum/crypto" | ||
p2pcrypto "github.com/libp2p/go-libp2p-core/crypto" | ||
"go.uber.org/zap" | ||
|
||
"github.com/certusone/wormhole/bridge/pkg/devnet" | ||
) | ||
|
||
func loadGuardianKey(logger *zap.Logger) *ecdsa.PrivateKey { | ||
var gk *ecdsa.PrivateKey | ||
|
||
if *unsafeDevMode { | ||
// Figure out our devnet index | ||
idx, err := devnet.GetDevnetIndex() | ||
if err != nil { | ||
logger.Fatal("Failed to parse hostname - are we running in devnet?") | ||
} | ||
|
||
// Generate guardian key | ||
gk = devnet.DeterministicEcdsaKeyByIndex(ethcrypto.S256(), uint64(idx)) | ||
} else { | ||
panic("not implemented") // TODO | ||
} | ||
|
||
logger.Info("Loaded guardian key", zap.String( | ||
"address", ethcrypto.PubkeyToAddress(gk.PublicKey).String())) | ||
|
||
return gk | ||
} | ||
|
||
func getOrCreateNodeKey(logger *zap.Logger, path string) (p2pcrypto.PrivKey, error) { | ||
b, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
logger.Info("No node key found, generating a new one...", zap.String("path", path)) | ||
|
||
// TODO(leo): what does -1 mean? | ||
priv, _, err := p2pcrypto.GenerateKeyPair(p2pcrypto.Ed25519, -1) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
s, err := p2pcrypto.MarshalPrivateKey(priv) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
err = ioutil.WriteFile(path, s, 0600) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to write node key: %w", err) | ||
} | ||
|
||
return priv, nil | ||
} else { | ||
return nil, fmt.Errorf("failed to read node key: %w", err) | ||
} | ||
} | ||
|
||
priv, err := p2pcrypto.UnmarshalPrivateKey(b) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to unmarshal node key: %w", err) | ||
} | ||
|
||
logger.Info("Found existing node key", zap.String("path", path)) | ||
|
||
return priv, nil | ||
} |