Skip to content

Commit

Permalink
fix: use tweaked public key
Browse files Browse the repository at this point in the history
  • Loading branch information
ieow committed Dec 4, 2024
1 parent 029a1a3 commit a6b1465
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
11 changes: 8 additions & 3 deletions mpc-core-kit-web/mpc-core-kit-bitcoin/src/BitcoinComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ecc from "@bitcoinerlab/secp256k1";
import ECPairFactory from "ecpair";
import { networks, Psbt, payments, SignerAsync } from "bitcoinjs-lib";
import * as bitcoinjs from "bitcoinjs-lib";
import { createBitcoinJsSigner } from "./BitcoinSigner";
import { createBitcoinJsSigner, createBitcoinJsSignerBip340 } from "./BitcoinSigner";
import axios from "axios";
import { BlurredLoading } from "./Loading";

Expand Down Expand Up @@ -62,7 +62,7 @@ const handleSendTransaction = async (signedTransaction: string) => {

export const BitcoinComponent: React.FC<BitcoinComponentProps> = ({ coreKitInstance }) => {
const [signer, setSigner] = useState<SignerAsync | null>(null);
const [receiverAddr, setReceiverAddr] = useState<string>("");
const [receiverAddr, setReceiverAddr] = useState<string>("tb1ph9cxmts2r8z56mfzyhem74pep0kfz2k0pc56uhujzx0c3v2rrgssx8zc5q");
const [amount, setAmount] = useState<string>("");
const [isLoading, setIsLoading] = useState<boolean>(false);

Expand Down Expand Up @@ -108,6 +108,8 @@ export const BitcoinComponent: React.FC<BitcoinComponentProps> = ({ coreKitInsta
const keyPair = ECPair.fromPublicKey(bufPubKey);
const tweakedChildNode = keyPair.tweak(bitcoinjs.crypto.taggedHash("TapTweak", xOnlyPubKey));

console.log("tweakedChildNode.publicKey", tweakedChildNode.publicKey);
console.log("bip340", coreKitInstance.getPubKeyBip340());
const account =
transactionType === "PSBT"
? payments.p2pkh({ pubkey: signer.publicKey, network: bitcoinNetwork })
Expand Down Expand Up @@ -162,6 +164,8 @@ export const BitcoinComponent: React.FC<BitcoinComponentProps> = ({ coreKitInsta
});
}

console.log("psbt.txInputs[0]", psbt.data.inputs);

psbt.addOutput({
address: receiverAddr || account.address!,
value: sendAmount,
Expand All @@ -174,7 +178,8 @@ export const BitcoinComponent: React.FC<BitcoinComponentProps> = ({ coreKitInsta
} else if (transactionType === "Segwit") {
await psbt.signAllInputsAsync(signer);
} else if (transactionType === "Taproot") {
await psbt.signInputAsync(0, signer);
const signerBip340 = createBitcoinJsSignerBip340({ coreKitInstance, network: bitcoinNetwork });
await psbt.signInputAsync(0, signerBip340);
}

const isValid = psbt.validateSignaturesOfInput(0, BTCValidator);
Expand Down
24 changes: 23 additions & 1 deletion mpc-core-kit-web/mpc-core-kit-bitcoin/src/BitcoinSigner.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
import { secp256k1 } from "@tkey/common-types";
import { Web3AuthMPCCoreKit } from "@web3auth/mpc-core-kit";
import { networks, SignerAsync } from "bitcoinjs-lib";
import * as bitcoinjs from "bitcoinjs-lib";
import ECPairFactory from "ecpair";

import ecc from "@bitcoinerlab/secp256k1";

const ECPair = ECPairFactory(ecc);

export function createBitcoinJsSigner(props: { coreKitInstance: Web3AuthMPCCoreKit; network: networks.Network }): SignerAsync {
return {
sign: async (msg: Buffer) => {
let sig = await props.coreKitInstance.sign(msg);
return sig;
},

publicKey: props.coreKitInstance.getPubKeyPoint().toSEC1(secp256k1, true),
network: props.network,
};
}


export function createBitcoinJsSignerBip340(props: { coreKitInstance: Web3AuthMPCCoreKit; network: networks.Network }): SignerAsync {
const bufPubKey = props.coreKitInstance.getPubKeyPoint().toSEC1(secp256k1, true);
const xOnlyPubKey = bufPubKey.subarray(1, 33);
const keyPair = ECPair.fromPublicKey(bufPubKey);
const tweakedChildNode = keyPair.tweak(bitcoinjs.crypto.taggedHash("TapTweak", xOnlyPubKey)); return {
sign: async (msg: Buffer) => {
let sig = await props.coreKitInstance.sign(msg);
return sig;
},
signSchnorr: async (msg: Buffer) => {
let sig = await props.coreKitInstance.sign(msg);
return sig;
},
publicKey: props.coreKitInstance.getPubKeyPoint().toSEC1(secp256k1, true),
publicKey: tweakedChildNode.publicKey,
network: props.network,
};
}

0 comments on commit a6b1465

Please sign in to comment.