tool-db 2.6.2 → 2.6.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/bundle.js +1 -1
  2. package/dist/adapters-base/storageAdapter.js +1 -1
  3. package/dist/adapters-base/userAdapter.js +1 -1
  4. package/dist/crdt/counterCrdt.d.ts +2 -2
  5. package/dist/crdt/listCrdt.d.ts +2 -2
  6. package/dist/crdt/mapCrdt.d.ts +2 -2
  7. package/dist/messageHandlers/handlePut.js +101 -12
  8. package/dist/messageHandlers/handlePut.js.map +1 -1
  9. package/dist/toolDbAnonSignIn.js +1 -1
  10. package/dist/toolDbCheckSignupConflicts.d.ts +7 -0
  11. package/dist/toolDbCheckSignupConflicts.js +123 -0
  12. package/dist/toolDbCheckSignupConflicts.js.map +1 -0
  13. package/dist/toolDbKeysSignIn.js +1 -1
  14. package/dist/toolDbSignIn.js +1 -1
  15. package/dist/toolDbSignUp.d.ts +10 -0
  16. package/dist/toolDbSignUp.js +83 -54
  17. package/dist/toolDbSignUp.js.map +1 -1
  18. package/dist/toolDbVerificationWrapper.js +1 -1
  19. package/dist/tooldb.d.ts +18 -0
  20. package/dist/tooldb.js +120 -32
  21. package/dist/tooldb.js.map +1 -1
  22. package/dist/types/message.d.ts +2 -2
  23. package/dist/types/tooldb.d.ts +7 -7
  24. package/dist/utils/verifyMessage.js +11 -3
  25. package/dist/utils/verifyMessage.js.map +1 -1
  26. package/package.json +3 -3
  27. package/lib/adapters-base/networkAdapter.ts +0 -218
  28. package/lib/adapters-base/storageAdapter.ts +0 -57
  29. package/lib/adapters-base/userAdapter.ts +0 -49
  30. package/lib/crdt/baseCrdt.ts +0 -21
  31. package/lib/crdt/counterCrdt.ts +0 -112
  32. package/lib/crdt/listCrdt.ts +0 -190
  33. package/lib/crdt/mapCrdt.ts +0 -121
  34. package/lib/index.ts +0 -49
  35. package/lib/logger.ts +0 -30
  36. package/lib/messageHandlers/handleCrdtGet.ts +0 -34
  37. package/lib/messageHandlers/handleCrdtPut.ts +0 -133
  38. package/lib/messageHandlers/handleFunction.ts +0 -59
  39. package/lib/messageHandlers/handleGet.ts +0 -34
  40. package/lib/messageHandlers/handlePing.ts +0 -40
  41. package/lib/messageHandlers/handlePong.ts +0 -37
  42. package/lib/messageHandlers/handlePut.ts +0 -59
  43. package/lib/messageHandlers/handleQuery.ts +0 -30
  44. package/lib/messageHandlers/handleSubscribe.ts +0 -46
  45. package/lib/server.ts +0 -7
  46. package/lib/shared.ts +0 -5
  47. package/lib/toolDbAnonSignIn.ts +0 -5
  48. package/lib/toolDbClientOnMessage.ts +0 -79
  49. package/lib/toolDbCrdtGet.ts +0 -83
  50. package/lib/toolDbCrdtPut.ts +0 -78
  51. package/lib/toolDbFunction.ts +0 -49
  52. package/lib/toolDbGet.ts +0 -81
  53. package/lib/toolDbKeysSignIn.ts +0 -13
  54. package/lib/toolDbPut.ts +0 -84
  55. package/lib/toolDbQueryKeys.ts +0 -71
  56. package/lib/toolDbSignIn.ts +0 -37
  57. package/lib/toolDbSignUp.ts +0 -72
  58. package/lib/toolDbSubscribe.ts +0 -54
  59. package/lib/toolDbVerificationWrapper.ts +0 -55
  60. package/lib/tooldb.ts +0 -343
  61. package/lib/types/message.ts +0 -154
  62. package/lib/types/tooldb.ts +0 -140
  63. package/lib/utils/catchReturn.ts +0 -4
  64. package/lib/utils/encoding/arrayBufferToHex.ts +0 -18
  65. package/lib/utils/encoding/arrayBufferToString.ts +0 -4
  66. package/lib/utils/encoding/base64ToUint8.ts +0 -13
  67. package/lib/utils/encoding/fromBase64.ts +0 -4
  68. package/lib/utils/encoding/hexToArrayBuffer.ts +0 -13
  69. package/lib/utils/encoding/hexToString.ts +0 -8
  70. package/lib/utils/encoding/hexToUint8.ts +0 -5
  71. package/lib/utils/encoding/stringToArrayBuffer.ts +0 -4
  72. package/lib/utils/encoding/toBase64.ts +0 -4
  73. package/lib/utils/encoding/uint8ToBase64.ts +0 -11
  74. package/lib/utils/generateGroupKey.ts +0 -11
  75. package/lib/utils/getPeerSignature.ts +0 -12
  76. package/lib/utils/getTimestamp.ts +0 -3
  77. package/lib/utils/proofOfWork.ts +0 -31
  78. package/lib/utils/randomAnimal.ts +0 -77
  79. package/lib/utils/sha1.ts +0 -7
  80. package/lib/utils/sha256.ts +0 -7
  81. package/lib/utils/textRandom.ts +0 -11
  82. package/lib/utils/uniq.ts +0 -3
  83. package/lib/utils/verifyMessage.ts +0 -88
  84. package/lib/utils/verifyPeer.ts +0 -15
