new WS client connection, working from WASM too

pull/19/head
Niko PLP 2 years ago
parent 178a5ebba0
commit 9c970a8ceb
  1. 432
      Cargo.lock
  2. 12
      README.md
  3. 28
      ng-app-js/Cargo.toml
  4. 12
      ng-app-js/README.md
  5. 2
      ng-app-js/index.html
  6. 3
      ng-app-js/src/client_connection.rs
  7. 58
      ng-app-js/src/lib.rs
  8. 3
      ngcli/Cargo.toml
  9. 4
      ngcli/src/main.rs
  10. 3
      ngd/Cargo.toml
  11. 8
      ngd/src/main.rs
  12. 6
      p2p-broker/Cargo.toml
  13. 113
      p2p-broker/src/auth.rs
  14. 4
      p2p-broker/src/broker_store/config.rs
  15. 29
      p2p-broker/src/broker_store/peer.rs
  16. 28
      p2p-broker/src/broker_store/repostoreinfo.rs
  17. 213
      p2p-broker/src/server.rs
  18. 3
      p2p-broker/src/server_connection.rs
  19. 27
      p2p-broker/src/server_ws.rs
  20. 41
      p2p-client-ws/Cargo.toml
  21. 2
      p2p-client-ws/src/connection_ws.rs
  22. 53
      p2p-client-ws/src/lib.rs
  23. 330
      p2p-client-ws/src/remote_ws.rs
  24. 198
      p2p-client-ws/src/remote_ws_wasm.rs
  25. 5
      p2p-client/Cargo.toml
  26. 1
      p2p-client/src/lib.rs
  27. 5
      p2p-net/Cargo.toml
  28. 60
      p2p-net/src/actor.rs
  29. 2
      p2p-net/src/actors/mod.rs
  30. 20
      p2p-net/src/actors/noise.rs
  31. 84
      p2p-net/src/broker.rs
  32. 142
      p2p-net/src/connection.rs
  33. 41
      p2p-net/src/errors.rs
  34. 48
      p2p-net/src/lib.rs
  35. 75
      p2p-net/src/types.rs
  36. 32
      p2p-net/src/utils.rs
  37. 4
      p2p-repo/Cargo.toml
  38. 31
      p2p-repo/src/types.rs
  39. 2
      p2p-stores-lmdb/Cargo.toml
  40. 2
      p2p-stores-lmdb/src/broker_store.rs
  41. 2
      p2p-stores-lmdb/src/lib.rs
  42. 1
      p2p-stores-lmdb/src/repo_store.rs

432
Cargo.lock generated

