Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(BUX-190): add keys generation #166

Merged
merged 6 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions examples/keys/keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main
pawellewandowski98 marked this conversation as resolved.
Show resolved Hide resolved

import (
"fmt"
"github.com/BuxOrg/go-buxclient/utils"
)

func main() {
// Generate a random set of keys
hdXpriv, hdXpub := utils.GenerateRandomSetOfKeys()
pawellewandowski98 marked this conversation as resolved.
Show resolved Hide resolved

fmt.Println("Your XPriv: ", hdXpriv)
fmt.Println("Your XPub: ", hdXpub)

xpub, err := utils.GetPublicKeyFromHDPrivateKey(hdXpriv.String())
dorzepowski marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
panic(err)
}

// hdXpub and xpub should be this same
if hdXpub.String() != xpub.String() {
panic("xpub and hdXpub are not the same")
} else {
fmt.Println("Keys generated successfully, xpub and hdXpub are the same")
}
}
38 changes: 38 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"fmt"
"github.com/libsv/go-bk/bip39"
"github.com/libsv/go-bk/chaincfg"
"math"
"strconv"

Expand Down Expand Up @@ -83,3 +86,38 @@ func GetChildNumsFromHex(hexHash string) ([]uint32, error) {

return childNums, nil
}

// GenerateRandomSetOfKeys generates a random set of keys - xpriv, xpb and address
func GenerateRandomSetOfKeys() (hdXpriv *bip32.ExtendedKey, hdXpub *bip32.ExtendedKey) {
pawellewandowski98 marked this conversation as resolved.
Show resolved Hide resolved
entropy, err := bip39.GenerateEntropy(160)
if err != nil {
panic(err)
}

_, seed, err := bip39.Mnemonic(entropy, "")

if err != nil {
fmt.Println(err)
panic(err)
pawellewandowski98 marked this conversation as resolved.
Show resolved Hide resolved
}

hdXpriv, err = bip32.NewMaster(seed, &chaincfg.MainNet)

if err != nil {
fmt.Println(err)
panic(err)
pawellewandowski98 marked this conversation as resolved.
Show resolved Hide resolved
}

hdXpub, err = hdXpriv.Neuter()
return
}

// GetPublicKeyFromHDPrivateKey returns the public key from the HD private key
func GetPublicKeyFromHDPrivateKey(hdXpriv string) (hdXpub *bip32.ExtendedKey, err error) {
hdKey, err := bip32.NewKeyFromString(hdXpriv)
hdXpub, err = hdKey.Neuter()
if err != nil {
return nil, err
}
return
}