tool-db 2.3.1 → 2.5.0

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 (615) hide show
  1. package/README.md +1 -74
  2. package/lib/adapters-base/networkAdapter.ts +215 -0
  3. package/lib/adapters-base/storageAdapter.ts +35 -0
  4. package/lib/adapters-base/userAdapter.ts +49 -0
  5. package/lib/crdt/baseCrdt.ts +21 -0
  6. package/lib/crdt/counterCrdt.ts +111 -0
  7. package/lib/crdt/listCrdt.ts +190 -0
  8. package/lib/crdt/mapCrdt.ts +119 -0
  9. package/{dist/index.d.ts → lib/index.ts} +8 -5
  10. package/lib/logger.ts +30 -0
  11. package/lib/messageHandlers/handleCrdtGet.ts +29 -0
  12. package/lib/messageHandlers/handleCrdtPut.ts +118 -0
  13. package/lib/messageHandlers/handleGet.ts +29 -0
  14. package/lib/messageHandlers/handlePing.ts +40 -0
  15. package/lib/messageHandlers/handlePong.ts +30 -0
  16. package/lib/messageHandlers/handlePut.ts +54 -0
  17. package/lib/messageHandlers/handleQuery.ts +25 -0
  18. package/lib/messageHandlers/handleSubscribe.ts +46 -0
  19. package/lib/server.ts +7 -0
  20. package/lib/shared.ts +5 -0
  21. package/lib/toolDbAnonSignIn.ts +5 -0
  22. package/lib/toolDbClientOnMessage.ts +75 -0
  23. package/lib/toolDbCrdtGet.ts +82 -0
  24. package/lib/toolDbCrdtPut.ts +77 -0
  25. package/lib/toolDbGet.ts +80 -0
  26. package/lib/toolDbKeysSignIn.ts +16 -0
  27. package/lib/toolDbPut.ts +83 -0
  28. package/lib/toolDbQueryKeys.ts +64 -0
  29. package/lib/toolDbSignIn.ts +31 -0
  30. package/lib/toolDbSignUp.ts +71 -0
  31. package/lib/toolDbSubscribe.ts +53 -0
  32. package/lib/toolDbVerificationWrapper.ts +55 -0
  33. package/lib/tooldb.ts +314 -0
  34. package/lib/types/message.ts +133 -0
  35. package/lib/types/tooldb.ts +89 -0
  36. package/lib/utils/catchReturn.ts +4 -0
  37. package/lib/utils/encoding/arrayBufferToHex.ts +18 -0
  38. package/lib/utils/encoding/arrayBufferToString.ts +8 -0
  39. package/lib/utils/encoding/hexToArrayBuffer.ts +13 -0
  40. package/lib/utils/encoding/hexToString.ts +8 -0
  41. package/lib/utils/encoding/hexToUint8.ts +5 -0
  42. package/lib/utils/encoding/stringToArrayBuffer.ts +8 -0
  43. package/lib/utils/getPeerSignature.ts +12 -0
  44. package/lib/utils/getTimestamp.ts +3 -0
  45. package/lib/utils/proofOfWork.ts +16 -0
  46. package/lib/utils/randomAnimal.ts +77 -0
  47. package/lib/utils/sha1.ts +7 -0
  48. package/lib/utils/sha256.ts +7 -0
  49. package/lib/utils/textRandom.ts +11 -0
  50. package/lib/utils/uniq.ts +3 -0
  51. package/lib/utils/verifyMessage.ts +88 -0
  52. package/lib/utils/verifyPeer.ts +15 -0
  53. package/package.json +31 -65
  54. package/tsconfig.json +10 -72
  55. package/.eslintrc.json +0 -74
  56. package/.github/workflows/main.yml +0 -29
  57. package/_____peer/000005.ldb +0 -0
  58. package/_____peer/000008.log +0 -0
  59. package/_____peer/CURRENT +0 -1
  60. package/_____peer/LOCK +0 -0
  61. package/_____peer/LOG +0 -0
  62. package/_____peer/LOG.old +0 -0
  63. package/_____peer/MANIFEST-000007 +0 -0
  64. package/_____peer_test-alice/000003.log +0 -0
  65. package/_____peer_test-alice/CURRENT +0 -1
  66. package/_____peer_test-alice/LOCK +0 -0
  67. package/_____peer_test-alice/LOG +0 -1
  68. package/_____peer_test-alice/MANIFEST-000002 +0 -0
  69. package/_____peer_test-base-client/000005.ldb +0 -0
  70. package/_____peer_test-base-client/000006.log +0 -0
  71. package/_____peer_test-base-client/CURRENT +0 -1
  72. package/_____peer_test-base-client/LOCK +0 -0
  73. package/_____peer_test-base-client/LOG +0 -5
  74. package/_____peer_test-base-client/LOG.old +0 -1
  75. package/_____peer_test-base-client/MANIFEST-000004 +0 -0
  76. package/_____peer_test-base-server/000005.ldb +0 -0
  77. package/_____peer_test-base-server/000006.log +0 -0
  78. package/_____peer_test-base-server/CURRENT +0 -1
  79. package/_____peer_test-base-server/LOCK +0 -0
  80. package/_____peer_test-base-server/LOG +0 -5
  81. package/_____peer_test-base-server/LOG.old +0 -1
  82. package/_____peer_test-base-server/MANIFEST-000004 +0 -0
  83. package/_____peer_test-bob/000003.log +0 -0
  84. package/_____peer_test-bob/CURRENT +0 -1
  85. package/_____peer_test-bob/LOCK +0 -0
  86. package/_____peer_test-bob/LOG +0 -1
  87. package/_____peer_test-bob/MANIFEST-000002 +0 -0
  88. package/_____peer_test-chris/000003.log +0 -0
  89. package/_____peer_test-chris/CURRENT +0 -1
  90. package/_____peer_test-chris/LOCK +0 -0
  91. package/_____peer_test-chris/LOG +0 -1
  92. package/_____peer_test-chris/MANIFEST-000002 +0 -0
  93. package/_____peer_test-node-a/000003.log +0 -0
  94. package/_____peer_test-node-a/CURRENT +0 -1
  95. package/_____peer_test-node-a/LOCK +0 -0
  96. package/_____peer_test-node-a/LOG +0 -1
  97. package/_____peer_test-node-a/MANIFEST-000002 +0 -0
  98. package/_____peer_test-node-b/000003.log +0 -0
  99. package/_____peer_test-node-b/CURRENT +0 -1
  100. package/_____peer_test-node-b/LOCK +0 -0
  101. package/_____peer_test-node-b/LOG +0 -1
  102. package/_____peer_test-node-b/MANIFEST-000002 +0 -0
  103. package/_____peer_test-store/000003.log +0 -0
  104. package/_____peer_test-store/CURRENT +0 -1
  105. package/_____peer_test-store/LOCK +0 -0
  106. package/_____peer_test-store/LOG +0 -1
  107. package/_____peer_test-store/MANIFEST-000002 +0 -0
  108. package/_____peer_test-verify-a/000003.log +0 -0
  109. package/_____peer_test-verify-a/CURRENT +0 -1
  110. package/_____peer_test-verify-a/LOCK +0 -0
  111. package/_____peer_test-verify-a/LOG +0 -1
  112. package/_____peer_test-verify-a/MANIFEST-000002 +0 -0
  113. package/babel.config.js +0 -6
  114. package/bundle.js +0 -1
  115. package/custom-test-env.js +0 -28
  116. package/dist/__tests__ copy/base64.d.ts +0 -1
  117. package/dist/__tests__ copy/base64.js +0 -61
  118. package/dist/__tests__ copy/base64.js.map +0 -1
  119. package/dist/__tests__ copy/encryption.d.ts +0 -1
  120. package/dist/__tests__ copy/encryption.js +0 -68
  121. package/dist/__tests__ copy/encryption.js.map +0 -1
  122. package/dist/__tests__ copy/keys.d.ts +0 -1
  123. package/dist/__tests__ copy/keys.js +0 -238
  124. package/dist/__tests__ copy/keys.js.map +0 -1
  125. package/dist/__tests__ copy/network.d.ts +0 -1
  126. package/dist/__tests__ copy/network.js +0 -206
  127. package/dist/__tests__ copy/network.js.map +0 -1
  128. package/dist/__tests__ copy/pow.d.ts +0 -1
  129. package/dist/__tests__ copy/pow.js +0 -25
  130. package/dist/__tests__ copy/pow.js.map +0 -1
  131. package/dist/__tests__ copy/sha1.d.ts +0 -1
  132. package/dist/__tests__ copy/sha1.js +0 -16
  133. package/dist/__tests__ copy/sha1.js.map +0 -1
  134. package/dist/__tests__ copy/sha256.d.ts +0 -1
  135. package/dist/__tests__ copy/sha256.js +0 -16
  136. package/dist/__tests__ copy/sha256.js.map +0 -1
  137. package/dist/__tests__ copy/textRandom.d.ts +0 -1
  138. package/dist/__tests__ copy/textRandom.js +0 -17
  139. package/dist/__tests__ copy/textRandom.js.map +0 -1
  140. package/dist/adapters/toolDbIndexedb.d.ts +0 -9
  141. package/dist/adapters/toolDbIndexedb.js +0 -131
  142. package/dist/adapters/toolDbIndexedb.js.map +0 -1
  143. package/dist/adapters/toolDbLeveldb.d.ts +0 -8
  144. package/dist/adapters/toolDbLeveldb.js +0 -109
  145. package/dist/adapters/toolDbLeveldb.js.map +0 -1
  146. package/dist/adapters/toolDbWeb3User.d.ts +0 -18
  147. package/dist/adapters/toolDbWeb3User.js +0 -74
  148. package/dist/adapters/toolDbWeb3User.js.map +0 -1
  149. package/dist/adapters/toolDbWebrtc.d.ts +0 -58
  150. package/dist/adapters/toolDbWebrtc.js +0 -487
  151. package/dist/adapters/toolDbWebrtc.js.map +0 -1
  152. package/dist/adapters/toolDbWebsocket.d.ts +0 -18
  153. package/dist/adapters/toolDbWebsocket.js +0 -176
  154. package/dist/adapters/toolDbWebsocket.js.map +0 -1
  155. package/dist/adapters-base/networkAdapter.d.ts +0 -58
  156. package/dist/adapters-base/networkAdapter.js +0 -214
  157. package/dist/adapters-base/networkAdapter.js.map +0 -1
  158. package/dist/adapters-base/storageAdapter.d.ts +0 -11
  159. package/dist/adapters-base/storageAdapter.js +0 -36
  160. package/dist/adapters-base/storageAdapter.js.map +0 -1
  161. package/dist/adapters-base/userAdapter.d.ts +0 -15
  162. package/dist/adapters-base/userAdapter.js +0 -42
  163. package/dist/adapters-base/userAdapter.js.map +0 -1
  164. package/dist/bundle.js +0 -69086
  165. package/dist/chain/Block.d.ts +0 -29
  166. package/dist/chain/Block.js +0 -142
  167. package/dist/chain/Block.js.map +0 -1
  168. package/dist/chain/ToolChain.d.ts +0 -27
  169. package/dist/chain/ToolChain.js +0 -253
  170. package/dist/chain/ToolChain.js.map +0 -1
  171. package/dist/chain/Tx.d.ts +0 -50
  172. package/dist/chain/Tx.js +0 -54
  173. package/dist/chain/Tx.js.map +0 -1
  174. package/dist/chain/Wallet.d.ts +0 -9
  175. package/dist/chain/Wallet.js +0 -37
  176. package/dist/chain/Wallet.js.map +0 -1
  177. package/dist/chain/isValidChain.d.ts +0 -1
  178. package/dist/chain/isValidChain.js +0 -37
  179. package/dist/chain/isValidChain.js.map +0 -1
  180. package/dist/chain/mempool.d.ts +0 -0
  181. package/dist/chain/mempool.js +0 -2
  182. package/dist/chain/mempool.js.map +0 -1
  183. package/dist/crdt/baseCrdt.d.ts +0 -9
  184. package/dist/crdt/baseCrdt.js +0 -27
  185. package/dist/crdt/baseCrdt.js.map +0 -1
  186. package/dist/crdt/counterCrdt.d.ts +0 -30
  187. package/dist/crdt/counterCrdt.js +0 -106
  188. package/dist/crdt/counterCrdt.js.map +0 -1
  189. package/dist/crdt/listCrdt.d.ts +0 -42
  190. package/dist/crdt/listCrdt.js +0 -159
  191. package/dist/crdt/listCrdt.js.map +0 -1
  192. package/dist/crdt/mapCrdt.d.ts +0 -32
  193. package/dist/crdt/mapCrdt.js +0 -118
  194. package/dist/crdt/mapCrdt.js.map +0 -1
  195. package/dist/getCrypto.d.ts +0 -1
  196. package/dist/getCrypto.js +0 -10
  197. package/dist/getCrypto.js.map +0 -1
  198. package/dist/index.js +0 -93
  199. package/dist/index.js.map +0 -1
  200. package/dist/infuraFilecoin.d.ts +0 -0
  201. package/dist/infuraFilecoin.js +0 -2
  202. package/dist/infuraFilecoin.js.map +0 -1
  203. package/dist/loadCrdtDocument.d.ts +0 -3
  204. package/dist/loadCrdtDocument.js +0 -43
  205. package/dist/loadCrdtDocument.js.map +0 -1
  206. package/dist/logger.d.ts +0 -2
  207. package/dist/logger.js +0 -29
  208. package/dist/logger.js.map +0 -1
  209. package/dist/makeTx.d.ts +0 -1
  210. package/dist/makeTx.js +0 -27
  211. package/dist/makeTx.js.map +0 -1
  212. package/dist/messageHandlers/handleCrdt.d.ts +0 -2
  213. package/dist/messageHandlers/handleCrdt.js +0 -15
  214. package/dist/messageHandlers/handleCrdt.js.map +0 -1
  215. package/dist/messageHandlers/handleCrdtGet.d.ts +0 -2
  216. package/dist/messageHandlers/handleCrdtGet.js +0 -29
  217. package/dist/messageHandlers/handleCrdtGet.js.map +0 -1
  218. package/dist/messageHandlers/handleCrdtPut.d.ts +0 -2
  219. package/dist/messageHandlers/handleCrdtPut.js +0 -93
  220. package/dist/messageHandlers/handleCrdtPut.js.map +0 -1
  221. package/dist/messageHandlers/handleGet.d.ts +0 -2
  222. package/dist/messageHandlers/handleGet.js +0 -29
  223. package/dist/messageHandlers/handleGet.js.map +0 -1
  224. package/dist/messageHandlers/handleJoin.d.ts +0 -2
  225. package/dist/messageHandlers/handleJoin.js +0 -36
  226. package/dist/messageHandlers/handleJoin.js.map +0 -1
  227. package/dist/messageHandlers/handlePing.d.ts +0 -2
  228. package/dist/messageHandlers/handlePing.js +0 -36
  229. package/dist/messageHandlers/handlePing.js.map +0 -1
  230. package/dist/messageHandlers/handlePong.d.ts +0 -2
  231. package/dist/messageHandlers/handlePong.js +0 -26
  232. package/dist/messageHandlers/handlePong.js.map +0 -1
  233. package/dist/messageHandlers/handlePut.d.ts +0 -2
  234. package/dist/messageHandlers/handlePut.js +0 -57
  235. package/dist/messageHandlers/handlePut.js.map +0 -1
  236. package/dist/messageHandlers/handleQuery.d.ts +0 -2
  237. package/dist/messageHandlers/handleQuery.js +0 -23
  238. package/dist/messageHandlers/handleQuery.js.map +0 -1
  239. package/dist/messageHandlers/handleServers.d.ts +0 -2
  240. package/dist/messageHandlers/handleServers.js +0 -23
  241. package/dist/messageHandlers/handleServers.js.map +0 -1
  242. package/dist/messageHandlers/handleSubscribe.d.ts +0 -2
  243. package/dist/messageHandlers/handleSubscribe.js +0 -44
  244. package/dist/messageHandlers/handleSubscribe.js.map +0 -1
  245. package/dist/sendJoin.d.ts +0 -2
  246. package/dist/sendJoin.js +0 -31
  247. package/dist/sendJoin.js.map +0 -1
  248. package/dist/server.d.ts +0 -1
  249. package/dist/server.js +0 -9
  250. package/dist/server.js.map +0 -1
  251. package/dist/shared.d.ts +0 -2
  252. package/dist/shared.js +0 -7
  253. package/dist/shared.js.map +0 -1
  254. package/dist/test.d.ts +0 -1
  255. package/dist/test.js +0 -17
  256. package/dist/test.js.map +0 -1
  257. package/dist/toolChainServer.d.ts +0 -8
  258. package/dist/toolChainServer.js +0 -60
  259. package/dist/toolChainServer.js.map +0 -1
  260. package/dist/toolDbAnonSignIn.d.ts +0 -2
  261. package/dist/toolDbAnonSignIn.js +0 -7
  262. package/dist/toolDbAnonSignIn.js.map +0 -1
  263. package/dist/toolDbClientOnMessage.d.ts +0 -2
  264. package/dist/toolDbClientOnMessage.js +0 -59
  265. package/dist/toolDbClientOnMessage.js.map +0 -1
  266. package/dist/toolDbCrdtGet.d.ts +0 -9
  267. package/dist/toolDbCrdtGet.js +0 -77
  268. package/dist/toolDbCrdtGet.js.map +0 -1
  269. package/dist/toolDbCrdtPut.d.ts +0 -9
  270. package/dist/toolDbCrdtPut.js +0 -63
  271. package/dist/toolDbCrdtPut.js.map +0 -1
  272. package/dist/toolDbGet.d.ts +0 -9
  273. package/dist/toolDbGet.js +0 -77
  274. package/dist/toolDbGet.js.map +0 -1
  275. package/dist/toolDbGetPubKey.d.ts +0 -2
  276. package/dist/toolDbGetPubKey.js +0 -18
  277. package/dist/toolDbGetPubKey.js.map +0 -1
  278. package/dist/toolDbKeysSignIn.d.ts +0 -2
  279. package/dist/toolDbKeysSignIn.js +0 -16
  280. package/dist/toolDbKeysSignIn.js.map +0 -1
  281. package/dist/toolDbNetwork.d.ts +0 -32
  282. package/dist/toolDbNetwork.js +0 -296
  283. package/dist/toolDbNetwork.js.map +0 -1
  284. package/dist/toolDbNetworkAdapter.d.ts +0 -57
  285. package/dist/toolDbNetworkAdapter.js +0 -188
  286. package/dist/toolDbNetworkAdapter.js.map +0 -1
  287. package/dist/toolDbPut.d.ts +0 -9
  288. package/dist/toolDbPut.js +0 -69
  289. package/dist/toolDbPut.js.map +0 -1
  290. package/dist/toolDbQueryKeys.d.ts +0 -8
  291. package/dist/toolDbQueryKeys.js +0 -66
  292. package/dist/toolDbQueryKeys.js.map +0 -1
  293. package/dist/toolDbSignIn.d.ts +0 -2
  294. package/dist/toolDbSignIn.js +0 -27
  295. package/dist/toolDbSignIn.js.map +0 -1
  296. package/dist/toolDbSignUp.d.ts +0 -2
  297. package/dist/toolDbSignUp.js +0 -101
  298. package/dist/toolDbSignUp.js.map +0 -1
  299. package/dist/toolDbSubscribe.d.ts +0 -9
  300. package/dist/toolDbSubscribe.js +0 -50
  301. package/dist/toolDbSubscribe.js.map +0 -1
  302. package/dist/toolDbVerificationWrapper.d.ts +0 -2
  303. package/dist/toolDbVerificationWrapper.js +0 -98
  304. package/dist/toolDbVerificationWrapper.js.map +0 -1
  305. package/dist/toolDbWeb3User.d.ts +0 -20
  306. package/dist/toolDbWeb3User.js +0 -75
  307. package/dist/toolDbWeb3User.js.map +0 -1
  308. package/dist/toolDbWebrtc.d.ts +0 -75
  309. package/dist/toolDbWebrtc.js +0 -472
  310. package/dist/toolDbWebrtc.js.map +0 -1
  311. package/dist/toolDbWebsocket.d.ts +0 -19
  312. package/dist/toolDbWebsocket.js +0 -187
  313. package/dist/toolDbWebsocket.js.map +0 -1
  314. package/dist/toolchain.d.ts +0 -1
  315. package/dist/toolchain.js +0 -8
  316. package/dist/toolchain.js.map +0 -1
  317. package/dist/tooldb.d.ts +0 -108
  318. package/dist/tooldb.js +0 -285
  319. package/dist/tooldb.js.map +0 -1
  320. package/dist/types/graph.d.ts +0 -0
  321. package/dist/types/graph.js +0 -2
  322. package/dist/types/graph.js.map +0 -1
  323. package/dist/types/message.d.ts +0 -100
  324. package/dist/types/message.js +0 -17
  325. package/dist/types/message.js.map +0 -1
  326. package/dist/types/tooldb.d.ts +0 -79
  327. package/dist/types/tooldb.js +0 -3
  328. package/dist/types/tooldb.js.map +0 -1
  329. package/dist/utils/arrayBufferToBase64.d.ts +0 -1
  330. package/dist/utils/arrayBufferToBase64.js +0 -12
  331. package/dist/utils/arrayBufferToBase64.js.map +0 -1
  332. package/dist/utils/arrayBufferToHex.d.ts +0 -1
  333. package/dist/utils/arrayBufferToHex.js +0 -19
  334. package/dist/utils/arrayBufferToHex.js.map +0 -1
  335. package/dist/utils/arrayBufferToString.d.ts +0 -1
  336. package/dist/utils/arrayBufferToString.js +0 -12
  337. package/dist/utils/arrayBufferToString.js.map +0 -1
  338. package/dist/utils/base64ToArrayBuffer.d.ts +0 -1
  339. package/dist/utils/base64ToArrayBuffer.js +0 -12
  340. package/dist/utils/base64ToArrayBuffer.js.map +0 -1
  341. package/dist/utils/base64ToBinaryChange.d.ts +0 -2
  342. package/dist/utils/base64ToBinaryChange.js +0 -18
  343. package/dist/utils/base64ToBinaryChange.js.map +0 -1
  344. package/dist/utils/base64ToBinaryDocument.d.ts +0 -2
  345. package/dist/utils/base64ToBinaryDocument.js +0 -18
  346. package/dist/utils/base64ToBinaryDocument.js.map +0 -1
  347. package/dist/utils/base64ToHex.d.ts +0 -1
  348. package/dist/utils/base64ToHex.js +0 -13
  349. package/dist/utils/base64ToHex.js.map +0 -1
  350. package/dist/utils/base64ToUint8.d.ts +0 -1
  351. package/dist/utils/base64ToUint8.js +0 -17
  352. package/dist/utils/base64ToUint8.js.map +0 -1
  353. package/dist/utils/catchReturn.d.ts +0 -1
  354. package/dist/utils/catchReturn.js +0 -8
  355. package/dist/utils/catchReturn.js.map +0 -1
  356. package/dist/utils/crypto/base64KeyToHex.d.ts +0 -1
  357. package/dist/utils/crypto/base64KeyToHex.js +0 -24
  358. package/dist/utils/crypto/base64KeyToHex.js.map +0 -1
  359. package/dist/utils/crypto/base64PublicKeyToHex.d.ts +0 -1
  360. package/dist/utils/crypto/base64PublicKeyToHex.js +0 -22
  361. package/dist/utils/crypto/base64PublicKeyToHex.js.map +0 -1
  362. package/dist/utils/crypto/decodeKeyString.d.ts +0 -1
  363. package/dist/utils/crypto/decodeKeyString.js +0 -13
  364. package/dist/utils/crypto/decodeKeyString.js.map +0 -1
  365. package/dist/utils/crypto/decryptData.d.ts +0 -1
  366. package/dist/utils/crypto/decryptData.js +0 -28
  367. package/dist/utils/crypto/decryptData.js.map +0 -1
  368. package/dist/utils/crypto/decryptWithPass.d.ts +0 -1
  369. package/dist/utils/crypto/decryptWithPass.js +0 -23
  370. package/dist/utils/crypto/decryptWithPass.js.map +0 -1
  371. package/dist/utils/crypto/deriveSecret.d.ts +0 -1
  372. package/dist/utils/crypto/deriveSecret.js +0 -18
  373. package/dist/utils/crypto/deriveSecret.js.map +0 -1
  374. package/dist/utils/crypto/encodeKeyString.d.ts +0 -1
  375. package/dist/utils/crypto/encodeKeyString.js +0 -13
  376. package/dist/utils/crypto/encodeKeyString.js.map +0 -1
  377. package/dist/utils/crypto/encryptData.d.ts +0 -1
  378. package/dist/utils/crypto/encryptData.js +0 -30
  379. package/dist/utils/crypto/encryptData.js.map +0 -1
  380. package/dist/utils/crypto/encryptWithPass.d.ts +0 -1
  381. package/dist/utils/crypto/encryptWithPass.js +0 -26
  382. package/dist/utils/crypto/encryptWithPass.js.map +0 -1
  383. package/dist/utils/crypto/exportKey.d.ts +0 -1
  384. package/dist/utils/crypto/exportKey.js +0 -14
  385. package/dist/utils/crypto/exportKey.js.map +0 -1
  386. package/dist/utils/crypto/exportKeyAsHex.d.ts +0 -1
  387. package/dist/utils/crypto/exportKeyAsHex.js +0 -18
  388. package/dist/utils/crypto/exportKeyAsHex.js.map +0 -1
  389. package/dist/utils/crypto/generateKeyFromPassword.d.ts +0 -1
  390. package/dist/utils/crypto/generateKeyFromPassword.js +0 -25
  391. package/dist/utils/crypto/generateKeyFromPassword.js.map +0 -1
  392. package/dist/utils/crypto/generateKeyPair.d.ts +0 -1
  393. package/dist/utils/crypto/generateKeyPair.js +0 -17
  394. package/dist/utils/crypto/generateKeyPair.js.map +0 -1
  395. package/dist/utils/crypto/generateKeysComb.d.ts +0 -4
  396. package/dist/utils/crypto/generateKeysComb.js +0 -60
  397. package/dist/utils/crypto/generateKeysComb.js.map +0 -1
  398. package/dist/utils/crypto/importKey.d.ts +0 -1
  399. package/dist/utils/crypto/importKey.js +0 -23
  400. package/dist/utils/crypto/importKey.js.map +0 -1
  401. package/dist/utils/crypto/loadKeysComb.d.ts +0 -5
  402. package/dist/utils/crypto/loadKeysComb.js +0 -90
  403. package/dist/utils/crypto/loadKeysComb.js.map +0 -1
  404. package/dist/utils/crypto/loadSignKeys.d.ts +0 -2
  405. package/dist/utils/crypto/loadSignKeys.js +0 -78
  406. package/dist/utils/crypto/loadSignKeys.js.map +0 -1
  407. package/dist/utils/crypto/recoverPubKey.d.ts +0 -1
  408. package/dist/utils/crypto/recoverPubKey.js +0 -27
  409. package/dist/utils/crypto/recoverPubKey.js.map +0 -1
  410. package/dist/utils/crypto/recoverPubKeyFromPrivate.d.ts +0 -1
  411. package/dist/utils/crypto/recoverPubKeyFromPrivate.js +0 -16
  412. package/dist/utils/crypto/recoverPubKeyFromPrivate.js.map +0 -1
  413. package/dist/utils/crypto/saveKeysComb.d.ts +0 -2
  414. package/dist/utils/crypto/saveKeysComb.js +0 -75
  415. package/dist/utils/crypto/saveKeysComb.js.map +0 -1
  416. package/dist/utils/crypto/saveSignKeys.d.ts +0 -2
  417. package/dist/utils/crypto/saveSignKeys.js +0 -67
  418. package/dist/utils/crypto/saveSignKeys.js.map +0 -1
  419. package/dist/utils/crypto/verifyData.d.ts +0 -1
  420. package/dist/utils/crypto/verifyData.js +0 -20
  421. package/dist/utils/crypto/verifyData.js.map +0 -1
  422. package/dist/utils/encoding/arrayBufferToBase64.d.ts +0 -1
  423. package/dist/utils/encoding/arrayBufferToBase64.js +0 -12
  424. package/dist/utils/encoding/arrayBufferToBase64.js.map +0 -1
  425. package/dist/utils/encoding/arrayBufferToHex.d.ts +0 -1
  426. package/dist/utils/encoding/arrayBufferToHex.js +0 -19
  427. package/dist/utils/encoding/arrayBufferToHex.js.map +0 -1
  428. package/dist/utils/encoding/arrayBufferToString.d.ts +0 -1
  429. package/dist/utils/encoding/arrayBufferToString.js +0 -12
  430. package/dist/utils/encoding/arrayBufferToString.js.map +0 -1
  431. package/dist/utils/encoding/base64ToArrayBuffer.d.ts +0 -1
  432. package/dist/utils/encoding/base64ToArrayBuffer.js +0 -12
  433. package/dist/utils/encoding/base64ToArrayBuffer.js.map +0 -1
  434. package/dist/utils/encoding/base64ToBinaryChange.d.ts +0 -2
  435. package/dist/utils/encoding/base64ToBinaryChange.js +0 -18
  436. package/dist/utils/encoding/base64ToBinaryChange.js.map +0 -1
  437. package/dist/utils/encoding/base64ToBinaryDocument.d.ts +0 -2
  438. package/dist/utils/encoding/base64ToBinaryDocument.js +0 -18
  439. package/dist/utils/encoding/base64ToBinaryDocument.js.map +0 -1
  440. package/dist/utils/encoding/base64ToHex.d.ts +0 -1
  441. package/dist/utils/encoding/base64ToHex.js +0 -13
  442. package/dist/utils/encoding/base64ToHex.js.map +0 -1
  443. package/dist/utils/encoding/base64ToUint8.d.ts +0 -1
  444. package/dist/utils/encoding/base64ToUint8.js +0 -17
  445. package/dist/utils/encoding/base64ToUint8.js.map +0 -1
  446. package/dist/utils/encoding/fromBase64.d.ts +0 -1
  447. package/dist/utils/encoding/fromBase64.js +0 -7
  448. package/dist/utils/encoding/fromBase64.js.map +0 -1
  449. package/dist/utils/encoding/hexToArrayBuffer.d.ts +0 -1
  450. package/dist/utils/encoding/hexToArrayBuffer.js +0 -15
  451. package/dist/utils/encoding/hexToArrayBuffer.js.map +0 -1
  452. package/dist/utils/encoding/hexToBase64.d.ts +0 -1
  453. package/dist/utils/encoding/hexToBase64.js +0 -12
  454. package/dist/utils/encoding/hexToBase64.js.map +0 -1
  455. package/dist/utils/encoding/hexToString.d.ts +0 -1
  456. package/dist/utils/encoding/hexToString.js +0 -12
  457. package/dist/utils/encoding/hexToString.js.map +0 -1
  458. package/dist/utils/encoding/hexToUint8.d.ts +0 -1
  459. package/dist/utils/encoding/hexToUint8.js +0 -8
  460. package/dist/utils/encoding/hexToUint8.js.map +0 -1
  461. package/dist/utils/encoding/stringToArrayBuffer.d.ts +0 -1
  462. package/dist/utils/encoding/stringToArrayBuffer.js +0 -12
  463. package/dist/utils/encoding/stringToArrayBuffer.js.map +0 -1
  464. package/dist/utils/encoding/toBase64.d.ts +0 -1
  465. package/dist/utils/encoding/toBase64.js +0 -7
  466. package/dist/utils/encoding/toBase64.js.map +0 -1
  467. package/dist/utils/encoding/uint8ArrayToHex.d.ts +0 -1
  468. package/dist/utils/encoding/uint8ArrayToHex.js +0 -7
  469. package/dist/utils/encoding/uint8ArrayToHex.js.map +0 -1
  470. package/dist/utils/encoding/uint8ToBase64.d.ts +0 -1
  471. package/dist/utils/encoding/uint8ToBase64.js +0 -15
  472. package/dist/utils/encoding/uint8ToBase64.js.map +0 -1
  473. package/dist/utils/fromBase64.d.ts +0 -1
  474. package/dist/utils/fromBase64.js +0 -7
  475. package/dist/utils/fromBase64.js.map +0 -1
  476. package/dist/utils/generateIv.d.ts +0 -1
  477. package/dist/utils/generateIv.js +0 -12
  478. package/dist/utils/generateIv.js.map +0 -1
  479. package/dist/utils/getPeerSignature.d.ts +0 -2
  480. package/dist/utils/getPeerSignature.js +0 -9
  481. package/dist/utils/getPeerSignature.js.map +0 -1
  482. package/dist/utils/getTimestamp.d.ts +0 -1
  483. package/dist/utils/getTimestamp.js +0 -7
  484. package/dist/utils/getTimestamp.js.map +0 -1
  485. package/dist/utils/hexToArrayBuffer.d.ts +0 -1
  486. package/dist/utils/hexToArrayBuffer.js +0 -15
  487. package/dist/utils/hexToArrayBuffer.js.map +0 -1
  488. package/dist/utils/hexToBase64.d.ts +0 -1
  489. package/dist/utils/hexToBase64.js +0 -12
  490. package/dist/utils/hexToBase64.js.map +0 -1
  491. package/dist/utils/hexToString.d.ts +0 -1
  492. package/dist/utils/hexToString.js +0 -12
  493. package/dist/utils/hexToString.js.map +0 -1
  494. package/dist/utils/hexToUint8.d.ts +0 -1
  495. package/dist/utils/hexToUint8.js +0 -8
  496. package/dist/utils/hexToUint8.js.map +0 -1
  497. package/dist/utils/indexedb.d.ts +0 -2
  498. package/dist/utils/indexedb.js +0 -116
  499. package/dist/utils/indexedb.js.map +0 -1
  500. package/dist/utils/leveldb.d.ts +0 -2
  501. package/dist/utils/leveldb.js +0 -83
  502. package/dist/utils/leveldb.js.map +0 -1
  503. package/dist/utils/proofOfWork.d.ts +0 -4
  504. package/dist/utils/proofOfWork.js +0 -16
  505. package/dist/utils/proofOfWork.js.map +0 -1
  506. package/dist/utils/randomAnimal.d.ts +0 -1
  507. package/dist/utils/randomAnimal.js +0 -77
  508. package/dist/utils/randomAnimal.js.map +0 -1
  509. package/dist/utils/sha1.d.ts +0 -1
  510. package/dist/utils/sha1.js +0 -13
  511. package/dist/utils/sha1.js.map +0 -1
  512. package/dist/utils/sha256.d.ts +0 -3
  513. package/dist/utils/sha256.js +0 -13
  514. package/dist/utils/sha256.js.map +0 -1
  515. package/dist/utils/signData.d.ts +0 -1
  516. package/dist/utils/signData.js +0 -19
  517. package/dist/utils/signData.js.map +0 -1
  518. package/dist/utils/stringToArrayBuffer.d.ts +0 -1
  519. package/dist/utils/stringToArrayBuffer.js +0 -12
  520. package/dist/utils/stringToArrayBuffer.js.map +0 -1
  521. package/dist/utils/textRandom.d.ts +0 -1
  522. package/dist/utils/textRandom.js +0 -15
  523. package/dist/utils/textRandom.js.map +0 -1
  524. package/dist/utils/toBase64.d.ts +0 -1
  525. package/dist/utils/toBase64.js +0 -7
  526. package/dist/utils/toBase64.js.map +0 -1
  527. package/dist/utils/uint8ArrayToHex.d.ts +0 -2
  528. package/dist/utils/uint8ArrayToHex.js +0 -7
  529. package/dist/utils/uint8ArrayToHex.js.map +0 -1
  530. package/dist/utils/uint8ToBase64.d.ts +0 -2
  531. package/dist/utils/uint8ToBase64.js +0 -15
  532. package/dist/utils/uint8ToBase64.js.map +0 -1
  533. package/dist/utils/uniq.d.ts +0 -1
  534. package/dist/utils/uniq.js +0 -7
  535. package/dist/utils/uniq.js.map +0 -1
  536. package/dist/utils/verifyMessage.d.ts +0 -8
  537. package/dist/utils/verifyMessage.js +0 -129
  538. package/dist/utils/verifyMessage.js.map +0 -1
  539. package/dist/utils/verifyPeer.d.ts +0 -2
  540. package/dist/utils/verifyPeer.js +0 -15
  541. package/dist/utils/verifyPeer.js.map +0 -1
  542. package/jest.config.ts +0 -195
  543. package/jest.setup.js +0 -0
  544. package/test-alice/000216.ldb +0 -0
  545. package/test-alice/000218.ldb +0 -0
  546. package/test-alice/000221.ldb +0 -0
  547. package/test-alice/000224.ldb +0 -0
  548. package/test-alice/000225.log +0 -0
  549. package/test-alice/CURRENT +0 -1
  550. package/test-alice/LOCK +0 -0
  551. package/test-alice/LOG +0 -5
  552. package/test-alice/LOG.old +0 -5
  553. package/test-alice/MANIFEST-000223 +0 -0
  554. package/test-base-client/000019.log +0 -0
  555. package/test-base-client/CURRENT +0 -1
  556. package/test-base-client/LOCK +0 -0
  557. package/test-base-client/LOG +0 -3
  558. package/test-base-client/LOG.old +0 -3
  559. package/test-base-client/MANIFEST-000018 +0 -0
  560. package/test-base-server/000019.log +0 -0
  561. package/test-base-server/CURRENT +0 -1
  562. package/test-base-server/LOCK +0 -0
  563. package/test-base-server/LOG +0 -3
  564. package/test-base-server/LOG.old +0 -3
  565. package/test-base-server/MANIFEST-000018 +0 -0
  566. package/test-bob/000231.ldb +0 -0
  567. package/test-bob/000233.ldb +0 -0
  568. package/test-bob/000234.log +0 -0
  569. package/test-bob/CURRENT +0 -1
  570. package/test-bob/LOCK +0 -0
  571. package/test-bob/LOG +0 -5
  572. package/test-bob/LOG.old +0 -14
  573. package/test-bob/MANIFEST-000232 +0 -0
  574. package/test-chris/000005.ldb +0 -0
  575. package/test-chris/000008.ldb +0 -0
  576. package/test-chris/000009.log +0 -0
  577. package/test-chris/CURRENT +0 -1
  578. package/test-chris/LOCK +0 -0
  579. package/test-chris/LOG +0 -5
  580. package/test-chris/LOG.old +0 -5
  581. package/test-chris/MANIFEST-000007 +0 -0
  582. package/test-node-a/000246.ldb +0 -0
  583. package/test-node-a/000248.ldb +0 -0
  584. package/test-node-a/000249.log +0 -0
  585. package/test-node-a/CURRENT +0 -1
  586. package/test-node-a/LOCK +0 -0
  587. package/test-node-a/LOG +0 -5
  588. package/test-node-a/LOG.old +0 -14
  589. package/test-node-a/MANIFEST-000247 +0 -0
  590. package/test-node-b/000246.ldb +0 -0
  591. package/test-node-b/000248.ldb +0 -0
  592. package/test-node-b/000249.log +0 -0
  593. package/test-node-b/CURRENT +0 -1
  594. package/test-node-b/LOCK +0 -0
  595. package/test-node-b/LOG +0 -5
  596. package/test-node-b/LOG.old +0 -14
  597. package/test-node-b/MANIFEST-000247 +0 -0
  598. package/test-store/000005.ldb +0 -0
  599. package/test-store/000008.ldb +0 -0
  600. package/test-store/000011.ldb +0 -0
  601. package/test-store/000014.ldb +0 -0
  602. package/test-store/000017.ldb +0 -0
  603. package/test-store/000018.log +0 -0
  604. package/test-store/CURRENT +0 -1
  605. package/test-store/LOCK +0 -0
  606. package/test-store/LOG +0 -6
  607. package/test-store/LOG.old +0 -6
  608. package/test-store/MANIFEST-000016 +0 -0
  609. package/test-verify-a/000059.ldb +0 -0
  610. package/test-verify-a/000066.log +0 -0
  611. package/test-verify-a/CURRENT +0 -1
  612. package/test-verify-a/LOCK +0 -0
  613. package/test-verify-a/LOG +0 -3
  614. package/test-verify-a/LOG.old +0 -3
  615. package/test-verify-a/MANIFEST-000065 +0 -0
