Skip to content

Commit

Permalink
chore(BUX-190): move new methods into separate package
Browse files Browse the repository at this point in the history
  • Loading branch information
pawellewandowski98 committed Oct 18, 2023
1 parent 8f8ca5f commit c09804e
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 89 deletions.
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
26 changes: 0 additions & 26 deletions examples/keys/keys.go

This file was deleted.

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, "[email protected]", "", "Foo", nil)
err = buxClient.NewPaymail(context.Background(), keys.Xpub.String(), "[email protected]", "", "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())
}
38 changes: 0 additions & 38 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ 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 @@ -86,38 +83,3 @@ 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) {
entropy, err := bip39.GenerateEntropy(160)
if err != nil {
panic(err)
}

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

if err != nil {
fmt.Println(err)
panic(err)
}

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

if err != nil {
fmt.Println(err)
panic(err)
}

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
}
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
Mnemonic string
}

// Generate generates a random set of keys - xpriv, xpb and mnemonic
func Generate() (*Keys, transports.ResponseError) {
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
}

0 comments on commit c09804e

Please sign in to comment.