From 6f4fb69db9196fc4582ac9b2e2765878e1aa3c11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren?= Date: Fri, 14 Jun 2024 12:39:43 +0300 Subject: [PATCH] Use CRC-enabled OASIS API package, streamline re-exports --- package.json | 2 +- src/FiatAssetAdapter.ts | 37 +++++++++++++++++++------------------ src/IAssetAdapter.ts | 8 ++++---- src/SwapHandler.ts | 4 ++-- yarn.lock | 5 ++--- 5 files changed, 28 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index 40c05f7..b6f8a74 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "dependencies": { "@nimiq/core-web": "^1.5.8", "@nimiq/electrum-client": "https://github.com/nimiq/electrum-client#build", - "@nimiq/oasis-api": "^1.0.1", + "@nimiq/oasis-api": "https://github.com/nimiq/oasis-api-js#9d63fac0ab1364e4e1494334ee2d74f3d6f96b35", "ethers": "^5.7.2", "promise.prototype.finally": "^3.1.3" }, diff --git a/src/FiatAssetAdapter.ts b/src/FiatAssetAdapter.ts index ce54fde..f3ed884 100644 --- a/src/FiatAssetAdapter.ts +++ b/src/FiatAssetAdapter.ts @@ -1,13 +1,14 @@ -import { Htlc as OasisHtlc, HtlcStatus, SettlementInstruction, SettlementStatus } from '@nimiq/oasis-api'; +import { Htlc, HtlcStatus, SettlementInstruction, SettlementStatus, SettlementTokens } from '@nimiq/oasis-api'; import type { AssetAdapter, FiatSwapAsset } from './IAssetAdapter'; -export type HtlcDetails = OasisHtlc; - -export type SettleHtlcTokens = { authorization: string, smsApi: string }; +export { + Htlc as OasisHtlcDetails, + SettlementTokens as OasisSettlementTokens, +}; export interface OasisClient { - getHtlc(id: string): Promise; - settleHtlc(id: string, secret: string, settlementJWS: string, tokens?: Partial): Promise; + getHtlc(id: string): Promise; + settleHtlc(id: string, secret: string, settlementJWS: string, tokens?: SettlementTokens): Promise; } export class FiatAssetAdapter implements AssetAdapter { @@ -18,9 +19,9 @@ export class FiatAssetAdapter implements AssetAdapter { private async findTransaction( id: string, - test: (htlc: HtlcDetails) => boolean, - ): Promise { - const check = async (): Promise => { + test: (htlc: Htlc) => boolean, + ): Promise { + const check = async (): Promise => { try { const htlc = await this.client.getHtlc(id); if (test(htlc)) return htlc; @@ -66,8 +67,8 @@ export class FiatAssetAdapter implements AssetAdapter { value: number, data?: string, confirmations?: number, - onUpdate?: (htlc: HtlcDetails) => any, - ): Promise { + onUpdate?: (htlc: Htlc) => any, + ): Promise { return this.findTransaction( id, (htlc) => { @@ -83,15 +84,15 @@ export class FiatAssetAdapter implements AssetAdapter { } // eslint-disable-next-line class-methods-use-this - public async fundHtlc(): Promise { + public async fundHtlc(): Promise { throw new Error('Method "fundHtlc" not available for EUR/CRC HTLCs'); } - public async awaitHtlcSettlement(id: string): Promise> { + public async awaitHtlcSettlement(id: string): Promise> { return this.findTransaction( id, (htlc) => typeof htlc.preimage.value === 'string', - ) as Promise>; + ) as Promise>; } public async awaitSwapSecret(id: string): Promise { @@ -103,8 +104,8 @@ export class FiatAssetAdapter implements AssetAdapter { settlementJWS: string, secret: string, hash: string, - tokens?: Partial, - ): Promise { + tokens?: SettlementTokens, + ): Promise { if (this.stopped) throw new Error('FiatAssetAdapter called while stopped'); const jwsBody = settlementJWS.split('.')[1]; @@ -112,7 +113,7 @@ export class FiatAssetAdapter implements AssetAdapter { const jsonBody = atob(jwsBody.replace(/_/g, '/').replace(/-/g, '+')); const payload = JSON.parse(jsonBody) as SettlementInstruction; - let htlc: HtlcDetails; + let htlc: Htlc; try { htlc = await this.client.settleHtlc(payload.contractId, secret, settlementJWS, tokens); } catch (error) { @@ -127,7 +128,7 @@ export class FiatAssetAdapter implements AssetAdapter { return htlc; } - public async awaitSettlementConfirmation(id: string, onUpdate?: (tx: HtlcDetails) => any): Promise { + public async awaitSettlementConfirmation(id: string, onUpdate?: (tx: Htlc) => any): Promise { return this.findTransaction(id, (htlc) => { if (htlc.status !== HtlcStatus.SETTLED) return false; diff --git a/src/IAssetAdapter.ts b/src/IAssetAdapter.ts index 2f23552..2dc48fd 100644 --- a/src/IAssetAdapter.ts +++ b/src/IAssetAdapter.ts @@ -11,9 +11,9 @@ import type { Web3Client, } from './UsdcAssetAdapter'; import type { - HtlcDetails, + OasisHtlcDetails, OasisClient, - SettleHtlcTokens, + OasisSettlementTokens, } from './FiatAssetAdapter'; export enum SwapAsset { @@ -31,7 +31,7 @@ export type Transaction = TAsset extends SwapAsset.NIM ? NimiqTransactionDetails : TAsset extends SwapAsset.BTC ? BitcoinTransactionDetails : TAsset extends SwapAsset.USDC | SwapAsset.USDC_MATIC ? UsdcTransactionDetails - : TAsset extends FiatSwapAsset ? HtlcDetails + : TAsset extends FiatSwapAsset ? OasisHtlcDetails : never; export type Client = @@ -66,7 +66,7 @@ export interface AssetAdapter { serializedTx: string, secret: string, hash: string, - tokens?: Partial + tokens?: OasisSettlementTokens, ): Promise>; awaitSettlementConfirmation( diff --git a/src/SwapHandler.ts b/src/SwapHandler.ts index b50dfa6..205a07e 100644 --- a/src/SwapHandler.ts +++ b/src/SwapHandler.ts @@ -4,7 +4,7 @@ import { NimiqAssetAdapter } from './NimiqAssetAdapter'; import { BitcoinAssetAdapter } from './BitcoinAssetAdapter'; import { UsdcAssetAdapter } from './UsdcAssetAdapter'; import { FiatAssetAdapter } from './FiatAssetAdapter'; -import type { SettleHtlcTokens } from './FiatAssetAdapter'; +import type { OasisSettlementTokens } from './FiatAssetAdapter'; // Re-export to centralize exports export { SwapAsset, Client, Transaction }; @@ -113,7 +113,7 @@ export class SwapHandler public async settleIncoming( serializedTx: string, secret: string, - tokens?: Partial, + tokens?: OasisSettlementTokens, ): Promise> { return this.toAssetAdapter.settleHtlc( serializedTx, diff --git a/yarn.lock b/yarn.lock index c0f19c8..be308d2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -355,10 +355,9 @@ dependencies: bitcoinjs-lib "^5.1.10" -"@nimiq/oasis-api@^1.0.1": +"@nimiq/oasis-api@https://github.com/nimiq/oasis-api-js#9d63fac0ab1364e4e1494334ee2d74f3d6f96b35": version "1.1.1" - resolved "https://registry.yarnpkg.com/@nimiq/oasis-api/-/oasis-api-1.1.1.tgz#d1aa3d0f9afabf9116205809a0fc72d2e392bafa" - integrity sha512-jXNO7nvE0mzAyDCrucByv5AcyDWZjyqb9810YctysxqUtOxNG27hPkhomL+WW6nrnTVgvriJo3yaTFPTfLphKw== + resolved "https://github.com/nimiq/oasis-api-js#9d63fac0ab1364e4e1494334ee2d74f3d6f96b35" "@types/node@10.12.18": version "10.12.18"