Skip to content

Commit

Permalink
Add destroy session (#6)
Browse files Browse the repository at this point in the history
add destroy session
  • Loading branch information
mattiaferrari02 authored Oct 24, 2024
1 parent e765162 commit ad1c12f
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 112 deletions.
38 changes: 35 additions & 3 deletions cpp/GdkHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ using json = nlohmann::json;
GdkHostObject::GdkHostObject(std::string dirUrl, jsi::Runtime &runtime, std::shared_ptr<react::CallInvoker> jsCallInvoker): invoker(jsCallInvoker), rt(runtime) {
sessionDirectoryUrl = dirUrl;
pool = std::make_shared<ThreadPool>();

session = NULL;
}

GdkHostObject::~GdkHostObject() {

if(session != nullptr) {
GA_set_notification_handler(session, nullptr, nullptr);
GA_destroy_session(session);
}

}


Expand Down Expand Up @@ -51,6 +55,7 @@ std::vector<jsi::PropNameID> GdkHostObject::getPropertyNames(jsi::Runtime& rt) {
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("getNetworks")));
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("signPsbt")));
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("registerNetwork")));
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("destroySession")));
return result;
}

Expand All @@ -59,6 +64,25 @@ jsi::Value GdkHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& prop
auto propName = propNameId.utf8(runtime);
auto funcName = "gdk." + propName;

if (propName == "destroySession") {
return jsi::Function::createFromHostFunction(runtime,
jsi::PropNameID::forAscii(runtime, funcName),
0,
[this](jsi::Runtime& runtime,
const jsi::Value& thisValue,
const jsi::Value* arguments,
size_t count) -> jsi::Value {

GA_set_notification_handler(session, nullptr, nullptr);
utils::wrapCall(GA_destroy_session(session));

session = NULL;


return jsi::Value::undefined();
});
}

if (propName == "generateMnemonic12") {
return jsi::Function::createFromHostFunction(runtime,
jsi::PropNameID::forAscii(runtime, funcName),
Expand Down Expand Up @@ -112,8 +136,16 @@ jsi::Value GdkHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& prop
const jsi::Value& thisValue,
const jsi::Value* arguments,
size_t count) -> jsi::Value {

utils::wrapCall(GA_create_session(&session));
if (session != NULL) {
throw new jsi::JSError(runtime, "Cannot recreate session. Call destroy before");
}

GA_session* newSession;


utils::wrapCall(GA_create_session(&newSession));

session = newSession;

utils::wrapCall(GA_set_notification_handler(session, utils::notificationsHandler, this));

Expand Down
8 changes: 8 additions & 0 deletions example/ios/GdkExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,10 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = 5N4S52Y7KS;
ENABLE_BITCODE = NO;
INFOPLIST_FILE = GdkExample/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
Expand All @@ -480,6 +483,7 @@
);
PRODUCT_BUNDLE_IDENTIFIER = "org.reactjs.native.example.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = GdkExample;
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
VERSIONING_SYSTEM = "apple-generic";
Expand All @@ -492,7 +496,10 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = 5N4S52Y7KS;
INFOPLIST_FILE = GdkExample/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
Expand All @@ -506,6 +513,7 @@
);
PRODUCT_BUNDLE_IDENTIFIER = "org.reactjs.native.example.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = GdkExample;
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_VERSION = 5.0;
VERSIONING_SYSTEM = "apple-generic";
};
Expand Down
2 changes: 1 addition & 1 deletion example/ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ prepare_react_native_project!
# dependencies: {
# ...(process.env.NO_FLIPPER ? { 'react-native-flipper': { platforms: { ios: null } } } : {}),
# ```
flipper_config = ENV['NO_FLIPPER'] == "1" ? FlipperConfiguration.disabled : FlipperConfiguration.enabled
flipper_config = FlipperConfiguration.disabled

