Skip to content

Commit

Permalink
fix: fix eos transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-IS authored Jan 30, 2024
2 parents de3480a + 2cc79aa commit 93520f4
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 16 deletions.
2 changes: 0 additions & 2 deletions config/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export const ONBOARDING_LINK =
process.env.ONBOARDING_LINK || "https://hyphawallet.page.link/get-hypha-wallet";
export const AUTHENTICATOR_NAME = "Hypha Wallet";
export const BUTTON_BACKGROUND_COLOR = "#1D2946";
export const BUTTON_TEXT_COLOR = "white";
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hypha-dao/ual-hypha",
"version": "1.0.13",
"version": "1.0.14",
"description": "UAL implementation for Hypha Wallet",
"type": "module",
"source": "index.js",
Expand Down
4 changes: 2 additions & 2 deletions src/HyphaAuthenticator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import WebTransportLink from "./web-transport/index.js";
import ESRUtil from "./utils/esr.js";

import {
ONBOARDING_LINK,
AUTHENTICATOR_NAME,
BUTTON_BACKGROUND_COLOR,
BUTTON_TEXT_COLOR,
Expand Down Expand Up @@ -65,9 +64,10 @@ export class HyphaAuthenticator extends Authenticator {
/**
* Returns a URL where the user can download and install the underlying authenticator
* if it is not found by the UAL Authenticator.
* We return undefined - we do not want people to download the wallet other than with an onboarding link
*/
getOnboardingLink() {
return ONBOARDING_LINK;
return undefined;
}

/**
Expand Down
24 changes: 17 additions & 7 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import { v4 as uuidv4 } from "uuid";


export const poll = function (fn, data, interval) {
export const poll = function (fn, data, interval, totalTimeout) {
interval = interval || 100;
totalTimeout = totalTimeout || 120000;

const startTime = Date.now();

var checkCondition = async (resolve, reject) => {
var result = await fn(data);
if (result) {
resolve(result);
} else {
setTimeout(checkCondition, interval, resolve, reject);
try {
var result = await fn(data);
if (result) {
resolve(result);
} else {
if (Date.now() - startTime < totalTimeout) {
setTimeout(checkCondition, interval, resolve, reject);
} else {
reject(new Error("Timeout"));
}
}
} catch (error) {
reject(error);
}
};

Expand Down
34 changes: 30 additions & 4 deletions src/web-transport/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,29 @@ const checkLoginData = function (transaction, loginCode, loginContract) {
};

class WebTransportLink {
constructor(esrUtil, pollingInterval = 2000) {
constructor(
esrUtil,
pollingInterval = 1000,
transactionCheckInterval = 500,
pollTimeout = 10 * 60 * 1000, // 10 minutes
transactionCheckTimeout = 30 * 1000 // 30 seconds
) {
if (!esrUtil || !esrUtil.rpc) {
throw new Error("Invalid esrUtil or esrUtil.rpc not found " + esrUtil);
}

this.dialog = new Dialog();
this.esrUtil = esrUtil;
this.rpc = esrUtil.rpc;
this.pollingInterval = pollingInterval;
this.transactionCheckInterval = transactionCheckInterval;
this.pollTimeout = pollTimeout;
this.transactionCheckTimeout = transactionCheckTimeout;
this.login = this.login.bind(this);
this.restore = this.restore.bind(this);
this.logout = this.logout.bind(this);
this.checkTransactionId = this.checkTransactionId.bind(this);

}

getContractFromTransaction(transactions) {
Expand All @@ -45,6 +60,17 @@ class WebTransportLink {
const data = response.text();
return data;
} catch (e) {
//console.error("error polling: " + e)
return;
}
}

async checkTransactionId(txId) {
try {
const transactionInfo = await this.rpc.history_get_transaction(txId);
return transactionInfo;
} catch (e) {
//console.log("checkTransactionId error: " + e)
return;
}
}
Expand Down Expand Up @@ -74,7 +100,7 @@ class WebTransportLink {
throw new UALHyphaWalletError(
"No transaction has been passed to sign transaction"
);
const { pollingInterval } = this;
const { pollingInterval, transactionCheckInterval, pollTimeout, transactionCheckTimeout } = this;
const uid = getTransactionUID(transaction);
const callbackUrl = `${CALLBACK_HOST}/transaction?uid=${uid}&tx_id={{tx}}`;
const esr = await this.esrUtil.encodeESR(
Expand All @@ -100,9 +126,9 @@ class WebTransportLink {

await this.dialog.showDialog(dialog);

const txId = await poll(this.checkForConfirmation, uid, pollingInterval);
const txId = await poll(this.checkForConfirmation, uid, pollingInterval, pollTimeout);

const transactionInfo = await this.rpc.history_get_transaction(txId);
const transactionInfo = await poll(this.checkTransactionId, txId, transactionCheckInterval, transactionCheckTimeout);

this.dialog.hide();
return transactionInfo;
Expand Down

0 comments on commit 93520f4

Please sign in to comment.