Skip to content

Commit

Permalink
feat: WIP - working fetch funding tx (leather)
Browse files Browse the repository at this point in the history
  • Loading branch information
kskulikova committed Nov 20, 2024
1 parent d3097db commit d37000e
Show file tree
Hide file tree
Showing 6 changed files with 2,148 additions and 731 deletions.
54 changes: 38 additions & 16 deletions netlify/functions/fetch-deposit-psbt-leather.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Handler } from '@netlify/functions';
import { bytesToHex } from '@noble/hashes/utils';
import { SoftwareWalletDLCHandler } from 'dlc-btc-lib';
import { BitcoinCoreRpcConnection, RawVault } from 'dlc-btc-lib/models';
import { BitcoinCoreRpcConnection, JSONFriendlyRawVault } from 'dlc-btc-lib/models';
import { shiftValue } from 'dlc-btc-lib/utilities';

const handler: Handler = async (event, context) => {
Expand Down Expand Up @@ -37,15 +38,15 @@ const handler: Handler = async (event, context) => {
};
}

const body = JSON.parse(event.body);
const dlcHandler = JSON.parse(body) as SoftwareWalletDLCHandler;
const rawVault = JSON.parse(vault) as RawVault;
console.log('INSIDE FETCH DEPOSIT TX NETLIFY FUNCTION');
const dlcHandler = SoftwareWalletDLCHandler.fromJSON(event.body);
console.log('AFTER FETCHING DLC HANDLER: ', JSON.stringify(dlcHandler));
const rawVault = JSONFriendlyRawVault.rawVaultFromJSON(vault);

console.log('MY SoftwareWalletDLCHandler INSIDE NETLIFY: ', dlcHandler);
const bitcoincoreRpcHost = process.env.BITCOINCORE_RPC_HOST;
const bitcoincoreRpcPort = Number(process.env.BITCOINCORE_RPC_PORT);
const bitcoincoreRpcUser = process.env.BITCOINCORE_RPC_USER;
const bitcoincoreRpcPassword = process.env.BITCOINCORE_RPC_PW;
const bitcoincoreRpcHost = process.env.VITE_BITCOINCORE_RPC_HOST;
const bitcoincoreRpcPort = Number(process.env.VITE_BITCOINCORE_RPC_PORT);
const bitcoincoreRpcUser = process.env.VITE_BITCOINCORE_RPC_USER;
const bitcoincoreRpcPassword = process.env.VITE_BITCOINCORE_RPC_PW;

if (
!bitcoincoreRpcHost ||
Expand All @@ -61,10 +62,6 @@ const handler: Handler = async (event, context) => {
};
}

console.log('HOST: ', bitcoincoreRpcHost);
console.log('PORT: ', bitcoincoreRpcPort);
console.log('USER: ', bitcoincoreRpcUser);

const bitcoincoreRpcConnection = new BitcoinCoreRpcConnection(
bitcoincoreRpcHost,
bitcoincoreRpcUser,
Expand All @@ -81,11 +78,36 @@ const handler: Handler = async (event, context) => {
Number(feeRateMultiplier)
);

const depositPayment = dlcHandler.payment;
if (!depositPayment) {
return {
statusCode: 500,
body: JSON.stringify({
message: 'Payment is required',
}),
};
}

// return {
// statusCode: 200,
// body: Buffer.from(depositPSBT.toPSBT()).toString('hex'),
// };
const hexPSBT = bytesToHex(depositPSBT.toPSBT());

// Construct the JSON response
const response = {
psbt: hexPSBT,
script: bytesToHex(depositPayment.fundingPayment.script),
};

console.log('fetch deposit tx NETLIFY RESPONSE: ', response);
// Return as JSON
return {
statusCode: 200,
body: JSON.stringify({
message: depositPSBT,
}),
body: JSON.stringify(response),
headers: {
'Content-Type': 'application/json',
},
};
};

Expand Down
29 changes: 7 additions & 22 deletions netlify/functions/fetch-funding-psbt-leather.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Handler } from '@netlify/functions';
import { bytesToHex } from '@noble/hashes/utils';
import { SoftwareWalletDLCHandler } from 'dlc-btc-lib';
import { BitcoinCoreRpcConnection, JSONFriendlyRawVault, RawVault } from 'dlc-btc-lib/models';
import { BitcoinCoreRpcConnection, JSONFriendlyRawVault } from 'dlc-btc-lib/models';
import { shiftValue } from 'dlc-btc-lib/utilities';

