use newer bank API

This commit is contained in:
Florian Dold 2020-03-09 14:17:06 +05:30
parent c5c308661e
commit 4999920373
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B

View File

@ -24,13 +24,16 @@
* Imports. * Imports.
*/ */
import Axios from "axios"; import Axios from "axios";
import querystring = require("querystring");
export interface BankUser { export interface BankUser {
username: string; username: string;
password: string; password: string;
} }
/**
* Generate a random alphanumeric ID. Does *not* use cryptographically
* secure randomness.
*/
function makeId(length: number): string { function makeId(length: number): string {
let result = ""; let result = "";
const characters = const characters =
@ -41,18 +44,25 @@ function makeId(length: number): string {
return result; return result;
} }
/**
* Helper function to generate the "Authorization" HTTP header.
*/
function makeAuth(username: string, password: string): string { function makeAuth(username: string, password: string): string {
const auth = `${username}:${password}`; const auth = `${username}:${password}`;
const authEncoded: string = Buffer.from(auth).toString("base64"); const authEncoded: string = Buffer.from(auth).toString("base64");
return `Basic ${authEncoded}`; return `Basic ${authEncoded}`;
} }
/**
* Client for the Taler bank access API.
*/
export class Bank { export class Bank {
constructor(private bankBaseUrl: string) {} constructor(private bankBaseUrl: string) {}
async generateWithdrawUri(bankUser: BankUser, amount: string): Promise<string> { async generateWithdrawUri(
bankUser: BankUser,
amount: string,
): Promise<string> {
const body = { const body = {
amount, amount,
}; };
@ -65,7 +75,7 @@ export class Bank {
data: body, data: body,
responseType: "json", responseType: "json",
headers: { headers: {
"Authorization": makeAuth(bankUser.username, bankUser.password), Authorization: makeAuth(bankUser.username, bankUser.password),
}, },
}); });
@ -86,14 +96,13 @@ export class Bank {
reservePub: string, reservePub: string,
exchangePaytoUri: string, exchangePaytoUri: string,
) { ) {
const reqUrl = new URL("api/withdraw-headless", this.bankBaseUrl).href; const reqUrl = new URL("testing/withdraw", this.bankBaseUrl).href;
const body = { const body = {
auth: { type: "basic" },
username: bankUser, username: bankUser,
amount, amount,
reserve_pub: reservePub, reserve_pub: reservePub,
exchange_wire_detail: exchangePaytoUri, exchange_payto_uri: exchangePaytoUri,
}; };
const resp = await Axios({ const resp = await Axios({
@ -102,7 +111,7 @@ export class Bank {
data: body, data: body,
responseType: "json", responseType: "json",
headers: { headers: {
"Authorization": makeAuth(bankUser.username, bankUser.password), Authorization: makeAuth(bankUser.username, bankUser.password),
}, },
}); });
@ -112,7 +121,7 @@ export class Bank {
} }
async registerRandomUser(): Promise<BankUser> { async registerRandomUser(): Promise<BankUser> {
const reqUrl = new URL("api/register", this.bankBaseUrl).href; const reqUrl = new URL("testing/register", this.bankBaseUrl).href;
const randId = makeId(8); const randId = makeId(8);
const bankUser: BankUser = { const bankUser: BankUser = {
username: `testuser-${randId}`, username: `testuser-${randId}`,
@ -122,7 +131,7 @@ export class Bank {
const resp = await Axios({ const resp = await Axios({
method: "post", method: "post",
url: reqUrl, url: reqUrl,
data: querystring.stringify(bankUser as any), data: bankUser,
responseType: "json", responseType: "json",
}); });