JP Richardson
8 years ago
committed by
GitHub
9 changed files with 5 additions and 437 deletions
@ -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 |
|||
} |
@ -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…
Reference in new issue