Skip to content

Commit

Permalink
payment requests with seed phrase
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Nov 21, 2024
1 parent 9d7dc78 commit 3338075
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 126 deletions.
76 changes: 7 additions & 69 deletions src/components/ReceiveTokenDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -208,64 +208,12 @@ export default defineComponent({
"showLastKey",
]),
...mapActions(useMintsStore, ["addMint"]),
knowThisMintOfTokenJson: function (tokenJson) {
const mintStore = useMintsStore();
return mintStore.mints
.map((m) => m.url)
.includes(token.getMint(tokenJson));
},
receiveToken: async function (encodedToken) {
const mintStore = useMintsStore();
const receiveStore = useReceiveTokensStore();
const uIStore = useUiStore();
receiveStore.showReceiveTokens = false;
console.log("### receive tokens", receiveStore.receiveData.tokensBase64);
if (receiveStore.receiveData.tokensBase64.length == 0) {
throw new Error("no tokens provided.");
}
// get the private key for the token we want to receive if it is locked with P2PK
receiveStore.receiveData.p2pkPrivateKey =
this.getPrivateKeyForP2PKEncodedToken(
receiveStore.receiveData.tokensBase64
);
const tokenJson = token.decode(receiveStore.receiveData.tokensBase64);
if (tokenJson == undefined) {
throw new Error("no tokens provided.");
}
// check if we have all mints
if (!this.knowThisMintOfTokenJson(tokenJson)) {
// // pop up add mint dialog warning
// // hack! The "add mint" component is in SettingsView which may now
// // have been loaded yet. We switch the tab to settings to make sure
// // that it loads. Remove this code when the TrustMintComnent is refactored!
// uIStore.setTab("mints");
// // hide the receive dialog
// receiveStore.showReceiveTokens = false;
// // set the mint to add
// this.addMintData = { url: token.getMint(tokenJson) };
// // show the add mint dialog
// this.showAddMintDialog = true;
// // show the token receive dialog again for the next attempt
// receiveStore.showReceiveTokens = true;
// return;
// add the mint
await this.addMint({ url: token.getMint(tokenJson) });
}
// redeem the token
await this.redeem(receiveStore.receiveData.tokensBase64);
},
...mapActions(useReceiveTokensStore, [
"receiveIfDecodes",
"decodeToken",
"knowThisMintOfTokenJson",
]),
// TOKEN METHODS
decodeToken: function (encoded_token) {
let decodedToken = undefined;
try {
decodedToken = token.decode(encoded_token);
} catch (error) {}
return decodedToken;
},
getProofs: function (decoded_token) {
return token.getProofs(decoded_token);
},
Expand All @@ -286,16 +234,6 @@ export default defineComponent({
prStore.newPaymentRequest();
}
},
receiveIfDecodes: function () {
try {
const decodedToken = this.decodeToken(this.receiveData.tokensBase64);
if (decodedToken) {
this.receiveToken(this.receiveData.tokensBase64);
}
} catch (error) {
console.error(error);
}
},
tokenAlreadyInHistory: function (tokenStr) {
const tokensStore = useTokensStore();
return (
Expand All @@ -310,7 +248,7 @@ export default defineComponent({
return;
}
const tokensStore = useTokensStore();
const decodedToken = this.decodeToken(token);
const decodedToken = tokensStore.decodeToken(token);
// get amount from decodedToken.token.proofs[..].amount
const amount = this.getProofs(decodedToken).reduce(
(sum, el) => (sum += el.amount),
Expand All @@ -323,7 +261,7 @@ export default defineComponent({
});
this.showReceiveTokens = false;
// show success notification
this.notifySuccess("Ecash added to history.");
this.notifySuccess("Incoming payment added to history.");
},
pasteToParseDialog: function () {
console.log("pasteToParseDialog");
Expand Down
70 changes: 53 additions & 17 deletions src/components/SendPaymentRequest.vue
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
<template>
<div
v-if="sendData.paymentRequest"
class="row text-left q-py-none q-my-none"
@click="clickPaymentRequest"
>
<q-btn rounded dense color="primary" class="q-px-md"
><q-icon name="send" class="q-pr-xs" /> Pay via
{{ getPaymentRequestTransportType(sendData.paymentRequest) }}
</q-btn>
<div v-if="sendData.paymentRequest" class="q-pa-none q-ma-none q-mt-md">
<div class="q-mb-md text-center">
<q-btn
rounded
dense
color="primary"
class="q-px-md"
@click="clickPaymentRequest"
>
<q-icon name="send" class="q-pr-xs" />
Pay via {{ getPaymentRequestTransportType(sendData.paymentRequest) }}
</q-btn>
</div>
<div class="q-mb-md text-center">
<pre
class="q-mt-md q-mb-md text-center"
style="
white-space: pre-wrap;
word-wrap: break-word;
max-width: 350px;
margin: 0 auto;
"
>{{ getPaymentRequestTarget(sendData.paymentRequest) }}
</pre>
</div>
</div>
</template>

<script>
import { defineComponent } from "vue";
import { mapActions, mapState, mapWritableState } from "pinia";
Expand All @@ -33,14 +50,7 @@ export default defineComponent({
"showLockInput",
"sendData",
]),
...mapState(useMintsStore, [
"activeMintUrl",
"activeProofs",
"mints",
"proofs",
"activeUnit",
"addMintBlocking",
]),
...mapState(useMintsStore, ["activeMintUrl", "mints"]),
},
methods: {
...mapActions(useP2PKStore, ["isLocked", "isLockedToUs"]),
Expand All @@ -66,6 +76,32 @@ export default defineComponent({
}
}
},
getPaymentRequestTarget: function (request) {
if (!request || !request.transport) {
return "Unknown";
}
console.log(`### getPaymentRequestDestination: ${request}`);
const transports = request.transport;
for (const transport of transports) {
if (transport.type == PaymentRequestTransportType.NOSTR) {
return `${transport.target.slice(0, 20)}..${transport.target.slice(
-10
)}`;
}
if (transport.type == PaymentRequestTransportType.POST) {
try {
const url = new URL(transport.target);
return url.hostname;
} catch (error) {
console.error(
`Invalid URL in transport.target: ${transport.target}`,
error
);
return "Invalid URL";
}
}
}
},
},
});
</script>
24 changes: 20 additions & 4 deletions src/components/SettingsView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,16 @@
</div>

