1- import React , { useState , useCallback , useMemo , useEffect } from 'react'
1+ import React , { useState , useCallback , useEffect } from 'react'
22import { useNavigate } from 'react-router-dom'
33import { useTranslation } from 'react-i18next'
44import TextField from 'widgets/TextField'
55import Dialog from 'widgets/Dialog'
6- import { ErrorCode , RoutePath , isSuccessResponse , errorFormatter } from 'utils'
6+ import { ErrorCode , isSuccessResponse , errorFormatter } from 'utils'
77import {
88 useState as useGlobalState ,
99 useDispatch ,
@@ -12,19 +12,21 @@ import {
1212 sendCreateSUDTAccountTransaction ,
1313 sendSUDTTransaction ,
1414} from 'states'
15- import { SignTransactionParams } from 'ckb-walletconnect-wallet-sdk'
16- import { OfflineSignType , OfflineSignStatus , signAndExportTransaction } from 'services/remote'
15+ import { SessionRequest } from 'ckb-walletconnect-wallet-sdk'
16+ import { OfflineSignType , OfflineSignStatus , signAndExportTransaction , signTransactionOnly } from 'services/remote'
1717import { PasswordIncorrectException } from 'exceptions'
1818import styles from './wcSignTransactionDialog.module.scss'
1919
2020const WCSignTransactionDialog = ( {
2121 wallet,
22- data ,
22+ event ,
2323 onDismiss,
24+ onApproveRequest,
2425} : {
2526 wallet : State . Wallet
26- data : SignTransactionParams
27+ event : SessionRequest
2728 onDismiss : ( ) => void
29+ onApproveRequest : ( event : SessionRequest , options : any ) => void
2830} ) => {
2931 const {
3032 app : {
@@ -34,6 +36,7 @@ const WCSignTransactionDialog = ({
3436 } = useGlobalState ( )
3537
3638 const walletID = wallet . id
39+ const data = event . params . request . params
3740 const {
3841 transaction,
3942 type : signType = OfflineSignType . Regular ,
@@ -78,6 +81,22 @@ const WCSignTransactionDialog = ({
7881 onDismiss ( )
7982 } , [ data , dispatch , onDismiss , t , password , walletID ] )
8083
84+ const sign = useCallback ( async ( ) => {
85+ const res = await signTransactionOnly ( {
86+ transaction,
87+ type : signType ,
88+ status : signStatus ,
89+ walletID,
90+ password,
91+ } )
92+ if ( ! isSuccessResponse ( res ) ) {
93+ setError ( errorFormatter ( res . message , t ) )
94+ return
95+ }
96+ onApproveRequest ( event , res . result )
97+ onDismiss ( )
98+ } , [ data , dispatch , onDismiss , t , password , walletID ] )
99+
81100 const onSubmit = useCallback (
82101 async ( e ?: React . FormEvent ) => {
83102 if ( e ) {
@@ -88,7 +107,7 @@ const WCSignTransactionDialog = ({
88107 }
89108 setIsSigning ( true )
90109 if ( ! isBroadcast ) {
91- await signAndExport ( )
110+ await sign ( )
92111 setIsSigning ( false )
93112 return
94113 }
@@ -98,10 +117,11 @@ const WCSignTransactionDialog = ({
98117 if ( isSigning ) {
99118 break
100119 }
101- await sendTransaction ( { walletID, tx : transaction , description, password } ) ( dispatch ) . then ( ( { status } ) => {
102- if ( isSuccessResponse ( { status } ) ) {
103- navigate ( RoutePath . History )
104- } else if ( status === ErrorCode . PasswordIncorrect ) {
120+ await sendTransaction ( { walletID, tx : transaction , description, password } ) ( dispatch ) . then ( res => {
121+ if ( isSuccessResponse ( res . status ) ) {
122+ onApproveRequest ( event , res . result )
123+ onDismiss ( )
124+ } else if ( res . status === ErrorCode . PasswordIncorrect ) {
105125 throw new PasswordIncorrectException ( )
106126 }
107127 } )
@@ -111,13 +131,11 @@ const WCSignTransactionDialog = ({
111131 if ( isSigning ) {
112132 break
113133 }
114- await sendTransaction ( { walletID, tx : transaction , description, password } ) ( dispatch ) . then ( ( { status } ) => {
115- if ( isSuccessResponse ( { status } ) ) {
116- dispatch ( {
117- type : AppActions . SetGlobalDialog ,
118- payload : 'unlock-success' ,
119- } )
120- } else if ( status === ErrorCode . PasswordIncorrect ) {
134+ await sendTransaction ( { walletID, tx : transaction , description, password } ) ( dispatch ) . then ( res => {
135+ if ( isSuccessResponse ( res ) ) {
136+ onApproveRequest ( event , res . result )
137+ onDismiss ( )
138+ } else if ( res . status === ErrorCode . PasswordIncorrect ) {
121139 throw new PasswordIncorrectException ( )
122140 }
123141 } )
@@ -130,10 +148,11 @@ const WCSignTransactionDialog = ({
130148 tx : transaction ,
131149 password,
132150 }
133- await sendCreateSUDTAccountTransaction ( params ) ( dispatch ) . then ( ( { status } ) => {
134- if ( isSuccessResponse ( { status } ) ) {
135- navigate ( RoutePath . History )
136- } else if ( status === ErrorCode . PasswordIncorrect ) {
151+ await sendCreateSUDTAccountTransaction ( params ) ( dispatch ) . then ( res => {
152+ if ( isSuccessResponse ( res ) ) {
153+ onApproveRequest ( event , res . result )
154+ onDismiss ( )
155+ } else if ( res . status === ErrorCode . PasswordIncorrect ) {
137156 throw new PasswordIncorrectException ( )
138157 }
139158 } )
@@ -145,10 +164,11 @@ const WCSignTransactionDialog = ({
145164 tx : transaction ,
146165 password,
147166 }
148- await sendSUDTTransaction ( params ) ( dispatch ) . then ( ( { status } ) => {
149- if ( isSuccessResponse ( { status } ) ) {
150- navigate ( RoutePath . History )
151- } else if ( status === ErrorCode . PasswordIncorrect ) {
167+ await sendSUDTTransaction ( params ) ( dispatch ) . then ( res => {
168+ if ( isSuccessResponse ( res ) ) {
169+ onApproveRequest ( event , res . result )
170+ onDismiss ( )
171+ } else if ( res . status === ErrorCode . PasswordIncorrect ) {
152172 throw new PasswordIncorrectException ( )
153173 }
154174 } )
@@ -192,14 +212,10 @@ const WCSignTransactionDialog = ({
192212 [ setPassword , setError ]
193213 )
194214
195- const title = useMemo ( ( ) => {
196- return ! isBroadcast ? t ( 'offline-sign.sign-and-export' ) : t ( 'offline-sign.sign-and-broadcast' )
197- } , [ isBroadcast , t ] )
198-
199215 return (
200216 < Dialog
201217 show
202- title = { title }
218+ title = { t ( 'wallet-connect.sign-confirmation' ) }
203219 onCancel = { onDismiss }
204220 onConfirm = { onSubmit }
205221 disabled = { disabled }
0 commit comments