Skip to content

Commit

Permalink
Merge pull request #94 from gandlafbtc/p2pk
Browse files Browse the repository at this point in the history
P2pk
  • Loading branch information
gandlafbtc authored Apr 3, 2024
2 parents 90648f8 + 786ecee commit 7240c84
Show file tree
Hide file tree
Showing 17 changed files with 417 additions and 151 deletions.
55 changes: 18 additions & 37 deletions package-lock.json

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

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@
},
"type": "module",
"dependencies": {
"@gandlaf21/bolt11-decode": "^3.1.1",
"@cashu/cashu-ts": "1.0.0-rc.1",
"@cashu/cashu-ts": "1.0.0-rc.2",
"@gandlaf21/bc-ur": "^1.1.12",
"@gandlaf21/bolt11-decode": "^3.1.1",
"@gandlaf21/cashu-crypto": "^0.2.6",
"nostr-relaypool": "^0.6.30",
"svelte-qrcode-image": "^1.0.0-rc.2",
"ws": "^8.16.0"
Expand Down
49 changes: 40 additions & 9 deletions src/actions/walletActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
type MintKeys,
type Proof,
type MeltQuoteResponse,
type TokenEntry
type TokenEntry,
getDecodedToken
} from '@cashu/cashu-ts';
import {
encodeBase64toUint8,
Expand All @@ -30,20 +31,25 @@ import { iv, key, seedIv } from '../stores/key';
import { randomBytes } from '@noble/hashes/utils';
import { isEncrypted } from '../stores/settings';
import { decode } from '@gandlaf21/bolt11-decode';
import { nostrPrivKey } from '../stores/nostr';

export const send = async (
mint: Mint,
amount: number,
proofsToSend: Proof[],
memo?: string,
preference?: AmountPreference[]
preference?: AmountPreference[],
pubkey?: string
) => {
const { count, keysetId, seedPhrase, wallet } = getWalletStuff(mint);
const { returnChange, send } = await wallet.send(
amount,
proofsToSend,
preference,
count
{
preference,
counter: count,
pubkey
}
);

if (seedPhrase) {
Expand Down Expand Up @@ -102,7 +108,7 @@ export const mint = async (
preference?: AmountPreference[]
) => {
const { count, keysetId, seedPhrase, wallet } = getWalletStuff(mint);
const { proofs } = await wallet.mintTokens(amount, quote, keysetId, preference, count);
const { proofs } = await wallet.mintTokens(amount, quote, {AmountPreference: preference, counter:count, keysetId});
if (seedPhrase) {
updateCount(keysetId, (count ?? 1) + proofs.length);
}
Expand All @@ -124,6 +130,30 @@ export const mint = async (
return { proofs };
};

export const receiveOffline = (encodedToken:string)=> {

const tokn = getDecodedToken(encodedToken)

const proofs = tokn.token.map((t) => t.proofs)
.flat();

token.update((ctx) => [...proofs, ...ctx]);
history.update((state) => [
{
type: HistoryItemType.RECEIVE_OFFLINE,
amount: getAmountForTokenSet(proofs),
date: Date.now(),
data: {
encodedToken: get(isEncrypted) ? '' : encodedToken,
mint: tokn.token[0].mint ?? '',
keyset: getKeysetsOfTokens(proofs),
receivedTokens: get(isEncrypted) ? [] : proofs
}
},
...state
]);
}

export const receive = async (
mint: Mint,
encodedToken: string,
Expand All @@ -133,7 +163,7 @@ export const receive = async (
const {
token: tokens,
tokensWithErrors,
} = await wallet.receive(encodedToken, preference, count);
} = await wallet.receive(encodedToken, {preference, counter:count, privkey:get(nostrPrivKey)});

const proofs = tokens.token.map((t: TokenEntry) => t.proofs).flat();

Expand Down Expand Up @@ -195,8 +225,9 @@ export const melt = async (
const { returnChange, send } = await wallet.send(
meltQuote.amount + meltQuote.fee_reserve,
proofs,
undefined,
currentCount
{
counter:currentCount
}
);
if (seedPhrase) {
currentCount = updateCount(keysetId, (currentCount ?? 1) + returnChange.length + send.length);
Expand All @@ -217,7 +248,7 @@ export const melt = async (
isPaid,
preimage,
change
} = await wallet.payLnInvoice(invoice, send, meltQuote, undefined, currentCount);
} = await wallet.payLnInvoice(invoice, send, meltQuote, {counter:currentCount});

if (seedPhrase) {
currentCount = updateCount(keysetId, (currentCount ?? 1) + change.length);
Expand Down
1 change: 0 additions & 1 deletion src/comp/Toast.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@
</span>
</div>
</div>

</div>

<div class="w-full bg-neutral-content h-1 bg-opacity-50">
Expand Down
6 changes: 5 additions & 1 deletion src/comp/base/Base.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import Setting from '../../comp/Setting.svelte';
import Wallet from '../../comp/wallet/Wallet.svelte';
import Logo from '../../comp/elements/Logo.svelte';
import { nostrMessages } from '../../stores/nostr';
import { createNewNostrKeys, nostrMessages, nostrPrivKey, nostrPubKey } from '../../stores/nostr';
import Donate from '../../comp/elements/Donate.svelte';
import Code from '../../comp/elements/Code.svelte';
import { version } from '../../stores/version';
Expand All @@ -13,13 +13,17 @@
import { mnemonic, seed } from '../../stores/mnemonic';
import { deriveSeedFromMnemonic } from '@cashu/cashu-ts';
$activeTab = 'wallet';
const changeTab = (tabName: string) => {
$activeTab = tabName;
};
onMount(() => {
if (!$nostrPubKey && !$nostrPrivKey) {
createNewNostrKeys()
}
setTimeout(() => {
if ($mnemonic.length) {
seed.set(deriveSeedFromMnemonic($mnemonic.join(' ')));
Expand Down
17 changes: 6 additions & 11 deletions src/comp/elements/NostrReceiveQR.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,33 @@
import { QRCodeImage } from 'svelte-qrcode-image';
import { nostrPubKey, useExternalNostrKey, useNostr } from '../../stores/nostr';
const getEncodedNpub = async () => {
if (!$useNostr) {
return '';
}
if ($useExternalNostrKey) {
if (browser) {
const pubK = await window.nostr?.getPublicKey();
if (!pubK) {
return '';
}
return nip19.npubEncode(pubK);
return pubK;
}
return '';
} else {
if (!$nostrPubKey) {
return '';
}
return Promise.resolve(nip19.npubEncode($nostrPubKey));
return Promise.resolve($nostrPubKey);
}
};
</script>

{#await getEncodedNpub() then npub}
{#if $useNostr && npub}
<div class="divider">nostr</div>
<!-- content here -->
{#if npub}
<div class="flex items-center justify-center w-full">
<div class="flex flex-col gap-2 items-center">
<div class="flex items-center justify-center flex-col">
<div class="text-sm bg-info text-info-content rounded-t-lg p-1 px-2">
Nostr - receive to npub
<div class="text-sm bg-accent text-accent-content rounded-t-lg p-1 px-2">
PubKey
</div>
<div class="border-info border-2 rounded-md p-2">
<div class="border-accent border-2 rounded-md p-2">
<QRCodeImage text={npub} displayHeight={250} displayWidth={250} margin={2} />
</div>
</div>
Expand Down
23 changes: 12 additions & 11 deletions src/comp/elements/NostrSettings.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { nostrPrivKey, useNostr, nostrPubKey, useExternalNostrKey } from '../../stores/nostr';
import { nostrPrivKey, useNostr, nostrPubKey, useExternalNostrKey, createNewNostrKeys } from '../../stores/nostr';
import { generateSecretKey, getPublicKey } from 'nostr-tools';
import { browser } from '$app/environment';
import { toast } from '../../stores/toasts';
Expand All @@ -10,13 +10,14 @@
let isShowNsec = false;
const generateNostrPrivKey = () => {
const priv = generateSecretKey();
nostrPrivKey.set(bytesToHex(priv));
nostrPubKey.set(getPublicKey(priv));
createNewNostrKeys()
restartNostr();
};
const restartNostr = () => {
if (!$useNostr) {
return
}
if (!$useExternalNostrKey && !$nostrPubKey) {
return;
}
Expand Down Expand Up @@ -84,12 +85,14 @@
</div>
</div>

<NostrRelaysConfig />
{/if}
{#if !$useExternalNostrKey}
<div>Keys</div>
<div class="bg-base-200 p-2 gap-2 rounded-md flex flex-col">
<div class="flex justify-between items-center">
<div class="">
<label for="npub">Npub</label>
<label for="npub">PubKey</label>
</div>

<div class="flex gap-2">
Expand Down Expand Up @@ -120,7 +123,7 @@
</div>
<div class="flex justify-between items-center">
<div class="flex flex-grow">
<label> Show nsec </label>
<label> PrivKey </label>
</div>
<div class="flex gap-2">
<input
Expand Down Expand Up @@ -201,14 +204,14 @@
</div>
<dialog bind:this={newKeysModal} class="modal">
<div class="modal-box">
<h3 class="font-bold text-lg">This will remove the current nostr keys!</h3>
<h3 class="font-bold text-lg text-error">This will remove your current keys!</h3>
<p class="py-4">
Are you sure you want to delete your current nostr keys and create new ones?
Are you sure you want to delete your current and create new ones? Ecash locked to this key pair might become un-spendable. Make sure your wallet contains no locked ecash before proceeding
</p>
<div class="modal-action">
<form method="dialog">
<button class="btn">abort</button>
<button class="btn btn-primary" on:click={generateNostrPrivKey}
<button class="btn btn-error" on:click={generateNostrPrivKey}
>Create new keys</button
>
</form>
Expand All @@ -218,5 +221,3 @@
</div>
</div>
{/if}
<NostrRelaysConfig />
{/if}
7 changes: 6 additions & 1 deletion src/comp/history/HistoryIcon.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
d="M3 16.5v2.25A2.25 2.25 0 005.25 21h13.5A2.25 2.25 0 0021 18.75V16.5m-13.5-9L12 3m0 0l4.5 4.5M12 3v13.5"
/>
</svg>
{:else if type === HistoryItemType.RECEIVE}
{:else if type === HistoryItemType.RECEIVE}
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
Expand All @@ -34,6 +34,11 @@
d="M3 16.5v2.25A2.25 2.25 0 005.25 21h13.5A2.25 2.25 0 0021 18.75V16.5M16.5 12L12 16.5m0 0L7.5 12m4.5 4.5V3"
/>
</svg>
{:else if type === HistoryItemType.RECEIVE_OFFLINE}
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="text-info w-4 h-4">
<path stroke-linecap="round" stroke-linejoin="round" d="M12 6v6h4.5m4.5 0a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z" />
</svg>

{:else if type === HistoryItemType.MELT}
<svg
xmlns="http://www.w3.org/2000/svg"
Expand Down
Loading

0 comments on commit 7240c84

Please sign in to comment.