UNPKG

@signumjs/wallets

Version:

Wallets communication package for DApps in the Signum Network

137 lines 5.45 kB
"use strict"; /** * Original work Copyright (c) 2026 Signum Network */ Object.defineProperty(exports, "__esModule", { value: true }); exports.MobileWallet = void 0; /* globals window */ const standards_1 = require("@signumjs/standards"); const isNodeJS_1 = require("../isNodeJS"); const errors_1 = require("./errors"); /** * This wallet allows interacting with SIP22 compatible mobile wallets via deeplinks. * * Unlike the DesktopWallet, the MobileWallet uses direct deeplinks (signum://) * supports the versatile sign command, and callback URLs for receiving responses from the mobile wallet app. */ class MobileWallet { openInBrowser; constructor(options) { this.openInBrowser = options?.openInBrowser === undefined ? true : options.openInBrowser; if ((0, isNodeJS_1.isNodeJS)()) { this.openInBrowser = false; } } /** * Opens the mobile wallet to request connection and public key. * The wallet will redirect to the callback URL with the public key as a URL parameter. * * Example callbacks: * https://myapp.com/connected * https://myotherapp.com?action=connected * * In the callback page you can use {@link MobileWallet.parseConnectCallback} to extract the public key * * @param args - The connection arguments. See {@link MobileWalletConnectArgs}. * @returns The deeplink URL (for testing or custom handling) */ connect({ callbackUrl, appName, network }) { const deeplink = standards_1.src22.createDeeplink({ action: 'connect', payload: { callbackUrl, network, appName } }); if (this.openInBrowser) { window?.open(deeplink, '_self', 'noopener noreferrer'); } return deeplink; } /** * Opens the mobile wallet to sign an unsigned transaction. * The wallet will redirect to the callback URL with status and transactionId parameters. * * Example callback on success: https://myapp.com/signed?status=success&transactionId=xyz789... * Example callback on rejection: https://myapp.com/signed?status=rejected * * @returns The deeplink URL (for testing or custom handling) */ sign({ network, callbackUrl, unsignedTransactionBytes }) { const deeplink = standards_1.src22.createDeeplink({ action: 'sign', payload: { unsignedTransactionBytes, callbackUrl, network } }); if (this.openInBrowser) { window?.open(deeplink, '_self'); } return deeplink; } /** * Static helper to parse callback data from URL parameters. * Use this in your callback pages to extract the data sent by the mobile wallet. * * The returned public key should be stored and used for further signing requests. * The public key is required to create unsigned transactions byte sequences. * * Example usage in callback page: * ```typescript * const data = MobileWallet.parseCallback(); * if (data.publicKey) { * localStorage.setItem('signum-wallet-publicKey', data.publicKey); * } * if (data.status === 'success' && data.transactionId) { * localStorage.setItem('signum-wallet-txId', data.transactionId); * } * ``` */ static parseConnectCallback() { if (typeof window === 'undefined') { throw new errors_1.MobileWalletError("window is undefined - Looks like you're not running in a browser environment"); } const params = new URLSearchParams(window.location.search); if (!params.has('publicKey')) { throw new errors_1.MobileWalletError("No public key found in callback URL"); } // TODO: this is optional at the moment, as we need to add the status in the wallet const status = params.get('status'); if (status) { // throw new MobileWalletError("No status found in callback URL"); if (status !== 'success' && status !== 'rejected' && status !== 'failed') { throw new errors_1.MobileWalletError("Invalid status found in callback URL"); } } return { publicKey: params.get('publicKey'), status: status }; } /** * Static helper to parse callback data from URL parameters from _sign_ command. * Use this in your callback pages to extract the data sent by the mobile wallet. */ static parseSignCallback() { if (typeof window === 'undefined') { throw new errors_1.MobileWalletError("window is undefined - Looks like you're not running in a browser environment"); } const params = new URLSearchParams(window.location.search); if (!params.has('status')) { throw new errors_1.MobileWalletError("No status found in callback URL"); } const status = params.get('status'); if (status !== 'success' && status !== 'rejected' && status !== 'failed') { throw new errors_1.MobileWalletError("Invalid status found in callback URL"); } return { status: status, transactionId: params.get('transactionId') || undefined }; } } exports.MobileWallet = MobileWallet; //# sourceMappingURL=mobileWallet.js.map