wallet-core: allow confirmPeerPullDebit via txId

This commit is contained in:
Florian Dold 2023-06-26 12:53:32 +02:00
parent fca893038d
commit eae3571607
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
2 changed files with 28 additions and 7 deletions

View File

@ -2434,8 +2434,12 @@ export const codecForConfirmPeerPushPaymentRequest =
export interface ConfirmPeerPullDebitRequest { export interface ConfirmPeerPullDebitRequest {
/** /**
* Transparent identifier of the incoming peer pull payment. * Transparent identifier of the incoming peer pull payment.
*
* @deprecated use transactionId instead
*/ */
peerPullPaymentIncomingId: string; peerPullPaymentIncomingId?: string;
transactionId?: string;
} }
export interface ApplyDevExperimentRequest { export interface ApplyDevExperimentRequest {
@ -2451,7 +2455,8 @@ export const codecForApplyDevExperiment =
export const codecForAcceptPeerPullPaymentRequest = export const codecForAcceptPeerPullPaymentRequest =
(): Codec<ConfirmPeerPullDebitRequest> => (): Codec<ConfirmPeerPullDebitRequest> =>
buildCodecForObject<ConfirmPeerPullDebitRequest>() buildCodecForObject<ConfirmPeerPullDebitRequest>()
.property("peerPullPaymentIncomingId", codecForString()) .property("peerPullPaymentIncomingId", codecOptional(codecForString()))
.property("transactionId", codecOptional(codecForString()))
.build("ConfirmPeerPullDebitRequest"); .build("ConfirmPeerPullDebitRequest");
export interface CheckPeerPullCreditRequest { export interface CheckPeerPullCreditRequest {

View File

@ -78,6 +78,7 @@ import {
import { import {
constructTransactionIdentifier, constructTransactionIdentifier,
notifyTransition, notifyTransition,
parseTransactionIdentifier,
stopLongpolling, stopLongpolling,
} from "./transactions.js"; } from "./transactions.js";
@ -387,10 +388,25 @@ export async function confirmPeerPullDebit(
ws: InternalWalletState, ws: InternalWalletState,
req: ConfirmPeerPullDebitRequest, req: ConfirmPeerPullDebitRequest,
): Promise<AcceptPeerPullPaymentResponse> { ): Promise<AcceptPeerPullPaymentResponse> {
let peerPullPaymentIncomingId: string;
if (req.transactionId) {
const parsedTx = parseTransactionIdentifier(req.transactionId);
if (!parsedTx || parsedTx.tag !== TransactionType.PeerPullDebit) {
throw Error("invalid peer-pull-debit transaction identifier");
}
peerPullPaymentIncomingId = parsedTx.peerPullPaymentIncomingId;
} else if (req.peerPullPaymentIncomingId) {
peerPullPaymentIncomingId = req.peerPullPaymentIncomingId;
} else {
throw Error("invalid request, transactionId or peerPullPaymentIncomingId required");
}
const peerPullInc = await ws.db const peerPullInc = await ws.db
.mktx((x) => [x.peerPullPaymentIncoming]) .mktx((x) => [x.peerPullPaymentIncoming])
.runReadOnly(async (tx) => { .runReadOnly(async (tx) => {
return tx.peerPullPaymentIncoming.get(req.peerPullPaymentIncomingId); return tx.peerPullPaymentIncoming.get(peerPullPaymentIncomingId);
}); });
if (!peerPullInc) { if (!peerPullInc) {
@ -436,7 +452,7 @@ export async function confirmPeerPullDebit(
// allocationId: `txn:peer-pull-debit:${req.peerPullPaymentIncomingId}`, // allocationId: `txn:peer-pull-debit:${req.peerPullPaymentIncomingId}`,
allocationId: constructTransactionIdentifier({ allocationId: constructTransactionIdentifier({
tag: TransactionType.PeerPullDebit, tag: TransactionType.PeerPullDebit,
peerPullPaymentIncomingId: req.peerPullPaymentIncomingId, peerPullPaymentIncomingId,
}), }),
coinPubs: sel.coins.map((x) => x.coinPub), coinPubs: sel.coins.map((x) => x.coinPub),
contributions: sel.coins.map((x) => contributions: sel.coins.map((x) =>
@ -446,7 +462,7 @@ export async function confirmPeerPullDebit(
}); });
const pi = await tx.peerPullPaymentIncoming.get( const pi = await tx.peerPullPaymentIncoming.get(
req.peerPullPaymentIncomingId, peerPullPaymentIncomingId,
); );
if (!pi) { if (!pi) {
throw Error(); throw Error();
@ -473,7 +489,7 @@ export async function confirmPeerPullDebit(
const transactionId = constructTransactionIdentifier({ const transactionId = constructTransactionIdentifier({
tag: TransactionType.PeerPullDebit, tag: TransactionType.PeerPullDebit,
peerPullPaymentIncomingId: req.peerPullPaymentIncomingId, peerPullPaymentIncomingId,
}); });
return { return {