diff --git a/examples/graphql/graphql.go b/examples/graphql/graphql.go index a641972..06882e0 100644 --- a/examples/graphql/graphql.go +++ b/examples/graphql/graphql.go @@ -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), diff --git a/examples/http/http.go b/examples/http/http.go index 1f38302..5fc40de 100644 --- a/examples/http/http.go +++ b/examples/http/http.go @@ -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), diff --git a/examples/keys/keys.go b/examples/keys/keys.go deleted file mode 100644 index c52ba9f..0000000 --- a/examples/keys/keys.go +++ /dev/null @@ -1,26 +0,0 @@ -package main - -import ( - "fmt" - "github.com/BuxOrg/go-buxclient/utils" -) - -func main() { - // Generate a random set of keys - hdXpriv, hdXpub := utils.GenerateRandomSetOfKeys() - - fmt.Println("Your XPriv: ", hdXpriv) - fmt.Println("Your XPub: ", hdXpub) - - xpub, err := utils.GetPublicKeyFromHDPrivateKey(hdXpriv.String()) - 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") - } -} diff --git a/examples/new_paymail/new_paymail.go b/examples/new_paymail/new_paymail.go index 1415d60..67c04a3 100644 --- a/examples/new_paymail/new_paymail.go +++ b/examples/new_paymail/new_paymail.go @@ -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), @@ -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()) diff --git a/examples/register_xpub/register_xpub.go b/examples/register_xpub/register_xpub.go index 5a1c60d..24e3393 100644 --- a/examples/register_xpub/register_xpub.go +++ b/examples/register_xpub/register_xpub.go @@ -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), @@ -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()) } diff --git a/utils/utils.go b/utils/utils.go index ddd323a..e322ec7 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -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" @@ -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 -} diff --git a/xkeys/xkeys.go b/xkeys/xkeys.go new file mode 100644 index 0000000..44019d8 --- /dev/null +++ b/xkeys/xkeys.go @@ -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 +}