NIP-47 Nostr Wallet Connect implementation for both clients and services.
npm install applesauce-wallet-connect
Connect to a wallet service using a connection string:
import { WalletConnect } from "applesauce-wallet-connect";
const wallet = WalletConnect.fromConnectURI("nostr+walletconnect://relay.example.com?secret=...&pubkey=...");
// Pay an invoice
const result = await wallet.payInvoice("lnbc1...");
console.log("Payment preimage:", result.preimage);
// Get wallet balance
const balance = await wallet.getBalance();
console.log("Balance:", balance.balance, "msats");
Create a wallet service that handles NIP-47 requests:
import { WalletService } from "applesauce-wallet-connect";
import { SimpleSigner } from "applesauce-signers";
// Create a signer for the service
const signer = new SimpleSigner();
// Define method handlers
const handlers = {
get_info: async () => ({
alias: "My Wallet",
color: "#ff0000",
pubkey: await signer.getPublicKey(),
network: "mainnet" as const,
block_height: 800000,
block_hash: "0000...0000",
methods: ["get_info", "get_balance", "pay_invoice"],
}),
get_balance: async () => ({
balance: 100000, // 100 sats in msats
}),
pay_invoice: async (params) => {
// Implement your payment logic here
console.log("Paying invoice:", params.invoice);
return {
preimage: "payment_preimage_here",
fees_paid: 1000,
};
},
};
// Create the service
const service = new WalletService({
subscriptionMethod: mySubscriptionMethod,
publishMethod: myPublishMethod,
relays: ["wss://relay.example.com"],
signer,
handlers,
// Supported encryption methods will be set based on the signer
});
// Start the service
await service.start();
console.log("Wallet service started");
// Get the connection string for the wallet service
console.log(service.getConnectURI());
// Stop the service when done
service.stop();
Both WalletConnect and WalletService support all NIP-47 methods:
get_info
- Get wallet informationget_balance
- Get wallet balancepay_invoice
- Pay a Lightning invoicemulti_pay_invoice
- Pay multiple invoicespay_keysend
- Send a keysend paymentmulti_pay_keysend
- Send multiple keysend paymentsmake_invoice
- Create a new invoicelookup_invoice
- Look up an invoicelist_transactions
- List transactionsFor more detailed documentation, visit the docs.