Skip to content

Commit

Permalink
Replace monic with epic, and implement web manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
ninegua committed Jun 13, 2021
1 parent 2b73b4c commit 5632db1
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 76 deletions.
30 changes: 18 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ SRC=src/index.js src/bare-agent.js src/index.html
DID_SRC=src/ledger.did src/governance.did
NIX_SRC=default.nix ic-qr-scanner.nix node-env.nix node-packages.nix shell.nix
NPM_SRC=webpack.config.js src/simple.min.css
ALL_SRC=$(SRC) $(DID_SRC) $(NIX_SRC) $(NPM_SRC)
OTHER_SRC=src/manifest.json src/favicon.ico src/logo.png
ALL_SRC=$(SRC) $(DID_SRC) $(NIX_SRC) $(NPM_SRC) $(OTHER_SRC)

all: fmt build

Expand All @@ -18,13 +19,21 @@ dist/index.html dist/main.bundle.js &: $(SRC) $(DID_SRC) $(NPM_SRC)
fmt: $(SRC) webpack.config.js
prettier -w $(SRC) webpack.config.js

dist/monic.wasm: dist/index.html
cd dist && sh ../monic.sh index.html
sha256sum dist/index.html dist/monic.wasm
dist/app.o: dist/app.c
clang --target=wasm32 -c -O3 $^ -o $@

install: dist/monic.wasm
install -D dist/monic.did ${out}/bin/ic-qr-scanner.did
install -D dist/monic.wasm ${out}/bin/ic-qr-scanner.wasm
dist/app.wasm: dist/app.o
wasm-ld --no-entry --export-dynamic --allow-undefined $^ -o $@

dist/app.did:
touch $@

dist/app.c: dist/index.html $(OTHER_SRC)
echo -e "/,./dist/index.html\n/manifest.json,./src/manifest.json\n/logo.png,./src/logo.png\n/favicon.ico,./src/favicon.ico" | sh epic.sh > $@

install: dist/app.wasm dist/app.did
install -D dist/app.did ${out}/bin/ic-qr-scanner.did
install -D dist/app.wasm ${out}/bin/ic-qr-scanner.wasm

dist/canister_ids.json: canister_ids.json
cd dist && ln -s ../canister_ids.json .
Expand All @@ -33,15 +42,12 @@ result : $(ALL_SRC)
nix-build ic-qr-scanner.nix

dfx.json:
echo '{"canisters":{"monic":{"type":"custom","candid":"result/bin/ic-qr-scanner.did","wasm":"result/bin/ic-qr-scanner.wasm","build":""}}}' > dfx.json
echo '{"canisters":{"app":{"type":"custom","candid":"result/bin/ic-qr-scanner.did","wasm":"result/bin/ic-qr-scanner.wasm","build":""}}}' > $@

release: result dfx.json
dfx deploy --network=ic

clean:
rm -rf dist result dfx.json .dfx

