Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Aug 14, 2024
1 parent 22a6126 commit 2e18ac3
Showing 1 changed file with 61 additions and 116 deletions.
177 changes: 61 additions & 116 deletions src/stores/npubcash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,100 +66,6 @@ export const useNPCStore = defineStore("npc", {
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`);
const token = await this.getClaim();
if (token) {
// add token to history first
this.addPendingTokenToHistory(token)
receiveStore.receiveData.tokensBase64 = token;
if (this.automaticClaim) {
try {
// redeem token automatically
const walletStore = useWalletStore()
await walletStore.redeem()
} catch {
// if it doesn't work, show the receive window
receiveStore.showReceiveTokens = true;
}
} else {
receiveStore.showReceiveTokens = true;
}

// this.storeUnclaimedProofs(token)
// const proofsToClaim = this.getUnclaimedProofs(token)
// const proofsStore = useProofsStore()
// const encodedToken = proofsStore.serializeProofs(proofsToClaim)
// receiveStore.receiveData.tokensBase64 = encodedToken;
// const walletStore = useWalletStore()
// try {
// // try to receive automatically
// await walletStore.redeem()
// } catch {
// // if it doesn't work, show the receive window
// receiveStore.showReceiveTokens = true;
// }
}
}
},
// storeUnclaimedProofs: function (encodedToken: string) {
// const proofs: WalletProof[] = token.getProofs(token.decode(encodedToken))
// const unclaimedProofs = proofs.filter((p) =>
// !this.unclaimedProofs.map((pu) => pu.secret).includes(p.secret)
// );
// this.unclaimedProofs = this.unclaimedProofs.concat(unclaimedProofs)
// },
// getUnclaimedProofs: function (encodedToken: string): WalletProof[] {
// // get all unclaimedProofs from the same mint as encodedToken
// const mintUrl = token.getMint(token.decode(encodedToken))
// const mintsStore = useMintsStore()
// const mint = mintsStore.mints.find((m) => m.url == mintUrl);
// if (mint == undefined) {
// return []
// }
// const mintKeysetIds = mint.keysets.map((k) => k.id)
// const unclaimedProofsFromSameMint = this.unclaimedProofs.filter((p) => mintKeysetIds.includes(p.id))
// console.log(`unclaimed: ${unclaimedProofsFromSameMint.length}`)
// return unclaimedProofsFromSameMint;
// },
tokenAlreadyInHistory: function (tokenStr: string) {
const tokensStore = useTokensStore();
return (
tokensStore.historyTokens.find((t) => t.token === tokenStr) !== undefined
);
},
addPendingTokenToHistory: function (tokenStr: string) {
const tokensStore = useTokensStore();
if (this.tokenAlreadyInHistory(tokenStr)) {
return;
}
const decodedToken = token.decode(tokenStr);
if (decodedToken == undefined) {
throw Error('could not decode token')
}
// get amount from decodedToken.token.proofs[..].amount
const amount = token.getProofs(decodedToken).reduce(
(sum, el) => (sum += el.amount),
0
);

const mintUrl = token.getMint(decodedToken)
const unit = token.getUnit(decodedToken)
tokensStore.addPendingToken({
amount: amount,
serializedProofs: tokenStr,
mint: mintUrl,
unit: unit
});
this.showReceiveTokens = false;
},
generateNPCConnection: async function () {
const nostrStore = useNostrStore()
if (!nostrStore.pubkey) {
Expand All @@ -181,32 +87,10 @@ export const useNPCStore = defineStore("npc", {
// log info
console.log(info)
if (info.username) {
notifySuccess(`You are now logged in as ${info.username}`)
this.npcAddress = info.username + '@' + this.npcDomain
} else {
this.npcAddress = nip19.npubEncode(walletPublicKeyHex) + '@' + this.npcDomain
}
// let conn: NPCConnection
// NOTE: we only support one connection for now
// if (!this.npcConnections.length) {
// 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,
// // } as NPCConnection;
// // this.npcConnections = this.npcConnections.concat(conn)
// this.npcAddress = nip19.npubEncode(this.npcConnections[0].walletPublicKey) + '@' + this.npcDomain

// }
// this.signer = new NDKPrivateKeySigner(this.npcConnections[0].walletPrivateKey)
// this.ndk = new NDK({ explicitRelayUrls: this.relays, signer: this.signer });
},
generateNip98Event: async function (url: string, method: string, body: string): Promise<string> {
const nostrStore = useNostrStore()
Expand Down Expand Up @@ -244,6 +128,67 @@ export const useNPCStore = defineStore("npc", {
}
}
},
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`);
const token = await this.getClaim();
if (token) {
// add token to history first
this.addPendingTokenToHistory(token)
receiveStore.receiveData.tokensBase64 = token;
if (this.automaticClaim) {
try {
// redeem token automatically
const walletStore = useWalletStore()
await walletStore.redeem()
} catch {
// if it doesn't work, show the receive window
receiveStore.showReceiveTokens = true;
}
} else {
receiveStore.showReceiveTokens = true;
}
}
}
},
tokenAlreadyInHistory: function (tokenStr: string) {
const tokensStore = useTokensStore();
return (
tokensStore.historyTokens.find((t) => t.token === tokenStr) !== undefined
);
},
addPendingTokenToHistory: function (tokenStr: string) {
const tokensStore = useTokensStore();
if (this.tokenAlreadyInHistory(tokenStr)) {
return;
}
const decodedToken = token.decode(tokenStr);
if (decodedToken == undefined) {
throw Error('could not decode token')
}
// get amount from decodedToken.token.proofs[..].amount
const amount = token.getProofs(decodedToken).reduce(
(sum, el) => (sum += el.amount),
0
);

const mintUrl = token.getMint(decodedToken)
const unit = token.getUnit(decodedToken)
tokensStore.addPendingToken({
amount: amount,
serializedProofs: tokenStr,
mint: mintUrl,
unit: unit
});
this.showReceiveTokens = false;
},

getBalance: async function (): Promise<number> {
const authHeader = await this.generateNip98Event(
`${this.baseURL}/api/v1/balance`,
Expand Down

0 comments on commit 2e18ac3

Please sign in to comment.