Skip to content

Commit

Permalink
Added generateSecretPhrase
Browse files Browse the repository at this point in the history
Creating a 12-word secret phrase (nmonic) based on the blox private key
  • Loading branch information
ehsan6sha committed Feb 19, 2024
1 parent 8d112d6 commit 0c7f851
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
38 changes: 31 additions & 7 deletions cmd/blox/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
Expand Down Expand Up @@ -43,6 +44,7 @@ import (
"github.com/mdp/qrterminal"
"github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
bip39 "github.com/tyler-smith/go-bip39"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v2/altsrc"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -72,13 +74,14 @@ var (
logger = logging.Logger("fula/cmd/blox")
app struct {
cli.App
initOnly bool
blockchainEndpoint string
secretsPath string
generateNodeKey bool
wireless bool
configPath string
config struct {
initOnly bool
blockchainEndpoint string
secretsPath string
generateNodeKey bool
generateSecretPhrase bool
wireless bool
configPath string
config struct {
Identity string `yaml:"identity"`
StoreDir string `yaml:"storeDir"`
PoolName string `yaml:"poolName"`
Expand Down Expand Up @@ -377,6 +380,11 @@ func init() {
Usage: "Generate node key from identity",
Destination: &app.generateNodeKey,
},
&cli.BoolFlag{
Name: "generateSecretPhrase",
Usage: "Generate 12-word Secret Phrase from identity",
Destination: &app.generateSecretPhrase,
},
&cli.StringFlag{
Name: "blockchainEndpoint",
Usage: "Change the blockchain APIs endpoint",
Expand Down Expand Up @@ -611,6 +619,22 @@ func action(ctx *cli.Context) error {
fmt.Print(nodeKey)
return nil // Exit after generating the node key
}
if app.generateSecretPhrase {
// Execute ConvertBase64PrivateKeyToHexNodeKey with the identity as input
hash := sha256.Sum256([]byte(app.config.Identity))

// Convert the first 128 bits of the hash to entropy for a 12-word mnemonic.
// BIP39 expects entropy to be a multiple of 32 bits, so we use the first 16 bytes of the hash.
entropy := hash[:16]

// Generate the mnemonic from the entropy
mnemonic, err := bip39.NewMnemonic(entropy)
if err != nil {
return fmt.Errorf("error NewMnemonic: %v", err)
}
fmt.Print(mnemonic)
return nil // Exit after generating the node key
}
authorizer, err := peer.Decode(app.config.Authorizer)
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ require (
github.com/stretchr/testify v1.8.4 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
github.com/twmb/murmur3 v1.1.8 // indirect
github.com/tyler-smith/go-bip39 v1.1.0
github.com/ucarion/urlpath v0.0.0-20200424170820-7ccc79b76bbb // indirect
github.com/whyrusleeping/base32 v0.0.0-20170828182744-c30ac30633cc // indirect
github.com/whyrusleeping/cbor v0.0.0-20171005072247-63513f603b11 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,8 @@ github.com/tv42/httpunix v0.0.0-20191220191345-2ba4b9c3382c h1:u6SKchux2yDvFQnDH
github.com/tv42/httpunix v0.0.0-20191220191345-2ba4b9c3382c/go.mod h1:hzIxponao9Kjc7aWznkXaL4U4TWaDSs8zcsY4Ka08nM=
github.com/twmb/murmur3 v1.1.8 h1:8Yt9taO/WN3l08xErzjeschgZU2QSrwm1kclYq+0aRg=
github.com/twmb/murmur3 v1.1.8/go.mod h1:Qq/R7NUyOfr65zD+6Q5IHKsJLwP7exErjN6lyyq3OSQ=
github.com/tyler-smith/go-bip39 v1.1.0 h1:5eUemwrMargf3BSLRRCalXT93Ns6pQJIjYQN2nyfOP8=
github.com/tyler-smith/go-bip39 v1.1.0/go.mod h1:gUYDtqQw1JS3ZJ8UWVcGTGqqr6YIN3CWg+kkNaLt55U=
github.com/ucarion/urlpath v0.0.0-20200424170820-7ccc79b76bbb h1:Ywfo8sUltxogBpFuMOFRrrSifO788kAFxmvVw31PtQQ=
github.com/ucarion/urlpath v0.0.0-20200424170820-7ccc79b76bbb/go.mod h1:ikPs9bRWicNw3S7XpJ8sK/smGwU9WcSVU3dy9qahYBM=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
Expand Down

0 comments on commit 0c7f851

Please sign in to comment.