Wei Lu
11 years ago
9 changed files with 298 additions and 184 deletions
@ -1,95 +1,104 @@ |
|||||
var assert = require('assert') |
var assert = require('assert') |
||||
|
var crypto = require('../src/crypto') |
||||
|
var sec = require('../src/sec') |
||||
|
var ecparams = sec('secp256k1') |
||||
|
|
||||
|
var BigInteger = require('bigi') |
||||
|
var ECPointFp = require('../src/ec').ECPointFp |
||||
var ECPubKey = require('../src/ecpubkey') |
var ECPubKey = require('../src/ecpubkey') |
||||
|
|
||||
|
var fixtures = require('./fixtures/ecpubkey') |
||||
|
|
||||
describe('ECPubKey', function() { |
describe('ECPubKey', function() { |
||||
describe('toBuffer/toHex', function() { |
var Q |
||||
var hcpub = '024b12d9d7c77db68388b6ff7c89046174c871546436806bcd80d07c28ea811992' |
|
||||
var hpub = '044b12d9d7c77db68388b6ff7c89046174c871546436806bcd80d07c28ea81199283fbec990dad6fb98f93f712d50cb874dd717de6a184158d63886dda3090f566' |
beforeEach(function() { |
||||
|
var curve = ecparams.getCurve() |
||||
|
|
||||
|
Q = new ECPointFp( |
||||
|
curve, |
||||
|
curve.fromBigInteger(new BigInteger(fixtures.Q.x)), |
||||
|
curve.fromBigInteger(new BigInteger(fixtures.Q.y)) |
||||
|
) |
||||
|
}) |
||||
|
|
||||
it('using toHex should support compression', function() { |
describe('constructor', function() { |
||||
var pub = ECPubKey.fromHex(hcpub) |
it('defaults to compressed', function() { |
||||
|
var pubKey = new ECPubKey(Q) |
||||
|
|
||||
assert.equal(pub.toHex(), hcpub) |
assert.equal(pubKey.compressed, true) |
||||
assert.equal(pub.compressed, true) |
|
||||
}) |
}) |
||||
|
|
||||
it('using toHex should support uncompressed', function() { |
it('supports the uncompressed flag', function() { |
||||
var pub = ECPubKey.fromHex(hpub) |
var pubKey = new ECPubKey(Q, false) |
||||
|
|
||||
assert.equal(pub.toHex(), hpub) |
assert.equal(pubKey.compressed, false) |
||||
assert.equal(pub.compressed, false) |
}) |
||||
|
}) |
||||
|
|
||||
|
describe('fromHex/toHex', function() { |
||||
|
it('supports compressed points', function() { |
||||
|
var pubKey = ECPubKey.fromHex(fixtures.compressed.hex) |
||||
|
|
||||
|
assert(pubKey.Q.equals(Q)) |
||||
|
assert.equal(pubKey.toHex(), fixtures.compressed.hex) |
||||
|
assert.equal(pubKey.compressed, true) |
||||
|
}) |
||||
|
|
||||
|
it('supports uncompressed points', function() { |
||||
|
var pubKey = ECPubKey.fromHex(fixtures.uncompressed.hex) |
||||
|
|
||||
|
assert(pubKey.Q.equals(Q)) |
||||
|
assert.equal(pubKey.toHex(), fixtures.uncompressed.hex) |
||||
|
assert.equal(pubKey.compressed, false) |
||||
}) |
}) |
||||
}) |
}) |
||||
|
|
||||
describe('getAddress', function() { |
describe('getAddress', function() { |
||||
var privkeys = [ |
it('calculates the expected hash (compressed)', function() { |
||||
'ca48ec9783cf3ad0dfeff1fc254395a2e403cbbc666477b61b45e31d3b8ab458', |
var pubKey = new ECPubKey(Q, true) |
||||
'1111111111111111111111111111111111111111111111111111111111111111', |
var address = pubKey.getAddress() |
||||
'18e14a7b6a307f426a94f8114701e7c8e774e7f9a47e2c2035db29a206321725' |
|
||||
] |
assert.equal(address.hash.toString('hex'), fixtures.compressed.hash160) |
||||
|
|
||||
// compressed pubkeys
|
|
||||
var cpubkeys = [ |
|
||||
'024b12d9d7c77db68388b6ff7c89046174c871546436806bcd80d07c28ea811992', |
|
||||
'034f355bdcb7cc0af728ef3cceb9615d90684bb5b2ca5f859ab0f0b704075871aa', |
|
||||
'0250863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352' |
|
||||
] |
|
||||
|
|
||||
var pubkeys = cpubkeys.map(function(x) { |
|
||||
var pk = ECPubKey.fromHex(x) |
|
||||
pk.compressed = false |
|
||||
return pk.toHex() |
|
||||
}) |
}) |
||||
|
|
||||
it('bitcoin', function() { |
it('calculates the expected hash (uncompressed)', function() { |
||||
var addresses = [ |
var pubKey = new ECPubKey(Q, false) |
||||
'19SgmoUj4xowEjwtXvNAtYTAgbvR9iBCui', |
var address = pubKey.getAddress() |
||||
'1MsHWS1BnwMc3tLE8G35UXsS58fKipzB7a', |
|
||||
'16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM' |
assert.equal(address.hash.toString('hex'), fixtures.uncompressed.hash160) |
||||
] |
}) |
||||
var compressedAddresses = [ |
|
||||
'1AA4sjKW2aUmbtN3MtegdvhYtDBbDEke1q', |
it('supports alternative networks', function() { |
||||
'1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9', |
var pubKey = new ECPubKey(Q) |
||||
'1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs', |
var address = pubKey.getAddress(0x09) |
||||
] |
|
||||
|
assert.equal(address.version, 0x09) |
||||
for (var i = 0; i < addresses.length; ++i) { |
assert.equal(address.hash.toString('hex'), fixtures.compressed.hash160) |
||||
var pub = ECPubKey.fromHex(pubkeys[i]) |
|
||||
var cpub = ECPubKey.fromHex(cpubkeys[i]) |
|
||||
cpub.compressed = true |
|
||||
|
|
||||
var addr = addresses[i] |
|
||||
var caddr = compressedAddresses[i] |
|
||||
|
|
||||
assert.equal(pub.getAddress().toString(), addr) |
|
||||
assert.equal(cpub.getAddress().toString(), caddr) |
|
||||
} |
|
||||
}) |
}) |
||||
|
}) |
||||
|
|
||||
|
describe('verify', function() { |
||||
|
var pubKey, signature |
||||
|
beforeEach(function() { |
||||
|
pubKey = new ECPubKey(Q) |
||||
|
|
||||
it('testnet', function() { |
signature = { |
||||
var addresses = [ |
r: new BigInteger(fixtures.signature.r), |
||||
'19SgmoUj4xowEjwtXvNAtYTAgbvR9iBCui', |
s: new BigInteger(fixtures.signature.s) |
||||
'1MsHWS1BnwMc3tLE8G35UXsS58fKipzB7a', |
|
||||
'16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM' |
|
||||
] |
|
||||
var compressedAddresses = [ |
|
||||
'1AA4sjKW2aUmbtN3MtegdvhYtDBbDEke1q', |
|
||||
'1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9', |
|
||||
'1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs', |
|
||||
] |
|
||||
|
|
||||
for (var i = 0; i < addresses.length; ++i) { |
|
||||
var pub = ECPubKey.fromHex(pubkeys[i]) |
|
||||
var cpub = ECPubKey.fromHex(cpubkeys[i]) |
|
||||
cpub.compressed = true |
|
||||
|
|
||||
var addr = addresses[i] |
|
||||
var caddr = compressedAddresses[i] |
|
||||
|
|
||||
assert.equal(pub.getAddress().toString(), addr) |
|
||||
assert.equal(cpub.getAddress().toString(), caddr) |
|
||||
} |
} |
||||
}) |
}) |
||||
|
|
||||
|
it('verifies a valid signature', function() { |
||||
|
var hash = crypto.sha256(fixtures.message) |
||||
|
|
||||
|
assert.ok(pubKey.verify(hash, signature)) |
||||
|
}) |
||||
|
|
||||
|
it('doesn\'t verify the wrong signature', function() { |
||||
|
var hash = crypto.sha256('mushrooms') |
||||
|
|
||||
|
assert.ok(!pubKey.verify(hash, signature)) |
||||
|
}) |
||||
}) |
}) |
||||
}) |
}) |
||||
|
@ -0,0 +1,98 @@ |
|||||
|
module.exports = { |
||||
|
"valid": [ |
||||
|
{ |
||||
|
"D": "1", |
||||
|
"Q": "(55066263022277343669578718895168534326250603453777594175500187360389116729240,32670510020758816978083085130507043184471273380659243275938904335757337482424)", |
||||
|
"WIFs": [ |
||||
|
{ |
||||
|
"network": "bitcoin", |
||||
|
"string": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn", |
||||
|
"compressed": true |
||||
|
}, |
||||
|
{ |
||||
|
"network": "bitcoin", |
||||
|
"string": "5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf", |
||||
|
"compressed": false |
||||
|
} |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"D": "19898843618908353587043383062236220484949425084007183071220218307100305431102", |
||||
|
"Q": "(83225686012142088543596389522774768397204444195709443235253141114409346958144,23739058578904784236915560265041168694780215705543362357495033621678991351768)", |
||||
|
"WIFs": [ |
||||
|
{ |
||||
|
"network": "bitcoin", |
||||
|
"string": "KxhEDBQyyEFymvfJD96q8stMbJMbZUb6D1PmXqBWZDU2WvbvVs9o", |
||||
|
"compressed": true |
||||
|
} |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"D": "48968302285117906840285529799176770990048954789747953886390402978935544927851", |
||||
|
"Q": "(30095590000961171681152428142595206241714764354580127609094760797518133922356,93521207164355458151597931319591130635754976513751247168472016818884561919702)", |
||||
|
"WIFs": [ |
||||
|
{ |
||||
|
"network": "bitcoin", |
||||
|
"string": "KzrA86mCVMGWnLGBQu9yzQa32qbxb5dvSK4XhyjjGAWSBKYX4rHx", |
||||
|
"compressed": true |
||||
|
}, |
||||
|
{ |
||||
|
"network": "bitcoin", |
||||
|
"string": "5JdxzLtFPHNe7CAL8EBC6krdFv9pwPoRo4e3syMZEQT9srmK8hh", |
||||
|
"compressed": false |
||||
|
}, |
||||
|
{ |
||||
|
"network": "testnet", |
||||
|
"string": "cRD9b1m3vQxmwmjSoJy7Mj56f4uNFXjcWMCzpQCEmHASS4edEwXv", |
||||
|
"compressed": true |
||||
|
}, |
||||
|
{ |
||||
|
"network": "testnet", |
||||
|
"string": "92Qba5hnyWSn5Ffcka56yMQauaWY6ZLd91Vzxbi4a9CCetaHtYj", |
||||
|
"compressed": false |
||||
|
} |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"D": "115792089237316195423570985008687907852837564279074904382605163141518161494336", |
||||
|
"Q": "(55066263022277343669578718895168534326250603453777594175500187360389116729240,83121579216557378445487899878180864668798711284981320763518679672151497189239)", |
||||
|
"WIFs": [ |
||||
|
{ |
||||
|
"network": "bitcoin", |
||||
|
"string": "L5oLkpV3aqBjhki6LmvChTCV6odsp4SXM6FfU2Gppt5kFLaHLuZ9", |
||||
|
"compressed": true |
||||
|
} |
||||
|
] |
||||
|
} |
||||
|
], |
||||
|
"invalid": { |
||||
|
"constructor": [ |
||||
|
{ |
||||
|
"description": "Private key ZERO", |
||||
|
"D": "0" |
||||
|
}, |
||||
|
{ |
||||
|
"description": "Private key equal to the curve order", |
||||
|
"D": "115792089237316195423570985008687907852837564279074904382605163141518161494337" |
||||
|
}, |
||||
|
{ |
||||
|
"description": "Private key greater than the curve order", |
||||
|
"D": "115792089237316195423570985008687907853269984665640564039457584007913129639935" |
||||
|
} |
||||
|
], |
||||
|
"WIF": [ |
||||
|
{ |
||||
|
"description": "Invalid compression flag", |
||||
|
"string": "ju9rooVsmagsb4qmNyTysUSFB1GB6MdpD7eoGjUTPmZRAApJxRz" |
||||
|
}, |
||||
|
{ |
||||
|
"description": "Payload too short", |
||||
|
"string": "7ZEtRQLhCsDQrd6ZKfmcESdXgas8ggZPN24ByEi5ey6VJW" |
||||
|
}, |
||||
|
{ |
||||
|
"description": "Payload too long", |
||||
|
"string": "5qibUKwsnMo1qDiNp3prGaQkD2JfVJa8F8Na87H2CkMHvuVg6uKhw67Rh" |
||||
|
} |
||||
|
] |
||||
|
} |
||||
|
} |
@ -0,0 +1,19 @@ |
|||||
|
module.exports = { |
||||
|
"Q": { |
||||
|
"x": "55066263022277343669578718895168534326250603453777594175500187360389116729240", |
||||
|
"y": "32670510020758816978083085130507043184471273380659243275938904335757337482424" |
||||
|
}, |
||||
|
"compressed": { |
||||
|
"hex": "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798", |
||||
|
"hash160": "751e76e8199196d454941c45d1b3a323f1433bd6" |
||||
|
}, |
||||
|
"uncompressed": { |
||||
|
"hex": "0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8", |
||||
|
"hash160": "91b24bf9f5288532960ac687abb035127b1d28a5" |
||||
|
}, |
||||
|
"message": "vires in numeris", |
||||
|
"signature": { |
||||
|
"r": "68972263025625296948424563184904289678530916807200550828762374724416876919710", |
||||
|
"s": "43478152510424186005054433052302509227777805602212468112169549534899266476898" |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue