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 fromkeychain
with same logic asisKeychainInstalled
notActive
: Return boolean fromkeychain
with same logic ashasKeychainBeenUsed
cancel
: Return boolean fromkeychain
which indicates whether the Keychain popup has been closed, thus cancelled. Should be ignored.success
: Return boolean fromkeychain
which indicates whether the transaction was successful. Will be false in case ofcancel
.msg
: Return string fromkeychain
which 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!
Good work! I've added a link to the module to Keychain documentation.
An idea on my mind on a dapp - offersworld.com, that would list all offers from local shops, validity, and will disappear after validity. So the dapp rewards hive to consumers, who buy through this ( some kind of authentication to make sure, they used the code from the site when they bought physically). So let's say, if we continuously reward consumers, then publishers ( local shops ) will be more and then adds can be built ( in fact designed from beginning). And offers may be shown based on user local.
What do you think ?
This is super helpful
work like this is why you are one of my fav Witnesses.
sending it over to our front-end devs right now.
Grateful for the evaluations of my publications. Successes in your Healing work. With my 68 years of age I have learned a lot in hive. Hugs from Omar Gustavo Rojas Ortiz from Cumaná-Venezuela.
Congratulations @therealwolf! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :
You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word
STOP
Do not miss the last post from @hivebuzz: