12-12-2021 11:59
12-12-2021 11:59
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 ?
12-21-2021 02:32
12-21-2021 02:32
Hi,
Does anyone have encountered this issue ? Can someone at fitbit confirm if this lib is still supposed to work ?
12-21-2021 11:59
12-21-2021 11:59
import { subtle } from "crypto"
const buffer = new ArrayBuffer(16);
subtle.digest("SHA-256", buffer).then(hash => { const h = new Uint8Array(hash); });
12-21-2021 12:10
12-21-2021 12:10
Amendment to the above: you can't use device API calls in the companion. The crypto API only exists in the device (watch) API.
12-21-2021 13:56
12-21-2021 13:56
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/
12-21-2021 14:03
12-21-2021 14:03
Sorry; you're right! I didn't notice that.
12-21-2021 14:38 - edited 12-21-2021 14:39
12-21-2021 14:38 - edited 12-21-2021 14:39
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 ...
12-21-2021 14:41
12-21-2021 14:41
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.
12-22-2021 12:16
12-22-2021 12:16
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).