From ff8cc967d0ac56e16d551b4d9b56d1ca6a925b3f Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Sun, 11 Aug 2024 22:01:43 +0300 Subject: [PATCH] toggle on and off and save tokens --- src/components/SettingsView.vue | 14 ++++++- src/pages/WalletPage.vue | 21 ++-------- src/stores/npubcash.ts | 74 +++++++++++++++++++++++++++++++-- 3 files changed, 87 insertions(+), 22 deletions(-) diff --git a/src/components/SettingsView.vue b/src/components/SettingsView.vue index d3138a0..47c80a5 100644 --- a/src/components/SettingsView.vue +++ b/src/components/SettingsView.vue @@ -244,7 +244,10 @@ Lightning address - This is your lightning address. + This is your lightning address. Enable it to check for incoming + payments at startup. @@ -266,6 +269,14 @@ + +
+ +
@@ -698,6 +709,7 @@ export default defineComponent({ ...mapState(useNPCStore, ["npcAddress"]), ...mapState(useNostrStore, ["pubkey", "mintRecommendations"]), ...mapState(useWalletStore, ["mnemonic"]), + ...mapWritableState(useNPCStore, ["npcEnabled"]), ...mapWritableState(useWalletStore, ["keysetCounters"]), ...mapWritableState(useMintsStore, [ "addMintData", diff --git a/src/pages/WalletPage.vue b/src/pages/WalletPage.vue index 06d15b5..00d343a 100644 --- a/src/pages/WalletPage.vue +++ b/src/pages/WalletPage.vue @@ -355,11 +355,7 @@ export default { ]), ...mapActions(useCameraStore, ["closeCamera", "showCamera"]), ...mapActions(useNWCStore, ["listenToNWCCommands"]), - ...mapActions(useNPCStore, [ - "generateNPCConnection", - "getBalance", - "getClaim", - ]), + ...mapActions(useNPCStore, ["generateNPCConnection", "claimAllTokens"]), // TOKEN METHODS decodeToken: function (encoded_token) { try { @@ -642,19 +638,10 @@ export default { if (this.nwcEnabled) { this.listenToNWCCommands(); } + + // generate NPC connection this.generateNPCConnection(); - const npubCashBalance = await this.getBalance(); - console.log("npub.cash balance: " + npubCashBalance); - if (npubCashBalance > 0) { - this.$q.notify({ - message: `You have ${npubCashBalance} sats on npub.cash`, - color: "positive", - position: "top", - timeout: 5000, - }); - this.showReceiveTokens = true; - this.receiveData.tokensBase64 = await this.getClaim(); - } + this.claimAllTokens(); }, }; diff --git a/src/stores/npubcash.ts b/src/stores/npubcash.ts index 2ce7e32..7cc170a 100644 --- a/src/stores/npubcash.ts +++ b/src/stores/npubcash.ts @@ -4,6 +4,9 @@ import { useLocalStorage } from "@vueuse/core"; import { bytesToHex } from '@noble/hashes/utils' // already an installed dependency import { generateSecretKey, getPublicKey } from 'nostr-tools' import { nip19 } from 'nostr-tools' +import { useWalletStore } from "./wallet"; +import { useReceiveTokensStore } from "./receiveTokensStore"; +import { notifyApiError, notifyError, notifySuccess, notifyWarning, notify } from "src/js/notify"; type NPCConnection = { walletPublicKey: string, @@ -21,6 +24,21 @@ type NPCClaim = { token: string, } } +type NPCWithdrawl = { + id: number; + claim_ids: number[]; + created_at: number; + pubkey: string; + amount: number; +} + +type NPCWithdrawals = { + error: string, + data: { + count: number, + withdrawals: Array + } +} const NIP98Kind = 27235; @@ -31,19 +49,46 @@ export const useNPCStore = defineStore("npc", { npcAddress: useLocalStorage("cashu.npc.address", ""), npcDomain: useLocalStorage("cashu.npc.domain", "npub.cash"), baseURL: useLocalStorage("cashu.npc.baseURL", "https://npub.cash"), + tokensToClaim: useLocalStorage("cashu.npc.tokensToClaim", []), + tokensClaimed: useLocalStorage("cashu.npc.tokensClaimed", []), ndk: new NDK(), signer: {} as NDKPrivateKeySigner, }), getters: { }, actions: { + claimAllTokens: async function () { + if (!this.npcEnabled) { + return + } + const receiveStore = useReceiveTokensStore(); + const npubCashBalance = await this.getBalance(); + console.log("npub.cash balance: " + npubCashBalance); + if (npubCashBalance > 0) { + notifySuccess(`You have ${npubCashBalance} sats on npub.cash`); + receiveStore.showReceiveTokens = true; + const token = await this.getClaim(); + if (token) { + this.storeTokenToClaim(token) + receiveStore.receiveData.tokensBase64 = token; + } + } + }, + storeTokenToClaim: function (token: string) { + this.tokensToClaim = this.tokensToClaim.concat(token) + }, generateNPCConnection: async function () { let conn: NPCConnection // NOTE: we only support one connection for now if (!this.npcConnections.length) { - const sk = generateSecretKey() // `sk` is a Uint8Array + const walletStore = useWalletStore(); + const sk = walletStore.seed.slice(0, 32) const walletPublicKeyHex = getPublicKey(sk) // `pk` is a hex string const walletPrivateKeyHex = bytesToHex(sk) + // print nsec and npub + console.log('Lightning address for wallet:', nip19.npubEncode(walletPublicKeyHex) + '@npub.cash') + // console.log('nsec:', nip19.nsecEncode(sk)) + console.log('npub:', nip19.npubEncode(walletPublicKeyHex)) conn = { walletPublicKey: walletPublicKeyHex, walletPrivateKey: walletPrivateKeyHex, @@ -88,7 +133,6 @@ export const useNPCStore = defineStore("npc", { `${this.baseURL}/api/v1/balance`, "GET" ); - console.log("Npub cash " + authHeader); try { const response = await fetch(`${this.baseURL}/api/v1/balance`, { method: "GET", @@ -112,7 +156,6 @@ export const useNPCStore = defineStore("npc", { `${this.baseURL}/api/v1/claim`, "GET" ); - console.log("Npub cash " + authHeader); try { const response = await fetch(`${this.baseURL}/api/v1/claim`, { method: "GET", @@ -130,6 +173,29 @@ export const useNPCStore = defineStore("npc", { console.error(e) return "" } - } + }, + // getWithdraw: async function (): Promise { + // const authHeader = await this.generateNip98Event( + // `${this.baseURL}/api/v1/withdrawals`, + // "POST", + // ); + // try { + // const response = await fetch(`${this.baseURL}/api/v1/withdraw`, { + // method: "GET", + // headers: { + // Authorization: `Nostr ${authHeader}`, + // }, + // }) + // // deserialize the response to NPCClaim + // const claim: NPCClaim = await response.json() + // if (claim.error) { + // return "" + // } + // return claim.data.token + // } catch (e) { + // console.error(e) + // return "" + // } + // } } });