From ee37f301fbe3f3bd01cb841ecaa8767f556f223c Mon Sep 17 00:00:00 2001 From: Ross Savage Date: Wed, 28 Aug 2024 14:54:45 +0200 Subject: [PATCH] Add file logger RN example --- libs/sdk-react-native/example/App.js | 173 ++------------ libs/sdk-react-native/example/package.json | 9 +- .../example/src/components/DebugLine.js | 15 ++ .../example/src/screens/HomeScreen.js | 212 ++++++++++++++++++ .../example/src/utils/logging.ts | 49 ++++ .../example/{ => src}/utils/security.ts | 0 .../example/{ => src}/utils/storage.ts | 0 libs/sdk-react-native/example/yarn.lock | 108 ++++++++- 8 files changed, 404 insertions(+), 162 deletions(-) create mode 100644 libs/sdk-react-native/example/src/components/DebugLine.js create mode 100644 libs/sdk-react-native/example/src/screens/HomeScreen.js create mode 100644 libs/sdk-react-native/example/src/utils/logging.ts rename libs/sdk-react-native/example/{ => src}/utils/security.ts (100%) rename libs/sdk-react-native/example/{ => src}/utils/storage.ts (100%) diff --git a/libs/sdk-react-native/example/App.js b/libs/sdk-react-native/example/App.js index a2185738a..6f6e7e0a9 100644 --- a/libs/sdk-react-native/example/App.js +++ b/libs/sdk-react-native/example/App.js @@ -6,169 +6,26 @@ * @flow strict-local */ -import React, { useState } from "react" -import { SafeAreaView, ScrollView, StatusBar, Text, TouchableOpacity, View } from "react-native" -import { - backup, - backupStatus, - buyBitcoin, - BuyBitcoinProvider, - checkMessage, - connect, - defaultConfig, - EnvironmentType, - fetchFiatRates, - fetchReverseSwapFees, - inProgressReverseSwaps, - lspInfo, - listFiatCurrencies, - mnemonicToSeed, - NodeConfigVariant, - nodeInfo, - openChannelFee, - receivePayment, - setLogStream, - signMessage -} from "@breeztech/react-native-breez-sdk" -import BuildConfig from "react-native-build-config" -import { generateMnemonic } from "@dreson4/react-native-quick-bip39" -import { obfuscateString } from "./utils/security" -import { getSecureItem, setSecureItem } from "./utils/storage" +import React from "react" +import { NavigationContainer } from "@react-navigation/native" +import { createNativeStackNavigator } from "@react-navigation/native-stack" +import { FileLogger } from "react-native-file-logger" +import HomeScreen from "./src/screens/HomeScreen" -const MNEMONIC_STORE = "MNEMONIC_SECURE_STORE" +FileLogger.configure({ + captureConsole: false, + maximumNumberOfFiles: 5 +}) -const DebugLine = ({ title, text }) => { - return ( - - - {title} - {text && text.length > 0 ? {text} : <>} - - - ) -} +const Stack = createNativeStackNavigator() const App = () => { - const [lines, setLines] = useState([]) - - const addLine = (title, text) => { - setLines((lines) => [{ at: new Date().getTime(), title, text }, ...lines]) - console.log(`${title}${text && text.length > 0 ? ": " + text : ""}`) - } - - const logHandler = (logEntry) => { - if (logEntry.level != "TRACE") { - console.log(`[${logEntry.level}]: ${logEntry.line}`) - } - } - - const eventHandler = (breezEvent) => { - addLine("event", JSON.stringify(breezEvent)) - } - - React.useEffect(() => { - let logSubscription, eventSubscription - - const asyncFn = async () => { - try { - logSubscription = await setLogStream(logHandler) - - let mnemonic = await getSecureItem(MNEMONIC_STORE) - - if (mnemonic == null) { - mnemonic = generateMnemonic(256) - setSecureItem(MNEMONIC_STORE, mnemonic) - } - - let seed = await mnemonicToSeed(mnemonic) - addLine("mnemonicToSeed", obfuscateString(JSON.stringify(seed))) - - const nodeConfig = { - type: NodeConfigVariant.GREENLIGHT, - config: {} - } - - const config = await defaultConfig(EnvironmentType.PRODUCTION, BuildConfig.BREEZ_API_KEY, nodeConfig) - addLine("defaultConfig", JSON.stringify(config)) - - eventSubscription = await connect({ config, seed }, eventHandler) - addLine("connect", null) - - const nodeState = await nodeInfo() - addLine("nodeInfo", JSON.stringify(nodeState)) - - const lsp = await lspInfo() - addLine("lspInfo", JSON.stringify(lsp)) - - const fiatCurrencies = await listFiatCurrencies() - addLine("listFiatCurrencies", JSON.stringify(fiatCurrencies)) - - const fiatRates = await fetchFiatRates() - addLine("fetchFiatRates", JSON.stringify(fiatRates)) - - const revSwapFees = await fetchReverseSwapFees({ sendAmountSat: null }) - addLine("revSwapFees", JSON.stringify(revSwapFees)) - - const inProgressRevSwaps = await inProgressReverseSwaps() - addLine("inProgressRevSwaps", JSON.stringify(inProgressRevSwaps)) - - const buyBitcoinResult = await buyBitcoin({ - provider: BuyBitcoinProvider.MOONPAY, - openingFeeParams: null - }) - addLine("buyBitcoin", JSON.stringify(buyBitcoinResult)) - - const signMessageResult = await signMessage({ message: "Hello world" }) - addLine("signMessage: Hello World", JSON.stringify(signMessageResult)) - - const verifyMessageResult = await checkMessage({ - message: "Hello world", - pubkey: nodeState.id, - signature: signMessageResult.signature - }) - addLine("verifyMessage:", JSON.stringify(verifyMessageResult)) - - const openChannelFeeResult = await openChannelFee({ - amountMsat: 100000000, - expiry: 3600 - }) - addLine("openChannelFee", JSON.stringify(openChannelFeeResult)) - - const receivePaymentResult = await receivePayment({ - amountMsat: 100000000, - description: "Hello world", - expiry: 3600, - cltv: 144, - useDescriptionHash: true, - openingFeeParams: openChannelFeeResult.usedFeeParams - }) - addLine("receivePayment", JSON.stringify(receivePaymentResult)) - - await backup() - addLine("backupStatus", JSON.stringify(await backupStatus())) - } catch (e) { - addLine("error", e.toString()) - console.log(`Error: ${JSON.stringify(e)}`) - } - } - - asyncFn() - - return () => { - logSubscription.remove() - eventSubscription.remove() - } - }, []) - return ( - - - - {lines.map((line) => ( - - ))} - - + + + + + ) } diff --git a/libs/sdk-react-native/example/package.json b/libs/sdk-react-native/example/package.json index 065dc4b6e..f671a9bab 100644 --- a/libs/sdk-react-native/example/package.json +++ b/libs/sdk-react-native/example/package.json @@ -13,13 +13,18 @@ "rebuild": "rm -rf node_modules && yarn && yarn pods" }, "dependencies": { - "@breeztech/react-native-breez-sdk": "0.5.1-rc6", + "@breeztech/react-native-breez-sdk": "0.5.2", "@dreson4/react-native-quick-bip39": "^0.0.5", + "@react-navigation/native": "6.1.1", + "@react-navigation/native-stack": "6.9.7", "react": "18.1.0", "react-native": "0.70.15", "react-native-build-config": "^0.3.2", + "react-native-file-logger": "0.4.1", "react-native-quick-base64": "^2.0.5", "react-native-quick-crypto": "^0.5.0", + "react-native-safe-area-context": "3.3.2", + "react-native-screens": "3.7.2", "react-native-secure-storage": "https://github.com/satimoto/react-native-secure-storage" }, "devDependencies": { @@ -36,4 +41,4 @@ "jest": { "preset": "react-native" } -} \ No newline at end of file +} diff --git a/libs/sdk-react-native/example/src/components/DebugLine.js b/libs/sdk-react-native/example/src/components/DebugLine.js new file mode 100644 index 000000000..a7d4ccb80 --- /dev/null +++ b/libs/sdk-react-native/example/src/components/DebugLine.js @@ -0,0 +1,15 @@ +import React from "react" +import { Text, TouchableOpacity, View } from "react-native" + +const DebugLine = ({ title, text }) => { + return ( + + + {title} + {text && text.length > 0 ? {text} : <>} + + + ) +} + +export default DebugLine diff --git a/libs/sdk-react-native/example/src/screens/HomeScreen.js b/libs/sdk-react-native/example/src/screens/HomeScreen.js new file mode 100644 index 000000000..2e116daae --- /dev/null +++ b/libs/sdk-react-native/example/src/screens/HomeScreen.js @@ -0,0 +1,212 @@ +import React, { useEffect, useState } from "react" +import { Button, Platform, SafeAreaView, ScrollView } from "react-native" +import { + backup, + backupStatus, + buyBitcoin, + BuyBitcoinProvider, + checkMessage, + connect, + defaultConfig, + EnvironmentType, + fetchFiatRates, + fetchReverseSwapFees, + inProgressReverseSwaps, + lspInfo, + listFiatCurrencies, + mnemonicToSeed, + NodeConfigVariant, + nodeInfo, + openChannelFee, + receivePayment, + setLogStream, + signMessage +} from "@breeztech/react-native-breez-sdk" +import BuildConfig from "react-native-build-config" +import { FileLogger } from "react-native-file-logger" +import { generateMnemonic } from "@dreson4/react-native-quick-bip39" +import DebugLine from "../components/DebugLine" +import { Log } from "../utils/logging" +import { obfuscateString } from "../utils/security" +import { getSecureItem, setSecureItem } from "../utils/storage" + +const log = new Log("ExampleApp") + +const DEBUG = Platform.select({ + android: BuildConfig.DEBUG, + ios: BuildConfig.DEBUG === "true" +}) + +const MNEMONIC_STORE = "MNEMONIC_SECURE_STORE" + +const HomeScreen = ({ navigation }) => { + const [lines, setLines] = useState([]) + + // Log sharing + const onShareLogs = async () => { + let nodeInfo = {} + + try { + nodeInfo = await nodeInfo() + } catch (e) { + nodeInfo = { error: JSON.stringify(e) } + } + + try { + await FileLogger.sendLogFilesByEmail({ + subject: "Logs", + body: `OS: ${Platform.OS} - ${Platform.Version}\n\n` + JSON.stringify(nodeInfo, null, 2) + }) + } catch (e) { + log.error(JSON.stringify(e)) + } + } + + useEffect(() => { + navigation.setOptions({ + headerRight: () =>