How to sign data with app private key


#1

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?


#2

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?


#3

@mikecohen.id,

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: https://github.com/ntheile/blockusign/issues/58

if you use the app there is a nice visual of it - https://blockusign.co/


#4

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


#5

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

and in the blockstack service


#6

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);

#7

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


Sharing data between different entities