Skip to content

Commit

Permalink
feat(SPV-904) update endpoint use cases
Browse files Browse the repository at this point in the history
  • Loading branch information
yarex-4chain committed Jul 1, 2024
1 parent f788b2b commit 8cea9ca
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 8 deletions.
7 changes: 6 additions & 1 deletion access_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ import (
func TestAccessKeys(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/v1/access-key":
case "/v1/users/current/keys/" + fixtures.AccessKey.ID:
switch r.Method {
case http.MethodGet, http.MethodDelete:
json.NewEncoder(w).Encode(fixtures.AccessKey)
}
case "/v1/users/current/keys":
switch r.Method {
case http.MethodGet, http.MethodPost, http.MethodDelete:
json.NewEncoder(w).Encode(fixtures.AccessKey)
Expand Down
19 changes: 19 additions & 0 deletions examples/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,27 @@ 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

access_key:
desc: "running access_key..."
cmds:
- echo "running access_key..."
- go run ./access_key/access_key.go

get_shared_config:
desc: "running get_shared_config..."
cmds:
- echo "running get_shared_config..."
- go run ./get_shared_config/get_shared_config.go

update_xpub_metadata:
desc: "running update_xpub_metadata..."
cmds:
- echo "running update_xpub_metadata..."
- go run ./update_xpub_metadata/update_xpub_metadata.go
49 changes: 49 additions & 0 deletions examples/access_key/access_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Package main - access_key example
*/
package main

import (
"context"
"fmt"
"os"

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

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{"some_metadata": "example"}
createdAccessKey, err := client.CreateAccessKey(ctx, metadata)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Created access key ID: ", createdAccessKey.ID)
fmt.Println("Metadata: ", createdAccessKey.Metadata)
fmt.Println("Created at: ", createdAccessKey.CreatedAt)

fetchedAccessKey, err := client.GetAccessKey(ctx, createdAccessKey.ID)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Fetched access key ID: ", fetchedAccessKey.ID)

revokedAccessKey, err := client.RevokeAccessKey(ctx, createdAccessKey.ID)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Revoked access key ID: ", revokedAccessKey.ID)
fmt.Println("Revoked at: ", revokedAccessKey.RevokedAt)
}
32 changes: 32 additions & 0 deletions examples/get_shared_config/get_shared_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Package main - get_shared_config example
*/
package main

import (
"context"
"fmt"
"os"

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

func main() {
defer examples.HandlePanic()

examples.CheckIfXPrivExists()

const server = "http://localhost:3003/v1"

client := walletclient.NewWithXPriv(server, examples.ExampleXPriv)
ctx := context.Background()

sharedConfig, err := client.GetSharedConfig(ctx)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Shared config (PaymailDomains): ", sharedConfig.PaymailDomains)
fmt.Println("Shared config (ExperimentalFeatures): ", sharedConfig.ExperimentalFeatures)
}
41 changes: 41 additions & 0 deletions examples/update_xpub_metadata/update_xpub_metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Package main - update_xpub_metadata example
*/
package main

import (
"context"
"fmt"
"os"

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

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("XPub metadata: ", xpubInfo.Metadata)
fmt.Println("XPub (updated_at): ", xpubInfo.UpdatedAt)

metadata := map[string]any{"some_metadata_2": "example2"}
updatedXpubInfo, err := client.UpdateXPubMetadata(ctx, metadata)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Updated XPub metadata: ", updatedXpubInfo.Metadata)
fmt.Println("Updated XPub (updated_at): ", updatedXpubInfo.UpdatedAt)
}
12 changes: 6 additions & 6 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (wc *WalletClient) SetAdminKey(adminKey *bip32.ExtendedKey) {
func (wc *WalletClient) GetXPub(ctx context.Context) (*models.Xpub, ResponseError) {
var xPub models.Xpub
if err := wc.doHTTPRequest(
ctx, http.MethodGet, "/xpub", nil, wc.xPriv, true, &xPub,
ctx, http.MethodGet, "/users/current", nil, wc.xPriv, true, &xPub,
); err != nil {
return nil, err
}
Expand All @@ -57,7 +57,7 @@ func (wc *WalletClient) UpdateXPubMetadata(ctx context.Context, metadata map[str

var xPub models.Xpub
if err := wc.doHTTPRequest(
ctx, http.MethodPatch, "/xpub", jsonStr, wc.xPriv, true, &xPub,
ctx, http.MethodPatch, "/users/current", jsonStr, wc.xPriv, true, &xPub,
); err != nil {
return nil, err
}
Expand All @@ -69,7 +69,7 @@ func (wc *WalletClient) UpdateXPubMetadata(ctx context.Context, metadata map[str
func (wc *WalletClient) GetAccessKey(ctx context.Context, id string) (*models.AccessKey, ResponseError) {
var accessKey models.AccessKey
if err := wc.doHTTPRequest(
ctx, http.MethodGet, "/access-key?"+FieldID+"="+id, nil, wc.xPriv, true, &accessKey,
ctx, http.MethodGet, "/users/current/keys/"+id, nil, wc.xPriv, true, &accessKey,
); err != nil {
return nil, err
}
Expand Down Expand Up @@ -115,7 +115,7 @@ func (wc *WalletClient) GetAccessKeysCount(
func (wc *WalletClient) RevokeAccessKey(ctx context.Context, id string) (*models.AccessKey, ResponseError) {
var accessKey models.AccessKey
if err := wc.doHTTPRequest(
ctx, http.MethodDelete, "/access-key?"+FieldID+"="+id, nil, wc.xPriv, true, &accessKey,
ctx, http.MethodDelete, "/users/current/keys/"+id, nil, wc.xPriv, true, &accessKey,
); err != nil {
return nil, err
}
Expand All @@ -133,7 +133,7 @@ func (wc *WalletClient) CreateAccessKey(ctx context.Context, metadata map[string
}
var accessKey models.AccessKey
if err := wc.doHTTPRequest(
ctx, http.MethodPost, "/access-key", jsonStr, wc.xPriv, true, &accessKey,
ctx, http.MethodPost, "/users/current/keys", jsonStr, wc.xPriv, true, &accessKey,
); err != nil {
return nil, err
}
Expand Down Expand Up @@ -665,7 +665,7 @@ func (wc *WalletClient) GetSharedConfig(ctx context.Context) (*models.SharedConf
return nil, WrapError(errors.New("neither xPriv nor adminXPriv is provided"))
}
if err := wc.doHTTPRequest(
ctx, http.MethodGet, "/shared-config", nil, key, true, &model,
ctx, http.MethodGet, "/configs/shared", nil, key, true, &model,
); err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion xpubs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestXpub(t *testing.T) {
var response xpub
// Check path and method to customize the response
switch {
case r.URL.Path == "/v1/xpub":
case r.URL.Path == "/v1/users/current":
metadata := &models.Metadata{"key": "value"}
if update {
metadata = &models.Metadata{"updated": "info"}
Expand Down

0 comments on commit 8cea9ca

Please sign in to comment.