Skip to content

Commit

Permalink
nits
Browse files Browse the repository at this point in the history
  • Loading branch information
shahbaz17 committed Oct 24, 2024
1 parent 4429e21 commit 84ff540
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 13 deletions.
83 changes: 79 additions & 4 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.

3 changes: 2 additions & 1 deletion mpc-core-kit-web/mpc-core-kit-bitcoin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@web3auth/ethereum-mpc-provider": "^9.3.0",
"@web3auth/mpc-core-kit": "^3.2.4",
"@web3auth/single-factor-auth": "^9.0.0",
"axios": "^1.7.7",
"bitcoinjs-lib": "^6.1.5",
"bn.js": "^5.2.1",
"ecpair": "^2.1.0",
Expand Down Expand Up @@ -70,4 +71,4 @@
"build": "tsc && vite build",
"serve": "vite preview"
}
}
}
10 changes: 6 additions & 4 deletions mpc-core-kit-web/mpc-core-kit-bitcoin/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -524,11 +524,13 @@ function App() {
<div className="left-panel">
<div className="grid">{coreKitStatus === COREKIT_STATUS.LOGGED_IN ? loggedInView : unloggedInView}</div>
</div>
<div className="right-panel">
<div id="console" style={{ whiteSpace: "pre-line" }}>
<p style={{ whiteSpace: "pre-line" }}></p>
{coreKitStatus === COREKIT_STATUS.LOGGED_IN && (
<div className="right-panel">
<div id="console" style={{ whiteSpace: "pre-line" }}>
<p style={{ whiteSpace: "pre-line" }}></p>
</div>
</div>
</div>
)}
</div>

<footer className="footer">
Expand Down
27 changes: 23 additions & 4 deletions mpc-core-kit-web/mpc-core-kit-bitcoin/src/BitcoinComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import ECPairFactory from "ecpair";
import { networks, Psbt, payments, SignerAsync } from "bitcoinjs-lib";
import * as bitcoinjs from "bitcoinjs-lib";
import { createBitcoinJsSigner } from "./BitcoinSigner";
import axios from "axios";

const ECPair = ECPairFactory(ecc);
bitcoinjs.initEccLib(ecc);

export const BTCValidator = (pubkey: Buffer, msghash: Buffer, signature: Buffer): boolean => {
return ECPair.fromPublicKey(pubkey).verify(msghash, signature);
Expand All @@ -23,12 +25,15 @@ function uiConsole(...args: any): void {

function getAddress(signer: SignerAsync, mode: string, network: networks.Network): string | undefined {
let bufPubKey = signer.publicKey;
const xOnlyPubKey = bufPubKey.subarray(1, 33);
const keyPair = ECPair.fromPublicKey(bufPubKey);
const tweakedChildNode = keyPair.tweak(bitcoinjs.crypto.taggedHash("TapTweak", xOnlyPubKey));
if (mode === "btc") {
return payments.p2pkh({ pubkey: bufPubKey, network }).address;
} else if (mode === "segwit") {
return payments.p2wpkh({ pubkey: bufPubKey, network }).address;
// } else if (mode === 'tapRoot') {
// return payments.p2tr({ pubkey: bufPubKey, network: networks.testnet }).address!;
} else if (mode === "tapRoot") {
return payments.p2tr({ pubkey: Buffer.from(tweakedChildNode.publicKey.subarray(1, 33)), network: networks.testnet }).address!;
} else {
return undefined;
}
Expand All @@ -50,6 +55,7 @@ async function handleSendTransaction(signedTransaction: string) {
console.log(respText);
uiConsole(respText);
}

export const BitcoinComponent = (props: BitcoinComponentParams) => {
const [signer, setSigner] = useState<SignerAsync | null>(null);
const [receiverAddr, setReceiverAddr] = useState<string | null>(null);
Expand All @@ -69,6 +75,16 @@ export const BitcoinComponent = (props: BitcoinComponentParams) => {
}
}, [props.coreKitInstance, bitcoinNetwork]);

const fetchUtxos = async (address: string) => {
try {
const response = await axios.get(`https://blockstream.info/testnet/api/address/${address}/utxo`);
return response.data.filter((utxo: { status: { confirmed: boolean } }) => utxo.status.confirmed);
} catch (error) {
console.error("Error fetching UTXOs:", error);
return [];
}
};

const signTransaction = async (send?: boolean) => {
if (!signer) {
uiConsole("signer not initialized yet");
Expand Down Expand Up @@ -127,6 +143,7 @@ export const BitcoinComponent = (props: BitcoinComponentParams) => {

psbt.validateSignaturesOfInput(0, BTCValidator);
const validation = psbt.validateSignaturesOfInput(0, BTCValidator);
console.log("validation", validation);
const signedTransaction = psbt.finalizeAllInputs().extractTransaction().toHex();
uiConsole("Signed Transaction: ", signedTransaction, "Copy the above into https://blockstream.info/testnet/tx/push");
console.log(validation ? "Validated" : "failed");
Expand Down Expand Up @@ -159,7 +176,8 @@ export const BitcoinComponent = (props: BitcoinComponentParams) => {
const value = amount ? Number(amount) : 20;
const miner = Number(minerFee);

const selfAddr = await getAddress(signer, "segwitAddress", bitcoinNetwork);
const selfAddr = await getAddress(signer, "segwit", bitcoinNetwork);
console.log("TESTTTTTTTT");
console.log(selfAddr, typeof selfAddr);
console.log("receiverAddr", receiverAddr);
const psbt = new Psbt({ network: bitcoinNetwork })
Expand Down Expand Up @@ -300,8 +318,9 @@ export const BitcoinComponent = (props: BitcoinComponentParams) => {

const address = getAddress(signer, "btc", bitcoinNetwork);
const segwitAddress = getAddress(signer, "segwit", bitcoinNetwork);
// const taprootAddress = getAddress(signer, "tapRoot", bitcoinNetwork);
if (address) {
uiConsole("Address: ", address, "Segwit Address: ", segwitAddress);
uiConsole({ Address: address, SegwitAddress: segwitAddress });
} else {
uiConsole("Invalid address");
}
Expand Down

0 comments on commit 84ff540

Please sign in to comment.