generated from mrz1836/go-template
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(BUX-190): move new methods into separate package
- Loading branch information
1 parent
8f8ca5f
commit c09804e
Showing
7 changed files
with
89 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, "[email protected]", "", "Foo", nil) | ||
err = buxClient.NewPaymail(context.Background(), keys.Xpub.String(), "[email protected]", "", "Foo", nil) | ||
|
||
if err != nil { | ||
log.Fatalln(err.Error()) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |