Skip to content

Commit

Permalink
Merge pull request #44 from near-examples/bitcoin
Browse files Browse the repository at this point in the history
Bitcoin: changes to get segwit bitcoin transactions working
  • Loading branch information
gagdiez authored Dec 2, 2024
2 parents 41867eb + 6789fc8 commit a21bf19
Show file tree
Hide file tree
Showing 6 changed files with 597 additions and 217 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"rxjs": "^7.8.1",
"secp256k1": "3.4.0",
"viem": "^2.18.4",
"vite-plugin-node-polyfills": "^0.21.0",
"web3": "^4.6.0"
Expand Down
26 changes: 17 additions & 9 deletions src/components/Bitcoin.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,31 @@ import { Bitcoin as Bitcoin } from "../services/bitcoin";
import { useDebounce } from "../hooks/debounce";
import PropTypes from 'prop-types';

const BTC_NETWORK = 'testnet';
const BTC = new Bitcoin('https://blockstream.info/testnet/api', BTC_NETWORK);
const BTC = Bitcoin;

export function BitcoinView({ props: { setStatus, MPC_CONTRACT } }) {
export function BitcoinView({ props: { setStatus, MPC_CONTRACT, transactions } }) {
const { wallet, signedAccountId } = useContext(NearContext);

const [receiver, setReceiver] = useState("tb1q86ec0aszet5r3qt02j77f3dvxruk7tuqdlj0d5");
const [amount, setAmount] = useState(1000);
const [loading, setLoading] = useState(false);
const [step, setStep] = useState("request");
const [step, setStep] = useState(transactions.length ? "relay" : "request");
const [signedTransaction, setSignedTransaction] = useState(null);
const [senderAddress, setSenderAddress] = useState("")
const [senderPK, setSenderPK] = useState("")

const [derivation, setDerivation] = useState("bitcoin-1");
const derivationPath = useDebounce(derivation, 500);

const getSignedTx = async () => {
const signedTx = await wallet.getTransactionResult(transactions[0])
console.log('signedTx', signedTx)
setSignedTransaction(signedTx)
}

useEffect(() => {
if (transactions.length) getSignedTx()
}, [transactions])

useEffect(() => {
setSenderAddress('Waiting for you to stop typing...')
Expand All @@ -37,22 +46,21 @@ export function BitcoinView({ props: { setStatus, MPC_CONTRACT } }) {
setSenderAddress(address);
setSenderPK(publicKey);

const balance = await BTC.getBalance(address);
const balance = await BTC.getBalance({ address });
setStatus(`Your Bitcoin address is: ${address}, balance: ${balance} satoshi`);
}
}, [signedAccountId, derivationPath, setStatus]);

async function chainSignature() {
setStatus('🏗️ Creating transaction');
const payload = await BTC.createPayload(senderAddress, receiver, amount);

setStatus('🕒 Asking MPC to sign the transaction, this might take a while...');
try {
const signedTransaction = await BTC.requestSignatureToMPC(wallet, MPC_CONTRACT, derivationPath, payload, senderPK);
const signedTransaction = await BTC.getSignature({ from: senderAddress, publicKey: senderPK, to: receiver, amount, path: derivationPath, wallet });
setStatus('✅ Signed payload ready to be relayed to the Bitcoin network');
setSignedTransaction(signedTransaction);
setStep('relay');
} catch (e) {
console.log(e)
setStatus(`❌ Error: ${e.message}`);
setLoading(false);
}
Expand All @@ -63,7 +71,7 @@ export function BitcoinView({ props: { setStatus, MPC_CONTRACT } }) {
setStatus('🔗 Relaying transaction to the Bitcoin network... this might take a while');

try {
const txHash = await BTC.relayTransaction(signedTransaction);
const txHash = await BTC.broadcast({ from: senderAddress, publicKey: senderPK, to: receiver, amount, path: derivationPath, sig: signedTransaction });
setStatus(
<>
<a href={`https://blockstream.info/testnet/tx/${txHash}`} target="_blank"> ✅ Successful </a>
Expand Down
Loading

0 comments on commit a21bf19

Please sign in to comment.