diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..3c30a17 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,54 @@ +# Quick Guide how to run examples + +In this directory you can find examples of how to use the `spv-wallet-go-client` package. + +## Before you run + +### Pre-requisites + +- You have access to the `spv-wallet` non-custodial wallet (running locally or remotely). +- You have installed this package on your machine (`go install` on this project's root directory). + +### Concerning the keys + +- 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 `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. + +## 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) + +> 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 +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 +7. `admin_remove_user` - removes the user + +In addition to the above, there are additional examples showing how to use the client from a developer perspective: + +- `handle_exceptions` - presents how to "catch" exceptions which the client can throw +- `custom_logger` - shows different ways you can configure (or disable) internal logger + +## Util examples + +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. + +## How to run an example + +The examples are written in Go and can be run by: + +```bash +cd examples +task name_of_the_example +``` + +> 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..a07d90d --- /dev/null +++ b/examples/Taskfile.yml @@ -0,0 +1,63 @@ +--- +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 diff --git a/examples/admin_add_user/admin_add_user.go b/examples/admin_add_user/admin_add_user.go new file mode 100644 index 0000000..322dab8 --- /dev/null +++ b/examples/admin_add_user/admin_add_user.go @@ -0,0 +1,36 @@ +/* +Package main - admin_add_user example +*/ +package main + +import ( + "context" + "fmt" + "os" + + "examples" + walletclient "github.com/bitcoin-sv/spv-wallet-go-client" +) + +func main() { + defer examples.HandlePanic() + + examples.CheckIfAdminKeyExists() + + server := "http://localhost:3003/v1" + + adminClient := walletclient.NewWithAdminKey(server, examples.ExampleAdminKey) + ctx := context.Background() + + metadata := map[string]any{"some_metadata": "example"} + + newXPubRes := adminClient.AdminNewXpub(ctx, examples.ExampleXPub, metadata) + fmt.Println("AdminNewXpub response: ", newXPubRes) + + createPaymailRes, err := adminClient.AdminCreatePaymail(ctx, examples.ExampleXPub, examples.ExamplePaymail, "Some public name", "") + if err != nil { + fmt.Println(err) + os.Exit(1) + } + fmt.Println("AdminCreatePaymail response: ", createPaymailRes) +} diff --git a/examples/admin_remove_user/admin_remove_user.go b/examples/admin_remove_user/admin_remove_user.go new file mode 100644 index 0000000..7498211 --- /dev/null +++ b/examples/admin_remove_user/admin_remove_user.go @@ -0,0 +1,30 @@ +/* +Package main - admin_remove_user example +*/ +package main + +import ( + "context" + "fmt" + "os" + + "examples" + walletclient "github.com/bitcoin-sv/spv-wallet-go-client" +) + +func main() { + defer examples.HandlePanic() + + examples.CheckIfAdminKeyExists() + + const server = "http://localhost:3003/v1" + + adminClient := walletclient.NewWithAdminKey(server, examples.ExampleAdminKey) + ctx := context.Background() + + 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 new file mode 100644 index 0000000..4e295a0 --- /dev/null +++ b/examples/create_transaction/create_transaction.go @@ -0,0 +1,43 @@ +/* +Package main - create_transaction example +*/ +package main + +import ( + "context" + "fmt" + "os" + + "examples" + walletclient "github.com/bitcoin-sv/spv-wallet-go-client" +) + +func main() { + defer examples.HandlePanic() + + examples.CheckIfXPrivExists() + + const server = "http://localhost:3003/v1" + + client := walletclient.NewWithXPriv(server, examples.ExampleXPriv) + ctx := context.Background() + + recipient := walletclient.Recipients{To: "receiver@example.com", Satoshis: 1} + recipients := []*walletclient.Recipients{&recipient} + metadata := map[string]any{"some_metadata": "example"} + + newTransaction, err := client.SendToRecipients(ctx, recipients, metadata) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + fmt.Println("SendToRecipients response: ", newTransaction) + + tx, err := client.GetTransaction(ctx, newTransaction.ID) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + fmt.Println("GetTransaction response: ", tx) + +} diff --git a/examples/example_keys.go b/examples/example_keys.go new file mode 100644 index 0000000..68cc2e8 --- /dev/null +++ b/examples/example_keys.go @@ -0,0 +1,37 @@ +/* +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 `task generate-keys` + + // ExampleXPriv - example private key + ExampleXPriv 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 new file mode 100644 index 0000000..f96fc8d --- /dev/null +++ b/examples/generate_keys/generate_keys.go @@ -0,0 +1,19 @@ +/* +Package main - generate_keys example +*/ +package main + +import ( + "fmt" + + "examples" +) + +func main() { + keys := examples.GenerateKeys() + exampleXPriv := keys.XPriv() + exampleXPub := keys.XPub().String() + + fmt.Println("exampleXPriv: ", exampleXPriv) + fmt.Println("exampleXPub: ", exampleXPub) +} diff --git a/examples/get_balance/get_balance.go b/examples/get_balance/get_balance.go new file mode 100644 index 0000000..8295b61 --- /dev/null +++ b/examples/get_balance/get_balance.go @@ -0,0 +1,31 @@ +/* +Package main - get_balance example +*/ +package main + +import ( + "context" + "fmt" + "os" + + "examples" + walletclient "github.com/bitcoin-sv/spv-wallet-go-client" +) + +func main() { + defer examples.HandlePanic() + + examples.CheckIfXPrivExists() + + const server = "http://localhost:3003/v1" + + client := walletclient.NewWithXPriv(server, examples.ExampleXPriv) + ctx := context.Background() + + xpubInfo, err := client.GetXPub(ctx) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + fmt.Println("Current balance: ", xpubInfo.CurrentBalance) +} diff --git a/examples/go.mod b/examples/go.mod new file mode 100644 index 0000000..55eee5b --- /dev/null +++ b/examples/go.mod @@ -0,0 +1,23 @@ +module examples + +go 1.22.3 + +replace examples => ./ + +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.12 +) + +require ( + github.com/bitcoinschema/go-bitcoin/v2 v2.0.5 // indirect + github.com/bitcoinsv/bsvd v0.0.0-20190609155523-4c29707f7173 // indirect + github.com/boombuler/barcode v1.0.1 // indirect + github.com/libsv/go-bk v0.1.6 // indirect + github.com/libsv/go-bt/v2 v2.2.5 // indirect + github.com/pkg/errors v0.9.1 // indirect + github.com/pquerna/otp v1.4.0 // indirect + golang.org/x/crypto v0.23.0 // indirect +) diff --git a/examples/go.sum b/examples/go.sum new file mode 100644 index 0000000..5e7a550 --- /dev/null +++ b/examples/go.sum @@ -0,0 +1,30 @@ +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= +github.com/bitcoinsv/bsvd v0.0.0-20190609155523-4c29707f7173/go.mod h1:BZ1UcC9+tmcDEcdVXgpt13hMczwJxWzpAn68wNs7zRA= +github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8= +github.com/boombuler/barcode v1.0.1 h1:NDBbPmhS+EqABEs5Kg3n/5ZNjy73Pz7SIV+KCeqyXcs= +github.com/boombuler/barcode v1.0.1/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/libsv/go-bk v0.1.6 h1:c9CiT5+64HRDbzxPl1v/oiFmbvWZTuUYqywCf+MBs/c= +github.com/libsv/go-bk v0.1.6/go.mod h1:khJboDoH18FPUaZlzRFKzlVN84d4YfdmlDtdX4LAjQA= +github.com/libsv/go-bt/v2 v2.2.5 h1:VoggBLMRW9NYoFujqe5bSYKqnw5y+fYfufgERSoubog= +github.com/libsv/go-bt/v2 v2.2.5/go.mod h1:cV45+jDlPOLfhJLfpLmpQoWzrIvVth9Ao2ZO1f6CcqU= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pquerna/otp v1.4.0 h1:wZvl1TIVxKRThZIBiwOOHOGP/1+nZyWBil9Y2XNEDzg= +github.com/pquerna/otp v1.4.0/go.mod h1:dkJfzwRKNiegxyNb54X/3fLwhCynbMspSyWKnvi1AEg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= +golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/examples/handle_exceptions/handle_exceptions.go b/examples/handle_exceptions/handle_exceptions.go new file mode 100644 index 0000000..8a2ac45 --- /dev/null +++ b/examples/handle_exceptions/handle_exceptions.go @@ -0,0 +1,35 @@ +/* +Package main - handle_exceptions example +*/ +package main + +import ( + "context" + "fmt" + "os" + + "examples" + walletclient "github.com/bitcoin-sv/spv-wallet-go-client" +) + +func main() { + defer examples.HandlePanic() + + examples.CheckIfXPubExists() + + const server = "http://localhost:3003/v1" + + client := walletclient.NewWithXPub(server, examples.ExampleXPub) + ctx := context.Background() + + status, err := client.AdminGetStatus(ctx) + + if err != nil { + fmt.Println("Response status: ", err.GetStatusCode()) + fmt.Println("Content: ", err.Error()) + + os.Exit(1) + } + + fmt.Println("Status: ", status) +} diff --git a/examples/http/http.go b/examples/http/http.go deleted file mode 100644 index 231b8bd..0000000 --- a/examples/http/http.go +++ /dev/null @@ -1,17 +0,0 @@ -package main - -import ( - "fmt" - - walletclient "github.com/bitcoin-sv/spv-wallet-go-client" - "github.com/bitcoin-sv/spv-wallet-go-client/xpriv" -) - -func main() { - // Generate keys - keys, _ := xpriv.Generate() - - // Create a client - wc := walletclient.NewWithXPriv("https://localhost:3001", keys.XPriv()) - fmt.Println(wc.IsSignRequest()) -} diff --git a/examples/http_with_access_key/http_with_access_key.go b/examples/http_with_access_key/http_with_access_key.go deleted file mode 100644 index 39d9f1a..0000000 --- a/examples/http_with_access_key/http_with_access_key.go +++ /dev/null @@ -1,14 +0,0 @@ -package main - -import ( - walletclient "github.com/bitcoin-sv/spv-wallet-go-client" -) - -func main() { - // Replace with created access key - exampleAccessKey := "some_generated_access_key" - - // Create a client - _ = walletclient.NewWithAccessKey("http://localhost:3003", exampleAccessKey) - -} diff --git a/examples/keys/keys.go b/examples/keys/keys.go deleted file mode 100644 index e088961..0000000 --- a/examples/keys/keys.go +++ /dev/null @@ -1,37 +0,0 @@ -package main - -import ( - "fmt" - - "github.com/bitcoin-sv/spv-wallet-go-client/xpriv" -) - -func main() { - // Generate keys - keys, err := xpriv.Generate() - if err != nil { - panic(err) - } - - // Generate keys from mnemonic string - xpriv3, err := xpriv.FromMnemonic(keys.Mnemonic()) - if err != nil { - panic(err) - } - - fmt.Println("<-- FromMnemonic method") - fmt.Println("XPriv: ", xpriv3.XPriv()) - fmt.Println("XPub: ", xpriv3.XPub().String()) - fmt.Println("Mnemonic: ", xpriv3.Mnemonic()) - - // Generate keys from string - xpriv2, err := xpriv.FromString(keys.XPriv()) - if err != nil { - panic(err) - } - - fmt.Println("<-- FromString method") - fmt.Println("XPriv: ", xpriv2.XPriv()) - fmt.Println("XPub: ", xpriv2.XPub().String()) - fmt.Println("Can not get mnemonic from keys generated from string") -} diff --git a/examples/list_transactions/list_transactions.go b/examples/list_transactions/list_transactions.go new file mode 100644 index 0000000..7bfa99f --- /dev/null +++ b/examples/list_transactions/list_transactions.go @@ -0,0 +1,48 @@ +/* +Package main - list_transactions example +*/ +package main + +import ( + "context" + "fmt" + "os" + + "examples" + walletclient "github.com/bitcoin-sv/spv-wallet-go-client" + "github.com/bitcoin-sv/spv-wallet/models/filter" +) + +func main() { + defer examples.HandlePanic() + + examples.CheckIfXPrivExists() + + const server = "http://localhost:3003/v1" + + client := walletclient.NewWithXPriv(server, examples.ExampleXPriv) + ctx := context.Background() + + metadata := map[string]any{} + + conditions := filter.TransactionFilter{} + queryParams := filter.QueryParams{} + + txs, err := client.GetTransactions(ctx, &conditions, metadata, &queryParams) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + fmt.Println("GetTransactions response: ", txs) + + conditions = filter.TransactionFilter{BlockHeight: func(i uint64) *uint64 { return &i }(839228)} + queryParams = filter.QueryParams{PageSize: 100, Page: 1} + + txsFiltered, err := client.GetTransactions(ctx, &conditions, metadata, &queryParams) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + fmt.Println("Filtered GetTransactions response: ", txsFiltered) + +} diff --git a/examples/new_paymail/new_paymail.go b/examples/new_paymail/new_paymail.go deleted file mode 100644 index 6930ea6..0000000 --- a/examples/new_paymail/new_paymail.go +++ /dev/null @@ -1,17 +0,0 @@ -package main - -import ( - "context" - - walletclient "github.com/bitcoin-sv/spv-wallet-go-client" - "github.com/bitcoin-sv/spv-wallet-go-client/xpriv" -) - -func main() { - // Replace with your admin keys - keys, _ := xpriv.Generate() - - // Create a client - wc := walletclient.NewWithXPriv("https://localhost:3001", keys.XPriv()) - wc.AdminCreatePaymail(context.Background(), keys.XPub().String(), "foo@domain.com", "", "Foo") -} diff --git a/examples/register_xpub/register_xpub.go b/examples/register_xpub/register_xpub.go deleted file mode 100644 index d63792e..0000000 --- a/examples/register_xpub/register_xpub.go +++ /dev/null @@ -1,26 +0,0 @@ -package main - -import ( - "context" - "fmt" - - walletclient "github.com/bitcoin-sv/spv-wallet-go-client" - "github.com/bitcoin-sv/spv-wallet-go-client/xpriv" -) - -func main() { - // Replace with your admin keys - keys, _ := xpriv.Generate() - - // Create a client - wc := walletclient.NewWithXPriv("localhost:3003", keys.XPriv()) - ctx := context.Background() - _ = wc.AdminNewXpub(ctx, keys.XPub().String(), map[string]any{"example_field": "example_data"}) - - xpubKey, err := wc.GetXPub(ctx) - if err != nil { - fmt.Println(err) - } - - fmt.Println(xpubKey) -} diff --git a/examples/send_op_return/send_op_return.go b/examples/send_op_return/send_op_return.go new file mode 100644 index 0000000..8c66d8a --- /dev/null +++ b/examples/send_op_return/send_op_return.go @@ -0,0 +1,49 @@ +/* +Package main - send_op_return example +*/ +package main + +import ( + "context" + "fmt" + "os" + + "examples" + walletclient "github.com/bitcoin-sv/spv-wallet-go-client" + "github.com/bitcoin-sv/spv-wallet/models" +) + +func main() { + defer examples.HandlePanic() + + examples.CheckIfXPrivExists() + + const server = "http://localhost:3003/v1" + + client := walletclient.NewWithXPriv(server, examples.ExampleXPriv) + ctx := context.Background() + + metadata := map[string]any{} + + opReturn := models.OpReturn{StringParts: []string{"hello", "world"}} + transactionConfig := models.TransactionConfig{Outputs: []*models.TransactionOutput{{OpReturn: &opReturn}}} + + draftTransaction, err := client.DraftTransaction(ctx, &transactionConfig, metadata) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + fmt.Println("DraftTransaction response: ", draftTransaction) + + finalized, err := client.FinalizeTransaction(draftTransaction) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + transaction, err := client.RecordTransaction(ctx, finalized, draftTransaction.ID, metadata) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + fmt.Println("Transaction with OP_RETURN: ", transaction) +} diff --git a/examples/utils.go b/examples/utils.go new file mode 100644 index 0000000..ef6f6c1 --- /dev/null +++ b/examples/utils.go @@ -0,0 +1,50 @@ +/* +Package examples - Utility functions for this package +*/ +package examples + +import ( + "fmt" + "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) +} + +// HandlePanic - function used to handle a recovery after a panic - use with defer +func HandlePanic() { + r := recover() + + if r != nil { + 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 new file mode 100644 index 0000000..8e35cbe --- /dev/null +++ b/examples/xpriv_from_mnemonic/xpriv_from_mnemonic.go @@ -0,0 +1,24 @@ +/* +Package main - xpriv_from_mnemonic example +*/ +package main + +import ( + "fmt" + "os" + + "github.com/bitcoin-sv/spv-wallet-go-client/xpriv" +) + +func main() { + // This is an example mnemonic phrase - replace it with your own + const mnemonicPhrase = "nut same spike popular already mercy kit board rent light illegal local eight filter tube" + + keys, err := xpriv.FromMnemonic(mnemonicPhrase) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + fmt.Println("extracted xPriv: ", keys.XPriv()) +} diff --git a/examples/xpub_from_xpriv/xpub_from_xpriv.go b/examples/xpub_from_xpriv/xpub_from_xpriv.go new file mode 100644 index 0000000..66fc8cd --- /dev/null +++ b/examples/xpub_from_xpriv/xpub_from_xpriv.go @@ -0,0 +1,24 @@ +/* +Package main - xpub_from_xpriv example +*/ +package main + +import ( + "fmt" + "os" + + "github.com/bitcoin-sv/spv-wallet-go-client/xpriv" +) + +func main() { + // This is an example xPriv key - replace it with your own + const xPriv = "xprv9s21ZrQH143K4VneY3UWCF1o5Kk2tmgGrGtMtsrThCTsHsszEZ6H1iP37ZTwuUBvMwudG68SRkcfTjeu8h3rkayfyqkjKAStFBkuNsBnAkS" + + keys, err := xpriv.FromString(xPriv) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + fmt.Println("extracted xPub: ", keys.XPub().String()) +}