more android helpers

This commit is contained in:
Florian Dold 2019-08-20 23:36:56 +02:00
parent 106bc6ad9a
commit d76bc2a03d
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
4 changed files with 55 additions and 28 deletions

View File

@ -18,8 +18,9 @@
* Imports. * Imports.
*/ */
import { Wallet } from "../wallet"; import { Wallet } from "../wallet";
import { getDefaultNodeWallet, withdrawTestBalance } from "../headless/helpers"; import { getDefaultNodeWallet, withdrawTestBalance, DefaultNodeWalletArgs } from "../headless/helpers";
import { openPromise } from "../promiseUtils"; import { openPromise } from "../promiseUtils";
import fs = require("fs");
export function installAndroidWalletListener() { export function installAndroidWalletListener() {
// @ts-ignore // @ts-ignore
@ -31,7 +32,8 @@ export function installAndroidWalletListener() {
throw new Error(errMsg); throw new Error(errMsg);
} }
let maybeWallet: Wallet | undefined; let maybeWallet: Wallet | undefined;
const wp = openPromise<Wallet>(); let wp = openPromise<Wallet>();
let walletArgs: DefaultNodeWalletArgs | undefined;
const onMessage = async (msgStr: any) => { const onMessage = async (msgStr: any) => {
if (typeof msgStr !== "string") { if (typeof msgStr !== "string") {
console.error("expected string as message"); console.error("expected string as message");
@ -48,35 +50,54 @@ export function installAndroidWalletListener() {
const id = msg.id; const id = msg.id;
let result; let result;
switch (operation) { switch (operation) {
case "init": case "init": {
{ walletArgs = {
maybeWallet = await getDefaultNodeWallet({
notifyHandler: async () => { notifyHandler: async () => {
sendMessage(JSON.stringify({ type: "notification" })); sendMessage(JSON.stringify({ type: "notification" }));
}, },
}); persistentStoragePath: msg.args.persistentStoragePath,
};
maybeWallet = await getDefaultNodeWallet(walletArgs);
wp.resolve(maybeWallet); wp.resolve(maybeWallet);
result = true; result = true;
}
break; break;
case "getBalances": }
{ case "getBalances": {
const wallet = await wp.promise; const wallet = await wp.promise;
result = await wallet.getBalances(); result = await wallet.getBalances();
}
break; break;
case "withdrawTestkudos": }
{ case "withdrawTestkudos": {
const wallet = await wp.promise; const wallet = await wp.promise;
result = await withdrawTestBalance(wallet); result = await withdrawTestBalance(wallet);
}
break; break;
case "downloadProposal": }
{ case "preparePay": {
const wallet = await wp.promise; const wallet = await wp.promise;
result = wallet.downloadProposal(msg.args.url); result = await wallet.preparePay(msg.args.url);
}
break; break;
}
case "confirmPay": {
const wallet = await wp.promise;
result = await wallet.confirmPay(msg.args.proposalId, undefined);
break;
}
case "reset": {
const wallet = await wp.promise;
wallet.stop()
wp = openPromise<Wallet>();
if (walletArgs && walletArgs.persistentStoragePath) {
try {
fs.unlinkSync(walletArgs.persistentStoragePath)
} catch (e) {
console.error("Error while deleting the wallet db:", e);
}
// Prevent further storage!
walletArgs.persistentStoragePath = undefined;
}
maybeWallet = undefined;
break;
}
default: default:
console.error(`operation "${operation}" not understood`); console.error(`operation "${operation}" not understood`);
return; return;

View File

@ -106,7 +106,7 @@ export class NodeHttpLib implements HttpRequestLibrary {
} }
} }
interface DefaultNodeWalletArgs { export interface DefaultNodeWalletArgs {
/** /**
* Location of the wallet database. * Location of the wallet database.
* *
@ -155,6 +155,10 @@ export async function getDefaultNodeWallet(
} }
myBackend.afterCommitCallback = async () => { myBackend.afterCommitCallback = async () => {
// Allow caller to stop persisting the wallet.
if (args.persistentStoragePath === undefined) {
return;
}
const dbContent = myBackend.exportDump(); const dbContent = myBackend.exportDump();
fs.writeFileSync(storagePath, JSON.stringify(dbContent, undefined, 2), { encoding: "utf-8" }); fs.writeFileSync(storagePath, JSON.stringify(dbContent, undefined, 2), { encoding: "utf-8" });
}; };

View File

@ -762,6 +762,7 @@ export class Wallet {
status: "payment-possible", status: "payment-possible",
contractTerms: proposal.contractTerms, contractTerms: proposal.contractTerms,
proposalId: proposal.id!, proposalId: proposal.id!,
totalFees: checkResult.coinSelection!.totalFees,
}; };
} }
throw Error("not reached"); throw Error("not reached");

View File

@ -478,4 +478,5 @@ export interface PreparePayResult {
contractTerms?: ContractTerms; contractTerms?: ContractTerms;
error?: string; error?: string;
proposalId?: number; proposalId?: number;
totalFees?: AmountJson;
} }