linkage = ENV['USE_FRAMEWORKS']
if linkage != nil
Expand Down
112 changes: 6 additions & 106 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,71 +1,13 @@
PODS:
- boost (1.83.0)
- CocoaAsyncSocket (7.6.5)
- DoubleConversion (1.1.6)
- FBLazyVector (0.73.3)
- Flipper (0.201.0):
- Flipper-Folly (~> 2.6)
- Flipper-Boost-iOSX (1.76.0.1.11)
- Flipper-DoubleConversion (3.2.0.1)
- Flipper-Fmt (7.1.7)
- Flipper-Folly (2.6.10):
- Flipper-Boost-iOSX
- Flipper-DoubleConversion
- Flipper-Fmt (= 7.1.7)
- Flipper-Glog
- libevent (~> 2.1.12)
- OpenSSL-Universal (= 1.1.1100)
- Flipper-Glog (0.5.0.5)
- Flipper-PeerTalk (0.0.4)
- FlipperKit (0.201.0):
- FlipperKit/Core (= 0.201.0)
- FlipperKit/Core (0.201.0):
- Flipper (~> 0.201.0)
- FlipperKit/CppBridge
- FlipperKit/FBCxxFollyDynamicConvert
- FlipperKit/FBDefines
- FlipperKit/FKPortForwarding
- SocketRocket (~> 0.6.0)
- FlipperKit/CppBridge (0.201.0):
- Flipper (~> 0.201.0)
- FlipperKit/FBCxxFollyDynamicConvert (0.201.0):
- Flipper-Folly (~> 2.6)
- FlipperKit/FBDefines (0.201.0)
- FlipperKit/FKPortForwarding (0.201.0):
- CocoaAsyncSocket (~> 7.6)
- Flipper-PeerTalk (~> 0.0.4)
- FlipperKit/FlipperKitHighlightOverlay (0.201.0)
- FlipperKit/FlipperKitLayoutHelpers (0.201.0):
- FlipperKit/Core
- FlipperKit/FlipperKitHighlightOverlay
- FlipperKit/FlipperKitLayoutTextSearchable
- FlipperKit/FlipperKitLayoutIOSDescriptors (0.201.0):
- FlipperKit/Core
- FlipperKit/FlipperKitHighlightOverlay
- FlipperKit/FlipperKitLayoutHelpers
- FlipperKit/FlipperKitLayoutPlugin (0.201.0):
- FlipperKit/Core
- FlipperKit/FlipperKitHighlightOverlay
- FlipperKit/FlipperKitLayoutHelpers
- FlipperKit/FlipperKitLayoutIOSDescriptors
- FlipperKit/FlipperKitLayoutTextSearchable
- FlipperKit/FlipperKitLayoutTextSearchable (0.201.0)
- FlipperKit/FlipperKitNetworkPlugin (0.201.0):
- FlipperKit/Core
- FlipperKit/FlipperKitReactPlugin (0.201.0):
- FlipperKit/Core
- FlipperKit/FlipperKitUserDefaultsPlugin (0.201.0):
- FlipperKit/Core
- FlipperKit/SKIOSNetworkPlugin (0.201.0):
- FlipperKit/Core
- FlipperKit/FlipperKitNetworkPlugin
- fmt (6.2.1)
- glog (0.3.5)
- hermes-engine (0.73.3):
- hermes-engine/Pre-built (= 0.73.3)
- hermes-engine/Pre-built (0.73.3)
- libevent (2.1.12)
- OpenSSL-Universal (1.1.1100)
- RCT-Folly (2022.05.16.00):
- boost
- DoubleConversion
Expand Down Expand Up @@ -943,7 +885,7 @@ PODS:
- React-Mapbuffer (0.73.3):
- glog
- React-debug
- react-native-gdk (0.0.3):
- react-native-gdk (0.0.6):
- React
- React-callinvoker
- React-Core
Expand Down Expand Up @@ -1164,30 +1106,9 @@ DEPENDENCIES:
- boost (from `../node_modules/react-native/third-party-podspecs/boost.podspec`)
- DoubleConversion (from `../node_modules/react-native/third-party-podspecs/DoubleConversion.podspec`)
- FBLazyVector (from `../node_modules/react-native/Libraries/FBLazyVector`)
- Flipper (= 0.201.0)
- Flipper-Boost-iOSX (= 1.76.0.1.11)
- Flipper-DoubleConversion (= 3.2.0.1)
- Flipper-Fmt (= 7.1.7)
- Flipper-Folly (= 2.6.10)
- Flipper-Glog (= 0.5.0.5)
- Flipper-PeerTalk (= 0.0.4)
- FlipperKit (= 0.201.0)
- FlipperKit/Core (= 0.201.0)
- FlipperKit/CppBridge (= 0.201.0)
- FlipperKit/FBCxxFollyDynamicConvert (= 0.201.0)
- FlipperKit/FBDefines (= 0.201.0)
- FlipperKit/FKPortForwarding (= 0.201.0)
- FlipperKit/FlipperKitHighlightOverlay (= 0.201.0)
- FlipperKit/FlipperKitLayoutPlugin (= 0.201.0)
- FlipperKit/FlipperKitLayoutTextSearchable (= 0.201.0)
- FlipperKit/FlipperKitNetworkPlugin (= 0.201.0)
- FlipperKit/FlipperKitReactPlugin (= 0.201.0)
- FlipperKit/FlipperKitUserDefaultsPlugin (= 0.201.0)
- FlipperKit/SKIOSNetworkPlugin (= 0.201.0)
- glog (from `../node_modules/react-native/third-party-podspecs/glog.podspec`)
- hermes-engine (from `../node_modules/react-native/sdks/hermes-engine/hermes-engine.podspec`)
- libevent (~> 2.1.12)
- OpenSSL-Universal (= 1.1.1100)
- RCT-Folly (from `../node_modules/react-native/third-party-podspecs/RCT-Folly.podspec`)
- RCT-Folly/Fabric (from `../node_modules/react-native/third-party-podspecs/RCT-Folly.podspec`)
- RCTRequired (from `../node_modules/react-native/Libraries/RCTRequired`)
Expand All @@ -1196,7 +1117,6 @@ DEPENDENCIES:
- React-callinvoker (from `../node_modules/react-native/ReactCommon/callinvoker`)
- React-Codegen (from `build/generated/ios`)
- React-Core (from `../node_modules/react-native/`)
- React-Core/DevSupport (from `../node_modules/react-native/`)
- React-Core/RCTWebSocket (from `../node_modules/react-native/`)
- React-CoreModules (from `../node_modules/react-native/React/CoreModules`)
- React-cxxreact (from `../node_modules/react-native/ReactCommon/cxxreact`)
Expand Down Expand Up @@ -1241,18 +1161,8 @@ DEPENDENCIES:

