-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransactionV2.js
More file actions
41 lines (36 loc) · 993 Bytes
/
TransactionV2.js
File metadata and controls
41 lines (36 loc) · 993 Bytes
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
41
const { verifySignature, sign } = require('./crypto');
const SHA256 = require('crypto-js/sha256');
class Transaction {
/**
*
* @param {number} version
* @param {number} vin
* @param {number} vout
* @param {array.<TXInput>} inputs
* @param {array.<UTXO>} outputs
* @param {number} locktime
*/
constructor(version = 1, vin = 0, vout = 0, inputs = [], outputs = [], locktime = 0) {
this.version = 1;
this.vin = vin;
this.vout = vout;
this.inputs = inputs;
this.outputs = outputs;
this.locktime = locktime;
this.hash = this.calculateHash();
}
hasValidSignature() {
return (
this.signature !== undefined && verifySignature(this.hash, this.signature, this.inputPublicKey)
)
}
sign(privateKey) {
this.signature = sign(this.hash, privateKey);
}
calculateHash() {
return SHA256(this.inputPublicKey + this.outputPublicKey + this.amount + this.fee).toString();
}
}
module.exports = {
Transaction
};