Skip to content

Commit 4460259

Browse files
authored
Merge pull request #48 from 0xfynnix/main
fix:sign message
2 parents c864411 + 9cc834a commit 4460259

File tree

2 files changed

+273
-101
lines changed

2 files changed

+273
-101
lines changed

src/app/page.tsx

Lines changed: 114 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import {
2929
// import { WalletSelector as AntdWalletSelector } from "@aptos-labs/wallet-adapter-ant-design";
3030
// import { WalletConnector as MuiWalletSelector } from "@aptos-labs/wallet-adapter-mui-design";
3131
import {
32-
AccountInfo,
3332
AptosChangeNetworkOutput,
3433
NetworkInfo,
3534
WalletInfo,
@@ -47,10 +46,11 @@ import { useState, useEffect, useCallback } from "react";
4746

4847
import { NavBar } from "@/components/NavBar";
4948

50-
import { AptosWallet, UserResponseStatus } from "@aptos-labs/wallet-standard";
49+
import { AptosWallet, ReadonlyUint8Array, UserResponseStatus } from "@aptos-labs/wallet-standard";
5150

5251
import { WalletButton } from "@/components/wallet/WalletButton";
5352
import { useAptosWallet } from "@razorlabs/wallet-kit";
53+
import { isValidElement } from "react";
5454

5555
// Add this interface declaration at the top of the file, after the imports
5656
declare global {
@@ -61,6 +61,17 @@ declare global {
6161
}
6262
}
6363

64+
interface AccountInfo {
65+
address: {
66+
data: Uint8Array;
67+
};
68+
publicKey: {
69+
key: {
70+
data: Uint8Array;
71+
};
72+
};
73+
}
74+
6475
// Example of how to register a browser extension wallet plugin.
6576
// Browser extension wallets should call registerWallet once on page load.
6677
// When you click "Connect Wallet", you should see "Example Wallet"
@@ -117,6 +128,7 @@ export default function Home() {
117128
// } = useWallet();
118129
const { connected, disconnect, account, signAndSubmitTransaction, adapter } =
119130
useAptosWallet();
131+
const [accountInfo, setAccountInfo] = useState<AccountInfo| null>(null);
120132

121133
// Move these inside useEffect to only run after connection
122134
// const [adapter, setAdapter] = useState<AptosWallet | null>(null);
@@ -148,6 +160,11 @@ export default function Home() {
148160
url: network.url,
149161
});
150162
}
163+
if (adapter?.account) {
164+
const accountInfo = await adapter.account();
165+
// console.log("accountInfo", accountInfo);
166+
setAccountInfo(accountInfo as unknown as AccountInfo);
167+
}
151168
};
152169

