> For the complete documentation index, see [llms.txt](https://docs.stacks.co/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.stacks.co/reference/stacks.js/stacks-wallet/derivation/selectstxderivation.md).

# selectStxDerivation

Determines the correct derivation path and username for an account by looking up BNS name ownership on the network. This is used during wallet restoration to figure out whether an account's STX key was derived via the Wallet path or the Data path.

***

### Usage

```ts
import { selectStxDerivation } from '@stacks/wallet-sdk';
import { HDKey } from '@scure/bip32';
import { mnemonicToSeed } from '@scure/bip39';

const seed = await mnemonicToSeed('your 24-word seed phrase ...');
const rootNode = HDKey.fromMasterSeed(seed);

// With a known username
const result = await selectStxDerivation({
  username: 'myname.btc',
  rootNode,
  index: 0,
  network: 'mainnet',
});
console.log(result.stxDerivationType); // DerivationType.Wallet or DerivationType.Data

// Without a username — auto-discovers
const result2 = await selectStxDerivation({
  rootNode,
  index: 0,
  network: 'mainnet',
});
console.log(result2.username);          // discovered username or undefined
console.log(result2.stxDerivationType); // DerivationType.Wallet (default)
```

#### Notes

* If a `username` is provided, it looks up the name's owner address on the network and checks which derivation path matches.
* If no `username` is provided, it tries both derivation paths and looks up names owned by each address.
* If no network is provided, returns `DerivationType.Unknown`.
* Falls back to `DerivationType.Wallet` when no username is found.

[**Reference Link**](https://github.com/stx-labs/stacks.js/tree/main/packages/wallet-sdk/src/derive.ts#L107)

***

### Signature

```ts
function selectStxDerivation(opts: {
  username?: string;
  rootNode: HDKey;
  index: number;
} & NetworkParam): Promise<{
  username: string | undefined;
  stxDerivationType: DerivationType;
}>;
```

***

### Returns

`Promise<{ username: string | undefined; stxDerivationType: DerivationType }>`

An object containing the resolved username (if any) and the derivation type that matches.

***

### Parameters

#### opts (required)

| Property   | Type                                 | Required | Description                                                                     |
| ---------- | ------------------------------------ | -------- | ------------------------------------------------------------------------------- |
| `rootNode` | `HDKey`                              | Yes      | The root HD key derived from the wallet's seed phrase.                          |
| `index`    | `number`                             | Yes      | The account index (0-based).                                                    |
| `username` | `string`                             | No       | A BNS username to look up. If omitted, the function tries to auto-discover one. |
| `network`  | `StacksNetworkName \| StacksNetwork` | No       | The network to query for BNS name lookups.                                      |


---

# 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.stacks.co/reference/stacks.js/stacks-wallet/derivation/selectstxderivation.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.
