Skip to content

Commit

Permalink
Added unconfirmed event, removed optimize txs from getTransactions, r…
Browse files Browse the repository at this point in the history
…emoved backgroundSyncTransactions and checkTx function
  • Loading branch information
n9lsjr committed Dec 2, 2023
1 parent 20f1707 commit 221700b
Showing 1 changed file with 13 additions and 66 deletions.
79 changes: 13 additions & 66 deletions src/backend/electron.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,6 @@ ipcMain.on("start-wallet", async (e, walletName, password, node) => {
const balance = await walletBackend.getBalance();
mainWindow.webContents.send("data", { walletBlockCount, localDaemonBlockCount, networkBlockCount, balance });

const privateViewKey = walletBackend.getPrivateViewKey();
const myAddress = walletBackend.getPrimaryAddress();
const [publicSpendKey, privateSpendKey, err] = await walletBackend.getSpendKeys(myAddress);
const keyset = { publicSpendKey, privateSpendKey, privateViewKey };

if (err) {
console.log("Failed to get spend keys for address: " + err.toString());
}

//////////////// EVENTS
walletBackend.on("desync", (walletHeight, networkHeight) => {
console.log(`Wallet is no longer synced! Wallet height: ${walletHeight}, Network height: ${networkHeight}`);
Expand All @@ -246,6 +237,17 @@ ipcMain.on("start-wallet", async (e, walletName, password, node) => {
console.log(`🚨 INCOMING TX - AMOUNT: ${WB.prettyPrintAmount(transaction.totalAmount())}`);
});

walletBackend.on("unconfirmedtx", (amount, hash) => {
mainWindow.webContents.send("incoming-hash", {hash, amount});
notifier.notify({
appID: "Kryptokrona Wallet",
title: "Found a transaction",
message: `Waiting for confirmation..`,
icon: path.join(__dirname, "../", "../", "static", "icon.png"),
wait: true
});
});

walletBackend.on("heightchange", async (walletBlockCount, localDaemonBlockCount, networkBlockCount) => {
miscs.set("node-stats", { walletBlockCount, localDaemonBlockCount, networkBlockCount });

Expand All @@ -259,7 +261,6 @@ ipcMain.on("start-wallet", async (e, walletName, password, node) => {
try {
//Start syncing
await sleep(5 * 1000);
backgroundSyncTransactions(keyset, node);
const [walletBlockCount, localDaemonBlockCount, networkBlockCount] = walletBackend.getSyncStatus();
const balance = await walletBackend.getBalance();
console.log('Balance: ', balance);
Expand Down Expand Up @@ -301,62 +302,6 @@ async function saveWallet(userDataDir, walletName, password) {

let known_pool_txs = [];

async function backgroundSyncTransactions(keyset, node) {

let message_was_unknown;
try {
const resp = await fetch(`${node.ssl ? 'https://' : 'http://'}${node.url}:${node.port}/get_pool_changes_lite`, {
method: "POST",
body: JSON.stringify({ knownTxsIds: known_pool_txs })
});
let json = await resp.json();
json = JSON.stringify(json).replaceAll(".txPrefix", "").replaceAll("transactionPrefixInfo.txHash", "transactionPrefixInfotxHash");
json = JSON.parse(json);
let txs = json.addedTxs;

txs.forEach(tx => {
checkTx(tx, keyset);
});

} catch (err) {
console.log(err);
console.log("Sync error");
}
}
//Checks if we can unlock transactions
async function checkTx(tx, keyset) {
if (tx.transactionPrefixInfo.extra.length >= 200) return
known_pool_txs.push(tx.transactionPrefixInfotxHash);
const txPublicKey = tx.transactionPrefixInfo.extra.substring(2, 66);
const derivation = await crypto.generateKeyDerivation(txPublicKey, keyset.privateViewKey);
const transactionOutputs = tx.transactionPrefixInfo.vout.entries();
const hash = tx.transactionPrefixInfotxHash
let found = false
let amount = 0
for (const [outputIndex, output] of tx.transactionPrefixInfo.vout.entries()) {

/* Derive the spend key from the transaction, using the previous
derivation */
const derivedSpendKey = await crypto.underivePublicKey(derivation, outputIndex, output.target.data.key);
/* See if the derived spend key matches any of our spend keys */
if (keyset.publicSpendKey === derivedSpendKey) {
found = true
amount += output.amount
}
}
if (found) {
mainWindow.webContents.send("incoming-hash", {hash, amount});
notifier.notify({
appID: "Kryptokrona Wallet",
title: "Found a transaction",
message: `Waiting for confirmation..`,
icon: path.join(__dirname, "../", "../", "static", "icon.png"),
wait: true
});
}

}

ipcMain.on("reset-wallet", (e, height) => {

walletBackend.reset(parseInt(height));
Expand Down Expand Up @@ -493,6 +438,8 @@ ipcMain.handle('get-transactions', async (e, startIndex, all = false) => {
const pages = Math.ceil(allTx.length / showPerPage)
const pageTx = []
for (const tx of await walletBackend.getTransactions(startIndex, showPerPage)) {
//Exclude optimize txs
if (tx.totalAmount() === 0) continue
pageTx.push({
hash: tx.hash,
amount: tx.totalAmount(),
Expand Down

0 comments on commit 221700b

Please sign in to comment.