Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support re-redeeming when the counterparty rbfs the initiates in bitcoin #54

Merged
merged 2 commits into from
Oct 23, 2024
Merged
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
2 changes: 0 additions & 2 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,3 @@ npmRegistries:
npmScopes:
gardenfi:
npmRegistryServer: 'https://registry.npmjs.org/'

yarnPath: .yarn/releases/yarn-4.3.1.cjs
28 changes: 27 additions & 1 deletion packages/core/src/lib/bitcoin/htlc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
// Amount to initiate the HTLC
private initiateAmount: number;

// UTXO hashes which will be used instead of fetching from the rpcs
private utxoHashes?: string[];

/**
* Note: redeemerAddress and initiatorAddress should be x-only public key without 02 or 03 prefix
*/
Expand All @@ -53,6 +56,7 @@
initiatorPubkey: string,
expiry: number,
network: bitcoin.networks.Network,
utxoHashes?: string[],
) {
this.secretHash = secretHash;
this.redeemerPubkey = redeemerPubkey;
Expand All @@ -62,6 +66,7 @@
this.network = network;
this.internalPubkey = generateInternalkey();
this.initiateAmount = initiateAmount;
this.utxoHashes = utxoHashes;
}

/**
Expand All @@ -84,6 +89,7 @@
initiatorPubkey: string,
redeemerPubkey: string,
expiry: number,
utxoHashes?: string[],
): Promise<GardenHTLC> {
// trim 0x prefix if present
secretHash = secretHash.startsWith('0x') ? secretHash.slice(2) : secretHash;
Expand All @@ -101,7 +107,7 @@
);
assert(expiry > 0, htlcErrors.zeroOrNegativeExpiry);

const network = await signer.getNetwork();

Check failure on line 110 in packages/core/src/lib/bitcoin/htlc.ts

View workflow job for this annotation

GitHub Actions / test

src/lib/bitcoin/htlc.spec.ts > htlc > test

TypeError: signer.getNetwork is not a function ❯ Function.from src/lib/bitcoin/htlc.ts:110:34 ❯ src/lib/bitcoin/htlc.spec.ts:14:35
return new GardenHTLC(
signer,
initiateAmount,
Expand All @@ -110,6 +116,7 @@
xOnlyPubkey(initiatorPubkey).toString('hex'),
expiry,
network,
utxoHashes,
);
}

Expand Down Expand Up @@ -138,7 +145,26 @@

const address = this.address();
const provider = await this.signer.getProvider();
const utxos = await provider.getUTXOs(address);

let utxos: BitcoinUTXO[] = [];
if (this.utxoHashes && this.utxoHashes.length > 0) {
for (const utxoHash of this.utxoHashes) {
const tx = await provider.getTransaction(utxoHash);
for (let i = 0; i < tx.vout.length; i++) {
const vout = tx.vout[i];
if (vout.scriptpubkey_address === address) {
utxos.push({
txid: tx.txid,
vout: i,
value: vout.value,
status: { confirmed: false },
});
}
}
}
} else {
utxos = await provider.getUTXOs(address);
}
const balance = utxos.reduce((acc, utxo) => acc + utxo.value, 0);
if (balance === 0) throw new Error(`${address} ${htlcErrors.notFunded}`);

Expand Down
5 changes: 3 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ __metadata:
"@catalogfi/utils": "npm:^0.1.6"
"@catalogfi/wallets": "npm:^0.2.50"
"@gardenfi/orderbook": "workspace:^"
"@gardenfi/utils": "workspace:^"
bitcoinjs-lib: "npm:^6.1.6"
dotenv: "npm:^16.3.1"
tiny-secp256k1: "npm:^2.2.3"
Expand Down Expand Up @@ -8065,11 +8066,11 @@ __metadata:

"typescript@patch:typescript@npm%3A^5.2.2#optional!builtin<compat/typescript>, typescript@patch:typescript@npm%3A^5.4.2#optional!builtin<compat/typescript>, typescript@patch:typescript@npm%3A^5.5.3#optional!builtin<compat/typescript>":
version: 5.6.3
resolution: "typescript@patch:typescript@npm%3A5.6.3#optional!builtin<compat/typescript>::version=5.6.3&hash=8c6c40"
resolution: "typescript@patch:typescript@npm%3A5.6.3#optional!builtin<compat/typescript>::version=5.6.3&hash=379a07"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
checksum: 10c0/7c9d2e07c81226d60435939618c91ec2ff0b75fbfa106eec3430f0fcf93a584bc6c73176676f532d78c3594fe28a54b36eb40b3d75593071a7ec91301533ace7
checksum: 10c0/ac8307bb06bbfd08ae7137da740769b7d8c3ee5943188743bb622c621f8ad61d244767480f90fbd840277fbf152d8932aa20c33f867dea1bb5e79b187ca1a92f
languageName: node
linkType: hard

Expand Down
Loading