How to store values from a exported class?

Hi All,

I have been utilising code that I have found to develop a blockchain, I am looking to create a web app using this code one that is permission-able but able to communicate with other nodes. I have been using the below code, however when I am calling the exported class, and trying to achieve specific actions within the class for example it seems to be creating a new instance of the exported class which then confuses the front end function. I provide the code below, any help support or advice would be greatly appreciated.

import { ChainUtil } from 'public/chain-util.js';
import { IntConfig } from 'public/config.js';
import { Transaction } from 'public/transaction.js';
import wixData from 'wix-data';

export class Wallet {
    constructor(id) {
 this.id = id || ChainUtil.id();
 this.balance = IntConfig.INITIAL_BALANCE;
 this.keyPair = ChainUtil .genKeyPair();
 this.privateKey = this.keyPair.getPrivate('hex');
 this.publicKey = this.keyPair.getPublic().encode('hex');
    }

    toString() {
 return `Wallet -
    Blockchain ID: ${this.id.toString()}
    Private Key  : ${this.privateKey.toString()}
    Public Key   : ${this.publicKey.toString()}
    Balance      : ${this.balance}`
    }

    sign(dataHash) {
 return this.keyPair.sign(dataHash);
    }

    createTransaction(recipient, amount, blockchain, transactionPool) {
 this.balance = this.calculateBalance(blockchain);

 if (amount > this.balance) {
            console.log(`Amount: ${amount}, exceeds current balance: ${this.balance}`);
 return;
        }

 let transaction = transactionPool.existingTransaction(this.publicKey);
 if (transaction) {
            transaction.update(this, recipient, amount);
        } else {
            transaction = Transaction.newTransaction(this, recipient, amount);
            transactionPool.updateOrAddTransaction(transaction);
        }

 return transaction;
    }

    calculateBalance(blockchain) {
 let balance = this.balance;
 let transactions = [];
        blockchain.chain.forEach(block => block.data.forEach(transaction => {
            transactions.push(transaction);
        }));

 const walletInputTs = transactions
            .filter(transaction => transaction.input.address === this.publicKey);

 let startTime = 0;

 if (walletInputTs.length > 0) {
 const recentInputT = walletInputTs.reduce(
                (prev, current) => prev.input.timestamp > current.input.timestamp ? prev : current
            );

            balance = recentInputT.outputs.find(output => output.address === this.publicKey).amount;
            startTime = recentInputT.input.timestamp;
        }

        transactions.forEach(transaction => {
 if (transaction.input.timestamp > startTime) {
                transaction.outputs.find(output => {
 if (output.address === this.publicKey) {
                        balance += output.amount;
                    }
                });
            }
        });
 return balance;
    }

 static blockchainWallet() {
 const blockchainWallet = new this();
        blockchainWallet.address = 'blockchain-wallet';
 return blockchainWallet;
    }
}

I also provide the following to aid in any help or support provided by others, below is the transaction class that is being requested:

import { ChainUtil } from 'public/chain-util.js';
import { Wallet } from 'public/wallet.js';
import {IntConfig} from 'public/config.js';

export class Transaction {
    constructor(id) {
 this.id = id || ChainUtil.id();
 this.input = null;
 this.outputs = [];
    }

    update(senderWallet, recipient, amount) {
        console.log("update Called");
 const senderOutput = this.outputs.find(output => output.address === senderWallet.publicKey);

 if (amount > senderOutput.amount) {
            console.log(`Amount: ${amount} exceeds balance.`);
 return;
        }

        senderOutput.amount = senderOutput.amount - amount;
 this.outputs.push({ amount, address: recipient });
        Transaction.signTransaction(this, senderWallet);

        console.log(this);

 return this;
    }

 static transactionWithOutputs(senderWallet, outputs) {
        console.log("transaction with outputs called");
 const transaction = new this();
        transaction.outputs.push(...outputs);
        Transaction.signTransaction(transaction, senderWallet);

        console.log(transaction);

 return transaction;
    }

 static newTransaction(senderWallet, recipient, amount) {
        console.log("new transaction called");
 if (amount > senderWallet.balance) {
            console.log(`Amount: ${amount} exceeds balance.`);
 return;
        }

 return Transaction.transactionWithOutputs(senderWallet, [
            { amount: senderWallet.balance - amount, address: senderWallet.publicKey },
            { amount, address: recipient }
        ]);
    }

 static rewardTransaction(minerWallet, blockchainWallet) {
        console.log("reward transaction Called");
 return Transaction.transactionWithOutputs(blockchainWallet, [{
            amount: IntConfig.MINING_REWARD,
            address: minerWallet.publicKey
        }]);
    }

 static signTransaction(transaction, senderWallet) {
        console.log("sign transaction called");
        console.log(transaction);
        console.log(ChainUtil.hash(transaction.outputs));
        console.log(Wallet.sign(ChainUtil.hash(transaction.outputs)));
        transaction.input = {
            timestamp: Date.now(),
            amount: senderWallet.balance,
            address: senderWallet.publicKey,
            signature: senderWallet.sign(ChainUtil.hash(transaction.outputs))
        }
    }
 static verifyTransaction(transaction) {
        console.log("verifytransaction called");
 return ChainUtil.verifySignature(
            transaction.input.address,
            transaction.input.signature,
            ChainUtil.hash(transaction.outputs)
        );
    }
}

and this is the client side code that is for the function to be adding a new transaction:

export function addNewTransaction_click(event) {
 const senderWallet = {
        balance: $w("#balance").value,
        publicKey: $w("#input1").value,
    }
 const recipient = $w("#input2").value;
 const amount = $w("#input3").value;
    Transaction.newTransaction(senderWallet, recipient, amount);
}

when the transaction is called by the above function on the client side, it creates all the data needed but gets stuck at “signature: senderWallet.sign(ChainUtil.hash(transaction.outputs))” on the transaction code because it doesn’t receive the senderWallet.sign details as it has created these and then forgotten them or created new ones, I am sure the answer will be to utilise the collections however when collecting the wallet data, how are the wallet.sign() saved from the code in the wallet within the collections? will it be a matter of saving the private and public keys from the ec.generateKeyPair and then returning them to be used for the purpose of signing? again any help would be greatly appreciated.

Si