Skip to content

Commit acb3283

Browse files
committed
Add minimal WebCrypto
1 parent 60430e2 commit acb3283

File tree

4 files changed

+244
-1
lines changed

4 files changed

+244
-1
lines changed

src/DOM.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ open Storage
2828
open WebLocks
2929
open CSSFontLoading
3030
open IndexedDB
31+
open WebCrypto
3132

3233
type shadowRootMode =
3334
| @as("closed") Closed

src/WebCrypto.js

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/WebCrypto.res

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
@@warning("-30")
2+
3+
open Prelude
4+
5+
type keyType =
6+
| @as("private") Private
7+
| @as("public") Public
8+
| @as("secret") Secret
9+
10+
type keyUsage =
11+
| @as("decrypt") Decrypt
12+
| @as("deriveBits") DeriveBits
13+
| @as("deriveKey") DeriveKey
14+
| @as("encrypt") Encrypt
15+
| @as("sign") Sign
16+
| @as("unwrapKey") UnwrapKey
17+
| @as("verify") Verify
18+
| @as("wrapKey") WrapKey
19+
20+
type keyFormat =
21+
| @as("jwk") Jwk
22+
| @as("pkcs8") Pkcs8
23+
| @as("raw") Raw
24+
| @as("spki") Spki
25+
26+
type keyAlgorithm = {mutable name: string}
27+
28+
/**
29+
This Web Crypto API interface provides a number of low-level cryptographic functions. It is accessed via the Crypto.subtle properties available in a window context (via Window.crypto).
30+
[See SubtleCrypto on MDN](https://developer.mozilla.org/docs/Web/API/SubtleCrypto)
31+
*/
32+
type subtleCrypto = {}
33+
34+
/**
35+
Basic cryptography features available in the current context. It allows access to a cryptographically strong random number generator and to cryptographic primitives.
36+
[See Crypto on MDN](https://developer.mozilla.org/docs/Web/API/Crypto)
37+
*/
38+
type crypto = {
39+
/**
40+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Crypto/subtle)
41+
*/
42+
subtle: subtleCrypto,
43+
}
44+
45+
/**
46+
The CryptoKey dictionary of the Web Crypto API represents a cryptographic key.
47+
[See CryptoKey on MDN](https://developer.mozilla.org/docs/Web/API/CryptoKey)
48+
*/
49+
type cryptoKey = {
50+
/**
51+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/CryptoKey/type)
52+
*/
53+
@as("type")
54+
type_: keyType,
55+
/**
56+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/CryptoKey/extractable)
57+
*/
58+
extractable: bool,
59+
/**
60+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/CryptoKey/algorithm)
61+
*/
62+
algorithm: keyAlgorithm,
63+
/**
64+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/CryptoKey/usages)
65+
*/
66+
usages: array<keyUsage>,
67+
}
68+
69+
type algorithmIdentifier = any
70+
71+
module SubtleCrypto = {
72+
/**
73+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/encrypt)
74+
*/
75+
@send
76+
external encrypt: (
77+
subtleCrypto,
78+
algorithmIdentifier,
79+
cryptoKey,
80+
bufferSource,
81+
) => Promise.t<ArrayBuffer.t> = "encrypt"
82+
83+
/**
84+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/decrypt)
85+
*/
86+
@send
87+
external decrypt: (
88+
subtleCrypto,
89+
algorithmIdentifier,
90+
cryptoKey,
91+
bufferSource,
92+
) => Promise.t<ArrayBuffer.t> = "decrypt"
93+
94+
/**
95+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/sign)
96+
*/
97+
@send
98+
external sign: (subtleCrypto, algorithmIdentifier, cryptoKey, bufferSource) => Promise.t<any> =
99+
"sign"
100+
101+
/**
102+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/verify)
103+
*/
104+
@send
105+
external verify: (
106+
subtleCrypto,
107+
algorithmIdentifier,
108+
cryptoKey,
109+
bufferSource,
110+
bufferSource,
111+
) => Promise.t<any> = "verify"
112+
113+
/**
114+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/digest)
115+
*/
116+
@send
117+
external digest: (subtleCrypto, algorithmIdentifier, bufferSource) => Promise.t<any> = "digest"
118+
119+
/**
120+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/generateKey)
121+
*/
122+
@send
123+
external generateKey: (
124+
subtleCrypto,
125+
algorithmIdentifier,
126+
bool,
127+
array<keyUsage>,
128+
) => Promise.t<any> = "generateKey"
129+
130+
/**
131+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/deriveKey)
132+
*/
133+
@send
134+
external deriveKey: (
135+
subtleCrypto,
136+
algorithmIdentifier,
137+
cryptoKey,
138+
algorithmIdentifier,
139+
bool,
140+
array<keyUsage>,
141+
) => Promise.t<any> = "deriveKey"
142+
143+
/**
144+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/deriveBits)
145+
*/
146+
@send
147+
external deriveBits: (
148+
subtleCrypto,
149+
algorithmIdentifier,
150+
cryptoKey,
151+
int,
152+
) => Promise.t<ArrayBuffer.t> = "deriveBits"
153+
154+
/**
155+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/importKey)
156+
*/
157+
@send
158+
external importKey: (
159+
subtleCrypto,
160+
any,
161+
bufferSource,
162+
algorithmIdentifier,
163+
bool,
164+
array<keyUsage>,
165+
) => Promise.t<cryptoKey> = "importKey"
166+
167+
/**
168+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/exportKey)
169+
*/
170+
@send
171+
external exportKey: (subtleCrypto, keyFormat, cryptoKey) => Promise.t<any> = "exportKey"
172+
173+
/**
174+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/wrapKey)
175+
*/
176+
@send
177+
external wrapKey: (
178+
subtleCrypto,
179+
keyFormat,
180+
cryptoKey,
181+
cryptoKey,
182+
algorithmIdentifier,
183+
) => Promise.t<any> = "wrapKey"
184+
185+
/**
186+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/unwrapKey)
187+
*/
188+
@send
189+
external unwrapKey: (
190+
subtleCrypto,
191+
keyFormat,
192+
bufferSource,
193+
cryptoKey,
194+
algorithmIdentifier,
195+
algorithmIdentifier,
196+
bool,
197+
array<keyUsage>,
198+
) => Promise.t<cryptoKey> = "unwrapKey"
199+
}
200+
201+
module Crypto = {
202+
/**
203+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Crypto/getRandomValues)
204+
*/
205+
@send
206+
external getRandomValues: (crypto, 't) => 't = "getRandomValues"
207+
208+
/**
209+
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Crypto/randomUUID)
210+
*/
211+
@send
212+
external randomUUID: crypto => string = "randomUUID"
213+
}

tools/TypeScript-DOM-lib-generator/src/build/emitter.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ export async function emitRescriptBindings(
474474
case "DOMString":
475475
case "ConstrainDOMString":
476476
case "USVString":
477+
case "`${string}-${string}-${string}-${string}-${string}`":
477478
return "string";
478479

479480
case "unrestricted double":
@@ -634,7 +635,11 @@ export async function emitRescriptBindings(
634635

635636
const genericType = parseGenericType(t);
636637
if (genericType) {
637-
console.log("GENERIC TYPE", genericType);
638+
// TODO: exclude is one of those weird TypeScript things were they subtract properties from types.
639+
if (genericType.typeName === "Exclude") {
640+
return "any";
641+
}
642+
638643
const ps = genericType.genericParameters
639644
.map((p) => {
640645
if (p.length === 1) {
@@ -1800,6 +1805,17 @@ export async function emitRescriptBindings(
18001805
],
18011806
opens: ["Prelude", "Event"],
18021807
},
1808+
// https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API
1809+
{
1810+
name: "WebCrypto",
1811+
entries: [
1812+
enums(["KeyType", "KeyUsage", "KeyFormat"]),
1813+
dictionaries(["KeyAlgorithm"]),
1814+
individualInterfaces(["SubtleCrypto", "Crypto", "CryptoKey"]),
1815+
byHand("AlgorithmIdentifier", emitAny("AlgorithmIdentifier")),
1816+
],
1817+
opens: ["Prelude"],
1818+
},
18031819
// https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model
18041820
// https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API
18051821
{
@@ -2030,6 +2046,7 @@ export async function emitRescriptBindings(
20302046
"WebLocks",
20312047
"CSSFontLoading",
20322048
"IndexedDB",
2049+
"WebCrypto",
20332050
],
20342051
},
20352052
// https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API

0 commit comments

Comments
 (0)