@welshare/react 0.0.1-alpha.2 → 0.1.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.
Files changed (38) hide show
  1. package/README.md +44 -0
  2. package/dist/esm/hooks/use-welshare.d.ts +5 -0
  3. package/dist/esm/hooks/use-welshare.d.ts.map +1 -1
  4. package/dist/esm/hooks/use-welshare.js +113 -4
  5. package/dist/esm/index.d.ts +3 -0
  6. package/dist/esm/index.d.ts.map +1 -1
  7. package/dist/esm/index.js +4 -1
  8. package/dist/esm/lib/encryption.d.ts +19 -0
  9. package/dist/esm/lib/encryption.d.ts.map +1 -0
  10. package/dist/esm/lib/encryption.js +61 -0
  11. package/dist/esm/lib/uploads.d.ts +3 -0
  12. package/dist/esm/lib/uploads.d.ts.map +1 -0
  13. package/dist/esm/lib/uploads.js +17 -0
  14. package/dist/esm/types.d.ts +31 -1
  15. package/dist/esm/types.d.ts.map +1 -1
  16. package/dist/node_modules/@welshare/react/.DS_Store +0 -0
  17. package/dist/node_modules/@welshare/react/README.md +44 -0
  18. package/dist/node_modules/@welshare/react/dist/esm/hooks/use-welshare.d.ts +5 -0
  19. package/dist/node_modules/@welshare/react/dist/esm/hooks/use-welshare.d.ts.map +1 -1
  20. package/dist/node_modules/@welshare/react/dist/esm/hooks/use-welshare.js +113 -4
  21. package/dist/node_modules/@welshare/react/dist/esm/index.d.ts +3 -0
  22. package/dist/node_modules/@welshare/react/dist/esm/index.d.ts.map +1 -1
  23. package/dist/node_modules/@welshare/react/dist/esm/index.js +4 -1
  24. package/dist/node_modules/@welshare/react/dist/esm/lib/encryption.d.ts +19 -0
  25. package/dist/node_modules/@welshare/react/dist/esm/lib/encryption.d.ts.map +1 -0
  26. package/dist/node_modules/@welshare/react/dist/esm/lib/encryption.js +61 -0
  27. package/dist/node_modules/@welshare/react/dist/esm/lib/uploads.d.ts +3 -0
  28. package/dist/node_modules/@welshare/react/dist/esm/lib/uploads.d.ts.map +1 -0
  29. package/dist/node_modules/@welshare/react/dist/esm/lib/uploads.js +17 -0
  30. package/dist/node_modules/@welshare/react/dist/esm/types.d.ts +31 -1
  31. package/dist/node_modules/@welshare/react/dist/esm/types.d.ts.map +1 -1
  32. package/dist/node_modules/@welshare/react/package.json +1 -1
  33. package/dist/node_modules/@welshare/react/src/hooks/use-welshare.ts +153 -4
  34. package/dist/node_modules/@welshare/react/src/index.ts +13 -5
  35. package/dist/node_modules/@welshare/react/src/lib/encryption.ts +110 -0
  36. package/dist/node_modules/@welshare/react/src/lib/uploads.ts +29 -0
  37. package/dist/node_modules/@welshare/react/src/types.ts +37 -1
  38. package/package.json +1 -1
package/README.md CHANGED
@@ -81,6 +81,49 @@ export function QuestionnaireForm() {
81
81
  }
