Skip to content

Commit

Permalink
crypto
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmtimbo committed Jul 25, 2024
1 parent 823253c commit c524f3f
Show file tree
Hide file tree
Showing 85 changed files with 2,739 additions and 390 deletions.
47 changes: 46 additions & 1 deletion src/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,50 @@ export type API = {
EventSource: typeof EventSource
}
channel: { local: (opt: ChannelOpt) => Channel }
crypto: {
generateKey: (
algorithm: AlgorithmIdentifier,
extractable: boolean,
keyUsages: string[]
) => Promise<CryptoKey | CryptoKeyPair>
exportKey: <T extends KeyFormat>(
format: T,
key: CryptoKey
) => Promise<ArrayBuffer | JsonWebKey>
encrypt: (
algorithm: AlgorithmIdentifier,
key: CryptoKey,
data: BufferSource
) => Promise<ArrayBuffer>
decrypt: (
algorithm: AlgorithmIdentifier,
key: CryptoKey,
data: BufferSource
) => Promise<ArrayBuffer>
sign: (
algorithm: AlgorithmIdentifier,
key: CryptoKey,
data: ArrayBuffer
) => Promise<ArrayBuffer>
verify: (
algorithm: AlgorithmIdentifier,
key: CryptoKey,
signature: BufferSource,
data: BufferSource
) => Promise<boolean>
importKey<T extends KeyFormat>(
format: T,
keyData: T extends 'jwk' ? JsonWebKey : BufferSource,
algorithm:
| AlgorithmIdentifier
| RsaHashedImportParams
| EcKeyImportParams
| HmacImportParams
| AesKeyAlgorithm,
extractable: boolean,
keyUsages: ReadonlyArray<KeyUsage>
): Promise<CryptoKey>
}
alert: {
alert: (message: string) => void
prompt: (message: string, defaultValue: string) => string
Expand Down Expand Up @@ -212,7 +256,6 @@ export type API = {
createRange(): Range
exitPictureInPicture(): Promise<void>
pictureInPictureElement: Element

MutationObserver: { new (callback: MutationCallback): MutationObserver }
PositionObserver: PositionObserverCostructor
ResizeObserver: { new (callback: ResizeObserverCallback): ResizeObserver }
Expand All @@ -228,6 +271,8 @@ export type API = {
parse: (str: string) => Dict<any>
}
text: {
TextEncoder: typeof TextEncoder
TextDecoder: typeof TextDecoder
measureText: MeasureTextFunction
}
worker: {
Expand Down
8 changes: 7 additions & 1 deletion src/Class/Graph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
} from '../../spec/actions/G'
import { cloneUnit, cloneUnitClass } from '../../spec/cloneUnit'
import { evaluate } from '../../spec/evaluate'
import { evaluateDataValue } from '../../spec/evaluateDataValue'
import { bundleFromId } from '../../spec/fromId'
import { applyUnitDefaultIgnored } from '../../spec/fromSpec'
import { renameUnitInMerges } from '../../spec/reducers/spec'
Expand Down Expand Up @@ -5315,7 +5316,12 @@ export class Graph<I extends Dict<any> = any, O extends Dict<any> = any>
}

setUnitPinData(
{ unitId, type, pinId, data: stringify(data) },
{
unitId,
type,
pinId,
data: evaluateDataValue(stringify(data), specs, classes),
},
this._spec,
specs,
classes
Expand Down
6 changes: 6 additions & 0 deletions src/client/icons/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export default JSON.parse(`{
"arrow-up-left": "M17 17 L7 7 M7 17 L7 7 L17 7",
"arrow-up-right": "M7 17 L17 7 M7 7 L17 7 L17 17",
"arrow-up": "M12 19 L12 5 M5 12 L12 5 L19 12",
"arrow-down-up": "m 3 16 4 4 4-4 M 7 20 V 4 m 14 4 -4 -4 -4 4 M 17 4 v16",
"arrow-up-down": "m 11 16 -4 4 -4 -4 M 17 20 V 4 m -4 4 4-4 4 4 M 7 4 v 16",
"asterisk": "M12 6v12 M17.196 9L6.804 15 M6.804 9l10.392 6",
"at-sign": "M16 12 A4 4 0 0 1 12 16 A4 4 0 0 1 8 12 A4 4 0 0 1 16 12 z M16 8v5a3 3 0 006 0v-1a10 10 0 10-3.92 7.94",
"atom-alt": "M 4 12 A 9 6 45 0 1 20 12 A 9 6 45 0 1 4 12 M 4 12 A 9 6 -45 0 1 20 12 A 9 6 -45 0 1 4 12 M13 12 A1 1 0 0 1 12 13 A1 1 0 0 1 11 12 A1 1 0 0 1 13 12 z M12 12 L12.001 12",
Expand Down Expand Up @@ -209,6 +211,7 @@ export default JSON.parse(`{
"file-x-2": "M4 22h14a2 2 0 002-2V7.5L14.5 2H6a2 2 0 00-2 2v4 M14 2v6h6 M3 12.5l5 5 M8 12.5l-5 5",
"file-x": "M14.5 2H6a2 2 0 00-2 2v16a2 2 0 002 2h12a2 2 0 002-2V7.5L14.5 2z M14 2 L14 8 L20 8 M9.5 12.5 L14.5 17.5 M14.5 12.5 L9.5 17.5",
"file": "M14.5 2H6a2 2 0 00-2 2v16a2 2 0 002 2h12a2 2 0 002-2V7.5L14.5 2z M14 2 L14 8 L20 8",
"file-lock": "M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z M 8 12 h 8 v 6 h -8 z M10 12v-2a2 2 0 1 1 4 0v2",
"files": "M15.5 2H8.6c-.4 0-.8.2-1.1.5-.3.3-.5.7-.5 1.1v12.8c0 .4.2.8.5 1.1.3.3.7.5 1.1.5h9.8c.4 0 .8-.2 1.1-.5.3-.3.5-.7.5-1.1V6.5L15.5 2z M3 7.6v12.8c0 .4.2.8.5 1.1.3.3.7.5 1.1.5h9.8 M15 2v5h5",
"film": "M4.18 2 H19.82 A2.18 2.18 0 0 1 22 4.18 V19.82 A2.18 2.18 0 0 1 19.82 22 H4.18 A2.18 2.18 0 0 1 2 19.82 V4.18 A2.18 2.18 0 0 1 4.18 2 z M7 2 L7 22 M17 2 L17 22 M2 12 L22 12 M2 7 L7 7 M2 17 L7 17 M17 17 L22 17 M17 7 L22 7",
"filter": "M22 3 L2 3 L10 12.46 L10 19 L14 21 L14 12.46 L22 3 Z",
Expand Down Expand Up @@ -278,6 +281,8 @@ export default JSON.parse(`{
"italic": "M19 4 L10 4 M14 20 L5 20 M15 4 L9 20",
"jersey-pound": "M12 9.5V21m0-11.5L6 3m6 6.5L18 3 M6 15h12 M6 11h12",
"key": "M21 2l-2 2m-7.61 7.61a5.5 5.5 0 11-7.778 7.778 5.5 5.5 0 017.777-7.777zm0 0L15.5 7.5m0 0l3 3L22 7l-3-3m-3.5 3.5L19 4",
"key-round": "M2.586 17.414A2 2 0 0 0 2 18.828V21a1 1 0 0 0 1 1h3a1 1 0 0 0 1-1v-1a1 1 0 0 1 1-1h1a1 1 0 0 0 1-1v-1a1 1 0 0 1 1-1h.172a2 2 0 0 0 1.414-.586l.814-.814a6.5 6.5 0 1 0-4-4z M 16 7.5 A 0.5 0.5 1 1 0 16 7.49zz",
"key-square": "M12.4 2.7c.9-.9 2.5-.9 3.4 0l5.5 5.5c.9.9.9 2.5 0 3.4l-3.7 3.7c-.9.9-2.5.9-3.4 0L8.7 9.8c-.9-.9-.9-2.5 0-3.4Z M 0 0 m14 7 3 3 M9.4 10.6 2 18v3c0 .6.4 1 1 1h4v-3h3v-3h2l1.4-1.4 ",
"keyboard": "M4 4 H20 A2 2 0 0 1 22 6 V18 A2 2 0 0 1 20 20 H4 A2 2 0 0 1 2 18 V6 A2 2 0 0 1 4 4 z M6 8 L6.01 8 M10 8 L10.01 8 M14 8 L14.01 8 M18 8 L18.01 8 M8 12 L8.01 12 M12 12 L12.01 12 M16 12 L16.01 12 M10 16 L14 16 M6 16 L6.01 16 M18 16 L18.01 16",
"languages": "M5 8l6 6 M4 14l6-6 2-3 M2 5h12 M7 2h1 M22 22l-5-10-5 10 M14 18h6",
"laptop-2": "M5 4 H19 A2 2 0 0 1 21 6 V14 A2 2 0 0 1 19 16 H5 A2 2 0 0 1 3 14 V6 A2 2 0 0 1 5 4 z M2 20 L22 20",
Expand Down Expand Up @@ -463,6 +468,7 @@ export default JSON.parse(`{
"square": "M5 3 H19 A2 2 0 0 1 21 5 V19 A2 2 0 0 1 19 21 H5 A2 2 0 0 1 3 19 V5 A2 2 0 0 1 5 3 z",
"square-play": "M4 4 h 18 v 18 h -18 z M 1 1 m9 8 6 4-6 4Z",
"square-activity": "M4 4 h 18 v 18 h -18 Z M 18 13 h -2 l -2 5 -2 -10 -2 5 H 7",
"square-function": "M 3 3 h 18 v 18 h -18 z M9 17c2 0 2.8-1 2.8-2.8V10c0-2 1-3.3 3.2-3 M9 11.2h5.7",
"star-half": "M12 17.8L5.8 21 7 14.1 2 9.3l7-1L12 2",
"star": "M12 2 L15.09 8.26 L22 9.27 L17 14.14 L18.18 21.02 L12 17.77 L5.82 21.02 L7 14.14 L2 9.27 L8.91 8.26 L12 2 Z",
"stop-circle": "M22 12 A10 10 0 0 1 12 22 A10 10 0 0 1 2 12 A10 10 0 0 1 22 12 z M9 9 H15 V15 H9 V9 z",
Expand Down
36 changes: 36 additions & 0 deletions src/client/method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import {
A_METHOD_REF,
A_METHOD_WATCH,
} from '../types/interface/async/$A'
import {
AB_METHOD_CALL,
AB_METHOD_REF,
AB_METHOD_WATCH,
} from '../types/interface/async/$AB'
import {
AC_METHOD_CALL,
AC_METHOD_REF,
Expand Down Expand Up @@ -50,6 +55,11 @@ import {
CH_METHOD_REF,
CH_METHOD_WATCH,
} from '../types/interface/async/$CH'
import {
CK_METHOD_CALL,
CK_METHOD_REF,
CK_METHOD_WATCH,
} from '../types/interface/async/$CK'
import {
CS_METHOD_CALL,
CS_METHOD_REF,
Expand Down Expand Up @@ -110,6 +120,12 @@ import {
S_METHOD_REF,
S_METHOD_WATCH,
} from '../types/interface/async/$S'
import { TD_METHOD_CALL, TD_METHOD_WATCH } from '../types/interface/async/$TD'
import {
TE_METHOD_CALL,
TE_METHOD_REF,
TE_METHOD_WATCH,
} from '../types/interface/async/$TE'
import {
U_METHOD_CALL,
U_METHOD_REF,
Expand Down Expand Up @@ -245,4 +261,24 @@ export const METHOD: AllKeys<
watch: D_METHOD_WATCH,
ref: D_METHOD_REF,
},
CK: {
call: CK_METHOD_CALL,
watch: CK_METHOD_WATCH,
ref: CK_METHOD_REF,
},
AB: {
call: AB_METHOD_CALL,
watch: AB_METHOD_REF,
ref: AB_METHOD_WATCH,
},
TE: {
call: TE_METHOD_CALL,
watch: TE_METHOD_WATCH,
ref: TE_METHOD_REF,
},
TD: {
call: TD_METHOD_CALL,
watch: TD_METHOD_WATCH,
ref: TE_METHOD_REF,
},
}
102 changes: 102 additions & 0 deletions src/client/platform/web/api/crypto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { API } from '../../../../API'
import { APINotSupportedError } from '../../../../exception/APINotImplementedError'
import { BootOpt } from '../../../../system'

export function webCrypto(window: Window, opt: BootOpt): API['crypto'] {
const crypto: API['crypto'] = {
generateKey: function (
algorithm: AlgorithmIdentifier,
extractable: boolean,
keyUsages: KeyUsage[]
): Promise<CryptoKey | CryptoKeyPair> {
if (window.crypto) {
return window.crypto.subtle.generateKey(
algorithm,
extractable,
keyUsages
)
}

throw new APINotSupportedError('Crypto')
},
exportKey: function (
format: KeyFormat,
key: CryptoKey
): Promise<ArrayBuffer | JsonWebKey> {
if (window.crypto) {
return window.crypto.subtle.exportKey(format, key)
}

throw new APINotSupportedError('Crypto')
},
importKey: function <
F extends KeyFormat,
K extends F extends 'jwk' ? JsonWebKey : BufferSource,
>(
format: F,
key: K,
algorithm: AlgorithmIdentifier,
extractable: boolean,
keyUsages: KeyUsage[]
): Promise<CryptoKey> {
if (window.crypto) {
return window.crypto.subtle.importKey(
format as any,
key as any,
algorithm,
extractable,
keyUsages
)
}

throw new APINotSupportedError('Crypto')
},
encrypt: function (
algorithm: AlgorithmIdentifier,
key: CryptoKey,
data: BufferSource
): Promise<ArrayBuffer> {
if (window.crypto) {
return window.crypto.subtle.encrypt(algorithm, key, data)
}

throw new APINotSupportedError('Crypto')
},
decrypt: function (
algorithm: AlgorithmIdentifier,
key: CryptoKey,
data: BufferSource
): Promise<ArrayBuffer> {
if (window.crypto) {
return window.crypto.subtle.decrypt(algorithm, key, data)
}

throw new APINotSupportedError('Crypto')
},
sign: function (
algorithm: AlgorithmIdentifier,
key: CryptoKey,
data: ArrayBuffer
): Promise<ArrayBuffer> {
if (window.crypto) {
return window.crypto.subtle.sign(algorithm, key, data)
}

throw new APINotSupportedError('Crypto')
},
verify: function (
algorithm: AlgorithmIdentifier,
key: CryptoKey,
signature: BufferSource,
data: BufferSource
): Promise<boolean> {
if (window.crypto) {
return window.crypto.subtle.verify(algorithm, key, signature, data)
}

throw new APINotSupportedError('Crypto')
},
}

return crypto
}
4 changes: 4 additions & 0 deletions src/client/platform/web/api/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export function webText(window: Window, opt: BootOpt): API['text'] {

return measureText(ctx, text, fontSize, maxWidth)
},
// @ts-ignore
TextEncoder: window.TextEncoder,
// @ts-ignore
TextDecoder: window.TextDecoder,
}

return text
Expand Down
3 changes: 3 additions & 0 deletions src/client/platform/web/boot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { webAnimation } from './api/animation'
import { webBluetooth } from './api/bluetooth'
import { webChannel } from './api/channel'
import { webClipboard } from './api/clipboard'
import { webCrypto } from './api/crypto'
import { webDB } from './api/db'
import { webDevice } from './api/device'
import { webDocument } from './api/document'
Expand Down Expand Up @@ -93,6 +94,7 @@ export function webBoot(
const navigator = webNavigator(window, opt)
const layout = webLayout(window, opt)
const theme = webTheme(window, _root, opt)
const crypto = webCrypto(window, opt)

const api: API = {
alert,
Expand All @@ -104,6 +106,7 @@ export function webBoot(
screen,
bluetooth,
clipboard,
crypto,
location,
history,
geolocation,
Expand Down
60 changes: 60 additions & 0 deletions src/host/none.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ export function noHost(): API {
measureText: (text: string) => {
throw new APINotSupportedError('Measure Text')
},
TextEncoder: undefined,
TextDecoder: undefined,
},
worker: {
start: () => {
Expand Down Expand Up @@ -313,6 +315,64 @@ export function noHost(): API {
throw new MethodNotImplementedError()
},
},
crypto: {
generateKey: function (
algorithm: AlgorithmIdentifier,
extractable: boolean,
keyUsages: string[]
): Promise<CryptoKey | CryptoKeyPair> {
throw new MethodNotImplementedError()
},
exportKey: function <T extends KeyFormat>(
format: T,
key: CryptoKey
): Promise<ArrayBuffer | JsonWebKey> {
throw new MethodNotImplementedError()
},
importKey: function <T extends KeyFormat>(
format: T,
keyData: T extends 'jwk' ? JsonWebKey : BufferSource,
algorithm:
| AlgorithmIdentifier
| RsaHashedImportParams
| EcKeyImportParams
| HmacImportParams
| AesKeyAlgorithm,
extractable: boolean,
keyUsages: ReadonlyArray<KeyUsage>
): Promise<CryptoKey> {
throw new MethodNotImplementedError()
},
encrypt: function (
algorithm: AlgorithmIdentifier,
key: CryptoKey,
data: ArrayBuffer
): Promise<ArrayBuffer> {
throw new MethodNotImplementedError()
},
decrypt: function (
algorithm: AlgorithmIdentifier,
key: CryptoKey,
data: ArrayBuffer
): Promise<ArrayBuffer> {
throw new MethodNotImplementedError()
},
sign: function (
algorithm: AlgorithmIdentifier,
key: CryptoKey,
data: ArrayBuffer
): Promise<ArrayBuffer> {
throw new MethodNotImplementedError()
},
verify: function (
algorithm: AlgorithmIdentifier,
key: CryptoKey,
signature: BufferSource,
data: BufferSource
): Promise<boolean> {
throw new MethodNotImplementedError()
},
},
}

return api
Expand Down
Loading

0 comments on commit c524f3f

Please sign in to comment.