Skip to content

Commit

Permalink
Ensure PoS and PoW Nimiq SDKs can live together on the same page
Browse files Browse the repository at this point in the history
To do this, we load the PoW SDK first, as it automatically sets a global `Nimiq` variable. Then in the loader for the PoS SDK, we check for that variable and store it as `NimiqPoW`, overwriting `Nimiq` with the PoS SDK.
  • Loading branch information
sisou committed Nov 17, 2024
1 parent 98b7658 commit db40fda
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
12 changes: 9 additions & 3 deletions src/lib/AlbatrossWasm.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
// @ts-expect-error Cannot read types
import * as Nimiq from '../../node_modules/@nimiq/albatross-wasm/web/index.js';
// @ts-expect-error window.Nimiq is not defined (because we already define Albatross as a global)
window.Nimiq = Nimiq;
import * as NimiqPoS from '../../node_modules/@nimiq/albatross-wasm/web/index.js';

// Move any defined `Nimiq` global to `NimiqPoW`
// @ts-expect-error window.NimiqPoW is not defined
window.NimiqPoW = window.Nimiq;

// Overwrite the global `Nimiq` with the PoS version
// @ts-expect-error window.Nimiq is not defined
window.Nimiq = NimiqPoS;
2 changes: 1 addition & 1 deletion src/request/export/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
font-src 'self';">
<title>Nimiq Keyguard</title>

<script defer manual type="module" src="../../lib/AlbatrossWasm.mjs"></script>
<script defer manual src="../../../node_modules/@nimiq/core-web/web-offline.js"></script>
<script defer manual type="module" src="../../lib/AlbatrossWasm.mjs"></script>
<script defer manual src="../../lib/Constants.js"></script>
<script defer manual src="../../config/config.local.js"></script>

Expand Down
2 changes: 1 addition & 1 deletion src/request/import/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
font-src 'self';">
<title>Nimiq Keyguard</title>

<script defer manual type="module" src="../../lib/AlbatrossWasm.mjs"></script>
<script defer manual src="../../../node_modules/@nimiq/core-web/web-offline.js"></script>
<script defer manual type="module" src="../../lib/AlbatrossWasm.mjs"></script>
<script defer manual src="../../lib/Constants.js"></script>
<script defer manual src="../../config/config.local.js"></script>

Expand Down
2 changes: 1 addition & 1 deletion src/request/remove-key/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
font-src 'self';">
<title>Nimiq Keyguard</title>

<script defer manual type="module" src="../../lib/AlbatrossWasm.mjs"></script>
<script defer manual src="../../../node_modules/@nimiq/core-web/web-offline.js"></script>
<script defer manual type="module" src="../../lib/AlbatrossWasm.mjs"></script>
<script defer manual src="../../lib/Constants.js"></script>
<script defer manual src="../../config/config.local.js"></script>

Expand Down
14 changes: 10 additions & 4 deletions tools/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@ CSS_TOPLEVEL_BUNDLE=$(add_hash_to_file_name dist/request/$CSS_TOPLEVEL_BUNDLE)

CORE_LIB_HASH=$(make_file_hash node_modules/@nimiq/core-web/web-offline.js)

# copy Albatross loader, replace import path and calculate the integrity hash
cp -v src/lib/AlbatrossWasm.mjs dist/lib/
inplace_sed 's/\.\.\/\.\.\/node_modules\/@nimiq\/albatross-wasm/\.\.\/assets\/albatross/' dist/lib/AlbatrossWasm.mjs
ALBATROSS_LOADER_HASH=$(make_file_hash dist/lib/AlbatrossWasm.mjs)

# process index.html scripts and links for each request
output "🛠️ Building request index.html files"
for DIR in src/request/*/ ; do
Expand All @@ -277,6 +282,11 @@ for DIR in src/request/*/ ; do
print space[1] "<script defer src=\"/assets/nimiq/web-offline.js\" integrity=\"sha256-'${CORE_LIB_HASH}'\"></script>"
next
}
/<script.*AlbatrossWasm\.mjs/ {
split($0, space, "<") # Preserve intendation.
print space[1] "<script defer src=\"/lib/AlbatrossWasm.mjs\" type=\"module\" integrity=\"sha256-'${ALBATROSS_LOADER_HASH}'\"></script>"
next
}
/<script.*type="module"/ {
print
next
Expand Down Expand Up @@ -358,10 +368,6 @@ cp -v node_modules/@nimiq/style/nimiq-style.icons.svg dist/assets/
# copy service worker (which has to be in root to work)
cp -v src/service-worker/ServiceWorker.js dist

# copy Albatross loader and replace import path
cp -v src/lib/AlbatrossWasm.mjs dist/lib/
sed -i 's/\.\.\/\.\.\/node_modules\/@nimiq\/albatross-wasm/\.\.\/assets\/albatross/' dist/lib/AlbatrossWasm.mjs

# copy Nimiq files
output "‼️ Copying Nimiq files"
cp -v node_modules/@nimiq/core-web/web-offline.js \
Expand Down

0 comments on commit db40fda

Please sign in to comment.