github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/examples/generate_key_with_mnemonic/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/hashgraph/hedera-sdk-go/v2"
     8  )
     9  
    10  func main() {
    11  	// Generate 24 word mnemonic
    12  	mnemonic24, err := hedera.GenerateMnemonic24()
    13  	if err != nil {
    14  		panic(fmt.Sprintf("%v : error generating 24 word mnemonic", err))
    15  	}
    16  
    17  	// Generate 12 word mnemonic
    18  	mnemonic12, err := hedera.GenerateMnemonic12()
    19  	if err != nil {
    20  		panic(fmt.Sprintf("%v : error generating 12 word mnemonic", err))
    21  	}
    22  
    23  	// Given legacy string
    24  	legacyString := "jolly,kidnap,tom,lawn,drunk,chick,optic,lust,mutter,mole,bride,galley,dense,member,sage,neural,widow,decide,curb,aboard,margin,manure"
    25  
    26  	// Initializing a legacy mnemonic from legacy string
    27  	mnemonicLegacy, err := hedera.NewMnemonic(strings.Split(legacyString, ","))
    28  	if err != nil {
    29  		panic(fmt.Sprintf("%v : error generating mnemonic from legacy string", err))
    30  	}
    31  
    32  	fmt.Printf("mnemonic 24 word = %v\n", mnemonic24)
    33  	fmt.Printf("mnemonic 12 word = %v\n", mnemonic12)
    34  	fmt.Printf("mnemonic legacy = %v\n", mnemonicLegacy)
    35  
    36  	// Creating a Private Key from 24 word mnemonic with an optional passphrase
    37  	privateKey24, err := mnemonic24.ToPrivateKey( /* passphrase */ "")
    38  	if err != nil {
    39  		panic(fmt.Sprintf("%v : error converting 24 word mnemonic to PrivateKey", err))
    40  	}
    41  
    42  	// Creating a Private Key from 12 word mnemonic with an optional passphrase
    43  	privateKey12, err := mnemonic12.ToPrivateKey( /* passphrase */ "")
    44  	if err != nil {
    45  		panic(fmt.Sprintf("%v : error converting 12 word mnemonic to PrivateKey", err))
    46  	}
    47  
    48  	// ToLegacyPrivateKey() doesn't support a passphrase
    49  	privateLegacy, err := mnemonicLegacy.ToLegacyPrivateKey()
    50  	if err != nil {
    51  		panic(fmt.Sprintf("%v : error converting legacy mnemonic to PrivateKey", err))
    52  	}
    53  
    54  	// Retrieving the Public Key
    55  	publicKey24 := privateKey24.PublicKey()
    56  	publicKey12 := privateKey12.PublicKey()
    57  	publicLegacy := privateLegacy.PublicKey()
    58  
    59  	fmt.Printf("private 24 word = %v\n", privateKey24)
    60  	fmt.Printf("public 24 word = %v\n", publicKey24)
    61  
    62  	fmt.Printf("private 12 word = %v\n", privateKey12)
    63  	fmt.Printf("public 12 word = %v\n", publicKey12)
    64  
    65  	fmt.Printf("private legacy = %v\n", privateLegacy)
    66  	fmt.Printf("public legacy = %v\n", publicLegacy)
    67  }