-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from algorand/maxj/add_mnemonic_type_support
Add mnemonic support for private keys/mdk
- Loading branch information
Showing
2 changed files
with
72 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package mnemonic | ||
|
||
import ( | ||
"golang.org/x/crypto/ed25519" | ||
|
||
"github.com/algorand/go-algorand-sdk/types" | ||
) | ||
|
||
// FromPrivateKey is a helper that converts an ed25519 private key to a | ||
// human-readable mnemonic | ||
func FromPrivateKey(sk ed25519.PrivateKey) (string, error) { | ||
seed := sk.Seed() | ||
return FromKey(seed) | ||
} | ||
|
||
// ToPrivateKey is a helper that converts a mnemonic directly to an ed25519 | ||
// private key | ||
func ToPrivateKey(mnemonic string) (sk ed25519.PrivateKey, err error) { | ||
seedBytes, err := ToKey(mnemonic) | ||
if err != nil { | ||
return | ||
} | ||
return ed25519.NewKeyFromSeed(seedBytes), nil | ||
} | ||
|
||
// FromMasterDerivationKey is a helper that converts an MDK to a human-readable | ||
// mnemonic | ||
func FromMasterDerivationKey(mdk types.MasterDerivationKey) (string, error) { | ||
return FromKey(mdk[:]) | ||
} | ||
|
||
// ToMasterDerivationKey is a helper that converts a mnemonic directly to a | ||
// master derivation key | ||
func ToMasterDerivationKey(mnemonic string) (mdk types.MasterDerivationKey, err error) { | ||
mdkBytes, err := ToKey(mnemonic) | ||
if err != nil { | ||
return | ||
} | ||
if len(mdkBytes) != len(mdk) { | ||
panic("recovered mdk is wrong length") | ||
} | ||
copy(mdk[:], mdkBytes) | ||
return | ||
} |
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