Skip to content

Commit

Permalink
Merge pull request #67 from gandlafbtc/tokenV3
Browse files Browse the repository at this point in the history
Token v3
  • Loading branch information
gandlafbtc authored Apr 2, 2023
2 parents b68d07c + f318b56 commit fb6cafa
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 46 deletions.
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "svelte-cashu-wallet",
"version": "0.1.7",
"version": "0.1.8",
"private": true,
"scripts": {
"dev": "vite dev",
Expand All @@ -14,7 +14,7 @@
"format": "prettier --plugin-search-dir . --write ."
},
"devDependencies": {
"@cashu/cashu-ts": "^0.5.2",
"@cashu/cashu-ts": "^0.6.1",
"@sveltejs/adapter-auto": "^1.0.1",
"@sveltejs/kit": "^1.0.0",
"@typescript-eslint/eslint-plugin": "^5.45.0",
Expand Down
9 changes: 5 additions & 4 deletions src/comp/plugin/NostrSocket.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script lang="ts">
import { getDecodedProofs } from '@cashu/cashu-ts';
import { getDecodedToken } from '@cashu/cashu-ts';
import * as rp from 'nostr-relaypool';
import * as nostrTools from 'nostr-tools';
Expand Down Expand Up @@ -97,18 +98,18 @@
let token;
try {
token = getDecodedProofs(decodedMessage);
token = getDecodedToken(decodedMessage);
} catch (e) {
console.error(e, 'could not decode nip-04 message as token. Ignoring this message');
return;
}
if (!token?.proofs) {
if (!token?.token) {
//if the event is not in a cashu token format, ignore it
return;
}
if (!isValidToken(token.proofs)) {
if (!isValidToken(token.token)) {
// ignore messages that are not tokens
return;
}
Expand Down
23 changes: 13 additions & 10 deletions src/comp/tokens/InboxRow.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import { browser } from '$app/environment';
import { CashuMint, CashuWallet, getEncodedProofs } from '@cashu/cashu-ts';
import { CashuMint, CashuWallet, getEncodedToken } from '@cashu/cashu-ts';
import { mints } from '../../stores/mints';
import type { NostrMessage } from '../../model/nostrMessage';
import { nostrMessages } from '../../stores/nostr';
Expand Down Expand Up @@ -32,7 +32,7 @@
let isLoadingMint = false;
onMount(() => {
hasMint = $mints.map((m) => m.mintURL).includes(nostrMessage.token.mints[0].url);
hasMint = $mints.map((m) => m.mintURL).includes(nostrMessage.token.token[0].mint);
});
const addContact = () => {
Expand All @@ -45,7 +45,7 @@
};
const addMint = async () => {
const mint = new CashuMint(nostrMessage.token.mints[0].url);
const mint = new CashuMint(nostrMessage.token.token[0].mint);
try {
if ($mints.filter((m) => m.mintURL === mint.mintUrl).length > 0) {
toast('warning', 'this mint has already been added.', "Didn't add mint!");
Expand Down Expand Up @@ -84,7 +84,7 @@
const acceptToken = async () => {
try {
const mint = getMintForToken(nostrMessage.token.proofs[0], $mints);
const mint = getMintForToken(nostrMessage.token.token[0].proofs[0], $mints);
if (!mint) {
toast('warning', 'This token is from an unknown mint.', 'Token could not be added');
return;
Expand All @@ -100,7 +100,10 @@
const cashuMint: CashuMint = new CashuMint(mint.mintURL);
const cashuWallet: CashuWallet = new CashuWallet(mint.keys, cashuMint);
const encodedProofs = getEncodedProofs(nostrMessage.token.proofs, nostrMessage.token.mints);
const encodedProofs = getEncodedToken(
nostrMessage.token.token[0].proofs,
nostrMessage.token.token[0].mint
);
isLoading = true;
const newTokens: Array<Token> = await cashuWallet.receive(encodedProofs);
Expand All @@ -118,7 +121,7 @@
history.update((state) => [
{
type: HistoryItemType.RECEIVE_NOSTR,
amount: getAmountForTokenSet(nostrMessage.token.proofs),
amount: getAmountForTokenSet(nostrMessage.token.token[0].proofs),
date: Date.now(),
data: {
encodedToken: encodedProofs,
Expand Down Expand Up @@ -203,7 +206,7 @@
</div>
{/if}
</td>
<td>{getAmountForTokenSet(nostrMessage?.token?.proofs)}</td>
<td>{getAmountForTokenSet(nostrMessage?.token?.token[0].proofs)}</td>
<td>
<p class="hidden lg:flex">
{date.toLocaleString('en-us', {
Expand Down Expand Up @@ -235,11 +238,11 @@
<div class="grid grid-cols-5">
<p class="font-bold">Amount:</p>
<p class="col-span-4">
{getAmountForTokenSet(nostrMessage?.token?.proofs) ?? ''}
{getAmountForTokenSet(nostrMessage?.token?.token[0]?.proofs) ?? ''}
</p>
<p class="font-bold">Mint:</p>
<p class="col-span-4">
{nostrMessage.token.mints[0].url}
{nostrMessage.token.token[0].mint}
</p>
<p class="font-bold">From:</p>
<div class="flex col-span-4 items-center gap-2 overflow-clip">
Expand Down Expand Up @@ -278,7 +281,7 @@
</div>
<p class="font-bold">Token:</p>
<p class="col-span-4 overflow-clip">
{getEncodedProofs(nostrMessage.token.proofs, nostrMessage.token.mints)}
{getEncodedToken(nostrMessage.token.token[0].proofs, nostrMessage.token.token[0].mint)}
</p>
</div>
</div>
Expand Down
14 changes: 8 additions & 6 deletions src/comp/tokens/InboxTable.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { CashuMint, CashuWallet, getEncodedProofs } from '@cashu/cashu-ts';
import { CashuMint, CashuWallet, getEncodedToken } from '@cashu/cashu-ts';
import type { Mint } from '../../model/mint';
import { mints } from '../../stores/mints';
import { nostrMessages } from '../../stores/nostr';
Expand All @@ -23,7 +23,7 @@
let hasError = 0;
isLoading = true;
for (const nM of $nostrMessages.filter((n) => !n.isAccepted)) {
const mint: CashuMint = new CashuMint(nM.token.mints[0].url);
const mint: CashuMint = new CashuMint(nM.token.token[0].mint);
let keys;
try {
if ($mints.map((m) => m.mintURL).includes(mint.mintUrl)) {
Expand All @@ -33,17 +33,19 @@
const storeMint: Mint = {
mintURL: mint.mintUrl,
keys,
keysets: nM.token.mints[0].ids,
keysets: [...new Set(nM.token.token[0].proofs.map((p) => p.id))],
isAdded: false
};
}
const wallet = new CashuWallet(keys, mint);
const spentProofs = await wallet.checkProofsSpent(nM.token.proofs);
const proofsToReceive = nM.token.proofs.filter((p) => !spentProofs.includes(p));
const spentProofs = await wallet.checkProofsSpent(nM.token.token[0].proofs);
const proofsToReceive = nM.token.token[0].proofs.filter((p) => !spentProofs.includes(p));
if (proofsToReceive.length > 0) {
const receivedProofs = await wallet.receive(getEncodedProofs(proofsToReceive));
const receivedProofs = await wallet.receive(
getEncodedToken(proofsToReceive, mint.mintUrl)
);
token.update((state) => [...receivedProofs, ...state]);
Expand Down
8 changes: 4 additions & 4 deletions src/comp/tokens/TokenHistoryRow.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { getEncodedProofs } from '@cashu/cashu-ts';
import { getEncodedToken } from '@cashu/cashu-ts';
import type { MeltData } from '../../model/data/MeltData';
import type { ReceiveNostrData } from '../../model/data/ReceiveNostrData';
import type { MintData } from '../../model/data/MintData';
Expand All @@ -15,7 +15,7 @@
if (historyItem.type === HistoryItemType.SEND) {
const sendData: SendData = historyItem.data;
token = getEncodedProofs(sendData.send ?? []);
token = getEncodedToken(sendData.send ?? []);
} else if (historyItem.type === HistoryItemType.RECEIVE) {
const recieveData: ReceiveData = historyItem.data;
token = recieveData.encodedToken ?? '';
Expand All @@ -24,10 +24,10 @@
token = recieveData.encodedToken ?? '';
} else if (historyItem.type === HistoryItemType.MINT) {
const mintData: MintData = historyItem.data;
token = getEncodedProofs(mintData.tokens ?? []);
token = getEncodedToken(mintData.tokens ?? []);
} else {
const meltData: MeltData = historyItem.data;
token = getEncodedProofs(meltData.change ?? []);
token = getEncodedToken(meltData.change ?? []);
}
</script>

Expand Down
6 changes: 3 additions & 3 deletions src/comp/tokens/TokenRow.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { CashuMint, CashuWallet, getEncodedProofs } from '@cashu/cashu-ts';
import { CashuMint, CashuWallet, getEncodedToken } from '@cashu/cashu-ts';
import { mints } from '../../stores/mints';
import type { Token } from '../../model/token';
import { getMintForToken, getTokenSubset } from '../util/walletUtils';
Expand Down Expand Up @@ -27,7 +27,7 @@
await checkTokenSpent();
try {
isLoading = true;
const encodedProofs = getEncodedProofs([token]);
const encodedProofs = getEncodedToken([token]);
const newTokens: Array<Token> = await cashuWallet.receive(encodedProofs);
//remove old token
tokenStore.update((state) => getTokenSubset(state, [token]));
Expand Down Expand Up @@ -146,7 +146,7 @@
</td>
<td class="max-w-0 overflow-clip">
<div class="overflow-x-clip">
{getEncodedProofs([token])}
{getEncodedToken([token])}
</div>
</td>
</tr>
Expand Down
9 changes: 5 additions & 4 deletions src/comp/wallet/Receiving.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { CashuMint, CashuWallet, getDecodedProofs } from '@cashu/cashu-ts';
import { CashuMint, CashuWallet, getDecodedToken } from '@cashu/cashu-ts';
import { toast } from '../../stores/toasts';
import { mints } from '../../stores/mints';
import LoadingCenter from '../LoadingCenter.svelte';
Expand Down Expand Up @@ -43,8 +43,7 @@
'Receive tokens from this mint by adding the mint',
'Not connected to this mint'
);
mintToAdd = getDecodedProofs(encodedToken).mints.filter((m) => m.ids.includes(mintId))[0]
.url;
mintToAdd = getDecodedToken(encodedToken).token[0].mint;
return;
}
const cashuMint: CashuMint = new CashuMint(mint.mintURL);
Expand Down Expand Up @@ -90,7 +89,9 @@
const validateToken = () => {
amount = 0;
try {
const { proofs, mints } = getDecodedProofs(encodedToken);
const { token } = getDecodedToken(encodedToken);
const proofs = token[0].proofs;
const mint = token[0].mint;
proofs.forEach((t) => {
mintId = t.id;
amount += t.amount;
Expand Down
4 changes: 2 additions & 2 deletions src/comp/wallet/Sending.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { CashuMint, CashuWallet, getDecodedProofs, getEncodedProofs } from '@cashu/cashu-ts';
import { CashuMint, CashuWallet, getDecodedToken, getEncodedToken } from '@cashu/cashu-ts';
import { toast } from '../../stores/toasts';
import type { Mint } from '../../model/mint';
import { mints } from '../../stores/mints';
Expand Down Expand Up @@ -76,7 +76,7 @@
//add newly minted tokens that have been returned as change
token.update((state) => [...state, ...returnChange]);
encodedToken = getEncodedProofs(send, [{ url: mint.mintURL, ids: mint.keysets }]);
encodedToken = getEncodedToken(send, mint.mintURL);
history.update((state) => [
{
type: HistoryItemType.SEND,
Expand Down
2 changes: 1 addition & 1 deletion src/model/nostrMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Token } from './token';
import type { Event } from 'nostr-tools';
interface NostrMessage {
event: Event;
token: { proofs: Array<Token>; mints: Array<{ url: string; ids: Array<string> }> };
token: { token: [{ proofs: Array<Token>; mint: string }]; memo?: string };
isAccepted: boolean;
}

Expand Down
2 changes: 1 addition & 1 deletion src/stores/version.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readable } from 'svelte/store';

const version = readable<string>('0.1.7');
const version = readable<string>('0.1.8');

export { version };

1 comment on commit fb6cafa

@vercel
Copy link

@vercel vercel bot commented on fb6cafa Apr 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.