How to sign data with app private key

Hi, I have some user data and I want to be able to sign and verify with the users app private key. Very much welcome pointers on how to go about this. I’m trying for example…

var bitcoin = require('bitcoinjs-lib')
var bitcoinMessage = require('bitcoinjs-message')

const bytes = Buffer.from(appprivkey)
const privkeyBs58 = bs58.encode(bytes)
var keyPair = bitcoin.ECPair.fromWIF(privkeyBs58)
var privateKey = keyPair.privateKey
var signature = bitcoinMessage.sign(message, privkey, keyPair.compressed)

where this fails to get valid base 58 encoding of the private key - e.g.

var keyPair = bitcoin.ECPair.fromWIF(privkeyBs58)

throws ‘Invalid Checksum’

Is this close / a valid use case?

1 Like

Ah - think I just found the answer from bottom of this this post.

  var privKeyWith01 = account.appPrivateKey + '01'
  let privkey = hexStringToECPair(privKeyWith01).toWIF()

and then

var keyPair = bitcoin.ECPair.fromWIF(privkey)
var privateKey = keyPair.privateKey
var signature = bitcoinMessage.sign(message, privateKey, keyPair.compressed)

looks like it creates a valid signature but any general comments on this approach will be helpful?

1 Like,

I implemented this in my dapp, Blockusign. I wanted to to prove a real person with a human identity signed a document hash ( preferable with attestations to twitter/facebook etc… ). I wanted it to link back to the users blockstack id, not just a public key. Then I wanted to persist this immutably forever via an anchor of that data to bitcoin using Blockstack subdomains ( $$$$ much cheaper) I anchor twice a day, costing only a few cents per day at the current BTC transaction cost!

code is here:

design thoughts here:

if you use the app there is a nice visual of it -

1 Like

Thanks Nick - will take a deeper look - good to know about your project!

No prob mike. Also there is some helper code in the bitcoin service:

and in the blockstack service

1 Like

Thanks Nick for pointing out a live example!

It’s also a little easier to just use blockstack.js and not have to use the Bitcoin libs:

import { signECDSA, verifyECDSA } from 'blockstack/lib/encryption';
import { loadUserData } from 'blockstack/lib/auth/authApp';

// this returns an object like { signature: string, publicKey: string }
const sign = (message) => {
  const { appPrivateKey } = loadUserData();
  return signECDSA(appPrivateKey, message);

// returns true or false
const verify = (message, signedObject) => {
  return verifyECDSA(message, signedObject.publicKey, signedObject.signature);

const message = 'hello, world';
const signature = sign(message);
const valid = verify(messaged, signature);

Thanks Hank. I was wondering where you guys put that in blockstack.js!