Skip to content

Commit

Permalink
toggle on and off and save tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Aug 11, 2024
1 parent 832e13f commit ff8cc96
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 22 deletions.
14 changes: 13 additions & 1 deletion src/components/SettingsView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,10 @@
<q-item>
<q-item-section>
<q-item-label overline>Lightning address</q-item-label>
<q-item-label caption>This is your lightning address.</q-item-label>
<q-item-label caption
>This is your lightning address. Enable it to check for incoming
payments at startup.</q-item-label
>
</q-item-section>
</q-item>
<q-item>
Expand All @@ -266,6 +269,14 @@
</q-input>
</div>
</div>
<!-- toggle to turn Lightning address on and off in new row -->
<div class="row q-pt-md">
<q-toggle
v-model="npcEnabled"
label="Enable Lightning address"
color="primary"
/>
</div>
</q-item-section>
</q-item>

Expand Down Expand Up @@ -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",
Expand Down
21 changes: 4 additions & 17 deletions src/pages/WalletPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
},
};
</script>
74 changes: 70 additions & 4 deletions src/stores/npubcash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<NPCWithdrawl>
}
}

const NIP98Kind = 27235;

Expand All @@ -31,19 +49,46 @@ export const useNPCStore = defineStore("npc", {
npcAddress: useLocalStorage<string>("cashu.npc.address", ""),
npcDomain: useLocalStorage<string>("cashu.npc.domain", "npub.cash"),
baseURL: useLocalStorage<string>("cashu.npc.baseURL", "https://npub.cash"),
tokensToClaim: useLocalStorage<string[]>("cashu.npc.tokensToClaim", []),
tokensClaimed: useLocalStorage<string[]>("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,
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -130,6 +173,29 @@ export const useNPCStore = defineStore("npc", {
console.error(e)
return ""
}
}
},
// getWithdraw: async function (): Promise<string> {
// 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 ""
// }
// }
}
});

0 comments on commit ff8cc96

Please sign in to comment.