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 2 commits
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
12 changes: 7 additions & 5 deletions examples/graphql/graphql.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package main

import (
"github.com/BuxOrg/go-buxclient/xkeys"
"log"

"github.com/BuxOrg/go-buxclient"
"github.com/bitcoinschema/go-bitcoin/v2"
)

func main() {

// Example xPub
masterKey, _ := bitcoin.GenerateHDKey(bitcoin.SecureSeedLength)
// rawXPub, _ := bitcoin.GetExtendedPublicKey(masterKey)
// Generate keys
keys, resErr := xkeys.Generate()
if resErr != nil {
log.Fatalln(resErr.Error())
}

// Create a client
buxClient, err := buxclient.New(
buxclient.WithXPriv(masterKey.String()),
buxclient.WithXPriv(keys.Xpriv.String()),
buxclient.WithGraphQL("localhost:3001"),
buxclient.WithDebugging(true),
buxclient.WithSignRequest(true),
Expand Down
12 changes: 7 additions & 5 deletions examples/http/http.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package main

import (
"github.com/BuxOrg/go-buxclient/xkeys"
"log"

"github.com/BuxOrg/go-buxclient"
"github.com/bitcoinschema/go-bitcoin/v2"
)

func main() {

// Example xPub
masterKey, _ := bitcoin.GenerateHDKey(bitcoin.SecureSeedLength)
// rawXPub, _ := bitcoin.GetExtendedPublicKey(masterKey)
// Generate keys
keys, resErr := xkeys.Generate()
if resErr != nil {
log.Fatalln(resErr.Error())
}

// Create a client
buxClient, err := buxclient.New(
buxclient.WithXPriv(masterKey.String()),
buxclient.WithXPriv(keys.Xpriv.String()),
buxclient.WithHTTP("localhost:3001"),
buxclient.WithDebugging(true),
buxclient.WithSignRequest(true),
Expand Down
16 changes: 8 additions & 8 deletions examples/new_paymail/new_paymail.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ package main

import (
"context"
"github.com/BuxOrg/go-buxclient/xkeys"
"log"

"github.com/bitcoinschema/go-bitcoin/v2"

"github.com/BuxOrg/go-buxclient"
)

func main() {

// Example xPub
masterKey, _ := bitcoin.GenerateHDKey(bitcoin.SecureSeedLength)

rawXPub, _ := bitcoin.GetExtendedPublicKey(masterKey)
// Generate keys
keys, resErr := xkeys.Generate()
if resErr != nil {
log.Fatalln(resErr.Error())
}

// Create a client
buxClient, err := buxclient.New(
buxclient.WithXPriv(masterKey.String()),
buxclient.WithXPriv(keys.Xpriv.String()),
buxclient.WithHTTP("localhost:3001"),
buxclient.WithDebugging(true),
buxclient.WithSignRequest(true),
Expand All @@ -28,7 +28,7 @@ func main() {
}

log.Printf("client loaded - bux debug: %v", buxClient.IsDebug())
err = buxClient.NewPaymail(context.Background(), rawXPub, "foo@domain.com", "", "Foo", nil)
err = buxClient.NewPaymail(context.Background(), keys.Xpub.String(), "foo@domain.com", "", "Foo", nil)

if err != nil {
log.Fatalln(err.Error())
Expand Down
16 changes: 9 additions & 7 deletions examples/register_xpub/register_xpub.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ package main

import (
"context"
"github.com/BuxOrg/go-buxclient/xkeys"
"log"

buxmodels "github.com/BuxOrg/bux-models"
"github.com/BuxOrg/go-buxclient"
"github.com/bitcoinschema/go-bitcoin/v2"
)

func main() {
// Example xPub
masterKey, _ := bitcoin.GenerateHDKey(bitcoin.SecureSeedLength)
rawXPub, _ := bitcoin.GetExtendedPublicKey(masterKey)
// Generate keys
keys, resErr := xkeys.Generate()
if resErr != nil {
log.Fatalln(resErr.Error())
}

// Create a client
buxClient, err := buxclient.New(
buxclient.WithXPriv(masterKey.String()),
buxclient.WithXPriv(keys.Xpriv.String()),
buxclient.WithHTTP("localhost:3001"),
buxclient.WithDebugging(true),
buxclient.WithSignRequest(true),
Expand All @@ -26,10 +28,10 @@ func main() {
}

if err = buxClient.NewXpub(
context.Background(), rawXPub, &buxmodels.Metadata{"example_field": "example_data"},
context.Background(), keys.Xpub.String(), &buxmodels.Metadata{"example_field": "example_data"},
); err != nil {
log.Fatalln(err.Error())
}

log.Println("registered xPub: " + rawXPub)
log.Println("registered xPub: " + keys.Xpub.String())
}
58 changes: 58 additions & 0 deletions xkeys/xkeys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package xkeys

import (
"github.com/BuxOrg/go-buxclient/transports"
"github.com/libsv/go-bk/bip32"
"github.com/libsv/go-bk/bip39"
"github.com/libsv/go-bk/chaincfg"
)

// Keys is a struct containing the xpriv, xpub and mnemonic
type Keys struct {
Xpriv *bip32.ExtendedKey
Xpub *bip32.ExtendedKey
pawellewandowski98 marked this conversation as resolved.
Show resolved Hide resolved
Mnemonic string
}

// Generate generates a random set of keys - xpriv, xpb and mnemonic
func Generate() (*Keys, transports.ResponseError) {
pawellewandowski98 marked this conversation as resolved.
Show resolved Hide resolved
entropy, err := bip39.GenerateEntropy(160)
if err != nil {
return nil, transports.WrapError(err)
}

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

if err != nil {
return nil, transports.WrapError(err)
}

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

if err != nil {
return nil, transports.WrapError(err)
}

hdXpub, err := hdXpriv.Neuter()
if err != nil {
return nil, transports.WrapError(err)
}

keys := &Keys{
Xpriv: hdXpriv,
Xpub: hdXpub,
Mnemonic: mnemonic,
}

return keys, nil
}

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