From 87cb0184668678820662e9f6b79a1cf78420a523 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Tue, 8 Sep 2015 15:51:14 +1000 Subject: [PATCH] rm message module --- README.md | 5 ++- src/index.js | 1 - src/message.js | 54 ------------------------ test/ecdsa.js | 4 +- test/fixtures/message.json | 86 -------------------------------------- test/message.js | 64 ---------------------------- 6 files changed, 4 insertions(+), 210 deletions(-) delete mode 100644 src/message.js delete mode 100644 test/fixtures/message.json delete mode 100644 test/message.js diff --git a/README.md b/README.md index 0001848..d3c3cc5 100644 --- a/README.md +++ b/README.md @@ -103,8 +103,9 @@ The below examples are implemented as integration tests, they should be very eas - [Generate a address and WIF for Litecoin](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/basic.js#L30) - [Import an address via WIF](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/basic.js#L44) - [Create a Transaction](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/basic.js#L51) -- [Sign a Bitcoin message](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/advanced.js#L8) -- [Verify a Bitcoin message](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/advanced.js#L16) +- [Generate a address and WIF for Litecoin](https://github.com/bitcoin/bitcoinjs-lib/blob/master/test/integration/basic.js#L29) +- [Import an address via WIF](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/basic.js#L43) +- [Create a Transaction](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/basic.js#L50) - [Create an OP RETURN transaction](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/advanced.js#L24) - [Create a 2-of-3 multisig P2SH address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/multisig.js#L9) - [Spend from a 2-of-4 multisig P2SH address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/multisig.js#L25) diff --git a/src/index.js b/src/index.js index ed9d562..25b3da5 100644 --- a/src/index.js +++ b/src/index.js @@ -9,7 +9,6 @@ module.exports = { address: require('./address'), bufferutils: require('./bufferutils'), crypto: require('./crypto'), - message: require('./message'), networks: require('./networks'), opcodes: require('./opcodes.json'), script: require('./script') diff --git a/src/message.js b/src/message.js deleted file mode 100644 index 29fe7dc..0000000 --- a/src/message.js +++ /dev/null @@ -1,54 +0,0 @@ -var bufferutils = require('./bufferutils') -var bcrypto = require('./crypto') -var ecdsa = require('./ecdsa') -var networks = require('./networks') - -var BigInteger = require('bigi') -var ECPair = require('./ecpair') -var ECSignature = require('./ecsignature') - -function magicHash (message, network) { - var messagePrefix = new Buffer(network.messagePrefix) - var messageBuffer = new Buffer(message) - var lengthBuffer = bufferutils.varIntBuffer(messageBuffer.length) - - var buffer = Buffer.concat([messagePrefix, lengthBuffer, messageBuffer]) - return bcrypto.hash256(buffer) -} - -function sign (keyPair, message, network) { - network = network || networks.bitcoin - - var hash = magicHash(message, network) - var signature = keyPair.sign(hash) - var e = BigInteger.fromBuffer(hash) - var i = ecdsa.calcPubKeyRecoveryParam(e, signature, keyPair.Q) - - return signature.toCompact(i, keyPair.compressed) -} - -function verify (address, signature, message, network) { - if (!Buffer.isBuffer(signature)) { - signature = new Buffer(signature, 'base64') - } - - network = network || networks.bitcoin - - var hash = magicHash(message, network) - var parsed = ECSignature.parseCompact(signature) - var e = BigInteger.fromBuffer(hash) - var Q = ecdsa.recoverPubKey(e, parsed.signature, parsed.i) - - var keyPair = new ECPair(null, Q, { - compressed: parsed.compressed, - network: network - }) - - return keyPair.getAddress() === address -} - -module.exports = { - magicHash: magicHash, - sign: sign, - verify: verify -} diff --git a/test/ecdsa.js b/test/ecdsa.js index db7c8b3..971f0d3 100644 --- a/test/ecdsa.js +++ b/test/ecdsa.js @@ -3,8 +3,6 @@ var assert = require('assert') var bcrypto = require('../src/crypto') var ecdsa = require('../src/ecdsa') -var message = require('../src/message') -var networks = require('../src/networks') var sinon = require('sinon') var BigInteger = require('bigi') @@ -98,7 +96,7 @@ describe('ecdsa', function () { }) describe('with i ∈ {0,1,2,3}', function () { - var hash = message.magicHash('1111', networks.bitcoin) + var hash = new Buffer('feef89995d7575f12d65ccc9d28ccaf7ab224c2e59dad4cc7f6a2b0708d24696', 'hex') var e = BigInteger.fromBuffer(hash) var signatureBuffer = new Buffer('INcvXVVEFyIfHLbDX+xoxlKFn3Wzj9g0UbhObXdMq+YMKC252o5RHFr0/cKdQe1WsBLUBi4morhgZ77obDJVuV0=', 'base64') diff --git a/test/fixtures/message.json b/test/fixtures/message.json deleted file mode 100644 index efedb4d..0000000 --- a/test/fixtures/message.json +++ /dev/null @@ -1,86 +0,0 @@ -{ - "valid": { - "magicHash": [ - { - "network": "bitcoin", - "message": "", - "magicHash": "80e795d4a4caadd7047af389d9f7f220562feb6196032e2131e10563352c4bcc" - }, - { - "network": "bitcoin", - "message": "Vires is Numeris", - "magicHash": "f8a5affbef4a3241b19067aa694562f64f513310817297089a8929a930f4f933" - }, - { - "network": "dogecoin", - "message": "Vires is Numeris", - "magicHash": "c0963d20d0accd0ea0df6c1020bf85a7e629a40e7b5363f2c3e9dcafd5638f12" - } - ], - "verify": [ - { - "message": "vires is numeris", - "network": "bitcoin", - "address": "16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM", - "signature": "G8JawPtQOrybrSP1WHQnQPr67B9S3qrxBrl1mlzoTJOSHEpmnF7D3+t+LX0Xei9J20B5AIdPbeL3AaTBZ4N3bY0=", - "compressed": { - "address": "1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs", - "signature": "H8JawPtQOrybrSP1WHQnQPr67B9S3qrxBrl1mlzoTJOSHEpmnF7D3+t+LX0Xei9J20B5AIdPbeL3AaTBZ4N3bY0=" - } - }, - { - "message": "vires is numeris", - "network": "dogecoin", - "address": "DFpN6QqFfUm3gKNaxN6tNcab1FArL9cZLE", - "signature": "H6k+dZwJ8oOei3PCSpdj603fDvhlhQ+sqaFNIDvo/bI+Xh6zyIKGzZpyud6YhZ1a5mcrwMVtTWL+VXq/hC5Zj7s=" - } - ], - "signing": [ - { - "description": "gives equal r, s values irrespective of point compression", - "message": "vires is numeris", - "network": "bitcoin", - "d": "1", - "signature": "HF8nHqFr3K2UKYahhX3soVeoW8W1ECNbr0wfck7lzyXjCS5Q16Ek45zyBuy1Fiy9sTPKVgsqqOuPvbycuVSSVl8=", - "compressed": { - "signature": "IF8nHqFr3K2UKYahhX3soVeoW8W1ECNbr0wfck7lzyXjCS5Q16Ek45zyBuy1Fiy9sTPKVgsqqOuPvbycuVSSVl8=" - } - }, - { - "description": "supports alternative networks", - "message": "vires is numeris", - "network": "dogecoin", - "d": "1", - "signature": "G6k+dZwJ8oOei3PCSpdj603fDvhlhQ+sqaFNIDvo/bI+Xh6zyIKGzZpyud6YhZ1a5mcrwMVtTWL+VXq/hC5Zj7s=" - } - ] - }, - "invalid": { - "verify": [ - { - "description": "will fail for the wrong message", - "message": "foobar", - "address": "16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM", - "signature": "G8JawPtQOrybrSP1WHQnQPr67B9S3qrxBrl1mlzoTJOSHEpmnF7D3+t+LX0Xei9J20B5AIdPbeL3AaTBZ4N3bY0=" - }, - { - "description": "will fail for the wrong address", - "message": "vires is numeris", - "address": "1111111111111111111114oLvT2", - "signature": "H8JawPtQOrybrSP1WHQnQPr67B9S3qrxBrl1mlzoTJOSHEpmnF7D3+t+LX0Xei9J20B5AIdPbeL3AaTBZ4N3bY0=" - }, - { - "description": "does not cross verify (uncompressed address, compressed signature)", - "message": "vires is numeris", - "address": "16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM", - "signature": "H8JawPtQOrybrSP1WHQnQPr67B9S3qrxBrl1mlzoTJOSHEpmnF7D3+t+LX0Xei9J20B5AIdPbeL3AaTBZ4N3bY0=" - }, - { - "description": "does not cross verify (compressed address, uncompressed signature)", - "message": "vires is numeris", - "address": "1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs", - "signature": "G8JawPtQOrybrSP1WHQnQPr67B9S3qrxBrl1mlzoTJOSHEpmnF7D3+t+LX0Xei9J20B5AIdPbeL3AaTBZ4N3bY0=" - } - ] - } -} diff --git a/test/message.js b/test/message.js deleted file mode 100644 index fb116b0..0000000 --- a/test/message.js +++ /dev/null @@ -1,64 +0,0 @@ -/* global describe, it */ - -var assert = require('assert') -var message = require('../src/message') -var networks = require('../src/networks') - -var BigInteger = require('bigi') -var ECPair = require('../src/ecpair') - -var fixtures = require('./fixtures/message.json') - -describe('message', function () { - describe('magicHash', function () { - fixtures.valid.magicHash.forEach(function (f) { - it('produces the correct magicHash for "' + f.message + '" (' + f.network + ')', function () { - var network = networks[f.network] - var actual = message.magicHash(f.message, network) - - assert.strictEqual(actual.toString('hex'), f.magicHash) - }) - }) - }) - - describe('verify', function () { - fixtures.valid.verify.forEach(function (f) { - it('verifies a valid signature for "' + f.message + '" (' + f.network + ')', function () { - var network = networks[f.network] - - assert(message.verify(f.address, f.signature, f.message, network)) - - if (f.compressed) { - assert(message.verify(f.compressed.address, f.compressed.signature, f.message, network)) - } - }) - }) - - fixtures.invalid.verify.forEach(function (f) { - it(f.description, function () { - assert(!message.verify(f.address, f.signature, f.message)) - }) - }) - }) - - describe('signing', function () { - fixtures.valid.signing.forEach(function (f) { - it(f.description, function () { - var network = networks[f.network] - - var keyPair = new ECPair(new BigInteger(f.d), null, { - compressed: false - }) - var signature = message.sign(keyPair, f.message, network) - assert.strictEqual(signature.toString('base64'), f.signature) - - if (f.compressed) { - var compressedPrivKey = new ECPair(new BigInteger(f.d)) - var compressedSignature = message.sign(compressedPrivKey, f.message) - - assert.strictEqual(compressedSignature.toString('base64'), f.compressed.signature) - } - }) - }) - }) -})