miner mines continuously,
moved to an account model, transactions working, querying working, more checking of valid data
This commit is contained in:
parent
59bb42be11
commit
9847b2056b
12 changed files with 663 additions and 365 deletions
|
@ -2,66 +2,77 @@ const ChainUtil = require('../chain-util');
|
|||
const { MINING_REWARD } = require('../config');
|
||||
|
||||
class Transaction {
|
||||
constructor() {
|
||||
this.id = ChainUtil.id();
|
||||
this.input = null;
|
||||
this.outputs = [];
|
||||
constructor(senderPublicKey, counter, outputs) {
|
||||
this.input = senderPublicKey;
|
||||
this.signature = null;
|
||||
this.counter = counter;
|
||||
this.outputs = outputs;
|
||||
}
|
||||
|
||||
update(senderWallet, recipient, amount) {
|
||||
const senderOutput = this.outputs.find(output => output.address === senderWallet.publicKey);
|
||||
addSignature(signature) {
|
||||
if (!ChainUtil.verifySignature(
|
||||
this.input,
|
||||
signature,
|
||||
Transaction.hashToSign(this))) {
|
||||
console.log("Tried to add an invalid signature to a transaction");
|
||||
throw new Error("Tried to add an invalid signature to a transaction");
|
||||
}
|
||||
this.signature = signature;
|
||||
}
|
||||
|
||||
if (amount > senderOutput.amount) {
|
||||
console.log(`Amount: ${amount} exceeds balance.`);
|
||||
return;
|
||||
static hashToSign(transaction) {
|
||||
return ChainUtil.hash({
|
||||
counter: transaction.counter,
|
||||
outputs: transaction.outputs
|
||||
});
|
||||
}
|
||||
|
||||
static createOutput(recipient, amount) {
|
||||
return {
|
||||
publicKey: recipient,
|
||||
amount: amount
|
||||
};
|
||||
}
|
||||
|
||||
//update(senderWallet, recipients) {
|
||||
// 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);
|
||||
|
||||
// return this;
|
||||
//}
|
||||
//static signTransaction(transaction, senderWallet) {
|
||||
// transaction.input = {
|
||||
// timestamp: Date.now(),
|
||||
// address: senderWallet.publicKey,
|
||||
// signature: senderWallet.sign(ChainUtil.hash(transaction.outputs))
|
||||
// }
|
||||
//}
|
||||
|
||||
static verify(transaction) {
|
||||
if (transaction.outputs.length === 0) {
|
||||
return false;
|
||||
}
|
||||
for (const output of transaction.outputs) {
|
||||
if (!output.hasOwnProperty('amount')) {
|
||||
return false;
|
||||
}
|
||||
if (!output.hasOwnProperty('publicKey')) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
senderOutput.amount = senderOutput.amount - amount;
|
||||
this.outputs.push({ amount, address: recipient });
|
||||
Transaction.signTransaction(this, senderWallet);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
static transactionWithOutputs(senderWallet, outputs) {
|
||||
const transaction = new this();
|
||||
transaction.outputs.push(...outputs);
|
||||
Transaction.signTransaction(transaction, senderWallet);
|
||||
return transaction;
|
||||
}
|
||||
|
||||
static newTransaction(senderWallet, recipient, amount) {
|
||||
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) {
|
||||
return Transaction.transactionWithOutputs(blockchainWallet, [{
|
||||
amount: MINING_REWARD, address: minerWallet.publicKey
|
||||
}]);
|
||||
}
|
||||
|
||||
static signTransaction(transaction, senderWallet) {
|
||||
transaction.input = {
|
||||
timestamp: Date.now(),
|
||||
amount: senderWallet.balance,
|
||||
address: senderWallet.publicKey,
|
||||
signature: senderWallet.sign(ChainUtil.hash(transaction.outputs))
|
||||
}
|
||||
}
|
||||
|
||||
static verifyTransaction(transaction) {
|
||||
return ChainUtil.verifySignature(
|
||||
transaction.input.address,
|
||||
transaction.input.signature,
|
||||
ChainUtil.hash(transaction.outputs)
|
||||
transaction.input,
|
||||
transaction.signature,
|
||||
Transaction.hashToSign(transaction)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue