fluent-pad/services/README.md

114 lines
3.8 KiB
Markdown
Raw Permalink Normal View History

2021-01-11 18:14:49 +03:00
Use [proto-distributor](https://github.com/fluencelabs/proto-distributor) to upload and test services.
## Build
2021-01-11 18:14:49 +03:00
Step-by-step:
- install `fce` if needed. [Documentation](https://fluence.dev/docs/how-to-develop-a-module#setting-up)
2021-01-11 18:14:49 +03:00
- build scripts and copy fresh artifacts to `artifacts` directory by a script:
```shell
cd services
./build.sh
```
## Deploy
2021-01-11 18:14:49 +03:00
```shell
$ cd services
$ ./build.sh
$ cd artifacts
$ fldist new_service --modules history.wasm:history.json --name pad-history
client seed: Fs6nQaGEsM5EgnprUbUtoLYWhUC8o6QK1gseP9pfhzUm
client peerId: 12D3KooWH2hc6NAE2t6EE5SjmhTtce8VieUiKYNw4ynVCqV6jf6w
node peerId: 12D3KooWBUJifCTgaxAUrcM9JysqCcS4CS8tiYH5hExbdWCAoNwb
...
service id: 64ea579e-b863-4a42-b80c-e7b5ec1ab7fa
service created successfully
$ fldist new_service --modules user-list.wasm:user-list.json --name pad-user-list
client seed: HaBkus2i7bg6DmvxxSwizcxeo3xhvVJA9wLjyQji4mWc
client peerId: 12D3KooWR4WGTieeectXFtJxgVqB8vvk3kn531rTdk4pwt4mBn5x
node peerId: 12D3KooWBUJifCTgaxAUrcM9JysqCcS4CS8tiYH5hExbdWCAoNwb
...
service id: 91041afe-0c3c-451a-9003-6bb92a570aae
service created successfully
2021-01-11 18:14:49 +03:00
```
Service ids change on every service creation.
## Set authorization info
We need to set tetraplet so `history` service knows which instance of `user-list` to trust.
[script](../scripts/set_tetraplet.air)
```clojure
(xor
(seq
(call relay ("op" "identity") [])
(seq
2021-02-26 14:19:50 +03:00
(call relay (history "set_tetraplet") [host userlist function json_path] auth_result)
(call %init_peer_id% (returnService "run") [auth_result])
)
)
(call %init_peer_id% (returnService "run") [%last_error%])
)
2021-01-11 18:14:49 +03:00
```
Command:
2021-01-11 18:14:49 +03:00
```shell
2021-02-26 14:19:50 +03:00
fldist run_air -p scripts/set_tetraplet.air \
-s Fs6nQaGEsM5EgnprUbUtoLYWhUC8o6QK1gseP9pfhzUm \
-d '{
"host":"12D3KooWBUJifCTgaxAUrcM9JysqCcS4CS8tiYH5hExbdWCAoNwb",
"json_path":"$.[\"is_authenticated\"]",
"function": "is_authenticated",
"userlist":"91041afe-0c3c-451a-9003-6bb92a570aae",
"history":"64ea579e-b863-4a42-b80c-e7b5ec1ab7fa"
}'
2021-01-11 18:14:49 +03:00
```
## Test
Let's create a new keypair of some user.
2021-01-11 18:14:49 +03:00
```
public key a.k.a peer id = 12D3KooWSaSTAV7ftrN8f5UXkB9MYnFtdpAJpgUZbV7r2XpfVsF2
private key a.k.a seed = G2r9BEsuaBHaDyMxGb2Bxv61PpH6r8UjKT564CdeU4BD
```
Let's add that user to `userlist` service:
[join script](../scripts/join.air)
```clojure
(xor
(seq
(call relay ("op" "identity") [])
(seq
(call relay (userlist "join") [user] result)
(call %init_peer_id% (returnService "run") [result])
)
)
(call %init_peer_id% (returnService "run") [%last_error%])
)
2021-01-11 18:14:49 +03:00
```
Let's execute it, specifying user in `-d` as a JSON object
2021-01-11 18:14:49 +03:00
```shell
fldist run_air -p scripts/join.air -d '{"user":{"peer_id":"12D3KooWSaSTAV7ftrN8f5UXkB9MYnFtdpAJpgUZbV7r2XpfVsF2","relay_id":"","name":"some_name"}, "userlist":"91041afe-0c3c-451a-9003-6bb92a570aae"}' -s 7sHe8vxCo4BkdPNPdb8f2T8CJMgTmSvBTmeqtH9QWrar
2021-01-11 18:14:49 +03:00
```
That user can now be authorized and send messages! Let's try it
[add message script](../scripts/add_message.air)
```clojure
(xor
(seq
(call relay ("op" "identity") [])
(seq
(call relay (userlist "is_authenticated") [] status)
(seq
(call relay (history "add") [msg status.$.["is_authenticated"]!] auth_result)
(call %init_peer_id% (returnService "run") [auth_result])
)
)
)
(call %init_peer_id% (returnService "run") [%last_error%])
)
2021-01-11 18:14:49 +03:00
```
Note that we provide private key via `-s`:
2021-01-11 18:14:49 +03:00
```shell
fldist -s G2r9BEsuaBHaDyMxGb2Bxv61PpH6r8UjKT564CdeU4BD run_air -p scripts/add_message.air -d '{"msg":"Hi there!", "userlist":"91041afe-0c3c-451a-9003-6bb92a570aae", "history":"64ea579e-b863-4a42-b80c-e7b5ec1ab7fa"}'
2021-01-11 18:14:49 +03:00
```
You can learn more about `Aquamarine` [in the doc](https://fluence.dev/).