virgil-e3kit-js icon indicating copy to clipboard operation
virgil-e3kit-js copied to clipboard

Offline intialization support

Open camhart opened this issue 4 years ago • 1 comments

Is your feature request related to a problem? Please describe. I'm using e3kit in situations where the initial setup requires an active internet connection, but the devices may go offline periodically (including reboots during offline use) and still need to encrypt data (that will be sent once the internet connection is restored). I'm caching all the card values I need to encrypt data for currently to allow me to do this, however e3's initializer fails because it requires a successful token callback in order to obtain the identity to pass to the EThree constructor.

Describe the solution you'd like Would it be possible to add an optional identity argument that, when present, would be used to allow EThree to be initialized offline (or in any situation where the token callback function fails)?

Something like this (hasn't been tested):

//eThree.ts from e3kit-node

    static async initialize(
        getToken: () => Promise<string>,
        options: EThreeInitializeOptions = {},
        cachedIdentity?: string
    ): Promise<EThree> {
        const cryptoOptions = EThree.getFoundationLibraryOptions(options);
        const pythiaOptions = EThree.getPythiaLibraryOptions(options);
        await Promise.all([initCrypto(cryptoOptions), initPythia(pythiaOptions)]);

        if (typeof getToken !== 'function') {
            throw new TypeError(
                `EThree.initialize expects a function that returns Virgil JWT, got ${typeof getToken}`,
            );
        }

        const opts = {
            accessTokenProvider: new CachingJwtProvider(getToken),
            ...options,
        };
        let identity = cachedIdentity;
        try {
           const token = await opts.accessTokenProvider.getToken({
                service: 'cards',
                operation: '',
            });
            identity = token.identity();
        } catch(e) {
        }
        
        return new EThree(identity, opts);
    }

camhart avatar Jun 25 '20 18:06 camhart

I created my own constructor following what I suggested above, and it appears to work. So I'm happy with it. I'll leave this issue open and let you decide if you want to close it now or later once the change is implemented.

camhart avatar Jun 25 '20 18:06 camhart