Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(SPV-1213): Admin API access with user xpubs implementation. #20

Conversation

mgosek-4chain
Copy link

@mgosek-4chain mgosek-4chain commented Nov 21, 2024

This PR lays the foundation for the Admin API HTTP client, which can be further extended to implement additional missing endpoints.

Major Changes

1. Package Renaming and Separation of Concerns

  • Refactored package structure to clearly separate responsibilities between the Admin API and User API HTTP clients.
  • API instances can be created by calling NewAdminAPIWithXPriv, NewAdminAPIWithXPub, NewUserAPIWithAccessKey, NewUserAPIWithXPriv, NewUserAPIWithXPub

2. Error Handling Enhancements

  • Introduced HTTPErrorFormatter within the errutil package to standardize error messages and enable consistent wrapping of errors after unsuccessful API calls.
  • Configured linter settings to skip certain formatting-related warnings for wrapped errors.

Example error message format after API error call:

failed to send HTTP GET request to record a transaction with ID: 1024 via User Transactions API: HTTP response failure: Bad Request.

3. HTTP Client Setup Refactoring

  • Moved the resty HTTP client setup into an internal package to align with the design agreement of consolidating internal implementation details in one place.

This refactoring improves code organization, error handling consistency, and adheres to agreed-upon design principles.

Pull Request Checklist

  • 📖 I created my PR using provided : CODE_STANDARDS
  • 📖 I have read the short Code of Conduct: CODE_OF_CONDUCT
  • 🏠 I tested my changes locally.
  • ✅ I have provided tests for my changes.
  • 📝 I have used conventional commits.
  • 📗 I have updated any related documentation.
  • 💾 PR was issued based on the Github or Jira issue.

Important

Before running the implementation with a manual example, ensure that the SPV Wallet API is running locally in your environment.

Example testing code snippet:

package main

import (
	"context"
	"encoding/json"
	"fmt"
	"log"
	"strings"

	spvwallet "github.com/bitcoin-sv/spv-wallet-go-client"
	"github.com/bitcoin-sv/spv-wallet-go-client/commands"
	"github.com/bitcoin-sv/spv-wallet-go-client/config"
)

func main() {
	spv, err := spvwallet.NewAdminAPIWithXPriv(ExampleConfig, ExampleXPriv)
	if err != nil {
		log.Fatal(err)
	}

	xPub, err := spv.CreateXPub(context.Background(), &commands.CreateUserXpub{
		Metadata: map[string]any{
			"key": "value",
		},
		XPub: "xPub",
	})
	if err != nil {
		log.Fatal(err)
	}

	Print("XPub:", xPub)
}

var ExampleConfig = config.NewDefaultConfig("http://localhost:3003")

const (
	ExampleXPriv string = "xPub"
	ExampleXPub  string = ""
)

func Print(s string, a any) {
	fmt.Println(strings.Repeat("~", 100))
	fmt.Println(s)
	fmt.Println(strings.Repeat("~", 100))
	res, err := json.MarshalIndent(a, "", " ")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(string(res))
}

Copy link

Manual Tests

ℹ️ Remember to ask team members to perform manual tests and to assign tested label after testing.

@mgosek-4chain mgosek-4chain marked this pull request as ready for review November 22, 2024 12:04
@mgosek-4chain mgosek-4chain changed the title Refactor/spv 1213 admin api access with user xpubs implementation (refactor:SPV-1213): Admin API access with user xpubs implementation. Nov 22, 2024
@mgosek-4chain mgosek-4chain changed the title (refactor:SPV-1213): Admin API access with user xpubs implementation. refactor:(SPV-1213): Admin API access with user xpubs implementation. Nov 22, 2024
@mgosek-4chain mgosek-4chain changed the title refactor:(SPV-1213): Admin API access with user xpubs implementation. refactor(SPV-1213): Admin API access with user xpubs implementation. Nov 22, 2024
user_api.go Show resolved Hide resolved
internal/api/v1/errutil/errutil.go Outdated Show resolved Hide resolved
internal/api/v1/errutil/errutil_test.go Outdated Show resolved Hide resolved
admin_api.go Outdated Show resolved Hide resolved
@mgosek-4chain mgosek-4chain enabled auto-merge (squash) November 27, 2024 10:13
@mgosek-4chain mgosek-4chain merged commit 1eb35b5 into main Nov 27, 2024
8 checks passed
@mgosek-4chain mgosek-4chain deleted the refactor/SPV-1213-admin-api-access-with-user-xpubs-implementation branch November 27, 2024 10:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants