> For the complete documentation index, see [llms.txt](https://docs.bsvblockchain.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.bsvblockchain.org/network-topology/spv-wallet/developer-docs/spv-wallet-go-client/authentication.md).

# Authentication

## Authenticate as admin

To authenticate within the SPV Wallet as an admin, you need to use admin HD key pair.\
At the SPV Wallet side the admin key pair is recognized by the admin xpub which need to be configured ([see configuration docs](/network-topology/spv-wallet/developer-docs/spv-wallet/configuration.md)).

When you have the admin xpriv, you can now create SPV Wallet Client for admin API:

```go
import (
	wallet "github.com/bitcoin-sv/spv-wallet-go-client"
	"github.com/bitcoin-sv/spv-wallet-go-client/config"
)

func main() {
	adminAPI, err := wallet.NewAdminAPIWithXPriv(config.New(config.WithAddr("{{spv-wallet-url}}")), "{{xpriv_of_the_admin}}")
	// ...
}
```

## User

### Authentication using HD keys of user

#### Register users xpub

To authenticate as user, first user and his xpub must be added by admin to SPV Wallet.

To register user, admin need to make a following request to SPV Wallet:

```go
import (
    "context"
    
    wallet "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() {
    adminAPI, err := wallet.NewAdminAPIWithXPriv(config.New(config.WithAddr("{{spv-wallet-url}}")), "{{xpriv_of_the_admin}}")
    // ...
    res, err := adminAPI.CreateXPub(context.Background(), &commands.CreateUserXpub{XPub: "{{xpub_of_the_user}}"})
    // ...
}
```

#### Authenticate with users xpub

To authenticate within the SPV Wallet as a user, you simply need to create a new SPV Wallet Client for users API.

```go
import (
	wallet "github.com/bitcoin-sv/spv-wallet-go-client"
	"github.com/bitcoin-sv/spv-wallet-go-client/config"
)

func main() {
	userAPI, err := wallet.NewUserAPIWithXPriv(config.New(config.WithAddr("{{spv-wallet-url}}")), "{{xpriv_of_the_user}}")
	//...
}
```

### Authentication using Access key

Another way of authenticate as a user is by use of access key.

#### Create

To authenticate as user with access key, first user must create an access key by making a following call:

```go
import (
	"context"

	wallet "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() {
	userAPI, err := wallet.NewUserAPIWithXPriv(config.New(config.WithAddr("{{spv-wallet-url}}")), "{{xpriv_of_the_user}}")
	// ...
	
	response, err := userAPI.GenerateAccessKey(context.Background(), &commands.GenerateAccessKey{})
	// ...
}
```

In response, you can find the following important properties:

* `Key` - which is actually an additional private key and is not stored on the SPV Wallet side, so it is displayed to user only once and user is responsible for storing it
* `ID` - which actually can be used for checking the state of the access key (if it was revoked or when it was created) or revoking it.

#### Use

To authenticate as user with access key, you need to create a new SPV Wallet Client for users API with the access key.

```go
import (
    wallet "github.com/bitcoin-sv/spv-wallet-go-client"
    "github.com/bitcoin-sv/spv-wallet-go-client/config"
)

func main() {
    userAPI, err := wallet.NewUserAPIWithAccessKey(config.New(config.WithAddr("{{spv-wallet-url}}")), "{{key}}")
    //...
}
```

#### Revoke

Whenever a user believes that a particular access key has been compromised or is no longer needed, it can be revoked to prevent any further use.

To revoke an access key, a user needs to make the following call:

```go
import (
	"context"

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

func main() {
	userAPI, err := wallet.NewUserAPIWithXPriv(config.New(config.WithAddr("{{spv-wallet-url}}")), "{{xpriv_of_the_user}}")
	// ...

	err := userAPI.RevokeAccessKey(context.Background(), "{{access_key_id}}")
	// ...
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.bsvblockchain.org/network-topology/spv-wallet/developer-docs/spv-wallet-go-client/authentication.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
