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.
- Loading branch information
1 parent
c09804e
commit 11b82d8
Showing
7 changed files
with
195 additions
and
73 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 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,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") | ||
} |
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,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" | ||
|
@@ -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), | ||
|
@@ -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()) | ||
|
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 |
---|---|---|
@@ -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 | ||
} |