Skip to content

Commit

Permalink
feat(SPV-851) changes after CR
Browse files Browse the repository at this point in the history
  • Loading branch information
yarex-4chain committed Jun 14, 2024
1 parent 0d77c56 commit 2580745
Show file tree
Hide file tree
Showing 17 changed files with 201 additions and 127 deletions.
54 changes: 0 additions & 54 deletions examples/Makefile

This file was deleted.

18 changes: 9 additions & 9 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
62 changes: 62 additions & 0 deletions examples/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -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
12 changes: 6 additions & 6 deletions examples/admin_add_user/admin_add_user.go
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
18 changes: 11 additions & 7 deletions examples/admin_remove_user/admin_remove_user.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
12 changes: 6 additions & 6 deletions examples/create_transaction/create_transaction.go
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
32 changes: 30 additions & 2 deletions examples/example_keys.go
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 5 additions & 1 deletion examples/generate_keys/generate_keys.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
/*
Package main - generate_keys example
*/
package main

import (
"examples"
"fmt"

"examples"
)

func main() {
Expand Down
13 changes: 6 additions & 7 deletions examples/get_balance/get_balance.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
/*
Package main - get_balance example
*/
package main

import (
"context"
"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()
Expand Down
2 changes: 1 addition & 1 deletion examples/go.mod

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

4 changes: 2 additions & 2 deletions examples/go.sum

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

13 changes: 6 additions & 7 deletions examples/handle_exceptions/handle_exceptions.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
/*
Package main - handle_exceptions example
*/
package main

import (
"context"
"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()
Expand Down
Loading

0 comments on commit 2580745

Please sign in to comment.