Skip to content

Instantly share code, notes, and snippets.

@callebtc
Last active February 20, 2024 00:19
Show Gist options
  • Save callebtc/8a1ac07e8676e702eef8fd2583bd5835 to your computer and use it in GitHub Desktop.
Save callebtc/8a1ac07e8676e702eef8fd2583bd5835 to your computer and use it in GitHub Desktop.
How to redeem a Cashu token to Lightning

Redeem Cashu tokens to Lightning

This gist very briefly describes how to redeem a Cashu token to a Lightning wallet. It uses no Cashu libraries as dependencies. JavaScript code is provided as an example.

Parsing

  1. Base64-urlsafe decode the part of the token after cashuA...
  2. You will get a JSON, let's call it cashu.
  3. Notice the proofs in cashu.token[0].proofs
  4. Get the Mint URL in cashu.token[0].mint
  5. Get the value of the token:
let totalAmount = 0;
cashu.token[0].proofs.forEach((proof) => {
        totalAmount += proof.amount;
});

Alternatively, in JavaScript, you can use .reduce:

let tokenAmount = obj.token[0].proofs.reduce((acc, proof) => acc + proof.amount, 0);

Redeem

  1. Create a Lightning invoice for the receiver (using LNURL for example) worth tokenAmount*0.98 but at least 2 sats less.

Make a POST request to the mint URL:

POST https://mint.host:3338/melt

With the body data:

{
"proofs" : cashu.token[0].proofs,
"pr": "lnbc100n1p3kdrv5sp5lpdxzghe5j67q..."
}

Read more in the NUT-05 Melting Tokens spec.

Overpaid fees

Note: This will not return the overpaid Lightning fees (that might be lower than the 2% reserve that we added above). To do that, you have to provide blank outputs (see NUT-08). The easiest way to do that in TypeScript would be to use the cashu-ts that will automatically get the returned fees for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment