Skip to content

Commit 2c14237

Browse files
Compute asset hashes in post_upgrade (#523)
* Compute asset hashes in post_upgrade This removes the build-time hash computation and make it happen when `for_each_asset` is called, effectively in `post_upgrade` (and `init`). The interface is still a bit cumbersome (e.g. `for_each_asset` is still callback based) but this makes sure that `main.rs` is not changed; the focus here is the asset hashes. This is the first step in removing `CANISTER_ID` from the build process; we want to inject it in the assets directly, and this will change `main.rs` and `for_each_asset`. I've tested this locally and assets (at least `index.js`) have the same ic-certificate header as they have on `main`. * Comment * Remove sha2 from build dep * Fix typo * Grab index.html once * Simplify hash_content Co-authored-by: Frederik Rothenberger <[email protected]>
1 parent 8b6dfcd commit 2c14237

File tree

3 files changed

+87
-146
lines changed

3 files changed

+87
-146
lines changed

src/internet_identity/Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ captcha = { git = 'https://github.com/nmattia/captcha', rev = '4751b6fa4e56229c2
2626
hex-literal = "0.2.1"
2727
rand = "0.8.3"
2828

29-
[build-dependencies]
30-
sha2 = "0.9.1"
31-
3229
[features]
3330
# the dummy_captcha feature which ensures the captcha string is always "a"
3431
# (needed for tests)

src/internet_identity/build.rs

Lines changed: 0 additions & 142 deletions
This file was deleted.

src/internet_identity/src/assets.rs

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,87 @@
1-
include!(concat!(env!("OUT_DIR"), "/assets.rs"));
1+
// All assets
2+
//
3+
// This file describes which assets are used and how (content, content type and content encoding).
4+
5+
use sha2::Digest;
6+
7+
#[derive(Debug, PartialEq, Eq)]
8+
pub enum ContentEncoding {
9+
Identity,
10+
GZip,
11+
}
12+
13+
#[derive(Debug, PartialEq, Eq)]
14+
pub enum ContentType {
15+
HTML,
16+
JS,
17+
ICO,
18+
WEBP,
19+
SVG
20+
}
21+
22+
pub fn for_each_asset(mut f: impl FnMut(&'static str, ContentEncoding, ContentType, &'static [u8], &[u8; 32])) {
23+
24+
let index_html = include_bytes!("../../../dist/index.html");
25+
26+
let assets: [ (&str, &[u8], ContentEncoding, ContentType); 8] = [
27+
("/",
28+
index_html,
29+
ContentEncoding::Identity,
30+
ContentType::HTML,
31+
),
32+
// The FAQ and about pages are the same webapp, but the webapp routes to the correct page
33+
(
34+
"/faq",
35+
index_html,
36+
ContentEncoding::Identity,
37+
ContentType::HTML,
38+
),
39+
(
40+
"/about",
41+
index_html,
42+
ContentEncoding::Identity,
43+
ContentType::HTML,
44+
),
45+
(
46+
"/index.html",
47+
index_html,
48+
ContentEncoding::Identity,
49+
ContentType::HTML,
50+
),
51+
(
52+
"/index.js",
53+
include_bytes!("../../../dist/index.js.gz"),
54+
ContentEncoding::GZip,
55+
ContentType::JS,
56+
),
57+
(
58+
"/loader.webp",
59+
include_bytes!("../../../dist/loader.webp"),
60+
ContentEncoding::Identity,
61+
ContentType::WEBP,
62+
),
63+
(
64+
"/favicon.ico",
65+
include_bytes!("../../../dist/favicon.ico"),
66+
ContentEncoding::Identity,
67+
ContentType::ICO,
68+
),
69+
(
70+
"/ic-badge.svg",
71+
include_bytes!("../../../dist/ic-badge.svg"),
72+
ContentEncoding::Identity,
73+
ContentType::SVG,
74+
),
75+
];
76+
77+
for (name, content, encoding, content_type) in assets {
78+
let hash = hash_content(content);
79+
f(name, encoding, content_type, content, &hash);
80+
}
81+
}
82+
83+
84+
// Hash the content of an asset in an `ic_certified_map` friendly way
85+
fn hash_content(bytes: &[u8]) -> [u8; 32] {
86+
sha2::Sha256::digest(bytes).into()
87+
}

0 commit comments

Comments
 (0)