Skip to content

Commit

Permalink
WIP debug
Browse files Browse the repository at this point in the history
  • Loading branch information
escapedcat committed Jul 13, 2022
1 parent 7167385 commit 363a87c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 19 deletions.
7 changes: 7 additions & 0 deletions src/app/screens/connectors/NewWallet/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,12 @@ export default function NewWallet() {
};

try {
console.log("TRY");

const validation = await utils.call("validateAccount", account);
console.log("validation", validation);
if (validation.valid) {
console.log("validation - valid", validation.valid);
const addResult = await utils.call("addAccount", account);
if (addResult.accountId) {
await utils.call("selectAccount", {
Expand All @@ -105,17 +109,20 @@ export default function NewWallet() {
navigate("/test-connection");
}
} else {
console.log("validation - ELSE", validation);
console.error({ validation });
toast.error(
`${tCommon("errors.connection_failed")} (${validation.error})`
);
}
} catch (e) {
console.log("validation - CATCH", e);
console.error(e);
if (e instanceof Error) {
toast.error(`${tCommon("errors.connection_failed")} (${e.message})`);
}
} finally {
console.log("validation - FINALLY");
setLoading(false);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import connectors from "../../connectors";

const validateAccount = async (message, sender) => {
console.log("validateAccount - mess", message);

const account = message.args;
const connector = new connectors[account.connector](account.config);
await connector.init();

try {
console.log("validateAccount - TRY");
const info = await connector.getInfo();
await connector.unload(); // unload the connector again, we just checked if it works but have no persistence

return { data: { valid: true, info: info } };
} catch (e) {
console.log("validateAccount - CATCH", e);
console.error(e);
return { data: { valid: false, error: e.message } };
}
Expand Down
60 changes: 41 additions & 19 deletions src/extension/background-script/connectors/lndhub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export default class LndHub implements Connector {
}

async init() {
console.log("INIT?!");

return this.authorize();
}

Expand Down Expand Up @@ -286,30 +288,50 @@ export default class LndHub implements Connector {
}

async authorize() {
const { data: authData } = await axios.post(
`${this.config.url}/auth?type=auth`,
{
console.log("AUTHORIZE - config url: ", this.config.url);

const res = await fetch(`${this.config.url}/auth?type=auth`, {
method: "POST",
headers: defaultHeaders,
body: JSON.stringify({
login: this.config.login,
password: this.config.password,
},
{
headers: defaultHeaders,
}
);
}),
});

if (authData.error || authData.errors) {
const error = authData.error || authData.errors;
const errMessage = error?.errors?.[0]?.message || error?.[0]?.message;
console.log("RES: ", res);

console.error(errMessage);
throw new Error("API error: " + errMessage);
} else {
this.refresh_token = authData.refresh_token;
this.access_token = authData.access_token;
this.refresh_token_created = +new Date();
this.access_token_created = +new Date();
try {
const { data: authData } = await axios.post(
`${this.config.url}/auth?type=auth`,
{
login: this.config.login,
password: this.config.password,
},
{
headers: defaultHeaders,
}
);

console.log("AUTHORIZE - data", authData);

return authData;
if (authData.error || authData.errors) {
const error = authData.error || authData.errors;
const errMessage = error?.errors?.[0]?.message || error?.[0]?.message;

console.error(errMessage);
throw new Error("API error: " + errMessage);
} else {
this.refresh_token = authData.refresh_token;
this.access_token = authData.access_token;
this.refresh_token_created = +new Date();
this.access_token_created = +new Date();

return authData;
}
} catch (e) {
// https://stackoverflow.com/questions/72798574/axios-error-typeerror-adapter-is-not-a-function
console.log("AUTHORIZE - CATCH", e);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/extension/background-script/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import axios, { AxiosRequestConfig, Method } from "axios";
import browser, { Runtime, Tabs } from "webextension-polyfill";
import utils from "~/common/lib/utils";

Expand Down

0 comments on commit 363a87c

Please sign in to comment.