Commit some new networking code, adding integration, broker still not 100%, hasn't been committed
This commit is contained in:
parent
1af6d56e2d
commit
050e69e23f
18 changed files with 1829 additions and 359 deletions
65
blockchain/compensation.js
Normal file
65
blockchain/compensation.js
Normal file
|
@ -0,0 +1,65 @@
|
|||
const ChainUtil = require('../chain-util');
|
||||
const Integration = require('./integration');
|
||||
|
||||
const integrationValidation = {
|
||||
input: ChainUtil.valdiateIsPublicKey,
|
||||
counter: ChainUtil.createValidateIsIntegerWithMin(1)
|
||||
};
|
||||
|
||||
const baseValidation = {
|
||||
input: ChainUtil.validateIsPublicKey,
|
||||
brokerName: ChainUtil.validateIsString,
|
||||
integration: ChainUtil.createValidateObject(integrationValidation),
|
||||
signature: ChainUtil.validateIsSignature
|
||||
};
|
||||
|
||||
class Compensation {
|
||||
constructor(senderKeyPair, brokerName, integration) {
|
||||
const verifyIntegration = Integration.verify(integration);
|
||||
|
||||
if (!verifyIntegration.result) {
|
||||
throw new Error(verifyIntegration.reason);
|
||||
}
|
||||
|
||||
this.input = senderKeyPair.getPublic().encode('hex');
|
||||
this.brokerName = brokerName;
|
||||
this.integration = {
|
||||
input: integration.input,
|
||||
counter: integration.counter
|
||||
};
|
||||
this.signature = senderKeyPair.sign(Compensation.hashToSign(this));
|
||||
|
||||
const verification = Compensation.verify(this);
|
||||
if (!verification.result) {
|
||||
throw new Error(verification.reason);
|
||||
}
|
||||
}
|
||||
|
||||
static hashToSign(transaction) {
|
||||
return ChainUtil.hash([
|
||||
transaction.input,
|
||||
transaction.brokerName,
|
||||
transaction.integration]);
|
||||
}
|
||||
|
||||
static verify(transaction) {
|
||||
const validationRes = ChainUtil.validateObject(transaction, baseValidation);
|
||||
if (!validationRes.result) {
|
||||
return validationRes;
|
||||
}
|
||||
|
||||
const verifyRes = ChainUtil.verifySignature(
|
||||
transaction.input,
|
||||
transaction.signature,
|
||||
Compensation.hashToSign(transaction));
|
||||
if (!verifyRes.result) {
|
||||
return verifyRes;
|
||||
}
|
||||
|
||||
return {
|
||||
result: true,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Compensation;
|
Loading…
Add table
Add a link
Reference in a new issue