@ -2,6 +2,41 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "aead"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b613b8e1e3cf911a086f53f03bf286f52fd7a7258e4fa606f0ef220d39d8877"
dependencies = [
"generic-array",
]
[[package]]
name = "aes"
version = "0.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e8b47f52ea9bae42228d07ec09eb676433d7c4ed1ebdf0f1d1c29ed446f1ab8"
dependencies = [
"cfg-if",
"cipher 0.3.0",
"cpufeatures",
"opaque-debug",
]
[[package]]
name = "aes-gcm"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df5f85a83a7d8b0442b6aa7b504b8212c1733da07b98aae43d4bc21b2cb3cdf6"
dependencies = [
"aead",
"aes",
"cipher 0.3.0",
"ctr",
"ghash",
"subtle",
]
[[package]]
name = "anstyle"
version = "0.3.1"
@ -140,6 +175,24 @@ dependencies = [
"futures-micro",
]
[[package]]
name = "async-process"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6381ead98388605d0d9ff86371043b5aa922a3905824244de40dc263a14fcba4"
dependencies = [
"async-io",
"async-lock",
"autocfg",
"blocking",
"cfg-if",
"event-listener",
"futures-lite",
"libc",
"signal-hook",
"windows-sys 0.42.0",
]
[[package]]
name = "async-std"
version = "1.12.0"
@ -151,6 +204,7 @@ dependencies = [
"async-global-executor",
"async-io",
"async-lock",
"async-process",
"crossbeam-utils",
"futures-channel",
"futures-core",
@ -198,6 +252,17 @@ dependencies = [
"tungstenite",
]
[[package]]
name = "async_io_stream"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6d7b9decdf35d8908a7e3ef02f64c5e9b1695e230154c0e8de3969142d9b94c"
dependencies = [
"futures",
"pharos",
"rustc_version",
]
[[package]]
name = "atomic-waker"
version = "1.1.0"
@ -231,6 +296,15 @@ version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]]
name = "blake2"
version = "0.10.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46502ad458c9a52b69d4d4d32775c788b7a1b85e8bc9d482d92250fc0e3f8efe"
dependencies = [
"digest 0.10.6",
]
[[package]]
name = "blake3"
version = "1.3.3"
@ -245,6 +319,16 @@ dependencies = [
"digest 0.10.6",
]
[[package]]
name = "blob-uuid"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "83fc15853171b33280f5614e77f5fa4debd33f51a86c44daa4ba3d759674c561"
dependencies = [
"base64",
"uuid 1.3.0",
]
[[package]]
name = "block-buffer"
version = "0.9.0"
@ -319,6 +403,18 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chacha20"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c80e5460aa66fe3b91d40bcbdab953a597b60053e34d684ac6903f863b680a6"
dependencies = [
"cfg-if",
"cipher 0.3.0",
"cpufeatures",
"zeroize",
]
[[package]]
name = "chacha20"
version = "0.9.0"
@ -326,10 +422,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7fc89c7c5b9e7a02dfe45cd2367bae382f9ed31c61ca8debe5f827c420a2f08"
dependencies = [
"cfg-if",
"cipher",
"cipher 0.4.4",
"cpufeatures",
]
[[package]]
name = "chacha20poly1305"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a18446b09be63d457bbec447509e85f662f32952b035ce892290396bc0b0cff5"
dependencies = [
"aead",
"chacha20 0.8.2",
"cipher 0.3.0",
"poly1305",
"zeroize",
]
[[package]]
name = "cipher"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ee52072ec15386f770805afd189a01c8841be8696bed250fa2f13c4c0d6dfb7"
dependencies = [
"generic-array",
]
[[package]]
name = "cipher"
version = "0.4.4"
@ -349,6 +467,16 @@ dependencies = [
"crossbeam-utils",
]
[[package]]
name = "console_error_panic_hook"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc"
dependencies = [
"cfg-if",
"wasm-bindgen",
]
[[package]]
name = "constant_time_eq"
version = "0.2.5"
@ -393,6 +521,15 @@ dependencies = [
"syn",
]
[[package]]
name = "ctr"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "049bb91fb4aaf0e3c7efa6cd5ef877dbbbd15b39dad06d9948de4ec8a75761ea"
dependencies = [
"cipher 0.3.0",
]
[[package]]
name = "cuckoofilter"
version = "0.5.0"
@ -417,6 +554,20 @@ dependencies = [
"zeroize",
]
[[package]]
name = "curve25519-dalek"
version = "4.0.0-rc.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d4ba9852b42210c7538b75484f9daa0655e9a3ac04f693747bb0f02cf3cfe16"
dependencies = [
"cfg-if",
"fiat-crypto",
"packed_simd_2",
"platforms",
"subtle",
"zeroize",
]
[[package]]
name = "debug_print"
version = "1.0.0"
@ -470,11 +621,11 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c762bae6dcaf24c4c84667b8579785430908723d5c889f469d76a41d59cc7a9d"
dependencies = [
"curve25519-dalek",
"curve25519-dalek 3.2.1",
"ed25519",
"rand 0.7.3",
"serde",
"sha2",
"sha2 0.9.9",
"zeroize",
]
@ -538,6 +689,12 @@ dependencies = [
"instant",
]
[[package]]
name = "fiat-crypto"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93ace6ec7cc19c8ed33a32eaa9ea692d7faea05006b5356b9e2b668ec4bc3955"
[[package]]
name = "fnv"
version = "1.0.7"
@ -683,8 +840,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce"
dependencies = [
"cfg-if",
"js-sys",
"libc",
"wasi 0.9.0+wasi-snapshot-preview1",
"wasm-bindgen",
]
[[package]]
@ -700,6 +859,16 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "ghash"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1583cc1656d7839fd3732b80cf4f38850336cdb9b8ded1cd399ca62958de3c99"
dependencies = [
"opaque-debug",
"polyval",
]
[[package]]
name = "gloo-timers"
version = "0.2.6"
@ -840,6 +1009,12 @@ version = "0.2.140"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99227334921fae1a979cf0bfdfcc6b3e5ce376ef57e16fb6fb3ea2ed6095f80c"
[[package]]
name = "libm"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fc7aa29613bd6a620df431842069224d8bc9011086b1db4c0e0cd47fa03ec9a"
[[package]]
name = "linux-raw-sys"
version = "0.1.4"
@ -897,8 +1072,21 @@ checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
name = "ng-app-js-sdk"
version = "0.1.0"
dependencies = [
"getrandom 0.2.8",
"async-std",
"debug_print",
"futures",
"getrandom 0.1.16",
"p2p-client-ws",
"p2p-net",
"p2p-repo",
"pharos",
"serde",
"serde_bare",
"serde_bytes",
"snow",
"wasm-bindgen",
"wasm-bindgen-test",
"ws_stream_wasm",
]
[[package]]
@ -913,6 +1101,7 @@ dependencies = [
"futures",
"p2p-broker",
"p2p-client",
"p2p-client-ws",
"p2p-net",
"p2p-repo",
"p2p-stores-lmdb",
@ -927,6 +1116,7 @@ version = "0.1.0"
dependencies = [
"async-std",
"p2p-broker",
"p2p-net",
]
[[package]]
@ -988,7 +1178,7 @@ dependencies = [
"async-std",
"async-trait",
"async-tungstenite",
"chacha20",
"chacha20 0.9.0",
"debug_print",
"futures",
"getrandom 0.2.8",
@ -1006,20 +1196,46 @@ dependencies = [
[[package]]
name = "p2p-client"
version = "0.1.0"
dependencies = [
"async-channel",
"async-oneshot",
"async-std",
"async-trait",
"chacha20 0.9.0",
"debug_print",
"futures",
"p2p-net",
"p2p-repo",
"serde",
"serde_bare",
"serde_bytes",
"snow",
"xactor",
]
[[package]]
name = "p2p-client-ws"
version = "0.1.0"
dependencies = [
"async-channel",
"async-oneshot",
"async-std",
"async-trait",
"async-tungstenite",
"chacha20",
"chacha20 0.9.0",
"debug_print",
"futures",
"getrandom 0.2.8",
"p2p-client",
"p2p-net",
"p2p-repo",
"pharos",
"serde",
"serde_bare",
"serde_bytes",
"wasm-bindgen",
"wasm-bindgen-test",
"ws_stream_wasm",
"xactor",
]
@ -1028,6 +1244,7 @@ name = "p2p-net"
version = "0.1.0"
dependencies = [
"async-broadcast",
"async-std",
"async-trait",
"blake3",
"debug_print",
@ -1037,6 +1254,8 @@ dependencies = [
"serde",
"serde_bare",
"serde_bytes",
"unique_id",
"wasm-bindgen",
]
[[package]]
@ -1044,7 +1263,7 @@ name = "p2p-repo"
version = "0.1.0"
dependencies = [
"blake3",
"chacha20",
"chacha20 0.9.0",
"debug_print",
"ed25519-dalek",
"fastbloom-rs",
@ -1073,7 +1292,7 @@ name = "p2p-verifier"
version = "0.1.0"
dependencies = [
"blake3",
"chacha20",
"chacha20 0.9.0",
"p2p-net",
"p2p-repo",
"serde",
@ -1081,6 +1300,16 @@ dependencies = [
"serde_bytes",
]
[[package]]
name = "packed_simd_2"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1914cd452d8fccd6f9db48147b29fd4ae05bea9dc5d9ad578509f72415de282"
dependencies = [
"cfg-if",
"libm",
]
[[package]]
name = "parking"
version = "2.0.0"
@ -1122,6 +1351,16 @@ version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e"
[[package]]
name = "pharos"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e9567389417feee6ce15dd6527a8a1ecac205ef62c2932bcf3d9f6fc5b78b414"
dependencies = [
"futures",
"rustc_version",
]
[[package]]
name = "pin-project-lite"
version = "0.2.9"
@ -1140,6 +1379,12 @@ version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160"
[[package]]
name = "platforms"
version = "3.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3d7ddaed09e0eb771a79ab0fd64609ba0afb0a8366421957936ad14cbd13630"
[[package]]
name = "polling"
version = "2.5.2"
@ -1154,6 +1399,29 @@ dependencies = [
"windows-sys 0.42.0",
]
[[package]]
name = "poly1305"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "048aeb476be11a4b6ca432ca569e375810de9294ae78f4774e78ea98a9246ede"
dependencies = [
"cpufeatures",
"opaque-debug",
"universal-hash",
]
[[package]]
name = "polyval"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8419d2b623c7c0896ff2d5d96e2cb4ede590fed28fcc34934f4c33c036e620a1"
dependencies = [
"cfg-if",
"cpufeatures",
"opaque-debug",
"universal-hash",
]
[[package]]
name = "ppv-lite86"
version = "0.2.17"
@ -1321,7 +1589,7 @@ dependencies = [
"serde_derive",
"thiserror",
"url",
"uuid",
"uuid 0.8.2",
]
[[package]]
@ -1343,6 +1611,15 @@ dependencies = [
"syn",
]
[[package]]
name = "rustc_version"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366"
dependencies = [
"semver",
]
[[package]]
name = "rustix"
version = "0.36.9"
@ -1357,12 +1634,30 @@ dependencies = [
"windows-sys 0.45.0",
]
[[package]]
name = "scoped-tls"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294"
[[package]]
name = "scopeguard"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "semver"
version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bebd363326d05ec3e2f532ab7660680f3b02130d780c299bca73469d521bc0ed"
[[package]]
name = "send_wrapper"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd0b0ec5f1c1ca621c432a25813d8d60c88abe6d3e08a3eb9cf37d97a0fe3d73"
[[package]]
name = "serde"
version = "1.0.142"
@ -1425,6 +1720,36 @@ dependencies = [
"opaque-debug",
]
[[package]]
name = "sha2"
version = "0.10.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "82e6b795fe2e3b1e845bafcb27aa35405c4d47cdfc92af5fc8d3002f76cebdc0"
dependencies = [
"cfg-if",
"cpufeatures",
"digest 0.10.6",
]
[[package]]
name = "signal-hook"
version = "0.3.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "732768f1176d21d09e076c23a93123d40bba92d50c4058da34d45c8de8e682b9"
dependencies = [
"libc",
"signal-hook-registry",
]
[[package]]
name = "signal-hook-registry"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1"
dependencies = [
"libc",
]
[[package]]
name = "signature"
version = "1.6.4"
@ -1446,6 +1771,22 @@ version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0"
[[package]]
name = "snow"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ccba027ba85743e09d15c03296797cad56395089b832b48b5a5217880f57733"
dependencies = [
"aes-gcm",
"blake2",
"chacha20poly1305",
"curve25519-dalek 4.0.0-rc.1",
"rand_core 0.6.4",
"rustc_version",
"sha2 0.10.6",
"subtle",
]
[[package]]
name = "socket2"
version = "0.4.9"
@ -1608,6 +1949,27 @@ version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c"
[[package]]
name = "unique_id"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae605c39dfbdec433798d4a8b03ffbac711dc51cdeb1ba5c725bdcaf24e464cc"
dependencies = [
"blob-uuid",
"lazy_static",
"uuid 1.3.0",
]
[[package]]
name = "universal-hash"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f214e8f697e925001e66ec2c6e37a4ef93f0f78c2eed7814394e10c62025b05"
dependencies = [
"generic-array",
"subtle",
]
[[package]]
name = "url"
version = "2.3.1"
@ -1631,6 +1993,15 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7"
[[package]]
name = "uuid"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1674845326ee10d37ca60470760d4288a6f80f304007d92e5c53bab78c9cfd79"
dependencies = [
"getrandom 0.2.8",
]
[[package]]
name = "value-bag"
version = "1.0.0-alpha.9"
@ -1740,6 +2111,30 @@ version = "0.2.84"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0046fef7e28c3804e5e38bfa31ea2a0f73905319b677e57ebe37e49358989b5d"
[[package]]
name = "wasm-bindgen-test"
version = "0.3.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6db36fc0f9fb209e88fb3642590ae0205bb5a56216dabd963ba15879fe53a30b"
dependencies = [
"console_error_panic_hook",
"js-sys",
"scoped-tls",
"wasm-bindgen",
"wasm-bindgen-futures",
"wasm-bindgen-test-macro",
]
[[package]]
name = "wasm-bindgen-test-macro"
version = "0.3.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0734759ae6b3b1717d661fe4f016efcfb9828f5edb4520c18eaee05af3b43be9"
dependencies = [
"proc-macro2",
"quote",
]
[[package]]
name = "web-sys"
version = "0.3.61"
@ -1871,6 +2266,25 @@ dependencies = [
"memchr",
]
[[package]]
name = "ws_stream_wasm"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7999f5f4217fe3818726b66257a4475f71e74ffd190776ad053fa159e50737f5"
dependencies = [
"async_io_stream",
"futures",
"js-sys",
"log",
"pharos",
"rustc_version",
"send_wrapper",
"thiserror",
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
]
[[package]]
name = "xactor"
version = "0.7.11"

@ -32,6 +32,7 @@ Read our [getting started guide](https://docs.nextgraph.org/en/getting-started/)
```
cargo install wasm-pack
// optionally, if you want a Rust REPL: cargo install evcxr_repl
git clone git@git.nextgraph.org:NextGraph/nextgraph-rs.git
cd nextgraph-rs
cargo build
@ -84,6 +85,17 @@ Test end-to-end client and server:
cargo test --package ngcli -- --nocapture
```
Test WASM websocket
```
cd ng-app-js
wasm-pack test --chrome --headless
```
Test Rust websocket
```
cargo test --package p2p-client-ws --lib -- --nocapture
```
### Build release binaries
```

@ -15,9 +15,27 @@ crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "0.2"
getrandom = "0.2.7"
# p2p-client = { path = "../p2p-client" }
ws_stream_wasm = "0.7"
p2p-net = { path = "../p2p-net" }
p2p-repo = { path = "../p2p-repo" }
p2p-client-ws = { path = "../p2p-client-ws" }
async-std = { version = "1.12.0", features = ["attributes","unstable"] }
futures = "0.3.24"
pharos = "0.5"
debug_print = "1.0.0"
serde = { version = "1.0", features = ["derive"] }
serde_bare = "0.5.0"
serde_bytes = "0.11.7"
snow = "0.9.2"
getrandom = { version = "0.1.1", features = ["wasm-bindgen"] }
[target.'cfg(target_arch = "wasm32")'.dependencies.getrandom]
version = "0.2.7"
features = ["js"]
# [target.'cfg(target_arch = "wasm32")'.dependencies.getrandom]
# version = "0.2.7"
# features = ["js"]
# [target.'cfg(target_arch = "wasm32")'.dependencies]
# wasm-bindgen-futures = "0.4.34"
[dev-dependencies]
wasm-bindgen-test = "^0.3"

@ -50,6 +50,18 @@ cd pkg-node
// npm publish --access=public
```
For testing in vanilla JS
```
wasm-pack build --target web -d web
python3 -m http.server
// open http://localhost:8000
```
Or automated testing with headless chrome:
```
wasm-pack test --chrome --headless
```
### Plain JS web app
```

@ -18,7 +18,7 @@
<body>
<p>run <code>python3 -m http.server</code> to use it</p>
<script type="module">
import init, { greet } from "./pkg/ng_app_js_sdk.js";
import init, { greet } from "./web/ng_app_js_sdk.js";
init().then(() => {
greet("WebAssembly");
});

@ -0,0 +1,3 @@
pub struct ClientConnection {}
impl ClientConnection {}

@ -1,13 +1,56 @@
use async_std::task;
#[cfg(target_arch = "wasm32")]
use p2p_client_ws::remote_ws_wasm::ConnectionWebSocket;
use p2p_net::broker::*;
use p2p_net::log;
use p2p_net::types::IP;
use p2p_net::utils::{spawn_and_log_error, ResultSend};
use p2p_repo::utils::generate_keypair;
use std::net::IpAddr;
use std::str::FromStr;
use std::sync::Arc;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern {
extern "C" {
pub fn alert(s: &str);
}
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn greet(name: &str) {
log!("I say: {}", name);
let mut random_buf = [0u8; 32];
getrandom::getrandom(&mut random_buf).unwrap();
//spawn_and_log_error(testt("ws://127.0.0.1:3012"));
async fn method() -> ResultSend<()> {
log!("start connecting");
let cnx = Arc::new(ConnectionWebSocket {});
let (priv_key, pub_key) = generate_keypair();
let broker = Broker::new();
let res = broker
.connect(
cnx,
IP::try_from(&IpAddr::from_str("127.0.0.1").unwrap()).unwrap(),
None,
priv_key,
pub_key,
pub_key,
)
.await;
log!("broker.connect : {:?}", res);
//res.expect_throw("assume the connection succeeds");
Ok(())
}
spawn_and_log_error(method());
//spawn_and_log_error(Arc::clone(&cnx).open("ws://127.0.0.1:3012", priv_key, pub_key));
}
#[cfg(not(target_arch = "wasm32"))]
#[wasm_bindgen]
pub fn greet(name: &str) {
alert(&format!("I say: {}", name));
task::spawn(async move {});
}
#[wasm_bindgen]
@ -16,3 +59,16 @@ pub fn change(name: &str) -> JsValue {
getrandom::getrandom(&mut random_buf).unwrap();
JsValue::from_str(&format!("Hellooo, {}!", name))
}
#[cfg(target_arch = "wasm32")]
#[cfg(test)]
mod test {
use wasm_bindgen_test::*;
wasm_bindgen_test_configure!(run_in_browser);
use crate::greet;
#[wasm_bindgen_test]
pub async fn test_greet() {
greet("test");
}
}

@ -11,10 +11,11 @@ repository = "https://git.nextgraph.org/NextGraph/nextgraph-rs"
debug_print = "1.0.0"
p2p-repo = { path = "../p2p-repo" }
p2p-net = { path = "../p2p-net" }
p2p-client-ws = { path = "../p2p-client-ws" }
p2p-client = { path = "../p2p-client" }
p2p-broker = { path = "../p2p-broker" }
p2p-stores-lmdb = { path = "../p2p-stores-lmdb" }
async-std = { version = "1.7.0", features = ["attributes"] }
async-std = { version = "1.12.0", features = ["attributes"] }
futures = "0.3.24"
xactor = "0.7.11"
tempfile = "3"

@ -29,7 +29,7 @@ use p2p_net::errors::*;
use p2p_net::types::*;
use p2p_net::broker_connection::*;
use p2p_client::connection_remote::*;
use p2p_client::connection_ws::*;
use p2p_client_ws::connection_ws::*;
use p2p_broker::connection_local::*;
fn block_size() -> usize {
@ -471,6 +471,8 @@ async fn test(cnx: &mut impl BrokerConnection, pub_key: PubKey, priv_key: PrivKe
.overlay_connect(&repo, true)
.await?;
debug_println!("put_block");
let my_block_id = public_overlay_cnx
.put_block(&Block::new(
vec![],

@ -9,4 +9,5 @@ repository = "https://git.nextgraph.org/NextGraph/nextgraph-rs"
[dependencies]
p2p-broker = { path = "../p2p-broker" }
async-std = { version = "1.7.0", features = ["attributes"] }
p2p-net = { path = "../p2p-net" }
async-std = { version = "1.12.0", features = ["attributes"] }

@ -1,18 +1,20 @@
// Copyright (c) 2022-2023 Niko Bonnieure, Par le Peuple, NextGraph.org developers
// All rights reserved.
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE2 or http://www.apache.org/licenses/LICENSE-2.0>
// <LICENSE-APACHE2 or http://www.apache.org/licenses/LICENSE-2.0>
// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
// at your option. All files in the project carrying such
// notice may not be copied, modified, or distributed except
// according to those terms.
use p2p_broker::server_ws::run_server;
use p2p_net::WS_PORT;
#[async_std::main]
async fn main() -> std::io::Result<()> {
println!("Starting NextGraph daemon...");
run_server("127.0.0.1:3012").await
run_server(format!("127.0.0.1:{}", WS_PORT).as_str()).await?;
Ok(())
}

@ -16,7 +16,7 @@ chacha20 = "0.9.0"
serde = { version = "1.0", features = ["derive"] }
serde_bare = "0.5.0"
serde_bytes = "0.11.7"
async-std = { version = "1.7.0", features = ["attributes"] }
async-std = { version = "1.12.0", features = ["attributes"] }
futures = "0.3.24"
rust-fsm = "0.6.0"
getrandom = "0.2.7"
@ -25,7 +25,3 @@ tempfile = "3"
hex = "0.4.3"
async-trait = "0.1.64"
async-tungstenite = { version = "0.17.2", features = ["async-std-runtime"] }
[target.'cfg(target_arch = "wasm32")'.dependencies.getrandom]
version = "0.2.7"
features = ["js"]

@ -19,37 +19,38 @@ use p2p_net::errors::*;
use p2p_net::types::*;
use rust_fsm::*;
state_machine! {
derive(Debug)
AuthProtocolClient(Ready)
Ready(ClientHelloSent) => ClientHelloSent,
ClientHelloSent(ServerHelloReceived) => ServerHelloReceived,
ServerHelloReceived(ClientAuthSent) => ClientAuthSent,
ClientAuthSent(AuthResultReceived) => AuthResult,
AuthResult => {
Ok => BrokerProtocol,
Error => Closed,
},
}
state_machine! {
derive(Debug)
AuthProtocolServer(Ready)
Ready(ClientHelloReceived) => ClientHelloReceived,
ClientHelloReceived(ServerHelloSent) => ServerHelloSent,
ServerHelloSent(ClientAuthReceived) => ClientAuthReceived,
ClientAuthReceived => {
Ok => AuthResultOk,
Error => AuthResultError,
},
AuthResultOk(AuthResultSent) => BrokerProtocol,
AuthResultError(AuthResultSent) => Closed,
}
// state_machine! {
// derive(Debug)
// AuthProtocolClient(Ready)
// Ready(ClientHelloSent) => ClientHelloSent,
// ClientHelloSent(ServerHelloReceived) => ServerHelloReceived,
// ServerHelloReceived(ClientAuthSent) => ClientAuthSent,
// ClientAuthSent(AuthResultReceived) => AuthResult,
// AuthResult => {
// Ok => BrokerProtocol,
// Error => Closed,
// },
// }
// state_machine! {
// derive(Debug)
// AuthProtocolServer(Ready)
// Ready(ClientHelloReceived) => ClientHelloReceived,
// ClientHelloReceived(ServerHelloSent) => ServerHelloSent,
// ServerHelloSent(ClientAuthReceived) => ClientAuthReceived,
// ClientAuthReceived => {
// Ok => AuthResultOk,
// Error => AuthResultError,
// },
// AuthResultOk(AuthResultSent) => BrokerProtocol,
// AuthResultError(AuthResultSent) => Closed,
// }
#[derive(Debug)]
pub struct AuthProtocolHandler {
machine: StateMachine<AuthProtocolServer>,
//machine: StateMachine<AuthProtocolServer>,
nonce: Option<Vec<u8>>,
user: Option<PubKey>,
}
@ -57,7 +58,7 @@ pub struct AuthProtocolHandler {
impl AuthProtocolHandler {
pub fn new() -> AuthProtocolHandler {
AuthProtocolHandler {
machine: StateMachine::new(),
//machine: StateMachine::new(),
nonce: None,
user: None,
}
@ -68,10 +69,10 @@ impl AuthProtocolHandler {
}
pub fn handle_init(&mut self, client_hello: ClientHello) -> Result<Vec<u8>, ProtocolError> {
let _ = self
.machine
.consume(&AuthProtocolServerInput::ClientHelloReceived)
.map_err(|_e| ProtocolError::InvalidState)?;
// let _ = self
// .machine
// .consume(&AuthProtocolServerInput::ClientHelloReceived)
// .map_err(|_e| ProtocolError::InvalidState)?;
let mut random_buf = [0u8; 32];
getrandom::getrandom(&mut random_buf).unwrap();
@ -81,10 +82,10 @@ impl AuthProtocolHandler {
});
self.nonce = Some(nonce);
let _ = self
.machine
.consume(&AuthProtocolServerInput::ServerHelloSent)
.map_err(|_e| ProtocolError::InvalidState)?;
// let _ = self
// .machine
// .consume(&AuthProtocolServerInput::ServerHelloSent)
// .map_err(|_e| ProtocolError::InvalidState)?;
//debug_println!("sending nonce to client: {:?}", self.nonce);
@ -110,13 +111,13 @@ impl AuthProtocolHandler {
handler: &mut AuthProtocolHandler,
frame: Vec<u8>,
) -> Result<Vec<u8>, ProtocolError> {
match handler.machine.state() {
&AuthProtocolServerState::ServerHelloSent => {
// match handler.machine.state() {
// &AuthProtocolServerState::ServerHelloSent => {
let message = serde_bare::from_slice::<ClientAuth>(&frame)?;
let _ = handler
.machine
.consume(&AuthProtocolServerInput::ClientAuthReceived)
.map_err(|_e| ProtocolError::InvalidState)?;
// let _ = handler
// .machine
// .consume(&AuthProtocolServerInput::ClientAuthReceived)
// .map_err(|_e| ProtocolError::InvalidState)?;
// verifying client auth
@ -136,10 +137,10 @@ impl AuthProtocolHandler {
// );
if message.nonce() != handler.nonce.as_ref().unwrap() {
let _ = handler
.machine
.consume(&AuthProtocolServerInput::Error)
.map_err(|_e| ProtocolError::InvalidState);
// let _ = handler
// .machine
// .consume(&AuthProtocolServerInput::Error)
// .map_err(|_e| ProtocolError::InvalidState);
return Err(ProtocolError::AccessDenied);
}
@ -147,17 +148,17 @@ impl AuthProtocolHandler {
// TODO check that the device has been registered for this user. if not, return AccessDenied
// all is good, we advance the FSM and send back response
let _ = handler
.machine
.consume(&AuthProtocolServerInput::Ok)
.map_err(|_e| ProtocolError::InvalidState)?;
// let _ = handler
// .machine
// .consume(&AuthProtocolServerInput::Ok)
// .map_err(|_e| ProtocolError::InvalidState)?;
handler.user = Some(message.user());
Ok(vec![]) // without any metadata
}
_ => Err(ProtocolError::InvalidState),
}
//}
//_ => Err(ProtocolError::InvalidState),
//}
}
let res = process_state(self, frame);

@ -1,7 +1,7 @@
// Copyright (c) 2022-2023 Niko Bonnieure, Par le Peuple, NextGraph.org developers
// All rights reserved.
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE2 or http://www.apache.org/licenses/LICENSE-2.0>
// <LICENSE-APACHE2 or http://www.apache.org/licenses/LICENSE-2.0>
// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
// at your option. All files in the project carrying such
// notice may not be copied, modified, or distributed except
@ -9,10 +9,10 @@
//! Broker Config, persisted to store
use p2p_net::types::*;
use p2p_repo::broker_store::BrokerStore;
use p2p_repo::store::*;
use p2p_repo::types::*;
use p2p_net::types::*;
use serde::{Deserialize, Serialize};
use serde_bare::{from_slice, to_vec};

@ -1,7 +1,7 @@
// Copyright (c) 2022-2023 Niko Bonnieure, Par le Peuple, NextGraph.org developers
// All rights reserved.
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE2 or http://www.apache.org/licenses/LICENSE-2.0>
// <LICENSE-APACHE2 or http://www.apache.org/licenses/LICENSE-2.0>
// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
// at your option. All files in the project carrying such
// notice may not be copied, modified, or distributed except
@ -9,10 +9,10 @@
//! Peer
use p2p_net::types::*;
use p2p_repo::broker_store::BrokerStore;
use p2p_repo::store::*;
use p2p_repo::types::*;
use p2p_net::types::*;
use serde::{Deserialize, Serialize};
use serde_bare::{from_slice, to_vec};
@ -79,13 +79,13 @@ impl<'a> Peer<'a> {
Self::PREFIX,
&to_vec(&id)?,
Some(Self::VERSION),
& to_vec(&advert.version())?,
&to_vec(&advert.version())?,
)?;
tx.put(
Self::PREFIX,
&to_vec(&id)?,
Some(Self::ADVERT),
& to_vec(&advert)?,
&to_vec(&advert)?,
)?;
Ok(())
})?;
@ -131,12 +131,21 @@ impl<'a> Peer<'a> {
if current_advert.version() >= advert.version() {
return Ok(());
}
self.store.replace(
Self::PREFIX,
&to_vec(&self.id)?,
Some(Self::ADVERT),
to_vec(advert)?,
)
self.store.write_transaction(&|tx| {
tx.replace(
Self::PREFIX,
&to_vec(&self.id)?,
Some(Self::VERSION),
&to_vec(&advert.version())?,
)?;
tx.replace(
Self::PREFIX,
&to_vec(&self.id)?,
Some(Self::ADVERT),
&to_vec(&advert)?,
)?;
Ok(())
})
}
pub fn advert(&self) -> Result<PeerAdvert, StorageError> {
match self

@ -19,21 +19,21 @@ use p2p_net::types::*;
use serde::{Deserialize, Serialize};
use serde_bare::{from_slice, to_vec};
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum RepoStoreId {
Overlay(OverlayId),
Repo(PubKey),
}
// #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
// pub enum RepoStoreId {
// Overlay(OverlayId),
// Repo(PubKey),
// }
impl From<RepoStoreId> for String {
fn from(id: RepoStoreId) -> Self {
hex::encode(to_vec(&id).unwrap())
}
}
// impl From<RepoStoreId> for String {
// fn from(id: RepoStoreId) -> Self {
// hex::encode(to_vec(&id).unwrap())
// }
// }
pub struct RepoStoreInfo<'a> {
/// RepoStore ID
id: RepoStoreId,
id: RepoHash,
store: &'a dyn BrokerStore,
}
@ -48,7 +48,7 @@ impl<'a> RepoStoreInfo<'a> {
const SUFFIX_FOR_EXIST_CHECK: u8 = Self::KEY;
pub fn open(
id: &RepoStoreId,
id: &RepoHash,
store: &'a dyn BrokerStore,
) -> Result<RepoStoreInfo<'a>, StorageError> {
let opening = RepoStoreInfo {
@ -61,7 +61,7 @@ impl<'a> RepoStoreInfo<'a> {
Ok(opening)
}
pub fn create(
id: &RepoStoreId,
id: &RepoHash,
key: &SymKey,
store: &'a dyn BrokerStore,
) -> Result<RepoStoreInfo<'a>, StorageError> {
@ -84,7 +84,7 @@ impl<'a> RepoStoreInfo<'a> {
)
.is_ok()
}
pub fn id(&self) -> &RepoStoreId {
pub fn id(&self) -> &RepoHash {
&self.id
}
pub fn key(&self) -> Result<SymKey, StorageError> {

@ -11,6 +11,7 @@
use std::collections::HashMap;
use std::collections::HashSet;
use std::net::SocketAddr;
use std::pin::Pin;
use std::sync::Arc;
use std::sync::RwLock;
@ -22,7 +23,6 @@ use crate::broker_store::config::ConfigMode;
use crate::connection_local::BrokerConnectionLocal;
use crate::broker_store::overlay::Overlay;
use crate::broker_store::peer::Peer;
use crate::broker_store::repostoreinfo::RepoStoreId;
use crate::broker_store::repostoreinfo::RepoStoreInfo;
use async_std::task;
use debug_print::*;
@ -76,16 +76,35 @@ enum ProtocolType {
}
pub struct ProtocolHandler {
addr: Option<SocketAddr>,
broker: Arc<BrokerServer>,
protocol: ProtocolType,
auth_protocol: Option<AuthProtocolHandler>,
broker_protocol: Option<BrokerProtocolHandler>,
broker_protocol: Option<Arc<BrokerProtocolHandler>>,
ext_protocol: Option<ExtProtocolHandler>,
r: Option<async_channel::Receiver<Vec<u8>>>,
s: async_channel::Sender<Vec<u8>>,
}
impl ProtocolHandler {
pub fn register(&mut self, addr: SocketAddr) {
self.addr = Some(addr);
}
pub fn deregister(&mut self) {
match &self.protocol {
ProtocolType::Start => (),
ProtocolType::Auth => (),
ProtocolType::Broker => {
let _ = self.broker_protocol.as_ref().unwrap().deregister(self.addr.unwrap());
},