Skip to content

How to sign Bitcoin transaction via KeyChain

Dmitry edited this page Mar 31, 2019 · 4 revisions

Here you can find an instruction on how to sign a Bitcoin transaction with KeyChain.

1. Download and install KeyChain for macOS

2. Select a key

Start with the command `wscat -c ws://localhost:16384/, then select a key.

{ 
  "command": "select_key"
}

3. Calculate the address from the publicKey

const bitcoin = require ('bitcoinjs-lib');
const publicKey = 'YOUR_PUBLIC_KEY'; // 08d6770d8219923fe25a4d6aeb2c171253d5de3bc225f09dbfb2cb93ed837be1a80fdd3af5046b8f1f5412e5b321dcc3c25be9f4dd285250421ea55071794277
const publicKeyCompressed = Buffer.from(`03${publicKey.substr(0, 64)}`, 'hex');
const keyPair = bitcoin.ECPair.fromPublicKeyBuffer(publicKeyCompressed, bitcoin.networks.testnet);
const address = keyPair.getAddress();

4. Transfer money to the address corresponding to the public key

In case you work with testnet - https://coinfaucet.eu/en/btc-testnet/

5. Check the balance on the address - it should have enough BTC for a successful transfer.

const fetch = require('node-fetch');
const API_URL = 'https://test-insight.bitpay.com/api';
const address = 'YOUR_ADDRESS'; // myLrbwvwJN59quivKSxCxgiqLdCw8m7aDf
fetch(`${API_URL}/addr/${address}`)
  .then( data => data.json() )
  .then(({ balance }) => console.log('BTC Balance: ', balance));

6. Sign transaction with the key that you have generated

You can find an example of the code here