Skip to content

Commit

Permalink
add redeem
Browse files Browse the repository at this point in the history
  • Loading branch information
Revantark committed Jun 13, 2024
1 parent 95b6fb2 commit df64701
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 139 deletions.
1 change: 1 addition & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodeLinker: node-modules
1 change: 1 addition & 0 deletions bitcoin/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const LEAF_VERSION = 0xc0;
22 changes: 19 additions & 3 deletions bitcoin/internalKey.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sha256 } from "bitcoinjs-lib/src/crypto";
import { sha256, taggedHash } from "bitcoinjs-lib/src/crypto";
import { toXOnly } from "bitcoinjs-lib/src/psbt/bip371";
import * as ecc from "tiny-secp256k1";

Expand All @@ -18,16 +18,32 @@ const H = Buffer.from(
"hex"
);

const errors = {
failedToCreateInternalPubkey: "failed to create internal pubkey",
failedToTweakPubkey: "failed to tweak pubkey",
};

export function generateInternalkey() {
const hash = sha256(Buffer.from("GardenHTLC", "utf-8"));
const R = ecc.pointMultiply(Buffer.concat([Buffer.from("04", "hex"), G]), hash);

if (!R) {
throw new Error("Could not create R");
throw new Error(errors.failedToCreateInternalPubkey);
}

const internalPubKey = ecc.pointAdd(H, R);
if (!internalPubKey) throw new Error(errors.failedToCreateInternalPubkey);

if (!internalPubKey) throw new Error("Could not create internal pubkey");
return toXOnly(Buffer.from(internalPubKey));
}

export function tweakPubkey(pubkey: Buffer, hash: Buffer) {
const tweak = taggedHash("TapTweak", Buffer.concat([pubkey, hash]));
const tweakedPubKey = ecc.xOnlyPointAddTweak(pubkey, tweak);

if (!tweakedPubKey) {
throw new Error(errors.failedToTweakPubkey);
}

return tweakedPubKey;
}
26 changes: 21 additions & 5 deletions bitcoin/script.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import { expect } from "chai";
import { GardenHTLC } from "./script";
import { randomBytes } from "ethers";
import { sha256 } from "bitcoinjs-lib/src/crypto";
import { hash160, sha256 } from "bitcoinjs-lib/src/crypto";
import { BitcoinNetwork, BitcoinProvider, BitcoinWallet } from "@catalogfi/wallets";
import { regtest } from "bitcoinjs-lib/src/networks";
import { regTestUtils } from "./regtest";

describe("Test", () => {
it("should pass", async () => {
describe("Bitcoin GardenHTLC", () => {
it("should be able initiate and redeem", async () => {
const provider = new BitcoinProvider(BitcoinNetwork.Regtest, "http://localhost:30000");
const alice = BitcoinWallet.createRandom(provider);
const bob = BitcoinWallet.createRandom(provider);

const secret = randomBytes(32);
const secretHash = sha256(Buffer.from(secret)).toString("hex");
const redeemer = await bob.getAddress();
const initiator = await alice.getAddress();
// TODO: write utils functions to parse pubkeys as bip340 compliant
const redeemer = hash160(
Buffer.from((await bob.getPublicKey()).slice(2), "hex")
).toString("hex");
const initiator = hash160(
Buffer.from((await alice.getPublicKey()).slice(2), "hex")
).toString("hex");

const expiry = 7200;

const htlc = new GardenHTLC(secretHash, redeemer, initiator, expiry, regtest);
Expand All @@ -24,5 +30,15 @@ describe("Test", () => {
expect(address).to.be.a("string");

await regTestUtils.fund(address, provider);

const hash = await htlc.redeem(Buffer.from(secret).toString("hex"), bob, provider);

expect(hash).to.be.a("string");
console.log(hash);

const tx = await provider.getTransaction(hash);
expect(tx).to.be.an("object");
expect(tx.txid).to.be.eq(hash);
expect(tx.vout[0].scriptpubkey_address).to.be.equal(address);
});
});
Loading

0 comments on commit df64701

Please sign in to comment.