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:
Mục lục bài viết
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 a library
Check the test.js
for an example.
More better test suite and docs – soon.
Basically, there are few steps:
- Trigger a sign request to some Injected web browser wallet, like Metamask
- Pass that signature string to the exported method.
- It returns the
bip32
root, taproot child, the signature, and a taproot address. - Show the
taproot
address to your user. It can be used for Bitcoin Ordinals.
Basic “server-side” example
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,
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
// 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
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.