Skip to main content

useImportAccountWithPrivateKey

Hook to import an account with a private key.

Usage

import { useAccounts, useImportAccountWithPrivateKey } from '@kibisis/katavault-react';

function Accounts() {
const accounts = useAccounts();
const importAccountWithPrivateKey = useImportAccountWithPrivateKey();
const handleOnAddAccountClick = useCallback(() => {
importAccountWithPrivateKey({
name: 'Personal',
privateKey: window.crypto.randomBytes(32),
}, {
onError: (error) => {
console.error(error);
},
onSuccess: (result: Account) => {
console.log(`added new account ${account.address}`);
},
});
}, [importAccountWithPrivateKey]);

return (
<div>
{accounts.map(({ address, name }) => (
<div>
<p>Address: {address}</p>
<p>Name: {name ?? '-'}</p>
</div>
))}

<button onClick={handleOnAddAccountClick}>Import account</button>
</div>
);
}

Returns

(params: ImportAccountWithPrivateKeyParameters, options?: { onError, onSuccess }) => void

The function can be called with callbacks that are invoked on error/success.

onError

(error: BaseError, params: ImportAccountWithPrivateKeyParameters) => void | Promimse<void>

This will be invoked if there was an error.

onSuccess

(result: Account, params: ImportAccountWithPrivateKeyParameters) => void | Promimse<void>

This will be invoked if the account import was successful.

See also