Guides
Guide

Connect to the Bitcoin Blockchain with Magic

2021-09-29

Digital Gold Meets Decentralized Auth

Bitcoin is a new way of creating, holding, and sending digital money. It has the potential to change the world because instead of banks and governments having control of the money / Bitcoin cryptocurrency, it is the people who own this power. Thanks to its core technology being a decentralized, peer-to-peer network, Bitcoin could be owned by everyone and spent anywhere. Because Bitcoin has a supply cap of 21 million, it can also be considered the digital version of gold.

Magic is focused on building future-proof authentication by first paving the path towards a passwordless future with a plug and play auth SDK, and then by enabling self-sovereign digital identities with decentralized authentication. With Magic, both web2 and web3 developers can onboard users using only their emails. When the user signs in, Magic generates a public and private key pair to represent their digital identity.

With Magic, developers can connect to the Bitcoin blockchain by simply specifying the network URL when initiating a Magic instance. This guide will cover the key parts needed to create a simple app that allows users to send Bitcoin transactions.

Getting Started

Let's clone the official repo of the sample app we'll be following throughout this guide.

git clone magiclabs/example-bitcoin

Configure

First, let's understand the libraries we've imported into our App.js file.

  • The Magic SDK helps us enable passwordless authentication: import { Magic } from "magic-sdk";
  • Magic’s Bitcoin Extension helps us interact with the Bitcoin network: import { BitcoinExtension } from "@magic-ext/bitcoin";
  • BitcoinJS helps us code with Bitcoin in JavaScript: import * as bitcoin from 'bitcoinjs-lib'

Initialize

Now, initialize the Magic SDK with your app's Publishable API Key and pass in your network config.

import { Magic } from 'magic-sdk';
import { BitcoinExtension } from "@magic-ext/bitcoin";

const magic = new Magic('YOUR_API_KEY', {
  extensions: [
    new BitcoinExtension({
      rpcUrl: 'BTC_RPC_NODE_URL', // Node provider service you’re using
      network: 'testnet' // Magic creates a testnet or mainnet API keys for you
    }),
  ],
});

Core Functions

Here are the core functions we'll need to create an experience where the user can log into the app, send BTC, and log out.

login()

The login() function logs users into the app using their email address.

const login = async () => {
    await magic.auth.loginWithMagicLink({ email });
    setIsLoggedIn(true);
};

logout()

The logout() function logs out the currently authenticated Magic user.

const logout = async () => {
    await magic.user.logout();
    setIsLoggedIn(false);
};

handleSignTransaction()

Once the authenticated user has shared their input transaction hash (a reference to an output from a previous transaction), and has specified the amount of BTC they want to send, as well as to whom they would like to send it to, they can then sign the transaction with the Magic's handleSignTransaction() function.

const handleSignTransaction = async () => {
    // Magic supports testnet or mainnet network
    const TESTNET = bitcoin.networks.testnet;
    setSendingTransaction(true);

    // If TransactionBuilder's parameter is empty, default network is mainnet
    const tx = new bitcoin.TransactionBuilder(TESTNET);
    // You'll need to grab the input hash & put it into the transaction
    tx.addInput(inputTxHash, 0);

    // Who to send BTC to and how much
    tx.addOutput(destinationAddress, sendAmount);

    const txHex = tx.buildIncomplete().toHex();

    // Sign transaction with user's Magic Secret Key
    const signedTransactionHex = await magic.bitcoin.signTransaction(txHex, 0);

    setSendingTransaction(false);

    setTxHash(signedTransactionHex);

    console.log("signed transaction", signedTransactionHex);
    
    // Add more logic to send transaction to Bitcoin blockchain
};

Ready to give Magic a try?

Now users are able to log in with a familiar web2 experience and sign a Bitcoin transaction! Easy, huh? Here's the sample app, and a link to the docs.

As of now, Magic only supports the basic send-and-receive type of transaction. If you want to see Magic support other types of transactions, feel free to join our community and let us know!

Let's make some magic!