commit cc9f108bb3de1ef922a6a9a310b3ac96f128020c Author: Sven Slootweg Date: Sat Oct 14 01:01:31 2017 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aa1f911 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +bundle.js +node_modules diff --git a/index.html b/index.html new file mode 100644 index 0000000..1698244 --- /dev/null +++ b/index.html @@ -0,0 +1,11 @@ + + + + + Ethereum Signing Test + + + + (check the console) + + diff --git a/index.js b/index.js new file mode 100644 index 0000000..3bc7da2 --- /dev/null +++ b/index.js @@ -0,0 +1,35 @@ +'use strict'; + +// ecsign (messageHash, privateKey) -> {r, s, v} +// ecrecover (messageHash, v, r, s) -> publicKey +// sha3 (message, bits) -> messageHash +// privateToPublic (privateKey) -> publicKey + +const crypto = require("crypto"); +const ethereumUtil = require("ethereumjs-util"); + +let privateKey = crypto.randomBytes(32); +let publicKey = ethereumUtil.privateToPublic(privateKey); + +console.log("-- Key data:"); +console.log("Private key: (hex)", privateKey.toString("hex")); +console.log("Public key: (hex)", publicKey.toString("hex")); + +let message = "hello world!"; +let messageHash = ethereumUtil.sha3(message); + +console.log("-- Message:"); +console.log("Plaintext:", message); +console.log("Hash: (hex)", messageHash.toString("hex")); + +let signature = ethereumUtil.ecsign(messageHash, privateKey); +let recoveredPublicKey = ethereumUtil.ecrecover(messageHash, signature.v, signature.r, signature.s); + +console.log("-- Signing:"); +console.log("Signature:"); +console.log(" v:", signature.v); +console.log(" r: (hex)", signature.r.toString("hex")); +console.log(" s: (hex)", signature.s.toString("hex")); +console.log("Recovered public key: (hex)", recoveredPublicKey.toString("hex")); + +console.log("\nPublic keys match:", (publicKey.compare(recoveredPublicKey) === 0)); diff --git a/package.json b/package.json new file mode 100644 index 0000000..71000cd --- /dev/null +++ b/package.json @@ -0,0 +1,23 @@ +{ + "name": "ethereum-signing-test", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "build": "browserify index.js -o bundle.js" + }, + "repository": { + "type": "git", + "url": "git@git.cryto.net:joepie91/ethereum-signing-test.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "devDependencies": { + "browserify": "^14.4.0" + }, + "dependencies": { + "ethereumjs-util": "^5.1.2" + } +}