<p>I'm trying to sign the message to UniSat to this <a href="https://docs.unisat.io/dev/unisat-developer-center/unisat-marketplace/brc20-marketplace" rel="nofollow noreferrer">endpoint</a>
<code>Bind btcAddress and nftAddress</code>.</p>
<p>Using code like:</p>
<pre><code>import * as bitcoin from 'bitcoinjs-lib';
import * as ecc from 'tiny-secp256k1';
import * as bitcoinMessage from 'bitcoinjs-message';
import { ECPairFactory } from 'ecpair';
import * as crypto from 'crypto';
import * as secp256k1 from 'tiny-secp256k1';

const ECPair = ECPairFactory(ecc);


function signMessage(privateKeyWIF: string, message: string): string {
    const ecPair = ECPair.fromWIF(privateKeyWIF, bitcoin.networks.bitcoin);
    const privateKey = ecPair.privateKey;
    const compressed = ecPair.compressed;

    if (!privateKey) {
        throw new Error('Private key is missing');
    }

    // Sign the message
    const signature = bitcoinMessage.sign(message, privateKey, compressed);

    // Encode the signature in base64
    const signatureCompact = signature.slice(1, 65).toString('hex');

    return signatureCompact;
}

const message: string = `Please confirm that\nPayment Address: ${paymentAddress}\nOrdinals Address: ${ordinalsAddress}`;

const signature: string = signMessage(privateKey, message);
</code></pre>
<p>But at the response receiving:</p>
<pre><code>{
  btcAddress: 'bc1q5fqwlf88dtu9qa2jkvwhjr94k07cerfau3mh9s',
  nftAddress: 'bc1p7n462ru9wtfgclwedfgp0eqe6w40pjwt7wvkxgdmzue2y6ptdkvsuzzmu2',
  sign: '732505d8a18002ec9b16600558457a1695c7b5b7b93b0001f566953fe8052c8b1d82d25d8233d675a8c22bacaa5858dab37b623d7fcccf5d58d8a80580d704af'
}
{
  code: -1,
  msg: 'Signature.fromCompact: Expected 64-byte hex',
  data: null
}
</code></pre>
<p>What am I doing wrong?</p>
<p>Thank you for your help.</p>

CC BY-SA 4.0
2024-09-17T20:00:48.023Z
What is the purpose of `signature.slice(1, 65)`?
Vojtěch Strnad
CC BY-SA 4.0
2024-09-17T20:02:12.453Z
@Vojtěch Strnad 
Hello, because UniSat endpoint expecting sign with 64-byte hex.
dill
CC BY-SA 4.0
2024-09-23T14:27:45.940Z
The signature should already be 64 bytes. If its not the library has some issues. I would try remove this slice operation. If you are trying to remove the "0x" prefix you would want to slice after toString()
Arminsdev
<bitcoin-core><javascript><bitcoinjs>
How to sign a message with privateKey on Bitcoin using TS/JS?
dill
# How to sign a message with privateKey on Bitcoin using TS/JS?

(Asked by: username_0 on 2024-09-17)

I'm trying to sign the message to UniSat to this [endpoint](https://docs.unisat.io/dev/unisat-developer-center/unisat-marketplace/brc20-marketplace)
`Bind btcAddress and nftAddress`.

Using code like:

```
import * as bitcoin from 'bitcoinjs-lib';
import * as ecc from 'tiny-secp256k1';
import * as bitcoinMessage from 'bitcoinjs-message';
import { ECPairFactory } from 'ecpair';
import * as crypto from 'crypto';
import * as secp256k1 from 'tiny-secp256k1';

const ECPair = ECPairFactory(ecc);

function signMessage(privateKeyWIF: string, message: string): string {
    const ecPair = ECPair.fromWIF(privateKeyWIF, bitcoin.networks.bitcoin);
    const privateKey = ecPair.privateKey;
    const compressed = ecPair.compressed;

    if (!privateKey) {
        throw new Error('Private key is missing');
    }

    // Sign the message
    const signature = bitcoinMessage.sign(message, privateKey, compressed);

    // Encode the signature in base64
    const signatureCompact = signature.slice(1, 65).toString('hex');

    return signatureCompact;
}

const message: string = `Please confirm that\nPayment Address: ${paymentAddress}\nOrdinals Address: ${ordinalsAddress}`;

const signature: string = signMessage(privateKey, message);

```

But at the response receiving:

```
{
  btcAddress: 'bc1q5fqwlf88dtu9qa2jkvwhjr94k07cerfau3mh9s',
  nftAddress: 'bc1p7n462ru9wtfgclwedfgp0eqe6w40pjwt7wvkxgdmzue2y6ptdkvsuzzmu2',
  sign: '732505d8a18002ec9b16600558457a1695c7b5b7b93b0001f566953fe8052c8b1d82d25d8233d675a8c22bacaa5858dab37b623d7fcccf5d58d8a80580d704af'
}
{
  code: -1,
  msg: 'Signature.fromCompact: Expected 64-byte hex',
  data: null
}

```

What am I doing wrong?

Thank you for your help.

**Tags:** bitcoin-core, javascript, bitcoinjs

> **Comments:**
> **username_1**: What is the purpose of `signature.slice(1, 65)`?  
> **username_0**: @username_1  Hello, because UniSat endpoint expecting sign with 64-byte hex.  
> **username_2**: The signature should already be 64 bytes. If its not the library has some issues. I would try remove this slice operation. If you are trying to remove the "0x" prefix you would want to slice after toString()  

---

## 0 answers

