extends parseId to include tmb

This commit is contained in:
Sebastian 2022-10-17 13:50:17 -03:00
parent 995b6b4e96
commit 57892db20a
No known key found for this signature in database
GPG Key ID: BE4FF68352439FC1
2 changed files with 14 additions and 18 deletions

View File

@ -306,23 +306,26 @@ export function makeTransactionId(
return `txn:${type}:${args.map((x) => encodeURIComponent(x)).join(":")}`; return `txn:${type}:${args.map((x) => encodeURIComponent(x)).join(":")}`;
} }
export function parseTransactionId(txId: string): { export function parseId(
idType: "txn" | "tmb" | "any",
txId: string,
): {
type: TransactionType; type: TransactionType;
args: string[]; args: string[];
} { } {
const txnParts = txId.split(":"); const txnParts = txId.split(":");
if (txnParts.length < 3) { if (txnParts.length < 3) {
throw Error("transactionId should have al least 3 parts separated by ':'"); throw Error("id should have al least 3 parts separated by ':'");
} }
const [txn, typeStr, ...args] = txnParts; const [prefix, typeStr, ...args] = txnParts;
const type = typeStr as TransactionType; const type = typeStr as TransactionType;
if (txn !== "txn") { if (idType != "any" && prefix !== idType) {
throw Error("transactionId should start with txn"); throw Error(`id should start with ${idType}`);
} }
if (args.length === 0) { if (args.length === 0) {
throw Error("transactionId should have one or more arguments"); throw Error("id should have one or more arguments");
} }
return { type, args }; return { type, args };

View File

@ -56,7 +56,7 @@ import { RetryTags } from "../util/retries.js";
import { import {
makeTombstoneId, makeTombstoneId,
makeTransactionId, makeTransactionId,
parseTransactionId, parseId,
TombstoneTag, TombstoneTag,
} from "./common.js"; } from "./common.js";
import { processDepositGroup } from "./deposits.js"; import { processDepositGroup } from "./deposits.js";
@ -122,7 +122,7 @@ export async function getTransactionById(
ws: InternalWalletState, ws: InternalWalletState,
req: TransactionByIdRequest, req: TransactionByIdRequest,
): Promise<Transaction> { ): Promise<Transaction> {
const { type, args: rest } = parseTransactionId(req.transactionId); const { type, args: rest } = parseId("txn", req.transactionId);
if ( if (
type === TransactionType.Withdrawal || type === TransactionType.Withdrawal ||
type === TransactionType.PeerPullCredit || type === TransactionType.PeerPullCredit ||
@ -959,11 +959,7 @@ export async function retryTransaction(
): Promise<void> { ): Promise<void> {
logger.info(`retrying transaction ${transactionId}`); logger.info(`retrying transaction ${transactionId}`);
const [tmbPrefix, type, ...rest] = transactionId.split(":"); const { type, args: rest } = parseId("any", transactionId);
if (tmbPrefix !== "tmb") {
throw Error("invalid tombstone, expected 'tmb' prefix");
}
switch (type) { switch (type) {
case TransactionType.Deposit: { case TransactionType.Deposit: {
@ -1005,11 +1001,8 @@ export async function deleteTransaction(
ws: InternalWalletState, ws: InternalWalletState,
transactionId: string, transactionId: string,
): Promise<void> { ): Promise<void> {
const [txnPrefix, typeStr, ...rest] = transactionId.split(":"); const { type, args: rest } = parseId("txn", transactionId);
if (txnPrefix !== "txn") {
throw Error("invalid transaction ID, expected 'txn' prefix");
}
const type = typeStr as TransactionType;
if ( if (
type === TransactionType.Withdrawal || type === TransactionType.Withdrawal ||
type === TransactionType.PeerPullCredit || type === TransactionType.PeerPullCredit ||