const handler: Handler = async (event, context) => {
Expand Down Expand Up @@ -38,17 +39,13 @@ const handler: Handler = async (event, context) => {
}

console.log('INSIDE FETCH FUNDING TX NETLIFY FUNCTION');
console.log('request body is: ', event.body);
const dlcHandler = SoftwareWalletDLCHandler.fromJSON(event.body);
console.log('vault JSON: ', vault);

const rawVault = JSONFriendlyRawVault.rawVaultFromJSON(vault);
console.log('Raw vault from JSON: ', rawVault);

const bitcoincoreRpcHost = process.env.BITCOINCORE_RPC_HOST;
const bitcoincoreRpcPort = Number(process.env.BITCOINCORE_RPC_PORT);
const bitcoincoreRpcUser = process.env.BITCOINCORE_RPC_USER;
const bitcoincoreRpcPassword = process.env.BITCOINCORE_RPC_PW;
const bitcoincoreRpcHost = process.env.VITE_BITCOINCORE_RPC_HOST;
const bitcoincoreRpcPort = Number(process.env.VITE_BITCOINCORE_RPC_PORT);
const bitcoincoreRpcUser = process.env.VITE_BITCOINCORE_RPC_USER;
const bitcoincoreRpcPassword = process.env.VITE_BITCOINCORE_RPC_PW;

if (
!bitcoincoreRpcHost ||
Expand All @@ -64,11 +61,6 @@ const handler: Handler = async (event, context) => {
};
}

console.log('HOST: ', bitcoincoreRpcHost);
console.log('PORT: ', bitcoincoreRpcPort);
console.log('USER: ', bitcoincoreRpcUser);
console.log('PW: ', bitcoincoreRpcPassword);

const bitcoincoreRpcConnection = new BitcoinCoreRpcConnection(
bitcoincoreRpcHost,
bitcoincoreRpcUser,
Expand All @@ -77,23 +69,16 @@ const handler: Handler = async (event, context) => {
);
dlcHandler.setBitcoinCoreRpcConnection(bitcoincoreRpcConnection);

// console.log('Software Wallet DLC Handler: ', dlcHandler);
// console.log('Is handler the right type? : ', dlcHandler instanceof SoftwareWalletDLCHandler);

const fundingPSBT = await dlcHandler?.createFundingPSBT(
rawVault,
BigInt(shiftValue(Number(bitcoinAmount))),
attestorGroupPublicKey,
Number(feeRateMultiplier)
);

console.log('NETLIFY Funding PSBT: ', fundingPSBT);
const jsonString = JSON.stringify(fundingPSBT, (key, value) =>
typeof value === 'bigint' ? value.toString() : value
);
return {
statusCode: 200,
body: jsonString,
body: bytesToHex(fundingPSBT.toPSBT()),
};
};

Expand Down
68 changes: 45 additions & 23 deletions netlify/functions/fetch-withdrawal-psbt-leather.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Handler } from '@netlify/functions';
import { SoftwareWalletDLCHandler } from 'dlc-btc-lib';
import { RawVault } from 'dlc-btc-lib/models';
import { BitcoinCoreRpcConnection, JSONFriendlyRawVault, RawVault } from 'dlc-btc-lib/models';
import { shiftValue } from 'dlc-btc-lib/utilities';

const handler: Handler = async (event, context) => {
Expand All @@ -13,25 +13,21 @@ const handler: Handler = async (event, context) => {
};
}

let {
dlcHandler,
vault,
withdrawAmount,
attestorGroupPublicKey,
feeRateMultiplier,
fundingTxId,
} = event.queryStringParameters;
if (!event.body) {
return {
statusCode: 400,
body: JSON.stringify({
message: 'Body is required',
}),
};
}

let { vault, withdrawAmount, attestorGroupPublicKey, feeRateMultiplier, fundingTxId } =
event.queryStringParameters;

console.log(event.multiValueQueryStringParameters);

if (
!dlcHandler ||
!vault ||
!withdrawAmount ||
!attestorGroupPublicKey ||
!feeRateMultiplier ||
!fundingTxId
) {
if (!vault || !withdrawAmount || !attestorGroupPublicKey || !feeRateMultiplier || !fundingTxId) {
return {
statusCode: 400,
body: JSON.stringify({
Expand All @@ -41,10 +37,38 @@ const handler: Handler = async (event, context) => {
};
}

const dlcHandlerObj = JSON.parse(dlcHandler) as SoftwareWalletDLCHandler;
const rawVault = JSON.parse(vault) as RawVault;
console.log('INSIDE FETCH WITHDRAWAL TX NETLIFY FUNCTION');
const dlcHandler = SoftwareWalletDLCHandler.fromJSON(event.body);
const rawVault = JSONFriendlyRawVault.rawVaultFromJSON(vault);

const bitcoincoreRpcHost = process.env.VITE_BITCOINCORE_RPC_HOST;
const bitcoincoreRpcPort = Number(process.env.VITE_BITCOINCORE_RPC_PORT);
const bitcoincoreRpcUser = process.env.VITE_BITCOINCORE_RPC_USER;
const bitcoincoreRpcPassword = process.env.VITE_BITCOINCORE_RPC_PW;

if (
!bitcoincoreRpcHost ||
!bitcoincoreRpcPort ||
!bitcoincoreRpcUser ||
!bitcoincoreRpcPassword
) {
return {
statusCode: 500,
body: JSON.stringify({
message: 'Bitcoincore rpc connection parameters are required',
}),
};
}

const bitcoincoreRpcConnection = new BitcoinCoreRpcConnection(
bitcoincoreRpcHost,
bitcoincoreRpcUser,
bitcoincoreRpcPassword,
bitcoincoreRpcPort
);
dlcHandler.setBitcoinCoreRpcConnection(bitcoincoreRpcConnection);

const withdrawalPSBT = await dlcHandlerObj?.createWithdrawPSBT(
const withdrawalPSBT = await dlcHandler?.createWithdrawPSBT(
rawVault,
BigInt(shiftValue(Number(withdrawAmount))),
attestorGroupPublicKey,
Expand All @@ -54,9 +78,7 @@ const handler: Handler = async (event, context) => {

return {
statusCode: 200,
body: JSON.stringify({
message: withdrawalPSBT,
}),
body: Buffer.from(withdrawalPSBT.toPSBT()).toString('hex'),
};
};

Expand Down
Loading

0 comments on commit d37000e

Please sign in to comment.