-
Notifications
You must be signed in to change notification settings - Fork 16
/
transaction.js
40 lines (35 loc) · 1014 Bytes
/
transaction.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const CryptoJS = require("crypto-js");
const EC = require("elliptic").ec;
const ec = new EC("secp256k1");
const getBalance = (address, blockchain) => {
let balance = 0;
// Code to calculate balance from the blockchain's transaction history
return balance;
};
exports.verifyTransaction = (transaction, blockchain) => {
// Check for the transaction structure
if (
!transaction.fromAddress ||
!transaction.toAddress ||
!transaction.amount
) {
return false;
}
// Check if the signature is valid
const publicKey = ec.keyFromPublic(transaction.fromAddress, "hex");
if (
!publicKey.verify(
CryptoJS.SHA256(
transaction.fromAddress + transaction.toAddress + transaction.amount
).toString(),
transaction.signature
)
) {
return false;
}
// Check for sufficient balance (this requires a function to calculate the balance)
if (getBalance(transaction.fromAddress, blockchain) < transaction.amount) {
return false;
}
return true;
};