2022-03-14 18:31:30 +01:00
|
|
|
/*
|
|
|
|
This file is part of GNU Taler
|
|
|
|
(C) 2022 Taler Systems S.A.
|
|
|
|
|
|
|
|
GNU Taler is free software; you can redistribute it and/or modify it under the
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation; either version 3, or (at your option) any later version.
|
|
|
|
|
|
|
|
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Client for the Taler (demo-)bank.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Imports.
|
|
|
|
*/
|
|
|
|
import {
|
|
|
|
AmountString,
|
2022-11-11 20:52:45 +01:00
|
|
|
base64FromArrayBuffer,
|
2022-03-14 18:31:30 +01:00
|
|
|
buildCodecForObject,
|
|
|
|
Codec,
|
2022-03-18 15:32:41 +01:00
|
|
|
codecForAny,
|
2022-03-14 18:31:30 +01:00
|
|
|
codecForString,
|
|
|
|
encodeCrock,
|
2023-05-11 13:04:09 +02:00
|
|
|
generateIban,
|
2022-03-14 18:31:30 +01:00
|
|
|
getRandomBytes,
|
2022-03-15 17:51:05 +01:00
|
|
|
j2s,
|
|
|
|
Logger,
|
2022-11-11 20:52:45 +01:00
|
|
|
stringToBytes,
|
2023-02-15 23:32:42 +01:00
|
|
|
TalerError,
|
2022-03-22 21:16:38 +01:00
|
|
|
TalerErrorCode,
|
2022-03-14 18:31:30 +01:00
|
|
|
} from "@gnu-taler/taler-util";
|
2023-02-20 16:37:05 +01:00
|
|
|
import {
|
2023-04-22 22:17:08 +02:00
|
|
|
checkSuccessResponseOrThrow,
|
2023-02-23 00:52:10 +01:00
|
|
|
createPlatformHttpLib,
|
2023-02-20 16:37:05 +01:00
|
|
|
HttpRequestLibrary,
|
|
|
|
readSuccessResponseJsonOrThrow,
|
|
|
|
} from "@gnu-taler/taler-util/http";
|
2022-03-14 18:31:30 +01:00
|
|
|
|
2022-03-15 17:51:05 +01:00
|
|
|
const logger = new Logger("bank-api-client.ts");
|
|
|
|
|
2022-03-14 18:31:30 +01:00
|
|
|
export enum CreditDebitIndicator {
|
|
|
|
Credit = "credit",
|
|
|
|
Debit = "debit",
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface BankAccountBalanceResponse {
|
|
|
|
balance: {
|
|
|
|
amount: AmountString;
|
|
|
|
credit_debit_indicator: CreditDebitIndicator;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface BankUser {
|
|
|
|
username: string;
|
|
|
|
password: string;
|
|
|
|
accountPaytoUri: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface WithdrawalOperationInfo {
|
|
|
|
withdrawal_id: string;
|
|
|
|
taler_withdraw_uri: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to generate the "Authorization" HTTP header.
|
|
|
|
*/
|
|
|
|
function makeBasicAuthHeader(username: string, password: string): string {
|
|
|
|
const auth = `${username}:${password}`;
|
2022-11-11 20:52:45 +01:00
|
|
|
const authEncoded: string = base64FromArrayBuffer(stringToBytes(auth));
|
2022-03-14 18:31:30 +01:00
|
|
|
return `Basic ${authEncoded}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
const codecForWithdrawalOperationInfo = (): Codec<WithdrawalOperationInfo> =>
|
|
|
|
buildCodecForObject<WithdrawalOperationInfo>()
|
|
|
|
.property("withdrawal_id", codecForString())
|
|
|
|
.property("taler_withdraw_uri", codecForString())
|
|
|
|
.build("WithdrawalOperationInfo");
|
|
|
|
|
2023-02-23 00:52:10 +01:00
|
|
|
export interface BankAccessApiClientArgs {
|
2023-08-24 18:29:54 +02:00
|
|
|
auth?: { username: string; password: string };
|
2023-09-06 12:32:31 +02:00
|
|
|
httpClient?: HttpRequestLibrary;
|
2023-02-23 00:52:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface BankAccessApiCreateTransactionRequest {
|
|
|
|
amount: AmountString;
|
|
|
|
paytoUri: string;
|
|
|
|
}
|
|
|
|
|
2023-04-22 22:17:08 +02:00
|
|
|
export class WireGatewayApiClientArgs {
|
2023-09-06 12:32:31 +02:00
|
|
|
auth?: {
|
|
|
|
username: string;
|
|
|
|
password: string;
|
|
|
|
};
|
|
|
|
httpClient?: HttpRequestLibrary;
|
2023-04-22 22:17:08 +02:00
|
|
|
}
|
|
|
|
|
2023-04-24 17:53:20 +02:00
|
|
|
/**
|
|
|
|
* This API look like it belongs to harness
|
|
|
|
* but it will be nice to have in utils to be used by others
|
|
|
|
*/
|
2023-04-22 22:17:08 +02:00
|
|
|
export class WireGatewayApiClient {
|
2023-04-24 17:53:20 +02:00
|
|
|
httpLib;
|
2023-04-22 22:17:08 +02:00
|
|
|
|
2023-09-06 12:32:31 +02:00
|
|
|
constructor(
|
|
|
|
private baseUrl: string,
|
|
|
|
private args: WireGatewayApiClientArgs = {},
|
|
|
|
) {
|
|
|
|
this.httpLib = args.httpClient ?? createPlatformHttpLib();
|
|
|
|
}
|
|
|
|
|
|
|
|
private makeAuthHeader(): Record<string, string> {
|
|
|
|
const auth = this.args.auth;
|
|
|
|
if (auth) {
|
|
|
|
return {
|
|
|
|
Authorization: makeBasicAuthHeader(auth.username, auth.password),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {};
|
2023-04-24 17:53:20 +02:00
|
|
|
}
|
2023-04-22 22:17:08 +02:00
|
|
|
|
|
|
|
async adminAddIncoming(params: {
|
|
|
|
amount: string;
|
|
|
|
reservePub: string;
|
|
|
|
debitAccountPayto: string;
|
|
|
|
}): Promise<void> {
|
2023-09-06 12:32:31 +02:00
|
|
|
let url = new URL(`admin/add-incoming`, this.baseUrl);
|
2023-04-22 22:17:08 +02:00
|
|
|
const resp = await this.httpLib.fetch(url.href, {
|
|
|
|
method: "POST",
|
|
|
|
body: {
|
|
|
|
amount: params.amount,
|
|
|
|
reserve_pub: params.reservePub,
|
|
|
|
debit_account: params.debitAccountPayto,
|
|
|
|
},
|
2023-09-06 12:32:31 +02:00
|
|
|
headers: this.makeAuthHeader(),
|
2023-04-22 22:17:08 +02:00
|
|
|
});
|
|
|
|
logger.info(`add-incoming response status: ${resp.status}`);
|
|
|
|
await checkSuccessResponseOrThrow(resp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-21 17:56:29 +02:00
|
|
|
export interface ChallengeContactData {
|
|
|
|
// E-Mail address
|
|
|
|
email?: string;
|
|
|
|
|
|
|
|
// Phone number.
|
|
|
|
phone?: string;
|
|
|
|
}
|
|
|
|
|
2023-09-21 19:43:59 +02:00
|
|
|
export interface AccountBalance {
|
2023-09-21 17:56:29 +02:00
|
|
|
amount: AmountString;
|
|
|
|
credit_debit_indicator: "credit" | "debit";
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface RegisterAccountRequest {
|
|
|
|
// Username
|
|
|
|
username: string;
|
|
|
|
|
|
|
|
// Password.
|
|
|
|
password: string;
|
|
|
|
|
|
|
|
// Legal name of the account owner
|
|
|
|
name: string;
|
|
|
|
|
|
|
|
// Defaults to false.
|
|
|
|
is_public?: boolean;
|
|
|
|
|
|
|
|
// Is this a taler exchange account?
|
|
|
|
// If true:
|
|
|
|
// - incoming transactions to the account that do not
|
|
|
|
// have a valid reserve public key are automatically
|
|
|
|
// - the account provides the taler-wire-gateway-api endpoints
|
|
|
|
// Defaults to false.
|
|
|
|
is_taler_exchange?: boolean;
|
|
|
|
|
|
|
|
// Addresses where to send the TAN for transactions.
|
|
|
|
// Currently only used for cashouts.
|
|
|
|
// If missing, cashouts will fail.
|
|
|
|
// In the future, might be used for other transactions
|
|
|
|
// as well.
|
|
|
|
challenge_contact_data?: ChallengeContactData;
|
|
|
|
|
|
|
|
// 'payto' address pointing a bank account
|
|
|
|
// external to the libeufin-bank.
|
|
|
|
// Payments will be sent to this bank account
|
|
|
|
// when the user wants to convert the local currency
|
|
|
|
// back to fiat currency outside libeufin-bank.
|
|
|
|
cashout_payto_uri?: string;
|
|
|
|
|
|
|
|
// Internal payto URI of this bank account.
|
|
|
|
// Used mostly for testing.
|
|
|
|
internal_payto_uri?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface AccountData {
|
|
|
|
// Legal name of the account owner.
|
|
|
|
name: string;
|
|
|
|
|
|
|
|
// Available balance on the account.
|
2023-09-21 19:43:59 +02:00
|
|
|
balance: AccountBalance;
|
2023-09-21 17:56:29 +02:00
|
|
|
|
|
|
|
// payto://-URI of the account.
|
|
|
|
payto_uri: string;
|
|
|
|
|
|
|
|
// Number indicating the max debit allowed for the requesting user.
|
|
|
|
debit_threshold: AmountString;
|
|
|
|
|
|
|
|
contact_data?: ChallengeContactData;
|
|
|
|
|
|
|
|
// 'payto' address pointing the bank account
|
|
|
|
// where to send cashouts. This field is optional
|
|
|
|
// because not all the accounts are required to participate
|
|
|
|
// in the merchants' circuit. One example is the exchange:
|
|
|
|
// that never cashouts. Registering these accounts can
|
|
|
|
// be done via the access API.
|
|
|
|
cashout_payto_uri?: string;
|
|
|
|
}
|
|
|
|
|
2023-09-24 13:01:42 +02:00
|
|
|
export interface ConfirmWithdrawalArgs {
|
|
|
|
withdrawalOperationId: string;
|
|
|
|
}
|
|
|
|
|
2023-04-24 17:53:20 +02:00
|
|
|
/**
|
2023-09-21 17:56:29 +02:00
|
|
|
* Client for the Taler corebank API.
|
2023-04-24 17:53:20 +02:00
|
|
|
*/
|
2023-09-21 17:56:29 +02:00
|
|
|
export class TalerCorebankApiClient {
|
2023-08-23 14:40:23 +02:00
|
|
|
httpLib: HttpRequestLibrary;
|
2023-02-23 00:52:10 +01:00
|
|
|
|
2023-09-06 12:32:31 +02:00
|
|
|
constructor(
|
|
|
|
private baseUrl: string,
|
|
|
|
private args: BankAccessApiClientArgs = {},
|
|
|
|
) {
|
|
|
|
this.httpLib = args.httpClient ?? createPlatformHttpLib();
|
2023-04-24 17:53:20 +02:00
|
|
|
}
|
2023-02-23 00:52:10 +01:00
|
|
|
|
2023-08-24 18:29:54 +02:00
|
|
|
setAuth(auth: { username: string; password: string }) {
|
|
|
|
this.args.auth = auth;
|
|
|
|
}
|
|
|
|
|
|
|
|
private makeAuthHeader(): Record<string, string> {
|
|
|
|
if (!this.args.auth) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
const authHeaderValue = makeBasicAuthHeader(
|
|
|
|
this.args.auth.username,
|
|
|
|
this.args.auth.password,
|
|
|
|
);
|
|
|
|
return {
|
|
|
|
Authorization: authHeaderValue,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-09-06 12:32:31 +02:00
|
|
|
async getAccountBalance(
|
|
|
|
username: string,
|
|
|
|
): Promise<BankAccountBalanceResponse> {
|
|
|
|
const url = new URL(`accounts/${username}`, this.baseUrl);
|
|
|
|
const resp = await this.httpLib.fetch(url.href, {
|
|
|
|
headers: this.makeAuthHeader(),
|
|
|
|
});
|
2023-09-24 21:03:22 +02:00
|
|
|
return readSuccessResponseJsonOrThrow(resp, codecForAny());
|
2023-09-06 12:32:31 +02:00
|
|
|
}
|
|
|
|
|
2023-08-24 18:29:54 +02:00
|
|
|
async getTransactions(username: string): Promise<void> {
|
2023-09-06 12:32:31 +02:00
|
|
|
const reqUrl = new URL(`accounts/${username}/transactions`, this.baseUrl);
|
2023-02-23 00:52:10 +01:00
|
|
|
const resp = await this.httpLib.fetch(reqUrl.href, {
|
|
|
|
method: "GET",
|
|
|
|
headers: {
|
2023-08-24 18:29:54 +02:00
|
|
|
...this.makeAuthHeader(),
|
2023-02-23 00:52:10 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const res = await readSuccessResponseJsonOrThrow(resp, codecForAny());
|
|
|
|
logger.info(`result: ${j2s(res)}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
async createTransaction(
|
2023-08-24 18:29:54 +02:00
|
|
|
username: string,
|
2023-02-23 00:52:10 +01:00
|
|
|
req: BankAccessApiCreateTransactionRequest,
|
|
|
|
): Promise<any> {
|
2023-09-06 12:32:31 +02:00
|
|
|
const reqUrl = new URL(`accounts/${username}/transactions`, this.baseUrl);
|
2023-08-24 18:29:54 +02:00
|
|
|
|
2023-02-23 00:52:10 +01:00
|
|
|
const resp = await this.httpLib.fetch(reqUrl.href, {
|
|
|
|
method: "POST",
|
|
|
|
body: req,
|
2023-08-24 18:29:54 +02:00
|
|
|
headers: this.makeAuthHeader(),
|
2023-02-23 00:52:10 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return await readSuccessResponseJsonOrThrow(resp, codecForAny());
|
|
|
|
}
|
2023-08-24 18:29:54 +02:00
|
|
|
|
2023-09-24 21:03:22 +02:00
|
|
|
async registerAccountExtended(req: RegisterAccountRequest): Promise<void> {
|
|
|
|
const url = new URL("accounts", this.baseUrl);
|
|
|
|
const resp = await this.httpLib.fetch(url.href, {
|
|
|
|
method: "POST",
|
|
|
|
body: req,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (
|
|
|
|
resp.status !== 200 &&
|
|
|
|
resp.status !== 201 &&
|
|
|
|
resp.status !== 202 &&
|
|
|
|
resp.status !== 204
|
|
|
|
) {
|
|
|
|
logger.error(`unexpected status ${resp.status} from POST ${url.href}`);
|
|
|
|
logger.error(`${j2s(await resp.json())}`);
|
|
|
|
throw TalerError.fromDetail(
|
|
|
|
TalerErrorCode.GENERIC_UNEXPECTED_REQUEST_ERROR,
|
|
|
|
{
|
|
|
|
httpStatusCode: resp.status,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-21 17:56:29 +02:00
|
|
|
/**
|
|
|
|
* Register a new account and return information about it.
|
|
|
|
*
|
|
|
|
* This is a helper, as it does both the registration and the
|
|
|
|
* account info query.
|
|
|
|
*/
|
|
|
|
async registerAccount(username: string, password: string): Promise<BankUser> {
|
|
|
|
const url = new URL("accounts", this.baseUrl);
|
2023-08-24 18:29:54 +02:00
|
|
|
const resp = await this.httpLib.fetch(url.href, {
|
|
|
|
method: "POST",
|
|
|
|
body: {
|
|
|
|
username,
|
|
|
|
password,
|
2023-09-21 17:56:29 +02:00
|
|
|
name: username,
|
2023-08-24 18:29:54 +02:00
|
|
|
},
|
|
|
|
});
|
2023-09-24 21:03:22 +02:00
|
|
|
if (
|
|
|
|
resp.status !== 200 &&
|
|
|
|
resp.status !== 201 &&
|
|
|
|
resp.status !== 202 &&
|
|
|
|
resp.status !== 204
|
|
|
|
) {
|
|
|
|
logger.error(`unexpected status ${resp.status} from POST ${url.href}`);
|
2023-08-24 18:29:54 +02:00
|
|
|
logger.error(`${j2s(await resp.json())}`);
|
|
|
|
throw TalerError.fromDetail(
|
|
|
|
TalerErrorCode.GENERIC_UNEXPECTED_REQUEST_ERROR,
|
|
|
|
{
|
|
|
|
httpStatusCode: resp.status,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2023-09-24 21:03:22 +02:00
|
|
|
// FIXME: Corebank should directly return this info!
|
2023-09-21 17:56:29 +02:00
|
|
|
const infoUrl = new URL(`accounts/${username}`, this.baseUrl);
|
2023-09-24 21:03:22 +02:00
|
|
|
const infoResp = await this.httpLib.fetch(infoUrl.href, {
|
|
|
|
headers: {
|
|
|
|
Authorization: makeBasicAuthHeader(username, password),
|
|
|
|
},
|
|
|
|
});
|
2023-09-21 17:56:29 +02:00
|
|
|
// FIXME: Validate!
|
|
|
|
const acctInfo: AccountData = await readSuccessResponseJsonOrThrow(
|
|
|
|
infoResp,
|
|
|
|
codecForAny(),
|
|
|
|
);
|
2023-08-24 18:29:54 +02:00
|
|
|
return {
|
|
|
|
password,
|
|
|
|
username,
|
2023-09-21 17:56:29 +02:00
|
|
|
accountPaytoUri: acctInfo.payto_uri,
|
2023-08-24 18:29:54 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async createRandomBankUser(): Promise<BankUser> {
|
|
|
|
const username = "user-" + encodeCrock(getRandomBytes(10)).toLowerCase();
|
|
|
|
const password = "pw-" + encodeCrock(getRandomBytes(10)).toLowerCase();
|
2023-09-21 17:56:29 +02:00
|
|
|
return await this.registerAccount(username, password);
|
2023-08-24 18:29:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async createWithdrawalOperation(
|
|
|
|
user: string,
|
|
|
|
amount: string,
|
|
|
|
): Promise<WithdrawalOperationInfo> {
|
2023-09-06 12:32:31 +02:00
|
|
|
const url = new URL(`accounts/${user}/withdrawals`, this.baseUrl);
|
2023-08-24 18:29:54 +02:00
|
|
|
const resp = await this.httpLib.fetch(url.href, {
|
|
|
|
method: "POST",
|
|
|
|
body: {
|
|
|
|
amount,
|
|
|
|
},
|
|
|
|
headers: this.makeAuthHeader(),
|
|
|
|
});
|
|
|
|
return readSuccessResponseJsonOrThrow(
|
|
|
|
resp,
|
|
|
|
codecForWithdrawalOperationInfo(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async confirmWithdrawalOperation(
|
|
|
|
username: string,
|
2023-09-24 13:01:42 +02:00
|
|
|
wopi: ConfirmWithdrawalArgs,
|
2023-08-24 18:29:54 +02:00
|
|
|
): Promise<void> {
|
|
|
|
const url = new URL(
|
2023-09-24 13:01:42 +02:00
|
|
|
`withdrawals/${wopi.withdrawalOperationId}/confirm`,
|
2023-09-06 12:32:31 +02:00
|
|
|
this.baseUrl,
|
2023-08-24 18:29:54 +02:00
|
|
|
);
|
|
|
|
logger.info(`confirming withdrawal operation via ${url.href}`);
|
|
|
|
const resp = await this.httpLib.fetch(url.href, {
|
|
|
|
method: "POST",
|
|
|
|
body: {},
|
|
|
|
headers: this.makeAuthHeader(),
|
|
|
|
});
|
|
|
|
|
|
|
|
logger.info(`response status ${resp.status}`);
|
|
|
|
const respJson = await readSuccessResponseJsonOrThrow(resp, codecForAny());
|
|
|
|
|
|
|
|
// FIXME: We don't check the status here!
|
|
|
|
}
|
|
|
|
|
|
|
|
async abortWithdrawalOperation(
|
|
|
|
accountName: string,
|
|
|
|
wopi: WithdrawalOperationInfo,
|
|
|
|
): Promise<void> {
|
|
|
|
const url = new URL(
|
|
|
|
`accounts/${accountName}/withdrawals/${wopi.withdrawal_id}/abort`,
|
2023-09-06 12:32:31 +02:00
|
|
|
this.baseUrl,
|
2023-08-24 18:29:54 +02:00
|
|
|
);
|
|
|
|
const resp = await this.httpLib.fetch(url.href, {
|
|
|
|
method: "POST",
|
|
|
|
body: {},
|
|
|
|
headers: this.makeAuthHeader(),
|
|
|
|
});
|
|
|
|
await readSuccessResponseJsonOrThrow(resp, codecForAny());
|
|
|
|
}
|
2023-02-23 00:52:10 +01:00
|
|
|
}
|