Skip to content

Instantly share code, notes, and snippets.

Created March 14, 2018 16:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/356d589687ed41180944a935a5dad1b7 to your computer and use it in GitHub Desktop.
Save anonymous/356d589687ed41180944a935a5dad1b7 to your computer and use it in GitHub Desktop.
Generate proof-of-public-key, for use with the Teikhos authentication method
const ethUtils = require('ethereumjs-util');
const EthCrypto = require('eth-crypto');
const xor = require('buffer-xor')
const keccak = require('keccakjs')
const identity = EthCrypto.createIdentity();
const privateKey = new Buffer(identity.privateKey.slice(2), 'hex')
console.log("privateKey:")
console.log(privateKey.toString('hex'))
var publicKey = ethUtils.privateToPublic(privateKey)
console.log("Public Key: ")
console.log(publicKey.toString('hex'))
var address = ethUtils.pubToAddress(publicKey)
console.log("Address: ")
console.log(address.toString('hex'))
var keccakHash = new keccak() // uses 512 bits by default
keccakHash.update(publicKey)
var keyHashKeccak = new Buffer(keccakHash.digest('hex'), 'hex')
console.log("sha3(publicKey):")
console.log(keyHashKeccak.toString('hex'))
var msgHash = ethUtils.hashPersonalMessage(publicKey)
console.log("Message Hash: ")
console.log(msgHash.toString('hex'))
var sig = ethUtils.ecsign(msgHash, privateKey)
var signature = new Buffer(ethUtils.toRpcSig(sig.v, sig.r, sig.s).slice(2, -2), 'hex')
console.log("Signature: ")
console.log(signature.toString('hex'))
console.log(sig.r.toString('hex'))
var proof_of_public_key = xor(signature, keyHashKeccak)
console.log("Proof-Of-Public-Key: ")
console.log(proof_of_public_key.toString('hex'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment