UNPKG

bitcoin-connect

Version:

Derive Bitcoin Taproot address, from an Ethereum signature. For example, a signature generated by your Injected Web3 wallet through the `personal_sign` RPC call. Useful for Bitcoin Ordinals.

142 lines (91 loc) 4.72 kB
# bitcoin-connect > Derive Bitcoin Taproot address, from an Ethereum signature. For example, a signature generated by your Injected Web3 wallet through the `personal_sign` RPC call. Useful for Bitcoin Ordinals. For demo check out the hosted API and UI on https://bitcoin-connect.deno.dev. Tweets about it: - https://twitter.com/wgw_eth/status/1644576875845496833 - https://twitter.com/wgw_eth/status/1644557972889018369 ## Install ``` npm install bitcoin-connect ``` For Deno, use the `npm` protocol, eg. `npm:bitcoin-connect`. **It's recommended though**, to use the `https://esm.sh/bitcoin-connect` because some of the bitcoin libraries that are used my not be resolved properly. I'll definitely make smaller ESM-only alternatives in the future. ## Table of Contents - [As Library](#as-a-library) - [API Usage](#api-usage) - [Use as a user](#ui-usage) ![image](https://user-images.githubusercontent.com/5038030/230706652-cbc02d1a-a7f2-4c76-ac0b-cde27650ac74.png) ### As a library Check the `test.js` for an example. More better test suite and docs - soon. Basically, there are few steps: 1. Trigger a sign request to some Injected web browser wallet, like Metamask 2. Pass that signature string to the exported method. 3. It returns the `bip32` root, taproot child, the signature, and a taproot address. 4. Show the `taproot` address to your user. It can be used for Bitcoin Ordinals. Basic "server-side" example ```js import { generateTaprootAddressFromSignature } from "bitcoin-connect"; const result = generateTaprootAddressFromSignature("0xSignature here"); console.log(result); console.log(result.taprootAddress); ``` This is also useful as a Serverless function, which I'll deploy soon. It's also useful if you want to generate the same wallet address as the one that Generative.xyz's website is generating for your Ethereum Address. I follow how they are doing it, but externalized it so there's no UI or React things. If you can generate Ethereum signature in some other way, we include their message for convenience on `TAPROOT_MESSAGE` export. For example, ```js import { TAPROOT_MESSAGE, generateTaprootAddressFromSignature, } from "bitcoin-connect"; const signature = requestEthereumPersonalSign(TAPROOT_MESSAGE); const { taprootAddress } = generateTaprootAddressFromSignature(signature); // you should get the same address as the one when you connect your wallet // to the generative.xyz site by clicking Wallet there. console.log(taprootAddress); ``` ### API Usage The API generates you a bitcoin taproot address, given an Ethereum/Web3 signature. It accepts both POST and GET requests to the root domain, e.g. `/`. Try this https://bitcoin-connect.deno.dev/?signature=0x849cdb2c6ab66269d95e219ab99d12762c6caad49f6b0fa569289935c21179242747640b69be801f8660b17cba85bb37e8897be01830c6442b95fd6bc69038991b That's my actual Bitcoin taproot wallet (that can be used on https://generative.xyz), generated from my Ethereum address (0xA20c...5002 / wgw.eth). You can verify that at https://etherscan.io/verifySig/16514 You can also pass the `signature` to a POST request body ```js // generate this signature somehow, // eg. using `personal_sign` of Metamask, or another (Injected browser) wallet const sig = `0x849cdb2c6ab66269d95e219ab99d12762c6caad49f6b0fa569289935c21179242747640b69be801f8660b17cba85bb37e8897be01830c6442b95fd6bc69038991b`; const res = await fetch("https://bitcoin-connect.deno.dev", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ signature: sig }), }); const { error, data } = await res.json(); const { address, signature, message } = data; constole.log({ error, address, signature, message }); ``` or a `GET` request example ```js const sig = `0x849cdb2c6ab66269d95e219ab99d12762c6caad49f6b0fa569289935c21179242747640b69be801f8660b17cba85bb37e8897be01830c6442b95fd6bc69038991b`; const res = await fetch("https://bitcoin-connect.deno.dev/?signature" + sig); const { error, data } = await res.json(); const { address, signature, message } = data; constole.log({ error, address, signature, message }); ``` _(for more check the source code in `api/index.ts`)_ For development ``` yarn dev # or from the `api` folder # deno run -A --watch index.ts ``` For production ``` yarn deploy # or from the `api` folder # deployctl deploy --project=bitcoin-connect index.ts ``` ### UI usage TODO: More docs on the site, explaining everything and the API As an example, you can check the `api/index.html` to see how Metamask/web3 wallet signing is implemented in 30 lines of code. No need for libraries, no need for MEGABYTES of stuff _on the client side_. Ethereum Web3 is so awful it's mind-blowing.