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.
- Base64-urlsafe decode the part of the token after
cashuA...
- You will get a JSON, let's call it
cashu
. - Notice the proofs in
cashu.token[0].proofs
- Get the Mint URL in
cashu.token[0].mint
- 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);
- Create a Lightning invoice for the receiver (using LNURL for example) worth
tokenAmount*0.98
but at least2
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.
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.