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

Expo Plugin #563

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions packages/react-native/app.plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("./plugin/build")
1 change: 1 addition & 0 deletions packages/react-native/example-expo/.env-example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
EXPO_PUBLIC_API_KEY=
40 changes: 40 additions & 0 deletions packages/react-native/example-expo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Learn more https://docs.github.com/en/get-started/getting-started-with-git/ignoring-files

# dependencies
node_modules/

# Expo
.expo/
dist/
web-build/
expo-env.d.ts

# Native
*.orig.*
*.jks
*.p8
*.p12
*.key
*.mobileprovision

# Metro
.metro-health-check*

# debug
npm-debug.*
yarn-debug.*
yarn-error.*

# macOS
.DS_Store
*.pem

# local env files
.env*.local

# typescript
*.tsbuildinfo

# Build
ios/
android/
151 changes: 151 additions & 0 deletions packages/react-native/example-expo/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import { useState, useEffect, useRef } from "react"
import { StatusBar } from "expo-status-bar"
import { StyleSheet, Text, View, ScrollView, TouchableOpacity, SafeAreaView } from "react-native"
import { getItemAsync, setItemAsync } from "expo-secure-store"
import { Mnemonic } from "./mnemonic"
import {
addEventListener,
connect,
defaultConfig,
getInfo,
LiquidNetwork,
listPayments,
removeEventListener,
prepareReceivePayment,
prepareSendPayment,
receivePayment,
sendPayment,
PaymentMethod
} from "@breeztech/react-native-breez-sdk-liquid"
import type { SdkEvent } from "@breeztech/react-native-breez-sdk-liquid"

const MNEMONIC_STORE = "mnemonic"

const DebugLine = ({ title, text }: { title: string; text?: string }) => {
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 mnemonicGenerator = new Mnemonic()

export default function App() {
const [lines, setLines] = useState<Array<{ at: number; title: string; text?: string }>>([])
const listenerIdRef = useRef<string>()

useEffect(() => {
const addLine = (title: string, text?: string): void => {
setLines((lines) => [{ at: new Date().getTime(), title, text }, ...lines])
console.log(`${title}${text && text.length > 0 ? ": " + text : ""}`)
}

const eventHandler = (event: SdkEvent) => {
addLine("SDK Event", JSON.stringify(event))
}

const bolt11Invoice: string = ""

const asyncFn = async () => {
try {
let mnemonic = await getItemAsync(MNEMONIC_STORE)
if (!mnemonic) {
mnemonic = await mnemonicGenerator.generateMnemonic(256)
await setItemAsync(MNEMONIC_STORE, mnemonic)
}

// Get API Key
const apiKey = process.env.EXPO_PUBLIC_API_KEY
larcho marked this conversation as resolved.
Show resolved Hide resolved

if (!apiKey) {
throw new Error("No API Key set")
}

// Connect using the config
const config = await defaultConfig(LiquidNetwork.MAINNET, apiKey)
addLine("defaultConfig", JSON.stringify(config))

await connect({ config, mnemonic })
addLine("connect")

// Get wallet info
const getInfoRes = await getInfo()
addLine("getInfo", JSON.stringify(getInfoRes))

// Historical payments list
await listPayments({})

// Register for events
const listenerId = await addEventListener(eventHandler)
listenerIdRef.current = listenerId
addLine("addEventListener", listenerId)

/* Receive lightning payment */

const prepareReceiveRes = await prepareReceivePayment({ payerAmountSat: 1000, paymentMethod: PaymentMethod.LIGHTNING })
addLine("prepareReceivePayment", JSON.stringify(prepareReceiveRes))
// Get the fees required for this payment
addLine("Payment fees", `${prepareReceiveRes.feesSat}`)

const receivePaymentRes = await receivePayment({ prepareResponse: prepareReceiveRes })
addLine("receivePayment", JSON.stringify(receivePaymentRes))
// Wait for payer to pay.... once successfully paid an event of `paymentSucceeded` will be emitted.
addLine("Bolt11 invoice", `${receivePaymentRes.destination}`)

/* Send lightning payment */

// Set the `bolt11Invoice` to enable sending in the example app
if (bolt11Invoice) {
let prepareSendRes = await prepareSendPayment({ destination: bolt11Invoice })
addLine("prepareSendPayment", JSON.stringify(prepareSendRes))
// Get the fees required for this payment
addLine("Payment fees", `${prepareSendRes.feesSat}`)

let sendPaymentRes = await sendPayment({ prepareResponse: prepareSendRes })
addLine("sendPayment", JSON.stringify(sendPaymentRes))
// Once successfully paid an event of `paymentSucceeded` will be emitted.
addLine("Payment", JSON.stringify(sendPaymentRes.payment))
}
} catch (error: unknown) {
if (error instanceof Error) {
addLine("error", error.message)
}
console.log(`Error: ${JSON.stringify(error)}`)
}
}

asyncFn()

return () => {
const listenerId = listenerIdRef.current
if (listenerId) {
removeEventListener(listenerId)
listenerIdRef.current = undefined
}
}
}, [setLines])

return (
<SafeAreaView style={styles.container}>
<ScrollView style={{ margin: 5 }}>
{lines.map((line) => (
<DebugLine key={`${line.at}-${line.title}`} title={line.title} text={line.text} />
))}
</ScrollView>
<StatusBar style="auto" />
</SafeAreaView>
)
}

const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center"
}
})
53 changes: 53 additions & 0 deletions packages/react-native/example-expo/app.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type { ConfigContext, ExpoConfig } from "expo/config"
export default function defineConfig({ config }: ConfigContext): ExpoConfig {
const apiKey = process.env.EXPO_PUBLIC_API_KEY ?? ""
return {
...config,
name: "expo-breez-sdk-liquid-example",
slug: "expo-breez-sdk-liquid-example",
version: "1.0.0",
orientation: "portrait",
icon: "./assets/icon.png",
userInterfaceStyle: "light",
newArchEnabled: true,
splash: {
image: "./assets/splash-icon.png",
resizeMode: "contain",
backgroundColor: "#ffffff"
},
ios: {
supportsTablet: true,
bundleIdentifier: "com.breeztech.expo-breez-sdk-liquid-example"
},
android: {
adaptiveIcon: {
foregroundImage: "./assets/adaptive-icon.png",
backgroundColor: "#ffffff"
}
},
web: {
favicon: "./assets/favicon.png"
},
extra: {
eas: {
build: {
experimental: {
ios: {
appExtensions: [
{
targetName: "BreezNotificationService",
bundleIdentifier: "com.breeztech.expo-breez-sdk-liquid-example.notificationextension",
entitlements: {
"com.apple.security.application-groups": ["group.com.breeztech.expo-breez-sdk-liquid-example"],
"keychain-access-groups": ["$(AppIdentifierPrefix)sharedkey"]
}
}
]
}
}
}
}
},
plugins: [["@breeztech/react-native-breez-sdk-liquid", { apiKey }]]
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions packages/react-native/example-expo/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { registerRootComponent } from "expo"

import App from "./App"

// registerRootComponent calls AppRegistry.registerComponent('main', () => App);
// It also ensures that whether you load the app in Expo Go or in a native build,
// the environment is set up appropriately
registerRootComponent(App)
Loading