Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 44 additions & 19 deletions packages/walletkit/src/core/TonWalletKit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import type { EventRouter } from './EventRouter';
import type { RequestProcessor } from './RequestProcessor';
import { JettonsManager } from './JettonsManager';
import type { JettonsAPI } from '../types/jettons';
import { ConnectHandler } from '../handlers/ConnectHandler';
import { SwapManager } from '../defi/swap';
import type {
RawBridgeEventConnect,
Expand Down Expand Up @@ -522,6 +523,24 @@ export class TonWalletKit implements ITonWalletKit {
this.eventRouter.removeErrorCallback();
}

// === URL Parsing API ===

/**
* Allow to convert url to ConnectionRequestEvent to use inline way
*/
async connectionEventFromUrl(url: string): Promise<ConnectionRequestEvent> {
await this.ensureInitialized();

try {
const bridgeEvent = this.parseBridgeConnectEventFromUrl(url);
const handler = new ConnectHandler(() => {}, this.config, this.analyticsManager);
return await handler.handle(bridgeEvent);
} catch (error) {
log.error('Failed to create connection event from URL', { error, url });
throw error;
}
}

// === URL Processing API ===

/**
Expand All @@ -532,25 +551,7 @@ export class TonWalletKit implements ITonWalletKit {
await this.ensureInitialized();

try {
// Parse and validate the TON Connect URL
const parsedUrl = this.parseTonConnectUrl(url);
if (!parsedUrl) {
throw new WalletKitError(ERROR_CODES.VALIDATION_ERROR, 'Invalid TON Connect URL format', undefined, {
url,
});
}

// Create a bridge event from the parsed URL
const bridgeEvent = this.createConnectEventFromUrl(parsedUrl);
if (!bridgeEvent) {
throw new WalletKitError(
ERROR_CODES.VALIDATION_ERROR,
'Invalid TON Connect URL - unable to create bridge event',
undefined,
{ parsedUrl },
);
}

const bridgeEvent = this.parseBridgeConnectEventFromUrl(url);
await this.eventRouter.routeEvent(bridgeEvent);
} catch (error) {
log.error('Failed to handle TON Connect URL', { error, url });
Expand Down Expand Up @@ -578,6 +579,30 @@ export class TonWalletKit implements ITonWalletKit {
await this.eventRouter.routeEvent(bridgeEvent);
}

/**
* Parse and validate TON Connect URL into a RawBridgeEventConnect
*/
private parseBridgeConnectEventFromUrl(url: string): RawBridgeEventConnect {
const parsedUrl = this.parseTonConnectUrl(url);
if (!parsedUrl) {
throw new WalletKitError(ERROR_CODES.VALIDATION_ERROR, 'Invalid TON Connect URL format', undefined, {
url,
});
}

const bridgeEvent = this.createConnectEventFromUrl(parsedUrl);
if (!bridgeEvent) {
throw new WalletKitError(
ERROR_CODES.VALIDATION_ERROR,
'Invalid TON Connect URL - unable to create bridge event',
undefined,
{ parsedUrl },
);
}

return bridgeEvent;
}

/**
* Parse TON Connect URL to extract connection parameters
*/
Expand Down
7 changes: 7 additions & 0 deletions packages/walletkit/src/types/kit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ export interface ITonWalletKit {
/** List all active sessions */
listSessions(): Promise<TONConnectSession[]>;

// === URL Parsing API ===

/**
* Allow to convert url to ConnectionRequestEvent to use inline way
*/
connectionEventFromUrl(url: string): Promise<ConnectionRequestEvent>;

// === URL Processing ===

/** Handle pasted TON Connect URL/link */
Expand Down
Loading