82
82
  ```
83
83
 
84
+ ### Binary file uploads (e.g. images)
85
+
86
+ binary file uploads require a lot of back and forth with the wallet dialog that we wrapped into one convenient upload API. If you want to include binary uploads into your questionnaires, you would typically hook into your own form, upload the file using the `uploadFile` function exposed by the `useWelshare` hook and use the response information to in the respective questionnaire form answer item.
87
+
88
+ Each download should contain a reference to the resource that initiated its upload. As Welshare right now is mostly about questionnaires, you should use a combination of the resource type (questionnaire), the questionnaire id and the answer item's id
89
+
90
+ ```ts
91
+ const reference = `questionnaire/${questionnaireId}/${answerItemId}`;
92
+ ```
93
+
94
+ Binary files are addressed as items of type `valueAttachment` in Fhir. See https://www.hl7.org/fhir/questionnaireresponse.html
95
+
96
+ Before uploading, welshare encrypts all files with a new random symmetric AES (GCM / 256 bits) key. Users request a presigned upload url and post the encrypted file to an S3 compatible API of ours. Finally, they encrypt the encryption key on a user controlled Nillion *owned* collection for binary data and grant respective access rights for the application. The application a user used to upload the file is by default able to download the file again (Technically, that application is always welshare right now. This will change to the "builder" address of the respective app and the hpmp enclave keys, which allow AI access to the files)
97
+
98
+ Here's an example how to use it:
99
+
100
+ ```ts
101
+ const { isConnected, openWallet, uploadFile, submitData } = useWelshare({
102
+ applicationId: process.env.NEXT_PUBLIC_WELSHARE_APP_ID || ""
103
+ })
104
+ //... let users select a file on their box
105
+
106
+ const { url: uploadedFileUrl, binaryFileUid } = await uploadFile(
107
+ userFile,
108
+ reference: `questionnaire/${questionnaireId}/<linkId>`
109
+ );
110
+
111
+ const responseItem = {
112
+ answer = [
113
+ {
114
+ valueAttachment: {
115
+ id: binaryFileUid,
116
+ contentType: userFile.type,
117
+ size: userFile.size,
118
+ title: userFile.name,
119
+ url: uploadedFileUrl,
120
+ },
121
+ },
122
+ ];
123
+ }
124
+ // insert the responseItem into your QuestionnaireResponse
125
+ ```
126
+
84
127
  ## API
85
128
 
86
129
  ### supported callbacks
@@ -94,6 +137,7 @@ those are configured in the `useWelshare` options parameter and called back duri
94
137
  onSessionReady?: (sessionPubKey: string) => void;
95
138
  onDialogClosing?: () => void;
96
139
  onDialogReady?: () => void;
140
+ onFileUploaded?: (insertedUid: string, url: string) => void
97
141
  }
98
142
  ```
99
143
 
@@ -4,7 +4,12 @@ export declare const useWelshare: (props: WelshareConnectionOptions) => {
4
4
  storageKey: string | undefined;
5
5
  openWallet: () => void;
6
6
  isDialogOpen: boolean;
7
+ uploadFile: (file: File, reference: string) => Promise<{
8
+ url: string;
9
+ binaryFileUid: string;
10
+ }>;
7
11
  submitData: <T>(schemaId: SubmissionSchemaId, submission: SubmissionPayload<T>) => void;
12
+ uploadState: "preparing" | "uploading" | "anchoring" | "finished" | "error" | undefined;
8
13
  isSubmitting: boolean;
9
14
  };
10
15
  //# sourceMappingURL=use-welshare.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"use-welshare.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-welshare.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,iBAAiB,EACjB,kBAAkB,EAClB,yBAAyB,EAC1B,MAAM,YAAY,CAAC;AAGpB,eAAO,MAAM,WAAW,UAAW,yBAAyB;;;;;iBAkFtC,CAAC,YACT,kBAAkB,cAChB,iBAAiB,CAAC,CAAC,CAAC;;CAuDnC,CAAC"}
