@spare-technologies/spare-link 1.0.1 → 1.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.
- package/README.md +34 -5
- package/lib/commonjs/core/services/hosted-flow-controller.js +5 -0
- package/lib/commonjs/core/services/hosted-flow-controller.js.map +1 -1
- package/lib/commonjs/internal/hosted/bridge-message.js +13 -0
- package/lib/commonjs/internal/hosted/bridge-message.js.map +1 -1
- package/lib/commonjs/internal/telemetry/native-telemetry-reply.js +59 -0
- package/lib/commonjs/internal/telemetry/native-telemetry-reply.js.map +1 -0
- package/lib/commonjs/internal/telemetry/risk-telemetry-collector.js +99 -0
- package/lib/commonjs/internal/telemetry/risk-telemetry-collector.js.map +1 -0
- package/lib/commonjs/internal/telemetry/risk-telemetry-probes.js +134 -0
- package/lib/commonjs/internal/telemetry/risk-telemetry-probes.js.map +1 -0
- package/lib/commonjs/ui/hosted/hosted-payment-page.js +8 -0
- package/lib/commonjs/ui/hosted/hosted-payment-page.js.map +1 -1
- package/lib/commonjs/ui/hosted/hosted-payment-webview.js +31 -2
- package/lib/commonjs/ui/hosted/hosted-payment-webview.js.map +1 -1
- package/lib/commonjs/version.js +1 -1
- package/lib/module/core/services/hosted-flow-controller.js +5 -0
- package/lib/module/core/services/hosted-flow-controller.js.map +1 -1
- package/lib/module/internal/hosted/bridge-message.js +13 -0
- package/lib/module/internal/hosted/bridge-message.js.map +1 -1
- package/lib/module/internal/telemetry/native-telemetry-reply.js +53 -0
- package/lib/module/internal/telemetry/native-telemetry-reply.js.map +1 -0
- package/lib/module/internal/telemetry/risk-telemetry-collector.js +94 -0
- package/lib/module/internal/telemetry/risk-telemetry-collector.js.map +1 -0
- package/lib/module/internal/telemetry/risk-telemetry-probes.js +129 -0
- package/lib/module/internal/telemetry/risk-telemetry-probes.js.map +1 -0
- package/lib/module/ui/hosted/hosted-payment-page.js +8 -0
- package/lib/module/ui/hosted/hosted-payment-page.js.map +1 -1
- package/lib/module/ui/hosted/hosted-payment-webview.js +32 -3
- package/lib/module/ui/hosted/hosted-payment-webview.js.map +1 -1
- package/lib/module/version.js +1 -1
- package/lib/typescript/commonjs/core/services/hosted-flow-controller.d.ts.map +1 -1
- package/lib/typescript/commonjs/internal/hosted/bridge-message.d.ts +12 -1
- package/lib/typescript/commonjs/internal/hosted/bridge-message.d.ts.map +1 -1
- package/lib/typescript/commonjs/internal/telemetry/native-telemetry-reply.d.ts +33 -0
- package/lib/typescript/commonjs/internal/telemetry/native-telemetry-reply.d.ts.map +1 -0
- package/lib/typescript/commonjs/internal/telemetry/risk-telemetry-collector.d.ts +47 -0
- package/lib/typescript/commonjs/internal/telemetry/risk-telemetry-collector.d.ts.map +1 -0
- package/lib/typescript/commonjs/internal/telemetry/risk-telemetry-probes.d.ts +19 -0
- package/lib/typescript/commonjs/internal/telemetry/risk-telemetry-probes.d.ts.map +1 -0
- package/lib/typescript/commonjs/ui/hosted/hosted-payment-page.d.ts.map +1 -1
- package/lib/typescript/commonjs/ui/hosted/hosted-payment-webview.d.ts +9 -0
- package/lib/typescript/commonjs/ui/hosted/hosted-payment-webview.d.ts.map +1 -1
- package/lib/typescript/commonjs/version.d.ts +1 -1
- package/lib/typescript/module/core/services/hosted-flow-controller.d.ts.map +1 -1
- package/lib/typescript/module/internal/hosted/bridge-message.d.ts +12 -1
- package/lib/typescript/module/internal/hosted/bridge-message.d.ts.map +1 -1
- package/lib/typescript/module/internal/telemetry/native-telemetry-reply.d.ts +33 -0
- package/lib/typescript/module/internal/telemetry/native-telemetry-reply.d.ts.map +1 -0
- package/lib/typescript/module/internal/telemetry/risk-telemetry-collector.d.ts +47 -0
- package/lib/typescript/module/internal/telemetry/risk-telemetry-collector.d.ts.map +1 -0
- package/lib/typescript/module/internal/telemetry/risk-telemetry-probes.d.ts +19 -0
- package/lib/typescript/module/internal/telemetry/risk-telemetry-probes.d.ts.map +1 -0
- package/lib/typescript/module/ui/hosted/hosted-payment-page.d.ts.map +1 -1
- package/lib/typescript/module/ui/hosted/hosted-payment-webview.d.ts +9 -0
- package/lib/typescript/module/ui/hosted/hosted-payment-webview.d.ts.map +1 -1
- package/lib/typescript/module/version.d.ts +1 -1
- package/package.json +24 -4
- package/plugins/with-spare-link.js +78 -5
- package/src/core/services/hosted-flow-controller.ts +5 -0
- package/src/internal/hosted/bridge-message.ts +20 -1
- package/src/internal/telemetry/native-telemetry-reply.ts +59 -0
- package/src/internal/telemetry/risk-telemetry-collector.ts +128 -0
- package/src/internal/telemetry/risk-telemetry-probes.ts +159 -0
- package/src/ui/hosted/hosted-payment-page.tsx +8 -0
- package/src/ui/hosted/hosted-payment-webview.tsx +42 -4
- package/src/version.ts +1 -1
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Delivers a telemetry reply into the hosted page.
|
|
3
|
+
*
|
|
4
|
+
* The WebView message channel is one-way, so the reply travels the other way:
|
|
5
|
+
* the page exposes `window.__spareLinkNativeTelemetry` before it asks, and the
|
|
6
|
+
* shell answers by injecting a call to it. A page without the callback — an
|
|
7
|
+
* older page, or one that already gave up waiting — receives nothing and the
|
|
8
|
+
* injected chunk evaluates to a harmless literal.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** Page-exposed global the shell invokes with its telemetry reply. */
|
|
12
|
+
export const NATIVE_TELEMETRY_CALLBACK_NAME = '__spareLinkNativeTelemetry';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Builds the script that hands [payload] to the page's telemetry callback.
|
|
16
|
+
*
|
|
17
|
+
* The payload is embedded as a JSON literal. U+2028 and U+2029 are legal inside
|
|
18
|
+
* a JSON string but terminate a statement when evaluated as script, so they are
|
|
19
|
+
* escaped; the result is a single expression that is inert on a page without
|
|
20
|
+
* the callback.
|
|
21
|
+
*/
|
|
22
|
+
export function buildNativeTelemetryReplyScript(payload: Record<string, unknown>): string {
|
|
23
|
+
const literal = JSON.stringify(payload)
|
|
24
|
+
.replaceAll('\u2028', '\\u2028')
|
|
25
|
+
.replaceAll('\u2029', '\\u2029');
|
|
26
|
+
return (
|
|
27
|
+
`window.${NATIVE_TELEMETRY_CALLBACK_NAME} && ` +
|
|
28
|
+
`window.${NATIVE_TELEMETRY_CALLBACK_NAME}(${literal}); true;`
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Builds the handler the webview runs when the page asks for telemetry.
|
|
34
|
+
*
|
|
35
|
+
* Collection and injection are injected so the handler is a pure seam: the
|
|
36
|
+
* component wires the real collector and the WebView reference into it. A
|
|
37
|
+
* collector that answers nothing, or throws, injects nothing — the page's own
|
|
38
|
+
* timeout settles the request.
|
|
39
|
+
*/
|
|
40
|
+
export function createTelemetryRequestHandler(deps: {
|
|
41
|
+
collect: () => Promise<Record<string, unknown> | null>;
|
|
42
|
+
inject: (script: string) => void;
|
|
43
|
+
}): () => Promise<void> {
|
|
44
|
+
return async () => {
|
|
45
|
+
let payload: Record<string, unknown> | null;
|
|
46
|
+
try {
|
|
47
|
+
payload = await deps.collect();
|
|
48
|
+
} catch {
|
|
49
|
+
payload = null;
|
|
50
|
+
}
|
|
51
|
+
if (payload == null) return;
|
|
52
|
+
|
|
53
|
+
try {
|
|
54
|
+
deps.inject(buildNativeTelemetryReplyScript(payload));
|
|
55
|
+
} catch {
|
|
56
|
+
// A torn-down webview cannot be injected into; the page's timeout answers.
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Collects this device's native risk facts for the hosted payment page.
|
|
3
|
+
*
|
|
4
|
+
* Every probe is injected, so the collector runs under Jest with fakes and the
|
|
5
|
+
* module wiring lives in `risk-telemetry-probes`. Every probe is also allowed to
|
|
6
|
+
* fail or hang: a throwing or slow reading leaves its fact absent, because a risk
|
|
7
|
+
* reading must never delay or break a payment.
|
|
8
|
+
*
|
|
9
|
+
* The payload mirrors the page's own risk contract: camelCase keys under a
|
|
10
|
+
* `deviceInformation` block, facts only, nothing fabricated.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** How long any single probe may take before its fact is given up on. */
|
|
14
|
+
export const TELEMETRY_PROBE_BUDGET_MS = 800;
|
|
15
|
+
|
|
16
|
+
/** Identity and build facts read from the platform. */
|
|
17
|
+
export interface RiskDeviceSnapshot {
|
|
18
|
+
readonly operatingSystem: string | null;
|
|
19
|
+
readonly operatingSystemVersion: string | null;
|
|
20
|
+
readonly model: string | null;
|
|
21
|
+
readonly manufacturer: string | null;
|
|
22
|
+
/** A per-vendor install-stable identifier, when the platform offers one. */
|
|
23
|
+
readonly vendorIdentifier: string | null;
|
|
24
|
+
/** False on simulators and emulators. */
|
|
25
|
+
readonly isPhysicalDevice: boolean | null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** A battery reading, with the level as a 0 to 1 fraction. */
|
|
29
|
+
export interface RiskBatteryReading {
|
|
30
|
+
readonly levelFraction: number | null;
|
|
31
|
+
readonly isCharging: boolean | null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The network medium classes the risk payload distinguishes. */
|
|
35
|
+
export type RiskConnectionKind = 'wifi' | 'cellular' | 'other';
|
|
36
|
+
|
|
37
|
+
/** The probes the collector runs. Each may answer null, throw, or hang. */
|
|
38
|
+
export interface RiskTelemetryProbes {
|
|
39
|
+
loadDeviceSnapshot(): Promise<RiskDeviceSnapshot | null>;
|
|
40
|
+
loadBattery(): Promise<RiskBatteryReading | null>;
|
|
41
|
+
loadConnection(): Promise<RiskConnectionKind | null>;
|
|
42
|
+
localeTag(): string | null;
|
|
43
|
+
localDateTime(): string | null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const CONNECTION_LABELS: Record<RiskConnectionKind, string> = {
|
|
47
|
+
wifi: 'WiFi',
|
|
48
|
+
cellular: 'Cellular',
|
|
49
|
+
other: 'Other',
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/** Runs a probe under the budget, yielding null on failure or timeout. */
|
|
53
|
+
async function guarded<T>(probe: () => Promise<T | null>): Promise<T | null> {
|
|
54
|
+
try {
|
|
55
|
+
return await Promise.race<T | null>([
|
|
56
|
+
probe(),
|
|
57
|
+
new Promise<null>((resolve) => setTimeout(() => resolve(null), TELEMETRY_PROBE_BUDGET_MS)),
|
|
58
|
+
]);
|
|
59
|
+
} catch {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function read<T>(reader: () => T | null): T | null {
|
|
65
|
+
try {
|
|
66
|
+
return reader();
|
|
67
|
+
} catch {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Collects whatever the device answered within the per-probe budget.
|
|
74
|
+
*
|
|
75
|
+
* Returns null when nothing answered, so a caller hands the page nothing rather
|
|
76
|
+
* than an empty block.
|
|
77
|
+
*/
|
|
78
|
+
export async function collectRiskTelemetry(
|
|
79
|
+
probes: RiskTelemetryProbes,
|
|
80
|
+
): Promise<Record<string, unknown> | null> {
|
|
81
|
+
const [snapshot, battery, connection] = await Promise.all([
|
|
82
|
+
guarded(probes.loadDeviceSnapshot),
|
|
83
|
+
guarded(probes.loadBattery),
|
|
84
|
+
guarded(probes.loadConnection),
|
|
85
|
+
]);
|
|
86
|
+
const localeTag = read(probes.localeTag);
|
|
87
|
+
const localDateTime = read(probes.localDateTime);
|
|
88
|
+
|
|
89
|
+
const deviceInformation: Record<string, unknown> = {};
|
|
90
|
+
|
|
91
|
+
if (snapshot?.operatingSystem != null) {
|
|
92
|
+
deviceInformation.deviceOperatingSystem = snapshot.operatingSystem;
|
|
93
|
+
}
|
|
94
|
+
if (snapshot?.operatingSystemVersion != null) {
|
|
95
|
+
deviceInformation.deviceOperatingSystemVersion = snapshot.operatingSystemVersion;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const manufacturer: Record<string, unknown> = {};
|
|
99
|
+
if (snapshot?.model != null) manufacturer.model = snapshot.model;
|
|
100
|
+
if (snapshot?.manufacturer != null) manufacturer.manufacturer = snapshot.manufacturer;
|
|
101
|
+
if (Object.keys(manufacturer).length > 0) {
|
|
102
|
+
deviceInformation.deviceManufacturer = manufacturer;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (snapshot?.vendorIdentifier != null) {
|
|
106
|
+
deviceInformation.alternativeDeviceId = snapshot.vendorIdentifier;
|
|
107
|
+
}
|
|
108
|
+
if (localeTag != null) deviceInformation.deviceLanguage = localeTag;
|
|
109
|
+
if (localDateTime != null) deviceInformation.deviceLocalDateTime = localDateTime;
|
|
110
|
+
if (connection != null) deviceInformation.connectionType = CONNECTION_LABELS[connection];
|
|
111
|
+
|
|
112
|
+
const batteryStatus: Record<string, unknown> = {};
|
|
113
|
+
const level = battery?.levelFraction;
|
|
114
|
+
if (level != null && level >= 0 && level <= 1) batteryStatus.level = level;
|
|
115
|
+
if (battery?.isCharging != null) batteryStatus.isCharging = battery.isCharging;
|
|
116
|
+
if (Object.keys(batteryStatus).length > 0) {
|
|
117
|
+
deviceInformation.batteryStatus = batteryStatus;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const environmentContext: string[] = [];
|
|
121
|
+
if (snapshot?.isPhysicalDevice === false) environmentContext.push('EmulatorDetected');
|
|
122
|
+
if (environmentContext.length > 0) {
|
|
123
|
+
deviceInformation.deviceEnvironmentContext = environmentContext;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (Object.keys(deviceInformation).length === 0) return null;
|
|
127
|
+
return { deviceInformation };
|
|
128
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The real device probes behind `collectRiskTelemetry`.
|
|
3
|
+
*
|
|
4
|
+
* Every expo module here is an optional peer, loaded lazily inside a try/catch:
|
|
5
|
+
* a host app that ships without one simply reports that fact as absent, and no
|
|
6
|
+
* import of this file can throw for want of a native module. Structural `*Like`
|
|
7
|
+
* types keep this file honest about how little of each module it relies on.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { Platform } from 'react-native';
|
|
11
|
+
import type {
|
|
12
|
+
RiskBatteryReading,
|
|
13
|
+
RiskConnectionKind,
|
|
14
|
+
RiskDeviceSnapshot,
|
|
15
|
+
RiskTelemetryProbes,
|
|
16
|
+
} from './risk-telemetry-collector';
|
|
17
|
+
|
|
18
|
+
interface DeviceModuleLike {
|
|
19
|
+
modelName?: string | null;
|
|
20
|
+
manufacturer?: string | null;
|
|
21
|
+
osVersion?: string | null;
|
|
22
|
+
isDevice?: boolean;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
interface BatteryModuleLike {
|
|
26
|
+
getBatteryLevelAsync(): Promise<number>;
|
|
27
|
+
getBatteryStateAsync(): Promise<number>;
|
|
28
|
+
BatteryState?: { CHARGING?: number; FULL?: number };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
interface NetworkModuleLike {
|
|
32
|
+
getNetworkStateAsync(): Promise<{ type?: string | null }>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface ApplicationModuleLike {
|
|
36
|
+
getIosIdForVendorAsync(): Promise<string | null>;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function load<T>(moduleName: string): T | null {
|
|
40
|
+
try {
|
|
41
|
+
// Resolved at call time so a host app without the optional peer never pays
|
|
42
|
+
// for it — bundlers keep the require because the name is static.
|
|
43
|
+
switch (moduleName) {
|
|
44
|
+
case 'expo-device':
|
|
45
|
+
return require('expo-device') as T;
|
|
46
|
+
case 'expo-battery':
|
|
47
|
+
return require('expo-battery') as T;
|
|
48
|
+
case 'expo-network':
|
|
49
|
+
return require('expo-network') as T;
|
|
50
|
+
case 'expo-application':
|
|
51
|
+
return require('expo-application') as T;
|
|
52
|
+
default:
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
} catch {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function loadDeviceSnapshot(): Promise<RiskDeviceSnapshot | null> {
|
|
61
|
+
const device = load<DeviceModuleLike>('expo-device');
|
|
62
|
+
if (device == null) return null;
|
|
63
|
+
|
|
64
|
+
// The vendor identifier is iOS-only by design: Android offers no equivalent
|
|
65
|
+
// scoped identifier worth persisting against a payment.
|
|
66
|
+
let vendorIdentifier: string | null = null;
|
|
67
|
+
if (Platform.OS === 'ios') {
|
|
68
|
+
const application = load<ApplicationModuleLike>('expo-application');
|
|
69
|
+
if (application != null) {
|
|
70
|
+
try {
|
|
71
|
+
vendorIdentifier = await application.getIosIdForVendorAsync();
|
|
72
|
+
} catch {
|
|
73
|
+
vendorIdentifier = null;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
operatingSystem: Platform.OS === 'ios' ? 'iOS' : 'Android',
|
|
80
|
+
operatingSystemVersion: device.osVersion ?? null,
|
|
81
|
+
model: device.modelName ?? null,
|
|
82
|
+
manufacturer: device.manufacturer ?? null,
|
|
83
|
+
vendorIdentifier,
|
|
84
|
+
isPhysicalDevice: typeof device.isDevice === 'boolean' ? device.isDevice : null,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async function loadBattery(): Promise<RiskBatteryReading | null> {
|
|
89
|
+
const battery = load<BatteryModuleLike>('expo-battery');
|
|
90
|
+
if (battery == null) return null;
|
|
91
|
+
|
|
92
|
+
const level = await battery.getBatteryLevelAsync();
|
|
93
|
+
const state = await battery.getBatteryStateAsync();
|
|
94
|
+
const charging = battery.BatteryState?.CHARGING;
|
|
95
|
+
const full = battery.BatteryState?.FULL;
|
|
96
|
+
|
|
97
|
+
return {
|
|
98
|
+
// The module reports -1 when the level cannot be read; the collector's 0..1
|
|
99
|
+
// bound would drop it anyway, made explicit here.
|
|
100
|
+
levelFraction: level >= 0 && level <= 1 ? level : null,
|
|
101
|
+
isCharging: charging != null ? state === charging || state === full : null,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async function loadConnection(): Promise<RiskConnectionKind | null> {
|
|
106
|
+
const network = load<NetworkModuleLike>('expo-network');
|
|
107
|
+
if (network == null) return null;
|
|
108
|
+
|
|
109
|
+
const state = await network.getNetworkStateAsync();
|
|
110
|
+
const type = state.type?.toUpperCase() ?? null;
|
|
111
|
+
if (type == null || type === 'NONE' || type === 'UNKNOWN') return null;
|
|
112
|
+
if (type === 'WIFI') return 'wifi';
|
|
113
|
+
if (type === 'CELLULAR') return 'cellular';
|
|
114
|
+
return 'other';
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function localeTag(): string | null {
|
|
118
|
+
try {
|
|
119
|
+
const tag = Intl.DateTimeFormat().resolvedOptions().locale;
|
|
120
|
+
return typeof tag === 'string' && tag.length > 0 ? tag : null;
|
|
121
|
+
} catch {
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Formats a date as local-time ISO 8601 with a numeric UTC offset, e.g.
|
|
128
|
+
* `2026-08-25T10:45:46.319+05:30` — never the UTC `Z` form. This keeps the
|
|
129
|
+
* timestamp on the payer's own clock so it reads the same as the device shows,
|
|
130
|
+
* and matches the Flutter SDK's `deviceLocalDateTime`.
|
|
131
|
+
*/
|
|
132
|
+
export function formatLocalIsoWithOffset(date: Date): string {
|
|
133
|
+
const pad = (value: number, width = 2): string => String(value).padStart(width, '0');
|
|
134
|
+
|
|
135
|
+
// getTimezoneOffset is minutes behind UTC, so east-of-UTC zones are negative;
|
|
136
|
+
// flip the sign to render the conventional offset.
|
|
137
|
+
const offsetMinutes = -date.getTimezoneOffset();
|
|
138
|
+
const sign = offsetMinutes >= 0 ? '+' : '-';
|
|
139
|
+
const absOffset = Math.abs(offsetMinutes);
|
|
140
|
+
|
|
141
|
+
const localDate = `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
|
|
142
|
+
const localTime = `${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}.${pad(date.getMilliseconds(), 3)}`;
|
|
143
|
+
const offset = `${sign}${pad(Math.floor(absOffset / 60))}:${pad(absOffset % 60)}`;
|
|
144
|
+
|
|
145
|
+
return `${localDate}T${localTime}${offset}`;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function localDateTime(): string | null {
|
|
149
|
+
try {
|
|
150
|
+
return formatLocalIsoWithOffset(new Date());
|
|
151
|
+
} catch {
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** The production probe set, ready for `collectRiskTelemetry`. */
|
|
157
|
+
export function createRiskTelemetryProbes(): RiskTelemetryProbes {
|
|
158
|
+
return { loadDeviceSnapshot, loadBattery, loadConnection, localeTag, localDateTime };
|
|
159
|
+
}
|
|
@@ -2,6 +2,8 @@ import { useCallback, useEffect, useRef } from 'react';
|
|
|
2
2
|
import { ActivityIndicator, StyleSheet, Text, View } from 'react-native';
|
|
3
3
|
import type { HostedFlowController } from '../../core/services/hosted-flow-controller';
|
|
4
4
|
import { isTerminalPhase } from '../../core/services/hosted-flow-state';
|
|
5
|
+
import { collectRiskTelemetry } from '../../internal/telemetry/risk-telemetry-collector';
|
|
6
|
+
import { createRiskTelemetryProbes } from '../../internal/telemetry/risk-telemetry-probes';
|
|
5
7
|
import { useHostedFlowState } from '../hooks/use-hosted-flow-state';
|
|
6
8
|
import { PageUnavailableScreen } from '../screens/page-unavailable-screen';
|
|
7
9
|
import { RedirectHandoffScreen } from '../screens/redirect-handoff-screen';
|
|
@@ -70,6 +72,11 @@ export function HostedPaymentPage(props: HostedPaymentPageProps): JSX.Element {
|
|
|
70
72
|
[controller],
|
|
71
73
|
);
|
|
72
74
|
|
|
75
|
+
// One probe set for the page's lifetime; collection itself runs per request.
|
|
76
|
+
const handleTelemetryRequest = useCallback(() => {
|
|
77
|
+
return collectRiskTelemetry(createRiskTelemetryProbes());
|
|
78
|
+
}, []);
|
|
79
|
+
|
|
73
80
|
const handleLoadFailure = useCallback(() => {
|
|
74
81
|
controller.failToLoad();
|
|
75
82
|
}, [controller]);
|
|
@@ -105,6 +112,7 @@ export function HostedPaymentPage(props: HostedPaymentPageProps): JSX.Element {
|
|
|
105
112
|
baseUrl={baseUrl}
|
|
106
113
|
reference={controller.reference}
|
|
107
114
|
onBridgeMessage={handleBridgeMessage}
|
|
115
|
+
onTelemetryRequest={handleTelemetryRequest}
|
|
108
116
|
onLoadFailure={handleLoadFailure}
|
|
109
117
|
onMounted={handlePageMounted}
|
|
110
118
|
/>
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import { useCallback, useEffect } from 'react';
|
|
1
|
+
import { useCallback, useEffect, useRef } from 'react';
|
|
2
2
|
import { StyleSheet, View } from 'react-native';
|
|
3
3
|
import { WebView, type WebViewMessageEvent } from 'react-native-webview';
|
|
4
|
+
import { parseHostedBridgeMessage } from '../../internal/hosted/bridge-message';
|
|
4
5
|
import { buildHostedPaymentUrl, isHostedOriginUrl } from '../../internal/hosted/hosted-url';
|
|
5
6
|
import { createLogger } from '../../internal/logger';
|
|
7
|
+
import { createTelemetryRequestHandler } from '../../internal/telemetry/native-telemetry-reply';
|
|
6
8
|
import { parseUrlParts } from '../../internal/url-parts';
|
|
7
9
|
import { Colors } from '../theme';
|
|
8
10
|
|
|
@@ -44,6 +46,15 @@ export interface HostedPaymentWebViewProps {
|
|
|
44
46
|
* this component stays a transport.
|
|
45
47
|
*/
|
|
46
48
|
onBridgeMessage: (message: unknown) => void;
|
|
49
|
+
/**
|
|
50
|
+
* Answers the page's telemetry request with this device's native risk facts.
|
|
51
|
+
*
|
|
52
|
+
* The reply travels back by injected script rather than a return value, so it
|
|
53
|
+
* is handled here where the webview reference lives, and the message never
|
|
54
|
+
* reaches [onBridgeMessage]. Absent, or answering null, means the page
|
|
55
|
+
* proceeds with what the browser alone can see.
|
|
56
|
+
*/
|
|
57
|
+
onTelemetryRequest?: () => Promise<Record<string, unknown> | null>;
|
|
47
58
|
/** Called when the page itself could not be loaded. */
|
|
48
59
|
onLoadFailure?: () => void;
|
|
49
60
|
/**
|
|
@@ -63,8 +74,10 @@ export interface HostedPaymentWebViewProps {
|
|
|
63
74
|
* the page reports, and refuses to navigate anywhere else.
|
|
64
75
|
*/
|
|
65
76
|
export function HostedPaymentWebView(props: HostedPaymentWebViewProps): JSX.Element {
|
|
66
|
-
const { baseUrl, reference, onBridgeMessage, onLoadFailure, onMounted } =
|
|
77
|
+
const { baseUrl, reference, onBridgeMessage, onTelemetryRequest, onLoadFailure, onMounted } =
|
|
78
|
+
props;
|
|
67
79
|
const uri = buildHostedPaymentUrl({ baseUrl, reference });
|
|
80
|
+
const webViewRef = useRef<WebView>(null);
|
|
68
81
|
|
|
69
82
|
// This reports that *this* webview exists, and a retry mounts a fresh instance
|
|
70
83
|
// which reports itself anew. Re-running on a new callback identity would
|
|
@@ -76,9 +89,23 @@ export function HostedPaymentWebView(props: HostedPaymentWebViewProps): JSX.Elem
|
|
|
76
89
|
|
|
77
90
|
const handleMessage = useCallback(
|
|
78
91
|
(event: WebViewMessageEvent) => {
|
|
79
|
-
|
|
92
|
+
const raw = event.nativeEvent.data;
|
|
93
|
+
|
|
94
|
+
// The telemetry request is answered here, where the webview reference
|
|
95
|
+
// lives, because the reply is delivered by injecting script — a channel
|
|
96
|
+
// only this component holds. Everything else stays the caller's.
|
|
97
|
+
if (onTelemetryRequest && parseHostedBridgeMessage(raw)?.type === 'telemetryRequest') {
|
|
98
|
+
const respond = createTelemetryRequestHandler({
|
|
99
|
+
collect: onTelemetryRequest,
|
|
100
|
+
inject: (script) => webViewRef.current?.injectJavaScript(script),
|
|
101
|
+
});
|
|
102
|
+
void respond();
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
onBridgeMessage(raw);
|
|
80
107
|
},
|
|
81
|
-
[onBridgeMessage],
|
|
108
|
+
[onBridgeMessage, onTelemetryRequest],
|
|
82
109
|
);
|
|
83
110
|
|
|
84
111
|
// Every navigation is routed through here rather than through `originWhitelist`.
|
|
@@ -99,6 +126,7 @@ export function HostedPaymentWebView(props: HostedPaymentWebViewProps): JSX.Elem
|
|
|
99
126
|
// modal, so this fills what it is given.
|
|
100
127
|
<View style={styles.container} testID="hosted-payment-webview">
|
|
101
128
|
<WebView
|
|
129
|
+
ref={webViewRef}
|
|
102
130
|
source={{ uri }}
|
|
103
131
|
onMessage={handleMessage}
|
|
104
132
|
originWhitelist={ALLOW_ALL_ORIGINS}
|
|
@@ -141,6 +169,16 @@ export function HostedPaymentWebView(props: HostedPaymentWebViewProps): JSX.Elem
|
|
|
141
169
|
style={styles.webview}
|
|
142
170
|
javaScriptEnabled
|
|
143
171
|
domStorageEnabled
|
|
172
|
+
// Lets the page's consent screen ask for a coarse location fix, which it
|
|
173
|
+
// attaches to the payment as a risk signal. Android only; iOS raises its
|
|
174
|
+
// own per-origin system prompt, gated by the host app's usage-description
|
|
175
|
+
// entry. This toggle is frame-wide — the platform offers no per-origin
|
|
176
|
+
// answer here — so the scope rests on `handleShouldStartLoad` above
|
|
177
|
+
// refusing every off-origin document in this frame. The toggle alone also
|
|
178
|
+
// reveals nothing: the host application must hold the user-facing location
|
|
179
|
+
// permission, and without it the page's request fails and the payment
|
|
180
|
+
// proceeds without the signal.
|
|
181
|
+
geolocationEnabled
|
|
144
182
|
// The page holds a payment in progress and nothing else should share its
|
|
145
183
|
// storage, on this device or between payers on it.
|
|
146
184
|
incognito
|
package/src/version.ts
CHANGED