Skip to content
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

Add file logger RN example #1080

Merged
merged 1 commit into from
Sep 2, 2024
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
173 changes: 15 additions & 158 deletions libs/sdk-react-native/example/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<TouchableOpacity style={{ flex: 1 }}>
<View style={{ margin: 5 }}>
<Text style={{ fontWeight: "bold" }}>{title}</Text>
{text && text.length > 0 ? <Text>{text}</Text> : <></>}
</View>
</TouchableOpacity>
)
}
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 (
<SafeAreaView>
<StatusBar />
<ScrollView style={{ padding: 5 }}>
{lines.map((line) => (
<DebugLine key={line.at} title={line.title} text={line.text} />
))}
</ScrollView>
</SafeAreaView>
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Example App" component={HomeScreen} />
</Stack.Navigator>
</NavigationContainer>
)
}

Expand Down
9 changes: 7 additions & 2 deletions libs/sdk-react-native/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -36,4 +41,4 @@
"jest": {
"preset": "react-native"
}
}
}
15 changes: 15 additions & 0 deletions libs/sdk-react-native/example/src/components/DebugLine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from "react"
import { Text, TouchableOpacity, View } from "react-native"

const DebugLine = ({ title, text }) => {
return (
<TouchableOpacity style={{ flex: 1 }}>
<View style={{ margin: 5 }}>
<Text style={{ fontWeight: "bold" }}>{title}</Text>
{text && text.length > 0 ? <Text>{text}</Text> : <></>}
</View>
</TouchableOpacity>
)
}

export default DebugLine
Loading
Loading