Skip to content

Commit

Permalink
add getBalance and login/register with pin credential type
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiaferrari02 committed Mar 4, 2024
1 parent deb8e4f commit 45b5ace
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 7 deletions.
47 changes: 47 additions & 0 deletions cpp/GdkHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ std::vector<jsi::PropNameID> GdkHostObject::getPropertyNames(jsi::Runtime& rt) {
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("validateMnemonic")));
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("getTransactions")));
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("getUnspentOutputs")));
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("getBalance")));
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("refresh")));
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("getFeeEstimates")));
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("getPreviousAddresses")));
Expand Down Expand Up @@ -588,6 +589,52 @@ jsi::Value GdkHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& prop
return utils::makePromise(runtime, func);
});
}

if (propName == "getBalance") {
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_get_balance(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 == "refresh") {
return jsi::Function::createFromHostFunction(runtime,
Expand Down
7 changes: 7 additions & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ const App: React.FunctionComponent = () => {
console.log("ERROR", error)
}
}} />
<Button title="get balance" onPress={async() => {
try {
console.log(await gdk.getBalance({ subaccount: 1, num_confs: 0 }))
} catch (error) {
console.log("ERROR", error)
}
}} />
<Button title="get prev addresses" onPress={async() => {
try {
console.log(await gdk.getPreviousAddresses({ subaccount: 1 }))
Expand Down
7 changes: 4 additions & 3 deletions src/GdkNativeInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export interface GdkNativeInterface {
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>
register: (hw_device: object, details: GDK.Credentials | GDK.PinCredentials) => Promise<void>
login: (hw_device: object, details: GDK.Credentials | GDK.PinCredentials) => Promise<void>
getSubaccounts: (details: { refresh: boolean }) => Promise<{
subaccounts: GDK.Subaccount[]
}>
Expand All @@ -34,5 +34,6 @@ 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>
signPsbt: (details: GDK.PsbtSignDetails) => Promise<{ psbt: string }>
getBalance: (details: GDK.GetSubaccountReq) => Promise<{ [assetId: string]: number }>
}
10 changes: 6 additions & 4 deletions src/createGdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ export interface GdkInterface {
* @param hw_device - leave empty
* @param userAgent - credentials used to authenticate the user
*/
register: (hw_device: object, details: GDK.Credentials) => Promise<void>
register: (hw_device: object, details: GDK.Credentials | GDK.PinCredentials) => Promise<void>
/**
* Logs in a user to gdk
* @param hw_device - leave empty
* @param userAgent - credentials used to authenticate the user
*/
login: (hw_device: object, details: GDK.Credentials) => Promise<void>
login: (hw_device: object, details: GDK.Credentials | GDK.PinCredentials) => Promise<void>
/**
* Lists the user's subaccounts
* @param details - subaccount details containing `refresh` If set to true, subaccounts are re-discovered if appropriate for the session type. Note that this will take significantly more time if set
Expand Down Expand Up @@ -139,7 +139,8 @@ export interface GdkInterface {
* @returns the transaction hash
*/
broadcastTransaction: (txHex: string) => Promise<string>
signPsbt: (details: GDK.PsbtSignDetails) => Promise<object>
signPsbt: (details: GDK.PsbtSignDetails) => Promise<{ psbt: string }>
getBalance: (details: GDK.GetSubaccountReq) => Promise<{ [assetId: string]: number }>
}

declare global {
Expand Down Expand Up @@ -215,6 +216,7 @@ export const createGdk = (): GdkInterface => {
sendTransaction: gdk.sendTransaction,
broadcastTransaction: gdk.broadcastTransaction,
getNetworks: gdk.getNetworks,
signPsbt: gdk.signPsbt
signPsbt: gdk.signPsbt,
getBalance: gdk.getBalance
}
}
10 changes: 10 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ export type Credentials = {
mnemonic: string
password: string
}

export type PinCredentials = {
pin: string
pin_data: {
encrypted_data: string
pin_identifier: string
salt: string
}
}

export type SubaccountType = "p2sh-p2wpkh" | "p2wpkh" | "p2pkh" | "2of2_no_recovery" | "2of3"
export type AddressType = "csv" | "p2sh" | "p2wsh" | "p2pkh" | "p2sh-p2wpkh" | "p2wpkh"
export type CreateSubaccountDetails = {
Expand Down

0 comments on commit 45b5ace

Please sign in to comment.