diff --git a/examples/README.md b/examples/README.md index 3c30a17..f08e3a5 100644 --- a/examples/README.md +++ b/examples/README.md @@ -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 diff --git a/examples/Taskfile.yml b/examples/Taskfile.yml index a07d90d..b67c760 100644 --- a/examples/Taskfile.yml +++ b/examples/Taskfile.yml @@ -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 diff --git a/examples/admin_add_user/admin_add_user.go b/examples/admin_add_user/admin_add_user.go index 322dab8..4476f51 100644 --- a/examples/admin_add_user/admin_add_user.go +++ b/examples/admin_add_user/admin_add_user.go @@ -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() { diff --git a/examples/admin_remove_user/admin_remove_user.go b/examples/admin_remove_user/admin_remove_user.go index 7498211..02c1475 100644 --- a/examples/admin_remove_user/admin_remove_user.go +++ b/examples/admin_remove_user/admin_remove_user.go @@ -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() { diff --git a/examples/create_transaction/create_transaction.go b/examples/create_transaction/create_transaction.go index 4e295a0..25be774 100644 --- a/examples/create_transaction/create_transaction.go +++ b/examples/create_transaction/create_transaction.go @@ -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() { @@ -39,5 +39,4 @@ func main() { os.Exit(1) } fmt.Println("GetTransaction response: ", tx) - } diff --git a/examples/example_keys.go b/examples/example_keys.go index 68cc2e8..8c52747 100644 --- a/examples/example_keys.go +++ b/examples/example_keys.go @@ -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` // ExampleXPriv - example private key ExampleXPriv string = "" @@ -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 -} diff --git a/examples/generate_keys/generate_keys.go b/examples/generate_keys/generate_keys.go index f96fc8d..93af46c 100644 --- a/examples/generate_keys/generate_keys.go +++ b/examples/generate_keys/generate_keys.go @@ -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() diff --git a/examples/generate_totp/generate_totp.go b/examples/generate_totp/generate_totp.go new file mode 100644 index 0000000..48f747e --- /dev/null +++ b/examples/generate_totp/generate_totp.go @@ -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: "test@paymail.com", + } + + 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) +} diff --git a/examples/get_balance/get_balance.go b/examples/get_balance/get_balance.go index 8295b61..9908492 100644 --- a/examples/get_balance/get_balance.go +++ b/examples/get_balance/get_balance.go @@ -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() { diff --git a/examples/go.mod b/examples/go.mod index 55eee5b..6a844ad 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -1,8 +1,6 @@ -module examples +module github.com/bitcoin-sv/spv-wallet-go-client/examples -go 1.22.3 - -replace examples => ./ +go 1.21 replace github.com/bitcoin-sv/spv-wallet-go-client => ../ diff --git a/examples/handle_exceptions/handle_exceptions.go b/examples/handle_exceptions/handle_exceptions.go index 8a2ac45..62437a8 100644 --- a/examples/handle_exceptions/handle_exceptions.go +++ b/examples/handle_exceptions/handle_exceptions.go @@ -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() { @@ -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()) diff --git a/examples/list_transactions/list_transactions.go b/examples/list_transactions/list_transactions.go index 7bfa99f..d1d0c6f 100644 --- a/examples/list_transactions/list_transactions.go +++ b/examples/list_transactions/list_transactions.go @@ -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" ) @@ -44,5 +44,4 @@ func main() { os.Exit(1) } fmt.Println("Filtered GetTransactions response: ", txsFiltered) - } diff --git a/examples/send_op_return/send_op_return.go b/examples/send_op_return/send_op_return.go index 8c66d8a..93ae290 100644 --- a/examples/send_op_return/send_op_return.go +++ b/examples/send_op_return/send_op_return.go @@ -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" ) diff --git a/examples/utils.go b/examples/utils.go index ef6f6c1..0fb323e 100644 --- a/examples/utils.go +++ b/examples/utils.go @@ -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 @@ -28,7 +24,7 @@ func HandlePanic() { // CheckIfXPrivExists - checks if ExampleXPriv is not empty func CheckIfXPrivExists() { if ExampleXPriv == "" { - fmt.Println(ErrMessage("xPriv")) + printMissingKeyError("xPriv") os.Exit(1) } } @@ -36,7 +32,7 @@ func CheckIfXPrivExists() { // CheckIfXPubExists - checks if ExampleXPub is not empty func CheckIfXPubExists() { if ExampleXPub == "" { - fmt.Println(ErrMessage("xPub")) + printMissingKeyError("xPub") os.Exit(1) } } @@ -44,7 +40,7 @@ func CheckIfXPubExists() { // CheckIfAdminKeyExists - checks if ExampleAdminKey is not empty func CheckIfAdminKeyExists() { if ExampleAdminKey == "" { - fmt.Println(ErrMessage("adminKey")) + printMissingKeyError("adminKey") os.Exit(1) } }