From 8d7b171d02e0ab4d5da7dc81eaea6c27106dbc57 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Wed, 12 Aug 2020 16:02:58 +0530 Subject: rename for consistency --- packages/taler-integrationtests/src/harness.ts | 8 +- .../taler-integrationtests/src/merchantApiTypes.ts | 45 +-- packages/taler-wallet-cli/src/index.ts | 4 +- .../taler-wallet-core/src/operations/testing.ts | 4 +- .../src/operations/transactions.ts | 1 - .../taler-wallet-core/src/types/ReserveStatus.ts | 10 +- .../src/types/ReserveTransaction.ts | 60 ++-- packages/taler-wallet-core/src/types/talerTypes.ts | 337 +++++++++++---------- .../taler-wallet-core/src/types/transactions.ts | 10 +- .../taler-wallet-core/src/types/walletTypes.ts | 58 ++-- packages/taler-wallet-core/src/util/amounts.ts | 12 +- packages/taler-wallet-core/src/util/codec-test.ts | 24 +- packages/taler-wallet-core/src/util/codec.ts | 76 ++--- .../taler-wallet-core/src/walletCoreApiHandler.ts | 58 ++-- 14 files changed, 356 insertions(+), 351 deletions(-) diff --git a/packages/taler-integrationtests/src/harness.ts b/packages/taler-integrationtests/src/harness.ts index 84b5bc5c4..ecb0758da 100644 --- a/packages/taler-integrationtests/src/harness.ts +++ b/packages/taler-integrationtests/src/harness.ts @@ -35,7 +35,7 @@ import { AmountJson, Amounts, Codec, - makeCodecForObject, + buildCodecForObject, codecForString, Duration, CoreApiResponse, @@ -627,9 +627,9 @@ export interface WithdrawalOperationInfo { } const codecForWithdrawalOperationInfo = (): Codec => - makeCodecForObject() - .property("withdrawal_id", codecForString) - .property("taler_withdraw_uri", codecForString) + buildCodecForObject() + .property("withdrawal_id", codecForString()) + .property("taler_withdraw_uri", codecForString()) .build("WithdrawalOperationInfo"); export const defaultCoinConfig = [ diff --git a/packages/taler-integrationtests/src/merchantApiTypes.ts b/packages/taler-integrationtests/src/merchantApiTypes.ts index 3c0965813..d08c354a1 100644 --- a/packages/taler-integrationtests/src/merchantApiTypes.ts +++ b/packages/taler-integrationtests/src/merchantApiTypes.ts @@ -28,19 +28,20 @@ import { ContractTerms, Duration, Codec, - makeCodecForObject, + buildCodecForObject, codecForString, - makeCodecOptional, - makeCodecForConstString, + codecOptional, + codecForConstString, codecForBoolean, codecForNumber, codecForContractTerms, codecForAny, - makeCodecForUnion, + buildCodecForUnion, AmountString, Timestamp, CoinPublicKeyString, } from "taler-wallet-core"; +import { codecForAmountString } from "taler-wallet-core/lib/util/amounts"; export interface PostOrderRequest { // The order must at least contain the minimal @@ -72,43 +73,43 @@ export interface PostOrderResponse { } export const codecForPostOrderResponse = (): Codec => - makeCodecForObject() - .property("order_id", codecForString) - .property("token", makeCodecOptional(codecForString)) + buildCodecForObject() + .property("order_id", codecForString()) + .property("token", codecOptional(codecForString())) .build("PostOrderResponse"); export const codecForCheckPaymentPaidResponse = (): Codec< CheckPaymentPaidResponse > => - makeCodecForObject() - .property("order_status", makeCodecForConstString("paid")) + buildCodecForObject() + .property("order_status", codecForConstString("paid")) .property("refunded", codecForBoolean) .property("wired", codecForBoolean) - .property("deposit_total", codecForString) - .property("exchange_ec", codecForNumber) - .property("exchange_hc", codecForNumber) - .property("refund_amount", codecForString) + .property("deposit_total", codecForAmountString()) + .property("exchange_ec", codecForNumber()) + .property("exchange_hc", codecForNumber()) + .property("refund_amount", codecForAmountString()) .property("contract_terms", codecForContractTerms()) // FIXME: specify - .property("wire_details", codecForAny) - .property("wire_reports", codecForAny) - .property("refund_details", codecForAny) + .property("wire_details", codecForAny()) + .property("wire_reports", codecForAny()) + .property("refund_details", codecForAny()) .build("CheckPaymentPaidResponse"); export const codecForCheckPaymentUnpaidResponse = (): Codec< CheckPaymentUnpaidResponse > => - makeCodecForObject() - .property("order_status", makeCodecForConstString("unpaid")) - .property("taler_pay_uri", codecForString) - .property("order_status_url", codecForString) - .property("already_paid_order_id", makeCodecOptional(codecForString)) + buildCodecForObject() + .property("order_status", codecForConstString("unpaid")) + .property("taler_pay_uri", codecForString()) + .property("order_status_url", codecForString()) + .property("already_paid_order_id", codecOptional(codecForString())) .build("CheckPaymentPaidResponse"); export const codecForMerchantOrderPrivateStatusResponse = (): Codec< MerchantOrderPrivateStatusResponse > => - makeCodecForUnion() + buildCodecForUnion() .discriminateOn("order_status") .alternative("paid", codecForCheckPaymentPaidResponse()) .alternative("unpaid", codecForCheckPaymentUnpaidResponse()) diff --git a/packages/taler-wallet-cli/src/index.ts b/packages/taler-wallet-cli/src/index.ts index 16897b7aa..a32ed2267 100644 --- a/packages/taler-wallet-cli/src/index.ts +++ b/packages/taler-wallet-cli/src/index.ts @@ -31,7 +31,7 @@ import { TalerUriType, decodeCrock, addPaytoQueryParams, - makeCodecForList, + codecForList, codecForString, printTestVectors, } from "taler-wallet-core"; @@ -548,7 +548,7 @@ advancedCli }); }); -const coinPubListCodec = makeCodecForList(codecForString); +const coinPubListCodec = codecForList(codecForString()); advancedCli .subcommand("suspendCoins", "suspend-coins", { diff --git a/packages/taler-wallet-core/src/operations/testing.ts b/packages/taler-wallet-core/src/operations/testing.ts index 71cee1f3a..3ccfafc93 100644 --- a/packages/taler-wallet-core/src/operations/testing.ts +++ b/packages/taler-wallet-core/src/operations/testing.ts @@ -112,7 +112,7 @@ async function createBankWithdrawalUri( }, }, ); - const respJson = await readSuccessResponseJsonOrThrow(resp, codecForAny); + const respJson = await readSuccessResponseJsonOrThrow(resp, codecForAny()); return respJson; } @@ -135,7 +135,7 @@ async function confirmBankWithdrawalUri( }, }, ); - await readSuccessResponseJsonOrThrow(resp, codecForAny); + await readSuccessResponseJsonOrThrow(resp, codecForAny()); return; } diff --git a/packages/taler-wallet-core/src/operations/transactions.ts b/packages/taler-wallet-core/src/operations/transactions.ts index 8de204d49..5521dda90 100644 --- a/packages/taler-wallet-core/src/operations/transactions.ts +++ b/packages/taler-wallet-core/src/operations/transactions.ts @@ -37,7 +37,6 @@ import { PaymentShortInfo, } from "../types/transactions"; import { getFundingPaytoUris } from "./reserves"; -import { ResultLevel } from "idb-bridge"; /** * Create an event ID from the type and the primary key for the event. diff --git a/packages/taler-wallet-core/src/types/ReserveStatus.ts b/packages/taler-wallet-core/src/types/ReserveStatus.ts index 18601b9a7..8a6092a7f 100644 --- a/packages/taler-wallet-core/src/types/ReserveStatus.ts +++ b/packages/taler-wallet-core/src/types/ReserveStatus.ts @@ -23,8 +23,8 @@ */ import { codecForString, - makeCodecForObject, - makeCodecForList, + buildCodecForObject, + codecForList, Codec, } from "../util/codec"; import { AmountString } from "./talerTypes"; @@ -51,7 +51,7 @@ export interface ReserveStatus { } export const codecForReserveStatus = (): Codec => - makeCodecForObject() - .property("balance", codecForString) - .property("history", makeCodecForList(codecForReserveTransaction())) + buildCodecForObject() + .property("balance", codecForString()) + .property("history", codecForList(codecForReserveTransaction())) .build("ReserveStatus"); diff --git a/packages/taler-wallet-core/src/types/ReserveTransaction.ts b/packages/taler-wallet-core/src/types/ReserveTransaction.ts index bdd9b0f93..0d946518f 100644 --- a/packages/taler-wallet-core/src/types/ReserveTransaction.ts +++ b/packages/taler-wallet-core/src/types/ReserveTransaction.ts @@ -23,9 +23,9 @@ */ import { codecForString, - makeCodecForObject, - makeCodecForConstString, - makeCodecForUnion, + buildCodecForObject, + codecForConstString, + buildCodecForUnion, Codec, } from "../util/codec"; import { @@ -182,54 +182,54 @@ export type ReserveTransaction = export const codecForReserveWithdrawTransaction = (): Codec< ReserveWithdrawTransaction > => - makeCodecForObject() - .property("amount", codecForString) - .property("h_coin_envelope", codecForString) - .property("h_denom_pub", codecForString) - .property("reserve_sig", codecForString) - .property("type", makeCodecForConstString(ReserveTransactionType.Withdraw)) - .property("withdraw_fee", codecForString) + buildCodecForObject() + .property("amount", codecForString()) + .property("h_coin_envelope", codecForString()) + .property("h_denom_pub", codecForString()) + .property("reserve_sig", codecForString()) + .property("type", codecForConstString(ReserveTransactionType.Withdraw)) + .property("withdraw_fee", codecForString()) .build("ReserveWithdrawTransaction"); export const codecForReserveCreditTransaction = (): Codec< ReserveCreditTransaction > => - makeCodecForObject() - .property("amount", codecForString) - .property("sender_account_url", codecForString) + buildCodecForObject() + .property("amount", codecForString()) + .property("sender_account_url", codecForString()) .property("timestamp", codecForTimestamp) - .property("wire_reference", codecForString) - .property("type", makeCodecForConstString(ReserveTransactionType.Credit)) + .property("wire_reference", codecForString()) + .property("type", codecForConstString(ReserveTransactionType.Credit)) .build("ReserveCreditTransaction"); export const codecForReserveClosingTransaction = (): Codec< ReserveClosingTransaction > => - makeCodecForObject() - .property("amount", codecForString) - .property("closing_fee", codecForString) - .property("exchange_pub", codecForString) - .property("exchange_sig", codecForString) - .property("h_wire", codecForString) + buildCodecForObject() + .property("amount", codecForString()) + .property("closing_fee", codecForString()) + .property("exchange_pub", codecForString()) + .property("exchange_sig", codecForString()) + .property("h_wire", codecForString()) .property("timestamp", codecForTimestamp) - .property("type", makeCodecForConstString(ReserveTransactionType.Closing)) - .property("wtid", codecForString) + .property("type", codecForConstString(ReserveTransactionType.Closing)) + .property("wtid", codecForString()) .build("ReserveClosingTransaction"); export const codecForReserveRecoupTransaction = (): Codec< ReserveRecoupTransaction > => - makeCodecForObject() - .property("amount", codecForString) - .property("coin_pub", codecForString) - .property("exchange_pub", codecForString) - .property("exchange_sig", codecForString) + buildCodecForObject() + .property("amount", codecForString()) + .property("coin_pub", codecForString()) + .property("exchange_pub", codecForString()) + .property("exchange_sig", codecForString()) .property("timestamp", codecForTimestamp) - .property("type", makeCodecForConstString(ReserveTransactionType.Recoup)) + .property("type", codecForConstString(ReserveTransactionType.Recoup)) .build("ReserveRecoupTransaction"); export const codecForReserveTransaction = (): Codec => - makeCodecForUnion() + buildCodecForUnion() .discriminateOn("type") .alternative( ReserveTransactionType.Withdraw, diff --git a/packages/taler-wallet-core/src/types/talerTypes.ts b/packages/taler-wallet-core/src/types/talerTypes.ts index acebbda95..bb0118a78 100644 --- a/packages/taler-wallet-core/src/types/talerTypes.ts +++ b/packages/taler-wallet-core/src/types/talerTypes.ts @@ -28,18 +28,18 @@ */ import { - makeCodecForObject, + buildCodecForObject, codecForString, - makeCodecForList, - makeCodecOptional, + codecForList, + codecOptional, codecForAny, codecForNumber, codecForBoolean, - makeCodecForMap, + codecForMap, Codec, - makeCodecForConstNumber, - makeCodecForUnion, - makeCodecForConstString, + codecForConstNumber, + buildCodecForUnion, + codecForConstString, } from "../util/codec"; import { Timestamp, @@ -48,6 +48,7 @@ import { codecForDuration, } from "../util/time"; import { ExchangeListItem } from "./walletTypes"; +import { codecForAmountString } from "../util/amounts"; /** * Denomination as found in the /keys response from the exchange. @@ -951,7 +952,7 @@ export interface BankWithdrawalOperationPostResponse { export const codecForBankWithdrawalOperationPostResponse = (): Codec< BankWithdrawalOperationPostResponse > => - makeCodecForObject() + buildCodecForObject() .property("transfer_done", codecForBoolean) .build("BankWithdrawalOperationPostResponse"); @@ -962,292 +963,292 @@ export type EddsaPublicKeyString = string; export type CoinPublicKeyString = string; export const codecForDenomination = (): Codec => - makeCodecForObject() - .property("value", codecForString) - .property("denom_pub", codecForString) - .property("fee_withdraw", codecForString) - .property("fee_deposit", codecForString) - .property("fee_refresh", codecForString) - .property("fee_refund", codecForString) + buildCodecForObject() + .property("value", codecForString()) + .property("denom_pub", codecForString()) + .property("fee_withdraw", codecForString()) + .property("fee_deposit", codecForString()) + .property("fee_refresh", codecForString()) + .property("fee_refund", codecForString()) .property("stamp_start", codecForTimestamp) .property("stamp_expire_withdraw", codecForTimestamp) .property("stamp_expire_legal", codecForTimestamp) .property("stamp_expire_deposit", codecForTimestamp) - .property("master_sig", codecForString) + .property("master_sig", codecForString()) .build("Denomination"); export const codecForAuditorDenomSig = (): Codec => - makeCodecForObject() - .property("denom_pub_h", codecForString) - .property("auditor_sig", codecForString) + buildCodecForObject() + .property("denom_pub_h", codecForString()) + .property("auditor_sig", codecForString()) .build("AuditorDenomSig"); export const codecForAuditor = (): Codec => - makeCodecForObject() - .property("auditor_pub", codecForString) - .property("auditor_url", codecForString) - .property("denomination_keys", makeCodecForList(codecForAuditorDenomSig())) + buildCodecForObject() + .property("auditor_pub", codecForString()) + .property("auditor_url", codecForString()) + .property("denomination_keys", codecForList(codecForAuditorDenomSig())) .build("Auditor"); export const codecForExchangeHandle = (): Codec => - makeCodecForObject() - .property("master_pub", codecForString) - .property("url", codecForString) + buildCodecForObject() + .property("master_pub", codecForString()) + .property("url", codecForString()) .build("ExchangeHandle"); export const codecForAuditorHandle = (): Codec => - makeCodecForObject() - .property("name", codecForString) - .property("master_pub", codecForString) - .property("url", codecForString) + buildCodecForObject() + .property("name", codecForString()) + .property("master_pub", codecForString()) + .property("url", codecForString()) .build("AuditorHandle"); export const codecForMerchantInfo = (): Codec => - makeCodecForObject() - .property("name", codecForString) - .property("address", makeCodecOptional(codecForString)) - .property("jurisdiction", makeCodecOptional(codecForString)) + buildCodecForObject() + .property("name", codecForString()) + .property("address", codecOptional(codecForString())) + .property("jurisdiction", codecOptional(codecForString())) .build("MerchantInfo"); export const codecForTax = (): Codec => - makeCodecForObject() - .property("name", codecForString) - .property("tax", codecForString) + buildCodecForObject() + .property("name", codecForString()) + .property("tax", codecForString()) .build("Tax"); export const codecForI18n = (): Codec<{ [lang_tag: string]: string }> => - makeCodecForMap(codecForString); + codecForMap(codecForString()); export const codecForProduct = (): Codec => - makeCodecForObject() - .property("product_id", makeCodecOptional(codecForString)) - .property("description", codecForString) - .property("description_i18n", makeCodecOptional(codecForI18n())) - .property("quantity", makeCodecOptional(codecForNumber)) - .property("unit", makeCodecOptional(codecForString)) - .property("price", makeCodecOptional(codecForString)) - .property("delivery_date", makeCodecOptional(codecForTimestamp)) - .property("delivery_location", makeCodecOptional(codecForString)) + buildCodecForObject() + .property("product_id", codecOptional(codecForString())) + .property("description", codecForString()) + .property("description_i18n", codecOptional(codecForI18n())) + .property("quantity", codecOptional(codecForNumber())) + .property("unit", codecOptional(codecForString())) + .property("price", codecOptional(codecForString())) + .property("delivery_date", codecOptional(codecForTimestamp)) + .property("delivery_location", codecOptional(codecForString())) .build("Tax"); export const codecForContractTerms = (): Codec => - makeCodecForObject() - .property("order_id", codecForString) - .property("fulfillment_url", codecForString) - .property("merchant_base_url", codecForString) - .property("h_wire", codecForString) - .property("auto_refund", makeCodecOptional(codecForDuration)) - .property("wire_method", codecForString) - .property("summary", codecForString) - .property("summary_i18n", makeCodecOptional(codecForI18n())) - .property("nonce", codecForString) - .property("amount", codecForString) - .property("auditors", makeCodecForList(codecForAuditorHandle())) + buildCodecForObject() + .property("order_id", codecForString()) + .property("fulfillment_url", codecForString()) + .property("merchant_base_url", codecForString()) + .property("h_wire", codecForString()) + .property("auto_refund", codecOptional(codecForDuration)) + .property("wire_method", codecForString()) + .property("summary", codecForString()) + .property("summary_i18n", codecOptional(codecForI18n())) + .property("nonce", codecForString()) + .property("amount", codecForString()) + .property("auditors", codecForList(codecForAuditorHandle())) .property("pay_deadline", codecForTimestamp) .property("refund_deadline", codecForTimestamp) .property("wire_transfer_deadline", codecForTimestamp) .property("timestamp", codecForTimestamp) - .property("locations", codecForAny) - .property("max_fee", codecForString) - .property("max_wire_fee", makeCodecOptional(codecForString)) + .property("locations", codecForAny()) + .property("max_fee", codecForString()) + .property("max_wire_fee", codecOptional(codecForString())) .property("merchant", codecForMerchantInfo()) - .property("merchant_pub", codecForString) - .property("exchanges", makeCodecForList(codecForExchangeHandle())) + .property("merchant_pub", codecForString()) + .property("exchanges", codecForList(codecForExchangeHandle())) .property( "products", - makeCodecOptional(makeCodecForList(codecForProduct())), + codecOptional(codecForList(codecForProduct())), ) - .property("extra", codecForAny) + .property("extra", codecForAny()) .build("ContractTerms"); export const codecForMerchantRefundPermission = (): Codec< MerchantAbortPayRefundDetails > => - makeCodecForObject() - .property("refund_amount", codecForString) - .property("refund_fee", codecForString) - .property("coin_pub", codecForString) - .property("rtransaction_id", codecForNumber) - .property("exchange_http_status", codecForNumber) - .property("exchange_code", makeCodecOptional(codecForNumber)) - .property("exchange_reply", makeCodecOptional(codecForAny)) - .property("exchange_sig", makeCodecOptional(codecForString)) - .property("exchange_pub", makeCodecOptional(codecForString)) + buildCodecForObject() + .property("refund_amount", codecForAmountString()) + .property("refund_fee", codecForAmountString()) + .property("coin_pub", codecForString()) + .property("rtransaction_id", codecForNumber()) + .property("exchange_http_status", codecForNumber()) + .property("exchange_code", codecOptional(codecForNumber())) + .property("exchange_reply", codecOptional(codecForAny())) + .property("exchange_sig", codecOptional(codecForString())) + .property("exchange_pub", codecOptional(codecForString())) .build("MerchantRefundPermission"); export const codecForMerchantRefundResponse = (): Codec< MerchantRefundResponse > => - makeCodecForObject() - .property("merchant_pub", codecForString) - .property("h_contract_terms", codecForString) - .property("refunds", makeCodecForList(codecForMerchantRefundPermission())) + buildCodecForObject() + .property("merchant_pub", codecForString()) + .property("h_contract_terms", codecForString()) + .property("refunds", codecForList(codecForMerchantRefundPermission())) .build("MerchantRefundResponse"); export const codecForReserveSigSingleton = (): Codec => - makeCodecForObject() - .property("reserve_sig", codecForString) + buildCodecForObject() + .property("reserve_sig", codecForString()) .build("ReserveSigSingleton"); export const codecForTipResponse = (): Codec => - makeCodecForObject() - .property("reserve_pub", codecForString) - .property("reserve_sigs", makeCodecForList(codecForReserveSigSingleton())) + buildCodecForObject() + .property("reserve_pub", codecForString()) + .property("reserve_sigs", codecForList(codecForReserveSigSingleton())) .build("TipResponse"); export const codecForRecoup = (): Codec => - makeCodecForObject() - .property("h_denom_pub", codecForString) + buildCodecForObject() + .property("h_denom_pub", codecForString()) .build("Recoup"); export const codecForExchangeSigningKey = (): Codec => - makeCodecForObject() - .property("key", codecForString) - .property("master_sig", codecForString) + buildCodecForObject() + .property("key", codecForString()) + .property("master_sig", codecForString()) .property("stamp_end", codecForTimestamp) .property("stamp_start", codecForTimestamp) .property("stamp_expire", codecForTimestamp) .build("ExchangeSignKeyJson"); export const codecForExchangeKeysJson = (): Codec => - makeCodecForObject() - .property("denoms", makeCodecForList(codecForDenomination())) - .property("master_public_key", codecForString) - .property("auditors", makeCodecForList(codecForAuditor())) + buildCodecForObject() + .property("denoms", codecForList(codecForDenomination())) + .property("master_public_key", codecForString()) + .property("auditors", codecForList(codecForAuditor())) .property("list_issue_date", codecForTimestamp) - .property("recoup", makeCodecOptional(makeCodecForList(codecForRecoup()))) - .property("signkeys", makeCodecForList(codecForExchangeSigningKey())) - .property("version", codecForString) + .property("recoup", codecOptional(codecForList(codecForRecoup()))) + .property("signkeys", codecForList(codecForExchangeSigningKey())) + .property("version", codecForString()) .build("KeysJson"); export const codecForWireFeesJson = (): Codec => - makeCodecForObject() - .property("wire_fee", codecForString) - .property("closing_fee", codecForString) - .property("sig", codecForString) + buildCodecForObject() + .property("wire_fee", codecForString()) + .property("closing_fee", codecForString()) + .property("sig", codecForString()) .property("start_date", codecForTimestamp) .property("end_date", codecForTimestamp) .build("WireFeesJson"); export const codecForAccountInfo = (): Codec => - makeCodecForObject() - .property("payto_uri", codecForString) - .property("master_sig", codecForString) + buildCodecForObject() + .property("payto_uri", codecForString()) + .property("master_sig", codecForString()) .build("AccountInfo"); export const codecForExchangeWireJson = (): Codec => - makeCodecForObject() - .property("accounts", makeCodecForList(codecForAccountInfo())) - .property("fees", makeCodecForMap(makeCodecForList(codecForWireFeesJson()))) + buildCodecForObject() + .property("accounts", codecForList(codecForAccountInfo())) + .property("fees", codecForMap(codecForList(codecForWireFeesJson()))) .build("ExchangeWireJson"); export const codecForProposal = (): Codec => - makeCodecForObject() - .property("contract_terms", codecForAny) - .property("sig", codecForString) + buildCodecForObject() + .property("contract_terms", codecForAny()) + .property("sig", codecForString()) .build("Proposal"); export const codecForCheckPaymentResponse = (): Codec => - makeCodecForObject() - .property("order_status", codecForString) - .property("refunded", makeCodecOptional(codecForBoolean)) - .property("refunded_amount", makeCodecOptional(codecForString)) - .property("contract_terms", makeCodecOptional(codecForAny)) - .property("taler_pay_uri", makeCodecOptional(codecForString)) - .property("contract_url", makeCodecOptional(codecForString)) + buildCodecForObject() + .property("order_status", codecForString()) + .property("refunded", codecOptional(codecForBoolean)) + .property("refunded_amount", codecOptional(codecForString())) + .property("contract_terms", codecOptional(codecForAny())) + .property("taler_pay_uri", codecOptional(codecForString())) + .property("contract_url", codecOptional(codecForString())) .build("CheckPaymentResponse"); export const codecForWithdrawOperationStatusResponse = (): Codec< WithdrawOperationStatusResponse > => - makeCodecForObject() + buildCodecForObject() .property("selection_done", codecForBoolean) .property("transfer_done", codecForBoolean) - .property("amount", codecForString) - .property("sender_wire", makeCodecOptional(codecForString)) - .property("suggested_exchange", makeCodecOptional(codecForString)) - .property("confirm_transfer_url", makeCodecOptional(codecForString)) - .property("wire_types", makeCodecForList(codecForString)) + .property("amount", codecForString()) + .property("sender_wire", codecOptional(codecForString())) + .property("suggested_exchange", codecOptional(codecForString())) + .property("confirm_transfer_url", codecOptional(codecForString())) + .property("wire_types", codecForList(codecForString())) .build("WithdrawOperationStatusResponse"); export const codecForTipPickupGetResponse = (): Codec => - makeCodecForObject() - .property("extra", codecForAny) - .property("amount", codecForString) - .property("amount_left", codecForString) - .property("exchange_url", codecForString) + buildCodecForObject() + .property("extra", codecForAny()) + .property("amount", codecForString()) + .property("amount_left", codecForString()) + .property("exchange_url", codecForString()) .property("stamp_expire", codecForTimestamp) .property("stamp_created", codecForTimestamp) .build("TipPickupGetResponse"); export const codecForRecoupConfirmation = (): Codec => - makeCodecForObject() - .property("reserve_pub", makeCodecOptional(codecForString)) - .property("old_coin_pub", makeCodecOptional(codecForString)) + buildCodecForObject() + .property("reserve_pub", codecOptional(codecForString())) + .property("old_coin_pub", codecOptional(codecForString())) .build("RecoupConfirmation"); export const codecForWithdrawResponse = (): Codec => - makeCodecForObject() - .property("ev_sig", codecForString) + buildCodecForObject() + .property("ev_sig", codecForString()) .build("WithdrawResponse"); export const codecForMerchantPayResponse = (): Codec => - makeCodecForObject() - .property("sig", codecForString) + buildCodecForObject() + .property("sig", codecForString()) .build("MerchantPayResponse"); export const codecForExchangeMeltResponse = (): Codec => - makeCodecForObject() - .property("exchange_pub", codecForString) - .property("exchange_sig", codecForString) - .property("noreveal_index", codecForNumber) - .property("refresh_base_url", makeCodecOptional(codecForString)) + buildCodecForObject() + .property("exchange_pub", codecForString()) + .property("exchange_sig", codecForString()) + .property("noreveal_index", codecForNumber()) + .property("refresh_base_url", codecOptional(codecForString())) .build("ExchangeMeltResponse"); export const codecForExchangeRevealItem = (): Codec => - makeCodecForObject() - .property("ev_sig", codecForString) + buildCodecForObject() + .property("ev_sig", codecForString()) .build("ExchangeRevealItem"); export const codecForExchangeRevealResponse = (): Codec< ExchangeRevealResponse > => - makeCodecForObject() - .property("ev_sigs", makeCodecForList(codecForExchangeRevealItem())) + buildCodecForObject() + .property("ev_sigs", codecForList(codecForExchangeRevealItem())) .build("ExchangeRevealResponse"); export const codecForMerchantCoinRefundSuccessStatus = (): Codec< MerchantCoinRefundSuccessStatus > => - makeCodecForObject() - .property("type", makeCodecForConstString("success")) - .property("coin_pub", codecForString) - .property("exchange_status", makeCodecForConstNumber(200)) - .property("exchange_sig", codecForString) - .property("rtransaction_id", codecForNumber) - .property("refund_amount", codecForString) - .property("exchange_pub", codecForString) + buildCodecForObject() + .property("type", codecForConstString("success")) + .property("coin_pub", codecForString()) + .property("exchange_status", codecForConstNumber(200)) + .property("exchange_sig", codecForString()) + .property("rtransaction_id", codecForNumber()) + .property("refund_amount", codecForString()) + .property("exchange_pub", codecForString()) .property("execution_time", codecForTimestamp) .build("MerchantCoinRefundSuccessStatus"); export const codecForMerchantCoinRefundFailureStatus = (): Codec< MerchantCoinRefundFailureStatus > => - makeCodecForObject() - .property("type", makeCodecForConstString("failure")) - .property("coin_pub", codecForString) - .property("exchange_status", makeCodecForConstNumber(200)) - .property("rtransaction_id", codecForNumber) - .property("refund_amount", codecForString) - .property("exchange_code", makeCodecOptional(codecForNumber)) - .property("exchange_reply", makeCodecOptional(codecForAny)) + buildCodecForObject() + .property("type", codecForConstString("failure")) + .property("coin_pub", codecForString()) + .property("exchange_status", codecForConstNumber(200)) + .property("rtransaction_id", codecForNumber()) + .property("refund_amount", codecForString()) + .property("exchange_code", codecOptional(codecForNumber())) + .property("exchange_reply", codecOptional(codecForAny())) .property("execution_time", codecForTimestamp) .build("MerchantCoinRefundSuccessStatus"); export const codecForMerchantCoinRefundStatus = (): Codec< MerchantCoinRefundStatus > => - makeCodecForUnion() + buildCodecForUnion() .discriminateOn("type") .alternative("success", codecForMerchantCoinRefundSuccessStatus()) .alternative("failure", codecForMerchantCoinRefundFailureStatus()) @@ -1256,17 +1257,17 @@ export const codecForMerchantCoinRefundStatus = (): Codec< export const codecForMerchantOrderStatusPaid = (): Codec< MerchantOrderStatusPaid > => - makeCodecForObject() - .property("merchant_pub", codecForString) - .property("refund_amount", codecForString) + buildCodecForObject() + .property("merchant_pub", codecForString()) + .property("refund_amount", codecForString()) .property("refunded", codecForBoolean) - .property("refunds", makeCodecForList(codecForMerchantCoinRefundStatus())) + .property("refunds", codecForList(codecForMerchantCoinRefundStatus())) .build("MerchantOrderStatusPaid"); export const codecForMerchantOrderStatusUnpaid = (): Codec< MerchantOrderStatusUnpaid > => - makeCodecForObject() - .property("taler_pay_uri", codecForString) - .property("already_paid_order_id", makeCodecOptional(codecForString)) + buildCodecForObject() + .property("taler_pay_uri", codecForString()) + .property("already_paid_order_id", codecOptional(codecForString())) .build("MerchantOrderStatusUnpaid"); diff --git a/packages/taler-wallet-core/src/types/transactions.ts b/packages/taler-wallet-core/src/types/transactions.ts index 47e3e7d5b..f3192e5a4 100644 --- a/packages/taler-wallet-core/src/types/transactions.ts +++ b/packages/taler-wallet-core/src/types/transactions.ts @@ -28,8 +28,8 @@ import { Timestamp } from "../util/time"; import { AmountString, Product } from "./talerTypes"; import { Codec, - makeCodecForObject, - makeCodecOptional, + buildCodecForObject, + codecOptional, codecForString, } from "../util/codec"; @@ -305,7 +305,7 @@ interface TransactionRefresh extends TransactionCommon { } export const codecForTransactionsRequest = (): Codec => - makeCodecForObject() - .property("currency", makeCodecOptional(codecForString)) - .property("search", makeCodecOptional(codecForString)) + buildCodecForObject() + .property("currency", codecOptional(codecForString())) + .property("search", codecOptional(codecForString())) .build("TransactionsRequest"); diff --git a/packages/taler-wallet-core/src/types/walletTypes.ts b/packages/taler-wallet-core/src/types/walletTypes.ts index d79ade971..7a648dd56 100644 --- a/packages/taler-wallet-core/src/types/walletTypes.ts +++ b/packages/taler-wallet-core/src/types/walletTypes.ts @@ -40,13 +40,13 @@ import { } from "./dbTypes"; import { Timestamp } from "../util/time"; import { - makeCodecForObject, + buildCodecForObject, codecForString, - makeCodecOptional, + codecOptional, Codec, - makeCodecForList, + codecForList, codecForBoolean, - makeCodecForConstString, + codecForConstString, codecForAny, } from "../util/codec"; import { AmountString, codecForContractTerms } from "./talerTypes"; @@ -174,17 +174,17 @@ export interface BalancesResponse { } export const codecForBalance = (): Codec => - makeCodecForObject() - .property("available", codecForString) + buildCodecForObject() + .property("available", codecForString()) .property("hasPendingTransactions", codecForBoolean) - .property("pendingIncoming", codecForString) - .property("pendingOutgoing", codecForString) + .property("pendingIncoming", codecForString()) + .property("pendingOutgoing", codecForString()) .property("requiresUserInput", codecForBoolean) .build("Balance"); export const codecForBalancesResponse = (): Codec => - makeCodecForObject() - .property("balances", makeCodecForList(codecForBalance())) + buildCodecForObject() + .property("balances", codecForList(codecForBalance())) .build("BalancesResponse"); /** @@ -270,12 +270,12 @@ export interface CreateReserveRequest { } export const codecForCreateReserveRequest = (): Codec => - makeCodecForObject() + buildCodecForObject() .property("amount", codecForAmountJson()) - .property("exchange", codecForString) - .property("exchangePaytoUri", codecForString) - .property("senderWire", makeCodecOptional(codecForString)) - .property("bankWithdrawStatusUrl", makeCodecOptional(codecForString)) + .property("exchange", codecForString()) + .property("exchangePaytoUri", codecForString()) + .property("senderWire", codecOptional(codecForString())) + .property("bankWithdrawStatusUrl", codecOptional(codecForString())) .build("CreateReserveRequest"); /** @@ -290,8 +290,8 @@ export interface ConfirmReserveRequest { } export const codecForConfirmReserveRequest = (): Codec => - makeCodecForObject() - .property("reservePub", codecForString) + buildCodecForObject() + .property("reservePub", codecForString()) .build("ConfirmReserveRequest"); /** @@ -360,43 +360,43 @@ export const enum PreparePayResultType { export const codecForPreparePayResultPaymentPossible = (): Codec< PreparePayResultPaymentPossible > => - makeCodecForObject() + buildCodecForObject() .property("amountEffective", codecForAmountString()) .property("amountRaw", codecForAmountString()) - .property("contractTerms", codecForAny) - .property("proposalId", codecForString) + .property("contractTerms", codecForAny()) + .property("proposalId", codecForString()) .property( "status", - makeCodecForConstString(PreparePayResultType.PaymentPossible), + codecForConstString(PreparePayResultType.PaymentPossible), ) .build("PreparePayResultPaymentPossible"); export const codecForPreparePayResultInsufficientBalance = (): Codec< PreparePayResultInsufficientBalance > => - makeCodecForObject() + buildCodecForObject() .property("amountRaw", codecForAmountString()) - .property("contractTerms", codecForAny) - .property("proposalId", codecForString) + .property("contractTerms", codecForAny()) + .property("proposalId", codecForString()) .property( "status", - makeCodecForConstString(PreparePayResultType.InsufficientBalance), + codecForConstString(PreparePayResultType.InsufficientBalance), ) .build("PreparePayResultInsufficientBalance"); export const codecForPreparePayResultAlreadyConfirmed = (): Codec< PreparePayResultAlreadyConfirmed > => - makeCodecForObject() + buildCodecForObject() .property( "status", - makeCodecForConstString(PreparePayResultType.AlreadyConfirmed), + codecForConstString(PreparePayResultType.AlreadyConfirmed), ) .property("amountEffective", codecForAmountString()) .property("amountRaw", codecForAmountString()) - .property("nextUrl", codecForString) + .property("nextUrl", codecForString()) .property("paid", codecForBoolean) - .property("contractTerms", codecForAny) + .property("contractTerms", codecForAny()) .build("PreparePayResultAlreadyConfirmed"); export type PreparePayResult = diff --git a/packages/taler-wallet-core/src/util/amounts.ts b/packages/taler-wallet-core/src/util/amounts.ts index 33faa868e..2a8c47905 100644 --- a/packages/taler-wallet-core/src/util/amounts.ts +++ b/packages/taler-wallet-core/src/util/amounts.ts @@ -22,7 +22,7 @@ * Imports. */ import { - makeCodecForObject, + buildCodecForObject, codecForString, codecForNumber, Codec, @@ -68,13 +68,13 @@ export interface AmountJson { } export const codecForAmountJson = (): Codec => - makeCodecForObject() - .property("currency", codecForString) - .property("value", codecForNumber) - .property("fraction", codecForNumber) + buildCodecForObject() + .property("currency", codecForString()) + .property("value", codecForNumber()) + .property("fraction", codecForNumber()) .build("AmountJson"); -export const codecForAmountString = (): Codec => codecForString; +export const codecForAmountString = (): Codec => codecForString(); /** * Result of a possibly overflowing operation. diff --git a/packages/taler-wallet-core/src/util/codec-test.ts b/packages/taler-wallet-core/src/util/codec-test.ts index b429c318c..f8f4c797c 100644 --- a/packages/taler-wallet-core/src/util/codec-test.ts +++ b/packages/taler-wallet-core/src/util/codec-test.ts @@ -21,10 +21,10 @@ import test from "ava"; import { Codec, - makeCodecForObject, - makeCodecForConstString, + buildCodecForObject, + codecForConstString, codecForString, - makeCodecForUnion, + buildCodecForUnion, } from "./codec"; interface MyObj { @@ -44,8 +44,8 @@ interface AltTwo { type MyUnion = AltOne | AltTwo; test("basic codec", (t) => { - const myObjCodec = makeCodecForObject() - .property("foo", codecForString) + const myObjCodec = buildCodecForObject() + .property("foo", codecForString()) .build("MyObj"); const res = myObjCodec.decode({ foo: "hello" }); t.assert(res.foo === "hello"); @@ -56,15 +56,15 @@ test("basic codec", (t) => { }); test("union", (t) => { - const altOneCodec: Codec = makeCodecForObject() - .property("type", makeCodecForConstString("one")) - .property("foo", codecForString) + const altOneCodec: Codec = buildCodecForObject() + .property("type", codecForConstString("one")) + .property("foo", codecForString()) .build("AltOne"); - const altTwoCodec: Codec = makeCodecForObject() - .property("type", makeCodecForConstString("two")) - .property("bar", codecForString) + const altTwoCodec: Codec = buildCodecForObject() + .property("type", codecForConstString("two")) + .property("bar", codecForString()) .build("AltTwo"); - const myUnionCodec: Codec = makeCodecForUnion() + const myUnionCodec: Codec = buildCodecForUnion() .discriminateOn("type") .alternative("one", altOneCodec) .alternative("two", altTwoCodec) diff --git a/packages/taler-wallet-core/src/util/codec.ts b/packages/taler-wallet-core/src/util/codec.ts index 2ce3c2cba..111abc38c 100644 --- a/packages/taler-wallet-core/src/util/codec.ts +++ b/packages/taler-wallet-core/src/util/codec.ts @@ -221,18 +221,18 @@ export class UnionCodecPreBuilder { /** * Return a builder for a codec that decodes an object with properties. */ -export function makeCodecForObject(): ObjectCodecBuilder { +export function buildCodecForObject(): ObjectCodecBuilder { return new ObjectCodecBuilder(); } -export function makeCodecForUnion(): UnionCodecPreBuilder { +export function buildCodecForUnion(): UnionCodecPreBuilder { return new UnionCodecPreBuilder(); } /** * Return a codec for a mapping from a string to values described by the inner codec. */ -export function makeCodecForMap( +export function codecForMap( innerCodec: Codec, ): Codec<{ [x: string]: T }> { if (!innerCodec) { @@ -255,7 +255,7 @@ export function makeCodecForMap( /** * Return a codec for a list, containing values described by the inner codec. */ -export function makeCodecForList(innerCodec: Codec): Codec { +export function codecForList(innerCodec: Codec): Codec { if (!innerCodec) { throw Error("inner codec must be defined"); } @@ -276,16 +276,18 @@ export function makeCodecForList(innerCodec: Codec): Codec { /** * Return a codec for a value that must be a number. */ -export const codecForNumber: Codec = { - decode(x: any, c?: Context): number { - if (typeof x === "number") { - return x; - } - throw new DecodingError( - `expected number at ${renderContext(c)} but got ${typeof x}`, - ); - }, -}; +export function codecForNumber(): Codec { + return { + decode(x: any, c?: Context): number { + if (typeof x === "number") { + return x; + } + throw new DecodingError( + `expected number at ${renderContext(c)} but got ${typeof x}`, + ); + }, + }; +} /** * Return a codec for a value that must be a number. @@ -304,30 +306,34 @@ export const codecForBoolean: Codec = { /** * Return a codec for a value that must be a string. */ -export const codecForString: Codec = { - decode(x: any, c?: Context): string { - if (typeof x === "string") { - return x; - } - throw new DecodingError( - `expected string at ${renderContext(c)} but got ${typeof x}`, - ); - }, -}; +export function codecForString(): Codec { + return { + decode(x: any, c?: Context): string { + if (typeof x === "string") { + return x; + } + throw new DecodingError( + `expected string at ${renderContext(c)} but got ${typeof x}`, + ); + }, + }; +} /** * Codec that allows any value. */ -export const codecForAny: Codec = { - decode(x: any, c?: Context): any { - return x; - }, -}; +export function codecForAny(): Codec { + return { + decode(x: any, c?: Context): any { + return x; + }, + }; +} /** * Return a codec for a value that must be a string. */ -export function makeCodecForConstString(s: V): Codec { +export function codecForConstString(s: V): Codec { return { decode(x: any, c?: Context): V { if (x === s) { @@ -345,7 +351,7 @@ export function makeCodecForConstString(s: V): Codec { /** * Return a codec for a boolean true constant. */ -export function makeCodecForConstTrue(): Codec { +export function codecForConstTrue(): Codec { return { decode(x: any, c?: Context): true { if (x === true) { @@ -361,7 +367,7 @@ export function makeCodecForConstTrue(): Codec { /** * Return a codec for a boolean true constant. */ -export function makeCodecForConstFalse(): Codec { +export function codecForConstFalse(): Codec { return { decode(x: any, c?: Context): false { if (x === false) { @@ -377,7 +383,7 @@ export function makeCodecForConstFalse(): Codec { /** * Return a codec for a value that must be a constant number. */ -export function makeCodecForConstNumber(n: V): Codec { +export function codecForConstNumber(n: V): Codec { return { decode(x: any, c?: Context): V { if (x === n) { @@ -392,9 +398,7 @@ export function makeCodecForConstNumber(n: V): Codec { }; } -export function makeCodecOptional( - innerCodec: Codec, -): Codec { +export function codecOptional(innerCodec: Codec): Codec { return { decode(x: any, c?: Context): V | undefined { if (x === undefined || x === null) { diff --git a/packages/taler-wallet-core/src/walletCoreApiHandler.ts b/packages/taler-wallet-core/src/walletCoreApiHandler.ts index 604439186..5bc9005a8 100644 --- a/packages/taler-wallet-core/src/walletCoreApiHandler.ts +++ b/packages/taler-wallet-core/src/walletCoreApiHandler.ts @@ -23,10 +23,10 @@ import { import { TalerErrorCode } from "./TalerErrorCode"; import { codecForTransactionsRequest } from "./types/transactions"; import { - makeCodecForObject, + buildCodecForObject, codecForString, Codec, - makeCodecOptional, + codecOptional, } from "./util/codec"; import { Amounts } from "./util/amounts"; import { OperationErrorDetails } from "./types/walletTypes"; @@ -36,8 +36,8 @@ export interface AddExchangeRequest { } export const codecForAddExchangeRequest = (): Codec => - makeCodecForObject() - .property("exchangeBaseUrl", codecForString) + buildCodecForObject() + .property("exchangeBaseUrl", codecForString()) .build("AddExchangeRequest"); export interface GetExchangeTosRequest { @@ -45,8 +45,8 @@ export interface GetExchangeTosRequest { } export const codecForGetExchangeTosRequest = (): Codec => - makeCodecForObject() - .property("exchangeBaseUrl", codecForString) + buildCodecForObject() + .property("exchangeBaseUrl", codecForString()) .build("GetExchangeTosRequest"); export interface AcceptManualWithdrawalRequest { @@ -57,9 +57,9 @@ export interface AcceptManualWithdrawalRequest { export const codecForAcceptManualWithdrawalRequet = (): Codec< AcceptManualWithdrawalRequest > => - makeCodecForObject() - .property("exchangeBaseUrl", codecForString) - .property("amount", codecForString) + buildCodecForObject() + .property("exchangeBaseUrl", codecForString()) + .property("amount", codecForString()) .build("AcceptManualWithdrawalRequest"); export interface GetWithdrawalDetailsForAmountRequest { @@ -75,17 +75,17 @@ export interface AcceptBankIntegratedWithdrawalRequest { export const codecForAcceptBankIntegratedWithdrawalRequest = (): Codec< AcceptBankIntegratedWithdrawalRequest > => - makeCodecForObject() - .property("exchangeBaseUrl", codecForString) - .property("talerWithdrawUri", codecForString) + buildCodecForObject() + .property("exchangeBaseUrl", codecForString()) + .property("talerWithdrawUri", codecForString()) .build("AcceptBankIntegratedWithdrawalRequest"); export const codecForGetWithdrawalDetailsForAmountRequest = (): Codec< GetWithdrawalDetailsForAmountRequest > => - makeCodecForObject() - .property("exchangeBaseUrl", codecForString) - .property("amount", codecForString) + buildCodecForObject() + .property("exchangeBaseUrl", codecForString()) + .property("amount", codecForString()) .build("GetWithdrawalDetailsForAmountRequest"); export interface AcceptExchangeTosRequest { @@ -94,9 +94,9 @@ export interface AcceptExchangeTosRequest { } export const codecForAcceptExchangeTosRequest = (): Codec => - makeCodecForObject() - .property("exchangeBaseUrl", codecForString) - .property("etag", codecForString) + buildCodecForObject() + .property("exchangeBaseUrl", codecForString()) + .property("etag", codecForString()) .build("AcceptExchangeTosRequest"); export interface ApplyRefundRequest { @@ -104,8 +104,8 @@ export interface ApplyRefundRequest { } export const codecForApplyRefundRequest = (): Codec => - makeCodecForObject() - .property("talerRefundUri", codecForString) + buildCodecForObject() + .property("talerRefundUri", codecForString()) .build("ApplyRefundRequest"); export interface GetWithdrawalDetailsForUriRequest { @@ -115,8 +115,8 @@ export interface GetWithdrawalDetailsForUriRequest { export const codecForGetWithdrawalDetailsForUri = (): Codec< GetWithdrawalDetailsForUriRequest > => - makeCodecForObject() - .property("talerWithdrawUri", codecForString) + buildCodecForObject() + .property("talerWithdrawUri", codecForString()) .build("GetWithdrawalDetailsForUriRequest"); export interface AbortProposalRequest { @@ -124,8 +124,8 @@ export interface AbortProposalRequest { } export const codecForAbortProposalRequest = (): Codec => - makeCodecForObject() - .property("proposalId", codecForString) + buildCodecForObject() + .property("proposalId", codecForString()) .build("AbortProposalRequest"); export interface PreparePayRequest { @@ -133,8 +133,8 @@ export interface PreparePayRequest { } const codecForPreparePayRequest = (): Codec => - makeCodecForObject() - .property("talerPayUri", codecForString) + buildCodecForObject() + .property("talerPayUri", codecForString()) .build("PreparePay"); export interface ConfirmPayRequest { @@ -143,9 +143,9 @@ export interface ConfirmPayRequest { } export const codecForConfirmPayRequest = (): Codec => - makeCodecForObject() - .property("proposalId", codecForString) - .property("sessionId", makeCodecOptional(codecForString)) + buildCodecForObject() + .property("proposalId", codecForString()) + .property("sessionId", codecOptional(codecForString())) .build("ConfirmPay"); /** -- cgit v1.2.3