android helpers

This commit is contained in:
Florian Dold 2019-08-19 20:24:29 +02:00
parent 08b490783b
commit 5fa70b1eb3
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
2 changed files with 52 additions and 24 deletions

View File

@ -18,28 +18,26 @@
* Imports. * Imports.
*/ */
import { Wallet } from "../wallet"; import { Wallet } from "../wallet";
import { getDefaultNodeWallet } from "../headless/helpers"; import { getDefaultNodeWallet, withdrawTestBalance } from "../headless/helpers";
import { openPromise } from "../promiseUtils";
class AndroidWalletHelper {
walletPromise: Promise<Wallet> | undefined;
constructor() {}
async init() {
this.walletPromise = getDefaultNodeWallet();
}
}
export function installAndroidWalletListener() { export function installAndroidWalletListener() {
// @ts-ignore // @ts-ignore
const sendMessage: (m: any) => void = global.__akono_sendMessage; const sendMessage: (m: string) => void = global.__akono_sendMessage;
if (typeof sendMessage !== "function") { if (typeof sendMessage !== "function") {
const errMsg = const errMsg =
"FATAL: cannot install android wallet listener: akono functions missing"; "FATAL: cannot install android wallet listener: akono functions missing";
console.error(errMsg); console.error(errMsg);
throw new Error(errMsg); throw new Error(errMsg);
} }
const walletHelper = new AndroidWalletHelper(); let maybeWallet: Wallet | undefined;
const onMessage = (msg: any) => { const wp = openPromise<Wallet>();
const onMessage = async (msgStr: any) => {
if (typeof msgStr !== "string") {
console.error("expected string as message");
return;
}
const msg = JSON.parse(msgStr);
const operation = msg.operation; const operation = msg.operation;
if (typeof operation !== "string") { if (typeof operation !== "string") {
console.error( console.error(
@ -51,21 +49,45 @@ export function installAndroidWalletListener() {
let result; let result;
switch (operation) { switch (operation) {
case "init": case "init":
result = walletHelper.init(); {
maybeWallet = await getDefaultNodeWallet({
notifyHandler: async () => {
sendMessage(JSON.stringify({ type: "notification" }));
},
});
wp.resolve(maybeWallet);
result = true;
}
break; break;
case "getBalances": case "getBalances":
{
const wallet = await wp.promise;
result = await wallet.getBalances();
}
break; break;
case "withdraw-testkudos": case "withdrawTestkudos":
{
const wallet = await wp.promise;
result = await withdrawTestBalance(wallet);
}
break;
case "downloadProposal":
{
const wallet = await wp.promise;
result = wallet.downloadProposal(msg.args.url);
}
break; break;
default: default:
console.error(`operation "${operation}" not understood`); console.error(`operation "${operation}" not understood`);
return; return;
} }
const respMsg = { result, id }; const respMsg = { result, id, operation, type: "response" };
console.log("sending message back", respMsg); console.log("sending message back", respMsg);
sendMessage(respMsg); sendMessage(JSON.stringify(respMsg));
}; };
// @ts-ignore // @ts-ignore
globalThis.__akono_onMessage = onMessage; globalThis.__akono_onMessage = onMessage;
console.log("android wallet listener installed");
} }

View File

@ -36,12 +36,6 @@ import fs = require("fs");
const enableTracing = false; const enableTracing = false;
class ConsoleNotifier implements Notifier {
notify(): void {
// nothing to do.
}
}
class ConsoleBadge implements Badge { class ConsoleBadge implements Badge {
startBusy(): void { startBusy(): void {
enableTracing && console.log("NOTIFICATION: busy"); enableTracing && console.log("NOTIFICATION: busy");
@ -120,6 +114,12 @@ interface DefaultNodeWalletArgs {
* the wallet database is stored only in memory. * the wallet database is stored only in memory.
*/ */
persistentStoragePath?: string; persistentStoragePath?: string;
/**
* Handler for asynchronous notifications from the wallet.
*/
notifyHandler?: (reason: string) => void;
} }
/** /**
@ -128,7 +128,13 @@ interface DefaultNodeWalletArgs {
export async function getDefaultNodeWallet( export async function getDefaultNodeWallet(
args: DefaultNodeWalletArgs = {}, args: DefaultNodeWalletArgs = {},
): Promise<Wallet> { ): Promise<Wallet> {
const myNotifier = new ConsoleNotifier(); const myNotifier: Notifier = {
notify() {
if (args.notifyHandler) {
args.notifyHandler("");
}
}
}
const myBadge = new ConsoleBadge(); const myBadge = new ConsoleBadge();