1
+ {"version":3,"file":"use-welshare.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-welshare.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,iBAAiB,EACjB,kBAAkB,EAElB,yBAAyB,EAC1B,MAAM,YAAY,CAAC;AAIpB,eAAO,MAAM,WAAW,UAAW,yBAAyB;;;;;uBAoLlD,IAAI,aACC,MAAM,KAChB,OAAO,CAAC;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,aAAa,EAAE,MAAM,CAAA;KAAE,CAAC;iBA0C9B,CAAC,YACT,kBAAkB,cAChB,iBAAiB,CAAC,CAAC,CAAC;;;CAyDnC,CAAC"}
@@ -1,10 +1,14 @@
1
- import { useEffect, useState } from "react";
1
+ import { useEffect, useRef, useState } from "react";
2
+ import { encryptAndUploadFile } from "../lib/uploads.js";
2
3
  export const useWelshare = (props) => {
3
4
  const [storageKey, setStorageKey] = useState();
4
5
  const [isDialogOpen, setIsDialogOpen] = useState(false);
5
6
  const [dialogWindow, setDialogWindow] = useState(null);
6
7
  const [messageIdCounter, setMessageIdCounter] = useState(0);
7
8
  const [isSubmitting, setIsSubmitting] = useState(false);
9
+ const [uploadState, setUploadState] = useState();
10
+ // (claude opus) Use ref to control current upload in effect without triggering re-renders
11
+ const currentUploadRef = useRef(null);
8
12
  const options = {
9
13
  environment: "development",
10
14
  apiBaseUrl: "https://wallet.welshare.app",
@@ -12,7 +16,7 @@ export const useWelshare = (props) => {
12
16
  };
13
17
  const WELSHARE_WALLET_URL = `${options.apiBaseUrl}/wallet-external`;
14
18
  useEffect(() => {
15
- const handleMessage = (event) => {
19
+ const handleMessage = async (event) => {
16
20
  // Verify origin for security
17
21
  if (event.origin !== new URL(WELSHARE_WALLET_URL).origin) {
18
22
  return;
@@ -24,6 +28,10 @@ export const useWelshare = (props) => {
24
28
  errorMessage = message.payload.error || "An unknown error occurred";
25
29
  console.error("Welshare Wallet sent an error:", errorMessage);
26
30
  setIsSubmitting(false);
31
+ // Reject the promise if there's an ongoing upload
32
+ if (currentUploadRef.current?.uploadPromise) {
33
+ currentUploadRef.current.uploadPromise.reject(new Error(errorMessage));
34
+ }
27
35
  options.callbacks.onError?.(errorMessage);
28
36
  break;
29
37
  case "DIALOG_READY":
@@ -46,6 +54,10 @@ export const useWelshare = (props) => {
46
54
  console.debug("Welshare Wallet Dialog is closing");
47
55
  setStorageKey(undefined);
48
56
  setIsDialogOpen(false);
57
+ // Reject any pending upload promises
58
+ if (currentUploadRef.current?.uploadPromise) {
59
+ currentUploadRef.current.uploadPromise.reject(new Error("Dialog closed before upload completed"));
60
+ }
49
61
  options.callbacks.onDialogClosing?.();
50
62
  break;
51
63
  case "DATA_UPLOADED":
@@ -53,6 +65,62 @@ export const useWelshare = (props) => {
53
65
  setIsSubmitting(false);
54
66
  options.callbacks.onUploaded?.(message.payload);
55
67
  break;
68
+ //todo: make this work for batches, too
69
+ case "UPLOAD_CREDENTIALS_CREATED":
70
+ const credentials = message.payload;
71
+ console.debug("upload credentials created", credentials);
72
+ if (!currentUploadRef.current) {
73
+ throw new Error("No upload in progress");
74
+ }
75
+ if (!dialogWindow) {
76
+ currentUploadRef.current.uploadPromise.reject(new Error("(UPLOAD_CREDENTIALS_CREATED) Dialog window not open"));
77
+ return;
78
+ }
79
+ try {
80
+ setUploadState("uploading");
81
+ const encodedEncryptionKey = await encryptAndUploadFile(currentUploadRef.current.file, credentials.presignedUrl);
82
+ setUploadState("anchoring");
83
+ const binarySubmissionPayload = {
84
+ timestamp: currentUploadRef.current.timestamp,
85
+ applicationId: options.applicationId,
86
+ reference: currentUploadRef.current.reference,
87
+ fileName: currentUploadRef.current.fileName,
88
+ fileType: currentUploadRef.current.fileType,
89
+ encryptionKey: encodedEncryptionKey,
90
+ fileSize: currentUploadRef.current.file.size,
91
+ url: `welshare://${credentials.uploadKey}`,
92
+ };
93
+ //write to Nillion
94
+ const message = {
95
+ type: "SUBMIT_BINARY_DATA",
96
+ id: String(messageIdCounter),
97
+ payload: binarySubmissionPayload,
98
+ };
99
+ dialogWindow.postMessage(message, WELSHARE_WALLET_URL);
100
+ setMessageIdCounter((prev) => prev + 1);
101
+ }
102
+ catch (error) {
103
+ console.error("error uploading file", error);
104
+ // Update status to error
105
+ currentUploadRef.current.uploadPromise.reject(error);
106
+ setUploadState("error");
107
+ // Call error callback
108
+ const errorMsg = error instanceof Error ? error.message : "Upload failed";
109
+ options.callbacks.onError?.(errorMsg);
110
+ }
111
+ break;
112
+ //{insertetUid, errors}
113
+ case "BINARY_DATA_SUBMITTED":
114
+ if (!currentUploadRef.current) {
115
+ throw new Error("No upload in progress");
116
+ }
117
+ currentUploadRef.current.uploadPromise.resolve({
118
+ url: message.payload.url,
119
+ binaryFileUid: message.payload.insertedUid,
120
+ });
121
+ setUploadState("finished");
122
+ options.callbacks.onFileUploaded?.(message.payload.insertedUid, message.payload.url);
123
+ break;
56
124
  default:
57
125
  console.log("Received unexpected message from Welshare Wallet:", message);
58
126
  }
@@ -61,14 +129,53 @@ export const useWelshare = (props) => {
61
129
  return () => {
62
130
  window.removeEventListener("message", handleMessage);
63
131
  };
64
- }, [WELSHARE_WALLET_URL, options.callbacks]);
132
+ }, [WELSHARE_WALLET_URL, dialogWindow, messageIdCounter, options.applicationId, options.callbacks]);
133
+ /**
134
+ * Starts a file upload and returns a promise that resolves with the uploaded file URL
135
+ * @param file The file to upload
136
+ * @param reference string A reference identifier for the upload
137
+ * @returns Promise<{ url: string; binaryFileUid: string }> url usually starts with welshare:// and resolves to an encrypted storage location
138
+ */
139
+ const uploadFile = (file, reference) => {
140
+ return new Promise((resolve, reject) => {
141
+ if (!dialogWindow) {
142
+ reject(new Error("(uploadFile) Dialog window not open"));
143
+ return;
144
+ }
145
+ const payload = {
146
+ timestamp: new Date(),
147
+ applicationId: options.applicationId,
148
+ reference: reference,
149
+ fileName: file.name,
150
+ fileType: file.type,
151
+ };
152
+ setUploadState("preparing");
153
+ // (claude opus) Store the upload with the file and promise handlers on a ref
154
+ currentUploadRef.current = {
155
+ ...payload,
156
+ file,
157
+ reference,
158
+ uploadPromise: { resolve, reject },
159
+ };
160
+ const message = {
161
+ type: "REQUEST_UPLOAD_CREDENTIALS",
162
+ id: String(messageIdCounter),
163
+ payload: {
164
+ ...payload,
165
+ applicationId: options.applicationId,
166
+ },
167
+ };
168
+ dialogWindow.postMessage(message, WELSHARE_WALLET_URL);
169
+ setMessageIdCounter((prev) => prev + 1);
170
+ });
171
+ };
65
172
  /**
66
173
  * @param schemaType a welshare schema type uid
67
174
  * @param submission your submission that validates against the schema type
68
175
  */
69
176
  const submitData = (schemaId, submission) => {
70
177
  if (!dialogWindow) {
71
- throw new Error("Dialog window not open");
178
+ throw new Error("(submitData) Dialog window not open");
72
179
  }
73
180
  const submissionPayload = {
74
181
  applicationId: options.applicationId,
@@ -105,7 +212,9 @@ export const useWelshare = (props) => {
105
212
  storageKey,
106
213
  openWallet,
107
214
  isDialogOpen,
215
+ uploadFile,
108
216
  submitData,
217
+ uploadState,
109
218
  isSubmitting,
110
219
  };
111
220
  };
@@ -1,7 +1,10 @@
1
1
  export { ConnectWelshareButton } from "./components/connect-button.js";
2
2
  export { useWelshare } from "./hooks/use-welshare.js";
3
+ export { decodeEncryptionKey, decrypt, encodeEncryptionKey, encryptFile, generateRandomAESKey, type EncryptionKey } from "./lib/encryption.js";
4
+ export { encryptAndUploadFile } from "./lib/uploads.js";
3
5
  export declare const Schemas: {
4
6
  QuestionnaireResponse: string;
5
7
  ReflexSubmission: string;
8
+ BinaryFile: string;
6
9
  };
7
10
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qBAAqB,EAAE,MAAM,gCAAgC,CAAC;AAEvE,OAAO,EACL,WAAW,EACZ,MAAM,yBAAyB,CAAC;AAGjC,eAAO,MAAM,OAAO;;;CAGnB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qBAAqB,EAAE,MAAM,gCAAgC,CAAC;AAEvE,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAEtD,OAAO,EACL,mBAAmB,EACnB,OAAO,EACP,mBAAmB,EACnB,WAAW,EACX,oBAAoB,EACpB,KAAK,aAAa,EACnB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AAExD,eAAO,MAAM,OAAO;;;;CAInB,CAAC"}
package/dist/esm/index.js CHANGED
@@ -3,8 +3,11 @@
3
3
  export { ConnectWelshareButton } from "./components/connect-button.js";
4
4
  // ---- hooks ----
5
5
  export { useWelshare } from "./hooks/use-welshare.js";
6
+ export { decodeEncryptionKey, decrypt, encodeEncryptionKey, encryptFile, generateRandomAESKey } from "./lib/encryption.js";
7
+ export { encryptAndUploadFile } from "./lib/uploads.js";
6
8
  //todo: import them from the SDK or a dedicated SDK constants export
7
9
  export const Schemas = {
8
10
  QuestionnaireResponse: "b14b538f-7de3-4767-ad77-464d755d78bd", //QuestionnaireResponseSchema.schemaUid,
9
- ReflexSubmission: "f5cf2d8a-1f78-4f21-b4bd-082e983b830c" //ReflexSubmissionSchema.schemaUid,
11
+ ReflexSubmission: "f5cf2d8a-1f78-4f21-b4bd-082e983b830c", //ReflexSubmissionSchema.schemaUid,
12
+ BinaryFile: "9d696baf-483f-4cc0-b748-23a22c1705f5" //BinaryFilesSchema.schemaUid,
10
13
  };
@@ -0,0 +1,19 @@
1
+ export declare const ALGORITHM = "AES-GCM";
2
+ export type Algorithm = "AES-GCM";
3
+ export declare const generateRandomAESKey: () => Promise<CryptoKey>;
4
+ export declare const encryptFile: (file: File, key: CryptoKey) => Promise<{
5
+ encryptedData: ArrayBuffer;
6
+ iv: Uint8Array;
7
+ }>;
8
+ export type EncryptionKey = {
9
+ algorithm: Algorithm;
10
+ key: string;
11
+ iv: string;
12
+ };
13
+ export declare const encodeEncryptionKey: (key: CryptoKey, iv: Uint8Array) => Promise<EncryptionKey>;
14
+ export declare const decodeEncryptionKey: (encryptionKey: EncryptionKey) => {
15
+ key: Uint8Array<ArrayBuffer>;
16
+ iv: Uint8Array<ArrayBuffer>;
17
+ };
18
+ export declare const decrypt: (encryptedData: ArrayBuffer, encryptionKey: EncryptionKey) => Promise<ArrayBuffer | null>;
19
+ //# sourceMappingURL=encryption.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"encryption.d.ts","sourceRoot":"","sources":["../../../src/lib/encryption.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,SAAS,YAAY,CAAC;AACnC,MAAM,MAAM,SAAS,GAAG,SAAS,CAAC;AAElC,eAAO,MAAM,oBAAoB,QAAa,OAAO,CAAC,SAAS,CAU9D,CAAC;AAIF,eAAO,MAAM,WAAW,SAChB,IAAI,OACL,SAAS,KACb,OAAO,CAAC;IAAE,aAAa,EAAE,WAAW,CAAC;IAAC,EAAE,EAAE,UAAU,CAAA;CAAE,CAgBxD,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG;IAC1B,SAAS,EAAE,SAAS,CAAC;IACrB,GAAG,EAAE,MAAM,CAAC;IACZ,EAAE,EAAE,MAAM,CAAC;CACZ,CAAC;AAEF,eAAO,MAAM,mBAAmB,QACzB,SAAS,MACV,UAAU,KACb,OAAO,CAAC,aAAa,CAgBvB,CAAC;AAEF,eAAO,MAAM,mBAAmB,kBACf,aAAa,KAC3B;IAAE,GAAG,EAAE,UAAU,CAAC,WAAW,CAAC,CAAC;IAAC,EAAE,EAAE,UAAU,CAAC,WAAW,CAAC,CAAA;CAU7D,CAAC;AAGF,eAAO,MAAM,OAAO,kBACH,WAAW,iBACX,aAAa,KAC3B,OAAO,CAAC,WAAW,GAAG,IAAI,CA0B5B,CAAC"}
@@ -0,0 +1,61 @@
1
+ export const ALGORITHM = "AES-GCM";
2
+ export const generateRandomAESKey = async () => {
3
+ // Generate a 256-bit AES-GCM key for file encryption
4
+ return window.crypto.subtle.generateKey({
5
+ name: ALGORITHM,
6
+ length: 256, // 256-bit key
7
+ }, true, // Key is extractable (needed for storage/transmission)
8
+ ["encrypt", "decrypt"] // Key usage
9
+ );
10
+ };
11
+ /// also Generates random IV (12 bytes for AES-GCM)
12
+ /// @return {arraybuffer ciphertext, uint8array iv}
13
+ export const encryptFile = async (file, key) => {
14
+ // Read file as ArrayBuffer
15
+ const fileData = await file.arrayBuffer();
16
+ const iv = window.crypto.getRandomValues(new Uint8Array(12));
17
+ // Encrypt the file data
18
+ const encryptedData = await window.crypto.subtle.encrypt({
19
+ name: ALGORITHM,
20
+ iv: iv,
21
+ }, key, fileData);
22
+ return { encryptedData, iv };
23
+ };
24
+ export const encodeEncryptionKey = async (key, iv) => {
25
+ // Export the key as raw bytes
26
+ const exportedKey = await window.crypto.subtle.exportKey("raw", key);
27
+ const keyHex = Array.from(new Uint8Array(exportedKey))
28
+ .map((b) => b.toString(16).padStart(2, "0"))
29
+ .join("");
30
+ const ivHex = Array.from(iv)
31
+ .map((b) => b.toString(16).padStart(2, "0"))
32
+ .join("");
33
+ return {
34
+ algorithm: ALGORITHM,
35
+ key: keyHex,
36
+ iv: ivHex,
37
+ };
38
+ };
39
+ export const decodeEncryptionKey = (encryptionKey) => {
40
+ const keyBytes = new Uint8Array(encryptionKey.key
41
+ .match(/.{1,2}/g)
42
+ .map((byte) => parseInt(byte, 16)));
43
+ const ivBytes = new Uint8Array(encryptionKey.iv.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
44
+ return { key: keyBytes, iv: ivBytes };
45
+ };
46
+ // Helper function to decrypt a file using encoded encryption key
47
+ export const decrypt = async (encryptedData, encryptionKey) => {
48
+ try {
49
+ const { key: keyBytes, iv } = decodeEncryptionKey(encryptionKey);
50
+ const key = await window.crypto.subtle.importKey("raw", keyBytes, { name: ALGORITHM }, false, ["decrypt"]);
51
+ const decryptedData = await window.crypto.subtle.decrypt({
52
+ name: ALGORITHM,
53
+ iv,
54
+ }, key, encryptedData);
55
+ return decryptedData;
56
+ }
57
+ catch (error) {
58
+ console.error("Failed to decrypt file:", error);
59
+ return null;
60
+ }
61
+ };
@@ -0,0 +1,3 @@
1
+ import { EncryptionKey } from "./encryption.js";
2
+ export declare const encryptAndUploadFile: (file: File, presignedUrl: string) => Promise<EncryptionKey>;
3
+ //# sourceMappingURL=uploads.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uploads.d.ts","sourceRoot":"","sources":["../../../src/lib/uploads.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,aAAa,EAEd,MAAM,iBAAiB,CAAC;AAEzB,eAAO,MAAM,oBAAoB,SACzB,IAAI,gBACI,MAAM,KACnB,OAAO,CAAC,aAAa,CAkBvB,CAAC"}
@@ -0,0 +1,17 @@
1
+ import { encodeEncryptionKey, encryptFile, generateRandomAESKey, } from "./encryption.js";
2
+ export const encryptAndUploadFile = async (file, presignedUrl) => {
3
+ const encryptionKey = await generateRandomAESKey();
4
+ const { encryptedData, iv } = await encryptFile(file, encryptionKey);
5
+ // Upload encrypted file to S3
6
+ const uploadResponse = await fetch(presignedUrl, {
7
+ method: "PUT",
8
+ body: encryptedData,
9
+ headers: {
10
+ "Content-Type": file.type,
11
+ },
12
+ });
13
+ if (!uploadResponse.ok) {
14
+ throw new Error(`Failed to upload file ${uploadResponse.status}`);
15
+ }
16
+ return encodeEncryptionKey(encryptionKey, iv);
17
+ };
@@ -1,3 +1,4 @@
1
+ import { EncryptionKey } from "./lib/encryption.js";
1
2
  export interface DialogMessage {
2
3
  type: string;
3
4
  payload?: any;
@@ -14,14 +15,43 @@ export interface SubmissionPayload<T> {
14
15
  schemaId: SubmissionSchemaId;
15
16
  submission: T;
16
17
  }
18
+ export type UploadCredentials = {
19
+ presignedUrl: string;
20
+ uploadKey: string;
21
+ };
22
+ export interface RequestUploadCredentialsPayload {
23
+ timestamp?: Date;
24
+ applicationId: string;
25
+ reference: string;
26
+ fileName: string;
27
+ fileType: string;
28
+ }
29
+ export interface BinaryFileSubmissionPayload extends RequestUploadCredentialsPayload {
30
+ encryptionKey: EncryptionKey;
31
+ fileSize: number;
32
+ url: string;
33
+ }
34
+ export interface RunningFileUpload extends RequestUploadCredentialsPayload {
35
+ credentials?: UploadCredentials;
36
+ file: File;
37
+ reference: string;
38
+ uploadPromise: {
39
+ resolve: (result: {
40
+ url: string;
41
+ binaryFileUid: string;
42
+ }) => void;
43
+ reject: (error: Error) => void;
44
+ };
45
+ }
17
46
  export interface DataSubmissionDialogMessage extends DialogMessage {
18
- payload: SubmissionPayload<unknown>;
47
+ payload: SubmissionPayload<unknown> | BinaryFileSubmissionPayload | RequestUploadCredentialsPayload;
19
48
  }
20
49
  export interface WelshareConnectionOptions {
21
50
  applicationId: string;
22
51
  apiBaseUrl?: string;
23
52
  environment?: WelshareEnvironment;
24
53
  callbacks: {
54
+ onFileUploaded?: (insertedUid: string, url: string) => void;
25
55
  onUploaded?: (payload: SubmissionPayload<unknown>) => void;
26
56
  onError?: (error: string) => void;
27
57
  onSessionReady?: (sessionPubKey: string) => void;
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,EAAE,GAAG,CAAC;IACd,EAAE,CAAC,EAAE,MAAM,CAAC;CACb;AAED,MAAM,MAAM,mBAAmB,GAAG,aAAa,GAAG,YAAY,CAAC;AAE/D;;GAEG;AACH,MAAM,MAAM,kBAAkB,GAAG,MAAM,CAAA;AAEvC,MAAM,WAAW,iBAAiB,CAAC,CAAC;IAClC,aAAa,EAAE,MAAM,CAAC;IACtB,SAAS,EAAE,IAAI,CAAC;IAChB,QAAQ,EAAE,kBAAkB,CAAC;IAC7B,UAAU,EAAE,CAAC,CAAC;CACf;AAED,MAAM,WAAW,2BAA4B,SAAQ,aAAa;IAChE,OAAO,EAAE,iBAAiB,CAAC,OAAO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AAM,IAAI,CAAC;KAC5B,CAAC;CACH"}
@@ -81,6 +81,49 @@ export function QuestionnaireForm() {
81
81
  }
82
82
  ```
83
83
 
84
+ ### Binary file uploads (e.g. images)
85
+
86
+ binary file uploads require a lot of back and forth with the wallet dialog that we wrapped into one convenient upload API. If you want to include binary uploads into your questionnaires, you would typically hook into your own form, upload the file using the `uploadFile` function exposed by the `useWelshare` hook and use the response information to in the respective questionnaire form answer item.
87
+
88
+ Each download should contain a reference to the resource that initiated its upload. As Welshare right now is mostly about questionnaires, you should use a combination of the resource type (questionnaire), the questionnaire id and the answer item's id
89
+
90
+ ```ts
91
+ const reference = `questionnaire/${questionnaireId}/${answerItemId}`;
92
+ ```
93
+
94
+ Binary files are addressed as items of type `valueAttachment` in Fhir. See https://www.hl7.org/fhir/questionnaireresponse.html
95
+
96
+ Before uploading, welshare encrypts all files with a new random symmetric AES (GCM / 256 bits) key. Users request a presigned upload url and post the encrypted file to an S3 compatible API of ours. Finally, they encrypt the encryption key on a user controlled Nillion *owned* collection for binary data and grant respective access rights for the application. The application a user used to upload the file is by default able to download the file again (Technically, that application is always welshare right now. This will change to the "builder" address of the respective app and the hpmp enclave keys, which allow AI access to the files)
97
+
98
+ Here's an example how to use it:
99
+
100
+ ```ts
101
+ const { isConnected, openWallet, uploadFile, submitData } = useWelshare({
102
+ applicationId: process.env.NEXT_PUBLIC_WELSHARE_APP_ID || ""
103
+ })
104
+ //... let users select a file on their box
105
+
106
+ const { url: uploadedFileUrl, binaryFileUid } = await uploadFile(
107
+ userFile,
108
+ reference: `questionnaire/${questionnaireId}/<linkId>`
109
+ );
110
+
111
+ const responseItem = {
112
+ answer = [
113
+ {
114
+ valueAttachment: {
115
+ id: binaryFileUid,
116
+ contentType: userFile.type,
117
+ size: userFile.size,
118
+ title: userFile.name,
119
+ url: uploadedFileUrl,
120
+ },
121
+ },
122
+ ];
123
+ }
124
+ // insert the responseItem into your QuestionnaireResponse
125
+ ```
126
+
84
127
  ## API
85
128
 
86
129
  ### supported callbacks
@@ -94,6 +137,7 @@ those are configured in the `useWelshare` options parameter and called back duri
94
137
  onSessionReady?: (sessionPubKey: string) => void;
95
138
  onDialogClosing?: () => void;
96
139
  onDialogReady?: () => void;
140
+ onFileUploaded?: (insertedUid: string, url: string) => void
97
141
  }
98
142
  ```
99
143
 
@@ -4,7 +4,12 @@ export declare const useWelshare: (props: WelshareConnectionOptions) => {
4
4
  storageKey: string | undefined;
5
5
  openWallet: () => void;
6
6
  isDialogOpen: boolean;
7
+ uploadFile: (file: File, reference: string) => Promise<{
8
+ url: string;
9
+ binaryFileUid: string;
10
+ }>;
7
11
  submitData: <T>(schemaId: SubmissionSchemaId, submission: SubmissionPayload<T>) => void;
12
+ uploadState: "preparing" | "uploading" | "anchoring" | "finished" | "error" | undefined;
8
13
  isSubmitting: boolean;
9
14
  };
10
15
  //# sourceMappingURL=use-welshare.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"use-welshare.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-welshare.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,iBAAiB,EACjB,kBAAkB,EAClB,yBAAyB,EAC1B,MAAM,YAAY,CAAC;AAGpB,eAAO,MAAM,WAAW,UAAW,yBAAyB;;;;;iBAkFtC,CAAC,YACT,kBAAkB,cAChB,iBAAiB,CAAC,CAAC,CAAC;;CAuDnC,CAAC"}
1
+ {"version":3,"file":"use-welshare.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-welshare.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,iBAAiB,EACjB,kBAAkB,EAElB,yBAAyB,EAC1B,MAAM,YAAY,CAAC;AAIpB,eAAO,MAAM,WAAW,UAAW,yBAAyB;;;;;uBAoLlD,IAAI,aACC,MAAM,KAChB,OAAO,CAAC;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,aAAa,EAAE,MAAM,CAAA;KAAE,CAAC;iBA0C9B,CAAC,YACT,kBAAkB,cAChB,iBAAiB,CAAC,CAAC,CAAC;;;CAyDnC,CAAC"}