All files / src sign.ts

13.33% Statements 4/30
50% Branches 6/12
16.66% Functions 1/6
13.33% Lines 4/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 7720x     1x 1x   2x                                                                                                                                            
import {ResolvedSigningRequest, UserInterfaceTranslateOptions} from '@wharfkit/session'
 
import {WAXCloudWalletSigningResponse} from './types'
import {getCurrentTime, isValidEvent, registerCloseListener} from './utils'
import {version} from './version'
 
export async function popupTransact(
    t: (key: string, options?: UserInterfaceTranslateOptions) => string,
    urlString: URL | string,
    request: ResolvedSigningRequest,
    timeout = 300000
): Promise<WAXCloudWalletSigningResponse> {
    const url = new URL(urlString)
 
    const popup = await window.open(url, 'WalletPluginCloudWalletPopup', 'height=800,width=600')
    Iif (!popup) {
        throw new Error(
            t('error.popup', {
                default:
                    'Unable to open the popup window. Check your browser settings and try again.',
            })
        )
    }
 
    return new Promise<WAXCloudWalletSigningResponse>((resolve, reject) => {
        const closeListener = registerCloseListener(t, popup, reject)
        const handleEvent = (event: MessageEvent) => {
            Iif (!isValidEvent(event, url, popup)) {
                return
            }
            popup?.postMessage(
                {
                    feeFallback: true,
                    freeBandwidth: true,
                    startTime: getCurrentTime(),
                    transaction: request.serializedTransaction,
                    type: 'TRANSACTION',
                    version,
                },
                String(urlString)
            )
            const handleSigning = (signingEvent: MessageEvent) => {
                Iif (!isValidEvent(signingEvent, url, popup)) {
                    return
                }
                try {
                    resolve(signingEvent.data)
                } catch (e) {
                    reject(e)
                } finally {
                    window.removeEventListener('message', handleEvent)
                    window.removeEventListener('message', handleSigning)
                    clearTimeout(autoCancel)
                    clearInterval(closeListener)
                }
            }
            window.addEventListener('message', handleSigning)
        }
        // Automatically cancel request after 5 minutes to cleanup windows/promises
        const autoCancel = setTimeout(() => {
            popup.close()
            window.removeEventListener('message', handleEvent)
            reject(
                new Error(
                    t('error.timeout', {
                        default:
                            'The request has timed out after {{timeout}} seconds. Please try again.',
                        timeout: timeout / 1000,
                    })
                )
            )
        }, timeout)
        // Add event listener awaiting Cloud Wallet Response
        window.addEventListener('message', handleEvent)
    })
}