SPEC REPOS:
trunk:
- CocoaAsyncSocket
- Flipper
- Flipper-Boost-iOSX
- Flipper-DoubleConversion
- Flipper-Fmt
- Flipper-Folly
- Flipper-Glog
- Flipper-PeerTalk
- FlipperKit
- fmt
- libevent
- OpenSSL-Universal
- SocketRocket

EXTERNAL SOURCES:
Expand Down Expand Up @@ -1364,22 +1274,12 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
boost: d3f49c53809116a5d38da093a8aa78bf551aed09
CocoaAsyncSocket: 065fd1e645c7abab64f7a6a2007a48038fdc6a99
DoubleConversion: fea03f2699887d960129cc54bba7e52542b6f953
FBLazyVector: 70590b4f9e8ae9b0ce076efacea3abd7bc585ace
Flipper: c7a0093234c4bdd456e363f2f19b2e4b27652d44
Flipper-Boost-iOSX: fd1e2b8cbef7e662a122412d7ac5f5bea715403c
Flipper-DoubleConversion: 2dc99b02f658daf147069aad9dbd29d8feb06d30
Flipper-Fmt: 60cbdd92fc254826e61d669a5d87ef7015396a9b
Flipper-Folly: 584845625005ff068a6ebf41f857f468decd26b3
Flipper-Glog: 70c50ce58ddaf67dc35180db05f191692570f446
Flipper-PeerTalk: 116d8f857dc6ef55c7a5a75ea3ceaafe878aadc9
FlipperKit: 37525a5d056ef9b93d1578e04bc3ea1de940094f
fmt: ff9d55029c625d3757ed641535fd4a75fedc7ce9
glog: 035f1e36e53b355cf70f6434d161b36e7d21fecd
glog: c5d68082e772fa1c511173d6b30a9de2c05a69a2
hermes-engine: 5420539d016f368cd27e008f65f777abd6098c56
libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913
OpenSSL-Universal: ebc357f1e6bc71fa463ccb2fe676756aff50e88c
RCT-Folly: 7169b2b1c44399c76a47b5deaaba715eeeb476c0
RCTRequired: 9b898847f76977a6dfed2a08f4c5ed37add75ec0
RCTTypeSafety: 0debdc4ba38c8138016d8d8ada4bdf9ec1b8aa82
Expand All @@ -1402,7 +1302,7 @@ SPEC CHECKSUMS:
React-jsitracing: 18b04aa81d7e36ce4d60db121ce0567f9ac4b5ea
React-logger: cb0dd15ac67b00e7b771ef15203edcb29d4a3f8e
React-Mapbuffer: d59258be3b0d2280c6ba8964ab6e36ec69211871
react-native-gdk: c32c6f2b309d9deefb236955e7721f8b36e7d017
react-native-gdk: 07f19b2ab7ff72ac28a2a2db21e64266f4ab2cf0
React-nativeconfig: 4d3076dc3dc498ec49819e4e4225b55d3507f902
React-NativeModulesApple: 46f14baf36010b22ffd84fd89d5586e4148edfb3
React-perflogger: 27ccacf853ba725524ef2b4e444f14e34d0837b0
Expand All @@ -1427,8 +1327,8 @@ SPEC CHECKSUMS:
React-utils: b22b4a51aa578b3aac1e7c19501c0b9ba358ed79
ReactCommon: e708b8be8cb317b83e31c6ccfeda8bf6c0d1a2b3
SocketRocket: f32cd54efbe0f095c4d7594881e52619cfe80b17
Yoga: ff0382b894475dba0b4d2a5fda860bfee5a9afad
Yoga: 08cd7601462818c4985bda7b205b9e6a92a7e66a

