Skip to content

Commit

Permalink
Add hash to albatross-client file for cache-bust
Browse files Browse the repository at this point in the history
  • Loading branch information
mraveux committed Nov 29, 2024
1 parent 133e7a5 commit 36d1aea
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 11 deletions.
2 changes: 1 addition & 1 deletion demos/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ <h2>Result</h2>
crossorigin="anonymous"></script>

<script type="module">
import init, * as Nimiq from '/albatross-client/web/index.js';
import init, * as Nimiq from '/albatross-client/web/index.js?v=<%= htmlWebpackPlugin.options.albatrossClientHash %>';

/** @type {Promise<Nimiq> | undefined} */
let initPromise;
Expand Down
2 changes: 1 addition & 1 deletion public/cashlink.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<link rel="icon" href="<%= htmlWebpackPlugin.options.domain %>/favicon.ico">

<script type="module">
import init, * as Nimiq from '/albatross-client/web/index.js';
import init, * as Nimiq from '/albatross-client/web/index.js?v=<%= htmlWebpackPlugin.options.albatrossClientHash %>';

/** @type {Promise<Nimiq> | undefined} */
let initPromise;
Expand Down
2 changes: 1 addition & 1 deletion public/export.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</style>

<script type="module">
import init, * as Nimiq from '/albatross-client/web/index.js';
import init, * as Nimiq from '/albatross-client/web/index.js?v=<%= htmlWebpackPlugin.options.albatrossClientHash %>';

/** @type {Promise<Nimiq> | undefined} */
let initPromise;
Expand Down
2 changes: 1 addition & 1 deletion public/iframe.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta name="robots" content="noindex">
<title>Nimiq Hub IFrame</title>
<script type="module">
import init, * as Nimiq from '/albatross-client/web/index.js';
import init, * as Nimiq from '/albatross-client/web/index.js?v=<%= htmlWebpackPlugin.options.albatrossClientHash %>';

/** @type {Promise<Nimiq> | undefined} */
let initPromise;
Expand Down
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<link rel="icon" href="<%= htmlWebpackPlugin.options.domain %>/favicon.ico">

<script type="module">
import init, * as Nimiq from '/albatross-client/web/index.js';
import init, * as Nimiq from '/albatross-client/web/index.js?v=<%= htmlWebpackPlugin.options.albatrossClientHash %>';

/** @type {Promise<Nimiq> | undefined} */
let initPromise;
Expand Down
6 changes: 3 additions & 3 deletions src/i18n/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ msgid "Connect your Ledger hardware wallet."
msgstr ""

#: src/views/Migrate.vue:143
#: src/views/Migrate.vue:285
#: src/views/Migrate.vue:283
msgid "Connecting to Keyguard..."
msgstr ""

Expand Down Expand Up @@ -1189,7 +1189,7 @@ msgstr ""
msgid "Unsupported Request"
msgstr ""

#: src/components/CheckoutCardNimiq.vue:329
#: src/components/CheckoutCardNimiq.vue:327
msgid "Updating balances"
msgstr ""

Expand All @@ -1198,7 +1198,7 @@ msgid "Updating Keyguard..."
msgstr ""

#: src/views/Migrate.vue:142
#: src/views/Migrate.vue:284
#: src/views/Migrate.vue:282
msgid "Updating your Accounts"
msgstr ""

Expand Down
27 changes: 24 additions & 3 deletions src/iframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,29 @@ import { deriveAddressesFromXPub } from './lib/bitcoin/BitcoinUtils';
import { detectLanguage, setLanguage } from './i18n/i18n-setup';

class IFrameApi {
public static run() {
private static async waitForLoadAlbatross(): Promise<void> {
// Wait for loadAlbatross to be available, with timeout
const maxWaitTime = 10000; // 10 seconds
const startTime = Date.now();

while (!window.loadAlbatross) {
if (Date.now() - startTime > maxWaitTime) {
throw new Error('Timeout waiting for loadAlbatross to be available');
}
await new Promise(resolve => setTimeout(resolve, 50)); // Wait 50ms before checking again
}
}

public static async run() {
// Wait for loadAlbatross to be available
try {
await IFrameApi.waitForLoadAlbatross();
window.Nimiq = await window.loadAlbatross();
} catch (e) {
console.error('Failed to load Albatross:', e);
throw e;
}

const rpcServer = new RpcServer(Config.privilegedOrigins);

// Register handlers
Expand All @@ -38,8 +60,7 @@ class IFrameApi {
}

public static async list(): Promise<Account[]> {
window.Nimiq = await window.loadAlbatross();

// Albatross is already loaded in run(), no need to load it again
let wallets: WalletInfoEntry[];
if (BrowserDetection.isIOS() || BrowserDetection.isSafari()) {
/*
Expand Down
9 changes: 9 additions & 0 deletions vue.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ const bitcoinJsIntegrityHash = `sha256-${createHash('sha256')
.update(fs.readFileSync(path.join(__dirname, 'public/bitcoin/BitcoinJS.min.js')))
.digest('base64')}`;

const albatrossClientHash = `sha256-${createHash('sha256')
.update(fs.readFileSync(path.join(__dirname, 'node_modules/@nimiq/albatross-wasm/web/index.js')))
.digest('base64')}`;

// Accessible within client code via process.env.VUE_APP_BITCOIN_JS_INTEGRITY_HASH,
// see https://cli.vuejs.org/guide/mode-and-env.html#using-env-variables-in-client-side-code
process.env.VUE_APP_BITCOIN_JS_INTEGRITY_HASH = bitcoinJsIntegrityHash;
Expand Down Expand Up @@ -129,6 +133,7 @@ const pages = {
// insert browser warning html template
browserWarningTemplate,
browserWarningIntegrityHash,
albatrossClientHash,
domain,
cdnDomain,
// output as dist/index.html
Expand All @@ -142,6 +147,7 @@ const pages = {
entry: 'src/iframe.ts',
// the source template
template: 'public/iframe.html',
albatrossClientHash,
// output as dist/iframe.html
filename: 'iframe.html',
// chunks to include on this page, by default includes
Expand All @@ -156,6 +162,7 @@ const pages = {
// insert browser warning html template
browserWarningTemplate,
browserWarningIntegrityHash,
albatrossClientHash,
domain,
cdnDomain,
// output as dist/cashlink/index.html
Expand All @@ -169,6 +176,7 @@ const pages = {
entry: 'src/export.ts',
// the source template
template: 'public/export.html',
albatrossClientHash,
cdnDomain,
// output as dist/iframe.html
filename: 'export.html',
Expand All @@ -183,6 +191,7 @@ if (buildName === 'local' || buildName === 'testnet') {
template: 'demos/index.html',
cdnDomain,
bitcoinJsIntegrityHash,
albatrossClientHash,
// output as dist/demos.html
filename: 'demos.html',
// chunks to include on this page, by default includes
Expand Down

0 comments on commit 36d1aea

Please sign in to comment.