Skip to content

Commit

Permalink
Cashu upgrade to v2 (#310)
Browse files Browse the repository at this point in the history
* [nip-60] implement create wallet hook

* [nip-60] updaate get wallet hook

* [nip-60] add create token event hook

* [nip-60] add spending history event hook

* [nip-61] add nutzap event hooks

* undo delete old hooks

* use NDKPrivateKeySigner

* [integration] create wallet event

* [integration] create token event

* [nip60] fix create token event

* [nip60] fix create spending event

* [integration] create spending event

* [nip60] fix create wallet event

* [nip60] add get token events by filters

* [nip60] add [[NIP-09]]-delete token events

* [integration] nostr nip60 on pay lightning invoice

* [integration] nostr nip60 on generate ecash

* add keys validations for nostr

* [update] cashu-ts to latest

* add share gift link functionality

* add receive ecash by link screen

* fix gift link url

* hide scroll indicators

* upgrade to cashu v2

* fix cashu screen styles
  • Loading branch information
lindsaymoralesb authored Nov 29, 2024
1 parent 5feb9e5 commit cf64a1e
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 169 deletions.
2 changes: 1 addition & 1 deletion apps/mobile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"dependencies": {
"@babel/runtime": "^7.24.8",
"@cashu/cashu-ts": "^1.2.1",
"@cashu/cashu-ts": "^2.0.0",
"@docusaurus/core": "^3.5.2",
"@docusaurus/preset-classic": "^3.5.2",
"@dynamic-labs/client": "4.0.0-alpha.8",
Expand Down
113 changes: 47 additions & 66 deletions apps/mobile/src/hooks/usePayment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,66 +37,54 @@ export const usePayment = () => {
if (!wallet) {
return undefined;
} else if (proofs) {
const proofsSpent = await wallet.checkProofsSpent(proofs);
let proofsCopy = Array.from(proofs);

if (proofsSpent) {
proofsCopy = proofsCopy?.filter((p) => !proofsSpent?.includes(p));
}

if (proofsCopy.length > 0) {
try {
const response = await meltTokens(pInvoice, proofsCopy);
if (response) {
const {meltQuote, meltResponse, proofsToKeep, remainingProofs, selectedProofs} =
response;
setProofsFilter(selectedProofs);
if (privateKey && publicKey) {
await refetchTokens();
await deleteMultiple(
filteredTokenEvents.map((event) => event.id),
'proofs spent in transaction',
);
const tokenEvent = await createTokenEvent({
walletId,
mint: activeMint,
proofs: proofsToKeep,
});
const destroyedEvents = filteredTokenEvents.map((event) => ({
id: event.id,
marker: 'destroyed' as EventMarker,
}));
await createSpendingEvent({
walletId,
direction: 'out',
amount: (meltQuote.amount + meltQuote.fee_reserve).toString(),
unit: activeUnit,
events: [...destroyedEvents, {id: tokenEvent.id, marker: 'created' as EventMarker}],
});
}
showToast({
title: 'Payment sent.',
type: 'success',
try {
const response = await meltTokens(pInvoice, proofs);
if (response) {
const {meltQuote, meltResponse, proofsToKeep, remainingProofs, selectedProofs} = response;
setProofsFilter(selectedProofs);
if (privateKey && publicKey) {
await refetchTokens();
await deleteMultiple(
filteredTokenEvents.map((event) => event.id),
'proofs spent in transaction',
);
const tokenEvent = await createTokenEvent({
walletId,
mint: activeMint,
proofs: proofsToKeep,
});
setProofs([...remainingProofs, ...proofsToKeep]);
setProofsStorage([...remainingProofs, ...proofsToKeep]);
const newInvoice: ICashuInvoice = {
amount: -(meltQuote.amount + meltQuote.fee_reserve),
bolt11: pInvoice,
quote: meltQuote.quote,
date: Date.now(),
state: MintQuoteState.PAID,
const destroyedEvents = filteredTokenEvents.map((event) => ({
id: event.id,
marker: 'destroyed' as EventMarker,
}));
await createSpendingEvent({
walletId,
direction: 'out',
};
setTransactions([...transactions, newInvoice]);
return meltResponse;
} else {
return undefined;
amount: (meltQuote.amount + meltQuote.fee_reserve).toString(),
unit: activeUnit,
events: [...destroyedEvents, {id: tokenEvent.id, marker: 'created' as EventMarker}],
});
}
} catch (error) {
showToast({
title: 'Payment sent.',
type: 'success',
});
setProofs([...remainingProofs, ...proofsToKeep]);
setProofsStorage([...remainingProofs, ...proofsToKeep]);
const newInvoice: ICashuInvoice = {
amount: -(meltQuote.amount + meltQuote.fee_reserve),
bolt11: pInvoice,
quote: meltQuote.quote,
date: Date.now(),
state: MintQuoteState.PAID,
direction: 'out',
};
setTransactions([...transactions, newInvoice]);
return meltResponse;
} else {
return undefined;
}
} else {
} catch (error) {
return undefined;
}
} else {
Expand All @@ -116,12 +104,7 @@ export const usePayment = () => {
}

if (proofs) {
const proofsSpent = await wallet.checkProofsSpent(proofs);
let proofsCopy = Array.from(proofs);

if (proofsSpent) {
proofsCopy = proofsCopy?.filter((p) => !proofsSpent?.includes(p));
}
const proofsCopy = Array.from(proofs);

const availableAmount = proofsCopy.reduce((s, t) => (s += t.amount), 0);

Expand All @@ -142,10 +125,7 @@ export const usePayment = () => {
}
}

const {returnChange: proofsToKeep, send: proofsToSend} = await wallet.send(
amount,
selectedProofs,
);
const {keep: proofsToKeep, send: proofsToSend} = await wallet.send(amount, selectedProofs);

if (proofsToKeep && proofsToSend) {
if (privateKey && publicKey) {
Expand Down Expand Up @@ -175,7 +155,8 @@ export const usePayment = () => {
setProofsStorage([...remainingProofs, ...proofsToKeep]);

const token = {
token: [{proofs: proofsToSend, mint: activeMint}],
mint: activeMint,
proofs: proofsToSend,
activeUnit,
} as Token;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import '../../../../../applyGlobalPolyfills';

import {MintQuoteResponse, MintQuoteState, Proof} from '@cashu/cashu-ts';
import {MintQuoteResponse, MintQuoteState} from '@cashu/cashu-ts';
import {ICashuInvoice, useAuth, useCreateSpendingEvent, useCreateTokenEvent} from 'afk_nostr_sdk';
import * as Clipboard from 'expo-clipboard';
import React, {useState} from 'react';
Expand Down Expand Up @@ -126,7 +126,7 @@ export const Invoices = () => {
const tokenEvent = await createTokenEvent({
walletId,
mint: activeMint,
proofs: receive?.proofs,
proofs: receive,
});
await createSpendingEvent({
walletId,
Expand All @@ -137,11 +137,11 @@ export const Invoices = () => {
});
}
if (!proofsStorage && !proofs) {
setProofsStorage([...(receive?.proofs as Proof[])]);
setProofs([...(receive?.proofs as Proof[])]);
setProofsStorage([...receive]);
setProofs([...receive]);
} else {
setProofsStorage([...proofs, ...(receive?.proofs as Proof[])]);
setProofs([...proofs, ...(receive?.proofs as Proof[])]);
setProofsStorage([...proofs, ...receive]);
setProofs([...proofs, ...receive]);
}
return receive;
}
Expand Down
2 changes: 1 addition & 1 deletion apps/mobile/src/modules/CashuWallet/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default ThemedStyleSheet((theme) => ({
padding: Spacing.medium,
borderRadius: 10,
flex: 1,
backgroundColor: 'transparent',
backgroundColor: theme.colors.background,
color: theme.colors.text,
},
modalBackdrop: {
Expand Down
7 changes: 1 addition & 6 deletions apps/mobile/src/screens/Cashu/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import React from 'react';
import {ScrollView} from 'react-native';

import {CashuView} from '../../modules/CashuWallet';
import {CashuWalletScreenProps} from '../../types';
export const CashuScreen: React.FC<CashuWalletScreenProps> = () => {
return (
<ScrollView showsHorizontalScrollIndicator={false} showsVerticalScrollIndicator={false}>
<CashuView></CashuView>
</ScrollView>
);
return <CashuView></CashuView>;
};
10 changes: 5 additions & 5 deletions apps/mobile/src/screens/ReceiveEcash/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const ReceiveEcash: React.FC<ReceiveEcashScreenProps> = ({navigation, rou
const {publicKey, privateKey} = useAuth();

const {handleReceiveEcash} = usePayment();
const {mints, setMints, setActiveMint, buildMintData, setActiveUnit, wallet} = useCashuContext()!;
const {mints, setMints, setActiveMint, buildMintData, setActiveUnit} = useCashuContext()!;
const {value: mintsStorage, setValue: setMintsStorage} = useMintStorage();
const {setValue: setActiveMintStorage} = useActiveMintStorage();
const {setValue: setActiveUnitStorage} = useActiveUnitStorage();
Expand All @@ -53,7 +53,7 @@ export const ReceiveEcash: React.FC<ReceiveEcashScreenProps> = ({navigation, rou
const decodedToken = getDecodedToken(token);
setTokenInfo(decodedToken);
if (decodedToken && mintsStorage.length === 0) {
const mintUrl = decodedToken.token[0].mint;
const mintUrl = decodedToken.mint;
handleAddMint(mintUrl);
}
}
Expand Down Expand Up @@ -90,7 +90,7 @@ export const ReceiveEcash: React.FC<ReceiveEcashScreenProps> = ({navigation, rou

const handleReceive = async () => {
setIsProcessing(true);
const mintUrl = tokenInfo?.token?.[0].mint;
const mintUrl = tokenInfo?.mint;
if (mintUrl) {
const mintAlreadyConfigured = mintsStorage?.some((mint) => mint.url === mintUrl) || false;
if (!mintAlreadyConfigured) {
Expand Down Expand Up @@ -141,8 +141,8 @@ export const ReceiveEcash: React.FC<ReceiveEcashScreenProps> = ({navigation, rou
<View style={styles.warningContainer}>
<InfoIcon width={30} height={30} color={theme.colors.primary} />
<Text style={styles.warning}>
This will connect the mint <b>{tokenInfo?.token?.[0].mint}</b> to your session if
it&apos;s not already configured.
This will connect the mint <b>{tokenInfo?.mint}</b> to your session if it&apos;s not
already configured.
</Text>
</View>
<Button
Expand Down
3 changes: 2 additions & 1 deletion packages/afk_nostr_sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"check:all-strict": "yarn check && yarn format:check && yarn lint:strict && yarn ts:check"
},
"dependencies": {
"@cashu/cashu-ts": "^1.2.1",
"@cashu/cashu-ts": "^2.0.0",
"@getalby/lightning-tools": "^5.0.3",
"@getalby/sdk": "^3.6.1",
"@noble/ciphers": "0.4.1",
Expand All @@ -24,6 +24,7 @@
"@nostr-dev-kit/ndk": "^2.10.1",
"@nostr-dev-kit/ndk-wallet": "^0.2.0",
"@scure/base": "1.1.5",
"@scure/bip39": "^1.5.0",
"@tanstack/react-query": "^5.40.0",
"@webbtc/webln-types": "^2.1.0",
"react": "^18.3.1",
Expand Down
Loading

0 comments on commit cf64a1e

Please sign in to comment.