Skip to content

Commit

Permalink
Merge pull request #226 from comit-network/lending-backup
Browse files Browse the repository at this point in the history
  • Loading branch information
bonomat authored Aug 12, 2021
2 parents 581554d + 933758c commit 638cd4e
Show file tree
Hide file tree
Showing 17 changed files with 580 additions and 169 deletions.
14 changes: 14 additions & 0 deletions e2e_tests/src/borrow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ describe("borrow test", () => {
debug("Signing loan");
let signLoanButton = await getElementById(driver, "//button[@data-cy='data-cy-sign-loan-button']", 20_000);
await signLoanButton.click();
debug("Download loan backup");
let downloadLoanButton = await getElementById(
driver,
"//button[@data-cy='data-cy-download-loan-button']",
20_000,
);
await downloadLoanButton.click();
debug("Confirming the backup");
let confirmLoanButton = await getElementById(
driver,
"//button[@data-cy='data-cy-confirm-loan-button']",
20_000,
);
await confirmLoanButton.click();

await switchToWindow(driver, webAppTitle);

Expand Down
2 changes: 2 additions & 0 deletions extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@
"@types/react": "^16.9.0",
"@types/react-dom": "^16.9.0",
"@types/uuid": "^8.3.1",
"chakra-ui-steps": "^1.3.0",
"debug": "^4.3.1",
"framer-motion": "^4.0.0",
"moment": "^2.29.1",
"react": "^17.0.2",
"react-async": "^10.0.1",
"react-browser-extension-scripts": "4.0.10",
"react-dom": "^17.0.2",
"react-icons": "^4.2.0",
"react-qr-code": "^1.1.1",
"react-scripts": "4.0.1",
"type-fest": "^2.0.0",
Expand Down
3 changes: 2 additions & 1 deletion extension/public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"storage",
"tabs",
"unlimitedStorage",
"webRequest"
"webRequest",
"downloads"
]
}
4 changes: 2 additions & 2 deletions extension/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { browser } from "webextension-polyfill-ts";
import { getBalances, getLoanToSign, getOpenLoans, getSwapToSign, getWalletStatus } from "./background-proxy";
import AddressQr from "./components/AddressQr";
import WalletBalances from "./components/Balances";
import ConfirmLoan from "./components/ConfirmLoan";
import ConfirmLoanWizard from "./components/ConfirmLoanWizard";
import ConfirmSwap from "./components/ConfirmSwap";
import CreateWallet from "./components/CreateWallet";
import OpenLoans from "./components/OpenLoans";
Expand Down Expand Up @@ -74,7 +74,7 @@ const App = () => {
swapToSign={swapToSign!}
/>}
{signLoan
&& <ConfirmLoan
&& <ConfirmLoanWizard
onCancel={refreshAll}
onSuccess={refreshAll}
loanToSign={loanToSign!}
Expand Down
28 changes: 26 additions & 2 deletions extension/src/background-proxy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { browser } from "webextension-polyfill-ts";
import { Address, BalanceUpdate, LoanDetails, LoanToSign, SwapToSign, Txid, WalletStatus } from "./models";
import {
Address,
BackupDetails,
BalanceUpdate,
LoanDetails,
LoanToSign,
SwapToSign,
Txid,
WalletStatus,
} from "./models";

const proxy = browser.extension.getBackgroundPage();

Expand All @@ -13,11 +22,26 @@ export async function signAndSendSwap(txHex: string): Promise<void> {
return proxy.signAndSendSwap(txHex);
}

export async function signLoan(): Promise<void> {
export async function signLoan(): Promise<string> {
// @ts-ignore
return proxy.signLoan();
}

export async function confirmLoan(payload: string): Promise<void> {
// @ts-ignore
return proxy.confirmLoan(payload);
}

export async function createLoanBackup(loanTx: string): Promise<string> {
// @ts-ignore
return proxy.createLoanBackup(loanTx);
}

export async function loadLoanBackup(backupDetails: BackupDetails): Promise<void> {
// @ts-ignore
return proxy.loadLoanBackup(backupDetails);
}

export async function getLoanToSign(): Promise<LoanToSign | undefined> {
// @ts-ignore
return proxy.getLoanToSign();
Expand Down
33 changes: 28 additions & 5 deletions extension/src/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import Debug from "debug";
import { browser } from "webextension-polyfill-ts";
import WavesProvider from "../in-page";
import { LoanDetails, LoanToSign, SwapToSign, Txid } from "../models";
import { BackupDetails, LoanDetails, LoanToSign, SwapToSign, Txid } from "../models";
import {
bip39SeedWords,
createLoanBackup,
createNewBip39Wallet,
extractLoan,
extractTrade,
Expand All @@ -12,6 +13,7 @@ import {
getBlockHeight,
getOpenLoans,
getPastTransactions,
loadLoanBackup,
makeBuyCreateSwapPayload,
makeLoanRequestPayload,
makeSellCreateSwapPayload,
Expand Down Expand Up @@ -171,11 +173,32 @@ window.signLoan = async () => {
// on the pop-up matches what is stored in the extension's
// storage. It would be better to send around the swap ID to check
// that the wallet is signing the same transaction the user has authorised
signLoan(walletName)
.then(resolveLoanSignRequest)
.catch(rejectLoanSignRequest)
.then(cleanupPendingLoan);

// if we receive an error, we respond directly, else we return the details
return await signLoan(walletName).catch(rejectLoanSignRequest);
};

// @ts-ignore
window.confirmLoan = async (payload: string) => {
if (!resolveLoanSignRequest || !rejectLoanSignRequest) {
throw new Error("No pending promise functions for loan sign request");
}
// once sent to the page, we assume the business is done.
// TODO: a feedback loop is required where the wallet gets told if bobtimus successfully published the transaction
resolveLoanSignRequest(payload);
await cleanupPendingLoan();
};

// @ts-ignore
window.createLoanBackup = async (loanTx: string) => {
return createLoanBackup(walletName, loanTx);
};

// @ts-ignore
window.loadLoanBackup = async (backupDetails: BackupDetails) => {
return loadLoanBackup(backupDetails);
};

// @ts-ignore
window.rejectLoan = () => {
if (!resolveLoanSignRequest || !rejectLoanSignRequest) {
Expand Down
112 changes: 0 additions & 112 deletions extension/src/components/ConfirmLoan.tsx

This file was deleted.

Loading

0 comments on commit 638cd4e

Please sign in to comment.