demo version
This commit is contained in:
parent
fbb282a801
commit
672d6daa8e
125 changed files with 17918 additions and 1481 deletions
347
util/chain-util.js
Normal file
347
util/chain-util.js
Normal file
|
@ -0,0 +1,347 @@
|
|||
const EC = require('elliptic').ec;
|
||||
const SHA256 = require('crypto-js/sha256');
|
||||
const { v1 : uuidV1 } = require ('uuid');
|
||||
const ec = new EC('secp256k1');
|
||||
|
||||
function convertJsonKeyValueToRDFImpl(key, object) {
|
||||
const returning = [];
|
||||
|
||||
for (const key in object) {
|
||||
const value = object[key];
|
||||
|
||||
if (value instanceof Array) {
|
||||
} else if (value instanceof Object) {
|
||||
returning.push({
|
||||
o
|
||||
});
|
||||
} else {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ChainUtil {
|
||||
static genKeyPair() {
|
||||
return ec.genKeyPair();
|
||||
}
|
||||
|
||||
static id() {
|
||||
return uuidV1();
|
||||
}
|
||||
|
||||
static hash(data) {
|
||||
return SHA256(ChainUtil.stableStringify(data)).toString();
|
||||
}
|
||||
|
||||
static verifySignature(publicKey, signature, dataHash) {
|
||||
//TODO, validate signature object
|
||||
if (!ec.keyFromPublic(publicKey, 'hex').verify(dataHash, signature)) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Couldn't verify signature"
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
result: true
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
static deserializeKeyPair(serialized) {
|
||||
return ec.keyFromPrivate(serialized, 'hex');
|
||||
}
|
||||
|
||||
static serializeKeyPair(keyPair) {
|
||||
return keyPair.getPrivate().toString('hex');
|
||||
}
|
||||
|
||||
//stable stringify for hashing
|
||||
static stableStringify(object) {
|
||||
|
||||
if (object instanceof Array) {
|
||||
let returning = '[';
|
||||
|
||||
for (let i = 0; i < object.length; i++) {
|
||||
if (typeof object[i] === "undefined" || object[i] === null) {
|
||||
returning += "null";
|
||||
} else {
|
||||
returning += ChainUtil.stableStringify(object[i]);
|
||||
}
|
||||
if (i !== object.length - 1) {
|
||||
returning += ',';
|
||||
}
|
||||
}
|
||||
|
||||
returning += ']';
|
||||
return returning;
|
||||
}
|
||||
if (!(object instanceof Object)) {
|
||||
return JSON.stringify(object);
|
||||
}
|
||||
|
||||
let returning = '{';
|
||||
|
||||
const keys = Object.keys(object).sort();
|
||||
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
const key = keys[i];
|
||||
if (!object.hasOwnProperty(key)) {
|
||||
continue;
|
||||
}
|
||||
if (typeof object[key] === "undefined" || object[key] === null) {
|
||||
continue;
|
||||
} else {
|
||||
returning += `"${key}":${ChainUtil.stableStringify(object[key])}`;
|
||||
if (i !== keys.length - 1) {
|
||||
returning += ',';
|
||||
}
|
||||
}
|
||||
}
|
||||
returning += '}';
|
||||
return returning;
|
||||
}
|
||||
|
||||
static validateExists(t) {
|
||||
if (typeof t === "undefined") {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Is undefined"
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
result: true
|
||||
};
|
||||
}
|
||||
|
||||
static validateIsObject(t) {
|
||||
if (typeof t === "undefined") {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Is undefined"
|
||||
};
|
||||
}
|
||||
if (typeof t !== 'object') {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Is not an object"
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
result: true
|
||||
};
|
||||
}
|
||||
|
||||
static validateIsString(t) {
|
||||
if (typeof t === "undefined") {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Is undefined"
|
||||
};
|
||||
}
|
||||
if (typeof t === 'string') {
|
||||
return {
|
||||
result: true
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Is not string"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
static validateIsNumber(t) {
|
||||
if (typeof t === "undefined") {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Is undefined"
|
||||
};
|
||||
}
|
||||
if (typeof t !== 'number') {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Is not number"
|
||||
};
|
||||
}
|
||||
return {
|
||||
result: true
|
||||
};
|
||||
}
|
||||
|
||||
static validateIsInteger(t) {
|
||||
if (typeof t === "undefined") {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Is undefined"
|
||||
};
|
||||
}
|
||||
if (typeof t !== 'number') {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Is not number"
|
||||
};
|
||||
|
||||
} else if (!Number.isInteger(t)) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Is not integer"
|
||||
};
|
||||
}
|
||||
return {
|
||||
result: true
|
||||
};
|
||||
}
|
||||
|
||||
//includes minimum
|
||||
static validateIsIntegerWithMin(t, minimum) {
|
||||
if (typeof t === "undefined") {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Is undefined"
|
||||
};
|
||||
}
|
||||
if (typeof t !== 'number') {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Is not number"
|
||||
};
|
||||
} else if (!Number.isInteger(t)) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Is not integer"
|
||||
};
|
||||
} else if (t < minimum) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Is below minimum"
|
||||
}
|
||||
}
|
||||
return {
|
||||
result: true
|
||||
};
|
||||
}
|
||||
|
||||
//includes minimum
|
||||
static createValidateIsIntegerWithMin(minimum) {
|
||||
return (t) => {
|
||||
return ChainUtil.validateIsIntegerWithMin(t, minimum);
|
||||
};
|
||||
}
|
||||
|
||||
static validateIsPublicKey(t) {
|
||||
//TODO
|
||||
return {
|
||||
result: true
|
||||
};
|
||||
}
|
||||
|
||||
static validateIsSignature(t) {
|
||||
//TODO
|
||||
return {
|
||||
result: true
|
||||
};
|
||||
}
|
||||
|
||||
static validateArray(t, memberValidator) {
|
||||
if (typeof t === "undefined") {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Is undefined"
|
||||
};
|
||||
}
|
||||
if (!(t instanceof Array)) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Is not an Array"
|
||||
};
|
||||
}
|
||||
for (const member of t) {
|
||||
const res = memberValidator(member);
|
||||
if (!res.result) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Array member validation failed: " + res.reason
|
||||
};
|
||||
}
|
||||
}
|
||||
return {
|
||||
result: true
|
||||
};
|
||||
}
|
||||
|
||||
static createValidateArray(memberValidator) {
|
||||
return function (t) {
|
||||
return ChainUtil.validateArray(t, memberValidator);
|
||||
};
|
||||
}
|
||||
|
||||
static validateObject(t, memberValidator) {
|
||||
if (typeof t === "undefined") {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Is undefined"
|
||||
};
|
||||
}
|
||||
if (!(t instanceof Object)) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Is not an object"
|
||||
};
|
||||
}
|
||||
|
||||
for (const key in memberValidator) {
|
||||
const validator = memberValidator[key];
|
||||
|
||||
//ALLOW OPTIONAL KEYS
|
||||
//if (!(key in t)) {
|
||||
// return {
|
||||
// result: false,
|
||||
// reason: "Couldn't find key: " + key
|
||||
// };
|
||||
//}
|
||||
|
||||
const res = validator(t[key]);
|
||||
|
||||
if (!res.result) {
|
||||
return {
|
||||
result: false,
|
||||
reason: `Validator for key '${key}' failed: ${res.reason}`
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
for (const key in t) {
|
||||
if (!(key in memberValidator)) {
|
||||
return {
|
||||
result: false,
|
||||
reason: "Verifying has key not in validators"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
result: true
|
||||
};
|
||||
}
|
||||
|
||||
static createValidateObject(memberValidator) {
|
||||
return function (t) {
|
||||
return ChainUtil.validateObject(t, memberValidator);
|
||||
};
|
||||
}
|
||||
|
||||
static createValidateOptional(validator) {
|
||||
return function (t) {
|
||||
if (typeof t === "undefined") {
|
||||
return {
|
||||
result: true
|
||||
};
|
||||
}
|
||||
|
||||
return validator(t);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ChainUtil;
|
39
util/config.js
Normal file
39
util/config.js
Normal file
|
@ -0,0 +1,39 @@
|
|||
const fs = require('fs');
|
||||
const process = require('process');
|
||||
|
||||
class Config {
|
||||
constructor(location, disallow_arg_overide) {
|
||||
//possible race if deleted after check, but we live with it I guess
|
||||
|
||||
const looking = location;
|
||||
|
||||
if (typeof disallow_arg_overide === undefined || disallow_arg_overide === null || !disallow_arg_overide) {
|
||||
const args = process.argv.slice(2);
|
||||
if (args.length > 0) {
|
||||
looking = args[0];
|
||||
}
|
||||
}
|
||||
|
||||
if (fs.existsSync(looking)) {
|
||||
const rawSettings = fs.readFileSync(looking, 'utf8');
|
||||
this.settings = JSON.parse(rawSettings);
|
||||
} else {
|
||||
this.settings = {};
|
||||
}
|
||||
}
|
||||
|
||||
get(config) {
|
||||
if (this.settings.hasOwnProperty(config.key)) {
|
||||
const value = this.settings[config.key];
|
||||
if (config.hasOwnProperty('transform')) {
|
||||
return config.transform(value);
|
||||
} else {
|
||||
return value;
|
||||
}
|
||||
} else {
|
||||
return config.default;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Config;
|
60
util/constants.js
Normal file
60
util/constants.js
Normal file
|
@ -0,0 +1,60 @@
|
|||
const DIFFICULTY = 3;
|
||||
const MINE_RATE = 3000;
|
||||
const MINING_REWARD = 50;
|
||||
|
||||
const SENSHAMART_URI_PREFIX = "SSM://";
|
||||
const SENSHAMART_URI_REPLACE = "SSMS://";
|
||||
|
||||
const DEFAULT_UI_HTML = "./ui/wallet-ui.html";
|
||||
const DEFAULT_UI_JS = "./ui/wallet-logic.js";
|
||||
const DEFAULT_DEMO_UI_HTML = "./ui/demo-ui.html";
|
||||
const DEFAULT_DEMO_UI_JS = "./ui/demo-logic.js";
|
||||
|
||||
const DEFAULT_PORT_MINER_BASE = 3000;
|
||||
const DEFAULT_PORT_MINER_API = DEFAULT_PORT_MINER_BASE + 1;
|
||||
const DEFAULT_PORT_MINER_CHAIN = DEFAULT_PORT_MINER_BASE + 2;
|
||||
const DEFAULT_PORT_MINER_TX_SHARE = DEFAULT_PORT_MINER_BASE + 3;
|
||||
const DEFAULT_PORT_MINER_TX_RECV = DEFAULT_PORT_MINER_BASE + 4;
|
||||
|
||||
|
||||
const DEFAULT_PORT_WALLET_BASE = 4000;
|
||||
const DEFAULT_PORT_WALLET_API = DEFAULT_PORT_WALLET_BASE + 1;
|
||||
const DEFAULT_PORT_WALLET_CHAIN = DEFAULT_PORT_WALLET_BASE + 2;
|
||||
|
||||
const DEFAULT_PORT_BROKER_BASE = 5000;
|
||||
const DEFAULT_PORT_BROKER_API = DEFAULT_PORT_BROKER_BASE + 1;
|
||||
const DEFAULT_PORT_BROKER_CHAIN = DEFAULT_PORT_BROKER_BASE + 2;
|
||||
const DEFAULT_PORT_BROKER_SENSOR_HANDSHAKE = DEFAULT_PORT_BROKER_BASE + 3;
|
||||
const DEFAULT_PORT_BROKER_MQTT = DEFAULT_PORT_BROKER_BASE + 4;
|
||||
|
||||
const DEFAULT_PORT_SENSOR_BASE = 6000;
|
||||
const DEFAULT_PORT_SENSOR_API = DEFAULT_PORT_SENSOR_BASE + 1;
|
||||
|
||||
module.exports = {
|
||||
DIFFICULTY,
|
||||
MINE_RATE,
|
||||
MINING_REWARD,
|
||||
|
||||
SENSHAMART_URI_PREFIX,
|
||||
SENSHAMART_URI_REPLACE,
|
||||
|
||||
DEFAULT_UI_HTML,
|
||||
DEFAULT_UI_JS,
|
||||
DEFAULT_DEMO_UI_HTML,
|
||||
DEFAULT_DEMO_UI_JS,
|
||||
|
||||
DEFAULT_PORT_MINER_API,
|
||||
DEFAULT_PORT_MINER_CHAIN,
|
||||
DEFAULT_PORT_MINER_TX_SHARE,
|
||||
DEFAULT_PORT_MINER_TX_RECV,
|
||||
|
||||
DEFAULT_PORT_WALLET_API,
|
||||
DEFAULT_PORT_WALLET_CHAIN,
|
||||
|
||||
DEFAULT_PORT_BROKER_API,
|
||||
DEFAULT_PORT_BROKER_CHAIN,
|
||||
DEFAULT_PORT_BROKER_SENSOR_HANDSHAKE,
|
||||
DEFAULT_PORT_BROKER_MQTT,
|
||||
|
||||
DEFAULT_PORT_SENSOR_API
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue