Skip to content

Commit

Permalink
Apply fixes from the Typescript update (PR #467)
Browse files Browse the repository at this point in the history
  • Loading branch information
sisou committed Jan 15, 2025
1 parent 76ce1a8 commit 87fa2c3
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 61 deletions.
3 changes: 1 addition & 2 deletions src/lib/rsa/sandboxed/RSAKeysIframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
* @typedef {GenerateKeyCommand} Command
*/

/** @type {HTMLSpanElement} */
const $loading = (document.querySelector('#loading'));
const $loading = /** @type {HTMLSpanElement} */ (document.querySelector('#loading'));

/**
* @param {any} key
Expand Down
25 changes: 10 additions & 15 deletions src/request/connect/Connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,16 @@ class Connect {
* @param {reject} reject
*/
constructor(request, resolve, reject) {
/** @type {HTMLDivElement} */
const $page = (document.getElementById(Connect.Pages.CONNECT_ACCOUNT));
const $page = /** @type {HTMLDivElement} */ (document.getElementById(Connect.Pages.CONNECT_ACCOUNT));

/** @type {HTMLSpanElement} */
const $appName = ($page.querySelector('.app-name'));
const $appName = /** @type {HTMLSpanElement} */ ($page.querySelector('.app-name'));
$appName.textContent = request.appName;

/** @type {HTMLImageElement} */
const $appIcon = ($page.querySelector('.app-icon'));
const $appIcon = /** @type {HTMLImageElement} */ ($page.querySelector('.app-icon'));
$appIcon.src = request.appLogoUrl.href;
$appIcon.alt = `${request.appName} logo`;

/** @type {HTMLDivElement} */
const $loginFileIcon = ($page.querySelector('.login-file-account-icon'));
const $loginFileIcon = /** @type {HTMLDivElement} */ ($page.querySelector('.login-file-account-icon'));
if (request.keyInfo.type === Nimiq.Secret.Type.ENTROPY) {
// eslint-disable-next-line no-new
new LoginFileAccountIcon(request.keyInfo.defaultAddress.toUserFriendlyAddress(), $loginFileIcon);
Expand All @@ -44,12 +40,10 @@ class Connect {
new Identicon(request.keyInfo.defaultAddress.toUserFriendlyAddress(), $loginFileIcon);
}

/** @type {HTMLButtonElement} */
const $button = ($page.querySelector('.nq-button.continue'));
const $button = /** @type {HTMLButtonElement} */ ($page.querySelector('.nq-button.continue'));

// Set up password box
/** @type {HTMLFormElement} */
const $passwordBox = (document.querySelector('#password-box'));
const $passwordBox = /** @type {HTMLFormElement} */ (document.querySelector('#password-box'));
this._passwordBox = new PasswordBox($passwordBox, {
hideInput: !request.keyInfo.encrypted,
buttonI18nTag: 'passwordbox-connect-account',
Expand Down Expand Up @@ -84,13 +78,14 @@ class Connect {
let key = null;
try {
key = await KeyStore.instance.get(request.keyInfo.id, passwordBuf);
} catch (e) {
if (e.message === 'Invalid key') {
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
if (errorMessage === 'Invalid key') {
TopLevelApi.setLoading(false);
this._passwordBox.onPasswordIncorrect();
return;
}
reject(new Errors.CoreError(e));
reject(new Errors.CoreError(errorMessage));
return;
}

Expand Down
64 changes: 27 additions & 37 deletions src/request/sign-multisig-transaction/SignMultisigTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,15 @@ class SignMultisigTransaction {
*/
constructor(request, resolve, reject) {
this._request = request;
/** @type {HTMLElement} */
this.$el = (document.getElementById(SignMultisigTransaction.Pages.CONFIRM_TRANSACTION));
this.$el = /** @type {HTMLElement} */ (
document.getElementById(SignMultisigTransaction.Pages.CONFIRM_TRANSACTION));
this.$el.classList.add(request.layout);

const transaction = request.transaction;

/** @type {HTMLElement} */
this.$accountDetails = (this.$el.querySelector('#account-details'));
this.$accountDetails = /** @type {HTMLElement} */ (this.$el.querySelector('#account-details'));

/** @type {HTMLLinkElement} */
const $sender = (this.$el.querySelector('.accounts .sender'));
const $sender = /** @type {HTMLLinkElement} */ (this.$el.querySelector('.accounts .sender'));
this._senderAddressInfo = new AddressInfo({
userFriendlyAddress: transaction.sender.toUserFriendlyAddress(),
label: request.senderLabel,
Expand All @@ -54,8 +52,7 @@ class SignMultisigTransaction {
this._openDetails(this._senderAddressInfo);
});

/** @type {HTMLLinkElement} */
const $recipient = (this.$el.querySelector('.accounts .recipient'));
const $recipient = /** @type {HTMLLinkElement} */ (this.$el.querySelector('.accounts .recipient'));
const recipientAddress = transaction.recipient.toUserFriendlyAddress();
/* eslint-disable no-nested-ternary */
// eslint-disable-next-line operator-linebreak
Expand Down Expand Up @@ -100,23 +97,19 @@ class SignMultisigTransaction {
// $paymentInfoLine.remove();
// }

/** @type {HTMLButtonElement} */
const $closeDetails = (this.$accountDetails.querySelector('#close-details'));

const $closeDetails = /** @type {HTMLButtonElement} */ (this.$accountDetails.querySelector('#close-details'));
$closeDetails.addEventListener('click', this._closeDetails.bind(this));

/** @type {HTMLDivElement} */
const $value = (this.$el.querySelector('#value'));
/** @type {HTMLDivElement} */
const $fee = (this.$el.querySelector('#fee'));
/** @type {HTMLDivElement} */
const $data = (this.$el.querySelector('#data'));
const $value = /** @type {HTMLDivElement} */ (this.$el.querySelector('#value'));
const $fee = /** @type {HTMLDivElement} */ (this.$el.querySelector('#fee'));
const $data = /** @type {HTMLDivElement} */ (this.$el.querySelector('#data'));

// Set value and fee.
$value.textContent = NumberFormatting.formatNumber(Nimiq.Policy.lunasToCoins(transaction.value));
if ($fee && transaction.fee > 0) {
$fee.textContent = NumberFormatting.formatNumber(Nimiq.Policy.lunasToCoins(transaction.fee));
/** @type {HTMLDivElement} */
const $feeSection = (this.$el.querySelector('.fee-section'));
const $feeSection = /** @type {HTMLDivElement} */ (this.$el.querySelector('.fee-section'));
$feeSection.classList.remove('display-none');
}

Expand All @@ -131,29 +124,24 @@ class SignMultisigTransaction {
/* } else */ if ($data && transaction.data.byteLength > 0) {
// Set transaction extra data.
$data.textContent = this._formatData(transaction);
/** @type {HTMLDivElement} */
const $dataSection = (this.$el.querySelector('.data-section'));
const $dataSection = /** @type {HTMLDivElement} */ (this.$el.querySelector('.data-section'));
$dataSection.classList.remove('display-none');
}

// Set up user and account names
/** @type {HTMLDivElement} */
const $nameSection = (this.$el.querySelector('.user-and-account-names'));
const $nameSection = /** @type {HTMLDivElement} */ (this.$el.querySelector('.user-and-account-names'));
if (request.multisigConfig.userName) {
$nameSection.classList.add('approving-as');
/** @type {HTMLDivElement} */
const $userName = ($nameSection.querySelector('.user-name'));
const $userName = /** @type {HTMLDivElement} */ ($nameSection.querySelector('.user-name'));
$userName.textContent = request.multisigConfig.userName;
} else {
$nameSection.classList.add('approving-with');
}
/** @type {HTMLDivElement} */
const $accountName = ($nameSection.querySelector('.account-name'));
const $accountName = /** @type {HTMLDivElement} */ ($nameSection.querySelector('.account-name'));
$accountName.textContent = request.keyLabel;

// Set up account icon
/** @type {HTMLDivElement} */
const $loginFileIcon = ($nameSection.querySelector('.login-file-account-icon'));
const $loginFileIcon = /** @type {HTMLDivElement} */ ($nameSection.querySelector('.login-file-account-icon'));
if (request.keyInfo.type === Nimiq.Secret.Type.ENTROPY) {
// eslint-disable-next-line no-new
new LoginFileAccountIcon(request.keyInfo.defaultAddress.toUserFriendlyAddress(), $loginFileIcon);
Expand All @@ -166,8 +154,7 @@ class SignMultisigTransaction {


// Set up password box.
/** @type {HTMLFormElement} */
const $passwordBox = (document.querySelector('#password-box'));
const $passwordBox = /** @type {HTMLFormElement} */ (document.querySelector('#password-box'));
this._passwordBox = new PasswordBox($passwordBox, {
hideInput: !request.keyInfo.encrypted,
buttonI18nTag: /* request.layout === SignMultisigTransactionApi.Layouts.CASHLINK
Expand Down Expand Up @@ -218,13 +205,14 @@ class SignMultisigTransaction {
let key = null;
try {
key = await KeyStore.instance.get(request.keyInfo.id, passwordBuf);
} catch (e) {
if (e.message === 'Invalid key') {
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
if (errorMessage === 'Invalid key') {
TopLevelApi.setLoading(false);
this._passwordBox.onPasswordIncorrect();
return;
}
reject(new Errors.CoreError(e));
reject(new Errors.CoreError(errorMessage));
return;
}
if (!key) {
Expand Down Expand Up @@ -256,15 +244,17 @@ class SignMultisigTransaction {
await window.crypto.subtle.decrypt({ name: 'RSA-OAEP' }, rsaKey, encrypted),
),
));
} catch (e) {
reject(new Errors.InvalidRequestError(`Cannot decrypt secrets: ${e.message}`));
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
reject(new Errors.InvalidRequestError(`Cannot decrypt secrets: ${errorMessage}`));
return;
}

try {
aggregatedSecret = await MultisigUtils.aggregateSecrets(secrets, request.multisigConfig.secret.bScalar);
} catch (e) {
reject(new Errors.InvalidRequestError(`Cannot aggregate secrets: ${e.message}`));
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
reject(new Errors.InvalidRequestError(`Cannot aggregate secrets: ${errorMessage}`));
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ class SignMultisigTransactionApi extends TopLevelApi {
publicKeys.push(new Nimiq.PublicKey(key));
}
} catch (error) {
throw new Errors.InvalidRequestError(`Invalid public keys: ${(error).message}`);
const errorMessage = error instanceof Error ? error.message : String(error);
throw new Errors.InvalidRequestError(`Invalid public keys: ${errorMessage}`);
}

const numberOfSigners = this.parsePositiveInteger(object.numberOfSigners, false, 'numberOfSigners');
Expand All @@ -112,7 +113,8 @@ class SignMultisigTransactionApi extends TopLevelApi {
signerPublicKeys.push(signerPublicKey);
}
} catch (error) {
throw new Errors.InvalidRequestError(`Invalid signer public keys: ${error.message}`);
const errorMessage = error instanceof Error ? error.message : String(error);
throw new Errors.InvalidRequestError(`Invalid signer public keys: ${errorMessage}`);
}

/** @type {MultisigConfig['secret']} */
Expand All @@ -126,7 +128,8 @@ class SignMultisigTransactionApi extends TopLevelApi {
aggregatedSecret: new Nimiq.RandomSecret(object.secret.aggregatedSecret),
};
} catch (error) {
throw new Errors.InvalidRequestError(`Invalid secret: ${error.message}`);
const errorMessage = error instanceof Error ? error.message : String(error);
throw new Errors.InvalidRequestError(`Invalid secret: ${errorMessage}`);
}
} else if ('encryptedSecrets' in object.secret && 'bScalar' in object.secret) {
// Not checking fixed length here, to stay flexible for future increases of the number of commitments
Expand Down Expand Up @@ -199,8 +202,9 @@ class SignMultisigTransactionApi extends TopLevelApi {
try {
aggregatedCommitment = new Nimiq.Commitment(object.aggregatedCommitment);
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
throw new Errors.InvalidRequestError(
`Invalid aggregated commitment: ${error.message}`,
`Invalid aggregated commitment: ${errorMessage}`,
);
}

Expand Down Expand Up @@ -277,10 +281,9 @@ class SignMultisigTransactionApi extends TopLevelApi {

/**
* @enum {KeyguardRequest.SignMultisigTransactionRequestLayout}
* @readonly
*/
SignMultisigTransactionApi.Layouts = {
SignMultisigTransactionApi.Layouts = Object.freeze({
STANDARD: /** @type {'standard'} */ ('standard'),
// CHECKOUT: /** @type {'checkout'} */ ('checkout'),
// CASHLINK: /** @type {'cashlink'} */ ('cashlink'),
};
});

0 comments on commit 87fa2c3

Please sign in to comment.