1
- import { useToast } from "@chakra-ui/react" ;
2
1
import { useDynamicModalContext } from "@umami/components" ;
3
2
import { type ImplicitAccount , estimate , toAccountOperations } from "@umami/core" ;
4
- import {
5
- useAsyncActionHandler ,
6
- useFindNetwork ,
7
- useGetOwnedAccountSafe ,
8
- walletKit ,
9
- } from "@umami/state" ;
10
- import { formatJsonRpcError } from "@walletconnect/jsonrpc-utils" ;
3
+ import { useAsyncActionHandler , useFindNetwork , useGetOwnedAccountSafe } from "@umami/state" ;
4
+ import { WalletConnectError } from "@umami/utils" ;
11
5
import { type SessionTypes , type SignClientTypes , type Verify } from "@walletconnect/types" ;
12
- import { getSdkError } from "@walletconnect/utils" ;
13
6
14
7
import { BatchSignPage } from "../SendFlow/common/BatchSignPage" ;
15
8
import { SingleSignPage } from "../SendFlow/common/SingleSignPage" ;
@@ -26,7 +19,6 @@ export const useHandleWcRequest = () => {
26
19
const { handleAsyncActionUnsafe } = useAsyncActionHandler ( ) ;
27
20
const getAccount = useGetOwnedAccountSafe ( ) ;
28
21
const findNetwork = useFindNetwork ( ) ;
29
- const toast = useToast ( ) ;
30
22
31
23
return async (
32
24
event : {
@@ -41,82 +33,84 @@ export const useHandleWcRequest = () => {
41
33
} > ,
42
34
session : SessionTypes . Struct
43
35
) => {
44
- await handleAsyncActionUnsafe (
45
- async ( ) => {
46
- const { id, topic, params } = event ;
47
- const { request, chainId } = params ;
36
+ await handleAsyncActionUnsafe ( async ( ) => {
37
+ const { id, topic, params } = event ;
38
+ const { request, chainId } = params ;
48
39
49
- let modal ;
50
- let onClose ;
40
+ let modal ;
41
+ let onClose ;
51
42
52
- switch ( request . method ) {
53
- case "tezos_getAccounts" : {
54
- const response = formatJsonRpcError ( id , getSdkError ( "INVALID_METHOD" ) . message ) ;
55
- await walletKit . respondSessionRequest ( { topic, response } ) ;
56
- return ;
57
- }
43
+ switch ( request . method ) {
44
+ case "tezos_getAccounts" : {
45
+ throw new WalletConnectError (
46
+ "Getting accounts is not supported yet" ,
47
+ "WC_METHOD_UNSUPPORTED" ,
48
+ session
49
+ ) ;
50
+ }
58
51
59
- case "tezos_sign" : {
60
- // onClose = async () => {
61
- // const response = formatJsonRpcError(id, getSdkError("USER_REJECTED").message);
62
- // await walletKit.respondSessionRequest({ topic, response });
63
- // };
64
- // return openWith(<SignPayloadRequestModal request={"FIXME"} />, { onClose });
65
- const response = formatJsonRpcError ( id , getSdkError ( "INVALID_METHOD" ) . message ) ;
66
- await walletKit . respondSessionRequest ( { topic, response } ) ;
67
- return ;
68
- }
52
+ case "tezos_sign" : {
53
+ throw new WalletConnectError (
54
+ "Sign is not supported yet" ,
55
+ "WC_METHOD_UNSUPPORTED" ,
56
+ session
57
+ ) ;
58
+ }
69
59
70
- case "tezos_send" : {
71
- if ( ! request . params . account ) {
72
- throw new Error ( "Missing account in request" ) ;
73
- }
74
- const signer = getAccount ( request . params . account ) ;
75
- if ( ! signer ) {
76
- throw new Error ( `Unknown account, no signer: ${ request . params . account } ` ) ;
77
- }
78
- const operation = toAccountOperations (
79
- request . params . operations ,
80
- signer as ImplicitAccount
60
+ case "tezos_send" : {
61
+ if ( ! request . params . account ) {
62
+ throw new WalletConnectError ( "Missing account in request" , "INVALID_EVENT" , session ) ;
63
+ }
64
+ const signer = getAccount ( request . params . account ) ;
65
+ if ( ! signer ) {
66
+ throw new WalletConnectError (
67
+ `Unknown account, no signer: ${ request . params . account } ` ,
68
+ "UNAUTHORIZED_EVENT" ,
69
+ session
81
70
) ;
82
- const network = findNetwork ( chainId . split ( ":" ) [ 1 ] ) ;
83
- if ( ! network ) {
84
- const response = formatJsonRpcError ( id , getSdkError ( "INVALID_EVENT" ) . message ) ;
85
- await walletKit . respondSessionRequest ( { topic, response } ) ;
86
- toast ( { description : `Unsupported network: ${ chainId } ` , status : "error" } ) ;
87
- return ;
88
- }
89
- const estimatedOperations = await estimate ( operation , network ) ;
90
- const headerProps : SignHeaderProps = {
91
- network,
92
- appName : session . peer . metadata . name ,
93
- appIcon : session . peer . metadata . icons [ 0 ] ,
94
- } ;
95
- const signProps : SdkSignPageProps = {
96
- headerProps : headerProps ,
97
- operation : estimatedOperations ,
98
- requestId : { sdkType : "walletconnect" , id : id , topic } ,
99
- } ;
100
-
101
- if ( operation . operations . length === 1 ) {
102
- modal = < SingleSignPage { ...signProps } /> ;
103
- } else {
104
- modal = < BatchSignPage { ...signProps } { ...event . params . request . params } /> ;
105
- }
106
- onClose = async ( ) => {
107
- const response = formatJsonRpcError ( id , getSdkError ( "USER_REJECTED" ) . message ) ;
108
- await walletKit . respondSessionRequest ( { topic, response } ) ;
109
- } ;
71
+ }
72
+ const operation = toAccountOperations (
73
+ request . params . operations ,
74
+ signer as ImplicitAccount
75
+ ) ;
76
+ const network = findNetwork ( chainId . split ( ":" ) [ 1 ] ) ;
77
+ if ( ! network ) {
78
+ throw new WalletConnectError (
79
+ `Unsupported network ${ chainId } ` ,
80
+ "UNSUPPORTED_CHAINS" ,
81
+ session
82
+ ) ;
83
+ }
84
+ const estimatedOperations = await estimate ( operation , network ) ;
85
+ const headerProps : SignHeaderProps = {
86
+ network,
87
+ appName : session . peer . metadata . name ,
88
+ appIcon : session . peer . metadata . icons [ 0 ] ,
89
+ } ;
90
+ const signProps : SdkSignPageProps = {
91
+ headerProps : headerProps ,
92
+ operation : estimatedOperations ,
93
+ requestId : { sdkType : "walletconnect" , id : id , topic } ,
94
+ } ;
110
95
111
- return openWith ( modal , { onClose } ) ;
96
+ if ( operation . operations . length === 1 ) {
97
+ modal = < SingleSignPage { ...signProps } /> ;
98
+ } else {
99
+ modal = < BatchSignPage { ...signProps } { ...event . params . request . params } /> ;
112
100
}
113
- default :
114
- throw new Error ( `Unsupported method ${ request . method } ` ) ;
101
+ onClose = ( ) => {
102
+ throw new WalletConnectError ( "Rejected by user" , "USER_REJECTED" , session ) ;
103
+ } ;
104
+
105
+ return openWith ( modal , { onClose } ) ;
115
106
}
107
+ default :
108
+ throw new WalletConnectError (
109
+ `Unsupported method ${ request . method } ` ,
110
+ "WC_METHOD_UNSUPPORTED" ,
111
+ session
112
+ ) ;
116
113
}
117
- // error => ({
118
- // description: `Error while processing WalletConnect request: ${error.message}`,
119
- // })
120
- ) ;
114
+ } ) ;
121
115
} ;
122
116
} ;
0 commit comments