Skip to content

Commit

Permalink
chore(BUX-190): refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
pawellewandowski98 committed Nov 13, 2023
1 parent c09804e commit 11b82d8
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 73 deletions.
6 changes: 3 additions & 3 deletions examples/graphql/graphql.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

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

"github.com/BuxOrg/go-buxclient"
Expand All @@ -10,14 +10,14 @@ import (
func main() {

// Generate keys
keys, resErr := xkeys.Generate()
keys, resErr := xpriv.Generate()
if resErr != nil {
log.Fatalln(resErr.Error())
}

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

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

"github.com/BuxOrg/go-buxclient"
Expand All @@ -10,14 +10,14 @@ import (
func main() {

// Generate keys
keys, resErr := xkeys.Generate()
keys, resErr := xpriv.Generate()
if resErr != nil {
log.Fatalln(resErr.Error())
}

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

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

func main() {
//Generate keys
keys, err := xpriv.Generate()
if err != nil {
panic(err)
}

fmt.Println("<-- Generate method")
fmt.Println("XPriv: ", keys.String())
fmt.Println("XPub: ", keys.XPub().String())
fmt.Println("Mnemonic: ", keys.Mnemonic())

//Generate keys from mnemonic string
xpriv3, err := xpriv.FromMnemonic(keys.Mnemonic())
if err != nil {
panic(err)
}

fmt.Println("<-- FromMnemonic method")
fmt.Println("XPriv: ", xpriv3.String())
fmt.Println("XPub: ", xpriv3.XPub().String())
fmt.Println("Mnemonic: ", xpriv3.Mnemonic())

//Generate keys from string
xpriv2, err := xpriv.FromString(keys.String())
if err != nil {
panic(err)
}

fmt.Println("<-- FromString method")
fmt.Println("XPriv: ", xpriv2.String())
fmt.Println("XPub: ", xpriv2.XPub().String())
fmt.Println("Can not get mnemonic from keys generated from string")
}
8 changes: 4 additions & 4 deletions examples/new_paymail/new_paymail.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

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

"github.com/BuxOrg/go-buxclient"
Expand All @@ -11,14 +11,14 @@ import (
func main() {

// Generate keys
keys, resErr := xkeys.Generate()
keys, resErr := xpriv.Generate()
if resErr != nil {
log.Fatalln(resErr.Error())
}

// Create a client
buxClient, err := buxclient.New(
buxclient.WithXPriv(keys.Xpriv.String()),
buxclient.WithXPriv(keys.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(), keys.Xpub.String(), "[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
10 changes: 5 additions & 5 deletions examples/register_xpub/register_xpub.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

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

buxmodels "github.com/BuxOrg/bux-models"
Expand All @@ -11,14 +11,14 @@ import (

func main() {
// Generate keys
keys, resErr := xkeys.Generate()
keys, resErr := xpriv.Generate()
if resErr != nil {
log.Fatalln(resErr.Error())
}

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

if err = buxClient.NewXpub(
context.Background(), keys.Xpub.String(), &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: " + keys.Xpub.String())
log.Println("registered xPub: " + keys.XPub().String())
}
58 changes: 0 additions & 58 deletions xkeys/xkeys.go

This file was deleted.

139 changes: 139 additions & 0 deletions xpriv/xpriv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package xpriv

import (
"fmt"

"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 string
xpub PublicKey
mnemonic string
}

// PublicKey is a struct containing public key information
type PublicKey struct {
Xpub string
}

// Key represents basic key methods
type Key interface {
String() string
XPub() PubKey
}

// PubKey represents public key methods
type PubKey interface {
String() string
}

// KeyWithMnemonic represents methods for generated keys
type KeyWithMnemonic interface {
Key
Mnemonic() string
}

// XPub return hierarchical struct which contain xpub info
func (k *Keys) XPub() PubKey {
return k.xpub
}

// String return hierarchical deterministic private key
func (k *Keys) String() string {
return k.xpriv
}

// Mnemonic return mnemonic from which keys where generated
func (k *Keys) Mnemonic() string {
return k.mnemonic
}

// String return hierarchical deterministic publick ey
func (k PublicKey) String() string {
return k.Xpub
}

// Generate generates a random set of keys - xpriv, xpb and mnemonic
func Generate() (KeyWithMnemonic, error) {
entropy, err := bip39.GenerateEntropy(160)
if err != nil {
return nil, fmt.Errorf("generate method: key generation error when creating entropy: %w", err)
}

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

if err != nil {
return nil, fmt.Errorf("generate method: key generation error when creating mnemonic: %w", err)
}

hdXpriv, hdXpub, err := createXPrivAndXPub(seed)
if err != nil {
return nil, err
}

keys := &Keys{
xpriv: hdXpriv.String(),
xpub: PublicKey{hdXpub.String()},
mnemonic: mnemonic,
}

return keys, nil
}

// FromMnemonic generates Keys based on given mnemonic
func FromMnemonic(mnemonic string) (KeyWithMnemonic, error) {
seed, err := bip39.MnemonicToSeed(mnemonic, "")
if err != nil {
return nil, fmt.Errorf("FromMnemonic method: error when creating seed: %w", err)
}

hdXpriv, hdXpub, err := createXPrivAndXPub(seed)
if err != nil {
return nil, fmt.Errorf("FromMnemonic method: %w", err)
}

keys := &Keys{
xpriv: hdXpriv.String(),
xpub: PublicKey{hdXpub.String()},
mnemonic: mnemonic,
}

return keys, nil
}

// FromString generates keys from given xpriv
func FromString(xpriv string) (Key, error) {
hdXpriv, err := bip32.NewKeyFromString(xpriv)
if err != nil {
return nil, fmt.Errorf("FromString method: key generation error when creating hd private key: %w", err)
}

hdXpub, err := hdXpriv.Neuter()
if err != nil {
return nil, fmt.Errorf("FromString method: key generation error when creating hd public hey: %w", err)
}

keys := &Keys{
xpriv: hdXpriv.String(),
xpub: PublicKey{hdXpub.String()},
}

return keys, nil
}

func createXPrivAndXPub(seed []byte) (*bip32.ExtendedKey, *bip32.ExtendedKey, error) {
hdXpriv, err := bip32.NewMaster(seed, &chaincfg.MainNet)
if err != nil {
return nil, nil, fmt.Errorf("key generation error when creating hd private key: %w", err)
}

hdXpub, err := hdXpriv.Neuter()
if err != nil {
return nil, nil, fmt.Errorf("key generation error when creating hd public hey: %w", err)
}
return hdXpriv, hdXpub, nil
}

0 comments on commit 11b82d8

Please sign in to comment.