Skip to content

Commit

Permalink
[wip] fix mpc taproot example
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasgeihs committed Dec 12, 2024
1 parent a6b1465 commit e3bf213
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 49 deletions.
68 changes: 31 additions & 37 deletions mpc-core-kit-web/mpc-core-kit-bitcoin/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions mpc-core-kit-web/mpc-core-kit-bitcoin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@vitejs/plugin-react": "^4.3.1",
"@web3auth/base": "^9.4.5",
"@web3auth/ethereum-mpc-provider": "^9.4.5",
"@web3auth/mpc-core-kit": "^4.1.0-alpha.0",
"@web3auth/mpc-core-kit": "file:web3auth-mpc-core-kit-4.0.0-alpha.1.tgz",
"axios": "^1.7.7",
"bitcoinjs-lib": "^6.1.5",
"bn.js": "^5.2.1",
Expand Down Expand Up @@ -70,4 +70,4 @@
"build": "tsc && vite build",
"serve": "vite preview"
}
}
}
2 changes: 1 addition & 1 deletion mpc-core-kit-web/mpc-core-kit-bitcoin/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ let evmProvider: EthereumSigningProvider;
if (typeof window !== "undefined") {
coreKitInstance = new Web3AuthMPCCoreKit({
web3AuthClientId,
web3AuthNetwork: WEB3AUTH_NETWORK.MAINNET,
web3AuthNetwork: WEB3AUTH_NETWORK.DEVNET,
storage: window.localStorage,
manualSync: true,
tssLib: tssLibFrostBip340, // tssLibDkls | tssLibFrostBip340 - Taproot only
Expand Down
12 changes: 7 additions & 5 deletions mpc-core-kit-web/mpc-core-kit-bitcoin/src/BitcoinComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const ECPair = ECPairFactory(ecc);
bitcoinjs.initEccLib(ecc);

const BTCValidator = (pubkey: Buffer, msghash: Buffer, signature: Buffer): boolean => {
return ECPair.fromPublicKey(pubkey).verify(msghash, signature);
return ecc.verifySchnorr(Uint8Array.from(msghash), Uint8Array.from(pubkey), Uint8Array.from(signature));
};

const uiConsole = (...args: any): void => {
Expand Down Expand Up @@ -77,7 +77,9 @@ export const BitcoinComponent: React.FC<BitcoinComponentProps> = ({ coreKitInsta

const fetchUtxos = async (address: string) => {
try {
const response = await axios.get(`https://blockstream.info/testnet/api/address/${address}/utxo`);
const url = `https://blockstream.info/testnet/api/address/${address}/utxo`;
console.log(url);
const response = await axios.get(url);
return response.data.filter((utxo: { status: { confirmed: boolean } }) => utxo.status.confirmed);
} catch (error) {
console.error("Error fetching UTXOs:", error);
Expand Down Expand Up @@ -128,9 +130,9 @@ export const BitcoinComponent: React.FC<BitcoinComponentProps> = ({ coreKitInsta
const maxFee = Math.max(...Object.values(feeResponse.data as Record<string, number>));
const fee = Math.ceil(maxFee * 1.2); // Adding 20% buffer to the fee

if (utxo.value <= fee) {
throw new Error(`Insufficient funds: ${utxo.value} satoshis <= ${fee} satoshis (estimated fee)`);
}
// if (utxo.value <= fee) {
// throw new Error(`Insufficient funds: ${utxo.value} satoshis <= ${fee} satoshis (estimated fee)`);
// }

const sendAmount = amount ? parseInt(amount) : utxo.value - fee;

Expand Down
15 changes: 11 additions & 4 deletions mpc-core-kit-web/mpc-core-kit-bitcoin/src/BitcoinSigner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as bitcoinjs from "bitcoinjs-lib";
import ECPairFactory from "ecpair";

import ecc from "@bitcoinerlab/secp256k1";
import BN from "bn.js";

const ECPair = ECPairFactory(ecc);

Expand All @@ -21,20 +22,26 @@ export function createBitcoinJsSigner(props: { coreKitInstance: Web3AuthMPCCoreK
}


export function createBitcoinJsSignerBip340(props: { coreKitInstance: Web3AuthMPCCoreKit; network: networks.Network }): SignerAsync {
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 {
const tweak = bitcoinjs.crypto.taggedHash("TapTweak", xOnlyPubKey);
const tweakedChildNode = keyPair.tweak(tweak);
const pk = tweakedChildNode.publicKey;

// const pk = props.coreKitInstance.getPubKeyPoint().toSEC1(secp256k1, true);
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);
const keyTweak = new BN(tweak);
let sig = await props.coreKitInstance.sign(msg, { keyTweak });
return sig;
},
publicKey: tweakedChildNode.publicKey,
publicKey: pk,
network: props.network,
};
}
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit e3bf213

Please sign in to comment.