nix-build:
cp `nix-build ic-qr-scanner.nix`/bin/* .

.PHONY: all fmt build release install clean nix-build
.PHONY: all fmt build release install clean
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Module hash: 0xde6c0dfa550b39f0d8ec19c0eac488fdf9201530a61f9bbfe97554eaad0c357c
```

Acknowledgement:
* Single page deployment on IC using the minimalistic tool [monic], courtesy of [blynn].
* Single page deployment on IC using the minimalistic tool [epic], courtesy of [blynn].
* QR scanning is from [zbar.wasm].
* CSS is from [Simple.css].

Expand All @@ -47,7 +47,7 @@ Acknowledgement:
[Add to Home screen]: https://developer.mozilla.org/en-US/docs/Web/Progressive_web_apps/Add_to_home_screen
[zbar.wasm]: https://github.com/samsam2310/zbar.wasm
[Simple.css]: https://simplecss.org
[monic]: https://fmfd2-7qaaa-aaaae-aaapq-cai.raw.ic0.app
[epic]: https://fxa77-fiaaa-aaaae-aaana-cai.raw.ic0.app/epic
[blynn]: https://crypto.stanford.edu/~blynn

To learn more about developing apps on the Internet Computer, see the following documentation available online:
Expand Down
4 changes: 2 additions & 2 deletions canister_ids.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"monic": {
"app": {
"ic": "p5deo-6aaaa-aaaab-aaaxq-cai"
}
}
}
104 changes: 104 additions & 0 deletions epic.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#!/usr/bin/env bash
# This tool is taken from https://fxa77-fiaaa-aaaae-aaana-cai.raw.ic0.app/epic

set -e
n=0
while read x; do
path=$(echo "$x" | cut -d, -f1)
file=$(echo "$x" | cut -d, -f2)
echo "char file$n[]={"
cat $file | xxd -p | sed 's/../0x&,/g'
echo "};"
tab[$n]="$path,file$n,sizeof(file$n)"
n=$((n+1))
done
cat << EOF
typedef unsigned size_t;
int strcmp(const char*s1,const char*s2){
if (*s1==*s2) {
return *s1 ? strcmp(s1+1,s2+1) : 0;
} else return 1;
}
EOF
(
echo "struct pathdesc {char*name;char*contents;unsigned contents_size;};";
echo "%%";
printf '%s\n' "${tab[@]}";
) | gperf -t
cat << EOF
#define WASM_IMPORT(m,n) __attribute__((import_module(m))) __attribute__((import_name(n)));
#define WASM_EXPORT(n) asm(n) __attribute__((visibility("default")))
void arg_data_copy(void *, int, int) WASM_IMPORT("ic0", "msg_arg_data_copy");
void reply_data_append(void *, int) WASM_IMPORT("ic0", "msg_reply_data_append");
void reply(void) WASM_IMPORT("ic0", "msg_reply");
typedef unsigned u;
u arg_i;
u byte() {
char buf[1];
arg_data_copy(buf, arg_i++, 1);
return *buf;
}
u leb128() {
u n = 0, base = 1;
for(;;) {
u d = byte();
if (d <= 127) return base*d + n;
n += base*(d - 128);
base *= 128;
}
}
void scan_type() {
u n;
switch(byte()) {
case 0x6e: // Opt
case 0x6d: // Vec.
scan_type();
break;
case 0x6c: // Rec.
case 0x6b: // Variant.
n = leb128();
while(n--) leb128(), scan_type();
break;
}
}
char path[16384];
void go() WASM_EXPORT("canister_query http_request");
void go() {
u n = 4;
while(n--) byte(); // "DIDL"
n = leb128(); // Type table.
while(n--) scan_type();
leb128(); // Arg count. Expect 1.
byte(); // Expect type #0, a record.
char hdr[] = "DIDL\x03\x6c\x03\xa2\xf5\xed\x88\x04\x01\xc6\xa4\xa1\x98\x06\x02\x9a\xa1\xb2\xf9\x0c\x7a\x6d\x7b\x6d\x7f\x01\x00";
char ftr[] = "\x00\xc8\x00";
char not_found[] = "\x03" "404\x00\x94\x01";
reply_data_append(hdr, sizeof(hdr) - 1);
char buf[5];
u path_size = leb128();
arg_data_copy(path, arg_i, path_size);
for (n = 0; n < path_size; n++) if (path[n] == '?') { path_size = n; break; }
path[path_size] = 0;
struct pathdesc *p = in_word_set(path, path_size);
if (!p) {
reply_data_append(not_found, sizeof(not_found) - 1);
} else {
n = p->contents_size;
int i;
for (i = 0; i < 4; i++) {
buf[i] = (n & 127) | 128;
n >>= 7;
}
buf[i] = n;
reply_data_append(buf, 5);
reply_data_append(p->contents, p->contents_size);
reply_data_append(ftr, sizeof(ftr) - 1);
}
reply();
}
EOF
1 change: 1 addition & 0 deletions ic-qr-scanner.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ in stdenv.mkDerivation {
nativeBuildInputs = [
nodejs
gnumake
gperf
nodePackages.prettier
nodePackages.webpack
xxd
Expand Down
59 changes: 0 additions & 59 deletions monic.sh

This file was deleted.

1 change: 1 addition & 0 deletions shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ stdenv.mkDerivation {
nativeBuildInputs = [
nodejs
gnumake
gperf
nodePackages.prettier
nodePackages.node2nix
pkgsCross.wasi32.buildPackages.clang_10
Expand Down
Binary file added src/favicon.ico
Binary file not shown.
1 change: 1 addition & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<title><%= htmlWebpackPlugin.options.title %></title>
<link rel="manifest" href="manifest.json" />
<style>
#scan,
#send,
Expand Down
Binary file added src/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions src/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"background_color": "white",
"description": "QR Scanner for Internet Computer Transactions",
"display": "standalone",
"icons": [
{
"src": "/logo.png",
"sizes": "360x360",
"type": "image/png"
}
],
"name": "IC QR Scanner",
"short_name": "IC QR",
"start_url": "/"
}
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = {
}),
new HtmlWebpackPlugin({
inject: true,
title: "Scan and send transactions",
title: "IC QR Scanner",
myPageHeader: "Scan & Send",
template: "./src/index.html",
filename: "./index.html",
Expand Down

0 comments on commit 5632db1

Please sign in to comment.