@@ -1,4 +0,0 @@
1
- export default function stringToArrayBuffer(str: string): ArrayBuffer {
2
- const encoder = new TextEncoder();
3
- return encoder.encode(str).buffer;
4
- }
@@ -1,4 +0,0 @@
1
- export default function toBase64(str: string) {
2
- return global.btoa(unescape(encodeURIComponent(str)));
3
- }
4
-
@@ -1,11 +0,0 @@
1
- import toBase64 from "./toBase64";
2
-
3
- export default function uint8ToBase64(byteArray: Uint8Array): string {
4
- let byteString = "";
5
- for (let i = 0; i < byteArray.byteLength; i += 1) {
6
- byteString += String.fromCodePoint(byteArray[i]);
7
- }
8
-
9
- return toBase64(byteString);
10
- }
11
-
@@ -1,11 +0,0 @@
1
- import arrayBufferToHex from "./encoding/arrayBufferToHex";
2
-
3
- /**
4
- * Generate a random 256-bit symmetric key for group encryption.
5
- * Returns the key as a hex string for easy storage and transmission.
6
- */
7
- export default function generateGroupKey(): string {
8
- const key = crypto.getRandomValues(new Uint8Array(32));
9
- return arrayBufferToHex(key.buffer);
10
- }
11
-
@@ -1,12 +0,0 @@
1
- import { ToolDbUserAdapter, sha256 } from "..";
2
-
3
- export default function getPeerSignature(
4
- account: ToolDbUserAdapter,
5
- topic: string,
6
- timestamp: number,
7
- host: string,
8
- port: number
9
- ) {
10
- const dataToSign = sha256(`${topic}-${timestamp}-${host}:${port}`);
11
- return account.signData(dataToSign);
12
- }
@@ -1,3 +0,0 @@
1
- export default function getTimestamp() {
2
- return new Date().getTime();
3
- }
@@ -1,31 +0,0 @@
1
- import { sha256 } from "..";
2
-
3
- /**
4
- * Calculates proof of work by finding a nonce that produces a hash
5
- * with the required number of leading zeroes.
6
- * @param value The value to hash
7
- * @param difficulty Number of leading zeroes required. Pass null to skip POW entirely.
8
- * @returns Promise with nonce and hash
9
- */
10
- export default function proofOfWork(
11
- value: string,
12
- difficulty: number | null
13
- ): Promise<{ nonce: number; hash: string }> {
14
- return new Promise((resolve) => {
15
- // When difficulty is null, skip POW calculation entirely
16
- // Just return hash with nonce 0 (useful for testing/CI)
17
- if (difficulty === null || difficulty === 0) {
18
- const hash = sha256(`${value}0`);
19
- resolve({ nonce: 0, hash });
20
- return;
21
- }
22
-
23
- let nonce = 0;
24
- let hash = sha256(`${value}${nonce}`);
25
- while (hash.substring(0, difficulty) !== Array(difficulty + 1).join("0")) {
26
- nonce += 1;
27
- hash = sha256(`${value}${nonce}`);
28
- }
29
- resolve({ nonce, hash });
30
- });
31
- }
@@ -1,77 +0,0 @@
1
- const adjectives = [
2
- "Fast",
3
- "Slow",
4
- "Cute",
5
- "Evil",
6
- "Anxious",
7
- "Cursed",
8
- "Average",
9
- "Zombie",
10
- "Smart",
11
- "Small",
12
- "Tall",
13
- "Amazing",
14
- "Splendid",
15
- "Fat",
16
- "Invisible",
17
- "Regular",
18
- "Domestic",
19
- "Unique",
20
- "Soft",
21
- "Lazy",
22
- "Angry",
23
- "Relaxed",
24
- "Huge",
25
- "Shy",
26
- "Playful",
27
- "Creepy",
28
- "Ancient",
29
- "Beautiful",
30
- ];
31
-
32
- const animals = [
33
- "Snake",
34
- "Monkey",
35
- "Platypus",
36
- "Fox",
37
- "Lynx",
38
- "Pug",
39
- "Chicken",
40
- "Slug",
41
- "Snail",
42
- "Pig",
43
- "Cow",
44
- "Sheep",
45
- "Horse",
46
- "Squirrel",
47
- "Turtle",
48
- "Unicorn",
49
- "Dragon",
50
- "Dolphin",
51
- "Cat",
52
- "Chow Chow",
53
- "Elephant",
54
- "Meerkat",
55
- "Polar Bear",
56
- "Bear",
57
- "Rabbit",
58
- "Koala",
59
- "Parrot",
60
- "Raven",
61
- "Frog",
62
- "Rat",
63
- "Mouse",
64
- "Bee",
65
- "Tiger",
66
- "Lion",
67
- "Giraffe",
68
- "Ant",
69
- "Spider",
70
- "Zebra",
71
- ];
72
-
73
- export default function randomAnimal() {
74
- return `${adjectives[Math.floor(Math.random() * adjectives.length)]} ${
75
- animals[Math.floor(Math.random() * animals.length)]
76
- }`;
77
- }
package/lib/utils/sha1.ts DELETED
@@ -1,7 +0,0 @@
1
- import crypto from "crypto";
2
-
3
- export default function sha1(str: string): string {
4
- const hash = crypto.createHash("sha1");
5
- hash.update(str);
6
- return hash.digest("hex");
7
- }
@@ -1,7 +0,0 @@
1
- import crypto, { BinaryLike } from "crypto";
2
-
3
- export default function sha256(str: BinaryLike): string {
4
- const hash = crypto.createHash("sha256");
5
- hash.update(str);
6
- return hash.digest("hex");
7
- }
@@ -1,11 +0,0 @@
1
- export default function textRandom(_l = 24, _c?: string): string {
2
- let l = _l;
3
- let s = "";
4
- const c =
5
- _c || "0123456789ABCDEFGHIJKLMNOPQRSTUVWXZabcdefghijklmnopqrstuvwxyz";
6
- while (l > 0) {
7
- s += c.charAt(Math.floor(Math.random() * c.length));
8
- l -= 1;
9
- }
10
- return s;
11
- }
package/lib/utils/uniq.ts DELETED
@@ -1,3 +0,0 @@
1
- export default function uniq<T>(array: T[]): T[] {
2
- return Array.from(new Set(array));
3
- }
@@ -1,88 +0,0 @@
1
- import { ToolDb, VerifyResult, VerificationData, sha256 } from "..";
2
-
3
- /**
4
- * Verifies a message validity (PoW, Address, timestamp, signatures)
5
- * @param msg AnyMessage
6
- * @param pow amount of proof of work required, number of leading zeroes (default is 0/no pow)
7
- * @returns boolean or undefined if the message type does not match
8
- */
9
- export default async function verifyMessage<T>(
10
- this: ToolDb,
11
- msg: Partial<VerificationData<T>>,
12
- pow: number | null = 0
13
- ): Promise<VerifyResult> {
14
- // this.logger("verify: ", msg);
15
- const strData = JSON.stringify(msg.v);
16
-
17
- if (
18
- msg.t === undefined ||
19
- msg.k === undefined ||
20
- msg.h === undefined ||
21
- msg.a === undefined ||
22
- msg.s === undefined ||
23
- msg.c === undefined
24
- ) {
25
- return VerifyResult.InvalidData;
26
- }
27
-
28
- // Max clock shift allowed is 30 seconds.
29
- // Ten seconds was my original threshold but it failed some times.
30
- if (msg.t > new Date().getTime() + 30000) {
31
- // this.logger("Invalid message timestamp.");
32
- return VerifyResult.InvalidTimestamp;
33
- }
34
-
35
- // This is a user namespace
36
- let addressNamespace: false | string = false;
37
- if (msg.k.slice(0, 1) == ":") {
38
- addressNamespace = msg.k.split(".")[0].slice(1);
39
- }
40
-
41
- // This namespace can only be written if data does not exist previously
42
- // This violates the offline first principle..?
43
- if (msg.k.slice(0, 2) == "==") {
44
- const key = msg.k;
45
- const data = await this.store
46
- .get(key)
47
- .then((data) => {
48
- try {
49
- const message = JSON.parse(data);
50
- return message;
51
- } catch (e) {
52
- return null;
53
- }
54
- })
55
- .catch(() => {
56
- return null;
57
- });
58
- if (data && data.a !== msg.a) return VerifyResult.CantOverwrite;
59
- }
60
-
61
- if (addressNamespace && addressNamespace !== msg.a) {
62
- // this.logger("Provided address does not match");
63
- return VerifyResult.AddressMismatch;
64
- }
65
-
66
- // Verify hash and nonce (adjust zeroes for difficulty of the network)
67
- // While this POW does not enforce security per-se, it does make it harder
68
- // for attackers to spam the network, and could be adjusted by peers.
69
- // When pow is null, skip POW verification entirely (useful for testing/CI)
70
- if (pow !== null && pow > 0) {
71
- if (msg.h.slice(0, pow) !== new Array(pow).fill("0").join("")) {
72
- // this.logger("No valid hash (no pow)");
73
- return VerifyResult.NoProofOfWork;
74
- }
75
-
76
- if (sha256(`${strData}${msg.a}${msg.t}${msg.n}`) !== msg.h) {
77
- // this.logger("Specified hash does not generate a valid pow");
78
- return VerifyResult.InvalidHashNonce;
79
- }
80
- }
81
-
82
- const verified = this.userAccount
83
- ? await this.userAccount.verifySignature(msg)
84
- : false;
85
- // this.logger(`Signature validation: ${verified ? "Sucess" : "Failed"}`);
86
-
87
- return verified ? VerifyResult.Verified : VerifyResult.InvalidSignature;
88
- }
@@ -1,15 +0,0 @@
1
- import { ToolDb, sha256, Peer } from "..";
2
-
3
- export default function verifyPeer(tooldb: ToolDb, peer: Peer) {
4
- const data = sha256(
5
- `${peer.topic}-${peer.timestamp}-${peer.host}:${peer.port}`
6
- );
7
-
8
- // its not really a message but this function works with a Partial
9
- // I want to keep it like this so the signature verification is in a single place
10
- return tooldb.verifyMessage({
11
- h: data,
12
- a: peer.address,
13
- s: peer.sig,
14
- });
15
- }