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
|
@@ -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
|
+
}
|
package/lib/toolDbGet.ts
ADDED
|
@@ -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
|
+
}
|
package/lib/toolDbPut.ts
ADDED
|
@@ -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
|
+
}
|