Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(SPV-879): TOTP example & all examples adjustments #241

Merged
merged 2 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ In addition to the above, there are additional examples showing how to use the c

1. `xpriv_from_mnemonic` - allows you to generate/extract an xPriv key from a mnemonic phrase. To you use it you just need to replace the `mnemonic` variable with your own mnemonic phrase.
2. `xpub_from_xpriv` - allows you to generate an xPub key from an xPriv key. To you use it you just need to replace the `xPriv` variable with your own xPriv key.
3. `generate_totp` - allows you to generate and check validity of a TOTP code for client xPriv and a contact's PKI

## How to run an example

Expand Down
5 changes: 5 additions & 0 deletions examples/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,8 @@ tasks:
cmds:
- echo "running xpub_from_xpriv..."
- go run ./xpub_from_xpriv/xpub_from_xpriv.go
generate_totp:
desc: "running generate_totp..."
cmds:
- echo "running generate_totp..."
- go run ./generate_totp/generate_totp.go
2 changes: 1 addition & 1 deletion examples/admin_add_user/admin_add_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"fmt"
"os"

"examples"
walletclient "github.com/bitcoin-sv/spv-wallet-go-client"
"github.com/bitcoin-sv/spv-wallet-go-client/examples"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/admin_remove_user/admin_remove_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"fmt"
"os"

"examples"
walletclient "github.com/bitcoin-sv/spv-wallet-go-client"
"github.com/bitcoin-sv/spv-wallet-go-client/examples"
)

func main() {
Expand Down
3 changes: 1 addition & 2 deletions examples/create_transaction/create_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"fmt"
"os"

"examples"
walletclient "github.com/bitcoin-sv/spv-wallet-go-client"
"github.com/bitcoin-sv/spv-wallet-go-client/examples"
)

func main() {
Expand Down Expand Up @@ -39,5 +39,4 @@ func main() {
os.Exit(1)
}
fmt.Println("GetTransaction response: ", tx)

}
20 changes: 1 addition & 19 deletions examples/example_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,11 @@ Package examples - key constants to be used in the examples and utility function
*/
package examples

import (
"fmt"
"os"

"github.com/bitcoin-sv/spv-wallet-go-client/xpriv"
)

