[wip] updating sidekick/jex/awcp for message sign demo

This commit is contained in:
Foo Bar
2022-11-25 18:01:28 -07:00
parent 9305208ba6
commit c3194b1ec2
6 changed files with 425 additions and 690 deletions
+1 -71
View File
@@ -1,73 +1,3 @@
# Sidekick
Sidekick is a simple JavaScript library for talking to an Aeternity
browser wallet extension such as Jaeck Russell or Superhero from the document
context of a webpage.
# Build Prereqs
Assuming Ubuntu 18.04. Adapt these instructions for your own system.
You need
- `npm` to build TypeScript (and TypeDoc if you want to build the
documentation)
- `tsc` to compile
- [jex](../utils/jex/) to facilitate the build
Steps:
sudo snap refresh
sudo snap install node --channel 18/stable
npm install -g typescript
## Protip: avoid using `sudo npm install -g`
If you want to avoid using sudo
```
mkdir ~/.npm-packages
npm config set prefix "${HOME}/.npm-packages"
```
Edit `~/.bashrc` or `~/.zshrc` with
```
NPM_PACKAGES="${HOME}/.npm-packages"
export PATH=$NPM_PACKAGES/bin:$PATH
```
# Build Steps
## 1. Build dependencies
The examples require `parasite` as a dependency, but sidekick itself does not.
```
~/src/vanillae $ cd libs/awcp
~/src/vanillae/libs/awcp $ jex dwim+
~/src/vanillae/libs/awcp $ cd ../parasite
~/src/vanillae/libs/parasite $ jex dwim+
```
## 2. Build sidekick
```
~/src/vanillae/libs/parasite $ cd ../../sidekick
~/src/vanillae/sidekick $ jex dwim+
```
## 3. Build examples
Note the `-`, not the `+`. The difference is that `-` just builds the project,
but does not package it.
```
~/src/vanillae/sidekick $ cd examples
~/src/vanillae/sidekick/examples $ jex dwim-
~/src/vanillae/sidekick/examples $ python3 -m http.server 8000
```
Navigate to `http://localhost:8000/` in your browser to see the examples
The examples are the best documentation, for now.
Click "sidekick" in the sidebar or hamburger menu
+1 -1
View File
@@ -2,4 +2,4 @@
{realm, local}.
{name, sidekick}.
{version, "0.2.0"}.
{deps, ["local-awcp-0.1.0"]}.
{deps, ["local-awcp-0.2.0"]}.
+16 -109
View File
@@ -1,124 +1,31 @@
// tomorrow:
// message signing
// examples
// documentation
// project organization
/**
* # How to use this library
* # tl;dr
*
* This is a library for communicating with a browser wallet extension such as
* Superhero
* 1. {@link detect} the wallet
* 2. {@link connect} to the wallet
* 3. Get the wallet's {@link address}
*
* ## Step 0: Include `sidekick`
* From there you can do one of two things
*
* ```
* import * as sk from './path/to/sidekick.js';
* ```
* 1. Have the wallet sign transactions ({@link tx_sign_noprop})
* 2. Have the wallet sign arbitrary messages ({@link msg_sign})
*
* ## Step 1: Make a `Logger`
*
* All of the entrypoints in sidekick require passing in a `Logger`. The idea
* is that you can pass in custom logging hooks to log potential errors.
*
* There are two built-in loggers exported by this module:
*
* 1. `let my_logger = sk.wsl();`: does nothing
* 2. `let my_logger = sk.cl();`: console logger
* 3. `let my_logger = new sk.HttpLogger('https://foo.bar/baz')`: sends JSON to
* the given endpoint in a POST request, in the following form
*
* ```
* {level : 'debug' | 'info' | 'warning' | 'error',
* message : string,
* data : object}
* ```
* 4. `let my_logger = new sk.SeqLogger([my_logger1, my_logger2]);`: a helper
* for composing several loggers sequentially.
* 5. You can define anything that satisfies the `Logger` interface and pass
* that in instead.
*
* ```
* interface Logger {
* debug : (message : string, data : object) => Promise<void>;
* info : (message : string, data : object) => Promise<void>;
* warning : (message : string, data : object) => Promise<void>;
* error : (message : string, data : object) => Promise<void>;
* }
* ```
*
* ## Step 2: Detect the wallet
*
* ```
* // timeout error message logger
* let maybe_detected = await sk.detect(sk.TIMEOUT_DEF_DETECT, 'detect: timeout', my_logger);
* ```
*
* Function:
*
* ```
* async function
* detect
* (timeout_ms : number,
* timeout_msg : string,
* logger : Logger)
* : Promise<Safe<awcp.Params_W2A_connection_announcePresence, SkTimeoutError>>
* ```
*
* This returns some garbage that doesn't matter in a `Safe` type.
* The `Safe` type does matter
*
* ```
* type Safe<ok_t, err_t>
* = Ok<ok_t>
* | Error<err_t>;
*
* type Ok<ok_t>
* = {ok : true,
* result : ok_t};
*
* type Error<err_t>
* = {ok : false,
* error : err_t};
* ```
*
* The motivation here is that when talking to the wallet, there are many
* possible sources of errors. For instance, if you ask the wallet to sign a
* transaction, the transaction might be malformed, maybe the user declines,
* maybe it times out, whatever. All you care about is "did it work?" and you
* don't want to deal with try/catch bullshit.
*
* The most straightforward way to extract the return value is with branching:
*
* ```
* if (maybe_detected.ok) {
* // ok is true in this case, so the field `result` exists
* let awcp_crap = maybe_detected.result;
* }
* else {
* // ok is false in this case, so the field `error` exists
* let the_error = maybe_detected.error;
* }
* ```
*
* ## Step 3: Connect to the wallet
*
*
*
* ## Step 4: Get the user address
*
* ## Step 5: Sign a transaction
* Forming the transactions and propagating them into the network is your
* problem.
*
* You need a {@link Logger} for most calls. Probably you want {@link cl}. You
* can write your own if you want but why would you complicate your life like
* that.
*
* @module
*/
// TODONE: add standardized logging interface
// TODONE: logging hooks
// TODO: invoice
// TODO: get connect done
// TODO: make the message queue for responses a map, fill the message queue properly
// TODO: get it working with superhero
// TODONE: invoice
// TODONE: get connect done
// TODONE: make the message queue for responses a map, fill the message queue properly
// TODONE: get it working with superhero
// TODO: jrx
// like: console, http, etc