Skip to content

Commit

Permalink
chore: optimize ton example (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteZhang1024 authored Sep 9, 2024
1 parent dda482c commit 5f69691
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ export function PresupposeParamsSelector({
);

const showPresupposeParams = presupposeParams && presupposeParams.length > 1;

const presupposeParamsDescription = presupposeParams?.find((param) => param.id === currentPurposeParamId)?.description;

return showPresupposeParams ? (
<div className="flex flex-col gap-2">
<span className="text-base font-medium">预设参数</span>
Expand All @@ -60,6 +63,12 @@ export function PresupposeParamsSelector({
))}
</SelectContent>
</Select>
{
presupposeParamsDescription &&
<p className="px-2 text-base text-muted-foreground">
{presupposeParamsDescription}
</p>
}
</div>
) : null;
}
10 changes: 9 additions & 1 deletion packages/example/components/ApiActuator/useApiExecutor.ts
Original file line number Diff line number Diff line change
@@ -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<any>;
Expand All @@ -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 {
Expand Down Expand Up @@ -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 };
Expand Down
24 changes: 21 additions & 3 deletions packages/example/components/chains/alephium/example.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@ 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, {
retries: 10,
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 (
<>
Expand Down Expand Up @@ -107,6 +109,13 @@ export function Example() {
unsignedTx,
signature,
});
if (!txId.txId) {
toast({
title: '交易提交失败',
description: '请排出网络问题,',
variant: 'destructive',
});
}
return txId.txId;
}}
/>
Expand All @@ -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();
}}
/>
</ApiGroup>
Expand Down
14 changes: 7 additions & 7 deletions packages/example/components/chains/alephium/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand Down
21 changes: 18 additions & 3 deletions packages/example/components/chains/ton/example.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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 (
<>
Expand All @@ -31,7 +33,7 @@ export function Example() {
<InfoLayout title="Base Info">
<div>
<p>伪装欺诈模式</p>
<Switch checked={!!enable} onCheckedChange={async (checked) => {
<Switch checked={!!scamEnable} onCheckedChange={async (checked) => {
if (tonConnectUI.connected) {
await tonConnectUI?.disconnect();
TonProofDemoApi.reset();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -130,7 +146,6 @@ export function Example() {
/>
<ApiPayload
title="sendTransaction"
description="带评论的转账普通 Native"
allowCallWithoutProvider={!!userFriendlyAddress}
presupposeParams={params.sendTransactionWithBody(userFriendlyAddress || '')}
onExecute={async (request: string) => {
Expand Down
15 changes: 15 additions & 0 deletions packages/example/components/chains/ton/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down

0 comments on commit 5f69691

Please sign in to comment.