const (
// ExampleAdminKey - example admin key
ExampleAdminKey string = "xprv9s21ZrQH143K3CbJXirfrtpLvhT3Vgusdo8coBritQ3rcS7Jy7sxWhatuxG5h2y1Cqj8FKmPp69536gmjYRpfga2MJdsGyBsnB12E19CESK"

// you can generate new keys using `task generate-keys`
// you can generate new keys using `task generate_keys`
chris-4chain marked this conversation as resolved.
Show resolved Hide resolved

// ExampleXPriv - example private key
ExampleXPriv string = ""
Expand All @@ -24,14 +17,3 @@ const (
// ExamplePaymail - example Paymail address
ExamplePaymail string = ""
)

// GenerateKeys - function for generating keys (private and public)
func GenerateKeys() xpriv.KeyWithMnemonic {
keys, err := xpriv.Generate()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

return keys
}
10 changes: 8 additions & 2 deletions examples/generate_keys/generate_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ package main

import (
"fmt"
"os"

"examples"
"github.com/bitcoin-sv/spv-wallet-go-client/xpriv"
)

func main() {
keys := examples.GenerateKeys()
keys, err := xpriv.Generate()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

exampleXPriv := keys.XPriv()
exampleXPub := keys.XPub().String()

Expand Down
44 changes: 44 additions & 0 deletions examples/generate_totp/generate_totp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Package main - generate_totp example
*/
package main

import (
"fmt"
"os"

walletclient "github.com/bitcoin-sv/spv-wallet-go-client"
"github.com/bitcoin-sv/spv-wallet-go-client/examples"
"github.com/bitcoin-sv/spv-wallet/models"
)

func main() {
defer examples.HandlePanic()

const server = "http://localhost:3003/v1"
const aliceXPriv = "xprv9s21ZrQH143K4JFXqGhBzdrthyNFNuHPaMUwvuo8xvpHwWXprNK7T4JPj1w53S1gojQncyj8JhSh8qouYPZpbocsq934cH5G1t1DRBfgbod"
const bobPKI = "03a48e13dc598dce5fda9b14ea13f32d5dbc4e8d8a34447dda84f9f4c457d57fe7"
const digits = 4
const period = 1200 // 20 minutes

client := walletclient.NewWithXPriv(server, aliceXPriv)

mockContact := &models.Contact{
PubKey: bobPKI,
Paymail: "[email protected]",
}

totpCode, err := client.GenerateTotpForContact(mockContact, period, digits)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("TOTP code from Alice to Bob: ", totpCode)

valid, err := client.ValidateTotpForContact(mockContact, totpCode, mockContact.Paymail, period, digits)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Is TOTP code valid: ", valid)
}
2 changes: 1 addition & 1 deletion examples/get_balance/get_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"fmt"
"os"

"examples"
walletclient "github.com/bitcoin-sv/spv-wallet-go-client"
"github.com/bitcoin-sv/spv-wallet-go-client/examples"
)

func main() {
Expand Down
6 changes: 2 additions & 4 deletions examples/go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions examples/handle_exceptions/handle_exceptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"fmt"
"os"

"examples"
walletclient "github.com/bitcoin-sv/spv-wallet-go-client"
"github.com/bitcoin-sv/spv-wallet-go-client/examples"
)

func main() {
Expand All @@ -23,7 +23,6 @@ func main() {
ctx := context.Background()

status, err := client.AdminGetStatus(ctx)

if err != nil {
fmt.Println("Response status: ", err.GetStatusCode())
fmt.Println("Content: ", err.Error())
Expand Down
3 changes: 1 addition & 2 deletions examples/list_transactions/list_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"fmt"
"os"

"examples"
walletclient "github.com/bitcoin-sv/spv-wallet-go-client"
"github.com/bitcoin-sv/spv-wallet-go-client/examples"
"github.com/bitcoin-sv/spv-wallet/models/filter"
)

Expand Down Expand Up @@ -44,5 +44,4 @@ func main() {
os.Exit(1)
}
fmt.Println("Filtered GetTransactions response: ", txsFiltered)

}
2 changes: 1 addition & 1 deletion examples/send_op_return/send_op_return.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"fmt"
"os"

"examples"
walletclient "github.com/bitcoin-sv/spv-wallet-go-client"
"github.com/bitcoin-sv/spv-wallet-go-client/examples"
"github.com/bitcoin-sv/spv-wallet/models"
)

Expand Down
14 changes: 5 additions & 9 deletions examples/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@ import (
"os"
)

// 'xPriv' | 'xPub' | 'adminKey' | 'Paymail'
type keyType string

// ErrMessage - function for displaying errors about missing keys (see CheckIfAdminKeyExists, CheckIfXPrivExists)
func ErrMessage(key keyType) string {
return fmt.Sprintf("Please provide a valid %s.", key)
func printMissingKeyError(key string) {
fmt.Printf("Please provide a valid %s. ", key)
}

// HandlePanic - function used to handle a recovery after a panic - use with defer
Expand All @@ -28,23 +24,23 @@ func HandlePanic() {
// CheckIfXPrivExists - checks if ExampleXPriv is not empty
func CheckIfXPrivExists() {
if ExampleXPriv == "" {
fmt.Println(ErrMessage("xPriv"))
printMissingKeyError("xPriv")
os.Exit(1)
}
}

// CheckIfXPubExists - checks if ExampleXPub is not empty
func CheckIfXPubExists() {
if ExampleXPub == "" {
fmt.Println(ErrMessage("xPub"))
printMissingKeyError("xPub")
os.Exit(1)
}
}

// CheckIfAdminKeyExists - checks if ExampleAdminKey is not empty
func CheckIfAdminKeyExists() {
if ExampleAdminKey == "" {
fmt.Println(ErrMessage("adminKey"))
printMissingKeyError("adminKey")
os.Exit(1)
}
}
Loading