Skip to content

Commit

Permalink
Merge pull request #97 from Egge21M/receive
Browse files Browse the repository at this point in the history
raw token receive
  • Loading branch information
gandlafbtc authored Jan 18, 2024
2 parents 5b6d14a + c53e794 commit 8dbe219
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
16 changes: 11 additions & 5 deletions src/CashuWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
SerializedBlindedMessage,
SerializedBlindedSignature,
SplitPayload,
Token,
TokenEntry
} from './model/types/index.js';
import {
Expand Down Expand Up @@ -163,22 +164,27 @@ class CashuWallet {
return this.payLnInvoice(invoice, proofs, undefined, counter);
}
/**
* Receive an encoded Cashu token
* @param encodedToken Cashu token
* Receive an encoded or raw Cashu token
* @param {(string|Token)} token - Cashu token
* @param preference optional preference for splitting proofs into specific amounts
* @param counter? optionally set counter to derive secret deterministically. CashuWallet class must be initialized with seed phrase to take effect
* @returns New token with newly created proofs, token entries that had errors, and newKeys if they have changed
*/
async receive(
encodedToken: string,
token: string | Token,
preference?: Array<AmountPreference>,
counter?: number
): Promise<ReceiveResponse> {
const { token } = cleanToken(getDecodedToken(encodedToken));
let decodedToken: Array<TokenEntry>;
if (typeof token === 'string') {
decodedToken = cleanToken(getDecodedToken(token)).token;
} else {
decodedToken = token.token;
}
const tokenEntries: Array<TokenEntry> = [];
const tokenEntriesWithError: Array<TokenEntry> = [];
let newKeys: MintKeys | undefined;
for (const tokenEntry of token) {
for (const tokenEntry of decodedToken) {
if (!tokenEntry?.proofs?.length) {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CashuWallet } from './CashuWallet.js';
import { setGlobalRequestOptions } from './request.js';
import { generateNewMnemonic, deriveSeedFromMnemonic } from './secrets.js';
import { getEncodedToken, getDecodedToken, deriveKeysetId, decodeInvoice } from './utils.js';
import { decode} from '@gandlaf21/bolt11-decode';
import { decode } from '@gandlaf21/bolt11-decode';

export * from './model/types/index.js';

Expand Down
32 changes: 31 additions & 1 deletion test/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { decode } from '@gandlaf21/bolt11-decode';
import nock from 'nock';
import { CashuMint } from '../src/CashuMint.js';
import { CashuWallet } from '../src/CashuWallet.js';
import { cleanToken, getDecodedToken } from '../src/utils.js';

const dummyKeysResp = { 1: '02f970b6ee058705c0dddc4313721cffb7efd3d142d96ea8e01d31c2b2ff09f181' };
const mintUrl = 'https://legend.lnbits.com/cashu/api/v1/4gr9Xcmz3XEkUNwiBiQGoC';
Expand Down Expand Up @@ -35,7 +36,7 @@ describe('test fees', () => {
describe('receive', () => {
const tokenInput =
'eyJwcm9vZnMiOlt7ImlkIjoiL3VZQi82d1duWWtVIiwiYW1vdW50IjoxLCJzZWNyZXQiOiJBZmtRYlJYQUc1UU1tT3ArbG9vRzQ2OXBZWTdiaStqbEcxRXRDT2tIa2hZPSIsIkMiOiIwMmY4NWRkODRiMGY4NDE4NDM2NmNiNjkxNDYxMDZhZjdjMGYyNmYyZWUwYWQyODdhM2U1ZmE4NTI1MjhiYjI5ZGYifV0sIm1pbnRzIjpbeyJ1cmwiOiJodHRwczovL2xlZ2VuZC5sbmJpdHMuY29tL2Nhc2h1L2FwaS92MS80Z3I5WGNtejNYRWtVTndpQmlRR29DIiwiaWRzIjpbIi91WUIvNndXbllrVSJdfV19';
test('test receive', async () => {
test('test receive encoded token', async () => {
nock(mintUrl)
.post('/split')
.reply(200, {
Expand All @@ -61,6 +62,35 @@ describe('receive', () => {
expect(/[A-Za-z0-9+/]{43}=/.test(t.token[0].proofs[0].secret)).toBe(true);
expect(tokensWithErrors).toBe(undefined);
});

test('test receive raw token', async () => {
const decodedInput = cleanToken(getDecodedToken(tokenInput));

nock(mintUrl)
.post('/split')
.reply(200, {
promises: [
{
id: 'z32vUtKgNCm1',
amount: 1,
C_: '021179b095a67380ab3285424b563b7aab9818bd38068e1930641b3dceb364d422'
}
]
});
const wallet = new CashuWallet(mint);

const { token: t, tokensWithErrors } = await wallet.receive(decodedInput);

expect(t.token).toHaveLength(1);
expect(t.token[0].proofs).toHaveLength(1);
expect(t.token[0]).toMatchObject({
proofs: [{ amount: 1, id: 'z32vUtKgNCm1' }],
mint: 'https://legend.lnbits.com/cashu/api/v1/4gr9Xcmz3XEkUNwiBiQGoC'
});
expect(/[0-9a-f]{64}/.test(t.token[0].proofs[0].C)).toBe(true);
expect(/[A-Za-z0-9+/]{43}=/.test(t.token[0].proofs[0].secret)).toBe(true);
expect(tokensWithErrors).toBe(undefined);
});
test('test receive custom split', async () => {
nock(mintUrl)
.post('/split')
Expand Down

0 comments on commit 8dbe219

Please sign in to comment.