github.com/algorand/go-algorand-sdk@v1.24.0/mnemonic/helpers.go (about)

     1  package mnemonic
     2  
     3  import (
     4  	"golang.org/x/crypto/ed25519"
     5  
     6  	"github.com/algorand/go-algorand-sdk/types"
     7  )
     8  
     9  // FromPrivateKey is a helper that converts an ed25519 private key to a
    10  // human-readable mnemonic
    11  func FromPrivateKey(sk ed25519.PrivateKey) (string, error) {
    12  	seed := sk.Seed()
    13  	return FromKey(seed)
    14  }
    15  
    16  // ToPrivateKey is a helper that converts a mnemonic directly to an ed25519
    17  // private key
    18  func ToPrivateKey(mnemonic string) (sk ed25519.PrivateKey, err error) {
    19  	seedBytes, err := ToKey(mnemonic)
    20  	if err != nil {
    21  		return
    22  	}
    23  	return ed25519.NewKeyFromSeed(seedBytes), nil
    24  }
    25  
    26  // FromMasterDerivationKey is a helper that converts an MDK to a human-readable
    27  // mnemonic
    28  func FromMasterDerivationKey(mdk types.MasterDerivationKey) (string, error) {
    29  	return FromKey(mdk[:])
    30  }
    31  
    32  // ToMasterDerivationKey is a helper that converts a mnemonic directly to a
    33  // master derivation key
    34  func ToMasterDerivationKey(mnemonic string) (mdk types.MasterDerivationKey, err error) {
    35  	mdkBytes, err := ToKey(mnemonic)
    36  	if err != nil {
    37  		return
    38  	}
    39  	if len(mdkBytes) != len(mdk) {
    40  		panic("recovered mdk is wrong length")
    41  	}
    42  	copy(mdk[:], mdkBytes)
    43  	return
    44  }