UNPKG

@b8safe/react-native-safe

Version:
56 lines (54 loc) 2.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getToken = void 0; var _reactNative = require("react-native"); var _reactNativeSha = require("react-native-sha256"); // const LINKING_ERROR = // `The package 'react-native-b8safe' doesn't seem to be linked. Make sure: \n\n` + // Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) + // '- You rebuilt the app after installing the package\n' + // '- You are not using Expo Go\n'; const DeviceCheck = _reactNative.NativeModules.DeviceCheck !== undefined ? _reactNative.NativeModules.DeviceCheck : null; const PlayIntegrity = _reactNative.NativeModules.PlayIntegrity !== undefined ? _reactNative.NativeModules.PlayIntegrity : null; function generateRandomString(length) { const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; let result = ''; const charactersLength = characters.length; for (let i = 0; i < length; i++) result += characters.charAt(Math.floor(Math.random() * charactersLength)); return result; } const getToken = async (payload, type) => { try { type = type ?? 'standard'; if (_reactNative.Platform.OS === 'android') { const isAvailable = await PlayIntegrity?.isPlayIntegrityAvailable(); if (isAvailable) { if (type === 'classic') { const nonce = await (0, _reactNativeSha.sha256)(JSON.stringify(payload ?? {})); const base64Nonce = new Buffer(nonce).toString('base64'); return [await PlayIntegrity?.requestIntegrityToken(base64Nonce, null), nonce].join('|'); } else { try { const isPrepared = await PlayIntegrity?.isStandardIntegrityTokenProviderPrepared(); if (isPrepared) { const hash = generateRandomString(64); return [await PlayIntegrity?.requestStandardIntegrityToken(hash), hash].join('|'); } } catch (e) { console.error('Erro ao obter o token de integridade padrão:', e); } } } return null; } else if (DeviceCheck !== null) { return await DeviceCheck.getDeviceToken(); } } catch (e) { console.error('Erro ao obter o token de integridade:', e); } return null; }; exports.getToken = getToken; //# sourceMappingURL=module.js.map