Skip to content

Commit 43e0307

Browse files
authored
Merge pull request #31 from reclaimprotocol/feat/device-detection
feat: add device detection for (ios/android) and enable appClip default
2 parents 7e6b647 + 8061cf6 commit 43e0307

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@reclaimprotocol/js-sdk",
3-
"version": "3.0.1",
3+
"version": "3.0.3",
44
"description": "Designed to request proofs from the Reclaim protocol and manage the flow of claims and witness interactions.",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/Reclaim.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
InitSessionResponse,
1010
ClaimCreationType,
1111
} from './utils/types'
12-
import { SessionStatus } from './utils/types'
12+
import { SessionStatus, DeviceType } from './utils/types'
1313
import { ethers } from 'ethers'
1414
import canonicalize from 'canonicalize'
1515
import {
@@ -35,6 +35,7 @@ import {
3535
import { validateContext, validateFunctionParams, validateParameters, validateSignature, validateURL } from './utils/validationUtils'
3636
import { fetchStatusUrl, initSession, updateSession } from './utils/sessionUtils'
3737
import { assertValidSignedClaim, createLinkWithTemplateData, getWitnessesForClaim } from './utils/proofUtils'
38+
import { userAgentIsAndroid, userAgentIsIOS } from './utils/device'
3839
import loggerModule from './utils/logger';
3940
const logger = loggerModule.logger
4041

@@ -149,6 +150,23 @@ export class ReclaimProofRequest {
149150
this.applicationId = applicationId;
150151
this.sessionId = "";
151152
this.parameters = {};
153+
154+
if (!options) {
155+
options = {};
156+
}
157+
158+
if (!options.device) {
159+
if (userAgentIsIOS) {
160+
options.device = DeviceType.IOS;
161+
} else if (userAgentIsAndroid) {
162+
options.device = DeviceType.ANDROID;
163+
}
164+
}
165+
166+
if (options.useAppClip === undefined) {
167+
options.useAppClip = true;
168+
}
169+
152170
if (options?.log) {
153171
loggerModule.setLogLevel('info');
154172
} else {
@@ -186,6 +204,9 @@ export class ReclaimProofRequest {
186204
{ paramName: 'log', input: options.log }
187205
], 'the constructor')
188206
}
207+
if (options.useAppClip === undefined) {
208+
options.useAppClip = true;
209+
}
189210
if (options.useAppClip) {
190211
validateFunctionParams([
191212
{ paramName: 'useAppClip', input: options.useAppClip }

src/utils/device.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { DeviceType } from "./types";
2+
3+
const navigatorDefined = typeof navigator !== 'undefined';
4+
const windowDefined = typeof window !== 'undefined';
5+
6+
const userAgent = navigatorDefined ? navigator.userAgent.toLowerCase() : '';
7+
const userAgentData = navigatorDefined ? (navigator as Navigator & { userAgentData?: { platform: string } }).userAgentData : undefined;
8+
9+
export const userAgentIsAndroid = userAgent.includes(DeviceType.ANDROID);
10+
11+
const isIpad =
12+
windowDefined &&
13+
navigatorDefined &&
14+
(userAgentData?.platform === DeviceType.IPAD || userAgent.includes(DeviceType.IPAD));
15+
16+
export const userAgentIsIOS =
17+
new RegExp(`${DeviceType.IOS}|ipod`, 'i').test(userAgent) || isIpad;
18+
19+
export const userAgentIsMobile =
20+
new RegExp(`${DeviceType.ANDROID}|webos|${DeviceType.IOS}|${DeviceType.IPAD}|ipod|blackberry|iemobile|opera mini`, 'i').test(userAgent) ||
21+
(windowDefined && 'orientation' in window);

src/utils/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ export enum ClaimCreationType {
4343
ON_ME_CHAIN = 'createClaimOnMechain'
4444
}
4545

46+
// Device type enum
47+
export enum DeviceType {
48+
ANDROID = 'android',
49+
IOS = 'ios',
50+
IPAD = 'ipad'
51+
}
52+
53+
4654
// Session and response types
4755
export type InitSessionResponse = {
4856
sessionId: string;

0 commit comments

Comments
 (0)