npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@double-spent/sbtc-react

v0.1.0-pre.3

Published

Integrate sBTC deposits and withdrawals in React apps with minimal configuration and an intuitive API.

Downloads

3

Readme

What's inside?

This package allows to easily implement sBTC deposits and withdrawals into React apps. The provided functions and helpers provide an interface to the on-chain contracts deployed on mainnet, testnet, and devnet.

Usage

Installation

npm i @double-spent/sbtc-react

Set up providers

The SbtcProvider component provides context to the hooks included in this package. Add an instance of SbtcProvider at the top of the component tree — this would be App.tsx in create-react-app or the Root Layout in Next.js.

This package is not tied to a particular wallet, so the SbtcProvider requires the callbacks used to sign messages and PSBTs to be passed as props. The example below uses the Leather Wallet.

// App.tsx

export default function App() {
  // Load user data from a connected wallet

  const user = userSession.loadUserData();

  // Define a callback used to sign PSBTs with Leather

  const signPsbt = async (request) => {
    return await (window as any).btc.request('signPsbt', request).result.hex;
  };

  // Define a callback used to sign messages with Leather

  const signMessage = async ({ message, stacksNetwork }) => {
    return await new Promise((resolve) => {
      openSignatureRequestPopup({
        message,
        userSession,
        network: stacksNetwork,
        onFinish: (data) => {
          resolve(data.signature);
        },
      });
    });
  };

  return (
    <>
      <SbtcProvider user={user} network={SbtcNetwork.TESTNET} signMessage={signMessage} signPsbt={signPsbt}>
        {/* rest of app goes here .. */}
      </SbtcProvider>
    </>
  );
}

The SbtcProvider component takes the following props:

| Prop | Type | Description | | ------------- | ----------------------------------------------------------- | ------------------------------------ | | user | UserData? | The connected wallet's user, if any. | | network | SbtcNetwork | The network to use. | | signPsbt | SbtcSignPsbtCallback | The callback used to sign PSBTs. | | signMessage | SbtcSignMessageCallback | The callback used to sign messages. |

Submit an sBTC deposit

The useSubmitSbtcDeposit hook returns a callback used to submit sBTC deposits. It requires an instance of SbtcProvider present in the component tree to retrieve common configuration, like the connected Stacks user session. As the example above, triggering a deposit will open Leather to request signing the PSBT (partially-signed Bitcoin transaction) to send the BTC to the bridge and receive sBTC.

See the @double-spent/sbtc-core for more details.

// Deposit.tsx

export function Deposit() {
  const [satsAmount, setSatsAmount] = useState(0);
  const [transactionHash, setTransactionHash] = useState<string | undefined>();
  const { submitSbtcDeposit } = useSubmitSbtcDeposit();

  const handleChange = (event) => {
    setSatsAmount(parseInt(event.target.value));
  };

  const handleSubmit = async () => {
    const { btcTransactionHash } = await submitSbtcDeposit(satsAmount);
    setTransactionHash(btcTransactionHash);
  };

  if (transactionHash) {
    return (
      <div>
        <h4>Deposit submitted!</h4>
        <p>
          Transaction hash:
          <a href={`https://blockstream.info/testnet/tx/${transactionHash}`} target="_blank">
            {transactionHash}
          </a>
        </p>
      </div>
    );
  }

  return (
    <div>
      <h4>Deposit sBTC</h4>
      <input name="amount" onChange={handleChange} />
      <button onClick={handleSubmit}>Deposit</button>
    </div>
  );
}

Sign and submit an sBTC withdrawal

The useSignSbtcWithdrawal and useSubmitSbtcWithdrawal hooks return callbacks to sign and submit sBTC withdrawals respectively. They require an instance of SbtcProvider present in the component tree to retrieve common configuration, like the connected Stacks user session. As the example above, calling either hook will open Leather to request signing the withdrawal and the PSBT (partially-signed Bitcoin transaction) to send the sBTC to the bridge and receive BTC.

See the @double-spent/sbtc-core for more details.

// Withdraw.tsx

export function Withdraw() {
  const [satsAmount, setSatsAmount] = useState(0);
  const [transactionHash, setTransactoinHash] = useState<string | undefined>();

  const { signSbtcWithdrawal } = useSignSbtcWithdrawal();
  const { submitSbtcDeposit } = useSubmitSbtcWithdrawal();

  const handleChange = (event) => {
    setSatsAmount(parseInt(event.target.value));
  };

  const handleSubmit = async () => {
    const signature = await signSbtcWithdrawal(satsAmount);
    const transactionHash = await submitSbtcWithdrawal(satsAmount, signature);

    setTransactionHash(transactionHash);
  };

  if (transactinHash) {
    return (
      <div>
        <h4>Withdrawal submitted!</h4>
        <p>
          Transaction hash:
          <a href={`https://blockstream.info/testnet/tx/${transactionHash}`} target="_blank">
            {transactionHash}
          </a>
        </p>
      </div>
    );
  }

  return (
    <div>
      <h4>Withdraw sBTC</h4>
      <input name="amount" onChange={handleChange} />
      <button onClick={handleSubmit}>Deposit</button>
    </div>
  );
}

Supported features

| Feature | Mode | Status | | ------------- | ----------- | ------------ | | sBTC Devnet | | ✅ Supported | | sBTC Testnet | | ✅ Supported | | sBTC Mainnet | | 🟡 Pending | | Deposit sBTC | OP_RETURN | ✅ Supported | | Withdraw sBTC | OP_RETURN | ✅ Supported | | Deposit sBTC | OP_DROP | 🟡 Pending | | Withdraw sBTC | OP_DROP | 🟡 Pending |

References