From 5f69691423ea2d97eb6066364f4c4887da547ed4 Mon Sep 17 00:00:00 2001 From: ByteZhang Date: Mon, 9 Sep 2024 20:59:13 +0800 Subject: [PATCH] chore: optimize ton example (#233) --- .../ApiActuator/PresupposeParamsSelector.tsx | 9 +++++++ .../components/ApiActuator/useApiExecutor.ts | 10 +++++++- .../components/chains/alephium/example.tsx | 24 ++++++++++++++++--- .../components/chains/alephium/params.ts | 14 +++++------ .../example/components/chains/ton/example.tsx | 21 +++++++++++++--- .../example/components/chains/ton/params.ts | 15 ++++++++++++ 6 files changed, 79 insertions(+), 14 deletions(-) diff --git a/packages/example/components/ApiActuator/PresupposeParamsSelector.tsx b/packages/example/components/ApiActuator/PresupposeParamsSelector.tsx index c7039e8d..bd31c211 100644 --- a/packages/example/components/ApiActuator/PresupposeParamsSelector.tsx +++ b/packages/example/components/ApiActuator/PresupposeParamsSelector.tsx @@ -41,6 +41,9 @@ export function PresupposeParamsSelector({ ); const showPresupposeParams = presupposeParams && presupposeParams.length > 1; + + const presupposeParamsDescription = presupposeParams?.find((param) => param.id === currentPurposeParamId)?.description; + return showPresupposeParams ? (
预设参数 @@ -60,6 +63,12 @@ export function PresupposeParamsSelector({ ))} + { + presupposeParamsDescription && +

+ {presupposeParamsDescription} +

+ }
) : null; } diff --git a/packages/example/components/ApiActuator/useApiExecutor.ts b/packages/example/components/ApiActuator/useApiExecutor.ts index 19e22807..b69fbc1f 100644 --- a/packages/example/components/ApiActuator/useApiExecutor.ts +++ b/packages/example/components/ApiActuator/useApiExecutor.ts @@ -1,6 +1,7 @@ import { get } from 'lodash'; import { useCallback } from 'react'; import { stringifyWithSpecialType } from '../../lib/jsonUtils'; +import { useToast } from '../ui/use-toast'; export type IApiExecutor = { onExecute: (request: string) => Promise; @@ -14,6 +15,8 @@ export function useApiExecutor({ onExecute, onValidate }: IApiExecutor): { result: string, ) => Promise<{ validation: string | undefined; error: string | undefined }>; } { + const { toast } = useToast(); + const execute = useCallback( async (request: string) => { try { @@ -68,11 +71,16 @@ export function useApiExecutor({ onExecute, onValidate }: IApiExecutor): { return { validation: validationString ?? 'null', error: undefined }; } catch (error) { + toast({ + title: '验证失败', + description: get(error, 'message', '验证失败'), + variant: 'destructive', + }); console.log('validate error', error); return { validation: undefined, error: get(error, 'message', 'Validation error') }; } }, - [onValidate], + [onValidate, toast], ); return { execute, validate }; diff --git a/packages/example/components/chains/alephium/example.tsx b/packages/example/components/chains/alephium/example.tsx index f35f0960..a6484377 100644 --- a/packages/example/components/chains/alephium/example.tsx +++ b/packages/example/components/chains/alephium/example.tsx @@ -17,6 +17,7 @@ import * as fetchRetry from 'fetch-retry' import { useState } from 'react'; import { RadioGroup, RadioGroupItem } from '../../ui/radio-group'; import { Label } from '../../ui/label'; +import { useToast } from '../../ui/use-toast'; // 防止限频 const retryFetch = fetchRetry.default(fetch, { @@ -24,12 +25,13 @@ const retryFetch = fetchRetry.default(fetch, { retryDelay: 1000 }) -const nodeUrl = "https://node.testnet.alephium.org" +const nodeUrl = "https://node.mainnet.alephium.org" const nodeProvider = new NodeProvider(nodeUrl, undefined, retryFetch) export function Example() { const wallet = useWallet(); const balance = useBalance(); + const { toast } = useToast(); return ( <> @@ -107,6 +109,13 @@ export function Example() { unsignedTx, signature, }); + if (!txId.txId) { + toast({ + title: '交易提交失败', + description: '请排出网络问题,', + variant: 'destructive', + }); + } return txId.txId; }} /> @@ -122,13 +131,22 @@ export function Example() { const params = JSON.parse(request); const signature = JSON.parse(response).signature; - return verifySignedMessage( + const signed = verifySignedMessage( params.message, params.messageHasher, wallet.account.publicKey, signature, params.signerKeyType, - ).toString(); + ) + + if (!signed) { + toast({ + title: '签名验证失败', + variant: 'destructive', + }); + } + + return signed.toString(); }} /> diff --git a/packages/example/components/chains/alephium/params.ts b/packages/example/components/chains/alephium/params.ts index 8d7fdb76..43d64b8b 100644 --- a/packages/example/components/chains/alephium/params.ts +++ b/packages/example/components/chains/alephium/params.ts @@ -140,7 +140,7 @@ export default { }, { id: 'signMessage-default-sha256', - name: 'Default with Sha256', + name: 'Default with Sha256(硬件不支持)', value: JSON.stringify({ signerAddress: from, signerKeyType: 'default', @@ -150,7 +150,7 @@ export default { }, { id: 'signMessage-default-blake2b', - name: 'Default with Blake2b', + name: 'Default with Blake2b(硬件不支持)', value: JSON.stringify({ signerAddress: from, signerKeyType: 'default', @@ -160,7 +160,7 @@ export default { }, { id: 'signMessage-default-identity', - name: 'Default with Identity', + name: 'Default with Identity(硬件不支持)', value: JSON.stringify({ signerAddress: from, signerKeyType: 'default', @@ -170,7 +170,7 @@ export default { }, { id: 'signMessage-bip340-schnorr-alephium', - name: 'Bip340 Schnorr with Alephium', + name: 'Bip340 Schnorr with Alephium(不支持)', value: JSON.stringify({ signerAddress: from, signerKeyType: 'bip340-schnorr', @@ -180,7 +180,7 @@ export default { }, { id: 'signMessage-bip340-schnorr-sha256', - name: 'Bip340 Schnorr with Sha256', + name: 'Bip340 Schnorr with Sha256(不支持)', value: JSON.stringify({ signerAddress: from, signerKeyType: 'bip340-schnorr', @@ -190,7 +190,7 @@ export default { }, { id: 'signMessage-bip340-schnorr-blake2b', - name: 'Bip340 Schnorr with Blake2b', + name: 'Bip340 Schnorr with Blake2b(不支持)', value: JSON.stringify({ signerAddress: from, signerKeyType: 'bip340-schnorr', @@ -200,7 +200,7 @@ export default { }, { id: 'signMessage-bip340-schnorr-identity', - name: 'Bip340 Schnorr with Identity', + name: 'Bip340 Schnorr with Identity(不支持)', value: JSON.stringify({ signerAddress: from, signerKeyType: 'bip340-schnorr', diff --git a/packages/example/components/chains/ton/example.tsx b/packages/example/components/chains/ton/example.tsx index a84ed4fd..39a608d6 100644 --- a/packages/example/components/chains/ton/example.tsx +++ b/packages/example/components/chains/ton/example.tsx @@ -13,6 +13,7 @@ import InfoLayout from '../../InfoLayout'; import params from './params'; import { TonProofDemoApi } from './TonProofDemoApi'; import { Switch } from '../../ui/switch'; +import { useToast } from '../../ui/use-toast'; const TON_SCAM_DAPP_ENABLE_KEY = 'ton_scam_dapp_enable'; @@ -21,8 +22,9 @@ export function Example() { const rawAddress = useTonAddress(false); const wallet = useTonWallet(); const [tonConnectUI, setOptions] = useTonConnectUI(); + const { toast } = useToast(); - const enable = localStorage.getItem(TON_SCAM_DAPP_ENABLE_KEY); + const scamEnable = localStorage.getItem(TON_SCAM_DAPP_ENABLE_KEY); return ( <> @@ -31,7 +33,7 @@ export function Example() {

伪装欺诈模式

- { + { if (tonConnectUI.connected) { await tonConnectUI?.disconnect(); TonProofDemoApi.reset(); @@ -91,6 +93,20 @@ export function Example() { if (wallet.connectItems?.tonProof && 'proof' in wallet.connectItems.tonProof) { try { const result = await TonProofDemoApi.checkProof(wallet.connectItems.tonProof.proof, wallet.account); + + if (!result) { + toast({ + variant: 'destructive', + title: 'Proof 签名验证失败' + }); + } + if (result && scamEnable) { + toast({ + title: '当前处于伪装欺诈模式,不应该成功连接账户', + variant: 'destructive' + }); + } + return JSON.stringify({ success: result, proof: wallet.connectItems.tonProof.proof @@ -130,7 +146,6 @@ export function Example() { /> { diff --git a/packages/example/components/chains/ton/params.ts b/packages/example/components/chains/ton/params.ts index 724e201a..55a6f6c3 100644 --- a/packages/example/components/chains/ton/params.ts +++ b/packages/example/components/chains/ton/params.ts @@ -58,6 +58,21 @@ export default { { id: 'sendTransaction-native', name: 'Native with body', + value: JSON.stringify({ + validUntil: Math.floor(Date.now() / 1000) + 360, + messages: [ + { + address: to, + amount: '5000000', + payload: 'te6ccsEBAQEADAAMABQAAAAASGVsbG8hCaTc/g==', + }, + ], + }), + }, + { + id: 'sendTransaction-native-stateInit', + name: 'Native with stateInit and body', + description: '带 stateInit 只有未初始化的账户才可以转账成功', value: JSON.stringify({ validUntil: Math.floor(Date.now() / 1000) + 360, messages: [