demo version
This commit is contained in:
parent
fbb282a801
commit
672d6daa8e
125 changed files with 17918 additions and 1481 deletions
|
@ -1,151 +1,91 @@
|
|||
const ChainUtil = require('../chain-util');
|
||||
const ChainUtil = require('../util/chain-util');
|
||||
const SENSHAMART_URI_PREFIX = require('../util/constants').SENSHAMART_URI_PREFIX;
|
||||
|
||||
const tripleValidator = {
|
||||
s: ChainUtil.validateIsString,
|
||||
p: ChainUtil.validateIsString,
|
||||
o: ChainUtil.validateIsString
|
||||
};
|
||||
function validateTerm(t) {
|
||||
const stringRes = ChainUtil.validateIsString(t);
|
||||
|
||||
function validateMetadata(t) {
|
||||
|
||||
let isBroker = [];
|
||||
let costPerMinute = [];
|
||||
let costPerKB = [];
|
||||
let integrationEndpoint = [];
|
||||
|
||||
const validationRes = ChainUtil.validateArray(t, ChainUtil.createValidateObject(tripleValidator));
|
||||
|
||||
if (!validationRes.result) {
|
||||
return validationRes;
|
||||
if (!stringRes.result) {
|
||||
return stringRes;
|
||||
}
|
||||
|
||||
for (const triple of t) {
|
||||
switch (triple.p) {
|
||||
case "http://SSM/Cost_of_Using_IoT_Devices/Cost_Per_Minute": costPerMinute.push(triple); break;
|
||||
case "http://SSM/Cost_of_Using_IoT_Devices/Cost_Per_Kbyte": costPerKB.push(triple); break;
|
||||
case "http://www.w3.org/1999/02/22-rdf-syntax-ns#type":
|
||||
if (triple.o === "http://SSM/Broker") {
|
||||
isBroker.push(triple.s);
|
||||
}
|
||||
break;
|
||||
case "http://SSM/Integration/Endpoint": integrationEndpoint.push(triple); break;
|
||||
}
|
||||
}
|
||||
|
||||
if (isBroker.length === 0) {
|
||||
if (t.startsWith(SENSHAMART_URI_PREFIX)) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "No broker is defined"
|
||||
};
|
||||
} else if (isBroker.length > 1) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Multiple brokers are defined"
|
||||
};
|
||||
}
|
||||
|
||||
const brokerName = isBroker[0];
|
||||
|
||||
if (costPerMinute.length === 0) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "No cost per minute was defined"
|
||||
};
|
||||
} else if (costPerMinute.length > 1) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Multiple cost per minutes were defined"
|
||||
}
|
||||
}
|
||||
const CostPerMinuteValue = Number.parseInt(costPerMinute[0].o);
|
||||
if (CostPerMinuteValue === NaN) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Couldn't parse cost per minute as an integer"
|
||||
};
|
||||
} else if (CostPerMinuteValue < 1) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Cost per minute was negative"
|
||||
}
|
||||
} else if (costPerMinute[0].s != brokerName) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Cost per minute object isn't the broker"
|
||||
};
|
||||
}
|
||||
|
||||
if (costPerKB.length === 0) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "No cost per KB was defined"
|
||||
};
|
||||
} else if (costPerKB.length > 1) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Multiple cost per KB were defined"
|
||||
}
|
||||
}
|
||||
const CostPerKBValue = Number.parseInt(costPerKB[0].o);
|
||||
if (CostPerKBValue === NaN) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Couldn't parse cost per KB as an integer"
|
||||
};
|
||||
} else if (CostPerKBValue < 1) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Cost per KB was negative"
|
||||
}
|
||||
} else if (costPerKB[0].s != brokerName) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Cost per KB object isn't the broker"
|
||||
};
|
||||
}
|
||||
|
||||
if (integrationEndpoint.length === 0) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "No integration endpoint was defined"
|
||||
};
|
||||
} else if (integrationEndpoint.length > 1) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Multiple integration endpoints were defined"
|
||||
};
|
||||
} else if (integrationEndpoint[0].s != brokerName) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Integration endpoint object isn't the broker"
|
||||
reason: "Starts with reserved prefix"
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
result: true,
|
||||
metadata: {
|
||||
brokerName: brokerName,
|
||||
costPerMinute: CostPerMinuteValue,
|
||||
costPerKB: CostPerKBValue,
|
||||
integrationEndpoint: integrationEndpoint[0].o
|
||||
}
|
||||
result: true
|
||||
};
|
||||
}
|
||||
|
||||
function validateLiteral(t) {
|
||||
const termRes = validateTerm(t);
|
||||
if (termRes.result) {
|
||||
return termRes;
|
||||
}
|
||||
|
||||
const numberRes = ChainUtil.validateIsNumber(t);
|
||||
|
||||
if (numberRes.result) {
|
||||
return numberRes;
|
||||
}
|
||||
|
||||
return {
|
||||
result: false,
|
||||
reason: "Wasn't a string or a number"
|
||||
};
|
||||
}
|
||||
|
||||
const nodeValidator = {
|
||||
s: validateTerm,
|
||||
p: validateTerm,
|
||||
o: validateTerm
|
||||
};
|
||||
|
||||
const literalValidator = {
|
||||
s: validateTerm,
|
||||
p: validateTerm,
|
||||
o: validateLiteral
|
||||
};
|
||||
|
||||
const metadataValidation = {
|
||||
name: ChainUtil.validateIsString,
|
||||
endpoint: ChainUtil.validateIsString,
|
||||
extraNodes: ChainUtil.createValidateOptional(
|
||||
ChainUtil.createValidateArray(
|
||||
ChainUtil.createValidateObject(
|
||||
nodeValidator))),
|
||||
extraLiterals: ChainUtil.createValidateOptional(
|
||||
ChainUtil.createValidateArray(
|
||||
ChainUtil.createValidateObject(
|
||||
literalValidator)))
|
||||
}
|
||||
|
||||
const baseValidation = {
|
||||
input: ChainUtil.validateIsPublicKey,
|
||||
counter: ChainUtil.validateIsInteger,
|
||||
counter: ChainUtil.createValidateIsIntegerWithMin(0),
|
||||
rewardAmount: ChainUtil.createValidateIsIntegerWithMin(0),
|
||||
metadata: validateMetadata,
|
||||
metadata: ChainUtil.createValidateObject(metadataValidation),
|
||||
signature: ChainUtil.validateIsSignature
|
||||
};
|
||||
|
||||
class BrokerRegistration {
|
||||
constructor(senderKeyPair, counter, metadata, rewardAmount) {
|
||||
constructor(senderKeyPair, counter, brokerName, endpoint, nodeMetadata, literalMetadata, rewardAmount) {
|
||||
this.input = senderKeyPair.getPublic().encode('hex');
|
||||
this.counter = counter;
|
||||
this.rewardAmount = rewardAmount;
|
||||
this.metadata = metadata;
|
||||
this.metadata = {
|
||||
name: brokerName,
|
||||
endpoint: endpoint
|
||||
};
|
||||
if (typeof nodeMetadata !== undefined && nodeMetadata !== null) {
|
||||
this.metadata.extraNodes = nodeMetadata;
|
||||
};
|
||||
if (typeof literalMetadata !== undefined && literalMetadata !== null) {
|
||||
this.metadata.extraLiterals = literalMetadata;
|
||||
};
|
||||
this.signature = senderKeyPair.sign(BrokerRegistration.hashToSign(this));
|
||||
|
||||
const verification = BrokerRegistration.verify(this);
|
||||
|
@ -154,6 +94,30 @@ class BrokerRegistration {
|
|||
}
|
||||
}
|
||||
|
||||
static getBrokerName(registration) {
|
||||
return registration.metadata.name;
|
||||
}
|
||||
|
||||
static getEndpoint(registration) {
|
||||
return registration.metadata.endpoint;
|
||||
}
|
||||
|
||||
static getExtraNodeMetadata(registration) {
|
||||
if ("extraNodes" in registration.metadata) {
|
||||
return registration.metadata.extraNodes;
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
static getExtraLiteralMetadata(registration) {
|
||||
if ("extraLiterals" in registration.metadata) {
|
||||
return registration.metadata.extraLiterals;
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
static hashToSign(registration) {
|
||||
return ChainUtil.hash([
|
||||
registration.counter,
|
||||
|
@ -173,7 +137,7 @@ class BrokerRegistration {
|
|||
BrokerRegistration.hashToSign(registration));
|
||||
|
||||
if (!signatureRes.result) {
|
||||
return signatureRes.reason;
|
||||
return signatureRes;
|
||||
}
|
||||
|
||||
return {
|
||||
|
@ -181,8 +145,8 @@ class BrokerRegistration {
|
|||
};
|
||||
}
|
||||
|
||||
static getExtInformation(registration) {
|
||||
return validateMetadata(registration.metadata);
|
||||
static name() {
|
||||
return "BrokerRegistration";
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue