Skip to content

Kukks/DotNut

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

80 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

DotNut

C# library for the Cashu protocol - a Chaumian e-cash system built on Bitcoin/Lightning.

NuGet License: MIT

Installation

dotnet add package DotNut

Usage

The main entry point is the Wallet class, which exposes a fluent builder for connecting to a mint and performing operations.

Setup

var wallet = Wallet.Create()
    .WithMint("https://testnut.cashu.space")
    .WithMnemonic("your twelve word mnemonic phrase here...")
    .WithCounter(new InMemoryCounter());

Mint (Lightning β†’ tokens)

var mintHandler = await wallet
    .CreateMintQuote()
    .WithAmount(1000)
    .WithUnit("sat")
    .ProcessAsyncBolt11();

// Pay the Lightning invoice
Console.WriteLine(mintHandler.GetQuote().Request);

// After payment, mint the tokens
List<Proof> proofs = await mintHandler.Mint();

Swap (rebalance / receive token)

// From a cashuB... token string
List<Proof> proofs = await wallet
    .Swap()
    .WithDLEQVerification()
    .ProcessAsync(); // pass token string to SwapBuilder or use FromInputs()

// From raw proofs
List<Proof> rebalanced = await wallet
    .Swap()
    .FromInputs(existingProofs)
    .ProcessAsync();

Melt (tokens β†’ Lightning)

var meltHandler = await wallet
    .CreateMeltQuote()
    .WithInvoice("lnbc...")
    .WithUnit("sat")
    .ProcessAsyncBolt11();

List<Proof> changeProofs = await meltHandler.Melt(inputProofs);

Restore (from mnemonic)

IEnumerable<Proof> recovered = await wallet
    .Restore()
    .ProcessAsync();

Token encoding

var token = new CashuToken
{
    Unit = "sat",
    Tokens = [new CashuToken.Token { Mint = "https://testnut.cashu.space", Proofs = proofs }]
};

string v4 = token.Encode("B");          // cashuB... (CBOR, compact)
string v3 = token.Encode("A");          // cashuA... (JSON)
string uri = token.Encode("B", makeUri: true); // cashu:cashuB...

var decoded = CashuTokenHelper.Decode(v4, out string version);

P2PK / HTLC spending conditions

// Mint tokens locked to a pubkey
var mintHandler = await wallet
    .CreateMintQuote()
    .WithAmount(500)
    .WithP2PkLock(new P2PKBuilder { Pubkeys = [pubkey], SignatureThreshold = 1 })
    .ProcessAsyncBolt11();

// Spend P2PK-locked tokens by signing during swap
List<Proof> proofs = await wallet
    .Swap()
    .FromInputs(lockedProofs)
    .WithPrivkeys([privKey])
    .ProcessAsync();

Direct API access

If you need raw protocol access without the wallet abstraction:

var httpClient = new HttpClient { BaseAddress = new Uri("https://testnut.cashu.space/") };
var api = new CashuHttpClient(httpClient);

var info = await api.GetInfo();
var keysets = await api.GetKeysets();
var mintQuote = await api.CreateMintQuote<PostMintQuoteBolt11Response, PostMintQuoteBolt11Request>(
    "bolt11", new PostMintQuoteBolt11Request { Amount = 1000, Unit = "sat" }
);

WebSockets (NUT-17)

var wallet = Wallet.Create()
    .WithMint("https://testnut.cashu.space")
    .WithWebsocketService(new WebsocketService());

var ws = await wallet.GetWebsocketService();
// subscribe to quote state changes, proof state updates, etc.

Implemented NUTs

NUT Description
00 Cryptographic primitives & token format
01 Mint public key distribution
02 Keysets and keyset IDs
03 Swapping tokens
04 Minting tokens
05 Melting tokens
06 Mint info
07 Token state check
08 Lightning fee return
09 Token restoration
10 Spending conditions
11 Pay-to-Public-Key (P2PK)
12 DLEQ proofs
13 Deterministic secrets (BIP39)
14 Hash Time-Locked Contracts (HTLC)
17 WebSocket subscriptions
18 Payment requests
20 Signature on Mint Quote
23 BOLT11
25 BOLT12
26 Payment Request Bech32m Encoding
27 Nostr Mint Backup
28 Pay-to-Blinded-Key (P2BK)

TODO:

NUT Description
15 Multipath payments
21 Clear Authentication
22 Blind Authentication

Requirements

  • .NET 8.0+

License

MIT - see LICENSE.

Resources

About

A C# Cashu library

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

 
 
 

Contributors

Languages