diff --git a/examples/Makefile b/examples/Makefile deleted file mode 100644 index 8b6c00f..0000000 --- a/examples/Makefile +++ /dev/null @@ -1,54 +0,0 @@ -.PHONY: admin_add_user -admin_add_user: - @echo "running admin_add_user..." - @go run ./admin_add_user/admin_add_user.go - -.PHONY: admin_remove_user -admin_remove_user: - @echo "running admin_remove_user..." - @go run ./admin_remove_user/admin_remove_user.go - -.PHONY: create_transaction -create_transaction: - @echo "running create_transaction..." - @go run ./create_transaction/create_transaction.go - -# .PHONY: custom_logger -# custom_logger: -# @echo "running custom_logger..." -# @go run ./custom_logger/custom_logger.go - -.PHONY: generate_keys -generate_keys: - @echo "running generate_keys..." - @go run ./generate_keys/generate_keys.go - -.PHONY: get_balance -get_balance: - @echo "running get_balance..." - @go run ./get_balance/get_balance.go - -.PHONY: handle_exceptions -handle_exceptions: - @echo "running handle_exceptions..." - @go run ./handle_exceptions/handle_exceptions.go - -.PHONY: list_transactions -list_transactions: - @echo "running list_transactions..." - @go run ./list_transactions/list_transactions.go - -.PHONY: send_op_return -send_op_return: - @echo "running send_op_return..." - @go run ./send_op_return/send_op_return.go - -.PHONY: xpriv_from_mnemonic -xpriv_from_mnemonic: - @echo "running xpriv_from_mnemonic..." - @go run ./xpriv_from_mnemonic/xpriv_from_mnemonic.go - -.PHONY: xpub_from_xpriv -xpub_from_xpriv: - @echo "running xpub_from_xpriv..." - @go run ./xpub_from_xpriv/xpub_from_xpriv.go diff --git a/examples/README.md b/examples/README.md index 060e0c5..3c30a17 100644 --- a/examples/README.md +++ b/examples/README.md @@ -11,22 +11,22 @@ In this directory you can find examples of how to use the `spv-wallet-go-client` ### Concerning the keys -- The `adminKey` defined in `example_keys.go` is the default one from [spv-wallet-web-backend repository](https://github.com/bitcoin-sv/spv-wallet-web-backend/blob/main/config/viper.go#L56) +- The `ExampleAdminKey` defined in `example_keys.go` is the default one from [spv-wallet-web-backend repository](https://github.com/bitcoin-sv/spv-wallet-web-backend/blob/main/config/viper.go#L56) - If in your current `spv-wallet` instance you have a different `adminKey`, you should replace the one in `example_keys` with the one you have. -- The `exampleXPub` and `exampleXPriv` are just placeholders, which won't work. - - You should replace them by newly generated ones using `make generate_keys`, +- The `ExampleXPub` and `ExampleXPriv` are just placeholders, which won't work. + - You should replace them by newly generated ones using `task generate_keys`, - ... or use your actual keys if you have them (don't use the keys which are already added to another wallet). -> Additionally, to make it work properly, you should adjust the `examplePaymail` to align with your `domains` configuration in the `spv-wallet` instance. +> Additionally, to make it work properly, you should adjust the `ExamplePaymail` to align with your `domains` configuration in the `spv-wallet` instance. ## Proposed order of executing examples 1. `generate_keys` - generates new keys (you can copy them to `example_keys` if you want to use them in next examples) -2. `admin_add_user` - adds a new user (more precisely adds `exampleXPub` and then `examplePaymail` to the wallet) +2. `admin_add_user` - adds a new user (more precisely adds `ExampleXPub` and then `ExamplePaymail` to the wallet) -> To fully experience the next steps, it would be beneficial to transfer some funds to your `examplePaymail`. This ensures the examples run smoothly by demonstrating the creation of a transaction with an actual balance. You can transfer funds to your `examplePaymail` using a Bitcoin SV wallet application such as HandCash or any other that supports Paymail. +> To fully experience the next steps, it would be beneficial to transfer some funds to your `ExamplePaymail`. This ensures the examples run smoothly by demonstrating the creation of a transaction with an actual balance. You can transfer funds to your `ExamplePaymail` using a Bitcoin SV wallet application such as HandCash or any other that supports Paymail. -3. `get_balance` - checks the balance - if you've transferred funds to your `examplePaymail`, you should see them here +3. `get_balance` - checks the balance - if you've transferred funds to your `ExamplePaymail`, you should see them here 4. `create_transaction` - creates a transaction (you can adjust the `outputs` to your needs) 5. `list_transactions` - lists all transactions and with example filtering 6. `send_op_return` - sends an OP_RETURN transaction @@ -48,7 +48,7 @@ The examples are written in Go and can be run by: ```bash cd examples -make name_of_the_example +task name_of_the_example ``` -> See the `examples/Makefile` for the list of available examples and scripts +> See the `examples/Taskfile.yml` for the list of available examples and scripts diff --git a/examples/Taskfile.yml b/examples/Taskfile.yml new file mode 100644 index 0000000..651d41e --- /dev/null +++ b/examples/Taskfile.yml @@ -0,0 +1,62 @@ +version: '3' + +tasks: + admin_add_user: + desc: "running admin_add_user..." + cmds: + - echo "running admin_add_user..." + - go run ./admin_add_user/admin_add_user.go + + admin_remove_user: + desc: "running admin_remove_user..." + cmds: + - echo "running admin_remove_user..." + - go run ./admin_remove_user/admin_remove_user.go + + create_transaction: + desc: "running create_transaction..." + cmds: + - echo "running create_transaction..." + - go run ./create_transaction/create_transaction.go + + generate_keys: + desc: "running generate_keys..." + cmds: + - echo "running generate_keys..." + - go run ./generate_keys/generate_keys.go + + get_balance: + desc: "running get_balance..." + cmds: + - echo "running get_balance..." + - go run ./get_balance/get_balance.go + + handle_exceptions: + desc: "running handle_exceptions..." + cmds: + - echo "running handle_exceptions..." + - go run ./handle_exceptions/handle_exceptions.go + + list_transactions: + desc: "running list_transactions..." + cmds: + - echo "running list_transactions..." + - go run ./list_transactions/list_transactions.go + + send_op_return: + desc: "running send_op_return..." + cmds: + - echo "running send_op_return..." + - go run ./send_op_return/send_op_return.go + + xpriv_from_mnemonic: + desc: "running xpriv_from_mnemonic..." + cmds: + - echo "running xpriv_from_mnemonic..." + - go run ./xpriv_from_mnemonic/xpriv_from_mnemonic.go + + xpub_from_xpriv: + desc: "running xpub_from_xpriv..." + cmds: + - echo "running xpub_from_xpriv..." + - go run ./xpub_from_xpriv/xpub_from_xpriv.go \ No newline at end of file diff --git a/examples/admin_add_user/admin_add_user.go b/examples/admin_add_user/admin_add_user.go index bc35157..322dab8 100644 --- a/examples/admin_add_user/admin_add_user.go +++ b/examples/admin_add_user/admin_add_user.go @@ -1,23 +1,23 @@ +/* +Package main - admin_add_user example +*/ package main import ( "context" - "examples" "fmt" "os" + "examples" walletclient "github.com/bitcoin-sv/spv-wallet-go-client" ) func main() { defer examples.HandlePanic() - server := "http://localhost:3003/v1" + examples.CheckIfAdminKeyExists() - if examples.ExampleAdminKey == "" { - fmt.Println(examples.ErrMessage("adminKey")) - os.Exit(1) - } + server := "http://localhost:3003/v1" adminClient := walletclient.NewWithAdminKey(server, examples.ExampleAdminKey) ctx := context.Background() diff --git a/examples/admin_remove_user/admin_remove_user.go b/examples/admin_remove_user/admin_remove_user.go index 9f79ce1..7498211 100644 --- a/examples/admin_remove_user/admin_remove_user.go +++ b/examples/admin_remove_user/admin_remove_user.go @@ -1,26 +1,30 @@ +/* +Package main - admin_remove_user example +*/ package main import ( "context" - "examples" "fmt" "os" + "examples" walletclient "github.com/bitcoin-sv/spv-wallet-go-client" ) func main() { defer examples.HandlePanic() - const server = "http://localhost:3003/v1" + examples.CheckIfAdminKeyExists() - if examples.ExampleAdminKey == "" { - fmt.Println(examples.ErrMessage("adminKey")) - os.Exit(1) - } + const server = "http://localhost:3003/v1" adminClient := walletclient.NewWithAdminKey(server, examples.ExampleAdminKey) ctx := context.Background() - adminClient.AdminDeletePaymail(ctx, examples.ExamplePaymail) + err := adminClient.AdminDeletePaymail(ctx, examples.ExamplePaymail) + if err != nil { + fmt.Println(err) + os.Exit(1) + } } diff --git a/examples/create_transaction/create_transaction.go b/examples/create_transaction/create_transaction.go index ab2a9e5..4e295a0 100644 --- a/examples/create_transaction/create_transaction.go +++ b/examples/create_transaction/create_transaction.go @@ -1,23 +1,23 @@ +/* +Package main - create_transaction example +*/ package main import ( "context" - "examples" "fmt" "os" + "examples" walletclient "github.com/bitcoin-sv/spv-wallet-go-client" ) func main() { defer examples.HandlePanic() - const server = "http://localhost:3003/v1" + examples.CheckIfXPrivExists() - if examples.ExampleXPriv == "" { - fmt.Println(examples.ErrMessage("xPriv")) - os.Exit(1) - } + const server = "http://localhost:3003/v1" client := walletclient.NewWithXPriv(server, examples.ExampleXPriv) ctx := context.Background() diff --git a/examples/example_keys.go b/examples/example_keys.go index 7225e50..1914d13 100644 --- a/examples/example_keys.go +++ b/examples/example_keys.go @@ -1,11 +1,39 @@ +/* +Package examples - key constants to be used in the examples and utility function for generating keys +*/ 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 `yarn generate-keys` + // you can generate new keys using `task generate-keys` + + // ExampleXPriv - example private key ExampleXPriv string = "" - ExampleXPub string = "" + // ExampleXPub - example public key + ExampleXPub string = "" + // 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 5bc18a9..f96fc8d 100644 --- a/examples/generate_keys/generate_keys.go +++ b/examples/generate_keys/generate_keys.go @@ -1,8 +1,12 @@ +/* +Package main - generate_keys example +*/ package main import ( - "examples" "fmt" + + "examples" ) func main() { diff --git a/examples/get_balance/get_balance.go b/examples/get_balance/get_balance.go index 0af899e..8295b61 100644 --- a/examples/get_balance/get_balance.go +++ b/examples/get_balance/get_balance.go @@ -1,3 +1,6 @@ +/* +Package main - get_balance example +*/ package main import ( @@ -5,20 +8,16 @@ import ( "fmt" "os" - walletclient "github.com/bitcoin-sv/spv-wallet-go-client" - "examples" + walletclient "github.com/bitcoin-sv/spv-wallet-go-client" ) func main() { defer examples.HandlePanic() - const server = "http://localhost:3003/v1" + examples.CheckIfXPrivExists() - if examples.ExampleXPriv == "" { - fmt.Println(examples.ErrMessage("xPriv")) - os.Exit(1) - } + const server = "http://localhost:3003/v1" client := walletclient.NewWithXPriv(server, examples.ExampleXPriv) ctx := context.Background() diff --git a/examples/go.mod b/examples/go.mod index eeca411..55eee5b 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -8,7 +8,7 @@ replace github.com/bitcoin-sv/spv-wallet-go-client => ../ require ( github.com/bitcoin-sv/spv-wallet-go-client v0.0.0-00010101000000-000000000000 - github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.9 + github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.12 ) require ( diff --git a/examples/go.sum b/examples/go.sum index 86acbc5..5e7a550 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -1,5 +1,5 @@ -github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.9 h1:lPN+r10zuQ19tNMczYT1H+FcUCZMh3LAhAqngrqM1i0= -github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.9/go.mod h1:i3txysriHpprqYd3u97wEQsC4/jn+KHcyFOmuFYMw8M= +github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.12 h1:FtKt6mBWOrWYQ7bACkCFycTVIid/Sjn/97n+gmN/H7U= +github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.12/go.mod h1:i3txysriHpprqYd3u97wEQsC4/jn+KHcyFOmuFYMw8M= github.com/bitcoinschema/go-bitcoin/v2 v2.0.5 h1:Sgh5Eb746Zck/46rFDrZZEXZWyO53fMuWYhNoZa1tck= github.com/bitcoinschema/go-bitcoin/v2 v2.0.5/go.mod h1:JjO1ivfZv6vhK0uAXzyH08AAHlzNMAfnyK1Fiv9r4ZA= github.com/bitcoinsv/bsvd v0.0.0-20190609155523-4c29707f7173 h1:2yTIV9u7H0BhRDGXH5xrAwAz7XibWJtX2dNezMeNsUo= diff --git a/examples/handle_exceptions/handle_exceptions.go b/examples/handle_exceptions/handle_exceptions.go index 1050251..8a2ac45 100644 --- a/examples/handle_exceptions/handle_exceptions.go +++ b/examples/handle_exceptions/handle_exceptions.go @@ -1,3 +1,6 @@ +/* +Package main - handle_exceptions example +*/ package main import ( @@ -5,20 +8,16 @@ import ( "fmt" "os" - walletclient "github.com/bitcoin-sv/spv-wallet-go-client" - "examples" + walletclient "github.com/bitcoin-sv/spv-wallet-go-client" ) func main() { defer examples.HandlePanic() - const server = "http://localhost:3003/v1" + examples.CheckIfXPubExists() - if examples.ExampleXPub == "" { - fmt.Println(examples.ErrMessage("xPub")) - os.Exit(1) - } + const server = "http://localhost:3003/v1" client := walletclient.NewWithXPub(server, examples.ExampleXPub) ctx := context.Background() diff --git a/examples/list_transactions/list_transactions.go b/examples/list_transactions/list_transactions.go index 2076ecd..7bfa99f 100644 --- a/examples/list_transactions/list_transactions.go +++ b/examples/list_transactions/list_transactions.go @@ -1,3 +1,6 @@ +/* +Package main - list_transactions example +*/ package main import ( @@ -5,21 +8,17 @@ import ( "fmt" "os" + "examples" walletclient "github.com/bitcoin-sv/spv-wallet-go-client" "github.com/bitcoin-sv/spv-wallet/models/filter" - - "examples" ) func main() { defer examples.HandlePanic() - const server = "http://localhost:3003/v1" + examples.CheckIfXPrivExists() - if examples.ExampleXPriv == "" { - fmt.Println(examples.ErrMessage("xPriv")) - os.Exit(1) - } + const server = "http://localhost:3003/v1" client := walletclient.NewWithXPriv(server, examples.ExampleXPriv) ctx := context.Background() diff --git a/examples/send_op_return/send_op_return.go b/examples/send_op_return/send_op_return.go index c23f423..8c66d8a 100644 --- a/examples/send_op_return/send_op_return.go +++ b/examples/send_op_return/send_op_return.go @@ -1,11 +1,14 @@ +/* +Package main - send_op_return example +*/ package main import ( "context" - "examples" "fmt" "os" + "examples" walletclient "github.com/bitcoin-sv/spv-wallet-go-client" "github.com/bitcoin-sv/spv-wallet/models" ) @@ -13,12 +16,9 @@ import ( func main() { defer examples.HandlePanic() - const server = "http://localhost:3003/v1" + examples.CheckIfXPrivExists() - if examples.ExampleXPriv == "" { - fmt.Println(examples.ErrMessage("xPriv")) - os.Exit(1) - } + const server = "http://localhost:3003/v1" client := walletclient.NewWithXPriv(server, examples.ExampleXPriv) ctx := context.Background() diff --git a/examples/utils.go b/examples/utils.go index bc4ddbe..9674def 100644 --- a/examples/utils.go +++ b/examples/utils.go @@ -1,29 +1,26 @@ +/* +Package examples - Utility functions for this package +*/ package examples import ( "fmt" "os" - - "github.com/bitcoin-sv/spv-wallet-go-client/xpriv" ) // '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 GenerateKeys() xpriv.KeyWithMnemonic { - keys, err := xpriv.Generate() - if err != nil { - fmt.Println(err) - os.Exit(1) - } - - return keys -} - +/* +HandlePanic - function used to handle a recovery after a panic - use with defer +*/ func HandlePanic() { r := recover() @@ -31,3 +28,33 @@ func HandlePanic() { fmt.Println("Recovering: ", r) } } + +/* +CheckIfXPrivExists - checks if ExampleXPriv is not empty +*/ +func CheckIfXPrivExists() { + if ExampleXPriv == "" { + fmt.Println(ErrMessage("xPriv")) + os.Exit(1) + } +} + +/* +CheckIfXPubExists - checks if ExampleXPub is not empty +*/ +func CheckIfXPubExists() { + if ExampleXPub == "" { + fmt.Println(ErrMessage("xPub")) + os.Exit(1) + } +} + +/* +CheckIfAdminKeyExists - checks if ExampleAdminKey is not empty +*/ +func CheckIfAdminKeyExists() { + if ExampleAdminKey == "" { + fmt.Println(ErrMessage("adminKey")) + os.Exit(1) + } +} diff --git a/examples/xpriv_from_mnemonic/xpriv_from_mnemonic.go b/examples/xpriv_from_mnemonic/xpriv_from_mnemonic.go index 8b940b2..8e35cbe 100644 --- a/examples/xpriv_from_mnemonic/xpriv_from_mnemonic.go +++ b/examples/xpriv_from_mnemonic/xpriv_from_mnemonic.go @@ -1,3 +1,6 @@ +/* +Package main - xpriv_from_mnemonic example +*/ package main import ( diff --git a/examples/xpub_from_xpriv/xpub_from_xpriv.go b/examples/xpub_from_xpriv/xpub_from_xpriv.go index 9007484..66fc8cd 100644 --- a/examples/xpub_from_xpriv/xpub_from_xpriv.go +++ b/examples/xpub_from_xpriv/xpub_from_xpriv.go @@ -1,3 +1,6 @@ +/* +Package main - xpub_from_xpriv example +*/ package main import (