Skip to content

Commit

Permalink
fix package, add signPsbt, add getNetworks
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiaferrari02 committed Feb 20, 2024
1 parent 788096a commit f169da1
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 6 deletions.
86 changes: 86 additions & 0 deletions cpp/GdkHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ std::vector<jsi::PropNameID> GdkHostObject::getPropertyNames(jsi::Runtime& rt) {
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("signTransaction")));
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("sendTransaction")));
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("broadcastTransaction")));
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("getNetworks")));
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("signPsbt")));
return result;
}

Expand Down Expand Up @@ -116,6 +118,44 @@ jsi::Value GdkHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& prop
return jsi::Value::undefined();
});
}

if (propName == "getNetworks") {
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 {

utils::Promised func = [=](jsi::Runtime& rt, std::shared_ptr<utils::Promise> p){

auto task = [=, &rt](){
try {
GA_json *networks;
utils::wrapCall(GA_get_networks(&networks));

std::shared_ptr<react::CallInvoker> c = invoker.lock();
c->invokeAsync([=, &rt] {
jsi::Value res = utils::GAJsonToObject(rt, networks);

GA_destroy_json(networks);
p->resolve(res);
});
} catch (utils::Exception e) {
std::shared_ptr<react::CallInvoker> c = invoker.lock();
c->invokeAsync([=] { p->reject(e.what()); });
}

};

pool->queueWork(task);

};

return utils::makePromise(runtime, func);
});
}

if (propName == "connect") {
return jsi::Function::createFromHostFunction(runtime,
Expand Down Expand Up @@ -981,6 +1021,52 @@ jsi::Value GdkHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& prop
});
}

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

GA_json *details;
utils::jsiValueJsonToGAJson(runtime, arguments[0].getObject(runtime), &details);


utils::Promised func = [=](jsi::Runtime& rt, std::shared_ptr<utils::Promise> p){

auto task = [=, &rt](){
try {
GA_auth_handler *call;
utils::wrapCall(GA_psbt_sign(session, details, &call));
json res = utils::resolve(call);
GA_destroy_json(details);

std::shared_ptr<react::CallInvoker> c = invoker.lock();
c->invokeAsync([=, &rt] {
if (res.contains("result")) {
p->resolve(utils::parse(rt, res["result"].dump()));
} else {
p->reject(res["error"].dump());
}
});

} catch (utils::Exception e) {
GA_destroy_json(details);
std::shared_ptr<react::CallInvoker> c = invoker.lock();
c->invokeAsync([=] { p->reject(e.what()); });
}
};

pool->queueWork(task);

};

return utils::makePromise(runtime, func);
});
}

if (propName == "broadcastTransaction") {
return jsi::Function::createFromHostFunction(runtime,
jsi::PropNameID::forAscii(runtime, funcName),
Expand Down
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -943,7 +943,7 @@ PODS:
- React-Mapbuffer (0.73.3):
- glog
- React-debug
- react-native-gdk (0.0.1):
- react-native-gdk (0.0.3):
- React
- React-callinvoker
- React-Core
Expand Down Expand Up @@ -1402,7 +1402,7 @@ SPEC CHECKSUMS:
React-jsitracing: 18b04aa81d7e36ce4d60db121ce0567f9ac4b5ea
React-logger: cb0dd15ac67b00e7b771ef15203edcb29d4a3f8e
React-Mapbuffer: d59258be3b0d2280c6ba8964ab6e36ec69211871
react-native-gdk: d354336ec13ce1ffb5b9edd0bbbb2f61348658be
react-native-gdk: c32c6f2b309d9deefb236955e7721f8b36e7d017
React-nativeconfig: 4d3076dc3dc498ec49819e4e4225b55d3507f902
React-NativeModulesApple: 46f14baf36010b22ffd84fd89d5586e4148edfb3
React-perflogger: 27ccacf853ba725524ef2b4e444f14e34d0837b0
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
"main": "lib/commonjs/index",
"module": "lib/module/index",
"types": "lib/typescript/src/index.d.ts",
"react-native": "lib/commonjs/index",
"react-native": "src/index",
"source": "src/index",
"files": [
"src",
"lib",
"android",
"ios",
Expand Down
2 changes: 2 additions & 0 deletions src/GdkNativeInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface GdkNativeInterface {
generateMnemonic12: () => string
init: (log_level: "debug" | "none") => void
createSession: () => void
getNetworks: () => Promise<GDK.GetNetworksRes>
connect: (name: GDK.Network, userAgent: string) => void
register: (hw_device: object, details: GDK.Credentials) => Promise<void>
login: (hw_device: object, details: GDK.Credentials) => Promise<void>
Expand Down Expand Up @@ -33,4 +34,5 @@ export interface GdkNativeInterface {
signTransaction: (details: GDK.BlindedTransaction | GDK.UnsignedTransaction) => Promise<GDK.SignedBlindedTransaction | GDK.SignedTransaction>
sendTransaction: (details: GDK.SignedBlindedTransaction | GDK.SignedTransaction) => Promise<GDK.SignedBlindedTransaction | GDK.SignedTransaction>
broadcastTransaction: (txHex: string) => Promise<string>
signPsbt: (details: GDK.PsbtSignDetails) => Promise<object>
}
6 changes: 5 additions & 1 deletion src/createGdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface GdkInterface {
* creates a new session with gdk, this should be called before any other gdk method
*/
createSession: () => void
getNetworks: () => Promise<GDK.GetNetworksRes>
/**
* connects to the network
* This called right after init and before any other gdk method
Expand Down Expand Up @@ -138,6 +139,7 @@ export interface GdkInterface {
* @returns the transaction hash
*/
broadcastTransaction: (txHex: string) => Promise<string>
signPsbt: (details: GDK.PsbtSignDetails) => Promise<object>
}

declare global {
Expand Down Expand Up @@ -217,6 +219,8 @@ export const createGdk = (): GdkInterface => {
blindTransaction: gdk.blindTransaction,
signTransaction: gdk.signTransaction,
sendTransaction: gdk.sendTransaction,
broadcastTransaction: gdk.broadcastTransaction
broadcastTransaction: gdk.broadcastTransaction,
getNetworks: gdk.getNetworks,
signPsbt: gdk.signPsbt
}
}
23 changes: 22 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
export type Network = "electrum-testnet-liquid" | "electrum-liduid"
export type Network =
"mainnet" |
"liquid" |
"testnet" |
"testnet-liquid" |
"electrum-liquid" |
"electrum-mainnet" |
"electrum-testnet" |
"electrum-testnet-liquid"

export type Credentials = {
mnemonic: string
password: string
Expand All @@ -10,6 +19,10 @@ export type CreateSubaccountDetails = {
type: SubaccountType
}

export type GetNetworksRes = {
all_networks: Network[]
}

export type Events = {
network: {
event: "network"
Expand Down Expand Up @@ -351,3 +364,11 @@ export type SignedTransaction = UnsignedTransaction & {
export type SignedBlindedTransaction = BlindedTransaction & {
txhash: string
}

export type PsbtSignDetails = {
psbt: string
// eslint-disable-next-line @typescript-eslint/ban-types
utxos: unknown[]
// eslint-disable-next-line @typescript-eslint/ban-types
blinding_nonces?: unknown[]
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"rootDir": ".",
"paths": {
"react-native-gdk": ["./src/index"]
"@vulpemventures/react-native-gdk": ["./src/index"]
},
"allowUnreachableCode": false,
"allowUnusedLabels": false,
Expand Down

0 comments on commit f169da1

Please sign in to comment.