From cfdfd725f3753b06d3b458d971c980ac9b0f3596 Mon Sep 17 00:00:00 2001 From: Denis Davidyuk Date: Tue, 7 May 2024 01:15:10 +1000 Subject: [PATCH] fix(channel): accept `host` only if initiator --- src/channel/internal.ts | 28 ++++++++++++++++++---------- test/integration/channel.ts | 8 ++++---- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/channel/internal.ts b/src/channel/internal.ts index 188f1fe5bb..a4a4ea4e83 100644 --- a/src/channel/internal.ts +++ b/src/channel/internal.ts @@ -52,7 +52,7 @@ export type SignTx = (tx: Encoded.Transaction, options?: SignOptions) => ( /** * @see {@link https://github.com/aeternity/protocol/blob/6734de2e4c7cce7e5e626caa8305fb535785131d/node/api/channels_api_usage.md#channel-establishing-parameters} */ -export interface ChannelOptions { +interface CommonChannelOptions { existingFsmId?: Encoded.Bytearray; /** * Channel url (for example: "ws://localhost:3001") @@ -92,17 +92,9 @@ export interface ChannelOptions { */ ttl?: number; /** - * Host of the responder's node - */ - host: string; - /** - * The port of the responders node + * The port of the responder's node */ port: number; - /** - * Participant role - */ - role: 'initiator' | 'responder'; /** * How to calculate minimum depth (default: txfee) */ @@ -185,6 +177,22 @@ export interface ChannelOptions { offchainTx?: Encoded.Transaction; } +export type ChannelOptions = CommonChannelOptions & ({ + /** + * Participant role + */ + role: 'initiator'; + /** + * Host of the responder's node + */ + host: string; +} | { + /** + * Participant role + */ + role: 'responder'; +}); + export interface ChannelHandler extends Function { enter?: Function; } diff --git a/test/integration/channel.ts b/test/integration/channel.ts index 6529f91b53..0701a77f4c 100644 --- a/test/integration/channel.ts +++ b/test/integration/channel.ts @@ -24,7 +24,7 @@ import { } from '../../src'; import { pause } from '../../src/utils/other'; import { - ChannelOptions, notify, SignTx, SignTxWithTag, + notify, SignTx, SignTxWithTag, } from '../../src/channel/internal'; import { appendSignature } from '../../src/channel/handlers'; import { assertNotNull, ensureEqual } from '../utils'; @@ -88,7 +88,7 @@ async function waitForChannel(channel: Channel): Promise { assertNotNull(signedTx); return buildTx(signedTx); }; - const sharedParams: Omit = { + const sharedParams = { url: channelUrl, pushAmount: 3, initiatorAmount: 1e15, @@ -98,8 +98,8 @@ async function waitForChannel(channel: Channel): Promise { host: 'localhost', port: 3114, lockPeriod: 1, - initiatorId: 'ak_', - responderId: 'ak_', + initiatorId: 'ak_' as Encoded.AccountAddress, + responderId: 'ak_' as Encoded.AccountAddress, role: 'initiator', minimumDepth: 0, };