Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/7 recieve notification #32

Merged
merged 5 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"react-router-dom": "^6.16.0",
"react-scripts": "^5.0.1",
"replace-module-webpack-plugin": "^1.0.0",
"spv-store": "^0.1.42",
"spv-store": "^0.1.43",
"stream-browserify": "^3.0.0",
"styled-components": "^6.0.8",
"ts-loader": "^9.5.1",
Expand Down
2 changes: 1 addition & 1 deletion public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"manifest_version": 3,
"name": "SPV Wallet",
"description": "A non-custodial and open-source wallet for BSV and 1Sat Ordinals",
"permissions": ["storage", "unlimitedStorage", "tabs"],
"permissions": ["storage", "unlimitedStorage", "tabs", "notifications"],
"action": {
"default_popup": "index.html",
"default_title": "SPV Wallet"
Expand Down
11 changes: 10 additions & 1 deletion src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
} from './inject';
import { EncryptResponse } from './pages/requests/EncryptRequest';
import { DecryptResponse } from './pages/requests/DecryptRequest';
import { removeWindow } from './utils/chromeHelpers';
import { removeWindow, sendTransactionNotification } from './utils/chromeHelpers';
import { GetSignaturesResponse } from './pages/requests/GetSignaturesRequest';
import { ChromeStorageObject, ConnectRequest } from './services/types/chromeStorage.types';
import { ChromeStorageService } from './services/ChromeStorage.service';
Expand Down Expand Up @@ -72,6 +72,14 @@ const INACTIVITY_LIMIT = 10 * 60 * 1000; // 10 minutes

// only run in background worker
if (isInServiceWorker) {
const initNewTxsListener = async () => {
const oneSatSPV = await oneSatSPVPromise;
oneSatSPV.events.on('newTxs', (data: number) => {
sendTransactionNotification(data);
});
};
initNewTxsListener();

const processSyncUtxos = async () => {
try {
const oneSatSPV = await oneSatSPVPromise;
Expand Down Expand Up @@ -107,6 +115,7 @@ if (isInServiceWorker) {
chromeStorageService = new ChromeStorageService();
await chromeStorageService.getAndSetStorage();
oneSatSPVPromise = initOneSatSPV(chromeStorageService, isInServiceWorker);
initNewTxsListener();
};

const launchPopUp = () => {
Expand Down
22 changes: 22 additions & 0 deletions src/utils/chromeHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { HOSTED_YOURS_IMAGE } from './constants';

/* eslint-disable @typescript-eslint/no-explicit-any */
export const sendMessage = (message: any) => {
try {
Expand All @@ -22,3 +24,23 @@ export const removeWindow = (windowId: number) => {
console.error(error);
}
};

export const sendTransactionNotification = (newTxCount: number) => {
// Create the Chrome notification
chrome.notifications.create(
{
type: 'basic',
iconUrl: HOSTED_YOURS_IMAGE,
title: 'New Transactions',
message: `Your SPV wallet has received ${newTxCount} new transaction${newTxCount > 1 ? 's' : ''}!`,
priority: 2,
},
(notificationId: string) => {
if (chrome.runtime.lastError) {
console.error('Notification error:', chrome.runtime.lastError.message || chrome.runtime.lastError);
} else {
console.log('Notification sent:', notificationId);
}
},
);
};
Loading