import { Transaction as PwTransaction } from '@lay2/pw-core' ; import { UNIPASS_URL } from '../constants' ; import { UnipassAction } from '../models/unipass' ; import i18n from '../i18n' ; export function generateUnipassUrl ( action , successURL , failURL , pubkey ? , message ? , state ? ) { const url = new URL ( `${ UNIPASS_URL }/${ action === UnipassAction . Login ? UnipassAction . Login : UnipassAction . Sign }` ) ; 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 === UnipassAction . SignTx || action === UnipassAction . 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 ] ) ; } return url . href ; } export function generateUnipassLoginUrl ( successURL , failURL ) { return generateUnipassUrl ( UnipassAction . Login , successURL , failURL ) ; } export function generateUnipassSignUrl ( successURL , failURL , pubkey ? , message ? ) { return generateUnipassUrl ( UnipassAction . Sign , successURL , failURL , pubkey , message ) ; } export function generateUnipassSignTxUrl ( successURL , failURL , pubkey ? , message ? , state ? ) { return generateUnipassUrl ( UnipassAction . SignTx , successURL , failURL , pubkey , message , state ) ; } export function generateUnipassRedeemUrl ( successURL , failURL , pubkey ? , message ? , state ? ) { return generateUnipassUrl ( UnipassAction . Redeem , successURL , failURL , pubkey , message , state ) ; } export class UnipassConfig { static redirectUri : string | null = null ; static setRedirectUri ( path ) { UnipassConfig . redirectUri = path ; } static getRedirectUri ( ) { return UnipassConfig . redirectUri ; } static clear ( ) { UnipassConfig . redirectUri = null ; } } export function isPwTransaction ( tx ) { return tx ?. raw != null ; }