From 96431f4aed6ba7778fc27e65d5deeeff2b807ddd Mon Sep 17 00:00:00 2001 From: Blake Kaufman Date: Sat, 5 Oct 2024 09:26:22 -0400 Subject: [PATCH 1/2] added notifications to contact payments --- .../contactsTransactions.js | 4 +- .../contacts/sendAndRequestPage.js | 15 ++++-- .../sendBitcoin/sendPaymentScreen.js | 10 +--- app/functions/liquidWallet/index.js | 4 +- app/functions/messaging/publishMessage.js | 54 +++++++++++++++++++ db/index.js | 4 +- 6 files changed, 74 insertions(+), 17 deletions(-) diff --git a/app/components/admin/homeComponents/contacts/internalComponents/contactsTransactions.js b/app/components/admin/homeComponents/contacts/internalComponents/contactsTransactions.js index c418458a..28dc0106 100644 --- a/app/components/admin/homeComponents/contacts/internalComponents/contactsTransactions.js +++ b/app/components/admin/homeComponents/contacts/internalComponents/contactsTransactions.js @@ -368,7 +368,7 @@ export default function ContactsTransactionItem(props) { if (txData.uuid === txID) { console.log('TRUE'); - console.log(txData); + // console.log(txData); return { ...tx, data: txDataType ? txData : {...txData, isRedeemed: true}, @@ -602,7 +602,7 @@ function ConfirmedOrSentTransaction({ const {nodeInformation, masterInfoObject} = useGlobalContextProvider(); const {textColor} = GetThemeColors(); - console.log(props.transaction, 'TES'); + // console.log(props.transaction, 'TES'); return ( {txParsed.isDeclined ? ( diff --git a/app/components/admin/homeComponents/contacts/sendAndRequestPage.js b/app/components/admin/homeComponents/contacts/sendAndRequestPage.js index 913b7254..f9f66f5c 100644 --- a/app/components/admin/homeComponents/contacts/sendAndRequestPage.js +++ b/app/components/admin/homeComponents/contacts/sendAndRequestPage.js @@ -52,6 +52,7 @@ import GetThemeColors from '../../../../hooks/themeColors'; import ThemeImage from '../../../../functions/CustomElements/themeImage'; import {assetIDS} from '../../../../functions/liquidWallet/assetIDS'; import useDebounce from '../../../../hooks/useDebounce'; +import {getSignleContact} from '../../../../../db'; export default function SendAndRequestPage(props) { const navigate = useNavigation(); @@ -415,6 +416,12 @@ export default function SendAndRequestPage(props) { ); async function handleSubmit() { + // sendPushNotification({ + // selectedContactUsername: selectedContact.uniqueName, + // myProfile: globalContactsInformation.myProfile, + // sendingAmountSat: 5000, + // }); + // return; if (!nodeInformation.didConnectToNode) { navigate.navigate('ErrorScreen', { errorMessage: 'Please reconnect to the internet to use this feature', @@ -446,9 +453,9 @@ export default function SendAndRequestPage(props) { process.env.BOLTZ_ENVIRONMENT === 'testnet' ? 'liquidtestnet:' : 'liquidnetwork:' - }${address}?amount=${(convertedSendAmount / SATSPERBITCOIN).toFixed( - 8, - )}&assetid=${assetIDS['L-BTC']}`; + }${'tlq1qqtxzyvkar7vhpxyylnf8mmra2xdndrcl64a4mm24p4pq5526nlwsn6k2nrl46z4hldpy25fakxdkzzv8z56nxgmp5q846eqwq'}?amount=${( + convertedSendAmount / SATSPERBITCOIN + ).toFixed(8)}&assetid=${assetIDS['L-BTC']}`; console.log(receiveAddress); @@ -486,6 +493,7 @@ export default function SendAndRequestPage(props) { paymentType, decodedAddedContacts, publicKey, + selectedContact, ), }); // setIsPerformingSwap(true); @@ -607,6 +615,7 @@ export default function SendAndRequestPage(props) { paymentType, decodedAddedContacts, publicKey, + selectedContact, ); navigate.goBack(); } diff --git a/app/components/admin/homeComponents/sendBitcoin/sendPaymentScreen.js b/app/components/admin/homeComponents/sendBitcoin/sendPaymentScreen.js index c3b3068b..f6140b39 100644 --- a/app/components/admin/homeComponents/sendBitcoin/sendPaymentScreen.js +++ b/app/components/admin/homeComponents/sendBitcoin/sendPaymentScreen.js @@ -261,15 +261,7 @@ export default function SendPaymentScreen({ navigate, setHasError, }); - }, [ - nodeInformation, - btcAdress, - goBackFunction, - liquidNodeInformation, - masterInfoObject, - setWebViewArgs, - webViewRef, - ]); + }, []); return ( diff --git a/app/functions/liquidWallet/index.js b/app/functions/liquidWallet/index.js index 4f091263..f475b471 100644 --- a/app/functions/liquidWallet/index.js +++ b/app/functions/liquidWallet/index.js @@ -176,7 +176,7 @@ async function getLiquidTxFee({amountSat, address}) { : process.env.BLITZ_LIQUID_TESTNET_ADDRESS; console.log(amountSat, address, '!'); - const fee_rate = 100; // this is the sat/vB * 100 fee rate. Example 280 would equal a fee rate of .28 sat/vB. 100 would equal .1 sat/vB + const fee_rate = process.env.BOLTZ_ENVIRONMENT === 'testnet' ? 20 : 100; // this is the sat/vB * 100 fee rate. Example 280 would equal a fee rate of .28 sat/vB. 100 would equal .1 sat/vB const builder = await new TxBuilder().create(network); @@ -234,7 +234,7 @@ async function sendLiquidTransaction(amountSat, address, doesNeedToWait) { const update = await client.fullScan(wollet); await wollet.applyUpdate(update); - const fee_rate = 100; // this is the sat/vB * 100 fee rate. Example 280 would equal a fee rate of .28 sat/vB. 100 would equal .1 sat/vB + const fee_rate = process.env.BOLTZ_ENVIRONMENT === 'testnet' ? 20 : 100; // this is the sat/vB * 100 fee rate. Example 280 would equal a fee rate of .28 sat/vB. 100 would equal .1 sat/vB const builder = await new TxBuilder().create(network); await builder.addLbtcRecipient(address, amountSat); await builder.feeRate(fee_rate); diff --git a/app/functions/messaging/publishMessage.js b/app/functions/messaging/publishMessage.js index fc9183c1..3c285a06 100644 --- a/app/functions/messaging/publishMessage.js +++ b/app/functions/messaging/publishMessage.js @@ -1,5 +1,7 @@ import {AblyRealtime} from './getToken'; import {encriptMessage} from './encodingAndDecodingMessages'; +import formatBalanceAmount from '../formatNumber'; +import {getSignleContact} from '../../../db'; export async function pubishMessageToAbly( fromPrivKey, @@ -11,6 +13,7 @@ export async function pubishMessageToAbly( paymentType, decodedContacts, sendingPublicKey, + selectedContact, ) { try { const channel = AblyRealtime.channels.get('blitzWalletPayments'); @@ -59,6 +62,11 @@ export async function pubishMessageToAbly( // return contact; // } else return contact; // }); + sendPushNotification({ + selectedContactUsername: selectedContact.uniqueName, + myProfile: globalContactsInformation.myProfile, + data: data, + }); toggleGlobalContactsInformation( { @@ -79,3 +87,49 @@ export async function pubishMessageToAbly( console.log(err); } } + +async function sendPushNotification({ + selectedContactUsername, + myProfile, + data, +}) { + console.log(selectedContactUsername); + const retrivedContact = await getSignleContact( + selectedContactUsername.toLowerCase(), + ); + + if (retrivedContact.length === 0) return; + const [selectedContact] = retrivedContact; + + const devicePushKey = selectedContact?.pushNotifications?.key?.encriptedText; + const deviceType = selectedContact?.pushNotifications?.platform; + + console.log(devicePushKey, deviceType); + + if (!devicePushKey || !deviceType) return; + let message; + if (data.isRequest) { + message = `${myProfile.uniqueName} requested you ${formatBalanceAmount( + JSON.parse(data).amountMsat / 1000, + )} sats`; + } else { + message = `${myProfile.uniqueName} paid you ${formatBalanceAmount( + JSON.parse(data).amountMsat / 1000, + )} sats`; + } + + await fetch( + 'http://localhost:8888/.netlify/functions/contactsPushNotification', + { + method: 'POST', // Specify the HTTP method + headers: { + 'Content-Type': 'application/json', // Set the content type to JSON + }, + body: JSON.stringify({ + devicePushKey: devicePushKey, + deviceType: deviceType, + message: message, + }), + }, + ); +} diff --git a/db/index.js b/db/index.js index d6ec6fc2..964ab351 100644 --- a/db/index.js +++ b/db/index.js @@ -319,7 +319,9 @@ export async function getSignleContact(wantedName) { where('contacts.myProfile.uniqueNameLower', '==', wantedName.toLowerCase()), ); const querySnapshot = await getDocs(q); - return new Promise(resolve => resolve(querySnapshot.docs)); + // Map through querySnapshot and return the data from each document + const contactData = querySnapshot.docs.map(doc => doc.data()); + return new Promise(resolve => resolve(contactData)); } export async function canUsePOSName( collectionName = 'blitzWalletUsers', From 1f616ed36259c1709067a1556a5572c9ad4570a9 Mon Sep 17 00:00:00 2001 From: Blake Kaufman Date: Sat, 5 Oct 2024 09:32:54 -0400 Subject: [PATCH 2/2] fixed request bug --- .../admin/homeComponents/contacts/sendAndRequestPage.js | 6 +++--- app/functions/messaging/publishMessage.js | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/components/admin/homeComponents/contacts/sendAndRequestPage.js b/app/components/admin/homeComponents/contacts/sendAndRequestPage.js index f9f66f5c..b38898e8 100644 --- a/app/components/admin/homeComponents/contacts/sendAndRequestPage.js +++ b/app/components/admin/homeComponents/contacts/sendAndRequestPage.js @@ -453,9 +453,9 @@ export default function SendAndRequestPage(props) { process.env.BOLTZ_ENVIRONMENT === 'testnet' ? 'liquidtestnet:' : 'liquidnetwork:' - }${'tlq1qqtxzyvkar7vhpxyylnf8mmra2xdndrcl64a4mm24p4pq5526nlwsn6k2nrl46z4hldpy25fakxdkzzv8z56nxgmp5q846eqwq'}?amount=${( - convertedSendAmount / SATSPERBITCOIN - ).toFixed(8)}&assetid=${assetIDS['L-BTC']}`; + }${address}?amount=${(convertedSendAmount / SATSPERBITCOIN).toFixed( + 8, + )}&assetid=${assetIDS['L-BTC']}`; console.log(receiveAddress); diff --git a/app/functions/messaging/publishMessage.js b/app/functions/messaging/publishMessage.js index 3c285a06..c49ad33f 100644 --- a/app/functions/messaging/publishMessage.js +++ b/app/functions/messaging/publishMessage.js @@ -108,7 +108,7 @@ async function sendPushNotification({ if (!devicePushKey || !deviceType) return; let message; - if (data.isRequest) { + if (JSON.parse(data).isRequest) { message = `${myProfile.uniqueName} requested you ${formatBalanceAmount( JSON.parse(data).amountMsat / 1000, )} sats`;