useSignMessage
Hook to sign a message.
Usage
- SignMessage.tsx
- App.tsx
- AuthScript.tsx
- config.ts
import { useAccounts, useSignMessage } from '@kibisis/katavault-react';
function SignMessage() {
const accounts = useAccounts();
const signMessage = useSignMessage();
const handleOnSignMessageClick = (address: string) => () => {
signMessage({
address,
encoding: 'hex',
message: 'hello humie!',
}, {
onError: (error) => {
console.error(error);
},
onSuccess: (result: string) => {
console.log(`signed message with signature: ${result}`);
},
});
};
return (
<div>
{accounts.map(({ address, name }) => (
<div>
<p>Address: {address}</p>
<p>Name: {name ?? '-'}</p>
<button onClick={handleOnSignMessageClick(address)}>Remove</button>
</div>
))}
</div>
);
}
import { KatavaultProvider } from '@kibisis/katavault-react';
import { AuthScript } from './AuthScript';
import { SignMessage } from './SignMessage';
import config from './config';
function App() {
return (
<KatavaultProvider config={config}>
<AuthScript />
<SignMessage />
</KatavaultProvider>
);
}
import { useAuthenticate } from '@kibisis/katavault-react';
import { useEffect } from 'react';
export function AuthScript() {
const {
authenticateWithPasskey,
isAuthenticated,
} = useAuthenticate();
useEffect(() => {
authenticateWithPasskey({
user: {
name: 'Kibi',
username: 'kibi@kibis.is',
},
}, {
onError: (error) => console.error(error),
onSuccess: () => console.log('authenticated successfully!'), // for successful authentication, you can listen to this call back
});
}, []);
useEffect(() => {
console.log('is authenticated', isAuthenticated); // ... and/or check this variable
}, [isAuthenticated]);
return null;
}
import { voi } from '@kibisis/chains';
import { CreateKatavaultParameters } from '@kibisis/katavault-core';
const config: CreateKatavaultParameters = {
chains: [voi],
client: {
name: 'Awesome App!!',
},
};
export default config;
Returns
(params: SignParameters, options?: { onError, onSuccess }) => void
The function can be called with callbacks that are invoked on error/success.
onError
(error: BaseError, params: SignParameters) => void | Promimse<void>
This will be invoked if there was an error.
onSuccess
(result: string | Uint8Array, params: SignParameters) => void | Promimse<void>
This will be invoked with the signature as the result if the message signing was successful.
note
If the params.encoding
are used, the result will be a string in the specified encoding. Otherwise, the raw signature bytes will be returned.