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.
- package/README.md +1 -74
- package/lib/adapters-base/networkAdapter.ts +215 -0
- package/lib/adapters-base/storageAdapter.ts +35 -0
- package/lib/adapters-base/userAdapter.ts +49 -0
- package/lib/crdt/baseCrdt.ts +21 -0
- package/lib/crdt/counterCrdt.ts +111 -0
- package/lib/crdt/listCrdt.ts +190 -0
- package/lib/crdt/mapCrdt.ts +119 -0
- package/{dist/index.d.ts → lib/index.ts} +8 -5
- package/lib/logger.ts +30 -0
- package/lib/messageHandlers/handleCrdtGet.ts +29 -0
- package/lib/messageHandlers/handleCrdtPut.ts +118 -0
- package/lib/messageHandlers/handleGet.ts +29 -0
- package/lib/messageHandlers/handlePing.ts +40 -0
- package/lib/messageHandlers/handlePong.ts +30 -0
- package/lib/messageHandlers/handlePut.ts +54 -0
- package/lib/messageHandlers/handleQuery.ts +25 -0
- package/lib/messageHandlers/handleSubscribe.ts +46 -0
- package/lib/server.ts +7 -0
- package/lib/shared.ts +5 -0
- package/lib/toolDbAnonSignIn.ts +5 -0
- package/lib/toolDbClientOnMessage.ts +75 -0
- package/lib/toolDbCrdtGet.ts +82 -0
- package/lib/toolDbCrdtPut.ts +77 -0
- package/lib/toolDbGet.ts +80 -0
- package/lib/toolDbKeysSignIn.ts +16 -0
- package/lib/toolDbPut.ts +83 -0
- package/lib/toolDbQueryKeys.ts +64 -0
- package/lib/toolDbSignIn.ts +31 -0
- package/lib/toolDbSignUp.ts +71 -0
- package/lib/toolDbSubscribe.ts +53 -0
- package/lib/toolDbVerificationWrapper.ts +55 -0
- package/lib/tooldb.ts +314 -0
- package/lib/types/message.ts +133 -0
- package/lib/types/tooldb.ts +89 -0
- package/lib/utils/catchReturn.ts +4 -0
- package/lib/utils/encoding/arrayBufferToHex.ts +18 -0
- package/lib/utils/encoding/arrayBufferToString.ts +8 -0
- package/lib/utils/encoding/hexToArrayBuffer.ts +13 -0
- package/lib/utils/encoding/hexToString.ts +8 -0
- package/lib/utils/encoding/hexToUint8.ts +5 -0
- package/lib/utils/encoding/stringToArrayBuffer.ts +8 -0
- package/lib/utils/getPeerSignature.ts +12 -0
- package/lib/utils/getTimestamp.ts +3 -0
- package/lib/utils/proofOfWork.ts +16 -0
- package/lib/utils/randomAnimal.ts +77 -0
- package/lib/utils/sha1.ts +7 -0
- package/lib/utils/sha256.ts +7 -0
- package/lib/utils/textRandom.ts +11 -0
- package/lib/utils/uniq.ts +3 -0
- package/lib/utils/verifyMessage.ts +88 -0
- package/lib/utils/verifyPeer.ts +15 -0
- package/package.json +31 -65
- package/tsconfig.json +10 -72
- package/.eslintrc.json +0 -74
- package/.github/workflows/main.yml +0 -29
- package/_____peer/000005.ldb +0 -0
- package/_____peer/000008.log +0 -0
- package/_____peer/CURRENT +0 -1
- package/_____peer/LOCK +0 -0
- package/_____peer/LOG +0 -0
- package/_____peer/LOG.old +0 -0
- package/_____peer/MANIFEST-000007 +0 -0
- package/_____peer_test-alice/000003.log +0 -0
- package/_____peer_test-alice/CURRENT +0 -1
- package/_____peer_test-alice/LOCK +0 -0
- package/_____peer_test-alice/LOG +0 -1
- package/_____peer_test-alice/MANIFEST-000002 +0 -0
- package/_____peer_test-base-client/000005.ldb +0 -0
- package/_____peer_test-base-client/000006.log +0 -0
- package/_____peer_test-base-client/CURRENT +0 -1
- package/_____peer_test-base-client/LOCK +0 -0
- package/_____peer_test-base-client/LOG +0 -5
- package/_____peer_test-base-client/LOG.old +0 -1
- package/_____peer_test-base-client/MANIFEST-000004 +0 -0
- package/_____peer_test-base-server/000005.ldb +0 -0
- package/_____peer_test-base-server/000006.log +0 -0
- package/_____peer_test-base-server/CURRENT +0 -1
- package/_____peer_test-base-server/LOCK +0 -0
- package/_____peer_test-base-server/LOG +0 -5
- package/_____peer_test-base-server/LOG.old +0 -1
- package/_____peer_test-base-server/MANIFEST-000004 +0 -0
- package/_____peer_test-bob/000003.log +0 -0
- package/_____peer_test-bob/CURRENT +0 -1
- package/_____peer_test-bob/LOCK +0 -0
- package/_____peer_test-bob/LOG +0 -1
- package/_____peer_test-bob/MANIFEST-000002 +0 -0
- package/_____peer_test-chris/000003.log +0 -0
- package/_____peer_test-chris/CURRENT +0 -1
- package/_____peer_test-chris/LOCK +0 -0
- package/_____peer_test-chris/LOG +0 -1
- package/_____peer_test-chris/MANIFEST-000002 +0 -0
- package/_____peer_test-node-a/000003.log +0 -0
- package/_____peer_test-node-a/CURRENT +0 -1
- package/_____peer_test-node-a/LOCK +0 -0
- package/_____peer_test-node-a/LOG +0 -1
- package/_____peer_test-node-a/MANIFEST-000002 +0 -0
- package/_____peer_test-node-b/000003.log +0 -0
- package/_____peer_test-node-b/CURRENT +0 -1
- package/_____peer_test-node-b/LOCK +0 -0
- package/_____peer_test-node-b/LOG +0 -1
- package/_____peer_test-node-b/MANIFEST-000002 +0 -0
- package/_____peer_test-store/000003.log +0 -0
- package/_____peer_test-store/CURRENT +0 -1
- package/_____peer_test-store/LOCK +0 -0
- package/_____peer_test-store/LOG +0 -1
- package/_____peer_test-store/MANIFEST-000002 +0 -0
- package/_____peer_test-verify-a/000003.log +0 -0
- package/_____peer_test-verify-a/CURRENT +0 -1
- package/_____peer_test-verify-a/LOCK +0 -0
- package/_____peer_test-verify-a/LOG +0 -1
- package/_____peer_test-verify-a/MANIFEST-000002 +0 -0
- package/babel.config.js +0 -6
- package/bundle.js +0 -1
- package/custom-test-env.js +0 -28
- package/dist/__tests__ copy/base64.d.ts +0 -1
- package/dist/__tests__ copy/base64.js +0 -61
- package/dist/__tests__ copy/base64.js.map +0 -1
- package/dist/__tests__ copy/encryption.d.ts +0 -1
- package/dist/__tests__ copy/encryption.js +0 -68
- package/dist/__tests__ copy/encryption.js.map +0 -1
- package/dist/__tests__ copy/keys.d.ts +0 -1
- package/dist/__tests__ copy/keys.js +0 -238
- package/dist/__tests__ copy/keys.js.map +0 -1
- package/dist/__tests__ copy/network.d.ts +0 -1
- package/dist/__tests__ copy/network.js +0 -206
- package/dist/__tests__ copy/network.js.map +0 -1
- package/dist/__tests__ copy/pow.d.ts +0 -1
- package/dist/__tests__ copy/pow.js +0 -25
- package/dist/__tests__ copy/pow.js.map +0 -1
- package/dist/__tests__ copy/sha1.d.ts +0 -1
- package/dist/__tests__ copy/sha1.js +0 -16
- package/dist/__tests__ copy/sha1.js.map +0 -1
- package/dist/__tests__ copy/sha256.d.ts +0 -1
- package/dist/__tests__ copy/sha256.js +0 -16
- package/dist/__tests__ copy/sha256.js.map +0 -1
- package/dist/__tests__ copy/textRandom.d.ts +0 -1
- package/dist/__tests__ copy/textRandom.js +0 -17
- package/dist/__tests__ copy/textRandom.js.map +0 -1
- package/dist/adapters/toolDbIndexedb.d.ts +0 -9
- package/dist/adapters/toolDbIndexedb.js +0 -131
- package/dist/adapters/toolDbIndexedb.js.map +0 -1
- package/dist/adapters/toolDbLeveldb.d.ts +0 -8
- package/dist/adapters/toolDbLeveldb.js +0 -109
- package/dist/adapters/toolDbLeveldb.js.map +0 -1
- package/dist/adapters/toolDbWeb3User.d.ts +0 -18
- package/dist/adapters/toolDbWeb3User.js +0 -74
- package/dist/adapters/toolDbWeb3User.js.map +0 -1
- package/dist/adapters/toolDbWebrtc.d.ts +0 -58
- package/dist/adapters/toolDbWebrtc.js +0 -487
- package/dist/adapters/toolDbWebrtc.js.map +0 -1
- package/dist/adapters/toolDbWebsocket.d.ts +0 -18
- package/dist/adapters/toolDbWebsocket.js +0 -176
- package/dist/adapters/toolDbWebsocket.js.map +0 -1
- package/dist/adapters-base/networkAdapter.d.ts +0 -58
- package/dist/adapters-base/networkAdapter.js +0 -214
- package/dist/adapters-base/networkAdapter.js.map +0 -1
- package/dist/adapters-base/storageAdapter.d.ts +0 -11
- package/dist/adapters-base/storageAdapter.js +0 -36
- package/dist/adapters-base/storageAdapter.js.map +0 -1
- package/dist/adapters-base/userAdapter.d.ts +0 -15
- package/dist/adapters-base/userAdapter.js +0 -42
- package/dist/adapters-base/userAdapter.js.map +0 -1
- package/dist/bundle.js +0 -69086
- package/dist/chain/Block.d.ts +0 -29
- package/dist/chain/Block.js +0 -142
- package/dist/chain/Block.js.map +0 -1
- package/dist/chain/ToolChain.d.ts +0 -27
- package/dist/chain/ToolChain.js +0 -253
- package/dist/chain/ToolChain.js.map +0 -1
- package/dist/chain/Tx.d.ts +0 -50
- package/dist/chain/Tx.js +0 -54
- package/dist/chain/Tx.js.map +0 -1
- package/dist/chain/Wallet.d.ts +0 -9
- package/dist/chain/Wallet.js +0 -37
- package/dist/chain/Wallet.js.map +0 -1
- package/dist/chain/isValidChain.d.ts +0 -1
- package/dist/chain/isValidChain.js +0 -37
- package/dist/chain/isValidChain.js.map +0 -1
- package/dist/chain/mempool.d.ts +0 -0
- package/dist/chain/mempool.js +0 -2
- package/dist/chain/mempool.js.map +0 -1
- package/dist/crdt/baseCrdt.d.ts +0 -9
- package/dist/crdt/baseCrdt.js +0 -27
- package/dist/crdt/baseCrdt.js.map +0 -1
- package/dist/crdt/counterCrdt.d.ts +0 -30
- package/dist/crdt/counterCrdt.js +0 -106
- package/dist/crdt/counterCrdt.js.map +0 -1
- package/dist/crdt/listCrdt.d.ts +0 -42
- package/dist/crdt/listCrdt.js +0 -159
- package/dist/crdt/listCrdt.js.map +0 -1
- package/dist/crdt/mapCrdt.d.ts +0 -32
- package/dist/crdt/mapCrdt.js +0 -118
- package/dist/crdt/mapCrdt.js.map +0 -1
- package/dist/getCrypto.d.ts +0 -1
- package/dist/getCrypto.js +0 -10
- package/dist/getCrypto.js.map +0 -1
- package/dist/index.js +0 -93
- package/dist/index.js.map +0 -1
- package/dist/infuraFilecoin.d.ts +0 -0
- package/dist/infuraFilecoin.js +0 -2
- package/dist/infuraFilecoin.js.map +0 -1
- package/dist/loadCrdtDocument.d.ts +0 -3
- package/dist/loadCrdtDocument.js +0 -43
- package/dist/loadCrdtDocument.js.map +0 -1
- package/dist/logger.d.ts +0 -2
- package/dist/logger.js +0 -29
- package/dist/logger.js.map +0 -1
- package/dist/makeTx.d.ts +0 -1
- package/dist/makeTx.js +0 -27
- package/dist/makeTx.js.map +0 -1
- package/dist/messageHandlers/handleCrdt.d.ts +0 -2
- package/dist/messageHandlers/handleCrdt.js +0 -15
- package/dist/messageHandlers/handleCrdt.js.map +0 -1
- package/dist/messageHandlers/handleCrdtGet.d.ts +0 -2
- package/dist/messageHandlers/handleCrdtGet.js +0 -29
- package/dist/messageHandlers/handleCrdtGet.js.map +0 -1
- package/dist/messageHandlers/handleCrdtPut.d.ts +0 -2
- package/dist/messageHandlers/handleCrdtPut.js +0 -93
- package/dist/messageHandlers/handleCrdtPut.js.map +0 -1
- package/dist/messageHandlers/handleGet.d.ts +0 -2
- package/dist/messageHandlers/handleGet.js +0 -29
- package/dist/messageHandlers/handleGet.js.map +0 -1
- package/dist/messageHandlers/handleJoin.d.ts +0 -2
- package/dist/messageHandlers/handleJoin.js +0 -36
- package/dist/messageHandlers/handleJoin.js.map +0 -1
- package/dist/messageHandlers/handlePing.d.ts +0 -2
- package/dist/messageHandlers/handlePing.js +0 -36
- package/dist/messageHandlers/handlePing.js.map +0 -1
- package/dist/messageHandlers/handlePong.d.ts +0 -2
- package/dist/messageHandlers/handlePong.js +0 -26
- package/dist/messageHandlers/handlePong.js.map +0 -1
- package/dist/messageHandlers/handlePut.d.ts +0 -2
- package/dist/messageHandlers/handlePut.js +0 -57
- package/dist/messageHandlers/handlePut.js.map +0 -1
- package/dist/messageHandlers/handleQuery.d.ts +0 -2
- package/dist/messageHandlers/handleQuery.js +0 -23
- package/dist/messageHandlers/handleQuery.js.map +0 -1
- package/dist/messageHandlers/handleServers.d.ts +0 -2
- package/dist/messageHandlers/handleServers.js +0 -23
- package/dist/messageHandlers/handleServers.js.map +0 -1
- package/dist/messageHandlers/handleSubscribe.d.ts +0 -2
- package/dist/messageHandlers/handleSubscribe.js +0 -44
- package/dist/messageHandlers/handleSubscribe.js.map +0 -1
- package/dist/sendJoin.d.ts +0 -2
- package/dist/sendJoin.js +0 -31
- package/dist/sendJoin.js.map +0 -1
- package/dist/server.d.ts +0 -1
- package/dist/server.js +0 -9
- package/dist/server.js.map +0 -1
- package/dist/shared.d.ts +0 -2
- package/dist/shared.js +0 -7
- package/dist/shared.js.map +0 -1
- package/dist/test.d.ts +0 -1
- package/dist/test.js +0 -17
- package/dist/test.js.map +0 -1
- package/dist/toolChainServer.d.ts +0 -8
- package/dist/toolChainServer.js +0 -60
- package/dist/toolChainServer.js.map +0 -1
- package/dist/toolDbAnonSignIn.d.ts +0 -2
- package/dist/toolDbAnonSignIn.js +0 -7
- package/dist/toolDbAnonSignIn.js.map +0 -1
- package/dist/toolDbClientOnMessage.d.ts +0 -2
- package/dist/toolDbClientOnMessage.js +0 -59
- package/dist/toolDbClientOnMessage.js.map +0 -1
- package/dist/toolDbCrdtGet.d.ts +0 -9
- package/dist/toolDbCrdtGet.js +0 -77
- package/dist/toolDbCrdtGet.js.map +0 -1
- package/dist/toolDbCrdtPut.d.ts +0 -9
- package/dist/toolDbCrdtPut.js +0 -63
- package/dist/toolDbCrdtPut.js.map +0 -1
- package/dist/toolDbGet.d.ts +0 -9
- package/dist/toolDbGet.js +0 -77
- package/dist/toolDbGet.js.map +0 -1
- package/dist/toolDbGetPubKey.d.ts +0 -2
- package/dist/toolDbGetPubKey.js +0 -18
- package/dist/toolDbGetPubKey.js.map +0 -1
- package/dist/toolDbKeysSignIn.d.ts +0 -2
- package/dist/toolDbKeysSignIn.js +0 -16
- package/dist/toolDbKeysSignIn.js.map +0 -1
- package/dist/toolDbNetwork.d.ts +0 -32
- package/dist/toolDbNetwork.js +0 -296
- package/dist/toolDbNetwork.js.map +0 -1
- package/dist/toolDbNetworkAdapter.d.ts +0 -57
- package/dist/toolDbNetworkAdapter.js +0 -188
- package/dist/toolDbNetworkAdapter.js.map +0 -1
- package/dist/toolDbPut.d.ts +0 -9
- package/dist/toolDbPut.js +0 -69
- package/dist/toolDbPut.js.map +0 -1
- package/dist/toolDbQueryKeys.d.ts +0 -8
- package/dist/toolDbQueryKeys.js +0 -66
- package/dist/toolDbQueryKeys.js.map +0 -1
- package/dist/toolDbSignIn.d.ts +0 -2
- package/dist/toolDbSignIn.js +0 -27
- package/dist/toolDbSignIn.js.map +0 -1
- package/dist/toolDbSignUp.d.ts +0 -2
- package/dist/toolDbSignUp.js +0 -101
- package/dist/toolDbSignUp.js.map +0 -1
- package/dist/toolDbSubscribe.d.ts +0 -9
- package/dist/toolDbSubscribe.js +0 -50
- package/dist/toolDbSubscribe.js.map +0 -1
- package/dist/toolDbVerificationWrapper.d.ts +0 -2
- package/dist/toolDbVerificationWrapper.js +0 -98
- package/dist/toolDbVerificationWrapper.js.map +0 -1
- package/dist/toolDbWeb3User.d.ts +0 -20
- package/dist/toolDbWeb3User.js +0 -75
- package/dist/toolDbWeb3User.js.map +0 -1
- package/dist/toolDbWebrtc.d.ts +0 -75
- package/dist/toolDbWebrtc.js +0 -472
- package/dist/toolDbWebrtc.js.map +0 -1
- package/dist/toolDbWebsocket.d.ts +0 -19
- package/dist/toolDbWebsocket.js +0 -187
- package/dist/toolDbWebsocket.js.map +0 -1
- package/dist/toolchain.d.ts +0 -1
- package/dist/toolchain.js +0 -8
- package/dist/toolchain.js.map +0 -1
- package/dist/tooldb.d.ts +0 -108
- package/dist/tooldb.js +0 -285
- package/dist/tooldb.js.map +0 -1
- package/dist/types/graph.d.ts +0 -0
- package/dist/types/graph.js +0 -2
- package/dist/types/graph.js.map +0 -1
- package/dist/types/message.d.ts +0 -100
- package/dist/types/message.js +0 -17
- package/dist/types/message.js.map +0 -1
- package/dist/types/tooldb.d.ts +0 -79
- package/dist/types/tooldb.js +0 -3
- package/dist/types/tooldb.js.map +0 -1
- package/dist/utils/arrayBufferToBase64.d.ts +0 -1
- package/dist/utils/arrayBufferToBase64.js +0 -12
- package/dist/utils/arrayBufferToBase64.js.map +0 -1
- package/dist/utils/arrayBufferToHex.d.ts +0 -1
- package/dist/utils/arrayBufferToHex.js +0 -19
- package/dist/utils/arrayBufferToHex.js.map +0 -1
- package/dist/utils/arrayBufferToString.d.ts +0 -1
- package/dist/utils/arrayBufferToString.js +0 -12
- package/dist/utils/arrayBufferToString.js.map +0 -1
- package/dist/utils/base64ToArrayBuffer.d.ts +0 -1
- package/dist/utils/base64ToArrayBuffer.js +0 -12
- package/dist/utils/base64ToArrayBuffer.js.map +0 -1
- package/dist/utils/base64ToBinaryChange.d.ts +0 -2
- package/dist/utils/base64ToBinaryChange.js +0 -18
- package/dist/utils/base64ToBinaryChange.js.map +0 -1
- package/dist/utils/base64ToBinaryDocument.d.ts +0 -2
- package/dist/utils/base64ToBinaryDocument.js +0 -18
- package/dist/utils/base64ToBinaryDocument.js.map +0 -1
- package/dist/utils/base64ToHex.d.ts +0 -1
- package/dist/utils/base64ToHex.js +0 -13
- package/dist/utils/base64ToHex.js.map +0 -1
- package/dist/utils/base64ToUint8.d.ts +0 -1
- package/dist/utils/base64ToUint8.js +0 -17
- package/dist/utils/base64ToUint8.js.map +0 -1
- package/dist/utils/catchReturn.d.ts +0 -1
- package/dist/utils/catchReturn.js +0 -8
- package/dist/utils/catchReturn.js.map +0 -1
- package/dist/utils/crypto/base64KeyToHex.d.ts +0 -1
- package/dist/utils/crypto/base64KeyToHex.js +0 -24
- package/dist/utils/crypto/base64KeyToHex.js.map +0 -1
- package/dist/utils/crypto/base64PublicKeyToHex.d.ts +0 -1
- package/dist/utils/crypto/base64PublicKeyToHex.js +0 -22
- package/dist/utils/crypto/base64PublicKeyToHex.js.map +0 -1
- package/dist/utils/crypto/decodeKeyString.d.ts +0 -1
- package/dist/utils/crypto/decodeKeyString.js +0 -13
- package/dist/utils/crypto/decodeKeyString.js.map +0 -1
- package/dist/utils/crypto/decryptData.d.ts +0 -1
- package/dist/utils/crypto/decryptData.js +0 -28
- package/dist/utils/crypto/decryptData.js.map +0 -1
- package/dist/utils/crypto/decryptWithPass.d.ts +0 -1
- package/dist/utils/crypto/decryptWithPass.js +0 -23
- package/dist/utils/crypto/decryptWithPass.js.map +0 -1
- package/dist/utils/crypto/deriveSecret.d.ts +0 -1
- package/dist/utils/crypto/deriveSecret.js +0 -18
- package/dist/utils/crypto/deriveSecret.js.map +0 -1
- package/dist/utils/crypto/encodeKeyString.d.ts +0 -1
- package/dist/utils/crypto/encodeKeyString.js +0 -13
- package/dist/utils/crypto/encodeKeyString.js.map +0 -1
- package/dist/utils/crypto/encryptData.d.ts +0 -1
- package/dist/utils/crypto/encryptData.js +0 -30
- package/dist/utils/crypto/encryptData.js.map +0 -1
- package/dist/utils/crypto/encryptWithPass.d.ts +0 -1
- package/dist/utils/crypto/encryptWithPass.js +0 -26
- package/dist/utils/crypto/encryptWithPass.js.map +0 -1
- package/dist/utils/crypto/exportKey.d.ts +0 -1
- package/dist/utils/crypto/exportKey.js +0 -14
- package/dist/utils/crypto/exportKey.js.map +0 -1
- package/dist/utils/crypto/exportKeyAsHex.d.ts +0 -1
- package/dist/utils/crypto/exportKeyAsHex.js +0 -18
- package/dist/utils/crypto/exportKeyAsHex.js.map +0 -1
- package/dist/utils/crypto/generateKeyFromPassword.d.ts +0 -1
- package/dist/utils/crypto/generateKeyFromPassword.js +0 -25
- package/dist/utils/crypto/generateKeyFromPassword.js.map +0 -1
- package/dist/utils/crypto/generateKeyPair.d.ts +0 -1
- package/dist/utils/crypto/generateKeyPair.js +0 -17
- package/dist/utils/crypto/generateKeyPair.js.map +0 -1
- package/dist/utils/crypto/generateKeysComb.d.ts +0 -4
- package/dist/utils/crypto/generateKeysComb.js +0 -60
- package/dist/utils/crypto/generateKeysComb.js.map +0 -1
- package/dist/utils/crypto/importKey.d.ts +0 -1
- package/dist/utils/crypto/importKey.js +0 -23
- package/dist/utils/crypto/importKey.js.map +0 -1
- package/dist/utils/crypto/loadKeysComb.d.ts +0 -5
- package/dist/utils/crypto/loadKeysComb.js +0 -90
- package/dist/utils/crypto/loadKeysComb.js.map +0 -1
- package/dist/utils/crypto/loadSignKeys.d.ts +0 -2
- package/dist/utils/crypto/loadSignKeys.js +0 -78
- package/dist/utils/crypto/loadSignKeys.js.map +0 -1
- package/dist/utils/crypto/recoverPubKey.d.ts +0 -1
- package/dist/utils/crypto/recoverPubKey.js +0 -27
- package/dist/utils/crypto/recoverPubKey.js.map +0 -1
- package/dist/utils/crypto/recoverPubKeyFromPrivate.d.ts +0 -1
- package/dist/utils/crypto/recoverPubKeyFromPrivate.js +0 -16
- package/dist/utils/crypto/recoverPubKeyFromPrivate.js.map +0 -1
- package/dist/utils/crypto/saveKeysComb.d.ts +0 -2
- package/dist/utils/crypto/saveKeysComb.js +0 -75
- package/dist/utils/crypto/saveKeysComb.js.map +0 -1
- package/dist/utils/crypto/saveSignKeys.d.ts +0 -2
- package/dist/utils/crypto/saveSignKeys.js +0 -67
- package/dist/utils/crypto/saveSignKeys.js.map +0 -1
- package/dist/utils/crypto/verifyData.d.ts +0 -1
- package/dist/utils/crypto/verifyData.js +0 -20
- package/dist/utils/crypto/verifyData.js.map +0 -1
- package/dist/utils/encoding/arrayBufferToBase64.d.ts +0 -1
- package/dist/utils/encoding/arrayBufferToBase64.js +0 -12
- package/dist/utils/encoding/arrayBufferToBase64.js.map +0 -1
- package/dist/utils/encoding/arrayBufferToHex.d.ts +0 -1
- package/dist/utils/encoding/arrayBufferToHex.js +0 -19
- package/dist/utils/encoding/arrayBufferToHex.js.map +0 -1
- package/dist/utils/encoding/arrayBufferToString.d.ts +0 -1
- package/dist/utils/encoding/arrayBufferToString.js +0 -12
- package/dist/utils/encoding/arrayBufferToString.js.map +0 -1
- package/dist/utils/encoding/base64ToArrayBuffer.d.ts +0 -1
- package/dist/utils/encoding/base64ToArrayBuffer.js +0 -12
- package/dist/utils/encoding/base64ToArrayBuffer.js.map +0 -1
- package/dist/utils/encoding/base64ToBinaryChange.d.ts +0 -2
- package/dist/utils/encoding/base64ToBinaryChange.js +0 -18
- package/dist/utils/encoding/base64ToBinaryChange.js.map +0 -1
- package/dist/utils/encoding/base64ToBinaryDocument.d.ts +0 -2
- package/dist/utils/encoding/base64ToBinaryDocument.js +0 -18
- package/dist/utils/encoding/base64ToBinaryDocument.js.map +0 -1
- package/dist/utils/encoding/base64ToHex.d.ts +0 -1
- package/dist/utils/encoding/base64ToHex.js +0 -13
- package/dist/utils/encoding/base64ToHex.js.map +0 -1
- package/dist/utils/encoding/base64ToUint8.d.ts +0 -1
- package/dist/utils/encoding/base64ToUint8.js +0 -17
- package/dist/utils/encoding/base64ToUint8.js.map +0 -1
- package/dist/utils/encoding/fromBase64.d.ts +0 -1
- package/dist/utils/encoding/fromBase64.js +0 -7
- package/dist/utils/encoding/fromBase64.js.map +0 -1
- package/dist/utils/encoding/hexToArrayBuffer.d.ts +0 -1
- package/dist/utils/encoding/hexToArrayBuffer.js +0 -15
- package/dist/utils/encoding/hexToArrayBuffer.js.map +0 -1
- package/dist/utils/encoding/hexToBase64.d.ts +0 -1
- package/dist/utils/encoding/hexToBase64.js +0 -12
- package/dist/utils/encoding/hexToBase64.js.map +0 -1
- package/dist/utils/encoding/hexToString.d.ts +0 -1
- package/dist/utils/encoding/hexToString.js +0 -12
- package/dist/utils/encoding/hexToString.js.map +0 -1
- package/dist/utils/encoding/hexToUint8.d.ts +0 -1
- package/dist/utils/encoding/hexToUint8.js +0 -8
- package/dist/utils/encoding/hexToUint8.js.map +0 -1
- package/dist/utils/encoding/stringToArrayBuffer.d.ts +0 -1
- package/dist/utils/encoding/stringToArrayBuffer.js +0 -12
- package/dist/utils/encoding/stringToArrayBuffer.js.map +0 -1
- package/dist/utils/encoding/toBase64.d.ts +0 -1
- package/dist/utils/encoding/toBase64.js +0 -7
- package/dist/utils/encoding/toBase64.js.map +0 -1
- package/dist/utils/encoding/uint8ArrayToHex.d.ts +0 -1
- package/dist/utils/encoding/uint8ArrayToHex.js +0 -7
- package/dist/utils/encoding/uint8ArrayToHex.js.map +0 -1
- package/dist/utils/encoding/uint8ToBase64.d.ts +0 -1
- package/dist/utils/encoding/uint8ToBase64.js +0 -15
- package/dist/utils/encoding/uint8ToBase64.js.map +0 -1
- package/dist/utils/fromBase64.d.ts +0 -1
- package/dist/utils/fromBase64.js +0 -7
- package/dist/utils/fromBase64.js.map +0 -1
- package/dist/utils/generateIv.d.ts +0 -1
- package/dist/utils/generateIv.js +0 -12
- package/dist/utils/generateIv.js.map +0 -1
- package/dist/utils/getPeerSignature.d.ts +0 -2
- package/dist/utils/getPeerSignature.js +0 -9
- package/dist/utils/getPeerSignature.js.map +0 -1
- package/dist/utils/getTimestamp.d.ts +0 -1
- package/dist/utils/getTimestamp.js +0 -7
- package/dist/utils/getTimestamp.js.map +0 -1
- package/dist/utils/hexToArrayBuffer.d.ts +0 -1
- package/dist/utils/hexToArrayBuffer.js +0 -15
- package/dist/utils/hexToArrayBuffer.js.map +0 -1
- package/dist/utils/hexToBase64.d.ts +0 -1
- package/dist/utils/hexToBase64.js +0 -12
- package/dist/utils/hexToBase64.js.map +0 -1
- package/dist/utils/hexToString.d.ts +0 -1
- package/dist/utils/hexToString.js +0 -12
- package/dist/utils/hexToString.js.map +0 -1
- package/dist/utils/hexToUint8.d.ts +0 -1
- package/dist/utils/hexToUint8.js +0 -8
- package/dist/utils/hexToUint8.js.map +0 -1
- package/dist/utils/indexedb.d.ts +0 -2
- package/dist/utils/indexedb.js +0 -116
- package/dist/utils/indexedb.js.map +0 -1
- package/dist/utils/leveldb.d.ts +0 -2
- package/dist/utils/leveldb.js +0 -83
- package/dist/utils/leveldb.js.map +0 -1
- package/dist/utils/proofOfWork.d.ts +0 -4
- package/dist/utils/proofOfWork.js +0 -16
- package/dist/utils/proofOfWork.js.map +0 -1
- package/dist/utils/randomAnimal.d.ts +0 -1
- package/dist/utils/randomAnimal.js +0 -77
- package/dist/utils/randomAnimal.js.map +0 -1
- package/dist/utils/sha1.d.ts +0 -1
- package/dist/utils/sha1.js +0 -13
- package/dist/utils/sha1.js.map +0 -1
- package/dist/utils/sha256.d.ts +0 -3
- package/dist/utils/sha256.js +0 -13
- package/dist/utils/sha256.js.map +0 -1
- package/dist/utils/signData.d.ts +0 -1
- package/dist/utils/signData.js +0 -19
- package/dist/utils/signData.js.map +0 -1
- package/dist/utils/stringToArrayBuffer.d.ts +0 -1
- package/dist/utils/stringToArrayBuffer.js +0 -12
- package/dist/utils/stringToArrayBuffer.js.map +0 -1
- package/dist/utils/textRandom.d.ts +0 -1
- package/dist/utils/textRandom.js +0 -15
- package/dist/utils/textRandom.js.map +0 -1
- package/dist/utils/toBase64.d.ts +0 -1
- package/dist/utils/toBase64.js +0 -7
- package/dist/utils/toBase64.js.map +0 -1
- package/dist/utils/uint8ArrayToHex.d.ts +0 -2
- package/dist/utils/uint8ArrayToHex.js +0 -7
- package/dist/utils/uint8ArrayToHex.js.map +0 -1
- package/dist/utils/uint8ToBase64.d.ts +0 -2
- package/dist/utils/uint8ToBase64.js +0 -15
- package/dist/utils/uint8ToBase64.js.map +0 -1
- package/dist/utils/uniq.d.ts +0 -1
- package/dist/utils/uniq.js +0 -7
- package/dist/utils/uniq.js.map +0 -1
- package/dist/utils/verifyMessage.d.ts +0 -8
- package/dist/utils/verifyMessage.js +0 -129
- package/dist/utils/verifyMessage.js.map +0 -1
- package/dist/utils/verifyPeer.d.ts +0 -2
- package/dist/utils/verifyPeer.js +0 -15
- package/dist/utils/verifyPeer.js.map +0 -1
- package/jest.config.ts +0 -195
- package/jest.setup.js +0 -0
- package/test-alice/000216.ldb +0 -0
- package/test-alice/000218.ldb +0 -0
- package/test-alice/000221.ldb +0 -0
- package/test-alice/000224.ldb +0 -0
- package/test-alice/000225.log +0 -0
- package/test-alice/CURRENT +0 -1
- package/test-alice/LOCK +0 -0
- package/test-alice/LOG +0 -5
- package/test-alice/LOG.old +0 -5
- package/test-alice/MANIFEST-000223 +0 -0
- package/test-base-client/000019.log +0 -0
- package/test-base-client/CURRENT +0 -1
- package/test-base-client/LOCK +0 -0
- package/test-base-client/LOG +0 -3
- package/test-base-client/LOG.old +0 -3
- package/test-base-client/MANIFEST-000018 +0 -0
- package/test-base-server/000019.log +0 -0
- package/test-base-server/CURRENT +0 -1
- package/test-base-server/LOCK +0 -0
- package/test-base-server/LOG +0 -3
- package/test-base-server/LOG.old +0 -3
- package/test-base-server/MANIFEST-000018 +0 -0
- package/test-bob/000231.ldb +0 -0
- package/test-bob/000233.ldb +0 -0
- package/test-bob/000234.log +0 -0
- package/test-bob/CURRENT +0 -1
- package/test-bob/LOCK +0 -0
- package/test-bob/LOG +0 -5
- package/test-bob/LOG.old +0 -14
- package/test-bob/MANIFEST-000232 +0 -0
- package/test-chris/000005.ldb +0 -0
- package/test-chris/000008.ldb +0 -0
- package/test-chris/000009.log +0 -0
- package/test-chris/CURRENT +0 -1
- package/test-chris/LOCK +0 -0
- package/test-chris/LOG +0 -5
- package/test-chris/LOG.old +0 -5
- package/test-chris/MANIFEST-000007 +0 -0
- package/test-node-a/000246.ldb +0 -0
- package/test-node-a/000248.ldb +0 -0
- package/test-node-a/000249.log +0 -0
- package/test-node-a/CURRENT +0 -1
- package/test-node-a/LOCK +0 -0
- package/test-node-a/LOG +0 -5
- package/test-node-a/LOG.old +0 -14
- package/test-node-a/MANIFEST-000247 +0 -0
- package/test-node-b/000246.ldb +0 -0
- package/test-node-b/000248.ldb +0 -0
- package/test-node-b/000249.log +0 -0
- package/test-node-b/CURRENT +0 -1
- package/test-node-b/LOCK +0 -0
- package/test-node-b/LOG +0 -5
- package/test-node-b/LOG.old +0 -14
- package/test-node-b/MANIFEST-000247 +0 -0
- package/test-store/000005.ldb +0 -0
- package/test-store/000008.ldb +0 -0
- package/test-store/000011.ldb +0 -0
- package/test-store/000014.ldb +0 -0
- package/test-store/000017.ldb +0 -0
- package/test-store/000018.log +0 -0
- package/test-store/CURRENT +0 -1
- package/test-store/LOCK +0 -0
- package/test-store/LOG +0 -6
- package/test-store/LOG.old +0 -6
- package/test-store/MANIFEST-000016 +0 -0
- package/test-verify-a/000059.ldb +0 -0
- package/test-verify-a/000066.log +0 -0
- package/test-verify-a/CURRENT +0 -1
- package/test-verify-a/LOCK +0 -0
- package/test-verify-a/LOG +0 -3
- package/test-verify-a/LOG.old +0 -3
- package/test-verify-a/MANIFEST-000065 +0 -0
package/README.md
CHANGED
|
@@ -1,74 +1 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
ToolDb is a peer-to-peer model for a decentralized database, inspired by GundB
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
It is the mix of a lot of cool concepts into one;
|
|
7
|
-
- Cryptographically secure.
|
|
8
|
-
- Offline first.
|
|
9
|
-
- Fully decentralized.
|
|
10
|
-
- Capable of providing realtime updates.
|
|
11
|
-
- Key-value/document storage.
|
|
12
|
-
- Works in the Browser and Nodejs seamlessly.
|
|
13
|
-
|
|
14
|
-
For a more detailed documentation head here;
|
|
15
|
-
|
|
16
|
-
[https://github.com/Manwe-777/tool-db-docs](https://github.com/Manwe-777/tool-db-docs)
|
|
17
|
-
|
|
18
|
-
_(No live version just yet!, until the protocol is more finalized)_
|
|
19
|
-
|
|
20
|
-
We push towards of the concept of federated servers, where we set up a p2p mesh network between any servers who want to join the swarm, and have those servers manage the connection and data sharing between them, while allowing client peers to connect to them to push updates to the database.
|
|
21
|
-
This setup IS NOT a requirement! It is just what I think is the best and most reliable way of creating a p2p network for dApps using both desktop and web compatible technologies. Any peer has the capability of connecting to any other peer trough websockets, and modifiying the code to allow webRtc connections between web peers is absolutely possible.
|
|
22
|
-
|
|
23
|
-
Since anyone can join a federated server swarm to help growing the network we use cryptography (basic public and private key authenthication and signature validation) to ensure all messages are coming from the real authors of the data that they intend to modify; this way by joining a swarm all peers in it can help validate without even having full data; even a new server peer can help, because all information is stored on each message and we dont rely on any centralized database to fetch users data.
|
|
24
|
-
|
|
25
|
-
Please check the [chain-swarm](https://github.com/Manwe-777/chain-swarm) repository to see how a federated server swarm would look like, Tool Db only handles the connection and messaging between peers, but it does not have any logic for peer discovery.
|
|
26
|
-
We do that using [discovery-channel](https://www.npmjs.com/package/discovery-channel), but you can use any DHT/networking solution you want! even WebRTC between browsers works using the `toolDbWebrtc` transport.
|
|
27
|
-
|
|
28
|
-
# Future work
|
|
29
|
-
|
|
30
|
-
While the database is currently functioning as expected, there are many things that could be added or improved to make it even better!
|
|
31
|
-
|
|
32
|
-
- Allow data encryption (proably built in), add methods for ECC encryption, shared keys, etc.
|
|
33
|
-
- Allow adding Noise/encryption to connections (probably based on the peer identity, requires initial keys exchange on connection)
|
|
34
|
-
|
|
35
|
-
# Install
|
|
36
|
-
|
|
37
|
-
You can install ToolDb via npm or yarn;
|
|
38
|
-
|
|
39
|
-
```
|
|
40
|
-
npm install tool-db
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
or use it via script in your html;
|
|
44
|
-
|
|
45
|
-
```
|
|
46
|
-
<script src="https://unpkg.com/tool-db/bundle.js"></script>
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
That will include all of ToolDb exports in `tooldb`, then you can use it like;
|
|
50
|
-
|
|
51
|
-
```
|
|
52
|
-
const { ToolDb, sha256 } = tooldb;
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
## Base usage
|
|
56
|
-
|
|
57
|
-
Creating a webrtc peers network is as easy as;
|
|
58
|
-
|
|
59
|
-
```
|
|
60
|
-
import { ToolDb, ToolDbWebrtc } from "tool-db";
|
|
61
|
-
|
|
62
|
-
const db = new ToolDb({
|
|
63
|
-
networkAdapter: ToolDbWebrtc,
|
|
64
|
-
debug: true,
|
|
65
|
-
});
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
From there you can put and get data using the api;
|
|
69
|
-
|
|
70
|
-
```
|
|
71
|
-
db.putData("foo-key", "var").then(console.log);
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
There is a lot more you can do, like subscribe for updates, built-in users credentials validation based on ECC, create federated networks, run servers on Nodejs, auto-replicate data trough them, etc. Make sure more you read trough the [documentation](https://github.com/Manwe-777/tool-db-docs)!
|
|
1
|
+
# `tool-db`
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ToolDb,
|
|
3
|
+
PingMessage,
|
|
4
|
+
ToolDbMessage,
|
|
5
|
+
Peer,
|
|
6
|
+
getPeerSignature,
|
|
7
|
+
textRandom,
|
|
8
|
+
uniq,
|
|
9
|
+
} from "..";
|
|
10
|
+
|
|
11
|
+
export default class ToolDbNetworkAdapter {
|
|
12
|
+
private _clientToSend: Record<string, (message: string) => void> = {};
|
|
13
|
+
|
|
14
|
+
private _isClientConnected: Record<string, () => boolean> = {};
|
|
15
|
+
|
|
16
|
+
private _clientIsServer: Record<string, boolean> = {};
|
|
17
|
+
|
|
18
|
+
private _tooldb: ToolDb;
|
|
19
|
+
|
|
20
|
+
constructor(db: ToolDb) {
|
|
21
|
+
this._tooldb = db;
|
|
22
|
+
|
|
23
|
+
if (this.tooldb.options.server) {
|
|
24
|
+
this.getMeAsPeer().then((meAsPeer) => {
|
|
25
|
+
this.tooldb.serverPeers.push(meAsPeer);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
get clientToSend() {
|
|
31
|
+
return this._clientToSend;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
get isClientConnected() {
|
|
35
|
+
return this._isClientConnected;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
get tooldb() {
|
|
39
|
+
return this._tooldb;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
public getMeAsPeer() {
|
|
43
|
+
const timestamp = new Date().getTime();
|
|
44
|
+
return getPeerSignature(
|
|
45
|
+
this.tooldb.peerAccount,
|
|
46
|
+
this.tooldb.options.topic,
|
|
47
|
+
timestamp,
|
|
48
|
+
this.tooldb.options.host,
|
|
49
|
+
this.tooldb.options.port
|
|
50
|
+
).then((signature) => {
|
|
51
|
+
return {
|
|
52
|
+
topic: this.tooldb.options.topic,
|
|
53
|
+
timestamp: timestamp,
|
|
54
|
+
host: this.tooldb.options.host,
|
|
55
|
+
port: this.tooldb.options.port,
|
|
56
|
+
address: this.tooldb.peerAccount.getAddress() || "",
|
|
57
|
+
sig: signature,
|
|
58
|
+
} as Peer;
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Check if the specified client is connected or not
|
|
64
|
+
* @param clientId Client ID
|
|
65
|
+
* @returns boolean
|
|
66
|
+
*/
|
|
67
|
+
public isConnected(clientId: string) {
|
|
68
|
+
return this._isClientConnected[clientId]
|
|
69
|
+
? this._isClientConnected[clientId]()
|
|
70
|
+
: false;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Check if the specified client is a server/relay
|
|
75
|
+
* @param clientId Client ID
|
|
76
|
+
* @returns boolean
|
|
77
|
+
*/
|
|
78
|
+
public isServer(clientId: string) {
|
|
79
|
+
return this._clientIsServer[clientId] || false;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
public craftPingMessage() {
|
|
83
|
+
return this.getMeAsPeer().then((meAsPeer) => {
|
|
84
|
+
return JSON.stringify({
|
|
85
|
+
type: "ping",
|
|
86
|
+
clientId: this.getClientAddress(),
|
|
87
|
+
to: [this.getClientAddress()],
|
|
88
|
+
isServer: this.tooldb.options.server,
|
|
89
|
+
id: textRandom(10),
|
|
90
|
+
peer: meAsPeer,
|
|
91
|
+
} as PingMessage);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Execute the function to send a message to the specified client ID
|
|
97
|
+
* @param clientId Client ID
|
|
98
|
+
* @param message Message
|
|
99
|
+
*/
|
|
100
|
+
private executeSendToClient(clientId: string, message: string) {
|
|
101
|
+
if (this._clientToSend[clientId]) {
|
|
102
|
+
this._clientToSend[clientId](message);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
public getClientAddress() {
|
|
107
|
+
// This is not a good idea to use on all adapters, so it should be replaced
|
|
108
|
+
// if its causing issues. The only reason we use the last 20 chars is to
|
|
109
|
+
// muse the same peer address as the webrtc adapter.
|
|
110
|
+
return (this.tooldb.peerAccount.getAddress() || "").slice(-20);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
public onClientDisconnect(clientId: string) {
|
|
114
|
+
delete this._clientToSend[clientId];
|
|
115
|
+
delete this._clientIsServer[clientId];
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Should be called as a message payload handler
|
|
120
|
+
* This function will take care of processing the messages and also making sure
|
|
121
|
+
* we use the correct response methods troughout the adapter.
|
|
122
|
+
* @param message message payload
|
|
123
|
+
* @param clientId Client ID (can be null for ping/pong)
|
|
124
|
+
* @param setClientId Callback to set the client id on the parent class
|
|
125
|
+
*/
|
|
126
|
+
public onClientMessage(
|
|
127
|
+
message: string,
|
|
128
|
+
clientId: string | null,
|
|
129
|
+
setClientId: (clientId: string) => void
|
|
130
|
+
) {
|
|
131
|
+
// this.tooldb.logger("onClientMessage", clientId);
|
|
132
|
+
|
|
133
|
+
if (clientId && !this.tooldb.processedOutHashes[clientId]) {
|
|
134
|
+
this.tooldb.processedOutHashes[clientId] = [];
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
try {
|
|
138
|
+
const parsedMessage = JSON.parse(message) as ToolDbMessage;
|
|
139
|
+
// We assume the first messages to arrive will always be ping or pong.
|
|
140
|
+
// Only after that we can get the client id for this socket.
|
|
141
|
+
if (parsedMessage.type === "ping" || parsedMessage.type === "pong") {
|
|
142
|
+
const cid = parsedMessage.clientId;
|
|
143
|
+
setClientId(cid);
|
|
144
|
+
this.tooldb.onPeerConnect(cid);
|
|
145
|
+
this._clientIsServer[cid] = parsedMessage.isServer;
|
|
146
|
+
this.tooldb.processedOutHashes[cid] = [];
|
|
147
|
+
this.tooldb.clientOnMessage(parsedMessage, cid);
|
|
148
|
+
} else if (clientId) {
|
|
149
|
+
this.tooldb.clientOnMessage(parsedMessage, clientId);
|
|
150
|
+
}
|
|
151
|
+
} catch (e) {
|
|
152
|
+
this.tooldb.logger("Got message ERR", message);
|
|
153
|
+
this.tooldb.logger(e);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Sends a message to all peers connected to us
|
|
159
|
+
* This function takes care of the message deduplication, making sure we dont send
|
|
160
|
+
* the same message twice to the same peer.
|
|
161
|
+
* @param msg message data
|
|
162
|
+
* @param crossServerOnly If this message should be send to server peers only
|
|
163
|
+
* @param isRelay if we should relay this message
|
|
164
|
+
*/
|
|
165
|
+
public sendToAll(msg: ToolDbMessage, crossServerOnly = false) {
|
|
166
|
+
const to = uniq([...msg.to, this.getClientAddress()]);
|
|
167
|
+
|
|
168
|
+
const finalMessage = JSON.stringify({ ...msg, to });
|
|
169
|
+
|
|
170
|
+
const filteredConns = Object.keys(this.clientToSend)
|
|
171
|
+
.filter((id) => !to.includes(id))
|
|
172
|
+
.filter((clientId) => this.isConnected(clientId));
|
|
173
|
+
|
|
174
|
+
filteredConns.forEach((clientId) => {
|
|
175
|
+
if ((crossServerOnly && this.isServer(clientId)) || !crossServerOnly) {
|
|
176
|
+
this.tooldb.logger(to, "Sent out to:", clientId);
|
|
177
|
+
|
|
178
|
+
if (msg.type === "put" || msg.type === "crdtPut") {
|
|
179
|
+
if (!this.tooldb.processedOutHashes[clientId].includes(msg.data.h)) {
|
|
180
|
+
this.executeSendToClient(clientId, finalMessage);
|
|
181
|
+
this.tooldb.processedOutHashes[clientId].push(msg.data.h);
|
|
182
|
+
}
|
|
183
|
+
} else {
|
|
184
|
+
this.executeSendToClient(clientId, finalMessage);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
// } else {
|
|
188
|
+
// this.tooldb.logger("Fitlered out;", clientId);
|
|
189
|
+
// }
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Sends a message to a single peer.
|
|
195
|
+
* This function also takes care of the message deduplication.
|
|
196
|
+
* @param clientId Peer/Client id we want to send to.
|
|
197
|
+
* @param msg message data
|
|
198
|
+
*/
|
|
199
|
+
public sendToClientId(clientId: string, msg: ToolDbMessage) {
|
|
200
|
+
const to = uniq([...msg.to, this.getClientAddress()]);
|
|
201
|
+
const finalMessage = JSON.stringify({ ...msg, to });
|
|
202
|
+
|
|
203
|
+
if (msg.type === "put" || msg.type === "crdtPut") {
|
|
204
|
+
if (
|
|
205
|
+
clientId &&
|
|
206
|
+
!this.tooldb.processedOutHashes[clientId].includes(msg.data.h)
|
|
207
|
+
) {
|
|
208
|
+
this.executeSendToClient(clientId, finalMessage);
|
|
209
|
+
this.tooldb.processedOutHashes[clientId].push(msg.data.h);
|
|
210
|
+
}
|
|
211
|
+
} else {
|
|
212
|
+
this.executeSendToClient(clientId, finalMessage);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ToolDb } from "..";
|
|
2
|
+
|
|
3
|
+
export default class ToolDbStorageAdapter {
|
|
4
|
+
private _tooldb: ToolDb;
|
|
5
|
+
|
|
6
|
+
private _forceStorageName: string | undefined;
|
|
7
|
+
|
|
8
|
+
get tooldb() {
|
|
9
|
+
return this._tooldb;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
get storageName() {
|
|
13
|
+
return this._forceStorageName || this._tooldb.options.storageName;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
constructor(db: ToolDb, forceStorageName?: string) {
|
|
17
|
+
this._tooldb = db;
|
|
18
|
+
|
|
19
|
+
if (forceStorageName) {
|
|
20
|
+
this._forceStorageName = forceStorageName;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
put(key: string, data: string): Promise<unknown> {
|
|
25
|
+
return Promise.resolve();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
get(key: string): Promise<string> {
|
|
29
|
+
return Promise.resolve("");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
query(key: string): Promise<string[]> {
|
|
33
|
+
return Promise.resolve([]);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { ToolDb, VerificationData } from "..";
|
|
2
|
+
|
|
3
|
+
export default class ToolDbUserAdapter {
|
|
4
|
+
private _tooldb: ToolDb;
|
|
5
|
+
|
|
6
|
+
get tooldb() {
|
|
7
|
+
return this._tooldb;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
constructor(db: ToolDb) {
|
|
11
|
+
this._tooldb = db;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
public anonUser() {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public setUser(account: unknown | undefined, name: string): void {}
|
|
19
|
+
|
|
20
|
+
public signData(data: string): Promise<string> {
|
|
21
|
+
return Promise.resolve("");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public verifySignature(
|
|
25
|
+
message: Partial<VerificationData<any>>
|
|
26
|
+
): Promise<boolean> {
|
|
27
|
+
return Promise.resolve(false);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public getAccountFromPrivate(privateKey: string): Promise<unknown> {
|
|
31
|
+
return Promise.resolve(undefined);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public encryptAccount(password: string): Promise<unknown> {
|
|
35
|
+
return Promise.resolve(undefined);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public decryptAccount(acc: unknown, password: string): Promise<any> {
|
|
39
|
+
return Promise.resolve(undefined);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
public getAddress(): string | undefined {
|
|
43
|
+
return "";
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public getUsername(): string | undefined {
|
|
47
|
+
return "";
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export const CRDT_MAP = "MAP";
|
|
2
|
+
|
|
3
|
+
export const CRDT_LIST = "LIST";
|
|
4
|
+
|
|
5
|
+
export const CRDT_COUNTER = "COUNTER";
|
|
6
|
+
|
|
7
|
+
export class BaseCrdt<T = any, Changes = any, Value = any> {
|
|
8
|
+
public type: string = "";
|
|
9
|
+
|
|
10
|
+
public mergeChanges(changes: Changes[]) {
|
|
11
|
+
//
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
public getChanges(): Changes[] {
|
|
15
|
+
return [];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
get value(): Value {
|
|
19
|
+
return "" as any;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { BaseCrdt, CRDT_LIST } from "..";
|
|
2
|
+
|
|
3
|
+
export type CounterOperations = "ADD" | "SUB";
|
|
4
|
+
|
|
5
|
+
export interface ChangeCounterBase {
|
|
6
|
+
t: CounterOperations; // Operation type
|
|
7
|
+
v: number; // Value
|
|
8
|
+
a: string;
|
|
9
|
+
i: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface AddCounterChange extends ChangeCounterBase {
|
|
13
|
+
t: "ADD";
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface SubCounterChange extends ChangeCounterBase {
|
|
17
|
+
t: "SUB";
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type CounterChanges = AddCounterChange | SubCounterChange;
|
|
21
|
+
|
|
22
|
+
export class CounterCrdt<T> extends BaseCrdt<number, CounterChanges, number> {
|
|
23
|
+
public type = CRDT_LIST;
|
|
24
|
+
|
|
25
|
+
public _changes: CounterChanges[] = [];
|
|
26
|
+
|
|
27
|
+
private _author = "";
|
|
28
|
+
|
|
29
|
+
private _value: number = 0;
|
|
30
|
+
|
|
31
|
+
private _lastUpdateSize: number = 0;
|
|
32
|
+
|
|
33
|
+
constructor(author: string, changes?: CounterChanges[]) {
|
|
34
|
+
super();
|
|
35
|
+
this._author = author;
|
|
36
|
+
if (changes) {
|
|
37
|
+
this.mergeChanges(changes);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
this.calculate();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
changesSort(a: CounterChanges, b: CounterChanges) {
|
|
44
|
+
if (a.i > b.i) return 1;
|
|
45
|
+
if (a.i < b.i) return -1;
|
|
46
|
+
return 0; // Should never be equal!
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
calculate() {
|
|
50
|
+
let temp: number = 0;
|
|
51
|
+
// Only update if we have new changes
|
|
52
|
+
if (Object.values(this._changes).length !== this._lastUpdateSize) {
|
|
53
|
+
this._changes.sort(this.changesSort).forEach((change) => {
|
|
54
|
+
if (change.t === "ADD") {
|
|
55
|
+
temp += change.v;
|
|
56
|
+
} else if (change.t === "SUB") {
|
|
57
|
+
temp -= change.v;
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
this._value = temp;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
get value(): number {
|
|
66
|
+
this.calculate();
|
|
67
|
+
return this._value;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
public mergeChanges(newChanges: CounterChanges[]) {
|
|
71
|
+
newChanges.forEach((change) => {
|
|
72
|
+
// Filter by author and index
|
|
73
|
+
const filtered = this._changes.filter(
|
|
74
|
+
(c) => c.i === change.i && c.a === change.a && c.t === change.t
|
|
75
|
+
);
|
|
76
|
+
// Only add if there are not matches
|
|
77
|
+
if (filtered.length === 0) {
|
|
78
|
+
this._changes.push(change);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
this.calculate();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
public getChanges(): CounterChanges[] {
|
|
85
|
+
return this._changes;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
public ADD(value: number) {
|
|
89
|
+
const ourChanges = this._changes.filter((c) => c.a === this._author);
|
|
90
|
+
|
|
91
|
+
this._changes.push({
|
|
92
|
+
t: "ADD",
|
|
93
|
+
v: value,
|
|
94
|
+
a: this._author,
|
|
95
|
+
i: ourChanges.length,
|
|
96
|
+
});
|
|
97
|
+
this.calculate();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
public SUB(value: number) {
|
|
101
|
+
const ourChanges = this._changes.filter((c) => c.a === this._author);
|
|
102
|
+
|
|
103
|
+
this._changes.push({
|
|
104
|
+
t: "SUB",
|
|
105
|
+
v: value,
|
|
106
|
+
a: this._author,
|
|
107
|
+
i: ourChanges.length,
|
|
108
|
+
});
|
|
109
|
+
this.calculate();
|
|
110
|
+
}
|
|
111
|
+
}
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { BaseCrdt, CRDT_LIST } from "..";
|
|
2
|
+
|
|
3
|
+
export type ListOperations = "INS" | "DEL";
|
|
4
|
+
|
|
5
|
+
export interface ChangeListBase<T> {
|
|
6
|
+
t: ListOperations; // Operation type
|
|
7
|
+
i: string; // "index", author + n
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface InsListChange<T> extends ChangeListBase<T> {
|
|
11
|
+
t: "INS";
|
|
12
|
+
v: T; // Value
|
|
13
|
+
p: string | undefined; // Previous index, if any
|
|
14
|
+
n: string | undefined; // Next index, if any
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface DelListChange<T> extends ChangeListBase<T> {
|
|
18
|
+
t: "DEL";
|
|
19
|
+
v: string; // target index to tombstone
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type ListChanges<T> = InsListChange<T> | DelListChange<T>;
|
|
23
|
+
|
|
24
|
+
interface ListTempCursor<T> {
|
|
25
|
+
value: T;
|
|
26
|
+
index: string;
|
|
27
|
+
tomb: boolean;
|
|
28
|
+
prev: string | undefined;
|
|
29
|
+
next: string | undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export class ListCrdt<T> extends BaseCrdt<T, ListChanges<T>, T[]> {
|
|
33
|
+
public type = CRDT_LIST;
|
|
34
|
+
|
|
35
|
+
public _changes: ListChanges<T>[] = [];
|
|
36
|
+
|
|
37
|
+
private _author = "";
|
|
38
|
+
|
|
39
|
+
private _value: T[] = [];
|
|
40
|
+
|
|
41
|
+
public _tempValues: ListTempCursor<T>[] = [];
|
|
42
|
+
|
|
43
|
+
private _lastUpdateSize: number = 0;
|
|
44
|
+
|
|
45
|
+
constructor(author: string, changes?: ListChanges<T>[]) {
|
|
46
|
+
super();
|
|
47
|
+
this._author = author;
|
|
48
|
+
if (changes) {
|
|
49
|
+
this.mergeChanges(changes);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
this.calculate();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
changesSort(a: ListChanges<T>, b: ListChanges<T>) {
|
|
56
|
+
if (a.i > b.i) return 1;
|
|
57
|
+
if (a.i < b.i) return -1;
|
|
58
|
+
return 0; // Should never be equal!
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
calculate() {
|
|
62
|
+
const temp: ListTempCursor<T>[] = [];
|
|
63
|
+
// Only update if we have new changes
|
|
64
|
+
if (Object.values(this._changes).length !== this._lastUpdateSize) {
|
|
65
|
+
this._changes.sort(this.changesSort).forEach((change) => {
|
|
66
|
+
if (change.t === "INS") {
|
|
67
|
+
let poisitionToInsert = 0;
|
|
68
|
+
if (change.p) {
|
|
69
|
+
poisitionToInsert = temp.findIndex((v) => v.index === change.p) + 1;
|
|
70
|
+
} else if (change.n) {
|
|
71
|
+
poisitionToInsert = temp.findIndex((v) => v.index === change.n);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const cursorValue: ListTempCursor<T> = {
|
|
75
|
+
value: change.v,
|
|
76
|
+
index: change.i,
|
|
77
|
+
tomb: false,
|
|
78
|
+
prev: change.p,
|
|
79
|
+
next: change.n,
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
temp.splice(poisitionToInsert, 0, cursorValue);
|
|
83
|
+
} else if (change.t === "DEL") {
|
|
84
|
+
const poisitionToInsert = temp.findIndex((v) => v.index === change.v);
|
|
85
|
+
temp[poisitionToInsert].tomb = true;
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
this._lastUpdateSize = Object.values(temp).length;
|
|
89
|
+
|
|
90
|
+
this._tempValues = temp;
|
|
91
|
+
|
|
92
|
+
this._value = temp.filter((v) => !v.tomb).map((v) => v.value);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
get value(): T[] {
|
|
97
|
+
this.calculate();
|
|
98
|
+
return this._value;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
public mergeChanges(newChanges: ListChanges<T>[]) {
|
|
102
|
+
newChanges.forEach((change) => {
|
|
103
|
+
// Filter by author and index
|
|
104
|
+
const filtered = this._changes.filter(
|
|
105
|
+
(c) => c.i === change.i && c.t === change.t && c.v === change.v
|
|
106
|
+
);
|
|
107
|
+
// Only add if there are not matches
|
|
108
|
+
if (filtered.length === 0) {
|
|
109
|
+
this._changes.push(change);
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
this.calculate();
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
public getChanges(): ListChanges<T>[] {
|
|
116
|
+
return this._changes;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
public INS(value: T, index: number) {
|
|
120
|
+
const filterTombs = this._tempValues.filter((v) => !v.tomb);
|
|
121
|
+
|
|
122
|
+
let currentPrev = undefined;
|
|
123
|
+
let currentNext = undefined;
|
|
124
|
+
|
|
125
|
+
if (filterTombs.length !== 0) {
|
|
126
|
+
const currentIndex = filterTombs[index].index;
|
|
127
|
+
const currentIndexPosition = this._tempValues.findIndex(
|
|
128
|
+
(v) => v.index === currentIndex
|
|
129
|
+
);
|
|
130
|
+
currentPrev = this._tempValues[currentIndexPosition - 1];
|
|
131
|
+
currentNext = this._tempValues[currentIndexPosition];
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const ourChanges = this._changes.filter((c) =>
|
|
135
|
+
c.i.startsWith(this._author)
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
this._changes.push({
|
|
139
|
+
t: "INS",
|
|
140
|
+
p: currentPrev?.index,
|
|
141
|
+
n: currentNext?.index,
|
|
142
|
+
v: value,
|
|
143
|
+
i: this._author + "-" + `${ourChanges.length}`.padStart(8, "0"),
|
|
144
|
+
});
|
|
145
|
+
this.calculate();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
public PUSH(value: T) {
|
|
149
|
+
const filterTombs = this._tempValues.filter((v) => !v.tomb);
|
|
150
|
+
const currentIndex = filterTombs[filterTombs.length - 1]?.index;
|
|
151
|
+
const currentIndexPosition = this._tempValues.findIndex(
|
|
152
|
+
(v) => v.index === currentIndex
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
const currentPrev = this._tempValues[currentIndexPosition];
|
|
156
|
+
|
|
157
|
+
const ourChanges = this._changes.filter((c) =>
|
|
158
|
+
c.i.startsWith(this._author)
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
this._changes.push({
|
|
162
|
+
t: "INS",
|
|
163
|
+
p: currentPrev?.index,
|
|
164
|
+
n: undefined,
|
|
165
|
+
v: value,
|
|
166
|
+
i: this._author + "-" + `${ourChanges.length}`.padStart(8, "0"),
|
|
167
|
+
});
|
|
168
|
+
this.calculate();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
public DEL(index: number) {
|
|
172
|
+
const currentIndex = this._tempValues.filter((v) => !v.tomb)[index].index;
|
|
173
|
+
const currentIndexPosition = this._tempValues.findIndex(
|
|
174
|
+
(v) => v.index === currentIndex
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
const current = this._tempValues[currentIndexPosition];
|
|
178
|
+
|
|
179
|
+
const ourChanges = this._changes.filter((c) =>
|
|
180
|
+
c.i.startsWith(this._author)
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
this._changes.push({
|
|
184
|
+
t: "DEL",
|
|
185
|
+
v: current.index,
|
|
186
|
+
i: this._author + "-" + `${ourChanges.length}`.padStart(8, "0"),
|
|
187
|
+
});
|
|
188
|
+
this.calculate();
|
|
189
|
+
}
|
|
190
|
+
}
|