Skip to content

Commit

Permalink
Merge pull request #36 from nguyenphuminh/patch-0.14.6
Browse files Browse the repository at this point in the history
Patch 0.14.6
  • Loading branch information
nguyenphuminh authored Jul 26, 2022
2 parents e445794 + 1b9d05a commit 231820b
Showing 1 changed file with 13 additions and 24 deletions.
37 changes: 13 additions & 24 deletions src/core/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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()
)
Expand Down

0 comments on commit 231820b

Please sign in to comment.