@d4l/collect-lib 1.42.0 → 1.44.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +335 -12
- package/dist/index.d.ts +335 -12
- package/dist/index.js +1357 -250
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1371 -264
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -3
package/dist/index.mjs
CHANGED
|
@@ -40,6 +40,7 @@ function toDeviceConfig(devices) {
|
|
|
40
40
|
case "FITBIT":
|
|
41
41
|
case "GOOGLE_HEALTH":
|
|
42
42
|
case "OURA":
|
|
43
|
+
case "GARMIN":
|
|
43
44
|
return {
|
|
44
45
|
type: "api",
|
|
45
46
|
device: device.type,
|
|
@@ -87,6 +88,7 @@ var DEVICE_KEY_TO_NAME = {
|
|
|
87
88
|
FITBIT: { shortName: "fitbit", fullName: "fitbit" },
|
|
88
89
|
GOOGLE_HEALTH: { shortName: "Google Health", fullName: "Google Health" },
|
|
89
90
|
OURA: { shortName: "Oura", fullName: "Oura Ring" },
|
|
91
|
+
GARMIN: { shortName: "Garmin", fullName: "Garmin" },
|
|
90
92
|
DEXCOM: { shortName: "Dexcom", fullName: "Dexcom" },
|
|
91
93
|
dexcom_g4: { shortName: "Dexcom G4", fullName: "Dexcom G4" },
|
|
92
94
|
dexcom_g5: { shortName: "Dexcom G5", fullName: "Dexcom G5" },
|
|
@@ -424,6 +426,19 @@ var OURA_SIGNAL_TYPE_ANNOTATIONS = {
|
|
|
424
426
|
batteryLevel: OURA_BATTERY_LEVEL_ANNOTATION
|
|
425
427
|
};
|
|
426
428
|
|
|
429
|
+
// src/devices/annotations/garmin.ts
|
|
430
|
+
var GARMIN_SIGNAL_TYPE_ANNOTATIONS = {
|
|
431
|
+
heartRate: FITBIT_HEART_RATE_ANNOTATION,
|
|
432
|
+
hrv: FITBIT_HEART_RATE_VARIABILITY_ANNOTATION,
|
|
433
|
+
spo2: FITBIT_SPO2_ANNOTATION,
|
|
434
|
+
sleep: FITBIT_SLEEP_ANNOTATION,
|
|
435
|
+
steps: FITBIT_STEPS_ANNOTATION,
|
|
436
|
+
calories: FITBIT_CALORIES_ANNOTATION,
|
|
437
|
+
respiratoryRate: FITBIT_BREATHING_RATE_ANNOTATION,
|
|
438
|
+
skinTemperature: FITBIT_SKIN_TEMPERATURE_ANNOTATION,
|
|
439
|
+
stress: OURA_STRESS_ANNOTATION
|
|
440
|
+
};
|
|
441
|
+
|
|
427
442
|
// src/devices/annotations/movella.ts
|
|
428
443
|
var DEVICE_MOVELLA_ANNOTATION_ITEM_TIMESTAMP = {
|
|
429
444
|
type: "timestamp",
|
|
@@ -892,7 +907,7 @@ var APPLE_HEALTH_ANNOTATION_ITEM_STEP_COUNT = {
|
|
|
892
907
|
format: "signedInt",
|
|
893
908
|
littleEndian: true
|
|
894
909
|
};
|
|
895
|
-
var
|
|
910
|
+
var APPLE_HEALTH_ANNOTATION_STEP_COUNT = [
|
|
896
911
|
APPLE_HEALTH_ANNOTATION_ITEM_STEP_COUNT,
|
|
897
912
|
DEVICE_ANNOTATION_ITEM_DATA_SOURCE
|
|
898
913
|
];
|
|
@@ -902,7 +917,7 @@ var APPLE_HEALTH_ANNOTATION_ITEM_WALKING_RUNNING_DISTANCE = {
|
|
|
902
917
|
format: "float32",
|
|
903
918
|
littleEndian: true
|
|
904
919
|
};
|
|
905
|
-
var
|
|
920
|
+
var APPLE_HEALTH_ANNOTATION_WALKING_RUNNING_DISTANCE = [
|
|
906
921
|
APPLE_HEALTH_ANNOTATION_ITEM_WALKING_RUNNING_DISTANCE,
|
|
907
922
|
DEVICE_ANNOTATION_ITEM_DATA_SOURCE
|
|
908
923
|
];
|
|
@@ -912,7 +927,7 @@ var APPLE_HEALTH_ANNOTATION_ITEM_ACTIVE_ENERGY = {
|
|
|
912
927
|
format: "float32",
|
|
913
928
|
littleEndian: true
|
|
914
929
|
};
|
|
915
|
-
var
|
|
930
|
+
var APPLE_HEALTH_ANNOTATION_ACTIVE_ENERGY = [
|
|
916
931
|
APPLE_HEALTH_ANNOTATION_ITEM_ACTIVE_ENERGY,
|
|
917
932
|
DEVICE_ANNOTATION_ITEM_DATA_SOURCE
|
|
918
933
|
];
|
|
@@ -935,16 +950,88 @@ var APPLE_HEALTH_ANNOTATION_ITEM_SLEEP_DURATION = {
|
|
|
935
950
|
littleEndian: true,
|
|
936
951
|
unit: "s"
|
|
937
952
|
};
|
|
938
|
-
var
|
|
953
|
+
var APPLE_HEALTH_ANNOTATION_SLEEP_ANALYSIS = [
|
|
939
954
|
APPLE_HEALTH_ANNOTATION_ITEM_SLEEP_STATE,
|
|
940
955
|
APPLE_HEALTH_ANNOTATION_ITEM_SLEEP_DURATION,
|
|
941
956
|
DEVICE_ANNOTATION_ITEM_DATA_SOURCE
|
|
942
957
|
];
|
|
958
|
+
var APPLE_HEALTH_ANNOTATION_ITEM_HEART_RATE = {
|
|
959
|
+
type: "heartRate",
|
|
960
|
+
size: 1,
|
|
961
|
+
format: "unsignedInt",
|
|
962
|
+
littleEndian: true,
|
|
963
|
+
unit: "bpm"
|
|
964
|
+
};
|
|
965
|
+
var APPLE_HEALTH_ANNOTATION_HEART_RATE = [
|
|
966
|
+
APPLE_HEALTH_ANNOTATION_ITEM_HEART_RATE,
|
|
967
|
+
DEVICE_ANNOTATION_ITEM_DATA_SOURCE
|
|
968
|
+
];
|
|
969
|
+
var APPLE_HEALTH_ANNOTATION_ITEM_RESTING_HEART_RATE = {
|
|
970
|
+
type: "restingHeartRate",
|
|
971
|
+
size: 1,
|
|
972
|
+
format: "unsignedInt",
|
|
973
|
+
littleEndian: true,
|
|
974
|
+
unit: "bpm"
|
|
975
|
+
};
|
|
976
|
+
var APPLE_HEALTH_ANNOTATION_RESTING_HEART_RATE = [
|
|
977
|
+
APPLE_HEALTH_ANNOTATION_ITEM_RESTING_HEART_RATE,
|
|
978
|
+
DEVICE_ANNOTATION_ITEM_DATA_SOURCE
|
|
979
|
+
];
|
|
980
|
+
var APPLE_HEALTH_ANNOTATION_ITEM_HRV = {
|
|
981
|
+
type: "hrv",
|
|
982
|
+
size: 4,
|
|
983
|
+
format: "float32",
|
|
984
|
+
littleEndian: true,
|
|
985
|
+
unit: "ms"
|
|
986
|
+
};
|
|
987
|
+
var APPLE_HEALTH_ANNOTATION_HRV = [
|
|
988
|
+
APPLE_HEALTH_ANNOTATION_ITEM_HRV,
|
|
989
|
+
DEVICE_ANNOTATION_ITEM_DATA_SOURCE
|
|
990
|
+
];
|
|
991
|
+
var APPLE_HEALTH_ANNOTATION_ITEM_RESPIRATORY_RATE = {
|
|
992
|
+
type: "respiratoryRate",
|
|
993
|
+
size: 4,
|
|
994
|
+
format: "float32",
|
|
995
|
+
littleEndian: true,
|
|
996
|
+
unit: "breaths/min"
|
|
997
|
+
};
|
|
998
|
+
var APPLE_HEALTH_ANNOTATION_RESPIRATORY_RATE = [
|
|
999
|
+
APPLE_HEALTH_ANNOTATION_ITEM_RESPIRATORY_RATE,
|
|
1000
|
+
DEVICE_ANNOTATION_ITEM_DATA_SOURCE
|
|
1001
|
+
];
|
|
1002
|
+
var APPLE_HEALTH_ANNOTATION_ITEM_SPO2 = {
|
|
1003
|
+
type: "spo2",
|
|
1004
|
+
size: 4,
|
|
1005
|
+
format: "float32",
|
|
1006
|
+
littleEndian: true,
|
|
1007
|
+
unit: "%"
|
|
1008
|
+
};
|
|
1009
|
+
var APPLE_HEALTH_ANNOTATION_SPO2 = [
|
|
1010
|
+
APPLE_HEALTH_ANNOTATION_ITEM_SPO2,
|
|
1011
|
+
DEVICE_ANNOTATION_ITEM_DATA_SOURCE
|
|
1012
|
+
];
|
|
1013
|
+
var APPLE_HEALTH_ANNOTATION_ITEM_SKIN_TEMPERATURE = {
|
|
1014
|
+
type: "skinTemperature",
|
|
1015
|
+
size: 4,
|
|
1016
|
+
format: "float32",
|
|
1017
|
+
littleEndian: true,
|
|
1018
|
+
unit: "\u02DAC"
|
|
1019
|
+
};
|
|
1020
|
+
var APPLE_HEALTH_ANNOTATION_SKIN_TEMPERATURE = [
|
|
1021
|
+
APPLE_HEALTH_ANNOTATION_ITEM_SKIN_TEMPERATURE,
|
|
1022
|
+
DEVICE_ANNOTATION_ITEM_DATA_SOURCE
|
|
1023
|
+
];
|
|
943
1024
|
var APPLE_HEALTH_SIGNAL_TYPE_ANNOTATIONS = {
|
|
944
|
-
stepCount:
|
|
945
|
-
walkingAndRunningDistance:
|
|
946
|
-
activeEnergy:
|
|
947
|
-
sleepAnalysis:
|
|
1025
|
+
stepCount: APPLE_HEALTH_ANNOTATION_STEP_COUNT,
|
|
1026
|
+
walkingAndRunningDistance: APPLE_HEALTH_ANNOTATION_WALKING_RUNNING_DISTANCE,
|
|
1027
|
+
activeEnergy: APPLE_HEALTH_ANNOTATION_ACTIVE_ENERGY,
|
|
1028
|
+
sleepAnalysis: APPLE_HEALTH_ANNOTATION_SLEEP_ANALYSIS,
|
|
1029
|
+
heartRate: APPLE_HEALTH_ANNOTATION_HEART_RATE,
|
|
1030
|
+
restingHeartRate: APPLE_HEALTH_ANNOTATION_RESTING_HEART_RATE,
|
|
1031
|
+
hrv: APPLE_HEALTH_ANNOTATION_HRV,
|
|
1032
|
+
respiratoryRate: APPLE_HEALTH_ANNOTATION_RESPIRATORY_RATE,
|
|
1033
|
+
spo2: APPLE_HEALTH_ANNOTATION_SPO2,
|
|
1034
|
+
skinTemperature: APPLE_HEALTH_ANNOTATION_SKIN_TEMPERATURE
|
|
948
1035
|
};
|
|
949
1036
|
|
|
950
1037
|
// src/devices/annotations/healthConnect.ts
|
|
@@ -2047,103 +2134,15 @@ var MonthFuncs = class extends TimeFuncs {
|
|
|
2047
2134
|
}
|
|
2048
2135
|
};
|
|
2049
2136
|
|
|
2137
|
+
// src/collector/sdk.ts
|
|
2138
|
+
import { Buffer as Buffer8 } from "buffer/";
|
|
2139
|
+
|
|
2050
2140
|
// src/donation/keyHandler.ts
|
|
2051
2141
|
import * as secpUtils from "@noble/curves/abstract/utils";
|
|
2052
2142
|
import { secp256k1 } from "@noble/curves/secp256k1";
|
|
2053
2143
|
import * as bip39 from "@scure/bip39";
|
|
2054
|
-
import { Buffer as Buffer8 } from "buffer/";
|
|
2055
|
-
import * as forge from "node-forge";
|
|
2056
|
-
|
|
2057
|
-
// src/helper/converter.ts
|
|
2058
2144
|
import { Buffer as Buffer7 } from "buffer/";
|
|
2059
|
-
|
|
2060
|
-
return new Uint8Array(binaryString.split("").map((c) => c.charCodeAt(0)));
|
|
2061
|
-
};
|
|
2062
|
-
var uint8ArrayToBinaryString = (array) => {
|
|
2063
|
-
return String.fromCharCode.apply(null, array);
|
|
2064
|
-
};
|
|
2065
|
-
var utf8ToUint8Array = (string) => {
|
|
2066
|
-
const result = [];
|
|
2067
|
-
const size = string.length;
|
|
2068
|
-
for (let index = 0; index < size; index++) {
|
|
2069
|
-
let point = string.charCodeAt(index);
|
|
2070
|
-
if (point >= 55296 && point <= 56319 && size > index + 1) {
|
|
2071
|
-
const second = string.charCodeAt(index + 1);
|
|
2072
|
-
if (second >= 56320 && second <= 57343) {
|
|
2073
|
-
point = (point - 55296) * 1024 + second - 56320 + 65536;
|
|
2074
|
-
index += 1;
|
|
2075
|
-
}
|
|
2076
|
-
}
|
|
2077
|
-
if (point < 128) {
|
|
2078
|
-
result.push(point);
|
|
2079
|
-
continue;
|
|
2080
|
-
}
|
|
2081
|
-
if (point < 2048) {
|
|
2082
|
-
result.push(point >> 6 | 192);
|
|
2083
|
-
result.push(point & 63 | 128);
|
|
2084
|
-
continue;
|
|
2085
|
-
}
|
|
2086
|
-
if (point < 55296 || point >= 57344 && point < 65536) {
|
|
2087
|
-
result.push(point >> 12 | 224);
|
|
2088
|
-
result.push(point >> 6 & 63 | 128);
|
|
2089
|
-
result.push(point & 63 | 128);
|
|
2090
|
-
continue;
|
|
2091
|
-
}
|
|
2092
|
-
if (point >= 65536 && point <= 1114111) {
|
|
2093
|
-
result.push(point >> 18 | 240);
|
|
2094
|
-
result.push(point >> 12 & 63 | 128);
|
|
2095
|
-
result.push(point >> 6 & 63 | 128);
|
|
2096
|
-
result.push(point & 63 | 128);
|
|
2097
|
-
continue;
|
|
2098
|
-
}
|
|
2099
|
-
result.push(239, 191, 189);
|
|
2100
|
-
}
|
|
2101
|
-
return new Uint8Array(result);
|
|
2102
|
-
};
|
|
2103
|
-
var WebConverter = class {
|
|
2104
|
-
constructor() {
|
|
2105
|
-
this.arrayBufferToBase64 = (buffer) => {
|
|
2106
|
-
let binary = "";
|
|
2107
|
-
const bytes = new Uint8Array(buffer);
|
|
2108
|
-
const len = bytes.byteLength;
|
|
2109
|
-
for (let i = 0; i < len; i++) {
|
|
2110
|
-
binary += String.fromCharCode(bytes[i]);
|
|
2111
|
-
}
|
|
2112
|
-
return btoa(binary);
|
|
2113
|
-
};
|
|
2114
|
-
this.base64ToArrayBuffer = (string) => {
|
|
2115
|
-
const base64 = atob(string);
|
|
2116
|
-
const buffer = new ArrayBuffer(base64.length);
|
|
2117
|
-
const bufferView = new Uint8Array(buffer);
|
|
2118
|
-
for (let i = 0, base64Len = base64.length; i < base64Len; i++) {
|
|
2119
|
-
bufferView[i] = base64.charCodeAt(i);
|
|
2120
|
-
}
|
|
2121
|
-
return buffer;
|
|
2122
|
-
};
|
|
2123
|
-
// convert Uint8Array to base64 string
|
|
2124
|
-
this.uint8ArrayToBase64 = (array) => {
|
|
2125
|
-
return btoa(String.fromCharCode(...array));
|
|
2126
|
-
};
|
|
2127
|
-
/**
|
|
2128
|
-
* Convert a base64 encoded string into Uint8Array
|
|
2129
|
-
* @param string The string that needs to be converted into an Uint8Array
|
|
2130
|
-
* @returns A Uint8Array
|
|
2131
|
-
*/
|
|
2132
|
-
this.base64ToUint8Array = (string) => new Uint8Array(this.base64ToArrayBuffer(string));
|
|
2133
|
-
/**
|
|
2134
|
-
* Convert a base64 encoded Uint8Array into a UTF-8 encoded string
|
|
2135
|
-
* @param uint The base64 encoded Uint8Array
|
|
2136
|
-
* @returns The UTF-8 encoded string
|
|
2137
|
-
*/
|
|
2138
|
-
this.uint8ArrayToUTF8 = (uint) => {
|
|
2139
|
-
const decoder = new TextDecoder();
|
|
2140
|
-
const str = decoder.decode(uint);
|
|
2141
|
-
return Buffer7.from(str, "base64").toString("utf8");
|
|
2142
|
-
};
|
|
2143
|
-
this.base64ToHex = (base64String) => Buffer7.from(base64String, "base64").toString("hex");
|
|
2144
|
-
this.hexToBase64 = (hexString) => Buffer7.from(hexString, "hex").toString("base64");
|
|
2145
|
-
}
|
|
2146
|
-
};
|
|
2145
|
+
import * as forge from "node-forge";
|
|
2147
2146
|
|
|
2148
2147
|
// src/helper/wordlists.ts
|
|
2149
2148
|
import { wordlist } from "@scure/bip39/wordlists/english";
|
|
@@ -4259,12 +4258,12 @@ var KeyHandler = class {
|
|
|
4259
4258
|
return "";
|
|
4260
4259
|
};
|
|
4261
4260
|
this.extendPublicKey = (publicKey) => {
|
|
4262
|
-
let key =
|
|
4261
|
+
let key = Buffer7.from(publicKey);
|
|
4263
4262
|
if (key.length !== 65) {
|
|
4264
4263
|
throw new Error("Invalid public key length");
|
|
4265
4264
|
}
|
|
4266
4265
|
key = key.slice(1);
|
|
4267
|
-
const header =
|
|
4266
|
+
const header = Buffer7.from([
|
|
4268
4267
|
48,
|
|
4269
4268
|
86,
|
|
4270
4269
|
48,
|
|
@@ -4290,7 +4289,7 @@ var KeyHandler = class {
|
|
|
4290
4289
|
0,
|
|
4291
4290
|
4
|
|
4292
4291
|
]);
|
|
4293
|
-
return
|
|
4292
|
+
return Buffer7.concat([header, key]).toString("base64");
|
|
4294
4293
|
};
|
|
4295
4294
|
/**
|
|
4296
4295
|
* Hash a message with SHA-256
|
|
@@ -4309,8 +4308,8 @@ var KeyHandler = class {
|
|
|
4309
4308
|
*/
|
|
4310
4309
|
this.sign = async (message, priv) => {
|
|
4311
4310
|
try {
|
|
4312
|
-
const
|
|
4313
|
-
return secp256k1.sign(
|
|
4311
|
+
const sha2563 = this.hash(message);
|
|
4312
|
+
return secp256k1.sign(sha2563, secpUtils.hexToBytes(this.converter.base64ToHex(priv))).toCompactRawBytes();
|
|
4314
4313
|
} catch (e) {
|
|
4315
4314
|
console.error(e);
|
|
4316
4315
|
throw new Error("could_not_sign" /* COULD_NOT_SIGN */);
|
|
@@ -4324,13 +4323,30 @@ var KeyHandler = class {
|
|
|
4324
4323
|
* @returns True, if the signature is valid. Otherwise, false.
|
|
4325
4324
|
*/
|
|
4326
4325
|
this.verify = async (message, signature, pub) => {
|
|
4327
|
-
const
|
|
4326
|
+
const sha2563 = this.hash(message);
|
|
4328
4327
|
return secp256k1.verify(
|
|
4329
4328
|
signature,
|
|
4330
|
-
|
|
4331
|
-
this.converter.base64ToUint8Array(pub)
|
|
4329
|
+
secpUtils.hexToBytes(sha2563),
|
|
4330
|
+
this.rawPublicKey(this.converter.base64ToUint8Array(pub))
|
|
4332
4331
|
);
|
|
4333
4332
|
};
|
|
4333
|
+
/**
|
|
4334
|
+
* Reduce a donor public key to the raw uncompressed point noble expects — the inverse
|
|
4335
|
+
* of extendPublicKey. The SPKI wrapper ends with the 0x04 point prefix, so the raw key
|
|
4336
|
+
* is simply its last 65 bytes. Already-raw keys are passed through.
|
|
4337
|
+
* @param publicKey SPKI-wrapped (as produced by extendPublicKey) or raw uncompressed key
|
|
4338
|
+
* @returns The 65-byte uncompressed point (0x04 || X || Y)
|
|
4339
|
+
*/
|
|
4340
|
+
this.rawPublicKey = (publicKey) => {
|
|
4341
|
+
const UNCOMPRESSED_LENGTH = 65;
|
|
4342
|
+
if (publicKey.length === UNCOMPRESSED_LENGTH) {
|
|
4343
|
+
return publicKey;
|
|
4344
|
+
}
|
|
4345
|
+
if (publicKey.length < UNCOMPRESSED_LENGTH) {
|
|
4346
|
+
throw new Error(`Invalid public key length: ${publicKey.length}`);
|
|
4347
|
+
}
|
|
4348
|
+
return publicKey.subarray(publicKey.length - UNCOMPRESSED_LENGTH);
|
|
4349
|
+
};
|
|
4334
4350
|
this.converter = converter;
|
|
4335
4351
|
this.crypto = crypto;
|
|
4336
4352
|
}
|
|
@@ -4345,7 +4361,7 @@ var KeyHandler = class {
|
|
|
4345
4361
|
*/
|
|
4346
4362
|
async deriveProgramKeys(recoveryKey, programName) {
|
|
4347
4363
|
let privateProgramKey = await this.crypto.deriveEncryptionKey(recoveryKey, programName);
|
|
4348
|
-
while (!secp256k1.utils.
|
|
4364
|
+
while (!secp256k1.utils.isValidSecretKey(privateProgramKey)) {
|
|
4349
4365
|
privateProgramKey = await this.crypto.deriveEncryptionKey(privateProgramKey, programName);
|
|
4350
4366
|
}
|
|
4351
4367
|
const publicProgramKey = this.extendPublicKey(secp256k1.getPublicKey(privateProgramKey, false));
|
|
@@ -4356,11 +4372,31 @@ var KeyHandler = class {
|
|
|
4356
4372
|
recovery: this.converter.uint8ArrayToBase64(recoveryKey32)
|
|
4357
4373
|
};
|
|
4358
4374
|
}
|
|
4375
|
+
/**
|
|
4376
|
+
* Derive the dedicated encryption keypair (secp256k1) as a hierarchical child of the
|
|
4377
|
+
* donor identity, reusing the same one-way KDF as the program key. The public key is
|
|
4378
|
+
* shared with the data-collector (SPKI), which ECIES-encrypts buffered payloads to it;
|
|
4379
|
+
* the private key feeds HybridEncrypter.decryptV4. Deterministic, so it survives reinstall.
|
|
4380
|
+
* @param programPrivateKey The donor identity's base64-encoded program private key (keys.priv)
|
|
4381
|
+
* @returns publicKey (SPKI, base64) and privateKey (secp256k1 scalar, base64 — the same
|
|
4382
|
+
* encoding as the program private key)
|
|
4383
|
+
*/
|
|
4384
|
+
async deriveEncryptionKeypair(programPrivateKey) {
|
|
4385
|
+
const seed = secpUtils.hexToBytes(this.converter.base64ToHex(programPrivateKey));
|
|
4386
|
+
let encryptionPrivateKey = await this.crypto.deriveEncryptionKey(seed, "encryption");
|
|
4387
|
+
while (!secp256k1.utils.isValidSecretKey(encryptionPrivateKey)) {
|
|
4388
|
+
encryptionPrivateKey = await this.crypto.deriveEncryptionKey(encryptionPrivateKey, "encryption");
|
|
4389
|
+
}
|
|
4390
|
+
const encryptionPublicKey = this.extendPublicKey(
|
|
4391
|
+
secp256k1.getPublicKey(encryptionPrivateKey, false)
|
|
4392
|
+
);
|
|
4393
|
+
return {
|
|
4394
|
+
publicKey: encryptionPublicKey,
|
|
4395
|
+
privateKey: this.converter.uint8ArrayToBase64(encryptionPrivateKey)
|
|
4396
|
+
};
|
|
4397
|
+
}
|
|
4359
4398
|
};
|
|
4360
4399
|
|
|
4361
|
-
// src/donation/sdk.ts
|
|
4362
|
-
import { Buffer as Buffer10 } from "buffer/";
|
|
4363
|
-
|
|
4364
4400
|
// src/helper/requester.ts
|
|
4365
4401
|
var defaultAccessToken = () => Promise.resolve("");
|
|
4366
4402
|
var Requester = class {
|
|
@@ -4459,152 +4495,689 @@ var Requester = class {
|
|
|
4459
4495
|
};
|
|
4460
4496
|
var requester_default = Requester;
|
|
4461
4497
|
|
|
4462
|
-
// src/
|
|
4463
|
-
|
|
4464
|
-
|
|
4465
|
-
|
|
4466
|
-
|
|
4498
|
+
// src/collector/sdk.ts
|
|
4499
|
+
var buildSelfSignedAuthToken = async (keyHandler, converter, pub, priv, token, selfSignSecret) => {
|
|
4500
|
+
const message = JSON.stringify({ token, donorID: pub });
|
|
4501
|
+
const signature = await keyHandler.sign(message + selfSignSecret, priv);
|
|
4502
|
+
const signedMessage = JSON.stringify({
|
|
4503
|
+
messageJSON: message,
|
|
4504
|
+
signature: converter.uint8ArrayToBase64(signature)
|
|
4505
|
+
});
|
|
4506
|
+
return Buffer8.from(signedMessage).toString("base64");
|
|
4507
|
+
};
|
|
4508
|
+
var CollectorClient = class {
|
|
4509
|
+
/**
|
|
4510
|
+
* @param selfSignSecret SELF_SIGN_SECRET shared with the collector (and the dispatcher)
|
|
4511
|
+
* @param collectorURL Base URL including the API prefix, e.g. https://collect.dev.d4l-dev.de/api/collector/v1
|
|
4512
|
+
*/
|
|
4513
|
+
constructor(selfSignSecret, collectorURL, converter, crypto) {
|
|
4514
|
+
this.selfSignSecret = selfSignSecret;
|
|
4515
|
+
this.api = new requester_default(collectorURL, () => Promise.resolve(""), false);
|
|
4516
|
+
this.keyHandler = new KeyHandler(converter, crypto);
|
|
4517
|
+
this.converter = converter;
|
|
4467
4518
|
}
|
|
4468
|
-
|
|
4469
|
-
|
|
4470
|
-
|
|
4519
|
+
async authenticatedApi(donorIdentity) {
|
|
4520
|
+
const getAuthToken = async () => {
|
|
4521
|
+
const [token] = await this.api.get({ uri: "/token" });
|
|
4522
|
+
return buildSelfSignedAuthToken(
|
|
4523
|
+
this.keyHandler,
|
|
4524
|
+
this.converter,
|
|
4525
|
+
donorIdentity.keys.pub,
|
|
4526
|
+
donorIdentity.keys.priv,
|
|
4527
|
+
token,
|
|
4528
|
+
this.selfSignSecret
|
|
4529
|
+
);
|
|
4530
|
+
};
|
|
4531
|
+
return new requester_default(this.api.baseUrl, getAuthToken, true);
|
|
4471
4532
|
}
|
|
4472
|
-
|
|
4473
|
-
|
|
4533
|
+
/**
|
|
4534
|
+
* The collector's donor endpoints are study-scoped in the URL (mirroring the
|
|
4535
|
+
* data-dispatcher); the study id is the identity's scope (the program name).
|
|
4536
|
+
*/
|
|
4537
|
+
studySegment(donorIdentity) {
|
|
4538
|
+
if (!donorIdentity.scope) {
|
|
4539
|
+
throw new Error("donor identity has no study scope; cannot address the collector");
|
|
4540
|
+
}
|
|
4541
|
+
return encodeURIComponent(donorIdentity.scope);
|
|
4542
|
+
}
|
|
4543
|
+
/** Register the donor's Garmin connection and start ingestion. */
|
|
4544
|
+
async connect(donorIdentity, params) {
|
|
4545
|
+
const api = await this.authenticatedApi(donorIdentity);
|
|
4546
|
+
const [response] = await api.put({
|
|
4547
|
+
uri: `/connect/${this.studySegment(donorIdentity)}`,
|
|
4548
|
+
body: JSON.stringify(params)
|
|
4549
|
+
});
|
|
4550
|
+
return response;
|
|
4474
4551
|
}
|
|
4475
|
-
|
|
4476
|
-
|
|
4477
|
-
|
|
4478
|
-
|
|
4479
|
-
|
|
4480
|
-
|
|
4481
|
-
|
|
4552
|
+
/** Pull buffered (encrypted) items with sequence number greater than `since`. */
|
|
4553
|
+
async sync(donorIdentity, since = 0) {
|
|
4554
|
+
const api = await this.authenticatedApi(donorIdentity);
|
|
4555
|
+
const [response] = await api.get({
|
|
4556
|
+
uri: `/sync/${this.studySegment(donorIdentity)}?since=${since}`
|
|
4557
|
+
});
|
|
4558
|
+
return response;
|
|
4559
|
+
}
|
|
4560
|
+
/** Acknowledge items up to (and including) `cursor`; the collector prunes them. */
|
|
4561
|
+
async ack(donorIdentity, cursor) {
|
|
4562
|
+
const api = await this.authenticatedApi(donorIdentity);
|
|
4563
|
+
await api.post({
|
|
4564
|
+
uri: `/sync/${this.studySegment(donorIdentity)}/ack`,
|
|
4565
|
+
body: JSON.stringify({ cursor })
|
|
4482
4566
|
});
|
|
4483
4567
|
}
|
|
4484
|
-
|
|
4485
|
-
|
|
4486
|
-
|
|
4487
|
-
|
|
4488
|
-
if (programBlur && programBlur.researchSubject) {
|
|
4489
|
-
const blurredPeriod = blurPeriod(document.period, programBlur.researchSubject);
|
|
4490
|
-
return __spreadValues(__spreadValues({}, document), blurredPeriod && { period: blurredPeriod });
|
|
4568
|
+
/** Revoke the Garmin authorization and purge the donor's token + buffer. */
|
|
4569
|
+
async disconnect(donorIdentity) {
|
|
4570
|
+
const api = await this.authenticatedApi(donorIdentity);
|
|
4571
|
+
await api.post({ uri: `/disconnect/${this.studySegment(donorIdentity)}` });
|
|
4491
4572
|
}
|
|
4492
|
-
return document;
|
|
4493
4573
|
};
|
|
4494
|
-
|
|
4495
|
-
|
|
4496
|
-
|
|
4574
|
+
|
|
4575
|
+
// src/donation/hybridEncryption.ts
|
|
4576
|
+
import { secp256k1 as secp256k12 } from "@noble/curves/secp256k1";
|
|
4577
|
+
|
|
4578
|
+
// node_modules/@noble/hashes/esm/utils.js
|
|
4579
|
+
function isBytes(a) {
|
|
4580
|
+
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
|
|
4581
|
+
}
|
|
4582
|
+
function anumber(n) {
|
|
4583
|
+
if (!Number.isSafeInteger(n) || n < 0)
|
|
4584
|
+
throw new Error("positive integer expected, got " + n);
|
|
4585
|
+
}
|
|
4586
|
+
function abytes(b, ...lengths) {
|
|
4587
|
+
if (!isBytes(b))
|
|
4588
|
+
throw new Error("Uint8Array expected");
|
|
4589
|
+
if (lengths.length > 0 && !lengths.includes(b.length))
|
|
4590
|
+
throw new Error("Uint8Array expected of length " + lengths + ", got length=" + b.length);
|
|
4591
|
+
}
|
|
4592
|
+
function ahash(h) {
|
|
4593
|
+
if (typeof h !== "function" || typeof h.create !== "function")
|
|
4594
|
+
throw new Error("Hash should be wrapped by utils.createHasher");
|
|
4595
|
+
anumber(h.outputLen);
|
|
4596
|
+
anumber(h.blockLen);
|
|
4597
|
+
}
|
|
4598
|
+
function aexists(instance, checkFinished = true) {
|
|
4599
|
+
if (instance.destroyed)
|
|
4600
|
+
throw new Error("Hash instance has been destroyed");
|
|
4601
|
+
if (checkFinished && instance.finished)
|
|
4602
|
+
throw new Error("Hash#digest() has already been called");
|
|
4603
|
+
}
|
|
4604
|
+
function aoutput(out, instance) {
|
|
4605
|
+
abytes(out);
|
|
4606
|
+
const min = instance.outputLen;
|
|
4607
|
+
if (out.length < min) {
|
|
4608
|
+
throw new Error("digestInto() expects output buffer of length at least " + min);
|
|
4497
4609
|
}
|
|
4498
|
-
|
|
4499
|
-
|
|
4500
|
-
|
|
4610
|
+
}
|
|
4611
|
+
function clean(...arrays) {
|
|
4612
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
4613
|
+
arrays[i].fill(0);
|
|
4501
4614
|
}
|
|
4502
|
-
|
|
4503
|
-
|
|
4504
|
-
|
|
4505
|
-
|
|
4506
|
-
|
|
4615
|
+
}
|
|
4616
|
+
function createView(arr) {
|
|
4617
|
+
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
4618
|
+
}
|
|
4619
|
+
function rotr(word, shift) {
|
|
4620
|
+
return word << 32 - shift | word >>> shift;
|
|
4621
|
+
}
|
|
4622
|
+
function utf8ToBytes(str) {
|
|
4623
|
+
if (typeof str !== "string")
|
|
4624
|
+
throw new Error("string expected");
|
|
4625
|
+
return new Uint8Array(new TextEncoder().encode(str));
|
|
4626
|
+
}
|
|
4627
|
+
function toBytes(data) {
|
|
4628
|
+
if (typeof data === "string")
|
|
4629
|
+
data = utf8ToBytes(data);
|
|
4630
|
+
abytes(data);
|
|
4631
|
+
return data;
|
|
4632
|
+
}
|
|
4633
|
+
var Hash = class {
|
|
4634
|
+
};
|
|
4635
|
+
function createHasher(hashCons) {
|
|
4636
|
+
const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
|
|
4637
|
+
const tmp = hashCons();
|
|
4638
|
+
hashC.outputLen = tmp.outputLen;
|
|
4639
|
+
hashC.blockLen = tmp.blockLen;
|
|
4640
|
+
hashC.create = () => hashCons();
|
|
4641
|
+
return hashC;
|
|
4642
|
+
}
|
|
4643
|
+
|
|
4644
|
+
// node_modules/@noble/hashes/esm/hmac.js
|
|
4645
|
+
var HMAC = class extends Hash {
|
|
4646
|
+
constructor(hash, _key) {
|
|
4647
|
+
super();
|
|
4648
|
+
this.finished = false;
|
|
4649
|
+
this.destroyed = false;
|
|
4650
|
+
ahash(hash);
|
|
4651
|
+
const key = toBytes(_key);
|
|
4652
|
+
this.iHash = hash.create();
|
|
4653
|
+
if (typeof this.iHash.update !== "function")
|
|
4654
|
+
throw new Error("Expected instance of class which extends utils.Hash");
|
|
4655
|
+
this.blockLen = this.iHash.blockLen;
|
|
4656
|
+
this.outputLen = this.iHash.outputLen;
|
|
4657
|
+
const blockLen = this.blockLen;
|
|
4658
|
+
const pad = new Uint8Array(blockLen);
|
|
4659
|
+
pad.set(key.length > blockLen ? hash.create().update(key).digest() : key);
|
|
4660
|
+
for (let i = 0; i < pad.length; i++)
|
|
4661
|
+
pad[i] ^= 54;
|
|
4662
|
+
this.iHash.update(pad);
|
|
4663
|
+
this.oHash = hash.create();
|
|
4664
|
+
for (let i = 0; i < pad.length; i++)
|
|
4665
|
+
pad[i] ^= 54 ^ 92;
|
|
4666
|
+
this.oHash.update(pad);
|
|
4667
|
+
clean(pad);
|
|
4668
|
+
}
|
|
4669
|
+
update(buf) {
|
|
4670
|
+
aexists(this);
|
|
4671
|
+
this.iHash.update(buf);
|
|
4672
|
+
return this;
|
|
4673
|
+
}
|
|
4674
|
+
digestInto(out) {
|
|
4675
|
+
aexists(this);
|
|
4676
|
+
abytes(out, this.outputLen);
|
|
4677
|
+
this.finished = true;
|
|
4678
|
+
this.iHash.digestInto(out);
|
|
4679
|
+
this.oHash.update(out);
|
|
4680
|
+
this.oHash.digestInto(out);
|
|
4681
|
+
this.destroy();
|
|
4682
|
+
}
|
|
4683
|
+
digest() {
|
|
4684
|
+
const out = new Uint8Array(this.oHash.outputLen);
|
|
4685
|
+
this.digestInto(out);
|
|
4686
|
+
return out;
|
|
4687
|
+
}
|
|
4688
|
+
_cloneInto(to) {
|
|
4689
|
+
to || (to = Object.create(Object.getPrototypeOf(this), {}));
|
|
4690
|
+
const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
|
|
4691
|
+
to = to;
|
|
4692
|
+
to.finished = finished;
|
|
4693
|
+
to.destroyed = destroyed;
|
|
4694
|
+
to.blockLen = blockLen;
|
|
4695
|
+
to.outputLen = outputLen;
|
|
4696
|
+
to.oHash = oHash._cloneInto(to.oHash);
|
|
4697
|
+
to.iHash = iHash._cloneInto(to.iHash);
|
|
4698
|
+
return to;
|
|
4699
|
+
}
|
|
4700
|
+
clone() {
|
|
4701
|
+
return this._cloneInto();
|
|
4702
|
+
}
|
|
4703
|
+
destroy() {
|
|
4704
|
+
this.destroyed = true;
|
|
4705
|
+
this.oHash.destroy();
|
|
4706
|
+
this.iHash.destroy();
|
|
4707
|
+
}
|
|
4708
|
+
};
|
|
4709
|
+
var hmac = (hash, key, message) => new HMAC(hash, key).update(message).digest();
|
|
4710
|
+
hmac.create = (hash, key) => new HMAC(hash, key);
|
|
4711
|
+
|
|
4712
|
+
// node_modules/@noble/hashes/esm/hkdf.js
|
|
4713
|
+
function extract(hash, ikm, salt) {
|
|
4714
|
+
ahash(hash);
|
|
4715
|
+
if (salt === void 0)
|
|
4716
|
+
salt = new Uint8Array(hash.outputLen);
|
|
4717
|
+
return hmac(hash, toBytes(salt), toBytes(ikm));
|
|
4718
|
+
}
|
|
4719
|
+
var HKDF_COUNTER = /* @__PURE__ */ Uint8Array.from([0]);
|
|
4720
|
+
var EMPTY_BUFFER = /* @__PURE__ */ Uint8Array.of();
|
|
4721
|
+
function expand(hash, prk, info, length = 32) {
|
|
4722
|
+
ahash(hash);
|
|
4723
|
+
anumber(length);
|
|
4724
|
+
const olen = hash.outputLen;
|
|
4725
|
+
if (length > 255 * olen)
|
|
4726
|
+
throw new Error("Length should be <= 255*HashLen");
|
|
4727
|
+
const blocks = Math.ceil(length / olen);
|
|
4728
|
+
if (info === void 0)
|
|
4729
|
+
info = EMPTY_BUFFER;
|
|
4730
|
+
const okm = new Uint8Array(blocks * olen);
|
|
4731
|
+
const HMAC2 = hmac.create(hash, prk);
|
|
4732
|
+
const HMACTmp = HMAC2._cloneInto();
|
|
4733
|
+
const T = new Uint8Array(HMAC2.outputLen);
|
|
4734
|
+
for (let counter = 0; counter < blocks; counter++) {
|
|
4735
|
+
HKDF_COUNTER[0] = counter + 1;
|
|
4736
|
+
HMACTmp.update(counter === 0 ? EMPTY_BUFFER : T).update(info).update(HKDF_COUNTER).digestInto(T);
|
|
4737
|
+
okm.set(T, olen * counter);
|
|
4738
|
+
HMAC2._cloneInto(HMACTmp);
|
|
4739
|
+
}
|
|
4740
|
+
HMAC2.destroy();
|
|
4741
|
+
HMACTmp.destroy();
|
|
4742
|
+
clean(T, HKDF_COUNTER);
|
|
4743
|
+
return okm.slice(0, length);
|
|
4744
|
+
}
|
|
4745
|
+
var hkdf = (hash, ikm, salt, info, length) => expand(hash, extract(hash, ikm, salt), info, length);
|
|
4746
|
+
|
|
4747
|
+
// node_modules/@noble/hashes/esm/_md.js
|
|
4748
|
+
function setBigUint64(view, byteOffset, value, isLE) {
|
|
4749
|
+
if (typeof view.setBigUint64 === "function")
|
|
4750
|
+
return view.setBigUint64(byteOffset, value, isLE);
|
|
4751
|
+
const _32n = BigInt(32);
|
|
4752
|
+
const _u32_max = BigInt(4294967295);
|
|
4753
|
+
const wh = Number(value >> _32n & _u32_max);
|
|
4754
|
+
const wl = Number(value & _u32_max);
|
|
4755
|
+
const h = isLE ? 4 : 0;
|
|
4756
|
+
const l = isLE ? 0 : 4;
|
|
4757
|
+
view.setUint32(byteOffset + h, wh, isLE);
|
|
4758
|
+
view.setUint32(byteOffset + l, wl, isLE);
|
|
4759
|
+
}
|
|
4760
|
+
function Chi(a, b, c) {
|
|
4761
|
+
return a & b ^ ~a & c;
|
|
4762
|
+
}
|
|
4763
|
+
function Maj(a, b, c) {
|
|
4764
|
+
return a & b ^ a & c ^ b & c;
|
|
4765
|
+
}
|
|
4766
|
+
var HashMD = class extends Hash {
|
|
4767
|
+
constructor(blockLen, outputLen, padOffset, isLE) {
|
|
4768
|
+
super();
|
|
4769
|
+
this.finished = false;
|
|
4770
|
+
this.length = 0;
|
|
4771
|
+
this.pos = 0;
|
|
4772
|
+
this.destroyed = false;
|
|
4773
|
+
this.blockLen = blockLen;
|
|
4774
|
+
this.outputLen = outputLen;
|
|
4775
|
+
this.padOffset = padOffset;
|
|
4776
|
+
this.isLE = isLE;
|
|
4777
|
+
this.buffer = new Uint8Array(blockLen);
|
|
4778
|
+
this.view = createView(this.buffer);
|
|
4779
|
+
}
|
|
4780
|
+
update(data) {
|
|
4781
|
+
aexists(this);
|
|
4782
|
+
data = toBytes(data);
|
|
4783
|
+
abytes(data);
|
|
4784
|
+
const { view, buffer, blockLen } = this;
|
|
4785
|
+
const len = data.length;
|
|
4786
|
+
for (let pos = 0; pos < len; ) {
|
|
4787
|
+
const take = Math.min(blockLen - this.pos, len - pos);
|
|
4788
|
+
if (take === blockLen) {
|
|
4789
|
+
const dataView = createView(data);
|
|
4790
|
+
for (; blockLen <= len - pos; pos += blockLen)
|
|
4791
|
+
this.process(dataView, pos);
|
|
4792
|
+
continue;
|
|
4793
|
+
}
|
|
4794
|
+
buffer.set(data.subarray(pos, pos + take), this.pos);
|
|
4795
|
+
this.pos += take;
|
|
4796
|
+
pos += take;
|
|
4797
|
+
if (this.pos === blockLen) {
|
|
4798
|
+
this.process(view, 0);
|
|
4799
|
+
this.pos = 0;
|
|
4800
|
+
}
|
|
4801
|
+
}
|
|
4802
|
+
this.length += data.length;
|
|
4803
|
+
this.roundClean();
|
|
4804
|
+
return this;
|
|
4805
|
+
}
|
|
4806
|
+
digestInto(out) {
|
|
4807
|
+
aexists(this);
|
|
4808
|
+
aoutput(out, this);
|
|
4809
|
+
this.finished = true;
|
|
4810
|
+
const { buffer, view, blockLen, isLE } = this;
|
|
4811
|
+
let { pos } = this;
|
|
4812
|
+
buffer[pos++] = 128;
|
|
4813
|
+
clean(this.buffer.subarray(pos));
|
|
4814
|
+
if (this.padOffset > blockLen - pos) {
|
|
4815
|
+
this.process(view, 0);
|
|
4816
|
+
pos = 0;
|
|
4817
|
+
}
|
|
4818
|
+
for (let i = pos; i < blockLen; i++)
|
|
4819
|
+
buffer[i] = 0;
|
|
4820
|
+
setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE);
|
|
4821
|
+
this.process(view, 0);
|
|
4822
|
+
const oview = createView(out);
|
|
4823
|
+
const len = this.outputLen;
|
|
4824
|
+
if (len % 4)
|
|
4825
|
+
throw new Error("_sha2: outputLen should be aligned to 32bit");
|
|
4826
|
+
const outLen = len / 4;
|
|
4827
|
+
const state = this.get();
|
|
4828
|
+
if (outLen > state.length)
|
|
4829
|
+
throw new Error("_sha2: outputLen bigger than state");
|
|
4830
|
+
for (let i = 0; i < outLen; i++)
|
|
4831
|
+
oview.setUint32(4 * i, state[i], isLE);
|
|
4832
|
+
}
|
|
4833
|
+
digest() {
|
|
4834
|
+
const { buffer, outputLen } = this;
|
|
4835
|
+
this.digestInto(buffer);
|
|
4836
|
+
const res = buffer.slice(0, outputLen);
|
|
4837
|
+
this.destroy();
|
|
4838
|
+
return res;
|
|
4839
|
+
}
|
|
4840
|
+
_cloneInto(to) {
|
|
4841
|
+
to || (to = new this.constructor());
|
|
4842
|
+
to.set(...this.get());
|
|
4843
|
+
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
|
4844
|
+
to.destroyed = destroyed;
|
|
4845
|
+
to.finished = finished;
|
|
4846
|
+
to.length = length;
|
|
4847
|
+
to.pos = pos;
|
|
4848
|
+
if (length % blockLen)
|
|
4849
|
+
to.buffer.set(buffer);
|
|
4850
|
+
return to;
|
|
4851
|
+
}
|
|
4852
|
+
clone() {
|
|
4853
|
+
return this._cloneInto();
|
|
4854
|
+
}
|
|
4855
|
+
};
|
|
4856
|
+
var SHA256_IV = /* @__PURE__ */ Uint32Array.from([
|
|
4857
|
+
1779033703,
|
|
4858
|
+
3144134277,
|
|
4859
|
+
1013904242,
|
|
4860
|
+
2773480762,
|
|
4861
|
+
1359893119,
|
|
4862
|
+
2600822924,
|
|
4863
|
+
528734635,
|
|
4864
|
+
1541459225
|
|
4865
|
+
]);
|
|
4866
|
+
|
|
4867
|
+
// node_modules/@noble/hashes/esm/sha2.js
|
|
4868
|
+
var SHA256_K = /* @__PURE__ */ Uint32Array.from([
|
|
4869
|
+
1116352408,
|
|
4870
|
+
1899447441,
|
|
4871
|
+
3049323471,
|
|
4872
|
+
3921009573,
|
|
4873
|
+
961987163,
|
|
4874
|
+
1508970993,
|
|
4875
|
+
2453635748,
|
|
4876
|
+
2870763221,
|
|
4877
|
+
3624381080,
|
|
4878
|
+
310598401,
|
|
4879
|
+
607225278,
|
|
4880
|
+
1426881987,
|
|
4881
|
+
1925078388,
|
|
4882
|
+
2162078206,
|
|
4883
|
+
2614888103,
|
|
4884
|
+
3248222580,
|
|
4885
|
+
3835390401,
|
|
4886
|
+
4022224774,
|
|
4887
|
+
264347078,
|
|
4888
|
+
604807628,
|
|
4889
|
+
770255983,
|
|
4890
|
+
1249150122,
|
|
4891
|
+
1555081692,
|
|
4892
|
+
1996064986,
|
|
4893
|
+
2554220882,
|
|
4894
|
+
2821834349,
|
|
4895
|
+
2952996808,
|
|
4896
|
+
3210313671,
|
|
4897
|
+
3336571891,
|
|
4898
|
+
3584528711,
|
|
4899
|
+
113926993,
|
|
4900
|
+
338241895,
|
|
4901
|
+
666307205,
|
|
4902
|
+
773529912,
|
|
4903
|
+
1294757372,
|
|
4904
|
+
1396182291,
|
|
4905
|
+
1695183700,
|
|
4906
|
+
1986661051,
|
|
4907
|
+
2177026350,
|
|
4908
|
+
2456956037,
|
|
4909
|
+
2730485921,
|
|
4910
|
+
2820302411,
|
|
4911
|
+
3259730800,
|
|
4912
|
+
3345764771,
|
|
4913
|
+
3516065817,
|
|
4914
|
+
3600352804,
|
|
4915
|
+
4094571909,
|
|
4916
|
+
275423344,
|
|
4917
|
+
430227734,
|
|
4918
|
+
506948616,
|
|
4919
|
+
659060556,
|
|
4920
|
+
883997877,
|
|
4921
|
+
958139571,
|
|
4922
|
+
1322822218,
|
|
4923
|
+
1537002063,
|
|
4924
|
+
1747873779,
|
|
4925
|
+
1955562222,
|
|
4926
|
+
2024104815,
|
|
4927
|
+
2227730452,
|
|
4928
|
+
2361852424,
|
|
4929
|
+
2428436474,
|
|
4930
|
+
2756734187,
|
|
4931
|
+
3204031479,
|
|
4932
|
+
3329325298
|
|
4933
|
+
]);
|
|
4934
|
+
var SHA256_W = /* @__PURE__ */ new Uint32Array(64);
|
|
4935
|
+
var SHA256 = class extends HashMD {
|
|
4936
|
+
constructor(outputLen = 32) {
|
|
4937
|
+
super(64, outputLen, 8, false);
|
|
4938
|
+
this.A = SHA256_IV[0] | 0;
|
|
4939
|
+
this.B = SHA256_IV[1] | 0;
|
|
4940
|
+
this.C = SHA256_IV[2] | 0;
|
|
4941
|
+
this.D = SHA256_IV[3] | 0;
|
|
4942
|
+
this.E = SHA256_IV[4] | 0;
|
|
4943
|
+
this.F = SHA256_IV[5] | 0;
|
|
4944
|
+
this.G = SHA256_IV[6] | 0;
|
|
4945
|
+
this.H = SHA256_IV[7] | 0;
|
|
4946
|
+
}
|
|
4947
|
+
get() {
|
|
4948
|
+
const { A, B, C, D, E, F, G, H } = this;
|
|
4949
|
+
return [A, B, C, D, E, F, G, H];
|
|
4950
|
+
}
|
|
4951
|
+
// prettier-ignore
|
|
4952
|
+
set(A, B, C, D, E, F, G, H) {
|
|
4953
|
+
this.A = A | 0;
|
|
4954
|
+
this.B = B | 0;
|
|
4955
|
+
this.C = C | 0;
|
|
4956
|
+
this.D = D | 0;
|
|
4957
|
+
this.E = E | 0;
|
|
4958
|
+
this.F = F | 0;
|
|
4959
|
+
this.G = G | 0;
|
|
4960
|
+
this.H = H | 0;
|
|
4961
|
+
}
|
|
4962
|
+
process(view, offset) {
|
|
4963
|
+
for (let i = 0; i < 16; i++, offset += 4)
|
|
4964
|
+
SHA256_W[i] = view.getUint32(offset, false);
|
|
4965
|
+
for (let i = 16; i < 64; i++) {
|
|
4966
|
+
const W15 = SHA256_W[i - 15];
|
|
4967
|
+
const W2 = SHA256_W[i - 2];
|
|
4968
|
+
const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ W15 >>> 3;
|
|
4969
|
+
const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ W2 >>> 10;
|
|
4970
|
+
SHA256_W[i] = s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16] | 0;
|
|
4971
|
+
}
|
|
4972
|
+
let { A, B, C, D, E, F, G, H } = this;
|
|
4973
|
+
for (let i = 0; i < 64; i++) {
|
|
4974
|
+
const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
|
|
4975
|
+
const T1 = H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i] | 0;
|
|
4976
|
+
const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
|
|
4977
|
+
const T2 = sigma0 + Maj(A, B, C) | 0;
|
|
4978
|
+
H = G;
|
|
4979
|
+
G = F;
|
|
4980
|
+
F = E;
|
|
4981
|
+
E = D + T1 | 0;
|
|
4982
|
+
D = C;
|
|
4983
|
+
C = B;
|
|
4984
|
+
B = A;
|
|
4985
|
+
A = T1 + T2 | 0;
|
|
4986
|
+
}
|
|
4987
|
+
A = A + this.A | 0;
|
|
4988
|
+
B = B + this.B | 0;
|
|
4989
|
+
C = C + this.C | 0;
|
|
4990
|
+
D = D + this.D | 0;
|
|
4991
|
+
E = E + this.E | 0;
|
|
4992
|
+
F = F + this.F | 0;
|
|
4993
|
+
G = G + this.G | 0;
|
|
4994
|
+
H = H + this.H | 0;
|
|
4995
|
+
this.set(A, B, C, D, E, F, G, H);
|
|
4507
4996
|
}
|
|
4508
|
-
|
|
4509
|
-
|
|
4510
|
-
return __spreadValues(__spreadValues({}, period.start && (startDate && startDate.isValid ? { start: blurDate(startDate, blurFn).toString() } : { start: period.start })), period.end && (endDate && endDate.isValid ? { end: blurDate(endDate, blurFn).toString() } : { end: period.end }));
|
|
4511
|
-
};
|
|
4512
|
-
var blurDate = (date, blurFn) => {
|
|
4513
|
-
const [fn, unit] = blurFn.toLowerCase().split("of");
|
|
4514
|
-
if (!["day", "week", "month"].includes(unit)) {
|
|
4515
|
-
return date;
|
|
4997
|
+
roundClean() {
|
|
4998
|
+
clean(SHA256_W);
|
|
4516
4999
|
}
|
|
4517
|
-
|
|
4518
|
-
|
|
4519
|
-
|
|
4520
|
-
return date.endOf(unit);
|
|
5000
|
+
destroy() {
|
|
5001
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
|
5002
|
+
clean(this.buffer);
|
|
4521
5003
|
}
|
|
4522
|
-
return date;
|
|
4523
5004
|
};
|
|
5005
|
+
var sha256 = /* @__PURE__ */ createHasher(() => new SHA256());
|
|
4524
5006
|
|
|
4525
|
-
//
|
|
4526
|
-
var
|
|
4527
|
-
|
|
4528
|
-
|
|
4529
|
-
|
|
4530
|
-
|
|
4531
|
-
|
|
4532
|
-
|
|
4533
|
-
|
|
4534
|
-
|
|
4535
|
-
|
|
4536
|
-
document: blurredDocument,
|
|
4537
|
-
status: record.status,
|
|
4538
|
-
id: record.id
|
|
4539
|
-
};
|
|
4540
|
-
});
|
|
5007
|
+
// node_modules/@noble/hashes/esm/sha256.js
|
|
5008
|
+
var sha2562 = sha256;
|
|
5009
|
+
|
|
5010
|
+
// src/donation/hybridEncryption.ts
|
|
5011
|
+
import { Buffer as Buffer10 } from "buffer/";
|
|
5012
|
+
import * as forge2 from "node-forge";
|
|
5013
|
+
|
|
5014
|
+
// src/helper/converter.ts
|
|
5015
|
+
import { Buffer as Buffer9 } from "buffer/";
|
|
5016
|
+
var binaryStringToUint8Array = (binaryString) => {
|
|
5017
|
+
return new Uint8Array(binaryString.split("").map((c) => c.charCodeAt(0)));
|
|
4541
5018
|
};
|
|
4542
|
-
var
|
|
4543
|
-
|
|
4544
|
-
|
|
4545
|
-
|
|
4546
|
-
|
|
4547
|
-
|
|
4548
|
-
|
|
4549
|
-
|
|
4550
|
-
|
|
4551
|
-
|
|
4552
|
-
|
|
4553
|
-
if (isObservation(document)) {
|
|
4554
|
-
return true;
|
|
4555
|
-
}
|
|
4556
|
-
if (isConsent(document)) {
|
|
4557
|
-
return true;
|
|
4558
|
-
}
|
|
4559
|
-
if (isRecording(document)) {
|
|
4560
|
-
return true;
|
|
4561
|
-
}
|
|
4562
|
-
if (isCommunication(document)) {
|
|
4563
|
-
return true;
|
|
5019
|
+
var uint8ArrayToBinaryString = (array) => {
|
|
5020
|
+
const CHUNK_SIZE = 16384;
|
|
5021
|
+
if (array.length <= CHUNK_SIZE) {
|
|
5022
|
+
return String.fromCharCode.apply(null, array);
|
|
5023
|
+
}
|
|
5024
|
+
let result = "";
|
|
5025
|
+
for (let offset = 0; offset < array.length; offset += CHUNK_SIZE) {
|
|
5026
|
+
result += String.fromCharCode.apply(
|
|
5027
|
+
null,
|
|
5028
|
+
array.subarray(offset, offset + CHUNK_SIZE)
|
|
5029
|
+
);
|
|
4564
5030
|
}
|
|
4565
|
-
return
|
|
5031
|
+
return result;
|
|
4566
5032
|
};
|
|
4567
|
-
var
|
|
4568
|
-
|
|
4569
|
-
|
|
4570
|
-
|
|
4571
|
-
|
|
4572
|
-
|
|
4573
|
-
|
|
4574
|
-
|
|
4575
|
-
|
|
4576
|
-
|
|
4577
|
-
|
|
4578
|
-
|
|
4579
|
-
|
|
4580
|
-
|
|
4581
|
-
|
|
5033
|
+
var utf8ToUint8Array = (string) => {
|
|
5034
|
+
const result = [];
|
|
5035
|
+
const size = string.length;
|
|
5036
|
+
for (let index = 0; index < size; index++) {
|
|
5037
|
+
let point = string.charCodeAt(index);
|
|
5038
|
+
if (point >= 55296 && point <= 56319 && size > index + 1) {
|
|
5039
|
+
const second = string.charCodeAt(index + 1);
|
|
5040
|
+
if (second >= 56320 && second <= 57343) {
|
|
5041
|
+
point = (point - 55296) * 1024 + second - 56320 + 65536;
|
|
5042
|
+
index += 1;
|
|
5043
|
+
}
|
|
5044
|
+
}
|
|
5045
|
+
if (point < 128) {
|
|
5046
|
+
result.push(point);
|
|
5047
|
+
continue;
|
|
5048
|
+
}
|
|
5049
|
+
if (point < 2048) {
|
|
5050
|
+
result.push(point >> 6 | 192);
|
|
5051
|
+
result.push(point & 63 | 128);
|
|
5052
|
+
continue;
|
|
5053
|
+
}
|
|
5054
|
+
if (point < 55296 || point >= 57344 && point < 65536) {
|
|
5055
|
+
result.push(point >> 12 | 224);
|
|
5056
|
+
result.push(point >> 6 & 63 | 128);
|
|
5057
|
+
result.push(point & 63 | 128);
|
|
5058
|
+
continue;
|
|
5059
|
+
}
|
|
5060
|
+
if (point >= 65536 && point <= 1114111) {
|
|
5061
|
+
result.push(point >> 18 | 240);
|
|
5062
|
+
result.push(point >> 12 & 63 | 128);
|
|
5063
|
+
result.push(point >> 6 & 63 | 128);
|
|
5064
|
+
result.push(point & 63 | 128);
|
|
5065
|
+
continue;
|
|
5066
|
+
}
|
|
5067
|
+
result.push(239, 191, 189);
|
|
4582
5068
|
}
|
|
4583
|
-
return
|
|
4584
|
-
};
|
|
4585
|
-
var includedResearchSubjects = (records) => {
|
|
4586
|
-
return records.filter((record) => isResearchSubject(record.document));
|
|
5069
|
+
return new Uint8Array(result);
|
|
4587
5070
|
};
|
|
4588
|
-
var
|
|
4589
|
-
|
|
4590
|
-
(
|
|
4591
|
-
|
|
4592
|
-
|
|
4593
|
-
|
|
4594
|
-
|
|
4595
|
-
|
|
4596
|
-
|
|
5071
|
+
var WebConverter = class {
|
|
5072
|
+
constructor() {
|
|
5073
|
+
this.arrayBufferToBase64 = (buffer) => {
|
|
5074
|
+
let binary = "";
|
|
5075
|
+
const bytes = new Uint8Array(buffer);
|
|
5076
|
+
const len = bytes.byteLength;
|
|
5077
|
+
for (let i = 0; i < len; i++) {
|
|
5078
|
+
binary += String.fromCharCode(bytes[i]);
|
|
5079
|
+
}
|
|
5080
|
+
return btoa(binary);
|
|
5081
|
+
};
|
|
5082
|
+
this.base64ToArrayBuffer = (string) => {
|
|
5083
|
+
const base64 = atob(string);
|
|
5084
|
+
const buffer = new ArrayBuffer(base64.length);
|
|
5085
|
+
const bufferView = new Uint8Array(buffer);
|
|
5086
|
+
for (let i = 0, base64Len = base64.length; i < base64Len; i++) {
|
|
5087
|
+
bufferView[i] = base64.charCodeAt(i);
|
|
5088
|
+
}
|
|
5089
|
+
return buffer;
|
|
5090
|
+
};
|
|
5091
|
+
// convert Uint8Array to base64 string
|
|
5092
|
+
this.uint8ArrayToBase64 = (array) => {
|
|
5093
|
+
return btoa(String.fromCharCode(...array));
|
|
5094
|
+
};
|
|
5095
|
+
/**
|
|
5096
|
+
* Convert a base64 encoded string into Uint8Array
|
|
5097
|
+
* @param string The string that needs to be converted into an Uint8Array
|
|
5098
|
+
* @returns A Uint8Array
|
|
5099
|
+
*/
|
|
5100
|
+
this.base64ToUint8Array = (string) => new Uint8Array(this.base64ToArrayBuffer(string));
|
|
5101
|
+
/**
|
|
5102
|
+
* Convert a base64 encoded Uint8Array into a UTF-8 encoded string
|
|
5103
|
+
* @param uint The base64 encoded Uint8Array
|
|
5104
|
+
* @returns The UTF-8 encoded string
|
|
5105
|
+
*/
|
|
5106
|
+
this.uint8ArrayToUTF8 = (uint) => {
|
|
5107
|
+
const decoder = new TextDecoder();
|
|
5108
|
+
const str = decoder.decode(uint);
|
|
5109
|
+
return Buffer9.from(str, "base64").toString("utf8");
|
|
5110
|
+
};
|
|
5111
|
+
this.base64ToHex = (base64String) => Buffer9.from(base64String, "base64").toString("hex");
|
|
5112
|
+
this.hexToBase64 = (hexString) => Buffer9.from(hexString, "hex").toString("base64");
|
|
4597
5113
|
}
|
|
4598
|
-
|
|
4599
|
-
});
|
|
5114
|
+
};
|
|
4600
5115
|
|
|
4601
5116
|
// src/donation/hybridEncryption.ts
|
|
4602
|
-
import { Buffer as Buffer9 } from "buffer/";
|
|
4603
|
-
import * as forge2 from "node-forge";
|
|
4604
5117
|
var AES_BLOCKSIZE = 16;
|
|
4605
5118
|
var TAG_LENGTH = 16;
|
|
4606
5119
|
var KEY_LENGTH = 32;
|
|
4607
5120
|
var HYBRID_ENCRYPTION_VERSION_RECOVERY = 3;
|
|
5121
|
+
var HYBRID_ENCRYPTION_VERSION_V4 = 4;
|
|
5122
|
+
var V4_PAYLOAD_ALG_AES_GCM = 0;
|
|
5123
|
+
var V4_DECRYPTER_MOBILE_APP = 0;
|
|
5124
|
+
var V4_DECRYPTER_DATA_RECEIVER = 1;
|
|
5125
|
+
var V4_KEY_ALG_AES_SYM = 0;
|
|
5126
|
+
var V4_KEY_ALG_RSA_OAEP = 1;
|
|
5127
|
+
var V4_KEY_ALG_EC = 2;
|
|
5128
|
+
var V4_UNCOMPRESSED_PUBKEY_LENGTH = 65;
|
|
5129
|
+
var V4_EC_NONCE_LENGTH = 12;
|
|
5130
|
+
var V4_RECOVERY_NONCE_LENGTH = 12;
|
|
5131
|
+
var V4_PAYLOAD_IV_LENGTH = 12;
|
|
5132
|
+
var V4_GCM_TAG_LENGTH = 16;
|
|
5133
|
+
var V4_EC_SALT = utf8ToUint8Array("d4l/hybrid-v4/ec/salt");
|
|
5134
|
+
var V4_EC_INFO = utf8ToUint8Array("d4l/hybrid-v4/ec/secp256k1/aes256gcm");
|
|
5135
|
+
var v4AesSlot = (decrypterId, symmetricKeyBase64) => ({
|
|
5136
|
+
decrypterId,
|
|
5137
|
+
keyAlg: V4_KEY_ALG_AES_SYM,
|
|
5138
|
+
key: symmetricKeyBase64
|
|
5139
|
+
});
|
|
5140
|
+
var v4RsaSlot = (decrypterId, publicKeyPem) => ({
|
|
5141
|
+
decrypterId,
|
|
5142
|
+
keyAlg: V4_KEY_ALG_RSA_OAEP,
|
|
5143
|
+
key: publicKeyPem
|
|
5144
|
+
});
|
|
5145
|
+
var v4EcSlot = (decrypterId, publicKeyBase64) => ({
|
|
5146
|
+
decrypterId,
|
|
5147
|
+
keyAlg: V4_KEY_ALG_EC,
|
|
5148
|
+
key: publicKeyBase64
|
|
5149
|
+
});
|
|
5150
|
+
var v4AesKey = (decrypterId, symmetricKeyBase64) => ({
|
|
5151
|
+
decrypterId,
|
|
5152
|
+
keyAlg: V4_KEY_ALG_AES_SYM,
|
|
5153
|
+
key: symmetricKeyBase64
|
|
5154
|
+
});
|
|
5155
|
+
var v4RsaKey = (decrypterId, privateKeyPem) => ({
|
|
5156
|
+
decrypterId,
|
|
5157
|
+
keyAlg: V4_KEY_ALG_RSA_OAEP,
|
|
5158
|
+
key: privateKeyPem
|
|
5159
|
+
});
|
|
5160
|
+
var v4EcKey = (decrypterId, privateKeyBase64) => ({
|
|
5161
|
+
decrypterId,
|
|
5162
|
+
keyAlg: V4_KEY_ALG_EC,
|
|
5163
|
+
key: privateKeyBase64
|
|
5164
|
+
});
|
|
5165
|
+
var requireV4Bytes = (bytes, offset, length, what) => {
|
|
5166
|
+
if (length < 0 || offset + length > bytes.length) {
|
|
5167
|
+
throw new Error(
|
|
5168
|
+
`Malformed v4 record: ${what} needs ${length} bytes at offset ${offset}, but the record is only ${bytes.length} bytes`
|
|
5169
|
+
);
|
|
5170
|
+
}
|
|
5171
|
+
};
|
|
5172
|
+
var rawEcPublicKey = (publicKey) => {
|
|
5173
|
+
if (publicKey.length === V4_UNCOMPRESSED_PUBKEY_LENGTH) {
|
|
5174
|
+
return publicKey;
|
|
5175
|
+
}
|
|
5176
|
+
if (publicKey.length < V4_UNCOMPRESSED_PUBKEY_LENGTH) {
|
|
5177
|
+
throw new Error(`Invalid secp256k1 public key length: ${publicKey.length}`);
|
|
5178
|
+
}
|
|
5179
|
+
return publicKey.subarray(publicKey.length - V4_UNCOMPRESSED_PUBKEY_LENGTH);
|
|
5180
|
+
};
|
|
4608
5181
|
var HybridEncrypter = class {
|
|
4609
5182
|
constructor(converter) {
|
|
4610
5183
|
/**
|
|
@@ -4694,7 +5267,355 @@ var HybridEncrypter = class {
|
|
|
4694
5267
|
donation.encrypted,
|
|
4695
5268
|
donation.iv
|
|
4696
5269
|
);
|
|
4697
|
-
return
|
|
5270
|
+
return Buffer10.from(decryptedDonation).toString("utf8");
|
|
5271
|
+
};
|
|
5272
|
+
/**
|
|
5273
|
+
* Decrypt a v4 hybrid ciphertext addressed to the mobile app. The normal path is the EC
|
|
5274
|
+
* slot (ECIES over secp256k1) the data-collector encrypts to the phone's derived key.
|
|
5275
|
+
*
|
|
5276
|
+
* The AES-256 recovery key is a durable fallback across formats, so it is consulted for
|
|
5277
|
+
* two kinds of input:
|
|
5278
|
+
* - a v3 ciphertext, delegated to the legacy recovery-key path (`decrypt`); and
|
|
5279
|
+
* - a v4 ciphertext that carries an AES recovery slot (decrypterId = mobile app,
|
|
5280
|
+
* keyAlg = AES symmetric) but no usable EC slot — the same recovery key that could
|
|
5281
|
+
* recover v3 records then recovers v4 records too.
|
|
5282
|
+
* The EC slot is always preferred when present; the recovery key is only needed to fall
|
|
5283
|
+
* back to the AES slot (or a v3 blob).
|
|
5284
|
+
*
|
|
5285
|
+
* Note the legacy branch is v3-only, matching `decrypt`: v1 (AES-CBC) and v2 (AES-GCM)
|
|
5286
|
+
* are single-recipient formats this library never produced — donations have always been
|
|
5287
|
+
* written with a recovery branch — so a v1/v2 blob is rejected rather than decrypted.
|
|
5288
|
+
* @param privateKey The phone's encryption private key (secp256k1 scalar, base64 — as
|
|
5289
|
+
* returned by KeyHandler.deriveEncryptionKeypair)
|
|
5290
|
+
* @param cipherText The base64-encoded ciphertext (v4, or v3 for the legacy fallback)
|
|
5291
|
+
* @param recoveryKey Base64 recovery key; needed only for the AES-recovery or v3 paths
|
|
5292
|
+
* @returns The decrypted UTF-8 string
|
|
5293
|
+
*/
|
|
5294
|
+
this.decryptV4 = (privateKey, cipherText, recoveryKey) => {
|
|
5295
|
+
const version = new Uint8Array(this.converter.base64ToArrayBuffer(cipherText))[0];
|
|
5296
|
+
if (version !== HYBRID_ENCRYPTION_VERSION_V4) {
|
|
5297
|
+
if (recoveryKey === void 0) {
|
|
5298
|
+
throw new Error(`Cannot decrypt v${version} ciphertext without a recovery key`);
|
|
5299
|
+
}
|
|
5300
|
+
return this.decrypt(recoveryKey, cipherText);
|
|
5301
|
+
}
|
|
5302
|
+
const keys = [v4EcKey(V4_DECRYPTER_MOBILE_APP, privateKey)];
|
|
5303
|
+
if (recoveryKey !== void 0) {
|
|
5304
|
+
keys.push(v4AesKey(V4_DECRYPTER_MOBILE_APP, recoveryKey));
|
|
5305
|
+
}
|
|
5306
|
+
const record = this.decodeV4(cipherText);
|
|
5307
|
+
const hasRecoverySlot = record.slots.some(
|
|
5308
|
+
(s) => s.decrypterId === V4_DECRYPTER_MOBILE_APP && s.keyAlg === V4_KEY_ALG_AES_SYM
|
|
5309
|
+
);
|
|
5310
|
+
if (recoveryKey === void 0 && hasRecoverySlot) {
|
|
5311
|
+
throw new Error("v4 ciphertext has an AES recovery slot but no recovery key was provided");
|
|
5312
|
+
}
|
|
5313
|
+
return this.openV4Record(record, keys);
|
|
5314
|
+
};
|
|
5315
|
+
/**
|
|
5316
|
+
* Decrypt a v4 record with an explicit keychain — the generic entry point, mirroring
|
|
5317
|
+
* go-crypto's `NewDecrypterFromKeys`. A slot is opened only by a key whose
|
|
5318
|
+
* (decrypterId, keyAlg) match, and every matching key is tried, so a record carrying
|
|
5319
|
+
* several slots for the same recipient (e.g. a rotated recovery key) still opens.
|
|
5320
|
+
* @param cipherText The base64-encoded v4 ciphertext
|
|
5321
|
+
* @param keys Keychain entries, built with v4EcKey / v4AesKey / v4RsaKey
|
|
5322
|
+
* @returns The decrypted UTF-8 string
|
|
5323
|
+
*/
|
|
5324
|
+
this.decryptV4WithKeys = (cipherText, keys) => this.openV4Record(this.decodeV4(cipherText), keys);
|
|
5325
|
+
/** Unwrap the data key with the first matching key, then open the payload. */
|
|
5326
|
+
this.openV4Record = (record, keys) => {
|
|
5327
|
+
const dataKey = this.unwrapV4DataKey(record.slots, keys);
|
|
5328
|
+
const plaintext = this.decryptAESGCM(dataKey, record.payloadIv, record.payload);
|
|
5329
|
+
return Buffer10.from(plaintext).toString("utf8");
|
|
5330
|
+
};
|
|
5331
|
+
/**
|
|
5332
|
+
* Parse a v4 record into its slots and payload. Every length comes from the record
|
|
5333
|
+
* itself, so each read is bounds-checked first: a truncated or malformed blob fails as a
|
|
5334
|
+
* clear format error instead of a raw RangeError or a silently short slice.
|
|
5335
|
+
* @param cipherText The base64-encoded v4 ciphertext
|
|
5336
|
+
* @returns The decoded slots, payload IV and payload
|
|
5337
|
+
*/
|
|
5338
|
+
this.decodeV4 = (cipherText) => {
|
|
5339
|
+
const buffer = this.converter.base64ToArrayBuffer(cipherText);
|
|
5340
|
+
const bytes = new Uint8Array(buffer);
|
|
5341
|
+
const view = new DataView(buffer);
|
|
5342
|
+
let offset = 0;
|
|
5343
|
+
requireV4Bytes(bytes, offset, 3, "header");
|
|
5344
|
+
const version = view.getUint8(offset);
|
|
5345
|
+
offset += 1;
|
|
5346
|
+
if (version !== HYBRID_ENCRYPTION_VERSION_V4) {
|
|
5347
|
+
throw new Error(`Not a v4 record: version ${version}`);
|
|
5348
|
+
}
|
|
5349
|
+
const payloadAlg = view.getUint8(offset);
|
|
5350
|
+
offset += 1;
|
|
5351
|
+
if (payloadAlg !== V4_PAYLOAD_ALG_AES_GCM) {
|
|
5352
|
+
throw new Error("Unsupported v4 payload algorithm");
|
|
5353
|
+
}
|
|
5354
|
+
const slotCount = view.getUint8(offset);
|
|
5355
|
+
offset += 1;
|
|
5356
|
+
const slots = [];
|
|
5357
|
+
for (let i = 0; i < slotCount; i++) {
|
|
5358
|
+
requireV4Bytes(bytes, offset, 4, `slot ${i} header`);
|
|
5359
|
+
const decrypterId = view.getUint8(offset);
|
|
5360
|
+
offset += 1;
|
|
5361
|
+
const keyAlg = view.getUint8(offset);
|
|
5362
|
+
offset += 1;
|
|
5363
|
+
const slotLen = view.getUint16(offset, true);
|
|
5364
|
+
offset += 2;
|
|
5365
|
+
requireV4Bytes(bytes, offset, slotLen, `slot ${i} body`);
|
|
5366
|
+
slots.push({ decrypterId, keyAlg, blob: bytes.slice(offset, offset + slotLen) });
|
|
5367
|
+
offset += slotLen;
|
|
5368
|
+
}
|
|
5369
|
+
requireV4Bytes(bytes, offset, V4_PAYLOAD_IV_LENGTH + 8, "payload header");
|
|
5370
|
+
const payloadIv = bytes.slice(offset, offset + V4_PAYLOAD_IV_LENGTH);
|
|
5371
|
+
offset += V4_PAYLOAD_IV_LENGTH;
|
|
5372
|
+
const payloadLen = view.getUint32(offset, true);
|
|
5373
|
+
const payloadLenHigh = view.getUint32(offset + 4, true);
|
|
5374
|
+
offset += 8;
|
|
5375
|
+
if (payloadLenHigh !== 0) {
|
|
5376
|
+
throw new Error("v4 payload too large");
|
|
5377
|
+
}
|
|
5378
|
+
requireV4Bytes(bytes, offset, payloadLen, "payload");
|
|
5379
|
+
return { slots, payloadIv, payload: bytes.slice(offset, offset + payloadLen) };
|
|
5380
|
+
};
|
|
5381
|
+
/**
|
|
5382
|
+
* Encrypt plaintext once under a random data key and wrap that key into one slot per
|
|
5383
|
+
* recipient (decryptV4 is the inverse). Slot decrypterIds are opaque to this class; callers
|
|
5384
|
+
* assign their meaning. Mirrors go-crypto's EncryptHybridV4.
|
|
5385
|
+
* @param plaintext The data to encrypt
|
|
5386
|
+
* @param slots One slot per party that must recover the data (see v4AesSlot / v4RsaSlot)
|
|
5387
|
+
* @returns The base64-encoded v4 ciphertext
|
|
5388
|
+
*/
|
|
5389
|
+
this.encryptV4 = (plaintext, slots) => {
|
|
5390
|
+
if (slots.length === 0) {
|
|
5391
|
+
throw new Error("encryptV4 requires at least one slot");
|
|
5392
|
+
}
|
|
5393
|
+
const dataKey = binaryStringToUint8Array(this.getDataKey());
|
|
5394
|
+
const payloadIv = binaryStringToUint8Array(forge2.random.getBytesSync(V4_PAYLOAD_IV_LENGTH));
|
|
5395
|
+
const payload = this.encryptAESGCM(dataKey, payloadIv, utf8ToUint8Array(plaintext));
|
|
5396
|
+
const wrapped = slots.map((slot) => ({
|
|
5397
|
+
decrypterId: slot.decrypterId,
|
|
5398
|
+
keyAlg: slot.keyAlg,
|
|
5399
|
+
blob: this.wrapV4Slot(slot, dataKey)
|
|
5400
|
+
}));
|
|
5401
|
+
return this.encodeV4(wrapped, payloadIv, payload);
|
|
5402
|
+
};
|
|
5403
|
+
/** wrapV4Slot wraps dataKey for one recipient, dispatching on the slot's key algorithm. */
|
|
5404
|
+
this.wrapV4Slot = (slot, dataKey) => {
|
|
5405
|
+
switch (slot.keyAlg) {
|
|
5406
|
+
case V4_KEY_ALG_AES_SYM:
|
|
5407
|
+
return this.wrapV4RecoveryDataKey(
|
|
5408
|
+
new Uint8Array(this.converter.base64ToArrayBuffer(slot.key)),
|
|
5409
|
+
dataKey
|
|
5410
|
+
);
|
|
5411
|
+
case V4_KEY_ALG_RSA_OAEP:
|
|
5412
|
+
return this.encryptWithTargetKey(slot.key, uint8ArrayToBinaryString(dataKey)).encryptedDataKey;
|
|
5413
|
+
case V4_KEY_ALG_EC:
|
|
5414
|
+
return this.wrapV4EcDataKey(slot.key, dataKey);
|
|
5415
|
+
default:
|
|
5416
|
+
throw new Error(`encryptV4: unsupported key algorithm ${slot.keyAlg}`);
|
|
5417
|
+
}
|
|
5418
|
+
};
|
|
5419
|
+
/**
|
|
5420
|
+
* Wrap the data key to a secp256k1 public key via ECIES — the inverse of
|
|
5421
|
+
* unwrapV4EcDataKey, and byte-compatible with go-crypto's EC slot:
|
|
5422
|
+
* ephemeral ECDH -> HKDF-SHA256 -> AES-256-GCM.
|
|
5423
|
+
* Slot layout: ephPub(65, uncompressed) | nonce(12) | wrappedDataKey||tag(48).
|
|
5424
|
+
* @param publicKeyBase64 The recipient's SPKI (or raw uncompressed) secp256k1 public key
|
|
5425
|
+
* @param dataKey The payload data key to wrap
|
|
5426
|
+
*/
|
|
5427
|
+
this.wrapV4EcDataKey = (publicKeyBase64, dataKey) => {
|
|
5428
|
+
const recipient = rawEcPublicKey(this.converter.base64ToUint8Array(publicKeyBase64));
|
|
5429
|
+
let ephemeralPrivate = binaryStringToUint8Array(forge2.random.getBytesSync(32));
|
|
5430
|
+
while (!secp256k12.utils.isValidSecretKey(ephemeralPrivate)) {
|
|
5431
|
+
ephemeralPrivate = binaryStringToUint8Array(forge2.random.getBytesSync(32));
|
|
5432
|
+
}
|
|
5433
|
+
const ephemeralPublic = secp256k12.getPublicKey(ephemeralPrivate, false);
|
|
5434
|
+
const sharedPoint = secp256k12.getSharedSecret(ephemeralPrivate, recipient);
|
|
5435
|
+
const sharedX = sharedPoint.slice(1, 33);
|
|
5436
|
+
const wrappingKey = hkdf(sha2562, sharedX, V4_EC_SALT, V4_EC_INFO, 32);
|
|
5437
|
+
const nonce = binaryStringToUint8Array(forge2.random.getBytesSync(V4_EC_NONCE_LENGTH));
|
|
5438
|
+
const wrapped = this.encryptAESGCM(wrappingKey, nonce, dataKey);
|
|
5439
|
+
const out = new Uint8Array(ephemeralPublic.length + nonce.length + wrapped.length);
|
|
5440
|
+
out.set(ephemeralPublic, 0);
|
|
5441
|
+
out.set(nonce, ephemeralPublic.length);
|
|
5442
|
+
out.set(wrapped, ephemeralPublic.length + nonce.length);
|
|
5443
|
+
return out;
|
|
5444
|
+
};
|
|
5445
|
+
/**
|
|
5446
|
+
* Wrap the data key under a symmetric key with AES-256-GCM — the inverse of
|
|
5447
|
+
* unwrapV4RecoveryDataKey. Layout: nonce(12) | wrappedDataKey||tag.
|
|
5448
|
+
*
|
|
5449
|
+
* This is deliberately the *same* donor recovery key that v3 uses with AES-CBC: one
|
|
5450
|
+
* key the donor can keep, usable across formats. Reusing it under two modes is safe
|
|
5451
|
+
* here — the modes are never mixed within a record, the key is only ever used to wrap
|
|
5452
|
+
* 32-byte data keys, and GCM is an upgrade over v3's unauthenticated CBC. GCM's
|
|
5453
|
+
* requirement is nonce uniqueness, which a fresh 96-bit random nonce per wrap gives
|
|
5454
|
+
* with an enormous margin at donation volumes (birthday bound ≈ 2^32 wraps per key).
|
|
5455
|
+
*/
|
|
5456
|
+
this.wrapV4RecoveryDataKey = (symmetricKey, dataKey) => {
|
|
5457
|
+
const nonce = binaryStringToUint8Array(forge2.random.getBytesSync(V4_RECOVERY_NONCE_LENGTH));
|
|
5458
|
+
const wrapped = this.encryptAESGCM(symmetricKey, nonce, dataKey);
|
|
5459
|
+
const out = new Uint8Array(nonce.length + wrapped.length);
|
|
5460
|
+
out.set(nonce, 0);
|
|
5461
|
+
out.set(wrapped, nonce.length);
|
|
5462
|
+
return out;
|
|
5463
|
+
};
|
|
5464
|
+
/** encodeV4 assembles the v4 record: header | slots | payloadIv | payloadLen | payload. */
|
|
5465
|
+
this.encodeV4 = (slots, payloadIv, payload) => {
|
|
5466
|
+
if (slots.length > 255) {
|
|
5467
|
+
throw new Error(`encryptV4: at most 255 slots are supported, got ${slots.length}`);
|
|
5468
|
+
}
|
|
5469
|
+
for (const slot of slots) {
|
|
5470
|
+
if (slot.decrypterId > 255 || slot.decrypterId < 0) {
|
|
5471
|
+
throw new Error(`encryptV4: decrypterId ${slot.decrypterId} does not fit in a byte`);
|
|
5472
|
+
}
|
|
5473
|
+
if (slot.keyAlg > 255 || slot.keyAlg < 0) {
|
|
5474
|
+
throw new Error(`encryptV4: keyAlg ${slot.keyAlg} does not fit in a byte`);
|
|
5475
|
+
}
|
|
5476
|
+
if (slot.blob.length > 65535) {
|
|
5477
|
+
throw new Error(
|
|
5478
|
+
`encryptV4: wrapped key of ${slot.blob.length} bytes exceeds the slot limit`
|
|
5479
|
+
);
|
|
5480
|
+
}
|
|
5481
|
+
}
|
|
5482
|
+
let total = 3;
|
|
5483
|
+
for (const slot of slots) {
|
|
5484
|
+
total += 2 + 2 + slot.blob.length;
|
|
5485
|
+
}
|
|
5486
|
+
total += payloadIv.length + 8 + payload.length;
|
|
5487
|
+
const buffer = new ArrayBuffer(total);
|
|
5488
|
+
const view = new DataView(buffer);
|
|
5489
|
+
const bytes = new Uint8Array(buffer);
|
|
5490
|
+
let offset = 0;
|
|
5491
|
+
view.setUint8(offset, HYBRID_ENCRYPTION_VERSION_V4);
|
|
5492
|
+
offset += 1;
|
|
5493
|
+
view.setUint8(offset, V4_PAYLOAD_ALG_AES_GCM);
|
|
5494
|
+
offset += 1;
|
|
5495
|
+
view.setUint8(offset, slots.length);
|
|
5496
|
+
offset += 1;
|
|
5497
|
+
for (const slot of slots) {
|
|
5498
|
+
view.setUint8(offset, slot.decrypterId);
|
|
5499
|
+
offset += 1;
|
|
5500
|
+
view.setUint8(offset, slot.keyAlg);
|
|
5501
|
+
offset += 1;
|
|
5502
|
+
view.setUint16(offset, slot.blob.length, true);
|
|
5503
|
+
offset += 2;
|
|
5504
|
+
bytes.set(slot.blob, offset);
|
|
5505
|
+
offset += slot.blob.length;
|
|
5506
|
+
}
|
|
5507
|
+
bytes.set(payloadIv, offset);
|
|
5508
|
+
offset += payloadIv.length;
|
|
5509
|
+
view.setUint32(offset, payload.length, true);
|
|
5510
|
+
view.setUint32(offset + 4, 0, true);
|
|
5511
|
+
offset += 8;
|
|
5512
|
+
bytes.set(payload, offset);
|
|
5513
|
+
return this.converter.arrayBufferToBase64(buffer);
|
|
5514
|
+
};
|
|
5515
|
+
/** Encrypt with AES-256-GCM (ciphertext||tag) — the inverse of decryptAESGCM. */
|
|
5516
|
+
this.encryptAESGCM = (key, iv, plaintext) => {
|
|
5517
|
+
const cipher2 = forge2.cipher.createCipher("AES-GCM", uint8ArrayToBinaryString(key));
|
|
5518
|
+
cipher2.start({ iv: uint8ArrayToBinaryString(iv) });
|
|
5519
|
+
cipher2.update(forge2.util.createBuffer(uint8ArrayToBinaryString(plaintext)));
|
|
5520
|
+
cipher2.finish();
|
|
5521
|
+
const ciphertext = binaryStringToUint8Array(cipher2.output.getBytes());
|
|
5522
|
+
const tag = binaryStringToUint8Array(cipher2.mode.tag.getBytes());
|
|
5523
|
+
const out = new Uint8Array(ciphertext.length + tag.length);
|
|
5524
|
+
out.set(ciphertext, 0);
|
|
5525
|
+
out.set(tag, ciphertext.length);
|
|
5526
|
+
return out;
|
|
5527
|
+
};
|
|
5528
|
+
/**
|
|
5529
|
+
* Recover the payload data key: walk the record's slots and, for each, try every key
|
|
5530
|
+
* whose (decrypterId, keyAlg) match. Same semantics as go-crypto — a slot addressed to
|
|
5531
|
+
* another recipient is ignored, and several slots for the same recipient (rotated keys)
|
|
5532
|
+
* are all attempted, so an unusable one never masks a usable one.
|
|
5533
|
+
* @param slots The record's decoded slots
|
|
5534
|
+
* @param keys The caller's keychain
|
|
5535
|
+
* @returns The unwrapped 32-byte data key
|
|
5536
|
+
*/
|
|
5537
|
+
this.unwrapV4DataKey = (slots, keys) => {
|
|
5538
|
+
for (const slot of slots) {
|
|
5539
|
+
for (const key of keys) {
|
|
5540
|
+
if (key.decrypterId !== slot.decrypterId || key.keyAlg !== slot.keyAlg) {
|
|
5541
|
+
continue;
|
|
5542
|
+
}
|
|
5543
|
+
try {
|
|
5544
|
+
return this.unwrapV4Slot(slot, key);
|
|
5545
|
+
} catch (e) {
|
|
5546
|
+
}
|
|
5547
|
+
}
|
|
5548
|
+
}
|
|
5549
|
+
throw new Error("No v4 slot could be opened with the given keys");
|
|
5550
|
+
};
|
|
5551
|
+
/** Unwrap one slot with one matching key, dispatching on the key algorithm. */
|
|
5552
|
+
this.unwrapV4Slot = (slot, key) => {
|
|
5553
|
+
switch (key.keyAlg) {
|
|
5554
|
+
case V4_KEY_ALG_EC:
|
|
5555
|
+
return this.unwrapV4EcDataKey(slot.blob, key.key);
|
|
5556
|
+
case V4_KEY_ALG_AES_SYM:
|
|
5557
|
+
return this.unwrapV4RecoveryDataKey(slot.blob, key.key);
|
|
5558
|
+
case V4_KEY_ALG_RSA_OAEP:
|
|
5559
|
+
return this.unwrapV4RsaDataKey(slot.blob, key.key);
|
|
5560
|
+
default:
|
|
5561
|
+
throw new Error(`Unsupported v4 key algorithm ${key.keyAlg}`);
|
|
5562
|
+
}
|
|
5563
|
+
};
|
|
5564
|
+
/**
|
|
5565
|
+
* Unwrap the data key from an RSA-OAEP slot with a PEM private key — the counterpart of
|
|
5566
|
+
* v4RsaSlot, for a receiver-side (non-phone) decrypter.
|
|
5567
|
+
*/
|
|
5568
|
+
this.unwrapV4RsaDataKey = (slot, privateKeyPem) => {
|
|
5569
|
+
const privateKey = forge2.pki.privateKeyFromPem(privateKeyPem);
|
|
5570
|
+
const dataKey = privateKey.decrypt(uint8ArrayToBinaryString(slot), "RSA-OAEP", {
|
|
5571
|
+
md: forge2.md.sha256.create()
|
|
5572
|
+
});
|
|
5573
|
+
return binaryStringToUint8Array(dataKey);
|
|
5574
|
+
};
|
|
5575
|
+
/**
|
|
5576
|
+
* Unwrap the data key from an EC slot via ECIES.
|
|
5577
|
+
* Slot layout: ephPub(65, uncompressed) | nonce(12) | wrappedDataKey||tag(48).
|
|
5578
|
+
*/
|
|
5579
|
+
this.unwrapV4EcDataKey = (ecSlot, privateKey) => {
|
|
5580
|
+
const ephemeralPublicKey = ecSlot.slice(0, V4_UNCOMPRESSED_PUBKEY_LENGTH);
|
|
5581
|
+
const ecNonce = ecSlot.slice(
|
|
5582
|
+
V4_UNCOMPRESSED_PUBKEY_LENGTH,
|
|
5583
|
+
V4_UNCOMPRESSED_PUBKEY_LENGTH + V4_EC_NONCE_LENGTH
|
|
5584
|
+
);
|
|
5585
|
+
const wrappedDataKey = ecSlot.slice(V4_UNCOMPRESSED_PUBKEY_LENGTH + V4_EC_NONCE_LENGTH);
|
|
5586
|
+
const privateKeyBytes = this.converter.base64ToUint8Array(privateKey);
|
|
5587
|
+
const sharedPoint = secp256k12.getSharedSecret(privateKeyBytes, ephemeralPublicKey);
|
|
5588
|
+
const sharedX = sharedPoint.slice(1, 33);
|
|
5589
|
+
const wrappingKey = hkdf(sha2562, sharedX, V4_EC_SALT, V4_EC_INFO, 32);
|
|
5590
|
+
return this.decryptAESGCM(wrappingKey, ecNonce, wrappedDataKey);
|
|
5591
|
+
};
|
|
5592
|
+
/**
|
|
5593
|
+
* Unwrap the data key from an AES recovery slot: the data key wrapped directly under the
|
|
5594
|
+
* base64 AES-256 recovery key (the same key the pre-v4 recovery branch uses).
|
|
5595
|
+
* Slot layout: nonce(12) | wrappedDataKey||tag(48).
|
|
5596
|
+
*/
|
|
5597
|
+
this.unwrapV4RecoveryDataKey = (recoverySlot, recoveryKey) => {
|
|
5598
|
+
const nonce = recoverySlot.slice(0, V4_RECOVERY_NONCE_LENGTH);
|
|
5599
|
+
const wrappedDataKey = recoverySlot.slice(V4_RECOVERY_NONCE_LENGTH);
|
|
5600
|
+
const key = new Uint8Array(this.converter.base64ToArrayBuffer(recoveryKey));
|
|
5601
|
+
return this.decryptAESGCM(key, nonce, wrappedDataKey);
|
|
5602
|
+
};
|
|
5603
|
+
/**
|
|
5604
|
+
* Decrypt an AES-256-GCM ciphertext (ciphertext||tag) with a 12-byte nonce.
|
|
5605
|
+
*/
|
|
5606
|
+
this.decryptAESGCM = (key, iv, ciphertextWithTag) => {
|
|
5607
|
+
const ciphertext = ciphertextWithTag.slice(0, -V4_GCM_TAG_LENGTH);
|
|
5608
|
+
const tag = ciphertextWithTag.slice(-V4_GCM_TAG_LENGTH);
|
|
5609
|
+
const decipher = forge2.cipher.createDecipher("AES-GCM", uint8ArrayToBinaryString(key));
|
|
5610
|
+
decipher.start({
|
|
5611
|
+
iv: uint8ArrayToBinaryString(iv),
|
|
5612
|
+
tag: forge2.util.createBuffer(uint8ArrayToBinaryString(tag))
|
|
5613
|
+
});
|
|
5614
|
+
decipher.update(forge2.util.createBuffer(uint8ArrayToBinaryString(ciphertext)));
|
|
5615
|
+
if (!decipher.finish()) {
|
|
5616
|
+
throw new Error("AES-GCM authentication failed");
|
|
5617
|
+
}
|
|
5618
|
+
return binaryStringToUint8Array(decipher.output.getBytes());
|
|
4698
5619
|
};
|
|
4699
5620
|
this.getDataKey = () => {
|
|
4700
5621
|
return forge2.random.getBytesSync(KEY_LENGTH);
|
|
@@ -4750,25 +5671,20 @@ var HybridEncrypter = class {
|
|
|
4750
5671
|
* @returns The encrypted donation, the used initialization vector and the length of the encrypted donation
|
|
4751
5672
|
*/
|
|
4752
5673
|
this.encryptDonation = (donation, dataKey) => {
|
|
4753
|
-
|
|
4754
|
-
|
|
4755
|
-
|
|
4756
|
-
|
|
4757
|
-
|
|
4758
|
-
|
|
4759
|
-
|
|
4760
|
-
|
|
4761
|
-
|
|
4762
|
-
|
|
4763
|
-
encrypted
|
|
4764
|
-
|
|
4765
|
-
|
|
4766
|
-
|
|
4767
|
-
};
|
|
4768
|
-
} catch (error) {
|
|
4769
|
-
console.error(`[HybridEncrypter] encrypt: ${error}`);
|
|
4770
|
-
return {};
|
|
4771
|
-
}
|
|
5674
|
+
const iv = forge2.random.getBytesSync(AES_BLOCKSIZE);
|
|
5675
|
+
const cipher2 = forge2.cipher.createCipher("AES-GCM", dataKey);
|
|
5676
|
+
cipher2.start({ iv });
|
|
5677
|
+
cipher2.update(forge2.util.createBuffer(utf8ToUint8Array(donation)));
|
|
5678
|
+
cipher2.finish();
|
|
5679
|
+
const encrypted = cipher2.output.getBytes();
|
|
5680
|
+
const tag = cipher2.mode.tag.getBytes();
|
|
5681
|
+
return {
|
|
5682
|
+
iv: binaryStringToUint8Array(iv),
|
|
5683
|
+
encrypted: Buffer10.concat([
|
|
5684
|
+
binaryStringToUint8Array(encrypted),
|
|
5685
|
+
binaryStringToUint8Array(tag)
|
|
5686
|
+
])
|
|
5687
|
+
};
|
|
4772
5688
|
};
|
|
4773
5689
|
this.decryptDonation = (dataKey, encryptedDonation, iv) => {
|
|
4774
5690
|
try {
|
|
@@ -4799,6 +5715,148 @@ var setUint8Array = (dataView, offset, values) => {
|
|
|
4799
5715
|
return [dataView, offset];
|
|
4800
5716
|
};
|
|
4801
5717
|
|
|
5718
|
+
// src/donation/sdk.ts
|
|
5719
|
+
import { Buffer as Buffer11 } from "buffer/";
|
|
5720
|
+
|
|
5721
|
+
// src/donation/blurring.ts
|
|
5722
|
+
import { DateTime as DateTime3 } from "luxon";
|
|
5723
|
+
var blurDocument = (document, anonymization) => {
|
|
5724
|
+
if (!anonymization || !document) {
|
|
5725
|
+
return document;
|
|
5726
|
+
}
|
|
5727
|
+
const blurredDocument = __spreadValues({}, document);
|
|
5728
|
+
if (isQuestionnaireResponse(blurredDocument)) {
|
|
5729
|
+
return blurQuestionnaireResponse(blurredDocument, anonymization);
|
|
5730
|
+
}
|
|
5731
|
+
if (isResearchSubject(blurredDocument)) {
|
|
5732
|
+
return blurResearchSubject(blurredDocument, anonymization);
|
|
5733
|
+
}
|
|
5734
|
+
return __spreadValues({}, blurredDocument);
|
|
5735
|
+
};
|
|
5736
|
+
var blurQuestionnaireResponse = (document, anonymization) => {
|
|
5737
|
+
const blur = anonymization == null ? void 0 : anonymization.blur;
|
|
5738
|
+
if (blur) {
|
|
5739
|
+
return __spreadValues(__spreadValues({}, document), document.authored && {
|
|
5740
|
+
authored: blurAuthored(document.authored, blur.authored)
|
|
5741
|
+
});
|
|
5742
|
+
}
|
|
5743
|
+
return document;
|
|
5744
|
+
};
|
|
5745
|
+
var blurResearchSubject = (document, anonymization) => {
|
|
5746
|
+
const programBlur = anonymization == null ? void 0 : anonymization.blur;
|
|
5747
|
+
if (programBlur && programBlur.researchSubject) {
|
|
5748
|
+
const blurredPeriod = blurPeriod(document.period, programBlur.researchSubject);
|
|
5749
|
+
return __spreadValues(__spreadValues({}, document), blurredPeriod && { period: blurredPeriod });
|
|
5750
|
+
}
|
|
5751
|
+
return document;
|
|
5752
|
+
};
|
|
5753
|
+
var blurAuthored = (authored, blurFn) => {
|
|
5754
|
+
if (!blurFn) {
|
|
5755
|
+
return authored;
|
|
5756
|
+
}
|
|
5757
|
+
const authoredDate = DateTime3.fromISO(authored);
|
|
5758
|
+
if (!authoredDate || !authoredDate.isValid) {
|
|
5759
|
+
return authored;
|
|
5760
|
+
}
|
|
5761
|
+
return blurDate(authoredDate, blurFn).toString();
|
|
5762
|
+
};
|
|
5763
|
+
var blurPeriod = (period, blurFn) => {
|
|
5764
|
+
if (!period) {
|
|
5765
|
+
return void 0;
|
|
5766
|
+
}
|
|
5767
|
+
const startDate = period.start ? DateTime3.fromISO(period.start) : void 0;
|
|
5768
|
+
const endDate = period.end ? DateTime3.fromISO(period.end) : void 0;
|
|
5769
|
+
return __spreadValues(__spreadValues({}, period.start && (startDate && startDate.isValid ? { start: blurDate(startDate, blurFn).toString() } : { start: period.start })), period.end && (endDate && endDate.isValid ? { end: blurDate(endDate, blurFn).toString() } : { end: period.end }));
|
|
5770
|
+
};
|
|
5771
|
+
var blurDate = (date, blurFn) => {
|
|
5772
|
+
const [fn, unit] = blurFn.toLowerCase().split("of");
|
|
5773
|
+
if (!["day", "week", "month"].includes(unit)) {
|
|
5774
|
+
return date;
|
|
5775
|
+
}
|
|
5776
|
+
if (fn === "start") {
|
|
5777
|
+
return date.startOf(unit);
|
|
5778
|
+
} else if (fn === "end") {
|
|
5779
|
+
return date.endOf(unit);
|
|
5780
|
+
}
|
|
5781
|
+
return date;
|
|
5782
|
+
};
|
|
5783
|
+
|
|
5784
|
+
// src/donation/document.ts
|
|
5785
|
+
var StudyNameSystem2 = "http://fhir.data4life.care/r5/CodeSystem/study-name";
|
|
5786
|
+
var filterRecords = (records, programName, donation) => {
|
|
5787
|
+
return records.filter(
|
|
5788
|
+
(record) => record.status === "Deleted" || isValidDocument(record.document, programName)
|
|
5789
|
+
).map((record) => {
|
|
5790
|
+
const blurredDocument = blurDocument(
|
|
5791
|
+
record.document,
|
|
5792
|
+
donation.anonymization
|
|
5793
|
+
);
|
|
5794
|
+
return {
|
|
5795
|
+
document: blurredDocument,
|
|
5796
|
+
status: record.status,
|
|
5797
|
+
id: record.id
|
|
5798
|
+
};
|
|
5799
|
+
});
|
|
5800
|
+
};
|
|
5801
|
+
var isValidDocument = (document, programName) => {
|
|
5802
|
+
if (typeof document !== "object" || document === null) {
|
|
5803
|
+
return false;
|
|
5804
|
+
}
|
|
5805
|
+
if (isQuestionnaireResponse(document)) {
|
|
5806
|
+
const questionnaireResponse = document;
|
|
5807
|
+
return hasValidItems(questionnaireResponse);
|
|
5808
|
+
}
|
|
5809
|
+
if (isResearchSubject(document) && programName) {
|
|
5810
|
+
return isAllowedResearchSubject(document, programName);
|
|
5811
|
+
}
|
|
5812
|
+
if (isObservation(document)) {
|
|
5813
|
+
return true;
|
|
5814
|
+
}
|
|
5815
|
+
if (isConsent(document)) {
|
|
5816
|
+
return true;
|
|
5817
|
+
}
|
|
5818
|
+
if (isRecording(document)) {
|
|
5819
|
+
return true;
|
|
5820
|
+
}
|
|
5821
|
+
if (isCommunication(document)) {
|
|
5822
|
+
return true;
|
|
5823
|
+
}
|
|
5824
|
+
return false;
|
|
5825
|
+
};
|
|
5826
|
+
var isQuestionnaireResponse = (document) => document.resourceType === "QuestionnaireResponse";
|
|
5827
|
+
var isResearchSubject = (document) => document.resourceType === "ResearchSubject";
|
|
5828
|
+
var isObservation = (document) => document.resourceType === "Observation";
|
|
5829
|
+
var isConsent = (document) => document.resourceType === "Consent";
|
|
5830
|
+
var isRecording = (document) => (document == null ? void 0 : document.resourceType) === "Bundle";
|
|
5831
|
+
var isCommunication = (document) => (document == null ? void 0 : document.resourceType) === "Communication";
|
|
5832
|
+
var isAllowedResearchSubject = (document, programName) => {
|
|
5833
|
+
if (!document.study || !document.subject || !document.study.identifier) {
|
|
5834
|
+
return false;
|
|
5835
|
+
}
|
|
5836
|
+
if (document.study.identifier.system !== StudyNameSystem2) {
|
|
5837
|
+
return false;
|
|
5838
|
+
}
|
|
5839
|
+
if (document.study.identifier.value !== programName) {
|
|
5840
|
+
return false;
|
|
5841
|
+
}
|
|
5842
|
+
return true;
|
|
5843
|
+
};
|
|
5844
|
+
var includedResearchSubjects = (records) => {
|
|
5845
|
+
return records.filter((record) => isResearchSubject(record.document));
|
|
5846
|
+
};
|
|
5847
|
+
var hasValidItems = (document) => Array.isArray(document.item) && document.item.every((value) => {
|
|
5848
|
+
const validKeys = Object.keys(value).every(
|
|
5849
|
+
(key) => ["linkId", "text", "answer", "item"].includes(key)
|
|
5850
|
+
);
|
|
5851
|
+
if (!validKeys) {
|
|
5852
|
+
return false;
|
|
5853
|
+
}
|
|
5854
|
+
if (value.answer || value.item) {
|
|
5855
|
+
return Array.isArray(value.answer || value.item);
|
|
5856
|
+
}
|
|
5857
|
+
return true;
|
|
5858
|
+
});
|
|
5859
|
+
|
|
4802
5860
|
// src/donation/utils.ts
|
|
4803
5861
|
var DONOR_IDENTITY_VERSION = 2;
|
|
4804
5862
|
var isPrivateProgram = (program) => {
|
|
@@ -4841,7 +5899,7 @@ var DonationSDK = class {
|
|
|
4841
5899
|
messageJSON: message,
|
|
4842
5900
|
signature: this.converter.uint8ArrayToBase64(signature)
|
|
4843
5901
|
});
|
|
4844
|
-
return
|
|
5902
|
+
return Buffer11.from(signedMessage).toString("base64");
|
|
4845
5903
|
};
|
|
4846
5904
|
/**
|
|
4847
5905
|
* Validate an authentication token which can be used to authenticated with the Donation API
|
|
@@ -5710,16 +6768,16 @@ function mergeContexts(a, b) {
|
|
|
5710
6768
|
}
|
|
5711
6769
|
|
|
5712
6770
|
// src/helper/ulid/index.ts
|
|
5713
|
-
import { Buffer as
|
|
6771
|
+
import { Buffer as Buffer13 } from "buffer/";
|
|
5714
6772
|
|
|
5715
6773
|
// src/helper/ulid/base32.ts
|
|
5716
|
-
import { Buffer as
|
|
6774
|
+
import { Buffer as Buffer12 } from "buffer/";
|
|
5717
6775
|
var B32_CHARACTERS = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
|
|
5718
6776
|
var crockfordEncode = (input) => {
|
|
5719
6777
|
const output = [];
|
|
5720
6778
|
let bitsRead = 0;
|
|
5721
6779
|
let buffer = 0;
|
|
5722
|
-
const reversedInput =
|
|
6780
|
+
const reversedInput = Buffer12.from(input).reverse();
|
|
5723
6781
|
for (const byte of reversedInput) {
|
|
5724
6782
|
buffer |= byte << bitsRead;
|
|
5725
6783
|
bitsRead += 8;
|
|
@@ -5756,7 +6814,7 @@ var crockfordDecode = (input) => {
|
|
|
5756
6814
|
if (bitsRead >= 5 || buffer > 0) {
|
|
5757
6815
|
output.unshift(buffer & 255);
|
|
5758
6816
|
}
|
|
5759
|
-
return
|
|
6817
|
+
return Buffer12.from(output);
|
|
5760
6818
|
};
|
|
5761
6819
|
|
|
5762
6820
|
// src/helper/ulid/index.ts
|
|
@@ -5770,7 +6828,7 @@ function UUIDtoULID(uuid2, opts) {
|
|
|
5770
6828
|
}
|
|
5771
6829
|
throw new Error("UUID to ULID conversion failed: invalid UUID input");
|
|
5772
6830
|
}
|
|
5773
|
-
const ulid = crockfordEncode(
|
|
6831
|
+
const ulid = crockfordEncode(Buffer13.from(uuid2.replace(/-/g, ""), "hex"));
|
|
5774
6832
|
return ulid;
|
|
5775
6833
|
}
|
|
5776
6834
|
function ULIDtoUUID(ulid, opts) {
|
|
@@ -5837,29 +6895,57 @@ var API_TARGET_MAP = {
|
|
|
5837
6895
|
dataEndpoint: "https://api.ouraring.com"
|
|
5838
6896
|
},
|
|
5839
6897
|
sandbox: {
|
|
5840
|
-
//
|
|
5841
|
-
//
|
|
6898
|
+
// No sandbox for Oura — this mirrors prod and is unused (kept only because the
|
|
6899
|
+
// API_TARGET_MAP type requires a `sandbox` entry for every API).
|
|
5842
6900
|
loginEndpoint: "https://cloud.ouraring.com/oauth/authorize",
|
|
5843
6901
|
tokenEndpoint: "https://api.ouraring.com/oauth/token",
|
|
5844
6902
|
dataEndpoint: "https://api.ouraring.com"
|
|
5845
6903
|
}
|
|
6904
|
+
},
|
|
6905
|
+
GARMIN: {
|
|
6906
|
+
// Garmin Connect OAuth2 + PKCE (endpoints confirmed against Garmin's OAuth2 PKCE
|
|
6907
|
+
// spec). The phone only captures the authorization code at the HTTPS redirect; the
|
|
6908
|
+
// data-collector exchanges it server-side and receives the data via ping callbacks.
|
|
6909
|
+
prod: {
|
|
6910
|
+
loginEndpoint: "https://connect.garmin.com/oauth2Confirm",
|
|
6911
|
+
tokenEndpoint: "https://diauth.garmin.com/di-oauth2-service/oauth/token",
|
|
6912
|
+
dataEndpoint: "https://apis.garmin.com"
|
|
6913
|
+
},
|
|
6914
|
+
sandbox: {
|
|
6915
|
+
loginEndpoint: "https://connect.garmin.com/oauth2Confirm",
|
|
6916
|
+
tokenEndpoint: "https://diauth.garmin.com/di-oauth2-service/oauth/token",
|
|
6917
|
+
dataEndpoint: "https://apis.garmin.com"
|
|
6918
|
+
}
|
|
5846
6919
|
}
|
|
5847
6920
|
};
|
|
5848
6921
|
export {
|
|
6922
|
+
AES_BLOCKSIZE,
|
|
5849
6923
|
ANDROID_SENSORS_CONSTANT_NAMES,
|
|
5850
6924
|
API_TARGET_MAP,
|
|
5851
|
-
|
|
6925
|
+
APPLE_HEALTH_ANNOTATION_ACTIVE_ENERGY,
|
|
6926
|
+
APPLE_HEALTH_ANNOTATION_HEART_RATE,
|
|
6927
|
+
APPLE_HEALTH_ANNOTATION_HRV,
|
|
5852
6928
|
APPLE_HEALTH_ANNOTATION_ITEM_ACTIVE_ENERGY,
|
|
6929
|
+
APPLE_HEALTH_ANNOTATION_ITEM_HEART_RATE,
|
|
6930
|
+
APPLE_HEALTH_ANNOTATION_ITEM_HRV,
|
|
6931
|
+
APPLE_HEALTH_ANNOTATION_ITEM_RESPIRATORY_RATE,
|
|
6932
|
+
APPLE_HEALTH_ANNOTATION_ITEM_RESTING_HEART_RATE,
|
|
6933
|
+
APPLE_HEALTH_ANNOTATION_ITEM_SKIN_TEMPERATURE,
|
|
5853
6934
|
APPLE_HEALTH_ANNOTATION_ITEM_SLEEP_DURATION,
|
|
5854
6935
|
APPLE_HEALTH_ANNOTATION_ITEM_SLEEP_STATE,
|
|
6936
|
+
APPLE_HEALTH_ANNOTATION_ITEM_SPO2,
|
|
5855
6937
|
APPLE_HEALTH_ANNOTATION_ITEM_STEP_COUNT,
|
|
5856
6938
|
APPLE_HEALTH_ANNOTATION_ITEM_WALKING_RUNNING_DISTANCE,
|
|
6939
|
+
APPLE_HEALTH_ANNOTATION_RESPIRATORY_RATE,
|
|
6940
|
+
APPLE_HEALTH_ANNOTATION_RESTING_HEART_RATE,
|
|
6941
|
+
APPLE_HEALTH_ANNOTATION_SKIN_TEMPERATURE,
|
|
6942
|
+
APPLE_HEALTH_ANNOTATION_SLEEP_ANALYSIS,
|
|
6943
|
+
APPLE_HEALTH_ANNOTATION_SPO2,
|
|
6944
|
+
APPLE_HEALTH_ANNOTATION_STEP_COUNT,
|
|
6945
|
+
APPLE_HEALTH_ANNOTATION_WALKING_RUNNING_DISTANCE,
|
|
5857
6946
|
APPLE_HEALTH_DATA_SOURCES,
|
|
5858
6947
|
APPLE_HEALTH_SIGNAL_TYPE_ANNOTATIONS,
|
|
5859
|
-
APPLE_HEALTH_SLEEP_ANALYSIS_ANNOTATION,
|
|
5860
6948
|
APPLE_HEALTH_SLEEP_STATE_MAP,
|
|
5861
|
-
APPLE_HEALTH_STEP_COUNT_ANNOTATION,
|
|
5862
|
-
APPLE_HEALTH_WALKING_RUNNING_DISTANCE_ANNOTATION,
|
|
5863
6949
|
ASMSECGMeasurementAnnotation,
|
|
5864
6950
|
ASMSSpO2MeasurementAnnotation,
|
|
5865
6951
|
AnswerCodeGuard,
|
|
@@ -5910,6 +6996,7 @@ export {
|
|
|
5910
6996
|
BoolGuard,
|
|
5911
6997
|
CPMSHealthServicesActivityDetectionAnnotation,
|
|
5912
6998
|
CPMSHealthServicesPassiveDataAnnotation,
|
|
6999
|
+
CollectorClient,
|
|
5913
7000
|
CommunicationTypeSystem,
|
|
5914
7001
|
Conjunction,
|
|
5915
7002
|
ConsentGuard,
|
|
@@ -5971,6 +7058,7 @@ export {
|
|
|
5971
7058
|
FITBIT_SLEEP_LEVEL_MAP,
|
|
5972
7059
|
FITBIT_SPO2_ANNOTATION,
|
|
5973
7060
|
FITBIT_STEPS_ANNOTATION,
|
|
7061
|
+
GARMIN_SIGNAL_TYPE_ANNOTATIONS,
|
|
5974
7062
|
GOOGLE_HEALTH_SIGNAL_TYPE_ANNOTATIONS,
|
|
5975
7063
|
HEALTH_CONNECT_ANNOTATION_ITEM_SLEEP_DURATION,
|
|
5976
7064
|
HEALTH_CONNECT_ANNOTATION_ITEM_SLEEP_STAGE,
|
|
@@ -5980,10 +7068,14 @@ export {
|
|
|
5980
7068
|
HEALTH_CONNECT_SLEEP_ANNOTATION,
|
|
5981
7069
|
HEALTH_CONNECT_SLEEP_STAGE_MAP,
|
|
5982
7070
|
HEALTH_CONNECT_STEPS_ANNOTATION,
|
|
7071
|
+
HYBRID_ENCRYPTION_VERSION_RECOVERY,
|
|
7072
|
+
HYBRID_ENCRYPTION_VERSION_V4,
|
|
7073
|
+
HybridEncrypter,
|
|
5983
7074
|
IMUAnnotation,
|
|
5984
7075
|
ImageExtension,
|
|
5985
7076
|
InternalSensorAnnotationTypes,
|
|
5986
7077
|
Iso3166CodeSystem,
|
|
7078
|
+
KEY_LENGTH,
|
|
5987
7079
|
KeyHandler,
|
|
5988
7080
|
MaxValueExtension,
|
|
5989
7081
|
MeasurementTypeSystem,
|
|
@@ -6039,17 +7131,21 @@ export {
|
|
|
6039
7131
|
SliderStepSizeExtension,
|
|
6040
7132
|
StudyNameSystem,
|
|
6041
7133
|
SurveyGuard,
|
|
7134
|
+
TAG_LENGTH,
|
|
6042
7135
|
TimeFuncs,
|
|
6043
7136
|
TokenNamingSystem,
|
|
6044
7137
|
ULID_REGEX,
|
|
6045
7138
|
ULIDtoUUID,
|
|
6046
7139
|
UUID_REGEX,
|
|
6047
7140
|
UUIDtoULID,
|
|
7141
|
+
V4_DECRYPTER_DATA_RECEIVER,
|
|
7142
|
+
V4_DECRYPTER_MOBILE_APP,
|
|
6048
7143
|
WORDLIST_LANGUAGES,
|
|
6049
7144
|
WearOS3AnnotationMap,
|
|
6050
7145
|
WebConverter,
|
|
6051
7146
|
YearGuard,
|
|
6052
7147
|
binaryStringToUint8Array,
|
|
7148
|
+
buildSelfSignedAuthToken,
|
|
6053
7149
|
calculateQuestionnaireScoreRange,
|
|
6054
7150
|
calculateScoreRangeForItem,
|
|
6055
7151
|
capitalizeFirstLetter,
|
|
@@ -6093,6 +7189,17 @@ export {
|
|
|
6093
7189
|
toQuestionnaireResponseItem,
|
|
6094
7190
|
uint8ArrayToBinaryString,
|
|
6095
7191
|
utf8ToUint8Array,
|
|
7192
|
+
v4AesKey,
|
|
7193
|
+
v4AesSlot,
|
|
7194
|
+
v4EcKey,
|
|
7195
|
+
v4EcSlot,
|
|
7196
|
+
v4RsaKey,
|
|
7197
|
+
v4RsaSlot,
|
|
6096
7198
|
wordlists
|
|
6097
7199
|
};
|
|
7200
|
+
/*! Bundled license information:
|
|
7201
|
+
|
|
7202
|
+
@noble/hashes/esm/utils.js:
|
|
7203
|
+
(*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
7204
|
+
*/
|
|
6098
7205
|
//# sourceMappingURL=index.mjs.map
|