<!-- payment requests -->
<!-- NOTE: temporarily disabled the button until fix -->
<div class="q-py-sm q-px-xs text-left" on-left>
<q-item class="q-pt-lg">
<q-item-section>
<q-item-label overline class="text-weight-bold"
>Payment requests</q-item-label
>
<q-item-label caption
>Payment requests allow you to receive payments via
nostr.</q-item-label
>Payment requests allow you to receive payments via nostr. If you
enable this, your wallet will subscribe to your nostr
relays.</q-item-label
>
</q-item-section>
</q-item>
Expand All @@ -238,6 +238,19 @@
/>
</q-item>
</div>
<div v-if="enablePaymentRequest" class="q-pb-sm q-px-xl text-left" on-left>
<q-item>
<q-toggle
v-model="receivePaymentRequestsAutomatically"
label="Receive automatically"
color="primary"
/> </q-item
><q-item class="q-pt-none">
<q-item-label caption
>If enabled, the wallet will automatically receive incoming payments.
</q-item-label>
</q-item>
</div>

<!-- ln address -->
<div class="q-py-sm q-px-xs text-left" on-left>
Expand Down Expand Up @@ -1083,7 +1096,10 @@ export default defineComponent({
"showRemoveMintDialog",
]),
...mapWritableState(useNWCStore, ["nwcEnabled", "connections", "relays"]),
...mapWritableState(usePRStore, ["enablePaymentRequest"]),
...mapWritableState(usePRStore, [
"enablePaymentRequest",
"receivePaymentRequestsAutomatically",
]),
keysetCountersByMint() {
const mints = this.mints;
const keysetCountersByMint = {}; // {mintUrl: [keysetCounter: {id: string, count: number}, ...]}
Expand Down
8 changes: 8 additions & 0 deletions src/stores/mints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,14 @@ export const useMintsStore = defineStore("mints", {
units[(units.indexOf(this.activeUnit) + 1) % units.length];
return this.activeUnit;
},
selectUnit: function (unit: string) {
const units = this.activeMint().units;
if (units.includes(unit)) {
this.activeUnit = unit;
} else {
notifyError(`Unit ${unit} not supported by mint`, "Unit selection failed");
}
},
proofsToWalletProofs(proofs: Proof[], quote?: string): WalletProof[] {
return proofs.map((p) => {
return {
Expand Down
Loading

0 comments on commit 3338075

Please sign in to comment.