NPM module for Hive Keychain

Words
186
Reading
1 min
Listen
Play
6y

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.

https://www.npmjs.com/package/@hiveio/keychain

https://gitlab.syncad.com/hive/keychain-node

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 function

  • isKeychainInstalled: 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 from keychain with same logic as isKeychainInstalled

  • notActive: Return boolean from keychain with same logic as hasKeychainBeenUsed

  • cancel: Return boolean from keychain which indicates whether the Keychain popup has been closed, thus cancelled. Should be ignored.

  • success: Return boolean from keychain which indicates whether the transaction was successful. Will be false in case of cancel.

  • msg: Return string from keychain 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!

NPM module for Hive Keychain | Ecency