NIP-47: Untitled NIP
NIP-47 ====== Nostr Wallet Connect (NWC)
No reviewsSpecification
NIP-47
Nostr Wallet Connect (NWC)
draft optional
Rationale
This NIP describes a way for clients to access a remote lightning wallet through a standardized protocol. Custodians may implement this, or the user may run a bridge that bridges their wallet/node and the Nostr Wallet Connect protocol.
Terms
- client: Nostr app on any platform that wants to interact with a lightning wallet.
- user: The person using the client, and wants to connect their wallet to their client.
- wallet service: Nostr app that typically runs on an always-on computer (eg. in the cloud or on a Raspberry Pi). This app has access to the APIs of the wallets it serves.
Theory of Operation
Fundamentally NWC is communication between a client and wallet service by the means of E2E-encrypted direct messages over a nostr relay. The relay knows the kinds and tags of notes, but not the content of the encrypted payloads. The user's identity key is not used to avoid linking payment activity to the user. Ideally unique keys are used for each individual connection.
-
Users who wish to use this NIP to allow client(s) to interact with their wallet must first acquire a special "connection" URI from their NIP-47 compliant wallet application. The wallet application may provide this URI using a QR screen, or a pasteable string, or some other means.
-
The user should then copy this URI into their client(s) by pasting, or scanning the QR, etc. The client(s) should save this URI and use it later whenever the user (or the client on the user's behalf) wants to interact with the wallet. The client should then request an
info(13194) event from the relay(s) specified in the URI. The wallet service will have sent that event to those relays earlier, and the relays will hold it as a replaceable event. -
When the user initiates a payment their nostr client create a
pay_invoicerequest, encrypts it using a token from the URI, and sends it (kind 23194) to the relay(s) specified in the connection URI. The wallet service will be listening on those relays and will decrypt the request and then contact the user's wallet application to send the payment. The wallet service will know how to talk to the wallet application because the connection URI specified relay(s) that have access to the wallet app API. -
Once the payment is complete the wallet service will send an encrypted
response(kind 23195) to the user over the relay(s) in the URI. -
The wallet service may send encrypted notifications (kind 23197) of wallet events (such as a received payment) to the client.
Events
There are four event kinds:
NIP-47 info event: 13194NIP-47 request: 23194NIP-47 response: 23195NIP-47 notification event: 23197 (23196 for backwards compatibility with NIP-04)
Info Event
The info event should be a replaceable event that is published by the wallet service on the relay to indicate which capabilities it supports.
The content should be a plaintext string with the supported capabilities space-separated, eg. pay_invoice get_balance notifications.
If the wallet service supports notifications, the info event SHOULD contain a notifications tag with the supported notification types space-separated, eg. payment_received payment_sent.
It should also contain supported encryption modes as described in the Encryption section. For example:
{
"kind": 13194,
"tags": [
["encryption", "nip44_v2 nip04"], // List of supported encryption schemes as described in the Encryption section.
["notifications", "payment_received payment_sent"]
// ...
],
"content": "pay_invoice get_balance make_invoice lookup_invoice list_transactions get_info notifications",
// ...
}
Request and Response Events
Both the request and response events SHOULD contain one p tag, containing the public key of the wallet service if this is a request, and the public key of the client if this is a response. The response event SHOULD contain an e tag with the id of the request event it is responding to.
Optionally, a request can have an expiration tag that has a unix timestamp in seconds. If the request is received after this timestamp, it should be ignored.
The content of requests and responses is encrypted with NIP44, and is a JSON-RPCish object with a semi-fixed structure.
Important note for backwards-compatibility: The initial version of the protocol used NIP04. If a wallet service or client app does not include the encryption tag in the
info or request events, it should be assumed that the connection is using NIP04 for encryption. See the Encryption section for more information.
Example request:
{
"kind" 23194,
"tags": [
["encryption", "nip44_v2"],
["p", "03..." ] // public key of the wallet service.
// ...
],
"content": nip44_encrypt({ // Encryption type corresponds to the `encryption` tag.
"method": "pay_invoice", // method, string
"params": { // params, object
"invoice": "lnbc50n1..." // command-related data
}
}),
}
Example response:
{
"kind" 23195,
"tags": [
["p", "03..." ] // public key of the requesting client app
["e", "1234"] // id of the request event this is responding to
// ...
],
"content": nip44_encrypt({ // Encrypted using the scheme requested by the client.
"result_type": "pay_invoice", //indicates the structure of the result field
"error": { //object, non-null in case of error
"code": "UNAUTHORIZED", //string error code, see below
"message": "human readable error message"
},
"result": { // result, object. null in case of error.
"preimage": "0123456789abcdef..." // command-related data
}
})
// ...
}
The result_type field MUST contain the name of the method that this event is responding to.
The error field MUST contain a message field with a human readable error message and a code field with the error code if the command was not successful.
If the command was successful, the error field must be null.
Notification Events
The notification event is a kind 23197 event SHOULD contain one p tag, the public key of the client.
The content of notifications is encrypted with NIP44 (or NIP-04 for legacy client apps), and is a JSON-RPCish object with a semi-fixed structure:
{
"notification_type": "payment_received", //indicates the structure of the notification field
"notification": {
"payment_hash": "0123456789abcdef..." // notification-related data
}
}
Error codes
RATE_LIMITED: The client is sending commands too fast. It should retry in a few seconds.NOT_IMPLEMENTED: The command is not known or is intentionally not implemented.INSUFFICIENT_BALANCE: The wallet does not have enough funds to cover a fee reserve or the payment amount.QUOTA_EXCEEDED: The wallet has exceeded its spending quota.RESTRICTED: This public key is not allowed to do this operation.UNAUTHORIZED: This public key has no wallet connected.INTERNAL: An internal error.UNSUPPORTED_ENCRYPTION: The encryption type of the request is not supported by the wallet service.OTHER: Other error.
Nostr Wallet Connect URI
Communication between the client and wallet service requires two keys in order to encrypt and decrypt messages. The connection URI includes the secret key of the client and only the public key of the wallet service.
The client discovers wallet service by scanning a QR code, handling a deeplink or pasting in a URI.
The wallet service generates this connection URI with protocol nostr+walletconnect:// and base path its 32-byte hex-encoded pubkey, which SHOULD be unique per client connection.
The connection URI contains the following query string parameters:
relayRequired. URL of the relay where the wallet service is connected and will be listening for events. May be more than one.secretRequired. 32-byte randomly generated hex encoded string. The client MUST use this to sign events and encrypt payloads when communicating with the wallet service. The wallet service MUST use the corresponding public key of this secret to communicate with the client.- Authorization does not require passing keys back and forth.
- The user can have different keys for different applications. Keys can be revoked and created at will and have arbitrary constraints (eg. budgets).
- The key is harder to leak since it is not shown to the user and backed up.
- It improves privacy because the user's main key would not be linked to their payments.
lud16Recommended. A lightning address that clients can use to automatically setup thelud16field on the user's profile if they have none configured.
The client should then store this connection and use it when the user wants to perform actions like paying an invoice. Due to this NIP using ephemeral events, it is recommended to pick relays that do not close connections on inactivity to not drop events, and ideally retain the events until they are either consumed or become stale.
- When the client sends or receives a message it will use the
secretfrom the connection URI and wallet service'spubkeyto encrypt or decrypt. - When the wallet service sends or receives a message it will use its own secret and the corresponding pubkey of the client's
secretto encrypt or decrypt. The wallet service SHOULD NOT store the secret it generates for the client and MUST NOT rely on the knowing the client secret for general operation.
Example connection string
nostr+walletconnect://b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4?relay=wss%3A%2F%2Frelay.damus.io&secret=71a8c14c1407c113601079c4302dab36460f0ccd0ad506f1f2dc73b5100e4f3c
Commands
pay_invoice
Description: Requests payment of an invoice.
Request:
{
"method": "pay_invoice",
"params": {
"invoice": "lnbc50n1...", // bolt11 invoice
"amount": 123, // invoice amount in msats, optional
"metadata": {} // generic metadata that can be used to add things like zap/boostagram details for a payer name/comment/etc, optional
}
}
Response:
{
"result_type": "pay_invoice",
"result": {
"preimage": "0123456789abcdef...", // preimage of the payment
"fees_paid": 123, // value in msats, optional
}
}
Errors:
PAYMENT_FAILED: The payment failed. This may be due to a timeout, exhausting all routes, insufficient capacity or similar.
pay_keysend
Request:
{
"method": "pay_keysend",
"params": {
"amount": 123, // invoice amount in msats, required
"pubkey": "03...", // payee pubkey, required
"preimage": "0123456789abcdef...", // preimage of the payment, optional
"tlv_records": [ // tlv records, optional
{
"type": 5482373484, // tlv type
"value": "0123456789abcdef" // hex encoded tlv value
}
]
}
}
Response:
{
"result_type": "pay_keysend",
"result": {
"preimage": "0123456789abcdef...", // preimage of the payment
"fees_paid": 123, // value in msats, optional
}
}
Errors:
PAYMENT_FAILED: The payment failed. This may be due to a timeout, exhausting all routes, insufficient capacity or similar.
make_invoice
Request:
{
"method": "make_invoice",
"params": {
"amount": 123, // value in msats
"description": "string", // invoice's description, optional
"description_hash": "string", // invoice's description hash, optional
"expiry": 213, // expiry in seconds from time invoice is created, optional
"metadata": {} // generic metadata that can be used to add things like zap/boostagram details for a payer name/comment/etc, optional
}
}
Response:
{
"result_type": "make_invoice",
"result": {
"type": "incoming", // "incoming" for invoices, "outgoing" for payments
"state": "pending", // optional
"invoice": "string", // encoded invoice, optional
"description": "string", // invoice's description, optional
"description_hash": "string", // invoice's description hash, optional
"preimage": "string", // payment's preimage, optional if unpaid
"payment_hash": "string", // Payment hash for the payment
"amount": 123, // value in msats
"fees_paid": 123, // value in msats
"created_at": unixtimestamp, // invoice/payment creation time
"expires_at": unixtimestamp, // invoice expiration time, optional if not applicable
"metadata": {} // generic metadata that can be used to add things like zap/boostagram details for a payer name/comment/etc.
}
}
lookup_invoice
Request:
{
"method": "lookup_invoice",
"params": {
"payment_hash": "31afdf1..", // payment hash of the invoice, one of payment_hash or invoice is required
"invoice": "lnbc50n1..." // invoice to lookup
}
}
Response:
{
"result_type": "lookup_invoice",
"result": {
"type": "incoming", // "incoming" for invoices, "outgoing" for payments
"state": "pending", // can be "pending", "settled", "accepted" (for hold invoices), "expired" (for invoices) or "failed" (for payments), optional
"invoice": "string", // encoded invoice, optional
"description": "string", // invoice's description, optional
"description_hash": "string", // invoice's description hash, optional
"preimage": "string", // payment's preimage, optional if unpaid
"payment_hash": "string", // Payment hash for the payment
"amount": 123, // value in msats
"fees_paid": 123, // value in msats
"created_at": unixtimestamp, // invoice/payment creation time
"expires_at": unixtimestamp, // invoice expiration time, optional if not applicable
"settled_at": unixtimestamp, // invoice/payment settlement time, optional if unpaid
"metadata": {} // generic metadata that can be used to add things like zap/boostagram details for a payer name/comment/etc.
}
}
Errors:
NOT_FOUND: The invoice could not be found by the given parameters.
list_transactions
Lists invoices and payments. If type is not specified, both invoices and payments are returned.
The from and until parameters are timestamps in seconds since epoch. If from is not specified, it defaults to 0.
If until is not specified
[Content truncated — view full spec at source]
Discussion (0 threads)
Sign in to start a discussion on this spec.