import { FLASH_SIGNER_URL } from '../constants' ; import { FlashsignerAction } from '../models/flashsigner' ; import i18n from '../i18n' ; import { UnipassConfig } from './unipass' ; export function generateFlashsignerUrl ( action , successURL , failURL , pubkey ? , message ? , state ? , extra = { } ) { const url = new URL ( `${ FLASH_SIGNER_URL }/${ action === FlashsignerAction . Login ? 'connect' : 'transfer-mnft' }` ) ; const surl = new URL ( successURL ) ; surl . searchParams . set ( 'action' , action ) ; const redirectUri = UnipassConfig . getRedirectUri ( ) ; if ( redirectUri ) { surl . searchParams . set ( 'redirect' , redirectUri ) ; const furl = new URL ( failURL ) ; furl . searchParams . set ( 'redirect' , redirectUri ) ; failURL = furl . href ; } if ( action === FlashsignerAction . SignTx || action === FlashsignerAction . Redeem ) { surl . searchParams . set ( 'prev_state' , JSON . stringify ( { ... state , } ) ) ; const furl = new URL ( failURL ) ; furl . searchParams . set ( 'prev_state' , JSON . stringify ( { ... state , } ) ) ; furl . searchParams . set ( 'action' , action ) ; failURL = furl . href ; } const params = { success_url : surl . href , fail_url : surl . href , } ; if ( pubkey ) { params . pubkey = pubkey ; } if ( message ) { params . message = message ; } const lang = i18n . language ; if ( lang ) { params . lang = lang ; } for ( const key of Object . keys ( params ) ) { url . searchParams . set ( key , params [ key ] ) ; } for ( const key in extra ) { if ( Object . prototype . hasOwnProperty . call ( extra , key ) ) { const val = extra [ key ] ; url . searchParams . set ( key , val ) ; } } url . searchParams . set ( 'dapp_name' , i18n . t ( 'common.title' , { ns : 'translations' } ) ) ; url . searchParams . set ( 'dapp_logo' , `${ window . location . origin }/icons/192.png` ) ; url . searchParams . set ( 'locale' , i18n . language ) ; return url . href ; } export function generateFlashsignerLoginUrl ( successURL , failURL ) { return generateFlashsignerUrl ( FlashsignerAction . Login , successURL , failURL ) ; } export function generateFlashsignerSignUrl ( successURL , failURL , pubkey ? , message ? ) { return generateFlashsignerUrl ( FlashsignerAction . Sign , successURL , failURL , pubkey , message ) ; } export function generateFlashsignerSignTxUrl ( successURL , failURL , pubkey ? , message ? , state ? , extra ? ) { return generateFlashsignerUrl ( FlashsignerAction . SignTx , successURL , failURL , pubkey , message , state , extra ) ; } export function generateFlashsignerRedeemUrl ( successURL , failURL , pubkey ? , message ? , state ? ) { return generateFlashsignerUrl ( FlashsignerAction . Redeem , successURL , failURL , pubkey , message , state ) ; }