14 Commits

Author SHA1 Message Date
pharpend 079009a13e [wip] now display user's camera
next tricky thing is connecting users
2026-02-24 10:40:43 -08:00
pharpend 484eea92e4 have call box 2026-02-12 20:54:58 -08:00
pharpend 08c0119ae0 have a roster and a whoami 2026-02-12 20:32:34 -08:00
pharpend db6d244cb6 finished code surgery
patient is comatose and is missing both testicles and eyeballs but is legally
alive
2026-02-12 19:31:08 -08:00
Peter Harpending 4a0e221790 Merge remote-tracking branch 'refs/remotes/origin/f-webrtc' into f-webrtc 2026-02-12 18:01:37 -08:00
Peter Harpending d348fe7cd1 Merge remote-tracking branch 'refs/remotes/origin/f-webrtc' into f-webrtc 2026-02-12 18:01:29 -08:00
pharpend ad5df4f550 merging 2026-02-12 17:57:29 -08:00
Peter Harpending b63130a061 stash 2026-02-12 10:14:16 -08:00
pharpend b871968f8c roster kind of sort of works 2026-02-09 21:11:53 -08:00
pharpend 6028bb5850 roster is broadcast 2026-02-09 20:48:43 -08:00
pharpend 25a775ee96 webrtc frontend can request username from backend 2026-02-09 16:24:33 -08:00
Peter Harpending b6436c84ee wip 2026-02-09 14:48:47 -08:00
pharpend cc95fc5829 webrtc 2026-02-09 12:54:45 -08:00
pharpend ce7b8a1ccd add webrtc grok demo 2026-02-04 21:59:37 -08:00
30 changed files with 791 additions and 1545 deletions
-6
View File
@@ -1,9 +1,3 @@
2026-02-25(PRH):
grids: instead of spying the chain for a tx, let's use dead drop
- so for "generate", need to form tx for the user to sign
- make dead drop -> have process that knows each tx and the dead drop location
OPEN LOOPS - 2025-11-12
- websockets
- separate websocket handling from websocket parsing/sending
+3 -37
View File
@@ -1,43 +1,9 @@
fewd = front end web dev
=====================================================================
# fewd = front end web dev
this is me (PRH) trying to learn some front end web dev because pixels are
important despite my wishes.
Building/Running
---------------------------------------------------------------------
# notes
### Prereqs
## goal queue
1. [Install Erlang and ZX](https://git.qpq.swiss/QPQ-AG/research-megadoc/wiki/Installing-Erlang-and-zx)
2. **DEV ONLY**: `apt install node-typescript` (Devuan Excalibur)
This is needed if you want to **edit** the `.ts` files found in
`/priv/static/js/ts/*.ts`. The built JS files are under version control and can
be found in `/priv/static/js/dist/`
### Building/Running HTTP Server
If you are only changing the Erlang or simply just want to run the program
without developing it, then just run
```
zxh runlocal
```
### Building TS->JS
**This is only necessary if you edited the `.ts` files and want to transpile
them over to JS.**
This requires you installed `tsc` as above.
```
make tsc
```
If you're doing development you may want
```
make watch
```
View File
-9
View File
@@ -1,9 +0,0 @@
/**
* Title: Title
* Description: Description
* Author: Peter Harpending <peterharpending@qpq.swiss>
* Date: YYYY-MM-DD
* Last-Updated: YYYY-MM-DD
*
* @module
*/
-60
View File
@@ -1,60 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Basic GRIDS Demo</title>
<link rel="stylesheet" href="/css/default.css">
</head>
<body>
<div id="titlebar">
<div class="content">
<a href="/" class="tb-home">Home</a>
</div>
</div>
<div class="content">
<h1 class="content-title">FEWD: GRIDS DEMO</h1>
<h2>Making a Spend</h2>
<label for="grids-n">Network ID:</label>
<input type = "text"
id = "grids-n"
value = "groot.testnet"
disabled>
<br>
<label for="grids-r">Recipient:</label>
<input type = "text"
id = "grids-r"
value = "ak_n6aVQ6PkBdVdv7kRRcfnzDVmBsH6hqEwVWSB6UAEb3kkjrPMe"
disabled>
<br>
<label for="grids-a">Amount (P):</label>
<input type = "number"
id = "grids-a"
value = "69"
disabled>
<br>
<label for="grids-p">Payload:</label>
<input type = "text"
id = "grids-p"
disabled>
<br>
<input type = "button"
id = "grids-submit"
value = "Generate">
<br>
<textarea disabled id="grids-url" hidden></textarea>
<br>
<img id="grids-png" hidden>
</div>
<script src="/js/dist/grids-basic.js"></script>
</body>
</html>
+1 -1
View File
@@ -17,7 +17,7 @@
<ul>
<li><a href="/echo.html">Echo</a></li>
<li><a href="/grids-basic.html">GRIDS: Basic Demo</a></li>
<li><a href="/webrtc.html">WebRTC</a></li>
<li><a href="/wfc.html">WFC</a></li>
</ul>
</div>
-29
View File
@@ -1,29 +0,0 @@
/**
* Title: GRIDS Basic Page Script
* Description: Page Script for /grids-basic.html
* Author: Peter Harpending <peterharpending@qpq.swiss>
* Date: 2025-12-29
* Last-Updated: 2025-12-29
*
* @module
*/
/**
* Runs on page load
*/
declare function main(): Promise<void>;
declare function on_submit(n_input: HTMLInputElement, r_input: HTMLInputElement, a_input: HTMLInputElement, p_input: HTMLInputElement, grids_url_elt: HTMLTextAreaElement, grids_png_elt: HTMLImageElement): Promise<void>;
type Safe<t> = {
ok: true;
result: t;
} | {
ok: false;
error: string;
};
type GridsResult = {
url: string;
png_base64: string;
};
/**
* gets the grids url
*/
declare function grids_request(net_id: string, recipient: string, amount: number, payload: string): Promise<Safe<GridsResult>>;
-87
View File
@@ -1,87 +0,0 @@
"use strict";
/**
* Title: GRIDS Basic Page Script
* Description: Page Script for /grids-basic.html
* Author: Peter Harpending <peterharpending@qpq.swiss>
* Date: 2025-12-29
* Last-Updated: 2025-12-29
*
* @module
*/
main();
/**
* Runs on page load
*/
async function main() {
let n_input = document.getElementById('grids-n');
let r_input = document.getElementById('grids-r');
let a_input = document.getElementById('grids-a');
let p_input = document.getElementById('grids-p');
let submit_btn = document.getElementById('grids-submit');
let grids_url_elt = document.getElementById('grids-url');
let grids_png_elt = document.getElementById('grids-png');
let rand_data = new Uint8Array(32);
window.crypto.getRandomValues(rand_data);
let hex = rand_data.toHex().toUpperCase();
p_input.value = 'text payload ' + hex;
// Page initialization
submit_btn.addEventListener('click', async function (e) {
await on_submit(n_input, r_input, a_input, p_input, grids_url_elt, grids_png_elt);
});
// enable buttons
submit_btn.disabled = false;
}
async function on_submit(n_input, r_input, a_input, p_input, grids_url_elt, grids_png_elt) {
// pull out values
let network_id = n_input.value;
let recipient = r_input.value;
let amount = parseInt(a_input.value);
let payload = p_input.value;
let result = await grids_request(network_id, recipient, amount, payload);
// show url field and png
if (result.ok) {
let url = result.result.url;
let png_base64 = result.result.png_base64;
let src_prefix = 'data:image/png;base64,';
let src = src_prefix + png_base64;
grids_url_elt.innerText = url;
grids_png_elt.src = src;
grids_url_elt.hidden = false;
grids_png_elt.hidden = false;
}
else {
alert('ERROR: ' + result.error);
}
}
/**
* gets the grids url
*/
async function grids_request(net_id, recipient, amount, payload) {
// format for network transmission
let obj = { 'network_id': net_id,
'recipient': recipient,
'amount': amount,
'payload': payload };
let obj_text = JSON.stringify(obj, undefined, 4);
let url = '/grids-mkdd';
let req_options = { method: 'POST',
headers: { 'content-type': 'application/json' },
body: obj_text };
let result = { ok: false,
error: 'IT DO BE LIKE THAT MISTA STANCIL' };
try {
let response = await fetch(url, req_options);
if (response.ok)
result = await response.json();
else {
console.log('bad http response:', response);
result = { ok: false, error: 'BAD HTTP RESPONSE' };
}
}
catch (x) {
console.log('network error:', x);
result = { ok: false, error: 'NETWORK ERROR' };
}
return result;
}
//# sourceMappingURL=grids-basic.js.map
-1
View File
@@ -1 +0,0 @@
{"version":3,"file":"grids-basic.js","sourceRoot":"","sources":["../ts/grids-basic.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG;AAEH,IAAI,EAAE,CAAC;AAGP;;GAEG;AACH,KAAK,UACL,IAAI;IAGA,IAAI,OAAO,GAAM,QAAQ,CAAC,cAAc,CAAC,SAAS,CAA0B,CAAC;IAC7E,IAAI,OAAO,GAAM,QAAQ,CAAC,cAAc,CAAC,SAAS,CAA0B,CAAC;IAC7E,IAAI,OAAO,GAAM,QAAQ,CAAC,cAAc,CAAC,SAAS,CAA0B,CAAC;IAC7E,IAAI,OAAO,GAAM,QAAQ,CAAC,cAAc,CAAC,SAAS,CAA0B,CAAC;IAC7E,IAAI,UAAU,GAAG,QAAQ,CAAC,cAAc,CAAC,cAAc,CAAqB,CAAC;IAE7E,IAAI,aAAa,GAAG,QAAQ,CAAC,cAAc,CAAC,WAAW,CAAwB,CAAC;IAChF,IAAI,aAAa,GAAG,QAAQ,CAAC,cAAc,CAAC,WAAW,CAAqB,CAAC;IAE7E,IAAI,SAAS,GAAgB,IAAI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gBAAgB,EAAE,CAAC,CAAC,CAAC;QACjC,MAAM,GAAG,EAAC,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,eAAe,EAAC,CAAC;IACjD,CAAC;IAED,OAAO,MAAM,CAAC;AAClB,CAAC"}
+1 -1
View File
@@ -1 +1 @@
{"version":3,"file":"libfewd.js","sourceRoot":"","sources":["../ts/libfewd.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EACH,WAAW,EACX,qBAAqB,EACxB,CAAC;AAGF,SACA,WAAW,CACN,gBAAmC,EACnC,cAAsC,EACtC,UAAyB;IAG1B,+DAA+D;IAC/D,IAAI,gBAAgB,CAAC,OAAO,EAAE,CAAC;QAC3B,IAAI,aAAa,GAAW,cAAc,CAAC,YAAY,CAAC;QACxD,sCAAsC;QACtC,IAAI,aAAa,GAAG,UAAU;YAC1B,cAAc,CAAC,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,aAAa,CAAC,GAAG,IAAI,CAAC;;YAE3D,cAAc,CAAC,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,UAAU,CAAC,GAAG,IAAI,CAAC;IAChE,CAAC;AACL,CAAC;AAGD,SACA,qBAAqB,CAChB,gBAAmC,EACnC,cAAsC;IAGvC,IAAI,gBAAgB,CAAC,OAAO,EAAE,CAAC;QAC3B,mBAAmB;QACnB,cAAc,CAAC,SAAS,GAAG,cAAc,CAAC,YAAY,CAAC;IAC3D,CAAC;AACL,CAAC"}
{"version":3,"file":"libfewd.js","sourceRoot":"","sources":["../ts/libfewd.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EACH,WAAW,EACX,qBAAqB,EACxB,CAAC;AAGF,SACA,WAAW,CACN,gBAAmC,EACnC,cAAsC,EACtC,UAAyB;IAG1B,+DAA+D;IAC/D,IAAI,gBAAgB,CAAC,OAAO,EAAE;QAC1B,IAAI,aAAa,GAAW,cAAc,CAAC,YAAY,CAAC;QACxD,sCAAsC;QACtC,IAAI,aAAa,GAAG,UAAU;YAC1B,cAAc,CAAC,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,aAAa,CAAC,GAAG,IAAI,CAAC;;YAE3D,cAAc,CAAC,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,UAAU,CAAC,GAAG,IAAI,CAAC;KAC/D;AACL,CAAC;AAGD,SACA,qBAAqB,CAChB,gBAAmC,EACnC,cAAsC;IAGvC,IAAI,gBAAgB,CAAC,OAAO,EAAE;QAC1B,mBAAmB;QACnB,cAAc,CAAC,SAAS,GAAG,cAAc,CAAC,YAAY,CAAC;KAC1D;AACL,CAAC"}
+22
View File
@@ -0,0 +1,22 @@
/**
* webrtc page script
*
* Author: Peter Harpending <peterharpending@qpq.swiss>
* Date: 2026-02-04
* Copyright: Copyright (c) 2026 QPQ AG
*
* Reference: https://git.qpq.swiss/QPQ-AG/research-megadoc/src/commit/c7c4592d4b21ad120145ef63334471a1a7ec1e60/paste/2026-02/grok-webrtc.html
*
* @module
*/
declare function main(): Promise<void>;
/**
* Try to get the user's camera
*
* tries to get screenshare if user camera not available for whatever reason
*
* chimps out if
* - not on https
* - user says no
*/
declare function getUserCamera(): Promise<MediaStream>;
+41
View File
@@ -0,0 +1,41 @@
"use strict";
/**
* webrtc page script
*
* Author: Peter Harpending <peterharpending@qpq.swiss>
* Date: 2026-02-04
* Copyright: Copyright (c) 2026 QPQ AG
*
* Reference: https://git.qpq.swiss/QPQ-AG/research-megadoc/src/commit/c7c4592d4b21ad120145ef63334471a1a7ec1e60/paste/2026-02/grok-webrtc.html
*
* @module
*/
main();
async function main() {
console.log('peepy');
let video = await getUserCamera();
console.log('poopy');
console.log('poopu');
let moi = document.getElementById('me');
moi.srcObject = video;
console.log('poopa');
}
/**
* Try to get the user's camera
*
* tries to get screenshare if user camera not available for whatever reason
*
* chimps out if
* - not on https
* - user says no
*/
async function getUserCamera() {
if (!(navigator.mediaDevices)) {
console.error('navigator.mediaDevices is null; user not on https or something');
}
let availableDevices = await navigator.mediaDevices.enumerateDevices();
console.log(availableDevices);
return await navigator.mediaDevices.getUserMedia({ video: true, audio: false });
// return await navigator.mediaDevices.getDisplayMedia();
}
//# sourceMappingURL=webrtc.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"webrtc.js","sourceRoot":"","sources":["../ts/webrtc.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;GAUG;AAEH,IAAI,EAAE,CAAC;AAEP,KAAK,UACL,IAAI;IAIA,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IACrB,IAAI,KAAK,GAAsB,MAAM,aAAa,EAAE,CAAC;IACrD,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IACrB,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IACrB,IAAI,GAAG,GAAG,QAAQ,CAAC,cAAc,CAAC,IAAI,CAAqB,CAAC;IAC5D,GAAG,CAAC,SAAS,GAAG,KAAK,CAAC;IACtB,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;AACzB,CAAC;AAGD;;;;;;;;GAQG;AACH,KAAK,UACL,aAAa;IAIT,IAAI,CAAC,CAAC,SAAS,CAAC,YAAY,CAAC,EAAE;QAC3B,OAAO,CAAC,KAAK,CAAC,gEAAgE,CAAC,CAAC;KACnF;IAED,IAAI,gBAAgB,GAChB,MAAM,SAAS,CAAC,YAAY,CAAC,gBAAgB,EAAE,CAAC;IACpD,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC;IAE9B,OAAO,MAAM,SAAS,CAAC,YAAY,CAAC,YAAY,CAAC,EAAC,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAC,CAAC,CAAC;IAC9E,yDAAyD;AAC7D,CAAC"}
+1 -1
View File
@@ -1 +1 @@
{"version":3,"file":"wfc.js","sourceRoot":"","sources":["../ts/wfc.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,OAAO,MAAM,cAAc,CAAA;AAEvC,oEA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B,EAAC;QAC7C,IAAI,EAAK,YAAY,EAAC,CAAC;IAE3C,mEAAmE;IACnE,4CAA4C;IAC5C,IAAI,MAAM,GAAW,EAAC,EAAE,EAAM,KAAK;QACb,KAAK,EAAG,kCAAkC,EAAC,CAAC;IAElE,IAAI;QACA,IAAI,QAAQ,GAAc,MAAM,KAAK,CAAC,QAAQ,EAAE,WAAW,CAAC,CAAC;QAC7D,IAAI,QAAQ,CAAC,EAAE;YACX,MAAM,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAY,CAAC;aACxC;YACD,OAAO,CAAC,GAAG,CAAC,oBAAoB,EAAE,QAAQ,CAAC,CAAC;YAC5C,MAAM,GAAG,EAAC,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,mBAAmB,EAAC,CAAC;SACpD;KACJ;IACD,OAAO,CAAM,EAAE;QACX,OAAO,CAAC,GAAG,CAAC,gBAAgB,EAAE,CAAC,CAAC,CAAC;QACjC,MAAM,GAAG,EAAC,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,eAAe,EAAC,CAAC;KAChD;IAED,OAAO,MAAM,CAAC;AAClB,CAAC"}
-136
View File
@@ -1,136 +0,0 @@
/**
* Title: GRIDS Basic Page Script
* Description: Page Script for /grids-basic.html
* Author: Peter Harpending <peterharpending@qpq.swiss>
* Date: 2025-12-29
* Last-Updated: 2025-12-29
*
* @module
*/
main();
/**
* Runs on page load
*/
async function
main
()
{
let n_input = document.getElementById('grids-n') as HTMLInputElement;
let r_input = document.getElementById('grids-r') as HTMLInputElement;
let a_input = document.getElementById('grids-a') as HTMLInputElement;
let p_input = document.getElementById('grids-p') as HTMLInputElement;
let submit_btn = document.getElementById('grids-submit') as HTMLInputElement;
let grids_url_elt = document.getElementById('grids-url') as HTMLTextAreaElement;
let grids_png_elt = document.getElementById('grids-png') as HTMLImageElement;
let rand_data : Uint8Array = new Uint8Array(32);
window.crypto.getRandomValues(rand_data);
let hex : string = rand_data.toHex().toUpperCase();
p_input.value = 'text payload ' + hex;
// Page initialization
submit_btn.addEventListener(
'click',
async function(e) {
await on_submit(n_input, r_input, a_input, p_input, grids_url_elt, grids_png_elt)
}
);
// enable buttons
submit_btn.disabled = false;
}
async function
on_submit
(n_input : HTMLInputElement,
r_input : HTMLInputElement,
a_input : HTMLInputElement,
p_input : HTMLInputElement,
grids_url_elt : HTMLTextAreaElement,
grids_png_elt : HTMLImageElement)
: Promise<void>
{
// pull out values
let network_id : string = n_input.value;
let recipient : string = r_input.value;
let amount : number = parseInt(a_input.value);
let payload : string = p_input.value;
let result: Safe<GridsResult> = await grids_request(network_id, recipient, amount, payload);
// show url field and png
if (result.ok) {
let url : string = result.result.url;
let png_base64 : string = result.result.png_base64;
let src_prefix : string = 'data:image/png;base64,'
let src = src_prefix + png_base64;
grids_url_elt.innerText = url;
grids_png_elt.src = src;
grids_url_elt.hidden = false;
grids_png_elt.hidden = false;
}
else {
alert('ERROR: ' + result.error);
}
}
type Safe<t> = {ok: true, result: t}
| {ok: false, error: string};
type GridsResult = {url : string,
png_base64: string};
/**
* gets the grids url
*/
async function
grids_request
(net_id : string,
recipient : string,
amount : number,
payload : string)
: Promise<Safe<GridsResult>>
{
// format for network transmission
let obj : object = {'network_id' : net_id,
'recipient' : recipient,
'amount' : amount,
'payload' : payload};
let obj_text : string = JSON.stringify(obj, undefined, 4);
let url = '/grids-mkdd';
let req_options = {method: 'POST',
headers: {'content-type': 'application/json'},
body: obj_text};
let result: Safe<GridsResult> =
{ok : false,
error : 'IT DO BE LIKE THAT MISTA STANCIL'};
try {
let response : Response = await fetch(url, req_options);
if (response.ok)
result = await response.json() as Safe<GridsResult>;
else {
console.log('bad http response:', response);
result = {ok: false, error: 'BAD HTTP RESPONSE'};
}
}
catch (x: any) {
console.log('network error:', x);
result = {ok: false, error: 'NETWORK ERROR'};
}
return result;
}
+54
View File
@@ -0,0 +1,54 @@
/**
* webrtc page script
*
* Author: Peter Harpending <peterharpending@qpq.swiss>
* Date: 2026-02-04
* Copyright: Copyright (c) 2026 QPQ AG
*
* Reference: https://git.qpq.swiss/QPQ-AG/research-megadoc/src/commit/c7c4592d4b21ad120145ef63334471a1a7ec1e60/paste/2026-02/grok-webrtc.html
*
* @module
*/
main();
async function
main
()
: Promise<void>
{
console.log('peepy');
let video : MediaStream = await getUserCamera();
console.log('poopy');
console.log('poopu');
let moi = document.getElementById('me') as HTMLVideoElement;
moi.srcObject = video;
console.log('poopa');
}
/**
* Try to get the user's camera
*
* tries to get screenshare if user camera not available for whatever reason
*
* chimps out if
* - not on https
* - user says no
*/
async function
getUserCamera
()
: Promise<MediaStream>
{
if (!(navigator.mediaDevices)) {
console.error('navigator.mediaDevices is null; user not on https or something');
}
let availableDevices: Array<MediaDeviceInfo> =
await navigator.mediaDevices.enumerateDevices();
console.log(availableDevices);
return await navigator.mediaDevices.getUserMedia({video: true, audio: false});
// return await navigator.mediaDevices.getDisplayMedia();
}
@@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="utf-8">
<title>FIXME</title>
<title>FEWD: voice chat demo</title>
<link rel="stylesheet" href="/css/default.css">
</head>
<body>
@@ -13,8 +13,15 @@
</div>
<div class="content">
<h1 class="content-title">FEWD: FIXME</h1>
<h1 class="content-title">FEWD: webrtc demo</h1>
<div class="content-body">
<textarea disabled id="wfc-output"></textarea>
<video id="me" autoplay muted></video>
</div>
</div>
<script src="/js/dist/webrtc.js"></script>
</body>
</html>
+166
View File
@@ -0,0 +1,166 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Super Simple WebRTC Manual Signaling</title>
<style>
body { font-family: sans-serif; margin: 20px; }
textarea { width: 100%; height: 90px; font-family: monospace; }
button { margin: 6px 0; padding: 8px 16px; }
video { max-width: 320px; border: 1px solid #444; margin: 8px; background: #000; }
</style>
</head>
<body>
<h2>WebRTC 1:1 test (copy-paste signaling)</h2>
<div>
<button id="btnCreateOffer">1. Create Offer (Peer A)</button>
<button id="btnSetRemoteOffer">2. Paste offer → Set remote description</button><br>
<textarea id="offerSdp" placeholder="Offer SDP will appear here (Peer A) or paste offer here (Peer B)"></textarea>
</div>
<div style="margin-top: 24px;">
<button id="btnCreateAnswer">3. Create Answer (Peer B)</button>
<button id="btnSetRemoteAnswer">4. Paste answer → Set remote description</button><br>
<textarea id="answerSdp" placeholder="Answer SDP will appear here (Peer B) or paste answer here (Peer A)"></textarea>
</div>
<div style="margin-top: 20px;">
<button id="btnAddIce">Add ICE candidate manually (if needed)</button><br>
<textarea id="iceInput" placeholder="Paste remote ICE candidate here"></textarea>
<div id="iceLog"></div>
</div>
<div style="margin-top: 20px;">
<video id="localVideo" autoplay playsinline muted></video>
<video id="remoteVideo" autoplay playsinline></video>
</div>
<pre id="status">Status: ready</pre>
<script>
// ────────────────────────────────────────────────
const $ = s => document.querySelector(s);
const status = $('pre#status');
const offerArea = $('#offerSdp');
const answerArea = $('#answerSdp');
const iceInput = $('#iceInput');
const iceLog = $('#iceLog');
let pc = null;
let localStream = null;
// ────────────────────────────────────────────────
async function initPeerConnection() {
if (pc) pc.close();
pc = new RTCPeerConnection({
iceServers: [
{ urls: "stun:stun.l.google.com:19302" },
{ urls: "stun:stun.stunprotocol.org" }
]
});
pc.onicecandidate = e => {
if (e.candidate) {
console.log("New ICE candidate:", e.candidate);
iceLog.innerHTML += "<div style='color:#c33'>→ " + e.candidate.candidate + "</div>";
}
};
pc.oniceconnectionstatechange = () => {
status.textContent = "ICE state: " + pc.iceConnectionState;
};
pc.ontrack = e => {
console.log("ontrack", e);
const remoteVideo = $('#remoteVideo');
remoteVideo.srcObject = e.streams[0];
status.textContent = "ICE state: " + pc.iceConnectionState + " (receiving media!)";
};
// Add local stream if we have camera/mic
if (localStream) {
localStream.getTracks().forEach(track => {
pc.addTrack(track, localStream);
});
}
}
// ────────────────────────────────────────────────
async function startLocalCamera() {
try {
localStream = await navigator.mediaDevices.getUserMedia({
video: true,
audio: false // change to true if you want audio too
});
$('#localVideo').srcObject = localStream;
} catch (err) {
console.error("Camera access failed", err);
status.textContent = "Camera access failed: " + err.message;
}
}
// ────────────────────────────────────────────────
$('#btnCreateOffer').onclick = async () => {
await initPeerConnection();
await startLocalCamera();
const offer = await pc.createOffer();
await pc.setLocalDescription(offer);
offerArea.value = JSON.stringify(pc.localDescription, null, 2);
status.textContent = "Offer created copy and send to peer B";
};
// ────────────────────────────────────────────────
$('#btnCreateAnswer').onclick = async () => {
await initPeerConnection();
await startLocalCamera();
const offer = JSON.parse(offerArea.value);
await pc.setRemoteDescription(offer);
const answer = await pc.createAnswer();
await pc.setLocalDescription(answer);
answerArea.value = JSON.stringify(pc.localDescription, null, 2);
status.textContent = "Answer created copy and send to peer A";
};
// ────────────────────────────────────────────────
$('#btnSetRemoteOffer').onclick = async () => {
if (!pc) await initPeerConnection();
const remote = JSON.parse(offerArea.value);
await pc.setRemoteDescription(remote);
status.textContent = "Remote offer set";
};
// ────────────────────────────────────────────────
$('#btnSetRemoteAnswer').onclick = async () => {
const remote = JSON.parse(answerArea.value);
await pc.setRemoteDescription(remote);
status.textContent = "Remote answer set waiting for ICE/media";
};
// ────────────────────────────────────────────────
$('#btnAddIce').onclick = async () => {
if (!iceInput.value.trim()) return;
try {
const candidate = new RTCIceCandidate(JSON.parse(iceInput.value));
await pc.addIceCandidate(candidate);
iceLog.innerHTML += "<div style='color:#3a3'>← added ICE candidate</div>";
iceInput.value = "";
} catch (err) {
console.error("Bad ICE candidate", err);
}
};
// Optional: auto-start camera when page loads
// startLocalCamera();
</script>
</body>
</html>
+127
View File
@@ -0,0 +1,127 @@
/**
* webrtc page script
*
* Author: Peter Harpending <peterharpending@qpq.swiss>
* Date: 2026-02-04
* Copyright: Copyright (c) 2026 QPQ AG
*
* Reference: https://git.qpq.swiss/QPQ-AG/research-megadoc/src/commit/c7c4592d4b21ad120145ef63334471a1a7ec1e60/paste/2026-02/grok-webrtc.html
*
* @module
*/
type state =
{whoami: string,
roster: Array<string>};
let st: state =
{whoami: "",
roster: []};
main();
async function
main
()
: Promise<void>
{
// start websocket immediately
let ws: WebSocket;
let init = document.getElementById('init') as HTMLDivElement;
let init_join = document.getElementById('init-join') as HTMLButtonElement;
// handle button click
init_join.addEventListener('click',
function() {
init.hidden = true;
document.getElementById('run')!.hidden = false;
ws = new WebSocket('/ws/webrtc');
ws.onopen = function(e) { console.log('ws open'); };
ws.onclose = function(e) { console.warn('ws closed'); };
ws.onerror = function(e) { console.error('ws error:', e); };
ws.onmessage =
function(e) {
// console.log('ws message:', e.data);
let message = JSON.parse(e.data) as ws_msg;
handle_ws_msg(message);
};
}
);
}
type ws_msg = ["whoami", string]
| ["roster", Array<string>];
function
handle_ws_msg
(message: ws_msg)
: void
{
console.log('message from server:', message);
switch(message[0]) {
case "whoami":
st.whoami = message[1];
break;
case "roster":
st.roster = (message[1] as Array<string>);
break;
default:
console.warn("unknown message", message);
}
render_state();
}
function
ws_send_json
(ws : WebSocket,
x : any)
: void
{
let s: string = JSON.stringify(x, undefined, 4);
console.log('sending:\n', s);
ws.send(s);
}
function
render_state
()
: void
{
console.log('st', st);
// whoami
document.getElementById('whoami')!.innerText = st.whoami;
//
let roster_ul = document.getElementById('roster-ul') as HTMLUListElement;
let newChildren : Array<HTMLElement> = [];
for (let nick of st.roster) {
if (!(nick === st.whoami)) {
let li = nickkk(nick);
newChildren.push(li);
}
}
roster_ul.replaceChildren(...newChildren);
}
function
nickkk
(nick: string)
: HTMLElement
{
let li = document.createElement('li');
li.innerText += nick;
li.innerText += ' ';
let call_a = document.createElement('button');
call_a.innerText = 'call';
li.appendChild(call_a);
return li;
}
-150
View File
@@ -1,150 +0,0 @@
% @doc grids cache
-module(fd_gridsd).
-vsn("0.2.0").
-behavior(gen_server).
-export_type([
]).
-export([
%% caller context
mkdd/4,
%% api
start_link/0,
%% process context
init/1, handle_call/3, handle_cast/2, handle_info/2,
code_change/3, terminate/2
]).
-include("$zx_include/zx_logger.hrl").
-type hex() :: binary().
-define(SEC, 1).
-define(MIN, 60*SEC).
-define(DEAD_DROP_TTL, 30*MIN).
-record(dd,
{created_at :: integer(),
payload :: string()}).
-type dd() :: #dd{}.
-record(s,
{drops = #{} :: #{hex() := dd()}}).
-type state() :: #s{}.
%%-----------------------------------------------------------------------------
%% caller context
%%-----------------------------------------------------------------------------
-spec mkdd(NetworkId, Recipient, Amount, Payload) -> Result
when NetworkId :: string(),
Recipient :: string(),
Amount :: non_neg_integer(),
Payload :: binary(),
Result :: {ok, URL, QR_PNG}
| {error, string()},
URL :: string(),
QR_PNG :: binary().
% @doc
% make a dead drop
mkdd(NetworkId, Recipient, Amount, Payload) ->
gen_server:call(?MODULE, {mkdd, NetworkId, Recipient, Amount, Payload}).
%% gen_server callbacks
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, none, []).
%%-----------------------------------------------------------------------------
%% process context below this line
%%-----------------------------------------------------------------------------
%% gen_server callbacks
init(none) ->
tell("starting fd_gridsd"),
InitState = #s{},
{ok, InitState}.
handle_call({mkdd, NetworkId, Recipient, Amount, Payload}, From, State) ->
case do_mkdd(NetworkId, Recipient, Amount, Payload, State) of
{ok, URL, PNG, NewState} -> {reply, {ok, URL, PNG}, NewState};
Error -> {reply, Error, State}
end;
handle_call(Unexpected, From, State) ->
tell("~tp: unexpected call from ~tp: ~tp", [?MODULE, Unexpected, From]),
{noreply, State}.
handle_cast(Unexpected, State) ->
tell("~tp: unexpected cast: ~tp", [?MODULE, Unexpected]),
{noreply, State}.
handle_info(Unexpected, State) ->
tell("~tp: unexpected info: ~tp", [?MODULE, Unexpected]),
{noreply, State}.
code_change(_, State, _) ->
{ok, State}.
terminate(_, _) ->
ok.
%%-----------------------------------------------------------------------------
%% internals
%%-----------------------------------------------------------------------------
-spec do_mkdd(NetworkId, Recipient, Amount, Payload, State) -> Result
when NetworkId :: string(),
Recipient :: string(),
Amount :: pos_integer(),
Payload :: binary(),
State :: state(),
Result :: {ok, URL, QR_PNG, NewState}
| {error, Reason},
URL :: string(),
QR_PNG :: binary(),
NewState :: state(),
Reason :: any().
do_mkdd(NetId, Recipient, Amount, Payload, State = #s{drops = Drops}) ->
HexStr = rand_hex(),
CreatedAt = now_sec(),
TxStr = form_txstr(NetId, Recipient, Amount, Payload),
DD = #dd{created_at = CreatedAt, payload = TxStr},
URL = dd_url(HexStr),
PNG = qr:encode_png(unicode:characters_to_binary(URL)),
NewDrops = maps:put(HexStr, DD, Drops),
NewState = State#s{drops = NewDrops},
{ok, URL, PNG, NewState}.
now_sec() ->
calendar:datetime_to_gregorian_seconds(calendar:universal_time()).
-spec rand_hex() -> hex().
rand_hex() ->
unicode:characters_to_binary(hexify(rand:bytes(10))).
hexify(<<B:8, Rest/binary>>) when B < 16#10 -> ["0", integer_to_list(B, 16), hexify(Rest)];
hexify(<<B:8, Rest/binary>>) -> [integer_to_list(B, 16), hexify(Rest)];
hexify(<<>>) -> [].
dd_url(HexStr) ->
unicode:characters_to_list(["grids://", fewd:host(), "/1/d/", HexStr]).
% ref
form_txstr(_NetId, _Recipient, _Amount, _Payload) ->
"foobar".
+8 -1
View File
@@ -23,6 +23,12 @@ start_link() ->
init([]) ->
RestartStrategy = {one_for_one, 1, 60},
WebRTC = {fd_httpd_webrtc,
{fd_httpd_webrtc, start_link, []},
permanent,
5000,
worker,
[fd_httpd_webrtc]},
FileCache = {fd_httpd_sfc,
{fd_httpd_sfc, start_link, []},
permanent,
@@ -35,5 +41,6 @@ init([]) ->
5000,
supervisor,
[fd_httpd_clients]},
Children = [FileCache, Clients],
Children = [WebRTC, FileCache, Clients],
%Children = [FileCache, Clients],
{ok, {RestartStrategy, Children}}.
+105 -59
View File
@@ -135,9 +135,19 @@ loop(Parent, Debug, State = #s{socket = Socket, next = Next0}) ->
Received = <<Next0/binary, Message/binary>>,
case qhl:parse(Socket, Received) of
{ok, Req, Next1} ->
Next2 = handle_request(Socket, Req, Next1),
%% FIXME: unfuck received logic here
%% handle_request should eventually call back into
%% loop/3 or close the socket
%%
%% also loop/3 should be loop/1, because we need to pass
%% the state down the callchains
Next2 =
case Next1 of
none -> <<>>;
Bin -> Bin
end,
NewState = State#s{next = Next2},
loop(Parent, Debug, NewState);
handle_request(Req, Parent, Debug, NewState);
Error ->
%% should trigger bad request
tell(error, "~p QHL parse error: ~tp", [?LINE, Error]),
@@ -210,41 +220,44 @@ system_replace_state(StateFun, State) ->
%%% http request handling
-spec handle_request(Sock, Request, Received) -> NewReceived
when Sock :: gen_tcp:socket(),
Request :: request(),
Received :: binary(),
NewReceived :: binary().
-spec handle_request(Request, Parent, Debug, State) -> no_return()
when Request :: request(),
Parent :: pid(),
Debug :: [sys:dbg_opt()],
State :: state().
handle_request(Sock, R = #request{method = M, path = P}, Received) when M =/= undefined, P =/= undefined ->
tell("~tp ~tp ~ts", [self(), M, P]),
route(Sock, M, P, R, Received).
handle_request(Req = #request{method = Method, path = Path}, Parent, Debug, State)
when Method =/= undefined,
Path =/= undefined ->
tell("~tp ~tp ~ts", [self(), Method, Path]),
route(Method, Path, Req, Parent, Debug, State).
-spec route(Sock, Method, Route, Request, Received) -> NewReceived
when Sock :: gen_tcp:socket(),
Method :: get | post,
Route :: binary(),
Request :: request(),
Received :: binary(),
NewReceived :: binary().
route(Sock, get, Route, Request, Received) ->
% hardcode routes first
route(get, Route, Request, P, D, S = #s{socket = Sock}) ->
case Route of
<<"/ws/echo">> -> ws_echo(Sock, Request) , Received;
<<"/">> -> route_static(Sock, <<"/index.html">>) , Received;
_ -> route_static(Sock, Route) , Received
<<"/ws/echo">> -> ws_echo(Sock, Request);
<<"/ws/webrtc">> ->
ws_webrtc(Request, S);
<<"/">> ->
route_static(Sock, <<"/index.html">>),
loop(P, D, S);
_ ->
route_static(Sock, Route),
loop(P, D, S)
end;
route(Sock, post, Route, Request, Received) ->
route(post, Route, Request, P, D, S = #s{socket = Sock}) ->
case Route of
<<"/grids-mkdd">> -> grids_mkdd(Sock, Request) , Received;
<<"/wfcin">> -> wfcin(Sock, Request) , Received;
_ -> fd_httpd_utils:http_err(Sock, 404) , Received
<<"/wfcin">> ->
wfcin(Sock, Request),
loop(P, D, S);
_ ->
fd_httpd_utils:http_err(Sock, 404),
loop(P, D, S)
end;
route(Sock, _, _, _, Received) ->
route(_, _, _, P, D, S = #s{socket = Sock}) ->
fd_httpd_utils:http_err(Sock, 404),
Received.
loop(P, D, S).
@@ -280,6 +293,69 @@ respond_static(Sock, not_found) ->
fd_httpd_utils:http_err(Sock, 404).
%% ------------------------------
%% webrtc
%% ------------------------------
ws_webrtc(Request, State = #s{socket = Sock}) ->
try
case qhl_ws:handshake(Request) of
{ok, Response} ->
fd_httpd_utils:respond(Sock, Response),
% wait for username
ok = fd_httpd_webrtc:join(),
ws_webrtc_loop(State);
Error ->
tell("ws_webrtc: error: ~tp", [Error]),
fd_httpd_utils:http_err(Sock, 400),
exit(bad_request)
end
catch
exit:Y:Z ->
tell("~tp ws_webrtc: dying peacefully: ~tp:~tp", [self(), Y, Z]),
gen_tcp:close(Sock);
error:Y:Z ->
tell(error, "~tp ws_webrtc: ERROR ~tp:~tp", [self(), Y, Z]),
fd_httpd_utils:http_err(Sock, 500),
error(internal_server_error)
end.
-spec ws_webrtc_loop(state()) -> no_return().
% we have no tcp bytes waiting to be parsed
ws_webrtc_loop(State = #s{socket = Socket, next = <<>>}) ->
ok = inet:setopts(Socket, [{active, once}]),
receive
{tcp, Socket, Message} ->
NewState = State#s{next = Message},
ws_webrtc_loop(NewState);
{webrtc, Message} ->
ok = ws_webrtc_relay(Socket, s2c, Message),
ws_webrtc_loop(State);
{tcp_closed, Socket} ->
ok = tell("~p Socket closed, retiring.~n", [self()]),
exit(normal)
end;
% we have TCP bytes sitting and waiting to be parsed
ws_webrtc_loop(State = #s{socket = Sock, next = Recv}) ->
{ok, Message, NewRecv} = qhl_ws:recv_json(Sock, Recv, 5000),
ok = ws_webrtc_relay(Sock, c2s, Message),
ws_webrtc_loop(State#s{next = NewRecv}).
% relay
% message is a zj:value()
ws_webrtc_relay(Socket, Direction, ZJValue) ->
tell("~p ws_webrtc_relay ~p: ~p", [self(), Direction, ZJValue]),
case Direction of
c2s -> fd_httpd_webrtc:hi(ZJValue);
s2c -> qhl_ws:send_json(Socket, ZJValue)
end,
ok.
%% ------------------------------
%% echo
%% ------------------------------
@@ -320,36 +396,6 @@ ws_echo_loop(Sock, Frames, Received) ->
error(Error)
end.
%% ------------------------------
%% grids
%% ------------------------------
grids_mkdd(Sock, #request{enctype = json,
body = B = #{"network_id" := NetId,
"recipient" := Recipient,
"amount" := Amount,
"payload" := Payload}}) ->
tell("grids_mkdd good request: ~tp", [B]),
RespObj =
case fd_gridsd:mkdd(NetId, Recipient, Amount, unicode:characters_to_binary(Payload)) of
{ok, URL, PNG} ->
#{"ok" => true,
"result" => #{"url" => URL,
"png_base64" => unicode:characters_to_list(base64:encode(PNG))}};
{error, String} ->
#{"ok" => false,
"error" => String}
end,
Body = zj:encode(RespObj),
% update cache with new context
Response = #response{headers = [{"content-type", "application/json"}],
body = Body},
fd_httpd_utils:respond(Sock, Response);
grids_mkdd(Sock, Request) ->
tell("grids_mkdd: bad request: ~tp", [Request]),
fd_httpd_utils:http_err(Sock, 400).
%% ------------------------------
%% wfc
+165
View File
@@ -0,0 +1,165 @@
% @doc webrtc peer pool
-module(fd_httpd_webrtc).
-behavior(gen_server).
%% api (caller context)
-export([
join/0, hi/1
]).
% startup/gen_server callbacks
-export([
% caller context
start_link/0,
% process context
init/1, handle_call/3, handle_cast/2, handle_info/2,
code_change/3, terminate/2
]).
-include("$zx_include/zx_logger.hrl").
-record(u, {pid :: pid()}).
-type user() :: #u{}.
-record(s, {roster = [] :: [user()]}).
-type state() :: #s{}.
%%-----------------------------------------------------------------------------
%% caller context
%%-----------------------------------------------------------------------------
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, none, []).
-spec join() -> ok | {error, any()}.
join() ->
gen_server:call(?MODULE, join).
-spec hi(Message) -> ok
when Message :: zj:value().
hi(Message) ->
gen_server:cast(?MODULE, {hi, self(), Message}).
%%-----------------------------------------------------------------------------
%% process context
%%-----------------------------------------------------------------------------
%% gen_server callbacks
init(none) ->
tell("starting fd_httpd_webrtc", []),
InitState = #s{},
{ok, InitState}.
handle_call(join, _From = {PID, _Tag}, State) ->
{Reply, NewState} = do_join(PID, State),
{reply, Reply, NewState};
handle_call(Unexpected, From, State) ->
tell("~tp: unexpected call from ~tp: ~tp", [?MODULE, Unexpected, From]),
{noreply, State}.
handle_cast(Unexpected, State) ->
tell("~tp: unexpected cast: ~tp", [?MODULE, Unexpected]),
{noreply, State}.
handle_info({'DOWN', _Ref, process, PID, _Reason}, State) ->
NewState = do_down(PID, State),
{noreply, NewState};
handle_info(Unexpected, State) ->
tell("~tp: unexpected info: ~tp", [?MODULE, Unexpected]),
{noreply, State}.
code_change(_, State, _) ->
{ok, State}.
terminate(_, _) ->
ok.
%%---------------------
%% doers
%%---------------------
-spec do_join(PID, State) -> {Reply, NewState}
when PID :: pid(),
State :: state(),
Reply :: ok
| {error, any()},
NewState :: state().
% @private join a user to a pool
do_join(PID, State = #s{roster = Users}) ->
% see if pid is already there
case lists:keymember(PID, #u.pid, Users) of
true -> {error, already_joined};
false -> do_join2(PID, State)
end.
do_join2(PID, State = #s{roster = Users}) ->
monitor(process, PID),
NewUser = #u{pid = PID},
NewRoster = usort([NewUser | Users]),
NewState = State#s{roster = NewRoster},
ok = whisper(whoami, PID),
ok = gossip(roster, NewState),
{ok, NewState}.
usort(Users) ->
lists:keysort(#u.pid, Users).
-spec do_down(PID, State) -> NewState
when PID :: pid(),
State :: state(),
NewState :: state().
% @private handle a user dying
do_down(PID, State = #s{roster = Users}) ->
% remove from roster
NewUsers = lists:keydelete(PID, #u.pid, Users),
NewState = State#s{roster = NewUsers},
% broadcast username
ok = gossip(roster, NewState),
NewState.
% @doc send a message to a specific person
whisper(whoami, PID) ->
PID ! {webrtc, ["whoami", pidstr(PID)]},
ok.
% @doc send a message to everyone
gossip(roster, State = #s{roster = Us}) ->
Pids = [pidstr(PID) || #u{pid = PID} <- Us],
gossip_json(["roster", Pids], State).
pidstr(PID) ->
unicode:characters_to_list(io_lib:format("~tp", [PID])).
-spec gossip_json(zj:value(), state()) -> ok.
% @private gossip a message to everyone
gossip_json(Message, #s{roster = Users}) ->
GossipTo =
fun(#u{pid = PID}) ->
PID ! {webrtc, Message}
end,
lists:foreach(GossipTo, Users).
-84
View File
@@ -1,84 +0,0 @@
% @doc hz helper functions
-module(fd_hz).
-export_type([
]).
-export([
txs_current/0,
txs_since_height/1,
txs_minus/1,
txs_from_to/2,
current_height/0,
txs_of_height/1,
mhs_of_height/1,
txs_of_mh/1,
filter_spends/1,
filter_spend/1,
spend_info/1,
test/0
]).
-spec test() -> no_return().
test() ->
io:format("~tp~n", [hz:gen_current()]),
io:format("~tp~n", [hz:kb_current()]),
ok.
txs_current() ->
H = current_height(),
txs_of_height(H).
txs_since_height(H) ->
txs_from_to(H, current_height()).
txs_minus(N) ->
H = current_height(),
L = H - N,
txs_from_to(L, H).
txs_from_to(Min, Max) when Min =< Max ->
lists:append([txs_of_height(H) || H <- lists:seq(Min, Max)]);
txs_from_to(Min, Max) when Min > Max ->
[].
current_height() ->
{ok, H} = hz:kb_current_height(),
H.
txs_of_height(Height) ->
lists:append([txs_of_mh(MH) || MH <- mhs_of_height(Height)]).
-spec mhs_of_height(Height :: pos_integer()) -> term().
mhs_of_height(Height) ->
{ok, #{"micro_blocks" := MHs}} = hz:gen_by_height(Height),
MHs.
txs_of_mh(MH) ->
{ok, TXs} = hz:mb_txs(MH),
TXs.
filter_spends(TXs) ->
lists:filtermap(fun filter_spend/1, TXs).
filter_spend(#{"tx" := TX = #{"type" := "SpendTx"}}) -> {true, spend_info(TX)};
filter_spend(_) -> false.
spend_info(#{"type" := "SpendTx",
"recipient_id" := Recipient,
"amount" := Amount,
"payload" := Payload}) ->
{sp, Recipient, Amount, Payload}.
-213
View File
@@ -1,213 +0,0 @@
% @doc spy: knows constraints {recipient, amount, payload}
%
% spies the chain for transactions that satisfy that constraint
%
-module(fd_spy).
-vsn("0.2.0").
% MVP: register search patterns
% simply print to console when one of them is found
-behavior(gen_server).
-export_type([
]).
-export([
%% caller context
reg/3,
%% api
start_link/0,
%% process context
init/1, handle_call/3, handle_cast/2, handle_info/2,
code_change/3, terminate/2
]).
% recipient is a string
-type pubkey32() :: <<_:256>>.
-record(sp,
{recipient :: pubkey32(),
amount :: pos_integer(),
payload :: binary()}).
-type search_pattern() :: #sp{}.
-record(s,
{last_height_seen = none :: none | integer(),
searching_for = [] :: [search_pattern()]}).
-type state() :: #s{}.
-include("$zx_include/zx_logger.hrl").
%%-----------------------------------------------------------------------------
%% caller context
%%-----------------------------------------------------------------------------
-spec reg(Recipient, Amount, Payload) -> ok | {error, Reason}
when Recipient :: pubkey32(),
Amount :: pos_integer(),
Payload :: binary(),
Reason :: any().
reg(R, A, P) when is_binary(R), byte_size(R) =:= 32,
is_integer(A), A >= 0,
is_binary(P) ->
gen_server:call(?MODULE, {reg, R, A, P}).
%% gen_server callbacks
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, none, []).
%%-----------------------------------------------------------------------------
%% process context below this line
%%-----------------------------------------------------------------------------
%% gen_server callbacks
-spec init(none) -> {ok, state()}.
init(none) ->
tell("starting fd_spy"),
hz:chain_nodes(fewd:chain_nodes()),
erlang:send_after(1000, self(), check_chain),
InitState = #s{},
{ok, InitState}.
-spec handle_call(Msg, From, State) -> Result
when Msg :: any(),
From :: {pid(), reference()},
State :: state(),
Result :: {reply, Reply, NewState}
| {noreply, NewState},
Reply :: any(),
NewState :: state().
handle_call({reg, Recipient, Amount, Payload}, _From, State) ->
{Reply, NewState} = do_reg(Recipient, Amount, Payload, State),
{reply, Reply, NewState};
handle_call(Unexpected, From, State) ->
tell("~tp: unexpected call from ~tp: ~tp", [?MODULE, Unexpected, From]),
{noreply, State}.
handle_cast(Unexpected, State) ->
tell("~tp: unexpected cast: ~tp", [?MODULE, Unexpected]),
{noreply, State}.
handle_info(check_chain, State) ->
NewState = do_check_chain(State),
erlang:send_after(1000, self(), check_chain),
{noreply, NewState};
handle_info(Unexpected, State) ->
tell("~tp: unexpected info: ~tp", [?MODULE, Unexpected]),
{noreply, State}.
code_change(_, State, _) ->
{ok, State}.
terminate(_, _) ->
ok.
%%-----------------------------------------------------------------------------
%% internals
%%-----------------------------------------------------------------------------
do_check_chain(State = #s{last_height_seen = none}) ->
case hz:kb_current_height() of
{ok, Max} ->
hh(Max-1, Max, State);
Error ->
tell("~tp hz error: ~tp", [?MODULE, Error]),
State
end;
do_check_chain(State = #s{last_height_seen = Min}) ->
case hz:kb_current_height() of
{ok, Max} ->
hh(Min, Max, State);
Error ->
tell("~tp hz error: ~tp", [?MODULE, Error]),
State
end.
% handle height
hh(PrevHeight, NewHeight, State) when PrevHeight < NewHeight ->
tell("~tp cool: PrevHeight=~tp, NewHeight=~tp", [?MODULE, PrevHeight, NewHeight]),
Spends = fd_hz:filter_spends(fd_hz:txs_from_to(PrevHeight + 1, NewHeight)),
tell("~tp spends: ~tp", [?MODULE, Spends]),
NewState = State#s{last_height_seen = NewHeight},
NewState;
hh(PrevHeight, NewHeight, State) when PrevHeight >= NewHeight ->
log(info, "~tp lame: PrevHeight=~tp, NewHeight=~tp", [?MODULE, PrevHeight, NewHeight]),
State.
-spec do_reg(Recipient, Amount, Payload, State) -> {Reply, NewState}
when Recipient :: pubkey32(),
Amount :: pos_integer(),
Payload :: binary(),
State :: state(),
Reply :: ok
| {error, Reason},
Reason :: any(),
NewState :: state().
do_reg(Recipient, Amount, Payload, State) ->
case already_registered(Recipient, Payload, State) of
true -> {error, already_registered};
false -> really_register(Recipient, Amount, Payload, State)
end.
-spec already_registered(Recipient, Payload, State) -> boolean()
when Recipient :: pubkey32(),
Payload :: binary(),
State :: state().
already_registered(Recipient, Payload, _State = #s{searching_for = SPs}) ->
ar(Recipient, Payload, SPs).
ar(Recipient, Payload, [#sp{recipient=Recipient, payload=Payload} | _]) ->
true;
ar(Recipient, Payload, [_ | Rest]) ->
ar(Recipient, Payload, Rest);
ar(_, _, []) ->
false.
-spec really_register(Recipient, Amount, Payload, State) -> {Reply, NewState}
when Recipient :: pubkey32(),
Amount :: pos_integer(),
Payload :: binary(),
State :: state(),
Reply :: ok
| {error, Reason},
Reason :: any(),
NewState :: state().
really_register(R, A, P, State = #s{searching_for = SPs}) ->
NewSearchPattern = #sp{recipient = R,
amount = A,
payload = P},
tell("~tp: really_register(~tp,~tp,~tp)", [?MODULE, R, A, P]),
NewSearchPatterns = [NewSearchPattern | SPs],
NewState = State#s{searching_for = NewSearchPatterns},
{reply, ok, NewState}.
+1 -13
View File
@@ -36,18 +36,6 @@ start_link() ->
init([]) ->
RestartStrategy = {one_for_one, 1, 60},
Spy = {fd_spy,
{fd_spy, start_link, []},
permanent,
5000,
worker,
[fd_spy]},
GridsD = {fd_gridsd,
{fd_gridsd, start_link, []},
permanent,
5000,
worker,
[fd_gridsd]},
WFCd = {fd_wfcd,
{fd_wfcd, start_link, []},
permanent,
@@ -60,5 +48,5 @@ init([]) ->
5000,
supervisor,
[fd_httpd]},
Children = [Spy, GridsD, WFCd, Httpd],
Children = [WFCd, Httpd],
{ok, {RestartStrategy, Children}}.
-23
View File
@@ -9,34 +9,12 @@
-copyright("Peter Harpending <peterharpending@qpq.swiss>").
-license("BSD-2-Clause-FreeBSD").
-export([chain_nodes/0, url/0, host/0, network_id/0, pubkey/0, akstr/0]).
-export([listen/1, ignore/0]).
-export([start/2, stop/1]).
-include("$zx_include/zx_logger.hrl").
%% for testing: use mainnet
%chain_nodes() ->
% [{"tsuriai.jp", 3013}].
chain_nodes() ->
[{"tsuriai.jp", 4013}].
url() -> "http://" ++ host().
host() -> "localhost:8000".
network_id() -> "groot.testnet".
pubkey() -> pad32(<<"fewd demo">>).
akstr() -> gmgrids:akstr(pubkey()).
pad32(Bytes) ->
BS = byte_size(Bytes),
Spaces = << <<" ">>
|| _ <- lists:seq(BS, 31)
>>,
<<Bytes/bytes, Spaces/bytes>>.
-spec listen(PortNum) -> Result
when PortNum :: inet:port_num(),
Result :: ok
@@ -64,7 +42,6 @@ ignore() ->
%% See: http://erlang.org/doc/apps/kernel/application.html
start(normal, _Args) ->
ok = application:ensure_started(hakuzaru),
Result = fd_sup:start_link(),
ok = listen(8000),
Result.
-618
View File
@@ -1,618 +0,0 @@
% @doc
% GRIDS library: grids
%
% This module simply handles encoding and decoding of GRIDS URLs.
%
% For documentation on GRIDS see
%
% https://git.qpq.swiss/QPQ-AG/research-megadoc/wiki/GRIDS
% @end
-module(gmgrids).
-vsn("0.2.0").
-author("Peter Harpending <peterharpending@qpq.swiss>").
-copyright("2025 QPQ AG").
-license("MIT").
% TODONE:
%
% TODO:
%
% - possibly input types should be iolists. I think for
% binaries it's fine
% - change the types... don't need 3 different record types
% FIXEDME:
%
% FIXME:
-export_type([
% field types
host/0,
target/0, akstr/0, pubkey/0,
% record types
grids/0
]).
-export([
% convenience functions
%% currency granularities
p/0, kp/0, mp/0, gp/0, tp/0, pp/0,
g/0, kg/0, mg/0, gg/0, tg/0,
%% type fuckery
target_to_path/1, akstr/1, unakstr/1,
dummy_target/0,
%% convenience encoders
encode/2,
% "primitives"
%% record constructor
mk_grids/2,
%% primitive encoders
encode/1, encode/7, percent_encode/1,
%% decoder
decode/1
]).
%%-------------------------------------------------------------------
%% API: TYPES
%%-------------------------------------------------------------------
% @doc Future-proofing... later this could be inet:addr() or whatever, or maybe
% {Host, Port}. Keeping it simple for now
-type host() :: string().
% @doc ak_... string
-type akstr() :: string().
% @doc 32-byte public key
-type pubkey() :: <<_:256>>.
% @doc later might want this to be flexible, "ak_..." etc
%
% FIXME: add support for all the different api keys: ak_..., ct_..., etc
-type target() :: pubkey()
| akstr().
-record(grids,
{secure = true :: boolean(),
host :: host(),
version = 1 :: integer(),
instruction :: dead_drop | spend | transfer,
path :: string(),
amount = none :: none | {value, integer()},
payload = none :: none | {value, binary()}}).
-type grids() :: #grids{}.
%%-------------------------------------------------------------------
%% API: CONVENIENCE FUNCTIONS
%%-------------------------------------------------------------------
%% currency granularities
p() -> 1.
kp() -> 1_000.
mp() -> 1_000_000.
gp() -> 1_000_000_000.
tp() -> 1_000_000_000_000.
pp() -> 1_000_000_000_000_000.
g() -> 1_000_000_000_000_000_000.
kg() -> 1_000_000_000_000_000_000_000.
mg() -> 1_000_000_000_000_000_000_000_000.
gg() -> 1_000_000_000_000_000_000_000_000_000.
tg() -> 1_000_000_000_000_000_000_000_000_000_000.
-spec target_to_path(Target) -> Path
when Target :: target(),
Path :: string().
% @doc
% Internal function exported for convenience purposes
%
% If `Target' is an "ak_..." string, leave as-is. If it's a 32 byte public key
% encode as an ak_... string
target_to_path(Target) ->
i_ttp(iolist_to_binary(Target)).
i_ttp(ApiStr = <<"ak_", _/binary>>) -> ApiStr;
i_ttp(Pubkey = <<_:32/bytes>>) -> akstr(Pubkey);
i_ttp(BadTarget) -> error({invalid_target, BadTarget}).
%% akstr/unakstr
-spec akstr(Pubkey) -> AkStr
when Pubkey :: pubkey(),
AkStr :: akstr().
% @doc
% convert a 32-byte public key into an ak_... string
akstr(PK) ->
unicode:characters_to_list(gmser_api_encoder:encode(account_pubkey, PK)).
-spec unakstr(AkStr) -> Pubkey
when Pubkey :: pubkey(),
AkStr :: string().
% @doc
% convert an ak_... string into a 32-byte public key
unakstr(Akstr) ->
{_, PK} = gmser_api_encoder:decode(unicode:characters_to_binary(Akstr)),
PK.
-spec dummy_target() -> akstr().
% @doc Make a dummy public key. For testing purposes. NOT secure!
dummy_target() ->
akstr(rand:bytes(32)).
-spec encode(Args, Options) -> URL
when Args :: {dead_drop, Host, Path}
| {spend, NetworkId, Recipient}
| {transfer, Host, Path},
Host :: iolist(),
Path :: iolist(),
NetworkId :: iolist(),
Recipient :: target(),
Options :: [Opt],
Opt :: {secure, boolean()}
| {version, integer()}
| {amount, Amount}
| {payload, Payload},
Amount :: integer() | none | {value, integer()},
Payload :: iolist() | none | {value, iolist()},
URL :: string().
encode(Args, Opts) ->
encode(mk_grids(Args, Opts)).
%%-------------------------------------------------------------------
%% API: RECORD CONSTRUCTORS
%%-------------------------------------------------------------------
-record(o,
{secure = true :: boolean(),
version = 1 :: integer(),
amount = none :: none | {value, integer()},
payload = none :: none | {value, binary()}}).
-spec mk_grids(Args, Options) -> Grids
when Args :: {dead_drop, Host, Path}
| {spend, NetworkId, Recipient}
| {transfer, Host, Path},
Host :: iolist(),
Path :: iolist(),
NetworkId :: iolist(),
Recipient :: target(),
Options :: [Opt],
Opt :: {secure, boolean()}
| {version, integer()}
| {amount, Amount}
| {payload, Payload},
Amount :: integer() | none | {value, integer()},
Payload :: iolist() | none | {value, iolist()},
Grids :: #grids{}.
mk_grids(Args, Options) ->
#o{secure = Secure,
version = Version,
amount = MaybeAmount,
payload = MaybePayload} = i_valid_opts(Options),
{Instruction, HostStr, Path} =
case Args of
{dead_drop, H0, P0} ->
H1 = unicode:characters_to_list(H0),
P1 = unicode:characters_to_list(P0),
{dead_drop, H1, P1};
{spend, NetId0, Recip0} ->
NetId1 = unicode:characters_to_list(NetId0),
Recip1 = target_to_path(Recip0),
{spend, NetId1, Recip1};
{transfer, H0, P0} ->
H1 = unicode:characters_to_list(H0),
P1 = unicode:characters_to_list(P0),
{transfer, H1, P1}
end,
#grids{secure = Secure,
host = HostStr,
version = Version,
instruction = Instruction,
path = Path,
amount = MaybeAmount,
payload = MaybePayload}.
i_valid_opts(Options) ->
Secure =
case proplists:get_value(secure, Options, true) of
S when is_boolean(S) -> S;
W -> error({invalid_option, {secure, W}})
end,
Version =
case proplists:get_value(version, Options, 1) of
V when is_integer(V) -> V;
X -> error({invalid_option, {version, X}})
end,
Amount =
case proplists:get_value(amount, Options, none) of
none -> none;
{value, N} when is_integer(N) -> {value, N};
N when is_integer(N) -> {value, N};
Y -> error({invalid_option, {amount, Y}})
end,
Payload =
case proplists:get_value(payload, Options, none) of
none -> none;
{value, P} -> {value, iolist_to_binary(P)};
P -> {value, iolist_to_binary(P)}
end,
#o{secure = Secure,
version = Version,
amount = Amount,
payload = Payload}.
%%-------------------------------------------------------------------
%% API: ENCODING (Record -> URL)
%%-------------------------------------------------------------------
-spec encode(GRIDS) -> URL
when GRIDS :: grids(),
URL :: string().
% @doc
% Encode a grids record type
% @end
encode(#grids{secure = Secure,
host = Host,
version = Vsn,
instruction = Instruction,
path = Path,
amount = Amt,
payload = Payload}) ->
encode(Secure, Host, Vsn, Instruction, Path, Amt, Payload).
-spec encode(Secure, Host, Version, Instruction, Path, Amount, Payload) -> URL
when Secure :: boolean(),
Host :: host(),
Version :: integer(),
Instruction :: dead_drop | spend | transfer,
Path :: string(),
Amount :: none | {value, integer()},
Payload :: none | {value, binary()},
URL :: string().
% @doc
% internal encode that's more verbose
encode(Secure, Host, Version, Instruction, Path, Amount, Payload) ->
unicode:characters_to_list(
["grid", i_encode_secure(Secure),
"://", i_encode_host(Host),
"/", integer_to_list(Version),
"/", i_encode_instruction(Instruction),
"/", Path,
i_encode_qstr(Amount, Payload)]
).
i_encode_secure(true) -> "s";
i_encode_secure(false) -> "".
% future-proofing against more complicated host arguments
i_encode_host(Host) -> Host.
i_encode_instruction(dead_drop) -> "d";
i_encode_instruction(spend) -> "s";
i_encode_instruction(transfer) -> "t".
i_encode_qstr(none, none) ->
"";
i_encode_qstr({value, Amt}, none) ->
["?a=", integer_to_list(Amt)];
i_encode_qstr(none, {value, Payload}) ->
["?p=", percent_encode(Payload)];
i_encode_qstr({value, Amt}, {value, Payload}) ->
["?a=", integer_to_list(Amt),
"&p=", percent_encode(Payload)].
-spec percent_encode(Payload) -> PercentEncoded
when Payload :: binary(),
PercentEncoded :: iolist().
% @doc
% internal function to percent-encode binary payload
% exported for convenience
%
% See: https://en.wikipedia.org/wiki/Percent-encoding
percent_encode(Payload) when is_binary(Payload) ->
i_percent_encode(Payload, []).
% unreserved characters
i_percent_encode(<<C:8, Rest/binary>>, Acc)
when ($A =< C andalso C =< $Z) orelse
($a =< C andalso C =< $z) orelse
($0 =< C andalso C =< $9) orelse
(C =:= $-) orelse
(C =:= $_) orelse
(C =:= $~) orelse
(C =:= $.) ->
i_percent_encode(Rest, [Acc, C]);
i_percent_encode(<<B:8, Rest/binary>>, Acc) ->
i_percent_encode(Rest, [Acc, i_pe_byte(B)]);
i_percent_encode(<<>>, Result) ->
Result.
% single hex digit
i_pe_byte(B) when 16#00 =< B, B =< 16#0F -> ["%0", integer_to_list(B, 16)];
i_pe_byte(B) when 16#10 =< B, B =< 16#FF -> ["%", integer_to_list(B, 16)].
%%---------------------------------------------------------
%% API: DECODING
%%---------------------------------------------------------
-record(dt,
{secure = undefined :: undefined | boolean(),
host = undefined :: undefined | iolist(),
version = undefined :: undefined | integer(),
instruction = undefined :: undefined | spend | transfer | dead_drop,
path = undefined :: undefined | iolist(),
amount = undefined :: undefined | none | {value, integer()},
payload = undefined :: undefined | none | {value, binary()}}).
% -type decode_target() :: #dt{}.
-spec decode(URL) -> Result
when URL :: string(),
Result :: {ok, grids(), Remainder :: string()}
| {error, Reason},
Reason :: term().
decode(URL) ->
case i_decode(unicode:characters_to_binary(URL)) of
{ok, DT, Remainder} ->
{ok, i_decode_dt(DT), unicode:characters_to_list(Remainder)};
Error ->
Error
end.
i_decode_dt(#dt{secure = S,
host = H,
version = V,
path = P,
instruction = Instruction,
amount = A,
payload = L}) ->
HStr = unicode:characters_to_list(H),
PStr = unicode:characters_to_list(P),
#grids{secure = S,
host = HStr,
version = V,
path = PStr,
instruction = Instruction,
amount = A,
payload = L}.
i_decode(URL) ->
i_pipeline([fun i_decode_secure/2,
fun i_decode_host/2,
fun i_decode_version/2,
fun i_decode_instruction/2,
fun i_decode_path/2,
fun i_decode_qstr/2],
{ok, #dt{}, URL}).
i_pipeline([Fun | Funs], Acc) ->
case Acc of
{ok, DT, URL} -> i_pipeline(Funs, Fun(DT, URL));
Error -> Error
end;
i_pipeline([], Result) ->
Result.
i_decode_secure(DT = #dt{secure = undefined},
<<"grid://", Rest/binary>>) ->
{ok, DT#dt{secure = false}, Rest};
i_decode_secure(DT = #dt{secure = undefined},
<<"grids://", Rest/binary>>) ->
{ok, DT#dt{secure = true}, Rest};
i_decode_secure(_, URL) ->
{error, {bad_protocol, URL}}.
i_decode_host(DT = #dt{host = undefined}, URL) ->
case idh2([], URL) of
{ok, Host, Rest} -> {ok, DT#dt{host = Host}, Rest};
Error -> Error
end.
% eliminate empty hosts and hosts not followed by /
idh2([], <<$/:8, _/binary>>) -> {error, empty_host};
idh2([], <<>>) -> {error, empty_host};
idh2(Host, <<>>) -> {error, {bad_host, Host}};
idh2(Host, <<$/:8, Rest/binary>>) -> {ok, Host, Rest};
idh2(Acc, <<Char:8, Rest/binary>>) -> idh2([Acc, Char], Rest).
i_decode_version(DT = #dt{version = undefined}, URL) ->
case idv2([], URL) of
{ok, VStr, Rest} ->
Version = list_to_integer(unicode:characters_to_list(VStr)),
NewDT = DT#dt{version = Version},
{ok, NewDT, Rest};
Error ->
Error
end.
idv2([], <<$/:8, _/binary>>) -> {error, empty_host};
idv2([], <<>>) -> {error, empty_host};
idv2(Vstr, <<>>) -> {error, {bad_version, iolist_to_binary(Vstr)}};
idv2(Vstr, <<$/:8, Rest/binary>>) -> {ok, Vstr, Rest};
idv2(Acc, <<N:8, Rest/binary>>) ->
case ($0 =< N) andalso (N =< $9) of
true -> idv2([Acc, N], Rest);
false -> {error, {illegal_version_char, [N]}}
end.
i_decode_instruction(DT = #dt{instruction = undefined}, <<"s/", Rest/binary>>) ->
{ok, DT#dt{instruction = spend}, Rest};
i_decode_instruction(DT = #dt{instruction = undefined}, <<"t/", Rest/binary>>) ->
{ok, DT#dt{instruction = transfer}, Rest};
i_decode_instruction(DT = #dt{instruction = undefined}, <<"d/", Rest/binary>>) ->
{ok, DT#dt{instruction = dead_drop}, Rest};
i_decode_instruction(_ = #dt{instruction = undefined}, Bad) ->
{error, {illegal_instruction, Bad}}.
i_decode_path(DT = #dt{path = undefined}, URL) ->
{Path, Rest} = idp([], URL),
{ok, DT#dt{path = Path}, Rest}.
% consume until we get to end of string or ?
idp(Path, <<"?", Rest/binary>>) -> {Path, Rest};
idp(Path, <<>>) -> {Path, <<>>};
idp(Path, <<C:8, Rest/binary>>) -> idp([Path, C], Rest).
i_decode_qstr(DT = #dt{amount = undefined, payload = undefined}, URL) ->
case idq([], URL) of
{ok, Proplist, Remainder} ->
Amount = proplists:get_value(amount, Proplist, none),
Payload = proplists:get_value(payload, Proplist, none),
NewDT = DT#dt{amount = Amount, payload = Payload},
{ok, NewDT, Remainder};
Error ->
Error
end.
-spec idq(Proplist, URL) -> Result
when URL :: binary(),
Result :: {ok, Proplist, Remainder}
| {error, ParseError},
Proplist :: [Prop],
Prop :: {amount, {value, integer()}}
| {payload, {value, binary()}},
ParseError :: any(),
Remainder :: binary().
idq(Params, <<"a=", Rest/binary>>) ->
case i_parse_amt(none, Rest) of
{ok, Amt, NewRest} ->
NewParams = [{amount, {value, Amt}} | Params],
idq(NewParams, NewRest);
Error ->
Error
end;
idq(Params, <<"p=", Rest/binary>>) ->
case i_parse_payload(none, Rest) of
{ok, Payload, NewRest} ->
NewParams = [{payload, {value, Payload}} | Params],
idq(NewParams, NewRest);
Error ->
Error
end;
idq(Params, Rest) ->
{ok, Params, Rest}.
-spec i_parse_amt(MaybeAmount, URL) -> Result
when URL :: binary(),
Result :: {ok, MaybeAmount, Rest}
| {error, term()},
MaybeAmount :: none | {value, integer()},
Rest :: binary().
% @private context here is we have an a= and we're parsing what comes after
% that
%
% we can error on empty amounts
i_parse_amt(Acc, <<DigitChar:8, Rest/binary>>)
when $0 =< DigitChar, DigitChar =< $9 ->
DigitInt = DigitChar - $0,
NewAcc =
case Acc of
none -> {value, DigitInt};
{value, N} -> {value, N*10 + DigitInt}
end,
i_parse_amt(NewAcc, Rest);
% either end of string or non-digit char
i_parse_amt(Acc, <<"&", Rest/binary>>) ->
case Acc of
{value, Amount} -> {ok, Amount, Rest};
none -> {error, empty_amount}
end;
i_parse_amt(Acc, Rest) ->
case Acc of
{value, Amount} -> {ok, Amount, Rest};
none -> {error, empty_amount}
end.
-define(IS_HEX_CHAR(P), ((($0 =< P) andalso (P =< $9)) orelse
(($A =< P) andalso (P =< $F)))).
-spec i_parse_payload(MaybePayload, URL) -> Result
when URL :: binary(),
Result :: {ok, MaybePayload, Rest}
| {error, term()},
MaybePayload :: none | {value, binary()},
Rest :: binary().
% unreserved chars
i_parse_payload(Acc, <<C:8, Rest/binary>>)
when ($A =< C andalso C =< $Z) orelse
($a =< C andalso C =< $z) orelse
($0 =< C andalso C =< $9) orelse
(C =:= $-) orelse
(C =:= $_) orelse
(C =:= $~) orelse
(C =:= $.) ->
NewAcc =
case Acc of
none -> {value, <<C:8>>};
{value, Bytes} -> {value, <<Bytes/bytes, C:8>>}
end,
i_parse_payload(NewAcc, Rest);
% percent char
i_parse_payload(Acc, <<"%", A:8, B:8, Rest/binary>>)
when ?IS_HEX_CHAR(A), ?IS_HEX_CHAR(B) ->
AInt = list_to_integer([A], 16),
BInt = list_to_integer([B], 16),
NewByte = AInt*16 + BInt,
NewAcc =
case Acc of
none -> {value, <<NewByte:8>>};
{value, Bytes} -> {value, <<Bytes/binary, NewByte:8>>}
end,
i_parse_payload(NewAcc, Rest);
% random char
i_parse_payload(Acc, <<"&", Rest/binary>>) ->
case Acc of
none -> {error, empty_payload};
{value, Payload} -> {ok, Payload, Rest}
end;
i_parse_payload(Acc, Rest) ->
case Acc of
none -> {error, {illegal_payload, Rest}};
{value, Payload} -> {ok, Payload, Rest}
end.
+85 -2
View File
@@ -16,7 +16,9 @@
%% porcelain
handshake/1,
recv/3, recv/4,
send/2
recv_strict/3, recv_json/3,
send/2,
send_dwim/2, send_json/2
]).
-include("http.hrl").
@@ -259,6 +261,54 @@ response_token(ChallengeToken) when is_binary(ChallengeToken) ->
base64:encode(Sha1).
-spec recv_json(Socket, Received, TimeoutMS) -> Result
when Socket :: gen_tcp:socket(),
Received :: binary(),
TimeoutMS :: non_neg_integer(),
Result :: {ok, zj:value(), Remainder}
| {error, Reason},
Remainder :: binary(),
Reason :: any().
% @doc
% asserts response is text
recv_json(Sock, Recv, TimeoutMS) ->
case recv_strict(Sock, Recv, TimeoutMS) of
{ok, {Type, Payload}, NewRecv} when Type =:= text orelse
Type =:= binary ->
case zj:decode(Payload) of
{ok, Value} ->
io:format("~p value: ~p~n", [self(), Value]),
{ok, Value, NewRecv};
Error -> {error, {zj_decode, Error}}
end;
Error ->
Error
end.
-spec recv_strict(Socket, Received, TimeoutMS) -> Result
when Socket :: gen_tcp:socket(),
Received :: binary(),
TimeoutMS :: non_neg_integer(),
Result :: {ok, Message, Remainder}
| {error, Reason},
Message :: ws_msg(),
Remainder :: binary(),
Reason :: any().
% @doc
% Almost equivalent to recv/3, but asserts resulting frames are empty
recv_strict(Sock, Recv, TimeoutMS) ->
case recv(Sock, Recv, TimeoutMS) of
{ok, Message, [], Remainder} ->
{ok, Message, Remainder};
Illegal = {ok, _, _NonEmptyFrames, _} ->
{error, {bad_frame_stack, Illegal}};
Error ->
Error
end.
-spec recv(Socket, Received, TimeoutMS) -> Result
when Socket :: gen_tcp:socket(),
@@ -604,10 +654,15 @@ recv_frame(Frame = #frame{payload_length = Len, payload = none}, Sock, Received,
%% factoring this out into a function to reduce repetition
recv_frame_await(Frame, Sock, Received, Timeout) ->
io:format("~p called: recv_frame_await(~p, ~p, ~p, ~p)~n",
[self(), Frame, Sock, Received, Timeout]),
case inet:setopts(Sock, [{active, once}]) of
ok ->
receive
{tcp, Sock, Bin} -> recv_frame(Frame, Sock, <<Received/bits, Bin/binary>>, Timeout);
{tcp, Sock, Bin} ->
io:format("~p calling: recv_frame(~p, ~p, ~p, ~p)~n",
[self(),Frame, Sock, <<Received/bits, Bin/binary>>, Timeout]),
recv_frame(Frame, Sock, <<Received/bits, Bin/binary>>, Timeout);
{tcp_closed, Sock} -> {error, tcp_closed};
{tcp_error, Sock, Reason} -> {error, {tcp_error, Reason}}
after Timeout ->
@@ -618,6 +673,34 @@ recv_frame_await(Frame, Sock, Received, Timeout) ->
end.
send_json(Socket, X) ->
send_dwim(Socket, {json, X}).
-spec send_dwim(Socket, Message) -> Result
when Socket :: gen_tcp:socket(),
Message :: string()
| binary()
| {json, zj:value()}
| ws_msg(),
Result :: ok
| {error, Reason},
Reason :: any().
% @doc
% equivalent to send/2 but assumes iolists/strings are meant to be `text`, and
% naked binaries are meant to be `binary`
%
% lists are assumed to be unicode iolists and are converted to strings via
% unicode:characters_to_list
%
% json is encoded as text and sent as such
% @end
send_dwim(Socket, X) when is_list(X) -> send(Socket, {text, X});
send_dwim(Socket, X) when is_binary(X) -> send(Socket, {binary, X});
send_dwim(Socket, {json, X}) -> send(Socket, {text, zj:encode(X)});
send_dwim(Socket, {Type, Payload}) -> send(Socket, {Type, Payload}).
-spec send(Socket, Message) -> Result
when Socket :: gen_tcp:socket(),
+1 -12
View File
@@ -5,18 +5,7 @@
{prefix,"fd"}.
{desc,"Front End Web Dev in Erlang stuff"}.
{package_id,{"otpr","fewd",{0,2,0}}}.
{deps,[{"otpr","hakuzaru",{0,8,3}},
{"otpr","sophia",{9,0,0}},
{"otpr","gmserialization",{0,1,3}},
{"otpr","eblake2",{1,0,1}},
{"otpr","base58",{0,1,1}},
{"otpr","gmbytecode",{3,4,1}},
{"otpr","base58",{0,1,1}},
{"otpr","eblake2",{1,0,1}},
{"otpr","ec_utils",{1,0,0}},
{"otpr","zj",{1,1,2}},
{"otpr","getopt",{1,0,2}},
{"otpr","qr",{0,1,0}}]}.
{deps,[{"otpr","zj",{1,1,2}}]}.
{key_name,none}.
{a_email,"peterharpending@qpq.swiss"}.
{c_email,"peterharpending@qpq.swiss"}.