-
Notifications
You must be signed in to change notification settings - Fork 173
feat: Solana relayer (fee payer) key importer, encryption and decryption #2673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
2d225c8
configure observer relayer key for Solana; remove hardcoded solana te…
ws4charlie 5da629e
implementation of relayer key importer, encryption and decryption
ws4charlie 6a64051
integrate relayer key into E2E and Solana signer
ws4charlie 66b7027
add relayer_key_balance metrics and unit tests
ws4charlie f751dd7
Merge branch 'develop' of https://github.com/zeta-chain/node into fea…
ws4charlie 5d11488
use TrimSpace to trim password
ws4charlie e8736a0
add changelog entry
ws4charlie 98f041a
use relayer account array in E2E config; a few renaming; add private …
ws4charlie 755ee5c
Merge branch 'develop' of https://github.com/zeta-chain/node into fea…
ws4charlie 2e2b8a9
fix linter
ws4charlie 3a415d2
remove GetNetworkName method for simplification
ws4charlie 3d2ebad
added PromptPassword method to prompt single password
ws4charlie dd56b78
Merge branch 'develop' into feat-solana-fee-payer-key
lumtis ad8773f
use network name as map index to store relayer key passwords
ws4charlie 6a4399c
moved relayer passwords to chain registry
ws4charlie 7c75231
Merge branch 'develop' of https://github.com/zeta-chain/node into fea…
ws4charlie c9f0b42
airdrop SOL token only if solana local node is available
ws4charlie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,153 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/pkg/errors" | ||
| "github.com/rs/zerolog/log" | ||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/zeta-chain/zetacore/pkg/chains" | ||
| "github.com/zeta-chain/zetacore/pkg/crypto" | ||
| zetaos "github.com/zeta-chain/zetacore/pkg/os" | ||
| "github.com/zeta-chain/zetacore/zetaclient/keys" | ||
| ) | ||
|
|
||
| var CmdImportRelayerKey = &cobra.Command{ | ||
| Use: "import-relayer-key --network=<network> --private-key=<private-key> --password=<password> --relayer-key-path=<relayer-key-path>", | ||
| Short: "Import a relayer private key", | ||
| Example: `zetaclientd import-relayer-key --network=7 --private-key=<your_private_key> --password=<your_password>`, | ||
| RunE: ImportRelayerKey, | ||
| } | ||
|
|
||
| var CmdRelayerAddress = &cobra.Command{ | ||
| Use: "relayer-address --network=<network> --password=<password> --relayer-key-path=<relayer-key-path>", | ||
| Short: "Show the relayer address", | ||
| Example: `zetaclientd relayer-address --network=7 --password=my_password`, | ||
| RunE: ShowRelayerAddress, | ||
| } | ||
|
|
||
| var importArgs = importRelayerKeyArguments{} | ||
| var addressArgs = relayerAddressArguments{} | ||
|
|
||
| // importRelayerKeyArguments is the struct that holds the arguments for the import command | ||
| type importRelayerKeyArguments struct { | ||
| network int32 | ||
| privateKey string | ||
| password string | ||
| relayerKeyPath string | ||
| } | ||
|
|
||
| // relayerAddressArguments is the struct that holds the arguments for the show command | ||
| type relayerAddressArguments struct { | ||
| network int32 | ||
| password string | ||
| relayerKeyPath string | ||
| } | ||
|
|
||
| func init() { | ||
| RootCmd.AddCommand(CmdImportRelayerKey) | ||
| RootCmd.AddCommand(CmdRelayerAddress) | ||
|
|
||
| // resolve default relayer key path | ||
| defaultRelayerKeyPath := "~/.zetacored/relayer-keys" | ||
| defaultRelayerKeyPath, err := zetaos.ExpandHomeDir(defaultRelayerKeyPath) | ||
| if err != nil { | ||
| log.Fatal().Err(err).Msg("failed to resolve default relayer key path") | ||
| } | ||
|
|
||
| CmdImportRelayerKey.Flags().Int32Var(&importArgs.network, "network", 7, "network id, (7: solana)") | ||
| CmdImportRelayerKey.Flags(). | ||
| StringVar(&importArgs.privateKey, "private-key", "", "the relayer private key to import") | ||
| CmdImportRelayerKey.Flags(). | ||
| StringVar(&importArgs.password, "password", "", "the password to encrypt the relayer private key") | ||
| CmdImportRelayerKey.Flags(). | ||
| StringVar(&importArgs.relayerKeyPath, "relayer-key-path", defaultRelayerKeyPath, "path to relayer keys") | ||
|
|
||
| CmdRelayerAddress.Flags().Int32Var(&addressArgs.network, "network", 7, "network id, (7:solana)") | ||
| CmdRelayerAddress.Flags(). | ||
| StringVar(&addressArgs.password, "password", "", "the password to decrypt the relayer private key") | ||
| CmdRelayerAddress.Flags(). | ||
| StringVar(&addressArgs.relayerKeyPath, "relayer-key-path", defaultRelayerKeyPath, "path to relayer keys") | ||
| } | ||
|
|
||
| // ImportRelayerKey imports a relayer private key | ||
| func ImportRelayerKey(_ *cobra.Command, _ []string) error { | ||
| // validate private key and password | ||
| if importArgs.privateKey == "" { | ||
| return errors.New("must provide a private key") | ||
| } | ||
| if importArgs.password == "" { | ||
| return errors.New("must provide a password") | ||
| } | ||
| if !keys.IsRelayerPrivateKeyValid(importArgs.privateKey, chains.Network(importArgs.network)) { | ||
| return errors.New("invalid private key") | ||
| } | ||
|
|
||
| // resolve the relayer key file path | ||
| fileName, err := keys.ResolveRelayerKeyFile(importArgs.relayerKeyPath, chains.Network(importArgs.network)) | ||
| if err != nil { | ||
| return errors.Wrap(err, "failed to resolve relayer key file path") | ||
| } | ||
|
|
||
| // create path (owner `rwx` permissions) if it does not exist | ||
| keyPath := filepath.Dir(fileName) | ||
| if _, err := os.Stat(keyPath); os.IsNotExist(err) { | ||
| if err := os.MkdirAll(keyPath, 0o700); err != nil { | ||
| return errors.Wrapf(err, "failed to create relayer key path: %s", keyPath) | ||
| } | ||
| } | ||
|
|
||
| // avoid overwriting existing key file | ||
| if zetaos.FileExists(fileName) { | ||
| return errors.Errorf( | ||
| "relayer key %s already exists, please backup and remove it before importing a new key", | ||
| fileName, | ||
| ) | ||
| } | ||
|
|
||
| // encrypt the private key | ||
| ciphertext, err := crypto.EncryptAES256GCMBase64(importArgs.privateKey, importArgs.password) | ||
| if err != nil { | ||
| return errors.Wrap(err, "private key encryption failed") | ||
| } | ||
|
|
||
| // create the relayer key file | ||
| err = keys.WriteRelayerKeyToFile(fileName, keys.RelayerKey{PrivateKey: ciphertext}) | ||
| if err != nil { | ||
| return errors.Wrapf(err, "failed to create relayer key file: %s", fileName) | ||
| } | ||
| fmt.Printf("successfully imported relayer key: %s\n", fileName) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // ShowRelayerAddress shows the relayer address | ||
| func ShowRelayerAddress(_ *cobra.Command, _ []string) error { | ||
| // try loading the relayer key if present | ||
| network := chains.Network(addressArgs.network) | ||
| relayerKey, err := keys.LoadRelayerKey(addressArgs.relayerKeyPath, network, addressArgs.password) | ||
| if err != nil { | ||
| return errors.Wrap(err, "failed to load relayer key") | ||
| } | ||
|
|
||
| // relayer key does not exist, return error | ||
| if relayerKey == nil { | ||
| return fmt.Errorf( | ||
| "relayer key not found for network %d in path: %s", | ||
| addressArgs.network, | ||
| addressArgs.relayerKeyPath, | ||
| ) | ||
| } | ||
|
|
||
| // resolve the relayer address | ||
| networkName, address, err := relayerKey.ResolveAddress(network) | ||
| if err != nil { | ||
| return errors.Wrap(err, "failed to resolve relayer address") | ||
| } | ||
| fmt.Printf("relayer address (%s): %s\n", networkName, address) | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.