Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sisou committed Nov 17, 2024
1 parent 59eb407 commit 3c566d7
Show file tree
Hide file tree
Showing 15 changed files with 359 additions and 395 deletions.
87 changes: 0 additions & 87 deletions karma.conf.coverage.js

This file was deleted.

4 changes: 3 additions & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ module.exports = function (/** @type {any} */ config) {

// list of files / patterns to load in the browser
files: [
{'pattern': 'node_modules/@nimiq/albatross-wasm/**/*', included: false},
{pattern: 'src/lib/AlbatrossWasm.mjs', type: 'module'},
{pattern: 'node_modules/@nimiq/albatross-wasm/**/*', included: false},
'src/lib/Observable.js',
'src/lib/*.js', // Force load of lib files before components and common.js
'src/request/TopLevelApi.js', // Force load of TopLevelApi before BitcoinEnabledTopLevelApi
'src/lib/bitcoin/*.js',
Expand Down
14 changes: 14 additions & 0 deletions src/lib/Key.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,20 @@ class Key {
: Nimiq.PublicKey.derive(this._secret).toAddress().serialize();
return Key.deriveHash(input);
}

// eslint-disable-next-line valid-jsdoc
/**
* @param {unknown} other
* @returns {other is Key}
*/
equals(other) {
return other instanceof Key
&& this.id === other.id
&& this.type === other.type
&& this.hasPin === other.hasPin
&& this.secret.equals(/** @type {Nimiq.PrivateKey} */ (other.secret))
&& this.defaultAddress.equals(other.defaultAddress);
}
}

Key.PIN_LENGTH = 6;
15 changes: 15 additions & 0 deletions src/lib/KeyInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,19 @@ class KeyInfo {
static fromObject(obj, encrypted, defaultAddress) {
return new KeyInfo(obj.id, obj.type, encrypted, obj.hasPin, defaultAddress);
}

// eslint-disable-next-line valid-jsdoc
/**
* @param {unknown} other
* @returns {other is KeyInfo}
*/
equals(other) {
return other instanceof KeyInfo
&& this.id === other.id
&& this.type === other.type
&& this.hasPin === other.hasPin
&& this.encrypted === other.encrypted
&& this.useLegacyStore === other.useLegacyStore
&& this.defaultAddress.equals(other.defaultAddress);
}
}
12 changes: 6 additions & 6 deletions src/lib/swap/HtlcUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ class HtlcUtils { // eslint-disable-line no-unused-vars
static decodeNimHtlcData(data) {
const error = new Errors.InvalidRequestError('Invalid NIM HTLC data');

if (!data || !(data instanceof Uint8Array) || data.length !== 78) throw error;
if (!data || !(data instanceof Uint8Array) || data.length !== 82) throw error;

const buf = new Nimiq.SerialBuffer(data);

const sender = new Nimiq.Address(buf).toUserFriendlyAddress();
const recipient = new Nimiq.Address(buf).toUserFriendlyAddress();
const sender = new Nimiq.Address(buf.read(20)).toUserFriendlyAddress();
const recipient = new Nimiq.Address(buf.read(20)).toUserFriendlyAddress();
const hashAlgorithm = buf.readUint8();
const hashRoot = Nimiq.BufferUtils.toHex(buf);
const hashRoot = Nimiq.BufferUtils.toHex(buf.read(32));
const hashCount = buf.readUint8();
const timeout = buf.readUint32();
const timeout = buf.readUint64();

if (hashAlgorithm !== 3 /* Nimiq.Hash.Algorithm.SHA256 */) throw error;
if (hashCount !== 1) throw error;
Expand All @@ -30,7 +30,7 @@ class HtlcUtils { // eslint-disable-line no-unused-vars
refundAddress: sender,
redeemAddress: recipient,
hash: hashRoot,
timeoutBlockHeight: timeout,
timeoutTimestamp: timeout / 1e3, // Convert to seconds to match Bitcoin, Polygon and OASIS
};
}

Expand Down
1 change: 0 additions & 1 deletion src/request/TopLevelApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
/* global KeyStore */
/* global CookieJar */
/* global I18n */
/* global Nimiq */
/* global RequestParser */
/* global NoRequestErrorPage */

Expand Down
40 changes: 10 additions & 30 deletions src/request/swap-iframe/SwapIFrameApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,6 @@ class SwapIFrameApi extends BitcoinRequestParserMixin(RequestParser) { // eslint
throw new Errors.InvalidRequestError('NIM HTLC refund address must be same as sender');
}

// Check that validityStartHeight is before HTLC timeout
if (storedRequest.fund.transaction.validityStartHeight >= htlcDetails.timeoutBlockHeight) {
throw new Errors.InvalidRequestError(
'Fund validityStartHeight must be lower than HTLC timeout block height',
);
}

fund = {
type: 'NIM',
htlcDetails,
Expand All @@ -182,13 +175,6 @@ class SwapIFrameApi extends BitcoinRequestParserMixin(RequestParser) { // eslint
throw new Errors.InvalidRequestError('NIM HTLC redeem address must be same as recipient');
}

// Check that validityStartHeight is before HTLC timeout
if (storedRequest.redeem.transaction.validityStartHeight >= htlcDetails.timeoutBlockHeight) {
throw new Errors.InvalidRequestError(
'Redeem validityStartHeight must be lower than HTLC timeout block height',
);
}

redeem = {
type: 'NIM',
htlcDetails,
Expand Down Expand Up @@ -395,22 +381,16 @@ class SwapIFrameApi extends BitcoinRequestParserMixin(RequestParser) { // eslint
// Validate timeouts of the two contracts
// The redeem HTLC must have a later timeout than the funding HTLC.
const fundingTimeout = 'htlcDetails' in fund
? 'timeoutTimestamp' in fund.htlcDetails
? fund.htlcDetails.timeoutTimestamp
: undefined
? fund.htlcDetails.timeoutTimestamp
: fund.description.args.timeout.toNumber();
const redeemingTimeout = 'timeoutTimestamp' in redeem.htlcDetails
? redeem.htlcDetails.timeoutTimestamp
: undefined;
if (fundingTimeout && redeemingTimeout) {
const diff = redeemingTimeout - fundingTimeout;

// Validate that the difference is at least 15 minutes
if (diff < 15 * 60) {
throw new Errors.InvalidRequestError(
'HTLC redeem timeout must be 15 min or more after the funding timeout',
);
}
const redeemingTimeout = redeem.htlcDetails.timeoutTimestamp;
const diff = redeemingTimeout - fundingTimeout;

// Validate that the difference is at least 15 minutes
if (diff < 15 * 60) {
throw new Errors.InvalidRequestError(
'HTLC redeem timeout must be 15 min or more after the funding timeout',
);
}

/** @type {Parsed<KeyguardRequest.SignSwapTransactionsRequest>} */
Expand Down Expand Up @@ -466,7 +446,7 @@ class SwapIFrameApi extends BitcoinRequestParserMixin(RequestParser) { // eslint
transaction.sender, Nimiq.AccountType.Basic, new Uint8Array(0),
transaction.value - fee, fee,
0 /* Nimiq.Transaction.Flag.NONE */,
parsedRequest.fund.htlcDetails.timeoutBlockHeight,
parsedRequest.fund.htlcDetails.timeoutTimestamp,
CONFIG.NIMIQ_NETWORK_ID,
);

Expand Down
Loading

0 comments on commit 3c566d7

Please sign in to comment.