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.
bridge: implement bridge key serialization
ghstack-source-id: f218021514618ae1eb8f03d7cc158b1114c45297 Pull Request resolved: wormhole-foundation#90
- Loading branch information
Leo
committed
Nov 19, 2020
1 parent
114524a
commit d9f8174
Showing
5 changed files
with
109 additions
and
28 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 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,66 @@ | ||
package guardiand | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"fmt" | ||
"io/ioutil" | ||
|
||
ethcrypto "github.com/ethereum/go-ethereum/crypto" | ||
"google.golang.org/protobuf/encoding/prototext" | ||
|
||
"github.com/certusone/wormhole/bridge/pkg/devnet" | ||
nodev1 "github.com/certusone/wormhole/bridge/pkg/proto/node/v1" | ||
) | ||
|
||
// loadGuardianKey loads a serialized guardian key from disk. | ||
func loadGuardianKey(filename string) (*ecdsa.PrivateKey, error) { | ||
b, err := ioutil.ReadFile(filename) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read guardian private key from disk: %w", err) | ||
} | ||
|
||
var m nodev1.GuardianKey | ||
err = prototext.Unmarshal(b, &m) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to deserialize private key from disk: %w", err) | ||
} | ||
|
||
gk, err := ethcrypto.ToECDSA(m.Data) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to deserialize key data: %w", err) | ||
} | ||
|
||
return gk, nil | ||
} | ||
|
||
// writeGuardianKey serializes a guardian key and writes it to disk. | ||
func writeGuardianKey(key *ecdsa.PrivateKey, description string, filename string) error { | ||
m := &nodev1.GuardianKey{ | ||
Description: description, | ||
Data: ethcrypto.FromECDSA(key), | ||
Pubkey: ethcrypto.PubkeyToAddress(key.PublicKey).String(), | ||
} | ||
|
||
b, err := prototext.MarshalOptions{Multiline: true, EmitASCII: true}.Marshal(m) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if err := ioutil.WriteFile(filename, b, 0600); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// generateDevnetGuardianKey returns a deterministic testnet key. | ||
func generateDevnetGuardianKey() (*ecdsa.PrivateKey, error) { | ||
// Figure out our devnet index | ||
idx, err := devnet.GetDevnetIndex() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Generate guardian key | ||
return devnet.DeterministicEcdsaKeyByIndex(ethcrypto.S256(), uint64(idx)), nil | ||
} |
26 changes: 0 additions & 26 deletions
26
bridge/cmd/guardiand/nodekeys.go → bridge/cmd/guardiand/nodekey.go
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 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 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