transactionIdStr nominal typing

This commit is contained in:
Sebastian 2023-05-22 13:12:27 -03:00
parent 3e95ae356a
commit 984cba226f
No known key found for this signature in database
GPG Key ID: 173909D1A5F66069

View File

@ -30,9 +30,11 @@
import { AmountJson, codecForAmountString } from "./amounts.js"; import { AmountJson, codecForAmountString } from "./amounts.js";
import { BackupRecovery } from "./backup-types.js"; import { BackupRecovery } from "./backup-types.js";
import { import {
Codec,
Context,
DecodingError,
buildCodecForObject, buildCodecForObject,
buildCodecForUnion, buildCodecForUnion,
Codec,
codecForAny, codecForAny,
codecForBoolean, codecForBoolean,
codecForConstString, codecForConstString,
@ -42,6 +44,7 @@ import {
codecForNumber, codecForNumber,
codecForString, codecForString,
codecOptional, codecOptional,
renderContext,
} from "./codec.js"; } from "./codec.js";
import { VersionMatchResult } from "./libtool-version.js"; import { VersionMatchResult } from "./libtool-version.js";
import { PaytoUri } from "./payto.js"; import { PaytoUri } from "./payto.js";
@ -50,40 +53,84 @@ import { TalerErrorCode } from "./taler-error-codes.js";
import { import {
AmountString, AmountString,
AuditorDenomSig, AuditorDenomSig,
codecForMerchantContractTerms,
codecForPeerContractTerms,
CoinEnvelope, CoinEnvelope,
DenominationPubKey,
DenomKeyType, DenomKeyType,
DenominationPubKey,
ExchangeAuditor, ExchangeAuditor,
MerchantContractTerms, MerchantContractTerms,
PeerContractTerms, PeerContractTerms,
TrackTransaction,
UnblindedSignature, UnblindedSignature,
codecForMerchantContractTerms,
codecForPeerContractTerms,
} from "./taler-types.js"; } from "./taler-types.js";
import { import {
AbsoluteTime, AbsoluteTime,
codecForAbsoluteTime,
codecForTimestamp,
TalerProtocolDuration, TalerProtocolDuration,
TalerProtocolTimestamp, TalerProtocolTimestamp,
codecForAbsoluteTime,
codecForTimestamp,
} from "./time.js"; } from "./time.js";
import { import { OrderShortInfo } from "./transactions-types.js";
codecForOrderShortInfo,
OrderShortInfo,
} from "./transactions-types.js";
/** /**
* Identifier for a transaction in the wallet. * Identifier for a transaction in the wallet.
*/ */
export type TransactionIdStr = `txn:${string}:${string}`; declare const __txId: unique symbol;
export type TransactionIdStr = `txn:${string}:${string}` & { [__txId]: true };
/** /**
* Identifier for a pending task in the wallet. * Identifier for a pending task in the wallet.
*/ */
export type PendingIdStr = `pnd:${string}:${string}`; declare const __pndId: unique symbol;
export type PendingIdStr = `pnd:${string}:${string}` & { [__pndId]: true };
export type TombstoneIdStr = `tmb:${string}:${string}`; declare const __tmbId: unique symbol;
export type TombstoneIdStr = `tmb:${string}:${string}` & { [__tmbId]: true };
function codecForTransactionIdStr(): Codec<TransactionIdStr> {
return {
decode(x: any, c?: Context): TransactionIdStr {
if (typeof x === "string" && x.startsWith("txn:")) {
return x as TransactionIdStr;
}
throw new DecodingError(
`expected string starting with "txn:" at ${renderContext(
c,
)} but got ${x}`,
);
},
};
}
function codecForPendingIdStr(): Codec<PendingIdStr> {
return {
decode(x: any, c?: Context): PendingIdStr {
if (typeof x === "string" && x.startsWith("txn:")) {
return x as PendingIdStr;
}
throw new DecodingError(
`expected string starting with "txn:" at ${renderContext(
c,
)} but got ${x}`,
);
},
};
}
function codecForTombstoneIdStr(): Codec<TombstoneIdStr> {
return {
decode(x: any, c?: Context): TombstoneIdStr {
if (typeof x === "string" && x.startsWith("tmb:")) {
return x as TombstoneIdStr;
}
throw new DecodingError(
`expected string starting with "tmb:" at ${renderContext(
c,
)} but got ${x}`,
);
},
};
}
/** /**
* Response for the create reserve request to the wallet. * Response for the create reserve request to the wallet.
@ -257,12 +304,12 @@ export enum ConfirmPayResultType {
export interface ConfirmPayResultDone { export interface ConfirmPayResultDone {
type: ConfirmPayResultType.Done; type: ConfirmPayResultType.Done;
contractTerms: MerchantContractTerms; contractTerms: MerchantContractTerms;
transactionId: string; transactionId: TransactionIdStr;
} }
export interface ConfirmPayResultPending { export interface ConfirmPayResultPending {
type: ConfirmPayResultType.Pending; type: ConfirmPayResultType.Pending;
transactionId: string; transactionId: TransactionIdStr;
lastError: TalerErrorDetail | undefined; lastError: TalerErrorDetail | undefined;
} }
@ -279,14 +326,14 @@ export const codecForConfirmPayResultPending =
(): Codec<ConfirmPayResultPending> => (): Codec<ConfirmPayResultPending> =>
buildCodecForObject<ConfirmPayResultPending>() buildCodecForObject<ConfirmPayResultPending>()
.property("lastError", codecOptional(codecForTalerErrorDetail())) .property("lastError", codecOptional(codecForTalerErrorDetail()))
.property("transactionId", codecForString()) .property("transactionId", codecForTransactionIdStr())
.property("type", codecForConstString(ConfirmPayResultType.Pending)) .property("type", codecForConstString(ConfirmPayResultType.Pending))
.build("ConfirmPayResultPending"); .build("ConfirmPayResultPending");
export const codecForConfirmPayResultDone = (): Codec<ConfirmPayResultDone> => export const codecForConfirmPayResultDone = (): Codec<ConfirmPayResultDone> =>
buildCodecForObject<ConfirmPayResultDone>() buildCodecForObject<ConfirmPayResultDone>()
.property("type", codecForConstString(ConfirmPayResultType.Done)) .property("type", codecForConstString(ConfirmPayResultType.Done))
.property("transactionId", codecForString()) .property("transactionId", codecForTransactionIdStr())
.property("contractTerms", codecForMerchantContractTerms()) .property("contractTerms", codecForMerchantContractTerms())
.build("ConfirmPayResultDone"); .build("ConfirmPayResultDone");
@ -387,7 +434,7 @@ export interface PrepareTipResult {
} }
export interface AcceptTipResponse { export interface AcceptTipResponse {
transactionId: string; transactionId: TransactionIdStr;
next_url?: string; next_url?: string;
} }
@ -419,7 +466,7 @@ export const codecForPreparePayResultPaymentPossible =
.property("amountEffective", codecForAmountString()) .property("amountEffective", codecForAmountString())
.property("amountRaw", codecForAmountString()) .property("amountRaw", codecForAmountString())
.property("contractTerms", codecForMerchantContractTerms()) .property("contractTerms", codecForMerchantContractTerms())
.property("transactionId", codecForString()) .property("transactionId", codecForTransactionIdStr())
.property("proposalId", codecForString()) .property("proposalId", codecForString())
.property("contractTermsHash", codecForString()) .property("contractTermsHash", codecForString())
.property("talerUri", codecForString()) .property("talerUri", codecForString())
@ -495,7 +542,7 @@ export const codecForPreparePayResultInsufficientBalance =
.property("contractTerms", codecForAny()) .property("contractTerms", codecForAny())
.property("talerUri", codecForString()) .property("talerUri", codecForString())
.property("proposalId", codecForString()) .property("proposalId", codecForString())
.property("transactionId", codecForString()) .property("transactionId", codecForTransactionIdStr())
.property("noncePriv", codecForString()) .property("noncePriv", codecForString())
.property( .property(
"status", "status",
@ -520,7 +567,7 @@ export const codecForPreparePayResultAlreadyConfirmed =
.property("talerUri", codecOptional(codecForString())) .property("talerUri", codecOptional(codecForString()))
.property("contractTerms", codecForAny()) .property("contractTerms", codecForAny())
.property("contractTermsHash", codecForString()) .property("contractTermsHash", codecForString())
.property("transactionId", codecForString()) .property("transactionId", codecForTransactionIdStr())
.property("proposalId", codecForString()) .property("proposalId", codecForString())
.build("PreparePayResultAlreadyConfirmed"); .build("PreparePayResultAlreadyConfirmed");
@ -554,7 +601,7 @@ export type PreparePayResult =
*/ */
export interface PreparePayResultPaymentPossible { export interface PreparePayResultPaymentPossible {
status: PreparePayResultType.PaymentPossible; status: PreparePayResultType.PaymentPossible;
transactionId: string; transactionId: TransactionIdStr;
/** /**
* @deprecated use transactionId instead * @deprecated use transactionId instead
*/ */
@ -569,7 +616,7 @@ export interface PreparePayResultPaymentPossible {
export interface PreparePayResultInsufficientBalance { export interface PreparePayResultInsufficientBalance {
status: PreparePayResultType.InsufficientBalance; status: PreparePayResultType.InsufficientBalance;
transactionId: string; transactionId: TransactionIdStr;
proposalId: string; proposalId: string;
contractTerms: MerchantContractTerms; contractTerms: MerchantContractTerms;
amountRaw: string; amountRaw: string;
@ -580,7 +627,7 @@ export interface PreparePayResultInsufficientBalance {
export interface PreparePayResultAlreadyConfirmed { export interface PreparePayResultAlreadyConfirmed {
status: PreparePayResultType.AlreadyConfirmed; status: PreparePayResultType.AlreadyConfirmed;
transactionId: string; transactionId: TransactionIdStr;
contractTerms: MerchantContractTerms; contractTerms: MerchantContractTerms;
paid: boolean; paid: boolean;
amountRaw: string; amountRaw: string;
@ -603,7 +650,7 @@ export interface BankWithdrawDetails {
export interface AcceptWithdrawalResponse { export interface AcceptWithdrawalResponse {
reservePub: string; reservePub: string;
confirmTransferUrl?: string; confirmTransferUrl?: string;
transactionId: string; transactionId: TransactionIdStr;
} }
/** /**
@ -1067,7 +1114,7 @@ export interface AcceptManualWithdrawalResult {
*/ */
reservePub: string; reservePub: string;
transactionId: string; transactionId: TransactionIdStr;
} }
export interface ManualWithdrawalDetails { export interface ManualWithdrawalDetails {
@ -1362,12 +1409,12 @@ export const codecForAcceptExchangeTosRequest =
.build("AcceptExchangeTosRequest"); .build("AcceptExchangeTosRequest");
export interface AcceptRefundRequest { export interface AcceptRefundRequest {
transactionId: string; transactionId: TransactionIdStr;
} }
export const codecForApplyRefundRequest = (): Codec<AcceptRefundRequest> => export const codecForApplyRefundRequest = (): Codec<AcceptRefundRequest> =>
buildCodecForObject<AcceptRefundRequest>() buildCodecForObject<AcceptRefundRequest>()
.property("transactionId", codecForString()) .property("transactionId", codecForTransactionIdStr())
.build("AcceptRefundRequest"); .build("AcceptRefundRequest");
export interface ApplyRefundFromPurchaseIdRequest { export interface ApplyRefundFromPurchaseIdRequest {
@ -1615,7 +1662,7 @@ export interface PrepareRefundRequest {
} }
export interface StartRefundQueryForUriResponse { export interface StartRefundQueryForUriResponse {
transactionId: string; transactionId: TransactionIdStr;
} }
export const codecForPrepareRefundRequest = (): Codec<PrepareRefundRequest> => export const codecForPrepareRefundRequest = (): Codec<PrepareRefundRequest> =>
@ -1624,13 +1671,13 @@ export const codecForPrepareRefundRequest = (): Codec<PrepareRefundRequest> =>
.build("PrepareRefundRequest"); .build("PrepareRefundRequest");
export interface StartRefundQueryRequest { export interface StartRefundQueryRequest {
transactionId: string; transactionId: TransactionIdStr;
} }
export const codecForStartRefundQueryRequest = export const codecForStartRefundQueryRequest =
(): Codec<StartRefundQueryRequest> => (): Codec<StartRefundQueryRequest> =>
buildCodecForObject<StartRefundQueryRequest>() buildCodecForObject<StartRefundQueryRequest>()
.property("transactionId", codecForString()) .property("transactionId", codecForTransactionIdStr())
.build("StartRefundQueryRequest"); .build("StartRefundQueryRequest");
export interface PrepareTipRequest { export interface PrepareTipRequest {
@ -1639,7 +1686,7 @@ export interface PrepareTipRequest {
export const codecForPrepareTipRequest = (): Codec<PrepareTipRequest> => export const codecForPrepareTipRequest = (): Codec<PrepareTipRequest> =>
buildCodecForObject<PrepareTipRequest>() buildCodecForObject<PrepareTipRequest>()
.property("talerTipUri", codecForString()) .property("talerTipUri", codecForTransactionIdStr())
.build("PrepareTipRequest"); .build("PrepareTipRequest");
export interface AcceptTipRequest { export interface AcceptTipRequest {
@ -1652,35 +1699,35 @@ export const codecForAcceptTipRequest = (): Codec<AcceptTipRequest> =>
.build("AcceptTipRequest"); .build("AcceptTipRequest");
export interface SuspendTransactionRequest { export interface SuspendTransactionRequest {
transactionId: string; transactionId: TransactionIdStr;
} }
export const codecForSuspendTransaction = export const codecForSuspendTransaction =
(): Codec<SuspendTransactionRequest> => (): Codec<SuspendTransactionRequest> =>
buildCodecForObject<AbortTransactionRequest>() buildCodecForObject<AbortTransactionRequest>()
.property("transactionId", codecForString()) .property("transactionId", codecForTransactionIdStr())
.build("SuspendTransactionRequest"); .build("SuspendTransactionRequest");
export interface ResumeTransactionRequest { export interface ResumeTransactionRequest {
transactionId: string; transactionId: TransactionIdStr;
} }
export const codecForResumeTransaction = (): Codec<ResumeTransactionRequest> => export const codecForResumeTransaction = (): Codec<ResumeTransactionRequest> =>
buildCodecForObject<ResumeTransactionRequest>() buildCodecForObject<ResumeTransactionRequest>()
.property("transactionId", codecForString()) .property("transactionId", codecForTransactionIdStr())
.build("ResumeTransactionRequest"); .build("ResumeTransactionRequest");
export interface AbortTransactionRequest { export interface AbortTransactionRequest {
transactionId: string; transactionId: TransactionIdStr;
} }
export interface CancelAbortingTransactionRequest { export interface CancelAbortingTransactionRequest {
transactionId: string; transactionId: TransactionIdStr;
} }
export const codecForAbortTransaction = (): Codec<AbortTransactionRequest> => export const codecForAbortTransaction = (): Codec<AbortTransactionRequest> =>
buildCodecForObject<AbortTransactionRequest>() buildCodecForObject<AbortTransactionRequest>()
.property("transactionId", codecForString()) .property("transactionId", codecForTransactionIdStr())
.build("AbortTransactionRequest"); .build("AbortTransactionRequest");
export interface DepositGroupFees { export interface DepositGroupFees {
@ -1727,11 +1774,11 @@ export const codecForCreateDepositGroupRequest =
export interface CreateDepositGroupResponse { export interface CreateDepositGroupResponse {
depositGroupId: string; depositGroupId: string;
transactionId: string; transactionId: TransactionIdStr;
} }
export interface TxIdResponse { export interface TxIdResponse {
transactionId: string; transactionId: TransactionIdStr;
} }
export interface WithdrawUriInfoResponse { export interface WithdrawUriInfoResponse {
@ -1762,23 +1809,23 @@ export interface WalletCurrencyInfo {
} }
export interface DeleteTransactionRequest { export interface DeleteTransactionRequest {
transactionId: string; transactionId: TransactionIdStr;
} }
export interface RetryTransactionRequest { export interface RetryTransactionRequest {
transactionId: string; transactionId: TransactionIdStr;
} }
export const codecForDeleteTransactionRequest = export const codecForDeleteTransactionRequest =
(): Codec<DeleteTransactionRequest> => (): Codec<DeleteTransactionRequest> =>
buildCodecForObject<DeleteTransactionRequest>() buildCodecForObject<DeleteTransactionRequest>()
.property("transactionId", codecForString()) .property("transactionId", codecForTransactionIdStr())
.build("DeleteTransactionRequest"); .build("DeleteTransactionRequest");
export const codecForRetryTransactionRequest = export const codecForRetryTransactionRequest =
(): Codec<RetryTransactionRequest> => (): Codec<RetryTransactionRequest> =>
buildCodecForObject<RetryTransactionRequest>() buildCodecForObject<RetryTransactionRequest>()
.property("transactionId", codecForString()) .property("transactionId", codecForTransactionIdStr())
.build("RetryTransactionRequest"); .build("RetryTransactionRequest");
export interface SetWalletDeviceIdRequest { export interface SetWalletDeviceIdRequest {
@ -1907,12 +1954,12 @@ interface AttentionBackupUnpaid {
interface AttentionMerchantRefund { interface AttentionMerchantRefund {
type: AttentionType.MerchantRefund; type: AttentionType.MerchantRefund;
transactionId: string; transactionId: TransactionIdStr;
} }
interface AttentionKycWithdrawal { interface AttentionKycWithdrawal {
type: AttentionType.KycWithdrawal; type: AttentionType.KycWithdrawal;
transactionId: string; transactionId: TransactionIdStr;
} }
interface AttentionExchangeTosChanged { interface AttentionExchangeTosChanged {
@ -1942,12 +1989,12 @@ interface AttentionAuditorDenominationExpires {
} }
interface AttentionPullPaymentPaid { interface AttentionPullPaymentPaid {
type: AttentionType.PullPaymentPaid; type: AttentionType.PullPaymentPaid;
transactionId: string; transactionId: TransactionIdStr;
} }
interface AttentionPushPaymentReceived { interface AttentionPushPaymentReceived {
type: AttentionType.PushPaymentReceived; type: AttentionType.PushPaymentReceived;
transactionId: string; transactionId: TransactionIdStr;
} }
export type UserAttentionUnreadList = Array<{ export type UserAttentionUnreadList = Array<{
@ -2070,7 +2117,7 @@ export interface InitiatePeerPushDebitResponse {
mergePriv: string; mergePriv: string;
contractPriv: string; contractPriv: string;
talerUri: string; talerUri: string;
transactionId: string; transactionId: TransactionIdStr;
} }
export const codecForInitiatePeerPushDebitRequest = export const codecForInitiatePeerPushDebitRequest =
@ -2130,11 +2177,11 @@ export interface ConfirmPeerPushCreditRequest {
peerPushPaymentIncomingId: string; peerPushPaymentIncomingId: string;
} }
export interface AcceptPeerPushPaymentResponse { export interface AcceptPeerPushPaymentResponse {
transactionId: string; transactionId: TransactionIdStr;
} }
export interface AcceptPeerPullPaymentResponse { export interface AcceptPeerPullPaymentResponse {
transactionId: string; transactionId: TransactionIdStr;
} }
export const codecForConfirmPeerPushPaymentRequest = export const codecForConfirmPeerPushPaymentRequest =
@ -2201,7 +2248,7 @@ export interface InitiatePeerPullCreditResponse {
*/ */
talerUri: string; talerUri: string;
transactionId: string; transactionId: TransactionIdStr;
} }
/** /**