Skip to content

Commit

Permalink
websockets for token sends
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Nov 20, 2024
1 parent 35c93cd commit 0784e3b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/components/SendTokenDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ export default defineComponent({
"coinSelect",
"spendableProofs",
"getFeesForProofs",
"onTokenPaid",
]),
...mapActions(useProofsStore, ["serializeProofs"]),
...mapActions(useTokensStore, [
Expand Down Expand Up @@ -714,7 +715,8 @@ export default defineComponent({
});
if (!this.g.offline) {
this.checkTokenSpendableWorker(this.sendData.tokensBase64);
//this.checkTokenSpendableWorker(this.sendData.tokensBase64);
this.onTokenPaid(this.sendData.tokensBase64);
}
} catch (error) {
console.error(error);
Expand Down
43 changes: 41 additions & 2 deletions src/stores/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { useWorkersStore } from "./workers";
import * as _ from "underscore";
import token from "src/js/token";
import { notifyApiError, notifyError, notifySuccess, notifyWarning, notify } from "src/js/notify";
import { CashuMint, CashuWallet, Proof, MintQuotePayload, MeltQuotePayload, MeltQuoteResponse, CheckStateEnum, MeltQuoteState, MintQuoteState, PaymentRequest, PaymentRequestTransportType, PaymentRequestTransport, decodePaymentRequest, MintQuoteResponse } from "@cashu/cashu-ts";
import { CashuMint, CashuWallet, Proof, MintQuotePayload, MeltQuotePayload, MeltQuoteResponse, CheckStateEnum, MeltQuoteState, MintQuoteState, PaymentRequest, PaymentRequestTransportType, PaymentRequestTransport, decodePaymentRequest, MintQuoteResponse, ProofState } from "@cashu/cashu-ts";
import { hashToCurve } from '@cashu/crypto/modules/common';
import * as bolt11Decoder from "light-bolt11-decoder";
import { bech32 } from "bech32";
Expand Down Expand Up @@ -723,6 +723,44 @@ export const useWalletStore = defineStore("wallet", {
throw error;
}
},
onTokenPaid: async function (tokenStr: string) {
const sendTokensStore = useSendTokensStore();
const tokenJson = token.decode(tokenStr);
const activeMint = useMintsStore().activeMint().mint;
const mintStore = useMintsStore();
if (!activeMint.info?.nuts[17]?.supported || !activeMint.info?.nuts[17]?.supported.find((s) => s.method == "bolt11" && s.unit == mintStore.activeUnit && s.commands.indexOf("proof_state") != -1)) {
console.log("Websockets not supported, kicking off token check worker.")
useWorkersStore().checkTokenSpendableWorker(tokenStr);
return;
}
try {
if (tokenJson == undefined) {
throw new Error("no tokens provided.");
}
const proofs = token.getProofs(tokenJson);
const oneProof = [proofs[0]];
const unsub = await this.wallet.onProofStateUpdates(oneProof,
async (proofState: ProofState) => {
console.log(`Websocket: proof state updated: ${proofState.state}`);
if (proofState.state == CheckStateEnum.SPENT) {
const tokenSpent = await this.checkTokenSpendable(tokenStr);
if (tokenSpent) {
sendTokensStore.showSendTokens = false;
unsub();
}
}
},
async (error: any) => {
console.error(error);
notifyApiError(error);
throw error;
}
);
} catch (error) {
console.error("Error in websocket subscription", error);
useWorkersStore().checkTokenSpendableWorker(tokenStr);
}
},
checkTokenSpendable: async function (tokenStr: string, verbose: boolean = true) {
/*
checks whether a base64-encoded token (from the history table) has been spent already.
Expand Down Expand Up @@ -797,13 +835,14 @@ export const useWalletStore = defineStore("wallet", {
throw new Error("invoice not found");
}
try {
await this.wallet.onMintQuotePaid(quote,
const unsub = await this.wallet.onMintQuotePaid(quote,
async (mintQuoteResponse: MintQuoteResponse) => {
console.log("Websocket: mint quote paid.")
const proofs = await this.mint(invoice.amount, quote, verbose);
uIStore.showInvoiceDetails = false;
if (!!window.navigator.vibrate) navigator.vibrate(200);
notifySuccess("Received " + invoice.amount + " via Lightning");
unsub();
return proofs;
},
async (error: any) => {
Expand Down

0 comments on commit 0784e3b

Please sign in to comment.