From fcf67a87ccee2be2817aa78da37c285d3e0010c3 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Sat, 22 Mar 2014 18:19:56 +1100 Subject: [PATCH] Restructures test/eckey.js --- test/eckey.js | 137 ++++++++++++++++++++++++++++---------------------- 1 file changed, 76 insertions(+), 61 deletions(-) diff --git a/test/eckey.js b/test/eckey.js index fb3f3af..7c5f594 100644 --- a/test/eckey.js +++ b/test/eckey.js @@ -1,16 +1,18 @@ /* global describe, it */ var assert = require('assert'); + var ECKey = require('../src/eckey.js').ECKey; +var ECPubKey = require('../src/eckey.js').ECPubKey; + var convert = require('../src/convert.js'); var bytesToHex = convert.bytesToHex; var hexToBytes = convert.hexToBytes; var Address = require('../src/address'); var Network = require('../src/network') -var mainnet = Network.mainnet.addressVersion var testnet = Network.testnet.addressVersion describe('ECKey', function() { - describe('constructor (base58 private) on mainnet', function() { + describe('constructor', function() { it('parses hex', function() { var priv = '18e14a7b6a307f426a94f8114701e7c8e774e7f9a47e2c2035db29a206321725'; var pub = '0450863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b235' + @@ -19,7 +21,6 @@ describe('ECKey', function() { assert.equal(key.getPub().toHex(), pub); assert.equal(key.compressed, false); - assert.equal(key.version, mainnet); }) it('parses base64', function() { @@ -30,7 +31,6 @@ describe('ECKey', function() { assert.equal(key.getPub().toHex(), pub); assert.equal(key.compressed, false); - assert.equal(key.version, mainnet); }) it('parses WIF', function() { @@ -42,8 +42,7 @@ describe('ECKey', function() { assert.equal(key.compressed, false); assert.equal(key.getPub().toHex(), pub); - assert.equal(key.getBitcoinAddress().toString(), addr); - assert.equal(key.version, mainnet); + assert.equal(key.getAddress().toString(), addr); }) it('parses compressed WIF', function() { @@ -54,73 +53,89 @@ describe('ECKey', function() { assert.equal(key.compressed, true); assert.equal(key.getPub().toHex(), pub); - assert.equal(key.getBitcoinAddress().toString(), addr); - assert.equal(key.version, mainnet); + assert.equal(key.getAddress().toString(), addr); }) - }) - describe('constructor (base58 private) on testnet', function() { - it('parses hex', function() { + it('alternative constructor syntax', function() { var priv = 'ca48ec9783cf3ad0dfeff1fc254395a2e403cbbc666477b61b45e31d3b8ab458'; var pub = '044b12d9d7c77db68388b6ff7c89046174c871546436806bcd80d07c28ea81199' + '283fbec990dad6fb98f93f712d50cb874dd717de6a184158d63886dda3090f566'; - var key = new ECKey(priv, false, testnet); + var key = ECKey(priv, false); assert.equal(key.getPub().toHex(), pub); assert.equal(key.compressed, false); - assert.equal(key.version, testnet); assert.equal(key.toHex(), priv); }) + }) - it('parses base64', function() { - var priv = 'VYdB+iv47y5FaUVIPdQInkgATrABeuD1lACUoM4x7tU='; - var pub = '042f43c16c08849fed20a35bb7b1947bbf0923c52d613ee13b5c665a1e10d24b2' + - '8be909a70f5f87c1adb79fbcd1b3f17d20aa91c04fc355112dba2ce9b1cbf013b'; - var key = new ECKey(priv, false, testnet); - - assert.equal(key.getPub().toHex(), pub); - assert.equal(key.compressed, false); - assert.equal(key.version, testnet); - assert.equal(key.toBase64(), priv); - }) - - it('parses WIF', function() { - var priv = '92tb9mjz6q9eKZjYvLsgk87kPrMoh7BGRumSzPeUGhmigtsfrbP'; - var pub = '04b70d0bd71417d7903b6c4b451d8cfbd6514cb3557d9363319e5d87377905c4f' + - '3bb6c6e8c1819b6dd95d60aa8da73f0e726a2311545842bf07e78487e8ea2801f'; - var addr = 'mmktwdfQ3oaEvyjS5p1TgovfJmgBQLjGa4'; - var key = new ECKey(priv); - - assert.equal(key.compressed, false); - assert.equal(key.getPub().toHex(), pub); - assert.equal(key.getBitcoinAddress().toString(), addr); - assert.equal(key.version, testnet); - assert.equal(key.toBase58(), priv); - }) - - it('parses compressed WIF', function() { - var priv = 'cTLhjiUj2jX9iwUotSiiRLQnXrrq62GK6wuBiJxPNJxP15ERrX8o'; - var pub = '03b70d0bd71417d7903b6c4b451d8cfbd6514cb3557d9363319e5d87377905c4f3' - var addr = 'n3nBy5dyx24CLT3kg3ZJwfTeTX5oxxtxNE'; - var key = new ECKey(priv); - - assert.equal(key.compressed, true); - assert.equal(key.getPub().toHex(), pub); - assert.equal(key.getBitcoinAddress().toString(), addr); - assert.equal(key.version, testnet); - assert.equal(key.toBase58(), priv); + describe('toAddress', function() { + var privkeys = [ + 'CA48EC9783CF3AD0DFEFF1FC254395A2E403CBBC666477B61B45E31D3B8AB458', + '1111111111111111111111111111111111111111111111111111111111111111', + '18E14A7B6A307F426A94F8114701E7C8E774E7F9A47E2C2035DB29A206321725' + ]; + + // compressed pubkeys + var cpubkeys = [ + '024B12D9D7C77DB68388B6FF7C89046174C871546436806BCD80D07C28EA811992', + '034F355BDCB7CC0AF728EF3CCEB9615D90684BB5B2CA5F859AB0F0B704075871AA', + '0250863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352' + ]; + + var pubkeys = cpubkeys.map(function(x) { + return new ECPubKey(x).pub.getEncoded(false); + }); + + it('mainnet', function() { + var addresses = [ + '19SgmoUj4xowEjwtXvNAtYTAgbvR9iBCui', + '1MsHWS1BnwMc3tLE8G35UXsS58fKipzB7a', + '16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM' + ]; + var compressedAddresses = [ + '1AA4sjKW2aUmbtN3MtegdvhYtDBbDEke1q', + '1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9', + '1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs', + ]; + + for (var i = 0; i < addresses.length; ++i) { + var priv = new ECKey(privkeys[i], false); + var pubcomp = new ECPubKey(cpubkeys[i], true); + var pub = new ECPubKey(pubkeys[i], false); + + var addr = addresses[i]; + var caddr = compressedAddresses[i]; + + assert.equal(priv.getAddress().toString(), addr); + assert.equal(pub.getAddress().toString(), addr); + assert.equal(pubcomp.getAddress().toString(), caddr); + } }) - it('initiation via alternative constructor syntax', function() { - var priv = 'ca48ec9783cf3ad0dfeff1fc254395a2e403cbbc666477b61b45e31d3b8ab458'; - var pub = '044b12d9d7c77db68388b6ff7c89046174c871546436806bcd80d07c28ea81199' + - '283fbec990dad6fb98f93f712d50cb874dd717de6a184158d63886dda3090f566'; - var key = ECKey(priv, false, testnet); - - assert.equal(key.getPub().toHex(), pub); - assert.equal(key.compressed, false); - assert.equal(key.version, testnet); - assert.equal(key.toHex(), priv); + it('testnet', function() { + var addresses = [ + '19SgmoUj4xowEjwtXvNAtYTAgbvR9iBCui', + '1MsHWS1BnwMc3tLE8G35UXsS58fKipzB7a', + '16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM' + ]; + var compressedAddresses = [ + '1AA4sjKW2aUmbtN3MtegdvhYtDBbDEke1q', + '1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9', + '1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs', + ]; + + for (var i = 0; i < addresses.length; ++i) { + var priv = new ECKey(privkeys[i], false); + var pubcomp = new ECPubKey(cpubkeys[i], true); + var pub = new ECPubKey(pubkeys[i], false); + + var addr = addresses[i]; + var caddr = compressedAddresses[i]; + + assert.equal(priv.getAddress().toString(), addr); + assert.equal(pub.getAddress().toString(), addr); + assert.equal(pubcomp.getAddress().toString(), caddr); + } }) - }) + }); })