Skip to content

Commit 6955d1a

Browse files
committed
fix(mobile): load expo lazily in the DPoP signer
Importing expo at module scope ran its async-require setup in Node tests, so suites that import the relay layer without mocking expo failed to load. Load it when a DPoP key operation runs, as attachmentDownload does, and share the Node-backed native fake with the suites that drive the real signer.
1 parent b2050af commit 6955d1a

5 files changed

Lines changed: 72 additions & 42 deletions

File tree

‎apps/mobile/src/features/agent-awareness/remoteRegistration.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ vi.mock("expo-crypto", () => ({
128128
},
129129
}));
130130

131+
vi.mock("expo", async () => {
132+
const { createNodeDpopKeysModule } = await import("../cloud/dpop.testing");
133+
const dpopKeys = createNodeDpopKeysModule();
134+
return { requireNativeModule: () => dpopKeys };
135+
});
136+
131137
vi.mock("expo-secure-store", () => ({
132138
getItemAsync: (key: string) => Promise.resolve(secureStore.get(key) ?? null),
133139
setItemAsync: (key: string, value: string) => {

‎apps/mobile/src/features/cloud/dpop.test.ts‎

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -41,42 +41,11 @@ vi.mock("expo-secure-store", () => ({
4141
},
4242
}));
4343

44-
function publicKeyFor(privateKey: Uint8Array) {
45-
const ecdh = NodeCrypto.createECDH("prime256v1");
46-
ecdh.setPrivateKey(privateKey);
47-
return ecdh.getPublicKey();
48-
}
49-
50-
// The native module's contract, backed by Node's P-256.
51-
const nativeDpopKeys = {
52-
generateDpopKey: () => {
53-
const ecdh = NodeCrypto.createECDH("prime256v1");
54-
const publicKey = ecdh.generateKeys();
55-
const privateKey = ecdh.getPrivateKey();
56-
return Promise.resolve(
57-
new Uint8Array(Buffer.concat([Buffer.alloc(32 - privateKey.length), privateKey, publicKey])),
58-
);
59-
},
60-
dpopKeyMatches: (privateKey: Uint8Array, publicKey: Uint8Array) =>
61-
Promise.resolve(publicKeyFor(privateKey).equals(publicKey)),
62-
signDpop: (privateKey: Uint8Array, signingInput: Uint8Array) => {
63-
const publicKey = publicKeyFor(privateKey);
64-
const key = NodeCrypto.createPrivateKey({
65-
format: "jwk",
66-
key: {
67-
kty: "EC",
68-
crv: "P-256",
69-
d: Buffer.from(privateKey).toString("base64url"),
70-
x: publicKey.subarray(1, 33).toString("base64url"),
71-
y: publicKey.subarray(33).toString("base64url"),
72-
},
73-
});
74-
return Promise.resolve(
75-
new Uint8Array(NodeCrypto.sign("sha256", signingInput, { key, dsaEncoding: "ieee-p1363" })),
76-
);
77-
},
78-
};
79-
vi.mock("expo", () => ({ requireNativeModule: () => nativeDpopKeys }));
44+
vi.mock("expo", async () => {
45+
const { createNodeDpopKeysModule } = await import("./dpop.testing");
46+
const dpopKeys = createNodeDpopKeysModule();
47+
return { requireNativeModule: () => dpopKeys };
48+
});
8049

8150
describe("mobile DPoP", () => {
8251
it.effect("passes typed-array digest input through the Expo Crypto adapter", () =>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// @effect-diagnostics nodeBuiltinImport:off
2+
/// <reference types="node" />
3+
4+
import * as NodeCrypto from "node:crypto";
5+
6+
function publicKeyFor(privateKey: Uint8Array) {
7+
const ecdh = NodeCrypto.createECDH("prime256v1");
8+
ecdh.setPrivateKey(privateKey);
9+
return ecdh.getPublicKey();
10+
}
11+
12+
/**
13+
* Stands in for the T3NativeControls DPoP functions with Node's P-256.
14+
* Return it from a mocked `expo` `requireNativeModule`.
15+
*/
16+
export function createNodeDpopKeysModule() {
17+
return {
18+
generateDpopKey: () => {
19+
const ecdh = NodeCrypto.createECDH("prime256v1");
20+
const publicKey = ecdh.generateKeys();
21+
const privateKey = ecdh.getPrivateKey();
22+
return Promise.resolve(
23+
new Uint8Array(
24+
Buffer.concat([Buffer.alloc(32 - privateKey.length), privateKey, publicKey]),
25+
),
26+
);
27+
},
28+
dpopKeyMatches: (privateKey: Uint8Array, publicKey: Uint8Array) =>
29+
Promise.resolve(publicKeyFor(privateKey).equals(publicKey)),
30+
signDpop: (privateKey: Uint8Array, signingInput: Uint8Array) => {
31+
const publicKey = publicKeyFor(privateKey);
32+
const key = NodeCrypto.createPrivateKey({
33+
format: "jwk",
34+
key: {
35+
kty: "EC",
36+
crv: "P-256",
37+
d: Buffer.from(privateKey).toString("base64url"),
38+
x: publicKey.subarray(1, 33).toString("base64url"),
39+
y: publicKey.subarray(33).toString("base64url"),
40+
},
41+
});
42+
// ieee-p1363 is the raw r‖s pair the native functions return.
43+
return Promise.resolve(
44+
new Uint8Array(NodeCrypto.sign("sha256", signingInput, { key, dsaEncoding: "ieee-p1363" })),
45+
);
46+
},
47+
};
48+
}

‎apps/mobile/src/features/cloud/dpop.ts‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import * as Base64Url from "effect/encoding/Base64Url";
55
import * as Hex from "effect/encoding/Hex";
66
import * as Result from "effect/Result";
77
import * as Schema from "effect/Schema";
8-
import { requireNativeModule } from "expo";
98
import * as ExpoCrypto from "expo-crypto";
109
import * as SecureStore from "expo-secure-store";
1110
import { DpopProof } from "@t3tools/client-runtime/relay";
@@ -84,7 +83,9 @@ interface NativeDpopKeys {
8483
}
8584

8685
// Platform crypto: in Hermes, @noble/curves blocks JS for about a second on its first P-256 key.
87-
const nativeDpopKeys = () => requireNativeModule<NativeDpopKeys>("T3NativeControls");
86+
// Imported lazily: loading `expo` at module scope breaks Node tests that import this file.
87+
const nativeDpopKeys = async () =>
88+
(await import("expo")).requireNativeModule<NativeDpopKeys>("T3NativeControls");
8889

8990
function base64UrlToBytes(value: string): Uint8Array {
9091
return Result.getOrThrow(Base64Url.decode(value));
@@ -128,7 +129,7 @@ function privateJwkFromPrivateKey(
128129
export const generateDpopProofKeyPair = Effect.fn("dpop.generateDpopProofKeyPair")(
129130
function* (): Effect.fn.Return<DpopProofKeyPair, CloudDpopError> {
130131
const key = yield* Effect.tryPromise({
131-
try: () => nativeDpopKeys().generateDpopKey(),
132+
try: async () => (await nativeDpopKeys()).generateDpopKey(),
132133
catch: cloudDpopError("Could not generate the DPoP key pair."),
133134
});
134135
const publicJwk = yield* Effect.try({
@@ -151,8 +152,8 @@ export const loadOrCreateDpopProofKeyPair = Effect.fn("dpop.loadOrCreateDpopProo
151152
);
152153
const { d, ...publicJwk } = storedPrivateJwk;
153154
const matches = yield* Effect.tryPromise({
154-
try: () =>
155-
nativeDpopKeys().dpopKeyMatches(
155+
try: async () =>
156+
(await nativeDpopKeys()).dpopKeyMatches(
156157
base64UrlToBytes(d),
157158
uncompressedPublicKeyFromJwk(publicJwk),
158159
),
@@ -186,7 +187,7 @@ export const loadOrCreateProofKey = Effect.fn("dpop.loadOrCreateProofKey")(funct
186187
publicJwk: keyPair.publicJwk,
187188
sign: (signingInput) =>
188189
Effect.tryPromise({
189-
try: () => nativeDpopKeys().signDpop(privateKey, signingInput),
190+
try: async () => (await nativeDpopKeys()).signDpop(privateKey, signingInput),
190191
catch: (cause) =>
191192
new DpopProof.DpopProofError({ message: "Could not sign DPoP proof.", cause }),
192193
}),

‎apps/mobile/src/features/cloud/managedRelayLayer.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ vi.mock("expo-crypto", () => ({
2323

2424
const secureStore = new Map<string, string>();
2525
let failNextRead = false;
26+
vi.mock("expo", async () => {
27+
const { createNodeDpopKeysModule } = await import("./dpop.testing");
28+
const dpopKeys = createNodeDpopKeysModule();
29+
return { requireNativeModule: () => dpopKeys };
30+
});
31+
2632
vi.mock("expo-secure-store", () => ({
2733
getItemAsync: (key: string) => {
2834
if (failNextRead) {

0 commit comments

Comments
 (0)