Greetings #Hivers,
I've created a small Keychain helper for myself a while ago and recently converted it to an npm module to make it easier for other devs to integrate Keychain into their websites. Seriously, just install it and you're good to go.
Example Code
import {keychain, isKeychainInstalled, hasKeychainBeenUsed} from '@hiveio/keychain'
const {success, msg, cancel, notInstalled, notActive} = await keychain(window, 'requestTransfer', 'test', 'therealwolf', 5, 'test memo', 'HIVE')
// All good
if(success) {
// do your thing
}
// User didn't cancel, so something must have happened
else if(!cancel) {
if(notActive) {
// alert('Please allow Keychain to access this website')
} else if(notInstalled) {
// alert('Please install Keychain')
} else {
// error happened - check msg
}
}
Explanation
keychain: run a specific keychain functionisKeychainInstalled: checks whether keychain is installed and can be found within the window object.hasKeychainBeenUsed: checks whether a key/value inside the localStorage exists, which means Keychain has been used before, but isn't active. That can happen if extensions have to be activated per site.notInstalled: Return boolean fromkeychainwith same logic asisKeychainInstallednotActive: Return boolean fromkeychainwith same logic ashasKeychainBeenUsedcancel: Return boolean fromkeychainwhich indicates whether the Keychain popup has been closed, thus cancelled. Should be ignored.success: Return boolean fromkeychainwhich indicates whether the transaction was successful. Will be false in case ofcancel.msg: Return string fromkeychainwhich can be an error.
For the normal workflow, you don't need to use isKeychainInstalled or hasKeychainBeenUsed as it's automatically checked when running keychain. These functions mostly matter for UX purposes.
Happy coding!