Skip to content

Commit dadd53c

Browse files
committed
Make decrypt function generic
Previous incorrect types surfaced problem with defined DecryptFn declaration
1 parent 352bab8 commit dadd53c

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

packages/auth/src/connection/Connection.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable object-shorthand */
22
import { EventEmitter } from '@herbcaudill/eventemitter42'
3-
import type { DecryptFnParams } from '@localfirst/crdx'
3+
import type { DecryptFn, DecryptFnParams, Graph } from '@localfirst/crdx'
44
import {
55
generateMessage,
66
headsAreEqual,
@@ -33,7 +33,13 @@ import { redactDevice } from '../device/index.js'
3333
import * as invitations from '../invitation/index.js'
3434
import { pack, unpack } from 'msgpackr'
3535
import { getTeamState } from '../team/getTeamState.js'
36-
import { Team, decryptTeamGraph, type TeamAction, type TeamContext } from '../team/index.js'
36+
import {
37+
Team,
38+
TeamGraph,
39+
decryptTeamGraph,
40+
type TeamAction,
41+
type TeamContext,
42+
} from '../team/index.js'
3743
import * as select from '../team/selectors/index.js'
3844
import { arraysAreEqual } from '../util/arraysAreEqual.js'
3945
import { KeyType } from '../util/index.js'
@@ -308,7 +314,7 @@ export class Connection extends EventEmitter<ConnectionEvents> {
308314
const deviceKeys = device.keys
309315

310316
// handle errors here
311-
const decrypt = ({ encryptedGraph, keys }: DecryptFnParams<TeamAction, TeamContext>) =>
317+
const decrypt: DecryptFn<TeamAction, TeamContext> = ({ encryptedGraph, keys }) =>
312318
decryptTeamGraph({ encryptedGraph, teamKeys: keys, deviceKeys })
313319

314320
const [newChain, syncState] = receiveMessage(

packages/crdx/src/graph/decrypt.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,10 @@ export const decryptLink = <A extends Action, C>(
4343
/**
4444
* Decrypts a graph using a one or more keys.
4545
*/
46-
export const decryptGraph: DecryptFn = <A extends Action, C>({
46+
export const decryptGraph = (<A extends Action, C>({
4747
encryptedGraph,
4848
keys,
49-
}: {
50-
encryptedGraph: MaybePartlyDecryptedGraph<A, C>
51-
keys: KeysetWithSecrets | KeysetWithSecrets[] | Keyring
52-
}): Graph<A, C> => {
49+
}: DecryptFnParams<A, C>): Graph<A, C> => {
5350
const { encryptedLinks, root, childMap = {} } = encryptedGraph
5451

5552
const links = encryptedGraph.links ?? {}
@@ -83,14 +80,14 @@ export const decryptGraph: DecryptFn = <A extends Action, C>({
8380
...encryptedGraph,
8481
links: decryptedLinks,
8582
}
86-
}
83+
}) satisfies DecryptFn<any, any>
8784

8885
export type DecryptFnParams<A extends Action, C> = {
8986
encryptedGraph: MaybePartlyDecryptedGraph<A, C>
9087
keys: KeysetWithSecrets | KeysetWithSecrets[] | Keyring
9188
}
9289

93-
export type DecryptFn = <A extends Action, C>({
90+
export type DecryptFn<A extends Action, C> = ({
9491
encryptedGraph,
9592
keys,
9693
}: DecryptFnParams<A, C>) => Graph<A, C>

packages/crdx/src/sync/receiveMessage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const receiveMessage = <A extends Action, C>(
2525

2626
keys: KeysetWithSecrets | Keyring,
2727

28-
decrypt: DecryptFn = decryptGraph
28+
decrypt: DecryptFn<A, C> = decryptGraph
2929
): [Graph<A, C>, SyncState] => {
3030
// if a keyset was provided, wrap it in a keyring
3131
const keyring = createKeyring(keys)

0 commit comments

Comments
 (0)