@browser-network/database
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -14,3 +14,3 @@ import { LocalDB, WrappedState } from './LocalDB'; | ||
networkId: t.IDString; | ||
clientId: t.IDString; | ||
address: t.IDString; | ||
publicKey: t.PublicKey; | ||
@@ -24,3 +24,3 @@ secret: string; | ||
set(state: S): Promise<void>; | ||
get(clientId: t.IDString): WrappedState<S> | undefined; | ||
get(address: t.IDString): WrappedState<S> | undefined; | ||
getAll: () => WrappedState<unknown>[]; | ||
@@ -27,0 +27,0 @@ onChange(handler: () => void): void; |
@@ -60,4 +60,12 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
var __rest = (this && this.__rest) || function (s, e) { | ||
var t = {}; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) | ||
t[p] = s[p]; | ||
if (s != null && typeof Object.getOwnPropertySymbols === "function") | ||
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { | ||
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) | ||
t[p[i]] = s[p[i]]; | ||
} | ||
return t; | ||
}; | ||
@@ -70,3 +78,3 @@ (function (factory) { | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports", "crypto", "eccrypto", "./LocalDB", "./util"], factory); | ||
define(["require", "exports", "@browser-network/crypto", "./LocalDB", "./util"], factory); | ||
} | ||
@@ -77,22 +85,22 @@ })(function (require, exports) { | ||
exports.Dbdb = void 0; | ||
var crypto_1 = __importDefault(require("crypto")); | ||
var eccrypto = __importStar(require("eccrypto")); | ||
var bnc = __importStar(require("@browser-network/crypto")); | ||
var LocalDB_1 = require("./LocalDB"); | ||
var util_1 = require("./util"); | ||
var debug = (0, util_1.debugFactory)('Dbdb'); | ||
var btos = function (buffer) { return buffer.toString('hex'); }; | ||
var stob = function (str) { return Buffer.from(str, 'hex'); }; | ||
var hash = function (data) { return crypto_1["default"].createHash('sha256').update(JSON.stringify(data)).digest(); }; | ||
var wrapState = function (state, clientId, pub, priv) { return __awaiter(void 0, void 0, void 0, function () { | ||
var wrapped; | ||
// convenience | ||
var wrapState = function (state, address, pub, priv) { return __awaiter(void 0, void 0, void 0, function () { | ||
var wrapp, signature, wrapped; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, signObject(priv, { | ||
id: clientId, | ||
case 0: | ||
wrapp = { | ||
id: address, | ||
timestamp: Date.now(), | ||
state: state, | ||
publicKey: pub | ||
})]; | ||
}; | ||
return [4 /*yield*/, bnc.sign(priv, wrapp)]; | ||
case 1: | ||
wrapped = _a.sent(); | ||
signature = _a.sent(); | ||
wrapped = Object.assign(wrapp, { signature: signature }); | ||
return [2 /*return*/, wrapped]; | ||
@@ -102,43 +110,7 @@ } | ||
}); }; | ||
var signObject = function (secret, obj) { return __awaiter(void 0, void 0, void 0, function () { | ||
var stringState, hashBuff, keyBuff, sigBuff, signature; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
stringState = JSON.stringify(obj); | ||
hashBuff = hash(stringState); | ||
keyBuff = stob(secret); | ||
return [4 /*yield*/, eccrypto.sign(keyBuff, hashBuff)]; | ||
case 1: | ||
sigBuff = _a.sent(); | ||
signature = btos(sigBuff); | ||
return [2 /*return*/, Object.assign({}, obj, { signature: signature })]; | ||
} | ||
}); | ||
}); }; | ||
var verifySignature = function (update) { return __awaiter(void 0, void 0, void 0, function () { | ||
var updateSansSig, stringState, hashBuff, pubBuff, sigBuff, e_1; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
updateSansSig = Object.assign({}, update); | ||
delete updateSansSig.signature; | ||
stringState = JSON.stringify(updateSansSig); | ||
hashBuff = hash(stringState); | ||
pubBuff = stob(update.publicKey); | ||
sigBuff = stob(update.signature); | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 3, , 4]); | ||
return [4 /*yield*/, eccrypto.verify(pubBuff, hashBuff, sigBuff)]; | ||
case 2: | ||
_a.sent(); | ||
return [2 /*return*/, true]; | ||
case 3: | ||
e_1 = _a.sent(); | ||
return [2 /*return*/, false]; | ||
case 4: return [2 /*return*/]; | ||
} | ||
}); | ||
}); }; | ||
// convenience | ||
var verifySignature = function (update) { | ||
var signature = update.signature, wrapp = __rest(update, ["signature"]); | ||
return bnc.verifySignature(wrapp, signature, update.publicKey); | ||
}; | ||
var Dbdb = /** @class */ (function () { | ||
@@ -186,4 +158,4 @@ function Dbdb(_a) { | ||
this.appId = appId; | ||
this.localDB = new LocalDB_1.LocalDB(this.networkId); | ||
this.clientId = network.clientId; | ||
this.localDB = new LocalDB_1.LocalDB(appId); | ||
this.address = network.address; | ||
this.secret = secret; | ||
@@ -198,3 +170,3 @@ this.network = network; | ||
// Here we derive the pub key from the private | ||
this.publicKey = btos(eccrypto.getPublicCompressed(stob(secret))); | ||
this.publicKey = bnc.derivePubKey(secret); | ||
setInterval(this.broadcastStateOfferings, 5000); | ||
@@ -207,3 +179,3 @@ } | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, wrapState(state, this.clientId, this.publicKey, this.secret)]; | ||
case 0: return [4 /*yield*/, wrapState(state, this.address, this.publicKey, this.secret)]; | ||
case 1: | ||
@@ -219,4 +191,4 @@ data = _a.sent(); | ||
}; | ||
Dbdb.prototype.get = function (clientId) { | ||
return this.localDB.get(clientId); | ||
Dbdb.prototype.get = function (address) { | ||
return this.localDB.get(address); | ||
}; | ||
@@ -231,13 +203,12 @@ // This will fire every time we update our state. This way reactive | ||
Dbdb.generateSecret = function () { | ||
var privBuf = eccrypto.generatePrivate(); | ||
return btos(privBuf); | ||
return bnc.generateSecret(); | ||
}; | ||
Dbdb.prototype.onStateOffering = function (offerings) { | ||
var _this = this; | ||
var requestState = function (clientId) { | ||
var requestState = function (address) { | ||
_this.network.broadcast({ | ||
type: 'state-request', | ||
appId: _this.appId, | ||
destination: clientId, | ||
data: clientId | ||
destination: address, | ||
data: address | ||
}); | ||
@@ -244,0 +215,0 @@ }; |
@@ -9,15 +9,14 @@ import * as t from './types.d'; | ||
}; | ||
declare type SupportedMetaKeys = 'clientId'; | ||
declare const buildStorageShim: () => Storage; | ||
export declare class LocalDB { | ||
keyPrefix: string; | ||
localStorage: ReturnType<typeof buildStorageShim>; | ||
appId: t.GUID; | ||
constructor(appId: t.GUID); | ||
set(val: WrappedState, clientId: t.IDString): void; | ||
get(clientId: t.IDString): WrappedState | undefined; | ||
set(val: WrappedState, address: t.IDString): void; | ||
get(address: t.IDString): WrappedState | undefined; | ||
getAll(): WrappedState[]; | ||
setMeta(key: SupportedMetaKeys, val: string): void; | ||
getMeta(key: SupportedMetaKeys): string; | ||
buildKey(clientId: t.IDString): string; | ||
buildKey(address: t.IDString): string; | ||
private getByKey; | ||
} | ||
export {}; |
@@ -13,13 +13,32 @@ (function (factory) { | ||
exports.LocalDB = void 0; | ||
var buildStorageShim = function () { | ||
// @ts-ignore | ||
var localStorageShim = {}; | ||
localStorageShim.setItem = function (key, val) { | ||
localStorageShim[key] = val; | ||
}; | ||
localStorageShim.getItem = function (key) { | ||
return localStorageShim[key]; | ||
}; | ||
return localStorageShim; | ||
}; | ||
var LocalDB = /** @class */ (function () { | ||
function LocalDB(appId) { | ||
var _this = this; | ||
this.keyPrefix = 'dbdb'; | ||
this.getByKey = function (key) { | ||
var gotten = _this.localStorage.getItem(key); | ||
if (!gotten) | ||
return null; | ||
return JSON.parse(gotten); | ||
}; | ||
this.appId = appId; | ||
this.localStorage = globalThis.localStorage || buildStorageShim(); | ||
} | ||
LocalDB.prototype.set = function (val, clientId) { | ||
var key = this.buildKey(clientId); | ||
localStorage.setItem(key, JSON.stringify(val)); | ||
LocalDB.prototype.set = function (val, address) { | ||
var key = this.buildKey(address); | ||
this.localStorage.setItem(key, JSON.stringify(val)); | ||
}; | ||
LocalDB.prototype.get = function (clientId) { | ||
var key = this.buildKey(clientId); | ||
LocalDB.prototype.get = function (address) { | ||
var key = this.buildKey(address); | ||
return this.getByKey(key); | ||
@@ -29,18 +48,9 @@ }; | ||
var _this = this; | ||
var lsKeys = Object.keys(localStorage); | ||
var lsKeys = Object.keys(this.localStorage); | ||
var ourKeys = lsKeys.filter(function (key) { return key.indexOf(_this.buildKey('')) > -1; }); | ||
return ourKeys.map(this.getByKey); | ||
}; | ||
LocalDB.prototype.setMeta = function (key, val) { | ||
return localStorage.setItem(key, val); | ||
LocalDB.prototype.buildKey = function (address) { | ||
return "".concat(this.keyPrefix, "-").concat(this.appId, "-").concat(address); | ||
}; | ||
LocalDB.prototype.getMeta = function (key) { | ||
return localStorage.getItem(key); | ||
}; | ||
LocalDB.prototype.buildKey = function (clientId) { | ||
return "".concat(this.keyPrefix, "-").concat(this.appId, "-").concat(clientId); | ||
}; | ||
LocalDB.prototype.getByKey = function (key) { | ||
return JSON.parse(localStorage.getItem(key)); | ||
}; | ||
return LocalDB; | ||
@@ -47,0 +57,0 @@ }()); |
@@ -28,3 +28,3 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
} | ||
if (window['DEBUG'] >= logLevel) { | ||
if (globalThis['DEBUG'] >= logLevel) { | ||
console.log.apply(console, __spreadArray(["[".concat(logLevel, "] ").concat(appName, ": ")], args, false)); | ||
@@ -31,0 +31,0 @@ } |
{ | ||
"name": "@browser-network/database", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "A type of distributed database built on top of the distributed browser-network", | ||
@@ -12,2 +12,3 @@ "main": "./dist/src/index.js", | ||
"scripts": { | ||
"test": "tap --ts --no-timeout --no-coverage test/*.ts", | ||
"clean": "shx rm -rf dist build umd; shx mkdir umd", | ||
@@ -42,11 +43,12 @@ "compile:ts": "tsc", | ||
], | ||
"license": "MIT", | ||
"dependencies": { | ||
"@browser-network/crypto": "^0.0.1", | ||
"@browser-network/network": "^0.0.1", | ||
"eccrypto": "^1.1.6", | ||
"uuid": "^8.3.2" | ||
"eccrypto": "^1.1.6" | ||
}, | ||
"devDependencies": { | ||
"@types/eccrypto": "^1.1.3", | ||
"@types/node": "^13.9.0", | ||
"@types/uuid": "^8.3.4", | ||
"@types/node": "^16", | ||
"@types/tap": "^15.0.7", | ||
"browserify": "^17.0.0", | ||
@@ -57,2 +59,5 @@ "nodemon": "^2.0.15", | ||
"shx": "^0.3.4", | ||
"tap": "^16.1.0", | ||
"tap-spec": "^5.0.0", | ||
"ts-node": "^10.7.0", | ||
"typescript": "^4.4.4", | ||
@@ -59,0 +64,0 @@ "uglify-js": "^3.15.3" |
@@ -61,2 +61,3 @@ # Distributed Browser Database (Dbdb) | ||
<script src="//unpkg.com/@browser-network/crypto/umd/crypto.min.js"></script> | ||
<script src="//unpkg.com/@browser-network/network/umd/network.min.js"></script> | ||
@@ -74,3 +75,3 @@ <script src="//unpkg.com/@browser-network/database/umd/dbdb.min.js"></script> | ||
network: network, | ||
secret: Dbdb.Dbdb.generateSecret(), | ||
secret: Bnc.generateSecret(), | ||
appId: 'dbdb-app-id' // this just needs to be unique for your app, which it is for this | ||
@@ -77,0 +78,0 @@ }) |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1269854
23906
193
13
+ Added@browser-network/crypto@0.0.1(transitive)
- Removeduuid@^8.3.2