Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Can't access subltlecrypto

Hi,

 

I'm trying to use the native subltle crypto implementation on the companion side (SDK 4.3). I got the snap from a previous blog article : https://dev.fitbit.com/blog/2019-10-29-announcing-fitbit-os-sdk-4.0/

import * as Crypto from "crypto";

const buffer = new ArrayBuffer(16);

Crypto.subtle.digest("SHA-256", buffer).then(hash => { const h = new Uint8Array(hash); });

yet it fail with :

TypeError: Cannot read property 'digest' of undefined ? at companion/index.js:5,15

 

After analysis, the Crypto Objet don't seem to have any keys ?

 

I've tryed multiple variant of the import but to no avail :

import { Crypto } from "crypto";
import Crypto from "crypto";
import * from "crypto";
import * as Crypto from "crypto";

 

Am I importing it wrong ?

Best Answer
8 REPLIES 8

Hi,

 

Does anyone have encountered this issue ? Can someone at fitbit confirm if this lib is still supposed to work ?

Best Answer
import { subtle } from "crypto"

const buffer = new ArrayBuffer(16);

subtle.digest("SHA-256", buffer).then(hash => { const h = new Uint8Array(hash); });
Peter McLennan
Gondwana Software
Best Answer
0 Votes

Amendment to the above: you can't use device API calls in the companion. The crypto API only exists in the device (watch) API.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

I'll try your snippet.

 

However I saw this in the reference, it should be available in the companion API if I understand correctly ?

https://dev.fitbit.com/build/reference/companion-api/crypto/

 

Best Answer

Sorry; you're right! I didn't notice that.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

I confirm your snippet is working ... on the app side.

On the companion side, same error ...

 

So either the doc is wrong/not up to date, or there is a bug on the companion SDK side

 

I guess for now I will mesage back to the watch to compute my sha256 ... but it's really not a clean way to do it ...

Best Answer
0 Votes

I'm going to go out on a limb and say that subtle isn't being exported from the companion's crypto. I can import getRandomValues in the companion, but not subtle using the same syntax. subtle might have been accidentally omitted when adapting the module to the new rollup export requirements.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

The thinking by some people who are smarter than me is that subtle is only implemented in the app API; ie, the documentation is wrong. (This surprises me because I would have thought it to be more useful in the companion than in the app.)

An alternative to passing files to the watch might be to try to add a third-party npm crypto module to your project (assuming you use CLI to build).

Peter McLennan
Gondwana Software
Best Answer
0 Votes