@@ -13,6 +13,8 @@ export default function Home() {
1313 const { sdk, connected, connecting, provider, account } = useSDK ( ) ;
1414 const [ accountInfo , setAccountInfo ] = useState < AccountInfo | null > ( null ) ;
1515 const [ isLoading , setIsLoading ] = useState < boolean > ( false ) ;
16+ const [ signature , setSignature ] = useState < string > ( '' ) ;
17+ const [ signLoading , setSignLoading ] = useState < boolean > ( false ) ;
1618
1719 const getBalance = useCallback ( async ( address : string ) : Promise < string > => {
1820 const balance = await provider ?. request ( {
@@ -38,13 +40,36 @@ export default function Home() {
3840 setIsLoading ( true ) ;
3941 await sdk ?. terminate ( ) ;
4042 setAccountInfo ( null ) ;
43+ setSignature ( '' ) ;
4144 } catch ( err ) {
4245 console . error ( 'Failed to terminate connection:' , err ) ;
4346 } finally {
4447 setIsLoading ( false ) ;
4548 }
4649 } ;
4750
51+ const handlePersonalSign = async ( ) : Promise < void > => {
52+ if ( ! provider || ! account ) return ;
53+
54+ try {
55+ setSignLoading ( true ) ;
56+ const message = `Hello from MetaMask SDK! ${ Date . now ( ) } ` ;
57+ const hexMessage = `0x${ Buffer . from ( message ) . toString ( 'hex' ) } ` ;
58+
59+ const result = await provider . request ( {
60+ method : 'personal_sign' ,
61+ params : [ hexMessage , account ]
62+ } ) ;
63+
64+ setSignature ( result as string ) ;
65+ } catch ( err ) {
66+ console . error ( 'Personal sign failed:' , err ) ;
67+ setSignature ( '' ) ;
68+ } finally {
69+ setSignLoading ( false ) ;
70+ }
71+ } ;
72+
4873 useEffect ( ( ) => {
4974 const updateAccountInfo = async ( ) => {
5075 if ( connected && account ) {
@@ -81,6 +106,22 @@ export default function Home() {
81106 < div className = { styles . address } >
82107 Balance: { accountInfo ?. balance } Wei
83108 </ div >
109+
110+ < button
111+ className = { styles . button }
112+ onClick = { handlePersonalSign }
113+ disabled = { signLoading }
114+ style = { { marginBottom : '10px' , marginTop : '10px' } }
115+ >
116+ { signLoading ? 'Signing...' : 'Personal Sign' }
117+ </ button >
118+
119+ { signature && (
120+ < div className = { styles . address } style = { { wordBreak : 'break-all' } } >
121+ < strong > Signature:</ strong > { signature }
122+ </ div >
123+ ) }
124+
84125 < button
85126 className = { styles . button }
86127 onClick = { terminateConnection }
0 commit comments