Skip to content

Commit

Permalink
Use CRC-enabled OASIS API package, streamline re-exports
Browse files Browse the repository at this point in the history
  • Loading branch information
sisou committed Jun 14, 2024
1 parent 1359ef4 commit cd6428e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
37 changes: 19 additions & 18 deletions src/FiatAssetAdapter.ts
Original file line number Diff line number Diff line change
@@ -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<HtlcDetails>;
settleHtlc(id: string, secret: string, settlementJWS: string, tokens?: Partial<SettleHtlcTokens>): Promise<HtlcDetails>;
getHtlc(id: string): Promise<Htlc>;
settleHtlc(id: string, secret: string, settlementJWS: string, tokens?: Partial<SettlementTokens>): Promise<Htlc>;
}

export class FiatAssetAdapter implements AssetAdapter<FiatSwapAsset> {
Expand All @@ -18,9 +19,9 @@ export class FiatAssetAdapter implements AssetAdapter<FiatSwapAsset> {

private async findTransaction(
id: string,
test: (htlc: HtlcDetails) => boolean,
): Promise<HtlcDetails> {
const check = async (): Promise<HtlcDetails | null> => {
test: (htlc: Htlc) => boolean,
): Promise<Htlc> {
const check = async (): Promise<Htlc | null> => {
try {
const htlc = await this.client.getHtlc(id);
if (test(htlc)) return htlc;
Expand Down Expand Up @@ -66,8 +67,8 @@ export class FiatAssetAdapter implements AssetAdapter<FiatSwapAsset> {
value: number,
data?: string,
confirmations?: number,
onUpdate?: (htlc: HtlcDetails) => any,
): Promise<HtlcDetails> {
onUpdate?: (htlc: Htlc) => any,
): Promise<Htlc> {
return this.findTransaction(
id,
(htlc) => {
Expand All @@ -83,15 +84,15 @@ export class FiatAssetAdapter implements AssetAdapter<FiatSwapAsset> {
}

// eslint-disable-next-line class-methods-use-this
public async fundHtlc(): Promise<HtlcDetails> {
public async fundHtlc(): Promise<Htlc> {
throw new Error('Method "fundHtlc" not available for EUR/CRC HTLCs');
}

public async awaitHtlcSettlement(id: string): Promise<OasisHtlc<HtlcStatus.SETTLED>> {
public async awaitHtlcSettlement(id: string): Promise<Htlc<HtlcStatus.SETTLED>> {
return this.findTransaction(
id,
(htlc) => typeof htlc.preimage.value === 'string',
) as Promise<OasisHtlc<HtlcStatus.SETTLED>>;
) as Promise<Htlc<HtlcStatus.SETTLED>>;
}

public async awaitSwapSecret(id: string): Promise<string> {
Expand All @@ -103,16 +104,16 @@ export class FiatAssetAdapter implements AssetAdapter<FiatSwapAsset> {
settlementJWS: string,
secret: string,
hash: string,
tokens?: Partial<SettleHtlcTokens>,
): Promise<HtlcDetails> {
tokens?: Partial<SettlementTokens>,
): Promise<Htlc> {
if (this.stopped) throw new Error('FiatAssetAdapter called while stopped');

const jwsBody = settlementJWS.split('.')[1];
// JWS is encoded as Base64Url
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) {
Expand All @@ -127,7 +128,7 @@ export class FiatAssetAdapter implements AssetAdapter<FiatSwapAsset> {
return htlc;
}

public async awaitSettlementConfirmation(id: string, onUpdate?: (tx: HtlcDetails) => any): Promise<HtlcDetails> {
public async awaitSettlementConfirmation(id: string, onUpdate?: (tx: Htlc) => any): Promise<Htlc> {
return this.findTransaction(id, (htlc) => {
if (htlc.status !== HtlcStatus.SETTLED) return false;

Expand Down
8 changes: 4 additions & 4 deletions src/IAssetAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import type {
Web3Client,
} from './UsdcAssetAdapter';
import type {
HtlcDetails,
OasisHtlcDetails,
OasisClient,
SettleHtlcTokens,
OasisSettlementTokens,
} from './FiatAssetAdapter';

export enum SwapAsset {
Expand All @@ -31,7 +31,7 @@ export type Transaction<TAsset extends SwapAsset> =
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<TAsset extends SwapAsset> =
Expand Down Expand Up @@ -66,7 +66,7 @@ export interface AssetAdapter<TAsset extends SwapAsset> {
serializedTx: string,
secret: string,
hash: string,
tokens?: Partial<SettleHtlcTokens>
tokens?: Partial<OasisSettlementTokens>
): Promise<Transaction<TAsset>>;

awaitSettlementConfirmation(
Expand Down
4 changes: 2 additions & 2 deletions src/SwapHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -113,7 +113,7 @@ export class SwapHandler<FromAsset extends SwapAsset, ToAsset extends SwapAsset>
public async settleIncoming(
serializedTx: string,
secret: string,
tokens?: Partial<SettleHtlcTokens>,
tokens?: Partial<OasisSettlementTokens>,
): Promise<Transaction<ToAsset>> {
return this.toAssetAdapter.settleHtlc(
serializedTx,
Expand Down
5 changes: 2 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]":
version "10.12.18"
Expand Down

0 comments on commit cd6428e

Please sign in to comment.