Skip to content

feat: add device detection for (ios/android) and enable appClip default #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@reclaimprotocol/js-sdk",
"version": "3.0.1",
"version": "3.0.3",
"description": "Designed to request proofs from the Reclaim protocol and manage the flow of claims and witness interactions.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
23 changes: 22 additions & 1 deletion src/Reclaim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
InitSessionResponse,
ClaimCreationType,
} from './utils/types'
import { SessionStatus } from './utils/types'
import { SessionStatus, DeviceType } from './utils/types'
import { ethers } from 'ethers'
import canonicalize from 'canonicalize'
import {
Expand All @@ -35,6 +35,7 @@ import {
import { validateContext, validateFunctionParams, validateParameters, validateSignature, validateURL } from './utils/validationUtils'
import { fetchStatusUrl, initSession, updateSession } from './utils/sessionUtils'
import { assertValidSignedClaim, createLinkWithTemplateData, getWitnessesForClaim } from './utils/proofUtils'
import { userAgentIsAndroid, userAgentIsIOS } from './utils/device'
import loggerModule from './utils/logger';
const logger = loggerModule.logger

Expand Down Expand Up @@ -149,6 +150,23 @@ export class ReclaimProofRequest {
this.applicationId = applicationId;
this.sessionId = "";
this.parameters = {};

if (!options) {
options = {};
}

if (!options.device) {
if (userAgentIsIOS) {
options.device = DeviceType.IOS;
} else if (userAgentIsAndroid) {
options.device = DeviceType.ANDROID;
}
}

if (options.useAppClip === undefined) {
options.useAppClip = true;
}

if (options?.log) {
loggerModule.setLogLevel('info');
} else {
Expand Down Expand Up @@ -186,6 +204,9 @@ export class ReclaimProofRequest {
{ paramName: 'log', input: options.log }
], 'the constructor')
}
if (options.useAppClip === undefined) {
options.useAppClip = true;
}
if (options.useAppClip) {
validateFunctionParams([
{ paramName: 'useAppClip', input: options.useAppClip }
Expand Down
21 changes: 21 additions & 0 deletions src/utils/device.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { DeviceType } from "./types";

const navigatorDefined = typeof navigator !== 'undefined';
const windowDefined = typeof window !== 'undefined';

const userAgent = navigatorDefined ? navigator.userAgent.toLowerCase() : '';
const userAgentData = navigatorDefined ? (navigator as Navigator & { userAgentData?: { platform: string } }).userAgentData : undefined;

export const userAgentIsAndroid = userAgent.includes(DeviceType.ANDROID);

const isIpad =
windowDefined &&
navigatorDefined &&
(userAgentData?.platform === DeviceType.IPAD || userAgent.includes(DeviceType.IPAD));

export const userAgentIsIOS =
new RegExp(`${DeviceType.IOS}|ipod`, 'i').test(userAgent) || isIpad;

export const userAgentIsMobile =
new RegExp(`${DeviceType.ANDROID}|webos|${DeviceType.IOS}|${DeviceType.IPAD}|ipod|blackberry|iemobile|opera mini`, 'i').test(userAgent) ||
(windowDefined && 'orientation' in window);
8 changes: 8 additions & 0 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ export enum ClaimCreationType {
ON_ME_CHAIN = 'createClaimOnMechain'
}

// Device type enum
export enum DeviceType {
ANDROID = 'android',
IOS = 'ios',
IPAD = 'ipad'
}


// Session and response types
export type InitSessionResponse = {
sessionId: string;
Expand Down