forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: move server.GenerateCoinKey and server.GenerateSaveCoinKey to …
…testutil (cosmos#10956) * chore: move server.GenerateCoinKey and server.GenerateSaveCoinKey to testutil * add changelog * move testutil/known_values.go to testutil/testdata * changelog
- Loading branch information
1 parent
7b29a91
commit 2627b59
Showing
15 changed files
with
129 additions
and
60 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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package testutil | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/cosmos/cosmos-sdk/crypto/keyring" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// GenerateCoinKey generates a new key mnemonic along with its addrress. | ||
func GenerateCoinKey(algo keyring.SignatureAlgo) (sdk.AccAddress, string, error) { | ||
// generate a private key, with mnemonic | ||
info, secret, err := keyring.NewInMemory().NewMnemonic( | ||
"name", | ||
keyring.English, | ||
sdk.GetConfig().GetFullBIP44Path(), | ||
keyring.DefaultBIP39Passphrase, | ||
algo, | ||
) | ||
if err != nil { | ||
return sdk.AccAddress{}, "", err | ||
} | ||
|
||
return sdk.AccAddress(info.GetPubKey().Address()), secret, nil | ||
} | ||
|
||
// GenerateSaveCoinKey generates a new key mnemonic with its addrress. | ||
// If mnemonic is provided then it's used for key generation. | ||
// The key is saved in the keyring. The function returns error if overwrite=true and the key | ||
// already exists. | ||
func GenerateSaveCoinKey( | ||
keybase keyring.Keyring, | ||
keyName, mnemonic string, | ||
overwrite bool, | ||
algo keyring.SignatureAlgo, | ||
) (sdk.AccAddress, string, error) { | ||
exists := false | ||
_, err := keybase.Key(keyName) | ||
if err == nil { | ||
exists = true | ||
} | ||
|
||
// ensure no overwrite | ||
if !overwrite && exists { | ||
return sdk.AccAddress{}, "", fmt.Errorf("key already exists, overwrite is disabled") | ||
} | ||
|
||
if exists { | ||
if err := keybase.Delete(keyName); err != nil { | ||
return sdk.AccAddress{}, "", fmt.Errorf("failed to overwrite key") | ||
} | ||
} | ||
|
||
var ( | ||
info keyring.Info | ||
secret string | ||
) | ||
|
||
if mnemonic != "" { | ||
secret = mnemonic | ||
info, err = keybase.NewAccount( | ||
keyName, | ||
mnemonic, | ||
keyring.DefaultBIP39Passphrase, | ||
sdk.GetConfig().GetFullBIP44Path(), | ||
algo, | ||
) | ||
} else { | ||
info, secret, err = keybase.NewMnemonic( | ||
keyName, | ||
keyring.English, | ||
sdk.GetConfig().GetFullBIP44Path(), | ||
keyring.DefaultBIP39Passphrase, | ||
algo, | ||
) | ||
} | ||
if err != nil { | ||
return sdk.AccAddress{}, "", err | ||
} | ||
|
||
return sdk.AccAddress(info.GetAddress()), secret, nil | ||
} |
Oops, something went wrong.