tokenio / sdk-js

Javascript (Node and Browser) SDK for interacting with the Token System
13 stars 5 forks source link

Improve docs for custom keystore/cryptoengine and add sample #277

Open bkniffler opened 4 years ago

bkniffler commented 4 years ago

The docs for custom engines are a bit hard to find, though it seems like a very important part of integrating tokenIO (if I need to write the keys into a SQL database for example).

https://github.com/tokenio/sdk-js/tree/master/core/src/security/engines

It might help to have a section in the main docs, as a little bonus it might help to have an example of how to implement a custom database.

WenxuanZhou-Token commented 4 years ago

Hi @bkniffler many thanks for the suggestion, I've raised a ticket internally for this

bkniffler commented 4 years ago

Actually I found the UnsecuredFileKeyStore source code to be a good guideline (https://github.com/tokenio/sdk-js/blob/c63122d62cce9eaf87e9c9d7967ab3840aed3bd6/tpp/src/security/engines/UnsecuredFileKeyStore.js).

bkniffler commented 4 years ago

And I'm sure I could easily build my own adapter if @token-io/core was served as transpiled code without esmodules. I wouldn't really want to introduce webpack to my server code just to handle @token-io/core (also mentioned here: https://github.com/tokenio/sdk-js/issues/275).

hookdump commented 4 years ago

Hey @bkniffler did you manage to figure this out? I'm having trouble figuring out how to use the other CryptoEngines that are not UnsecuredFileKeyStore. For example the Memory or Manual one. Can you share any insights if it's not too much to ask?

i.e. if I use Memory or Manual, how do I retrieve the keys that were generated when creating the member? and how do I enter them when instantiating a new Token Client that needs to load them?

bkniffler commented 4 years ago

Let me first say that the last time I used token sdk js, it was in a poor state for that usecase. When I look into commit logs, I don't think much changed. None of my raised issues was fixed after 2 months. Thats a huge shame.

I'm not currently using it, so feel free to use my code or something similar: https://gist.github.com/bkniffler/911f3b82eb69181c64e0befdb96ac223

Basically, I created a keystore that allowed to be handed a write/read method, which will get called whenever keys are written or read. This allows you to define your database write/read logic from above, e.g. from within the engine like shown in my gist.

Don't forget to tell webpack or whatever bundler you use to apply babel transformations to @token-io since the compiled token packages don't expose the necessary stuff (as seen in https://github.com/tokenio/sdk-js/issues/275 which hasn't seen any response whatsover).