-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2029 from trilitech/add-wc-request
feat: WalletConnect integration, part 6, request
- Loading branch information
Showing
8 changed files
with
230 additions
and
30 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
apps/web/src/components/SendFlow/WalletConnect/useSignWithWalletConnect.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { type TezosToolkit } from "@taquito/taquito"; | ||
import { useDynamicModalContext } from "@umami/components"; | ||
import { executeOperations, totalFee } from "@umami/core"; | ||
import { useAsyncActionHandler, walletKit } from "@umami/state"; | ||
import { formatJsonRpcResult } from "@walletconnect/jsonrpc-utils"; | ||
import { useForm } from "react-hook-form"; | ||
|
||
import { SuccessStep } from "../SuccessStep"; | ||
import { type CalculatedSignProps, type SdkSignPageProps } from "../utils"; | ||
|
||
export const useSignWithWalletConnect = ({ | ||
operation, | ||
headerProps, | ||
requestId, | ||
}: SdkSignPageProps): CalculatedSignProps => { | ||
const { isLoading: isSigning, handleAsyncAction } = useAsyncActionHandler(); | ||
const { openWith } = useDynamicModalContext(); | ||
|
||
const form = useForm({ defaultValues: { executeParams: operation.estimates } }); | ||
|
||
if (requestId.sdkType !== "walletconnect") { | ||
return { | ||
fee: 0, | ||
isSigning: false, | ||
onSign: async () => {}, | ||
network: null, | ||
}; | ||
} | ||
|
||
const onSign = async (tezosToolkit: TezosToolkit) => | ||
handleAsyncAction( | ||
async () => { | ||
const { opHash } = await executeOperations( | ||
{ ...operation, estimates: form.watch("executeParams") }, | ||
tezosToolkit | ||
); | ||
|
||
const response = formatJsonRpcResult(requestId.id, { hash: opHash }); | ||
await walletKit.respondSessionRequest({ topic: requestId.topic, response }); | ||
return openWith(<SuccessStep hash={opHash} />); | ||
}, | ||
error => ({ | ||
description: `Failed to confirm WalletConnect operation: ${error.message}`, | ||
}) | ||
); | ||
|
||
return { | ||
fee: totalFee(form.watch("executeParams")), | ||
isSigning, | ||
onSign, | ||
network: headerProps.network, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
apps/web/src/components/WalletConnect/useHandleWcRequest.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { useDynamicModalContext } from "@umami/components"; | ||
import { type ImplicitAccount, estimate, toAccountOperations } from "@umami/core"; | ||
import { useAsyncActionHandler, useFindNetwork, useGetOwnedAccountSafe } from "@umami/state"; | ||
import { WalletConnectError } from "@umami/utils"; | ||
import { type SessionTypes, type SignClientTypes, type Verify } from "@walletconnect/types"; | ||
|
||
import { BatchSignPage } from "../SendFlow/common/BatchSignPage"; | ||
import { SingleSignPage } from "../SendFlow/common/SingleSignPage"; | ||
import { type SdkSignPageProps, type SignHeaderProps } from "../SendFlow/utils"; | ||
|
||
/** | ||
* @returns a function that handles a beacon message and opens a modal with the appropriate content | ||
* | ||
* For operation requests it will also try to convert the operation(s) to our {@link Operation} format, | ||
* estimate the fee and open the BeaconSignPage only if it succeeds | ||
*/ | ||
export const useHandleWcRequest = () => { | ||
const { openWith } = useDynamicModalContext(); | ||
const { handleAsyncActionUnsafe } = useAsyncActionHandler(); | ||
const getAccount = useGetOwnedAccountSafe(); | ||
const findNetwork = useFindNetwork(); | ||
|
||
return async ( | ||
event: { | ||
verifyContext: Verify.Context; | ||
} & SignClientTypes.BaseEventArgs<{ | ||
request: { | ||
method: string; | ||
params: any; | ||
expiryTimestamp?: number; | ||
}; | ||
chainId: string; | ||
}>, | ||
session: SessionTypes.Struct | ||
) => { | ||
await handleAsyncActionUnsafe(async () => { | ||
const { id, topic, params } = event; | ||
const { request, chainId } = params; | ||
|
||
let modal; | ||
let onClose; | ||
|
||
switch (request.method) { | ||
case "tezos_getAccounts": { | ||
throw new WalletConnectError( | ||
"Getting accounts is not supported yet", | ||
"WC_METHOD_UNSUPPORTED", | ||
session | ||
); | ||
} | ||
|
||
case "tezos_sign": { | ||
throw new WalletConnectError( | ||
"Sign is not supported yet", | ||
"WC_METHOD_UNSUPPORTED", | ||
session | ||
); | ||
} | ||
|
||
case "tezos_send": { | ||
if (!request.params.account) { | ||
throw new WalletConnectError("Missing account in request", "INVALID_EVENT", session); | ||
} | ||
const signer = getAccount(request.params.account); | ||
if (!signer) { | ||
throw new WalletConnectError( | ||
`Unknown account, no signer: ${request.params.account}`, | ||
"UNAUTHORIZED_EVENT", | ||
session | ||
); | ||
} | ||
const operation = toAccountOperations( | ||
request.params.operations, | ||
signer as ImplicitAccount | ||
); | ||
const network = findNetwork(chainId.split(":")[1]); | ||
if (!network) { | ||
throw new WalletConnectError( | ||
`Unsupported network ${chainId}`, | ||
"UNSUPPORTED_CHAINS", | ||
session | ||
); | ||
} | ||
const estimatedOperations = await estimate(operation, network); | ||
const headerProps: SignHeaderProps = { | ||
network, | ||
appName: session.peer.metadata.name, | ||
appIcon: session.peer.metadata.icons[0], | ||
}; | ||
const signProps: SdkSignPageProps = { | ||
headerProps: headerProps, | ||
operation: estimatedOperations, | ||
requestId: { sdkType: "walletconnect", id: id, topic }, | ||
}; | ||
|
||
if (operation.operations.length === 1) { | ||
modal = <SingleSignPage {...signProps} />; | ||
} else { | ||
modal = <BatchSignPage {...signProps} {...event.params.request.params} />; | ||
} | ||
onClose = () => { | ||
throw new WalletConnectError("Rejected by user", "USER_REJECTED", session); | ||
}; | ||
|
||
return openWith(modal, { onClose }); | ||
} | ||
default: | ||
throw new WalletConnectError( | ||
`Unsupported method ${request.method}`, | ||
"WC_METHOD_UNSUPPORTED", | ||
session | ||
); | ||
} | ||
}); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9001b25
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.