rm message module
This commit is contained in:
parent
fd40232147
commit
87cb018466
6 changed files with 4 additions and 210 deletions
|
@ -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)
|
- [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)
|
- [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)
|
- [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)
|
- [Generate a address and WIF for Litecoin](https://github.com/bitcoin/bitcoinjs-lib/blob/master/test/integration/basic.js#L29)
|
||||||
- [Verify a Bitcoin message](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/advanced.js#L16)
|
- [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 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)
|
- [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)
|
- [Spend from a 2-of-4 multisig P2SH address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/multisig.js#L25)
|
||||||
|
|
|
@ -9,7 +9,6 @@ module.exports = {
|
||||||
address: require('./address'),
|
address: require('./address'),
|
||||||
bufferutils: require('./bufferutils'),
|
bufferutils: require('./bufferutils'),
|
||||||
crypto: require('./crypto'),
|
crypto: require('./crypto'),
|
||||||
message: require('./message'),
|
|
||||||
networks: require('./networks'),
|
networks: require('./networks'),
|
||||||
opcodes: require('./opcodes.json'),
|
opcodes: require('./opcodes.json'),
|
||||||
script: require('./script')
|
script: require('./script')
|
||||||
|
|
|
@ -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
|
|
||||||
}
|
|
|
@ -3,8 +3,6 @@
|
||||||
var assert = require('assert')
|
var assert = require('assert')
|
||||||
var bcrypto = require('../src/crypto')
|
var bcrypto = require('../src/crypto')
|
||||||
var ecdsa = require('../src/ecdsa')
|
var ecdsa = require('../src/ecdsa')
|
||||||
var message = require('../src/message')
|
|
||||||
var networks = require('../src/networks')
|
|
||||||
var sinon = require('sinon')
|
var sinon = require('sinon')
|
||||||
|
|
||||||
var BigInteger = require('bigi')
|
var BigInteger = require('bigi')
|
||||||
|
@ -98,7 +96,7 @@ describe('ecdsa', function () {
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('with i ∈ {0,1,2,3}', 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 e = BigInteger.fromBuffer(hash)
|
||||||
|
|
||||||
var signatureBuffer = new Buffer('INcvXVVEFyIfHLbDX+xoxlKFn3Wzj9g0UbhObXdMq+YMKC252o5RHFr0/cKdQe1WsBLUBi4morhgZ77obDJVuV0=', 'base64')
|
var signatureBuffer = new Buffer('INcvXVVEFyIfHLbDX+xoxlKFn3Wzj9g0UbhObXdMq+YMKC252o5RHFr0/cKdQe1WsBLUBi4morhgZ77obDJVuV0=', 'base64')
|
||||||
|
|
86
test/fixtures/message.json
vendored
86
test/fixtures/message.json
vendored
|
@ -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="
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
Loading…
Add table
Reference in a new issue