PODFILE CHECKSUM: c3bf70c3e64beb3e9646a17bb26321a569363c56
PODFILE CHECKSUM: c8067a7f65bb9bd09ea4c1d92497439e59390a44

COCOAPODS: 1.12.0
COCOAPODS: 1.15.2
15 changes: 14 additions & 1 deletion example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const gdk = Gdk()


const App: React.FunctionComponent = () => {
const [mnemonic] = React.useState(gdk.generateMnemonic12())
const [mnemonic, setMnemonic] = React.useState(gdk.generateMnemonic12())
const [connected, setConnected] = React.useState(false)
const called = React.useRef(false)

Expand Down Expand Up @@ -50,6 +50,12 @@ const App: React.FunctionComponent = () => {
<ScrollView>
<Text>mnemonic: {mnemonic}</Text>
<Text>Connected: {connected ? "yes" : "no"}</Text>
<Button title="createSession" onPress={() => {
gdk.createSession()
}} />
<Button title="gen mnmemonic" onPress={() => {
setMnemonic(gdk.generateMnemonic12())
}} />
<Button title="connect" onPress={() => {
gdk.connect("electrum-testnet-liquid", "test-app")
}} />
Expand All @@ -60,6 +66,13 @@ const App: React.FunctionComponent = () => {
console.log("ERROR", error)
}
}} />
<Button title="destroy session" onPress={async() => {
try {
console.log(gdk.destroySession())
} catch (error) {
console.log("ERROR", error)
}
}} />
<Button title="login" onPress={async() => {
try {
console.log(await gdk.login({}, { mnemonic, password: "" }))
Expand Down
1 change: 1 addition & 0 deletions src/GdkNativeInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ export interface GdkNativeInterface {
signPsbt: (details: GDK.PsbtSignDetails) => Promise<{ psbt: string }>
getBalance: (details: GDK.GetSubaccountReq) => Promise<{ [assetId: string]: number }>
registerNetwork: (name: string, network: GDK.NetworkDetails) => void
destroySession: () => void
}
4 changes: 3 additions & 1 deletion src/createGdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export interface GdkInterface {
signPsbt: (details: GDK.PsbtSignDetails) => Promise<{ psbt: string }>
getBalance: (details: GDK.GetSubaccountReq) => Promise<{ [assetId: string]: number }>
registerNetwork: (name: string, network: GDK.NetworkDetails) => void
destroySession: () => void
}

declare global {
Expand Down Expand Up @@ -219,6 +220,7 @@ export const createGdk = (): GdkInterface => {
getNetworks: gdk.getNetworks,
signPsbt: gdk.signPsbt,
getBalance: gdk.getBalance,
registerNetwork: gdk.registerNetwork
registerNetwork: gdk.registerNetwork,
destroySession: gdk.destroySession
}
}

0 comments on commit ad1c12f

Please sign in to comment.