# MetaMask Snap

During the Nocturne onboarding flow, the user derives an alternative private key called their Nocturne spending key. This key controls the user's funds within Nocturne. The Nocturne MetaMask Snap stores and manages this spending key.

#### Nocturne Spending Key Derivation

During the first user onboarding flow, the user is prompted to sign a fixed message. The produced signature serves as the user's spending key and is stored in the Nocturne Snap. After being stored, the key never leaves the Nocturne Snap and is only accessed by the Snap for producing signatures.

The Nocturne spending key is meant to be derived from an Ethereum account the user has carefully protected and secured. The reason for this is that if the user ever deletes their Nocturne Snap, they can always recover the stored spending key by re-signing the original fixed message with their Ethereum account. That said if the user ever loses both the Nocturne spending key and the private key to the original signing Ethereum account, they will be unable to recover their funds in Nocturne.

#### Registering Your Canonical Address

A canonical Nocturne address is a public address that can be used to generate more stealth addresses that belong to the owner of the corresponding Nocturne spending key. If you would like to pay someone privately to their Nocturne account, you would simply need to produce a stealth address using the recipient's canonical address and deposit to that stealth address. For ease of access, users publish a mapping of their public Ethereum address to their canonical Nocturne address. This makes private payments convenient, as one simply needs to look up the canonical address of the recipient they'd like to pay (using the recipient's Ethereum address) and generate a stealth address.

One of the last steps in the user-onboarding flow is the registration of the user's canonical Nocturne address against their public Ethereum address. The Nocturne Snap will prompt the user to sign a message proving that they own the canonical Nocturne address. After that, the user generates a ZKP of the signature and then submits it to the canonical address registry contract to link their Nocturne address to their public wallet.

#### Signing Operations

All operations that spend private funds, (transferring ETH to a fresh address, performing a private swap, etc) must be authorized from the user's Nocturne spending key. When a user wants to perform an operation, the Nocturne Snap will prompt the user to confirm they would like to authorize the operation, displaying metadata about the particular operation in question. If the user hits "Confirm," the Snap will sign the operation with the user's spending key, authorizing it.


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://nocturne-xyz.gitbook.io/nocturne/users/metamask-snap.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
