Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ import {

import { mintClaimedKeyId } from "./routes/auth/claim";
import { registerPayerHandler } from "./routes/delegate/register";
import { addPayeeHandler } from "./routes/delegate/user";
import { addPayeeHandler, removePayeeHandler } from "./routes/delegate/user";
import { sendTxnHandler } from "./routes/auth/sendTxn";

const app = express();
Expand Down Expand Up @@ -222,6 +222,7 @@ app.get("/auth/status/:requestId", getAuthStatusHandler);
// -- Payment Delegation
app.post("/register-payer", registerPayerHandler);
app.post("/add-users", addPayeeHandler);
app.post("/remove-users", removePayeeHandler);

// --- Send TXN
app.post("/send-txn", sendTxnHandler);
Expand Down
40 changes: 40 additions & 0 deletions lit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,46 @@ export async function addPaymentDelegationPayee({
}
}

export async function removePaymentDelegationPayee({
wallet,
payeeAddresses,
}: {
wallet: ethers.Wallet;
payeeAddresses: string[];
}) {
// remove payer in contract
const paymentDelegationContract = await getContractFromJsSdk(
config.network,
"PaymentDelegation",
wallet,
);

try {
// Estimate gas first
const estimatedGas = await paymentDelegationContract.estimateGas.undelegatePaymentsBatch(
payeeAddresses
);

// Add 30% buffer using proper BigNumber math
const gasLimit = estimatedGas
.mul(ethers.BigNumber.from(130))
.div(ethers.BigNumber.from(100));

console.log(`Estimated gas: ${estimatedGas.toString()}, Using gas limit: ${gasLimit.toString()}`);

const tx = await paymentDelegationContract.functions.undelegatePaymentsBatch(
payeeAddresses,
{ gasLimit }
);
console.log("tx hash for undelegatePaymentsBatch()", tx.hash);
await tx.wait();
return tx;
} catch (err) {
console.error("Error while estimating or executing undelegatePaymentsBatch:", err);
throw err;
}
}

// export function packAuthData({
// credentialPublicKey,
// credentialID,
Expand Down
53 changes: 52 additions & 1 deletion routes/delegate/user.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Request, Response } from 'express';
import { deriveWallet } from './register';
import { addPaymentDelegationPayee } from '../../lit';
import { addPaymentDelegationPayee, removePaymentDelegationPayee } from '../../lit';

export async function addPayeeHandler(req: Request, res: Response) {
const payeeAddresses = req.body as string[];
Expand Down Expand Up @@ -41,6 +41,57 @@ export async function addPayeeHandler(req: Request, res: Response) {
error = (err as Error).toString();
}

if (error) {
res.status(500).json({
success: false,
error
});
} else {
res.status(200).json({
success: true
});
}
}

export async function removePayeeHandler(req: Request, res: Response) {
const payeeAddresses = req.body as string[];
const apiKey = req.header('api-key');
const payerSecret = req.header('payer-secret-key');

if (!apiKey || !payerSecret) {
res.status(400).json({
success: false,
error: 'Missing or invalid API / Payer key'
});

return;
}

if (!payeeAddresses || !Array.isArray(payeeAddresses) || payeeAddresses.length < 1) {
res.status(400).json({
success: false,
error: 'Missing or invalid payee addresses'
});
return;
}

const wallet = await deriveWallet(apiKey, payerSecret);
let error: string | boolean = false;

try {
const tx = await removePaymentDelegationPayee({
wallet,
payeeAddresses
});

if (!tx) {
throw new Error('Failed to remove payee: delegation transaction failed');
}
} catch (err) {
console.error('Failed to remove payee', err);
error = (err as Error).toString();
}

if (error) {
res.status(500).json({
success: false,
Expand Down
2 changes: 1 addition & 1 deletion tests/routes/auth/sendTxn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ describe("sendTxn Integration Tests", () => {

// check that the txn hash is the same as the one from the client
expect(response.body.requestId).toBe(txnHashFromClient);
}, 30000);
}, 60000); // Increase timeout to 60s since we're waiting for real transactions

it("should reject transaction with invalid signature", async () => {
// Create a new random wallet
Expand Down
Loading