diff --git a/src/core/transaction.js b/src/core/transaction.js index 275de7c..4c71ec6 100644 --- a/src/core/transaction.js +++ b/src/core/transaction.js @@ -16,25 +16,22 @@ class Transaction { this.gas = gas; // Gas that transaction consumed + tip for miner this.additionalData = additionalData; // Additional data that goes into the transaction this.timestamp = timestamp; // Creation timestamp (doesn't matter if true or not, just for randomness) + this.signature = ""; // Transaction's signature, will be generated later + } - // Transaction's hash - this.hash = SHA256( - this.sender + - this.recipient + - this.amount.toString() + - this.gas.toString() + - JSON.stringify(this.additionalData) + - this.timestamp.toString() - ); - - this.signature = ""; // Transaction's signature, will be generated later + static getHash(tx) { + return SHA256( + tx.sender + + tx.recipient + + tx.amount.toString() + + tx.gas.toString() + + JSON.stringify(tx.additionalData) + + tx.timestamp.toString() + ) } static sign(transaction, keyPair) { - if (keyPair.getPublic("hex") === transaction.sender) { - // The signature is generated by signing transaction's hash. - transaction.signature = keyPair.sign(transaction.hash, "base64").toDER("hex"); - } + transaction.signature = keyPair.sign(Transaction.getHash(transaction), "base64").toDER("hex"); } static async isValid(tx, stateDB) { @@ -57,15 +54,7 @@ class Transaction { return ( tx.amount >= 0 && ((senderBalance >= tx.amount + tx.gas + (tx.additionalData.contractGas || 0) && tx.gas >= 1) || tx.sender === MINT_PUBLIC_ADDRESS) && - tx.hash === SHA256( - tx.sender + - tx.recipient + - tx.amount.toString() + - tx.gas.toString() + - JSON.stringify(tx.additionalData) + - tx.timestamp.toString() - ) && - ec.keyFromPublic(tx.sender, "hex").verify(tx.hash, tx.signature) && + ec.keyFromPublic(tx.sender, "hex").verify(Transaction.getHash(tx), tx.signature) && !usedTimestamps.includes(tx.timestamp) && tx.timestamp <= Date.now() )