@@ -0,0 +1,82 @@
1
+ import { textRandom, BaseCrdt, ToolDb, CrdtPutMessage } from ".";
2
+
3
+ /**
4
+ * Triggers a GET request to other peers. If the data is available locally it will return that instead.
5
+ * @param key key of the data
6
+ * @param userNamespaced If this key bolongs to a user or its public. Making it private will enforce validation for our address and signatures.
7
+ * @param timeout Max time to wait for remote.
8
+ * @returns Promise<Data>
9
+ */
10
+ export default function toolDbCrdtGet<T = any>(
11
+ this: ToolDb,
12
+ key: string,
13
+ crdt: BaseCrdt<T, any, any>,
14
+ userNamespaced = false,
15
+ timeoutMs = 1000
16
+ ): Promise<CrdtPutMessage<T> | null> {
17
+ return new Promise((resolve, reject) => {
18
+ if (userNamespaced && this.userAccount.getAddress() === undefined) {
19
+ reject(new Error("You are not authorized yet!"));
20
+ return;
21
+ }
22
+ const finalKey = userNamespaced
23
+ ? `:${this.userAccount.getAddress()}.${key}`
24
+ : key;
25
+ this.logger("CRDT GET", finalKey);
26
+
27
+ const msgId = textRandom(10);
28
+
29
+ const cancelTimeout = setTimeout(() => {
30
+ this.store
31
+ .get(finalKey)
32
+ .then((data) => {
33
+ try {
34
+ const message = JSON.parse(data);
35
+ crdt.mergeChanges(message.v);
36
+
37
+ this.emit("data", message);
38
+ resolve(message);
39
+ } catch (e) {
40
+ resolve(null);
41
+ }
42
+ })
43
+ .catch((e) => reject(null));
44
+ }, timeoutMs);
45
+
46
+ this.addIdListener(msgId, (msg) => {
47
+ this.logger("GET RECV", finalKey);
48
+
49
+ clearTimeout(cancelTimeout);
50
+ if (msg.type === "crdtPut") {
51
+ crdt.mergeChanges(msg.data.v);
52
+ resolve(msg);
53
+ }
54
+ });
55
+
56
+ this.store
57
+ .get(finalKey)
58
+ .then((data) => {
59
+ try {
60
+ const msg = JSON.parse(data);
61
+ clearTimeout(cancelTimeout);
62
+ this.removeIdListener(msgId);
63
+ crdt.mergeChanges(msg.v);
64
+ this.emit("data", msg);
65
+ resolve(msg);
66
+ } catch (e) {
67
+ // do nothing
68
+ }
69
+ })
70
+ .catch(() => {
71
+ // do nothing
72
+ });
73
+
74
+ // Do get
75
+ this.network.sendToAll({
76
+ type: "crdtGet",
77
+ to: [],
78
+ key: finalKey,
79
+ id: msgId,
80
+ });
81
+ });
82
+ }
@@ -0,0 +1,77 @@
1
+ import {
2
+ ToolDb,
3
+ CrdtPutMessage,
4
+ textRandom,
5
+ VerificationData,
6
+ proofOfWork,
7
+ BaseCrdt,
8
+ } from ".";
9
+
10
+ /**
11
+ * Triggers a PUT request to other peers.
12
+ * @param key key where we want to put the data at.
13
+ * @param value Data we want to any (any type)
14
+ * @param userNamespaced If this key bolongs to a user or its public. Making it private will enforce validation for our address and signatures.
15
+ * @returns Promise<Data | null>
16
+ */
17
+ export default function toolDbCrdtPut<T = any>(
18
+ this: ToolDb,
19
+ key: string,
20
+ crdt: BaseCrdt<T, any, any>,
21
+ userNamespaced = false
22
+ ): Promise<CrdtPutMessage | null> {
23
+ return new Promise((resolve, reject) => {
24
+ if (key.includes(".")) {
25
+ // Dots are used as a delimitator character between bublic keys and the key of the user's data
26
+ reject(new Error(`Key cannot include dots!; ${key}`));
27
+ return;
28
+ }
29
+
30
+ if (!this.userAccount.getAddress()) {
31
+ reject(new Error("You need to log in before you can PUT."));
32
+ return;
33
+ }
34
+
35
+ const timestamp = new Date().getTime();
36
+
37
+ const crdtChanges = crdt.getChanges();
38
+
39
+ const encodedData = JSON.stringify(crdtChanges);
40
+
41
+ const dataString = `${encodedData}${this.userAccount.getAddress()}${timestamp}`;
42
+
43
+ // WORK
44
+ proofOfWork(dataString, this.options.pow)
45
+ .then(({ hash, nonce }) => {
46
+ this.userAccount.signData(hash).then((signature) => {
47
+ if (signature && this.userAccount.getAddress()) {
48
+ // Compose the message
49
+ const data: VerificationData<any> = {
50
+ k: userNamespaced
51
+ ? `:${this.userAccount.getAddress()}.${key}`
52
+ : key,
53
+ a: this.userAccount.getAddress() || "",
54
+ n: nonce,
55
+ t: timestamp,
56
+ h: hash,
57
+ s: signature,
58
+ v: crdtChanges,
59
+ c: crdt.type,
60
+ };
61
+
62
+ this.logger("PUT CRDT", key, data);
63
+
64
+ const finalMessage: CrdtPutMessage<any> = {
65
+ type: "crdtPut",
66
+ id: textRandom(10),
67
+ to: [],
68
+ data,
69
+ };
70
+ this.network.sendToAll(finalMessage);
71
+ resolve(finalMessage);
72
+ }
73
+ });
74
+ })
75
+ .catch(reject);
76
+ });
77
+ }
@@ -0,0 +1,80 @@
1
+ import { ToolDb, textRandom } from ".";
2
+
3
+ /**
4
+ * Triggers a GET request to other peers. If the data is available locally it will return that instead.
5
+ * @param key key of the data
6
+ * @param userNamespaced If this key bolongs to a user or its public. Making it private will enforce validation for our address and signatures.
7
+ * @param timeout Max time to wait for remote.
8
+ * @returns Promise<Data>
9
+ */
10
+ export default function toolDbGet<T = any>(
11
+ this: ToolDb,
12
+ key: string,
13
+ userNamespaced = false,
14
+ timeoutMs = 1000
15
+ ): Promise<T | null> {
16
+ return new Promise((resolve, reject) => {
17
+ if (userNamespaced && this.userAccount.getAddress() === undefined) {
18
+ reject(new Error("You are not authorized yet!"));
19
+ return;
20
+ }
21
+ const finalKey = userNamespaced
22
+ ? `:${this.userAccount.getAddress()}.${key}`
23
+ : key;
24
+ this.logger("GET", finalKey);
25
+
26
+ const msgId = textRandom(10);
27
+
28
+ const cancelTimeout = setTimeout(() => {
29
+ this.store
30
+ .get(finalKey)
31
+ .then((data) => {
32
+ try {
33
+ const message = JSON.parse(data);
34
+ this.emit("data", message);
35
+ resolve(message.v);
36
+ } catch (e) {
37
+ resolve(null);
38
+ }
39
+ })
40
+ .catch((e) => {
41
+ resolve(null);
42
+ });
43
+ }, timeoutMs);
44
+
45
+ this.addIdListener(msgId, (msg) => {
46
+ this.logger("GET RECV", finalKey);
47
+
48
+ clearTimeout(cancelTimeout);
49
+ if (msg.type === "put") {
50
+ resolve(msg.data.v);
51
+ }
52
+ });
53
+
54
+ this.store
55
+ .get(finalKey)
56
+ .then((data) => {
57
+ try {
58
+ const parsed = JSON.parse(data);
59
+ const val = parsed.v;
60
+ clearTimeout(cancelTimeout);
61
+ this.removeIdListener(msgId);
62
+ this.emit("data", parsed);
63
+ resolve(val);
64
+ } catch (e) {
65
+ // do nothing
66
+ }
67
+ })
68
+ .catch((e) => {
69
+ // do nothing
70
+ });
71
+
72
+ // Do get
73
+ this.network.sendToAll({
74
+ type: "get",
75
+ to: [],
76
+ key: finalKey,
77
+ id: msgId,
78
+ });
79
+ });
80
+ }
@@ -0,0 +1,16 @@
1
+ import { ToolDb, randomAnimal } from ".";
2
+
3
+ export default function toolDbKeysSignIn(
4
+ this: ToolDb,
5
+ privateKey: string,
6
+ username?: string
7
+ ): Promise<unknown> {
8
+ if (!this.userAccount) return Promise.resolve(undefined);
9
+
10
+ return this.userAccount
11
+ .getAccountFromPrivate(privateKey)
12
+ .then((newAccount) => {
13
+ this.userAccount.setUser(newAccount, username || randomAnimal());
14
+ return newAccount;
15
+ });
16
+ }
@@ -0,0 +1,83 @@
1
+ import {
2
+ ToolDb,
3
+ PutMessage,
4
+ textRandom,
5
+ VerificationData,
6
+ proofOfWork,
7
+ } from ".";
8
+
9
+ /**
10
+ * Triggers a PUT request to other peers.
11
+ * @param key key where we want to put the data at.
12
+ * @param value Data we want to any (any type)
13
+ * @param userNamespaced If this key bolongs to a user or its public. Making it private will enforce validation for our address and signatures.
14
+ * @returns Promise<Data | null>
15
+ */
16
+ export default function toolDbPut<T = any>(
17
+ this: ToolDb,
18
+ key: string,
19
+ value: T,
20
+ userNamespaced = false
21
+ ): Promise<PutMessage<T> | null> {
22
+ return new Promise((resolve, reject) => {
23
+ if (key.includes(".")) {
24
+ // Dots are used as a delimitator character between bublic keys and the key of the user's data
25
+ reject(new Error(`Key cannot include dots!; ${key}`));
26
+ return;
27
+ }
28
+
29
+ if (!this.userAccount || !this.userAccount.getAddress()) {
30
+ reject(new Error("You need to log in before you can PUT."));
31
+ return;
32
+ }
33
+
34
+ const timestamp = new Date().getTime();
35
+ const dataString = `${JSON.stringify(
36
+ value
37
+ )}${this.userAccount.getAddress()}${timestamp}`;
38
+
39
+ // WORK
40
+ proofOfWork(dataString, this.options.pow)
41
+ .then(({ hash, nonce }) => {
42
+ this.userAccount.signData(hash).then((signature) => {
43
+ if (signature && this.userAccount.getAddress()) {
44
+ const finalKey = userNamespaced
45
+ ? `:${this.userAccount.getAddress()}.${key}`
46
+ : key;
47
+
48
+ // Compose the message
49
+ const data: VerificationData = {
50
+ k: finalKey,
51
+ a: this.userAccount.getAddress() || "",
52
+ n: nonce,
53
+ t: timestamp,
54
+ h: hash,
55
+ s: signature,
56
+ v: value,
57
+ c: null,
58
+ };
59
+
60
+ this.logger("PUT", key, data);
61
+
62
+ const finalMessage: PutMessage = {
63
+ type: "put",
64
+ id: textRandom(10),
65
+ to: [],
66
+ data,
67
+ };
68
+
69
+ this.network.sendToAll(finalMessage);
70
+ this.store
71
+ .put(finalKey, JSON.stringify(data))
72
+ .catch((e) => {
73
+ // do nothing
74
+ })
75
+ .finally(() => {
76
+ resolve(finalMessage);
77
+ });
78
+ }
79
+ });
80
+ })
81
+ .catch(reject);
82
+ });
83
+ }
@@ -0,0 +1,64 @@
1
+ import { ToolDb, textRandom, uniq } from ".";
2
+
3
+ /**
4
+ * Triggers a QUERY request to other peers.
5
+ * @param key start of the key
6
+ * @param userNamespaced If this key bolongs to a user or its public.
7
+ * @returns Promise<Data>
8
+ */
9
+ export default function toolDbQueryKeys(
10
+ this: ToolDb,
11
+ key: string,
12
+ userNamespaced = false,
13
+ timeoutMs = 1000
14
+ ): Promise<string[] | null> {
15
+ const user = this.userAccount;
16
+
17
+ return new Promise((resolve, reject) => {
18
+ if (userNamespaced && user.getAddress() === undefined) {
19
+ reject(new Error("You are not authorized yet!"));
20
+ return;
21
+ }
22
+ const finalKey = userNamespaced ? `:${user.getAddress()}.${key}` : key;
23
+ this.logger("QUERY", finalKey);
24
+
25
+ const msgId = textRandom(10);
26
+ let foundKeys: string[] = [];
27
+ let timeout: NodeJS.Timeout | undefined;
28
+
29
+ this.store
30
+ .query(finalKey)
31
+ .then((localKeys) => {
32
+ foundKeys = [...foundKeys, ...localKeys];
33
+ timeout = setTimeout(finishListening, timeoutMs);
34
+ })
35
+ .catch((e) => {
36
+ // do nothing
37
+ });
38
+
39
+ const finishListening = () => {
40
+ resolve(uniq(foundKeys));
41
+ };
42
+
43
+ this.addIdListener(msgId, (msg) => {
44
+ this.logger("QUERY RECV", finalKey);
45
+
46
+ if (msg.type === "queryAck") {
47
+ foundKeys = [...foundKeys, ...msg.keys];
48
+
49
+ if (timeout) {
50
+ clearTimeout(timeout);
51
+ }
52
+ timeout = setTimeout(finishListening, timeoutMs);
53
+ }
54
+ });
55
+
56
+ // Do get
57
+ this.network.sendToAll({
58
+ type: "query",
59
+ to: [],
60
+ key: finalKey,
61
+ id: msgId,
62
+ });
63
+ });
64
+ }
@@ -0,0 +1,31 @@
1
+ import { ToolDb, sha256, randomAnimal } from ".";
2
+
3
+ export default function toolDbSignIn(
4
+ this: ToolDb,
5
+ user: string,
6
+ password: string
7
+ ): Promise<unknown | undefined> {
8
+ return new Promise((resolve, reject) => {
9
+ this.getData<unknown>(`==${user}`, false, 5000).then((_user) => {
10
+ if (!_user) {
11
+ reject("Could not find user");
12
+ return;
13
+ }
14
+
15
+ try {
16
+ this.userAccount
17
+ .decryptAccount(_user, sha256(password))
18
+ .then((newAccount) => {
19
+ this.userAccount.setUser(
20
+ newAccount,
21
+ user || `Anonymous ${randomAnimal()}`
22
+ );
23
+
24
+ resolve(_user);
25
+ });
26
+ } catch (e) {
27
+ reject(e);
28
+ }
29
+ });
30
+ });
31
+ }
@@ -0,0 +1,71 @@
1
+ import {
2
+ ToolDb,
3
+ PutMessage,
4
+ textRandom,
5
+ VerificationData,
6
+ proofOfWork,
7
+ sha256,
8
+ } from ".";
9
+
10
+ export default async function toolDbSignUp(
11
+ this: ToolDb,
12
+ user: string,
13
+ password: string
14
+ ): Promise<PutMessage<any>> {
15
+ const userRoot = `==${user}`;
16
+ return new Promise((resolve, reject) => {
17
+ this.getData(userRoot, false, 3000)
18
+ .then((data) => {
19
+ if (data === null) {
20
+ const account = new this.options.userAdapter(this);
21
+ account.encryptAccount(sha256(password)).then((userData) => {
22
+ const timestamp = new Date().getTime();
23
+ const userDataString = `${JSON.stringify(
24
+ userData
25
+ )}${account.getAddress()}${timestamp}`;
26
+
27
+ proofOfWork(userDataString, 0)
28
+ .then(({ hash, nonce }) => {
29
+ account.signData(hash).then((signature) => {
30
+ const signupMessage: VerificationData = {
31
+ k: userRoot,
32
+ a: account.getAddress() || "",
33
+ n: nonce,
34
+ t: timestamp,
35
+ h: hash,
36
+ s: signature,
37
+ v: userData,
38
+ c: null,
39
+ };
40
+
41
+ this.logger("SIGNUP PUT", userRoot, signupMessage);
42
+
43
+ const finalMsg = {
44
+ type: "put",
45
+ id: textRandom(10),
46
+ to: [],
47
+ data: signupMessage,
48
+ } as PutMessage;
49
+
50
+ this.network.sendToAll(finalMsg);
51
+ this.store
52
+ .put(userRoot, JSON.stringify(signupMessage))
53
+ .catch((e) => {
54
+ // do nothing
55
+ })
56
+ .finally(() => {
57
+ resolve(finalMsg);
58
+ });
59
+ });
60
+ })
61
+ .catch(reject);
62
+ });
63
+ } else {
64
+ reject(new Error("User already exists!"));
65
+ }
66
+ })
67
+ .catch(() => {
68
+ reject(new Error("Could not fetch user"));
69
+ });
70
+ });
71
+ }
@@ -0,0 +1,53 @@
1
+ import { ToolDb, textRandom } from ".";
2
+
3
+ /**
4
+ * Subscribe to all PUT updates for this key.
5
+ * @param key key of the data
6
+ * @param userNamespaced If this key bolongs to a user or its public. Making it
7
+ * private will enforce validation for our address and signatures.
8
+ * @returns Promise<Data>
9
+ */
10
+ export default function toolDbSubscribe(
11
+ this: ToolDb,
12
+ key: string,
13
+ userNamespaced = false
14
+ ): Promise<void> {
15
+ return new Promise((resolve, reject) => {
16
+ if (userNamespaced && this.userAccount.getAddress() === undefined) {
17
+ reject(new Error("You are not authorized yet!"));
18
+ return;
19
+ }
20
+
21
+ const finalKey = userNamespaced
22
+ ? `:${this.userAccount.getAddress()}.${key}`
23
+ : key;
24
+
25
+ this.logger("SUBSCRIBE", finalKey);
26
+
27
+ const msgId = textRandom(10);
28
+
29
+ this.network.sendToAll({
30
+ type: "subscribe",
31
+ key: finalKey,
32
+ to: [],
33
+ id: msgId,
34
+ });
35
+
36
+ this.store
37
+ .get(finalKey)
38
+ .then((data) => {
39
+ try {
40
+ const message = JSON.parse(data);
41
+ this.triggerKeyListener(finalKey, message);
42
+ } catch (e) {
43
+ // do nothing
44
+ }
45
+ })
46
+ .catch((e) => {
47
+ // do nothing
48
+ })
49
+ .finally(() => {
50
+ resolve();
51
+ });
52
+ });
53
+ }
@@ -0,0 +1,55 @@
1
+ import { ToolDb, VerificationData, VerifyResult } from ".";
2
+
3
+ export default function toolDbVerificationWrapper(
4
+ this: ToolDb,
5
+ data: VerificationData<any>
6
+ ) {
7
+ // This wrapper function adds our custom verification functions to all messages.
8
+ return new Promise((resolve) => {
9
+ this.verifyMessage(data, this.options.pow).then(async (verified) => {
10
+ if (verified) {
11
+ let skipCustom = true;
12
+ this._customVerificator.forEach((listener) => {
13
+ if (listener && data.k && data.k.startsWith(listener.key)) {
14
+ skipCustom = false;
15
+
16
+ let previousData: any = undefined;
17
+ // Get the previously stored value of this key
18
+ this.store
19
+ .get(data.k)
20
+ .then((prev) => {
21
+ try {
22
+ previousData = JSON.parse(prev);
23
+ } catch (e) {
24
+ // do nothing
25
+ }
26
+ })
27
+ .catch((e) => {
28
+ // do nothing
29
+ })
30
+ .finally(() => {
31
+ listener
32
+ .fn(data, previousData)
33
+ .then((verified: boolean) => {
34
+ if (verified) {
35
+ resolve(VerifyResult.Verified);
36
+ } else {
37
+ resolve(VerifyResult.CustomVerificationFailed);
38
+ }
39
+ })
40
+ .catch((e) => {
41
+ resolve(VerifyResult.CustomVerificationFailed);
42
+ });
43
+ });
44
+ }
45
+ });
46
+
47
+ if (skipCustom) {
48
+ resolve(verified);
49
+ }
50
+ } else {
51
+ resolve(VerifyResult.InvalidVerification);
52
+ }
53
+ });
54
+ });
55
+ }