153170
getNetwork();
@@ -249,14 +266,14 @@ export default function Home() {
249266
Send Transaction Example: Transfer
250267
</button>
251268
<WalletConnection
252-
account={
253-
{
254-
address: account?.address || "",
255-
publicKey: account?.publicKey || "",
256-
minKeysRequired: undefined,
257-
ansName: undefined,
258-
} as AccountInfo
259-
}
269+
// account={
270+
// {
271+
// address: account?.address || "",
272+
// publicKey: account?.publicKey || "",
273+
// minKeysRequired: undefined,
274+
// ansName: undefined,
275+
// } as AccountInfo
276+
account={accountInfo}
260277
network={networkInfo}
261278
wallet={
262279
{
@@ -317,13 +334,94 @@ function WalletConnection({ account, network, wallet }: WalletConnectionProps) {
317334
if (network && isAptosNetwork(network)) {
318335
return Object.values<string | undefined>(Network).includes(network?.name);
319336
}
320-
// If the configured network is not an Aptos network, i.e is a custom network
321-
// we resolve it as a valid network name
322337
return true;
323338
};
324339

325-
// TODO: Do a proper check for network change support
326340
const isNetworkChangeSupported = wallet?.name === "Nightly";
341+
342+
// Add function to handle address conversion
343+
const getAddressString = () => {
344+
if (!account?.address) return null;
345+
346+
try {
347+
if (typeof account.address === 'object' && 'data' in account.address) {
348+
const addressData = account.address.data;
349+
return Object.values(addressData)
350+
.map(b => b.toString(16).padStart(2, '0'))
351+
.join('');
352+
}
353+
354+
if (typeof account.address === 'string') {
355+
return account.address;
356+
}
357+
358+
return null;
359+
} catch (error) {
360+
console.error('Error processing address:', error);
361+
return null;
362+
}
363+
};
364+
365+
const getPublicKeyString = () => {
366+
if (!account?.publicKey) return null;
367+
368+
try {
369+
if (typeof account.publicKey === 'object' && 'key' in account.publicKey) {
370+
const keyData = account.publicKey.key.data;
371+
return Object.values(keyData)
372+
.map(b => b.toString(16).padStart(2, '0'))
373+
.join('');
374+
}
375+
376+
return null;
377+
} catch (error) {
378+
console.error('Error processing public key:', error);
379+
return null;
380+
}
381+
};
382+
383+
const address = getAddressString();
384+
const publicKey = getPublicKeyString();
385+
386+
const items = [
387+
{
388+
label: "Address",
389+
value: (
390+
<DisplayValue
391+
value={address ? "0x" + address : "Not Present"}
392+
isCorrect={!!address}
393+
/>
394+
),
395+
},
396+
{
397+
label: "Public key",
398+
value: (
399+
<DisplayValue
400+
value={publicKey ? "0x" + publicKey : "Not Present"}
401+
isCorrect={!!publicKey}
402+
/>
403+
),
404+
},
405+
{
406+
label: "ANS name",
407+
subLabel: "(only if attached)",
408+
value: "Not Present",
409+
},
410+
{
411+
label: "Min keys required",
412+
subLabel: "(only for multisig)",
413+
value: "Not Present",
414+
},
415+
];
416+
417+
// Debug logs
418+
console.log('=== Debug Info ===');
419+
console.log('Account:', JSON.stringify(account, null, 2));
420+
console.log('Network:', JSON.stringify(network, null, 2));
421+
console.log('Wallet:', JSON.stringify(wallet, null, 2));
422+
423+
console.log('Processed address:', address);
424+
console.log('Processed publicKey:', publicKey);
327425

328426
return (
329427
<Card>
@@ -350,7 +448,7 @@ function WalletConnection({ account, network, wallet }: WalletConnectionProps) {
350448
},
351449
{
352450
label: "Name",
353-
value: <p>{wallet?.name ?? "Not Present"}</p>,
451+
value: wallet?.name ?? "Not Present",
354452
},
355453
{
356454
label: "URL",
@@ -373,40 +471,7 @@ function WalletConnection({ account, network, wallet }: WalletConnectionProps) {
373471

374472
<div className="flex flex-col gap-6">
375473
<h4 className="text-lg font-medium">Account Info</h4>
376-
<LabelValueGrid
377-
items={[
378-
{
379-
label: "Address",
380-
value: (
381-
<DisplayValue
382-
value={account?.address ?? "Not Present"}
383-
isCorrect={!!account?.address}
384-
/>
385-
),
386-
},
387-
{
388-
label: "Public key",
389-
value: (
390-
<DisplayValue
391-
value={account?.publicKey.toString() ?? "Not Present"}
392-
isCorrect={!!account?.publicKey}
393-
/>
394-
),
395-
},
396-
{
397-
label: "ANS name",
398-
subLabel: "(only if attached)",
399-
value: <p>{account?.ansName ?? "Not Present"}</p>,
400-
},
401-
{
402-
label: "Min keys required",
403-
subLabel: "(only for multisig)",
404-
value: (
405-
<p>{account?.minKeysRequired?.toString() ?? "Not Present"}</p>
406-
),
407-
},
408-
]}
409-
/>
474+
<LabelValueGrid items={items} />
410475
</div>
411476

412477
<div className="flex flex-col gap-6">
@@ -440,7 +505,7 @@ function WalletConnection({ account, network, wallet }: WalletConnectionProps) {
440505
},
441506
{
442507
label: "Chain ID",
443-
value: <p>{network?.chainId ?? "Not Present"}</p>,
508+
value: network?.chainId ?? "Not Present",
444509
},
445510
]}
446511
/>

0 commit comments

Comments
 (0)