diff --git a/.jshintrc b/.jshintrc index 7b801cb6a..b15caaa4a 100644 --- a/.jshintrc +++ b/.jshintrc @@ -8,7 +8,7 @@ "eqeqeq": true, // Require triple equals i.e. `===`. "immed": true, // Require immediate invocations to be wrapped in parens e.g. `( function(){}() );` "latedef": true, // Prohibit variable use before definition. - "newcap": true, // Require capitalization of all constructor functions e.g. `new F()`. + "newcap": false, // Require capitalization of all constructor functions e.g. `new F()`. "noarg": true, // Prohibit use of `arguments.caller` and `arguments.callee`. "quotmark": "single", // Define quotes to string values. "regexp": true, // Prohibit `.` and `[^...]` in regular expressions. diff --git a/Gruntfile.js b/Gruntfile.js index adc5e7545..934a78dfd 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -4,7 +4,6 @@ module.exports = function(grunt) { //Load NPM tasks grunt.loadNpmTasks('grunt-contrib-watch'); - grunt.loadNpmTasks('grunt-mocha-test'); grunt.loadNpmTasks('grunt-markdown'); grunt.loadNpmTasks('grunt-shell'); @@ -17,7 +16,7 @@ module.exports = function(grunt) { stderr: true }, command: grunt.option('target') === 'dev' ? - 'node ./browser/build.js -a -d; docco lib/* ' : 'node ./browser/build.js -a' + './browser/build; docco lib/* ' : './browser/build' } }, watch: { @@ -30,12 +29,6 @@ module.exports = function(grunt) { tasks: ['shell'], }, }, - mochaTest: { - options: { - reporter: 'spec', - }, - src: ['test/*.js'], - }, markdown: { all: { files: [{ diff --git a/LICENSE.md b/LICENSE.md deleted file mode 100644 index 6fdf61aae..000000000 --- a/LICENSE.md +++ /dev/null @@ -1,30 +0,0 @@ -This software is licensed under the MIT License. - -Copyright (c) 2014 Ryan X. Charles - -Parts of this software are based on bitcore -Copyright (c) 2014 BitPay Inc. - -Parts of this software are based on BitcoinJS -Copyright (c) 2011 Stefan Thomas - -Parts of this software are based on BitcoinJ -Copyright (c) 2011 Google Inc. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. diff --git a/examples.md b/examples.md deleted file mode 100755 index 8f300318a..000000000 --- a/examples.md +++ /dev/null @@ -1,1104 +0,0 @@ -#Address.js - 'use strict'; - - - var run = function() { - // Replace '../bitcore' with 'bitcore' if you use this code elsewhere. - var bitcore = require('../bitcore'); - var Address = bitcore.Address; - - var addrs = [ - '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', - '1A1zP1eP5QGefi2DMPTfTL5SLmv7Dixxxx', - 'A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', - '1600 Pennsylvania Ave NW', - ].map(function(addr) { - return new Address(addr); - }); - - addrs.forEach(function(addr) { - var valid = addr.isValid(); - console.log(addr.data + ' is ' + (valid ? '' : 'not ') + 'valid'); - }); - - }; - - module.exports.run = run; - if (require.main === module) { - run(); - } - -#Armory.js - var Armory = require('../lib/Armory'); - var Address = require('../lib/Address'); - - // Initial public key can be retrieved from paper backup - - var PublicX = '9df5 23e7 18b9 1f59 a790 2d46 999f 9357 ccf8 7208 24d4 3076 4516 b809 f7ab ce4e'; - var PublicY = '66ba 5d21 4682 0dae 401d 9506 8437 2516 79f9 0c56 4186 cc50 07df c6d0 6989 1ff4'; - var pubkey = '04' + PublicX.split(' ').join('') + PublicY.split(' ').join(''); - - // Chain code can be generated by entering paper backup - // on brainwallet.org/#chains or by using Armory.fromSeed() below - - var chaincode = '84ac14bc4b388b33da099a0b4ee3b507284d99e1476639e36e5ca5e6af86481e'; - - var armory = new Armory(chaincode, pubkey); - - console.log('Deriving public keys for'); - console.log('------------------------'); - console.log('Chain code: %s', chaincode); - console.log('Public key: %s', pubkey); - console.log(''); - - for (var i = 0; i < 5; i++) { - console.log(Address.fromPubKey(armory.pubkey).as('base58')); - armory = armory.next(); - } - - // Derive first public key and chain code from seed - var seed = [ - 'aagh hjfj sihk ietj giik wwai awtd uodh hnji', - 'soss uaku egod utai itos fijj ihgi jhau jtoo' - ]; - - console.log(''); - console.log(''); - console.log('Deriving public keys for'); - console.log('------------------------'); - console.log('Seed: %s', seed.join(' ')); - console.log(''); - - // skip first public key - var a = Armory.fromSeed(seed.join('\n')).next(); - - for (var i = 0; i < 5; i++) { - console.log(Address.fromPubKey(a.pubkey).as('base58')); - a = a.next(); - } - - - var mpk = '045a09a3286873a72f164476bde9d1d8e5c2bc044e35aa47eb6e798e325a86417f7c35b61d9905053533e0b4f2a26eca0330aadf21c638969e45aaace50e4c0c8784ac14bc4b388b33da099a0b4ee3b507284d99e1476639e36e5ca5e6af86481e'; - - console.log(''); - console.log(''); - console.log('Deriving public keys for'); - console.log('------------------------'); - console.log('Master Public Key: %s', mpk); - console.log(''); - - // skip first public key - var b = Armory.fromMasterPublicKey(mpk).next(); - - for (var i = 0; i < 5; i++) { - console.log(Address.fromPubKey(b.pubkey).as('base58')); - b = b.next(); - } - -#ConnectionTor.js - var Peer = require('../lib/Peer'); - var Connection = require('../lib/Connection'); - var dns = require('dns'); - - // get a peer from dns seed - dns.resolve('dnsseed.bluematt.me', function(err, seeds) { - // use the first peer - var peer = new Peer(seeds[0], 8333); - - //Custom peer: - //var peer = new Peer('180.153.139.246', '8888'); - - // create a connection without an existing socket - // but specify a socks5 proxy to create a socket - // that's bound to that proxy in it's place - var connection = new Connection(null, peer, { - proxy: { - host: '127.0.0.1', - port: 9050 - } - }); - - connection.open(); - - connection.on('connect', function(data) { - console.log('connected through socks5!'); - }); - - connection.on('error', function(err) { - console.log('There was an error running this example.'); - console.log('Are you running Tor? Tor must running for this example to work.'); - console.log('If you still get an error, you may need to use a different proxy from here:'); - console.log('http://sockslist.net/'); - //console.log(err); - }); - - }); - -#CreateAndSignTx-Multisig.js - var run = function() { - bitcore = typeof(bitcore) === 'undefined' ? require('../bitcore') : bitcore; - var networks = require('../networks'); - var WalletKey = bitcore.WalletKey; - var Builder = bitcore.TransactionBuilder; - var opts = { - network: networks.testnet - }; - - console.log('## Network: ' + opts.network.name); - - var input = {}; - input.addr = "n2hoFVbPrYQf7RJwiRy1tkbuPPqyhAEfbp"; - input.priv = "cS62Ej4SobZnpFQYN1PEEBr2KWf5sgRYYnELtumcG6WVCfxno39V"; - - // Complete with the corresponding UTXO you want to use - var utxos = [{ - address: input.addr, - txid: "39c71ebda371f75f4b854a720eaf9898b237facf3c2b101b58cd4383a44a6adc", - vout: 1, - scriptPubKey: "76a914e867aad8bd361f57c50adc37a0c018692b5b0c9a88ac", - amount: 0.4296, - confirmations: 2 - }]; - - var privs = [ - "cP6JBHuQf7yqeqtdKRd22ibF3VehDv7G6BdzxSNABgrv3jFJUGoN", - "cQfRwF7XLSM5xGUpF8PZvob2MZyULvZPA2j5cat2RKDJrja7FtCZ", - "cUkYub4jtFVYymHh38yMMW36nJB4pXG5Pzd5QjResq79kAndkJcg", - "cMyBgowsyrJRufoKWob73rMQB1PBqDdwFt8z4TJ6APN2HkmX1Ttm", - "cN9yZCom6hAZpHtCp8ovE1zFa7RqDf3Cr4W6AwH2tp59Jjh9JcXu", - ]; - - var pubkeys = [] - privs.forEach(function(p) { - var wk = new WalletKey(opts); - wk.fromObj({ - priv: p - }); - pubkeys.push(bitcore.buffertools.toHex(wk.privKey.public)); - }); - - - var outs = [{ - nreq: 3, - pubkeys: pubkeys, - amount: 0.05 - }]; - var tx = new Builder(opts) - .setUnspent(utxos) - .setOutputs(outs) - .sign([input.priv]) - .build(); - var txHex = tx.serialize().toString('hex'); - console.log('1) SEND TO MULSISIG TX: ', txHex); - console.log('[this example originally generated TXID: e4bc22d8c519d3cf848d710619f8480be56176a4a6548dfbe865ab3886b578b5 on testnet]\n\n\thttp://test.bitcore.io/tx/e4bc22d8c519d3cf848d710619f8480be56176a4a6548dfbe865ab3886b578b5\n\n'); - - - //save scriptPubKey - var scriptPubKey = tx.outs[0].s.toString('hex'); - - /* - * - * REDDEEM TX - */ - var utxos2 = [{ - address: input.addr, - txid: "e4bc22d8c519d3cf848d710619f8480be56176a4a6548dfbe865ab3886b578b5", - vout: 0, - scriptPubKey: scriptPubKey, - amount: 0.05, - confirmations: 2 - }]; - - outs = [{ - address: input.addr, - amount: 0.04 - }]; - var b = new Builder(opts) - .setUnspent(utxos2) - .setOutputs(outs) - .sign(privs); - - - tx = b.build(); - - - var txHex = tx.serialize().toString('hex'); - console.log('2) REDEEM SCRIPT: ', txHex); - console.log('=> Is signed status:', b.isFullySigned(), tx.countInputMissingSignatures(0)); - - console.log('[this example originally generated TXID: 1eb388977b2de99562eb0fbcc661a100eaffed99c53bfcfebe5a087002039b83 on testnet]\n\n\thttp://test.bitcore.io/tx/1eb388977b2de99562eb0fbcc661a100eaffed99c53bfcfebe5a087002039b83'); - - }; - - // This is just for browser & mocha compatibility - if (typeof module !== 'undefined') { - module.exports.run = run; - if (require.main === module) { - run(); - } - } else { - run(); - } - - //// - -#CreateAndSignTx-PayToPubkeyHash.js - var run = function() { - bitcore = typeof(bitcore) === 'undefined' ? require('../bitcore') : bitcore; - - var priv = 'cTgGUrcro89yUtKeG6gHBAS14r3qp25KwTTxG9d4kEzcFxecuZDm'; - var amt = '0.005'; - var toAddress = 'myuAQcCc1REUgXGsCTiYhZvPPc3XxZ36G1'; - var changeAddressString = 'moDz3jEo9q7CxjBDjmb13sL4SKkgo2AACE'; - - var utxos = [{ - address: "mqSjTad2TKbPcKQ3Jq4kgCkKatyN44UMgZ", - txid: "2ac165fa7a3a2b535d106a0041c7568d03b531e58aeccdd3199d7289ab12cfc1", - vout: 1, - scriptPubKey: "76a9146ce4e1163eb18939b1440c42844d5f0261c0338288ac", - amount: 0.01, - confirmations: 2 - }]; - - console.log('TX Data: BTC:' + amt + ' => ' + toAddress + ', change To:' + changeAddressString); - console.log('Unspends Outputs:', utxos); - - - var outs = [{ - address: toAddress, - amount: amt - }]; - var keys = [priv]; - var opts = { - remainderOut: { - address: changeAddressString - } - }; - var Builder = bitcore.TransactionBuilder; - - var tx = new Builder(opts) - .setUnspent(utxos) - .setOutputs(outs) - .sign(keys) - .build(); - - /* create and signing can be done in multiple steps using: - * - * var builder = new bitcore.TransactionBuilder(opts) - * .setUnspent(utxos) - * .setOutputs(outs); - * - * builder.sign(key1); - * builder.sign(key2); - * ... - * if (builder.isFullySigned()){ - * var tx = builder.build(); - * } - * - * The selected Unspent Outputs for the transaction can be retrieved with: - * - * var selectedUnspent = build.getSelectedUnspent(); - */ - - var txHex = tx.serialize().toString('hex'); - console.log('TX HEX IS: ', txHex); - }; - - // This is just for browser & mocha compatibility - if (typeof module !== 'undefined') { - module.exports.run = run; - if (require.main === module) { - run(); - } - } else { - run(); - } - - //// - -#CreateAndSignTx-PayToScriptHash.js - var run = function() { - bitcore = typeof(bitcore) === 'undefined' ? require('../bitcore') : bitcore; - var networks = require('../networks'); - var WalletKey = bitcore.WalletKey; - var Script = bitcore.Script; - var Builder = bitcore.TransactionBuilder; - var opts = { - network: networks.testnet - }; - - console.log('## Network: ' + opts.network.name); - - var input = {}; - input.addr = "n2hoFVbPrYQf7RJwiRy1tkbuPPqyhAEfbp"; - input.priv = "cS62Ej4SobZnpFQYN1PEEBr2KWf5sgRYYnELtumcG6WVCfxno39V"; - - // Complete with the corresponding UTXO you want to use - var utxos = [{ - address: "n2hoFVbPrYQf7RJwiRy1tkbuPPqyhAEfbp", - txid: "e4bc22d8c519d3cf848d710619f8480be56176a4a6548dfbe865ab3886b578b5", - vout: 1, - scriptPubKey: "76a914e867aad8bd361f57c50adc37a0c018692b5b0c9a88ac", - amount: 0.3795, - confirmations: 7 - }]; - - var privs = [ - "cMpKwGr5oxEacN95WFKNEq6tTcvi11regFwS3muHvGYVxMPJX8JA", - "cVf32m9MR4vxcPwKNJuPepUe8XrHD2z63eCk76d6njRGyCkXpkSM", - "cQ2sVRFX4jQYMLhWyzz6jTQ2xju51P36968ecXnPhRLKLH677eKR", - "cSw7x9ERcmeWCU3yVBT6Nz7b9JiZ5yjUB7JMhBUv9UM7rSaDpwX9", - "cRQBM8qM4ZXJGP1De4D5RtJm7Q6FNWQSMx7YExxzgn2ehjM3haxW", - ]; - - var pubkeys = [] - privs.forEach(function(p) { - var wk = new WalletKey(opts); - wk.fromObj({ - priv: p - }); - pubkeys.push(bitcore.buffertools.toHex(wk.privKey.public)); - }); - - // multisig p2sh - var opts = { - nreq: 3, - pubkeys: pubkeys - }; - - // p2scriphash p2sh - //var opts = [{address: an_address}]; - - var info = Builder.infoForP2sh(opts, 'testnet'); - var p2shScript = info.scriptBufHex; - var p2shAddress = info.address; - - - var outs = [{ - address: p2shAddress, - amount: 0.05 - }]; - var tx = new Builder(opts) - .setUnspent(utxos) - .setOutputs(outs) - .sign([input.priv]) - .build(); - - var txHex = tx.serialize().toString('hex'); - - - console.log('## p2sh address: ' + p2shAddress); //TODO - console.log('\n1) SEND TO P2SH TX: ', txHex); - console.log('[this example originally generated TXID: c2e50d1c8c581d8c4408378b751633f7eb86687fc5f0502be7b467173f275ae7 on testnet]\n\n\thttp://test.bitcore.io/tx/c2e50d1c8c581d8c4408378b751633f7eb86687fc5f0502be7b467173f275ae7\n\n'); - - //save scriptPubKey - var scriptPubKey = tx.outs[0].s.toString('hex'); - - /* - * - * REDDEEM TX - */ - var utxos2 = [{ - address: p2shAddress, - txid: "c2e50d1c8c581d8c4408378b751633f7eb86687fc5f0502be7b467173f275ae7", - vout: 0, - scriptPubKey: scriptPubKey, - amount: 0.05, - confirmations: 1 - }]; - - outs = [{ - address: input.addr, - amount: 0.04 - }]; - - var hashMap = {}; - hashMap[p2shAddress] = p2shScript; - - var b = new Builder(opts) - .setUnspent(utxos2) - .setHashToScriptMap(hashMap) - .setOutputs(outs) - .sign(privs); - - tx = b.build(); - - - console.log('Builder:'); - console.log('\tSignatures:' + tx.countInputMissingSignatures(0)); - console.log('\t#isFullySigned:' + b.isFullySigned()); - - console.log('TX:'); - console.log('\t #isComplete:' + tx.isComplete()); - - var txHex = tx.serialize().toString('hex'); - console.log('2) REDEEM SCRIPT: ', txHex); - console.log('[this example originally generated TXID: 8284aa3b6f9c71c35ecb1d61d05ae78c8ca1f36940eaa615b50584dfc3d95cb7 on testnet]\n\n\thttp://test.bitcore.io/tx/8284aa3b6f9c71c35ecb1d61d05ae78c8ca1f36940eaa615b50584dfc3d95cb7\n\n'); - - /* - // To send TX with RPC: - var RpcClient = bitcore.RpcClient; - var config = { - protocol: 'http', - user: 'user', - pass: 'pass', - host: '127.0.0.1', - port: '18332', - }; - var rpc = new RpcClient(config); - rpc.sendRawTransaction(txHex, function(err, ret) { - console.log('err', err); //TODO - console.log('ret', ret); //TODO - process.exit(-1); - }); - }; - */ - - }; - - - // This is just for browser & mocha compatibility - if (typeof module !== 'undefined') { - module.exports.run = run; - if (require.main === module) { - run(); - } - } else { - run(); - } - -#CreateKey.js - 'use strict'; - - - - var run = function() { - // replace '../bitcore' with 'bitcore' if you use this code elsewhere. - var bitcore = require('../bitcore'); - var networks = require('../networks'); - var WalletKey = bitcore.WalletKey; - - var opts = { - network: networks.testnet - }; - - function print(wk) { - - console.log('\n## Network: ' + wk.network.name); - console.log('\t * Hex Representation'); - console.log('\tPrivate: ' + bitcore.buffertools.toHex(wk.privKey.private)); - console.log('\tPublic : ' + bitcore.buffertools.toHex(wk.privKey.public)); - console.log('\tPublic Compressed : ' + (wk.privKey.compressed ? 'Yes' : 'No')); - - var wkObj = wk.storeObj(); - console.log('\n\t * WalletKey Store Object'); - console.log('\tPrivate: ' + wkObj.priv); - console.log('\tPublic : ' + wkObj.pub); - console.log('\tAddr : ' + wkObj.addr); - }; - - //Generate a new one (compressed public key, compressed WIF flag) - var wk = new WalletKey(opts); - wk.generate(); - print(wk); - - //Generate from private Key WIF. Compressed status taken from WIF. - var wk2 = new WalletKey(opts); - wk2.fromObj({ - priv: 'cMpKwGr5oxEacN95WFKNEq6tTcvi11regFwS3muHvGYVxMPJX8JA' - }); - print(wk2); - - - }; - - module.exports.run = run; - if (require.main === module) { - run(); - } - -#CreateScript.js - 'use strict'; - - var run = function() { - // replace '../bitcore' with 'bitcore' if you use this code elsewhere. - var bitcore = require('../bitcore'); - var networks = require('../networks'); - var Script = bitcore.Script; - var WalletKey = bitcore.WalletKey; - var buffertools = bitcore.buffertools; - var Address = bitcore.Address; - var util = bitcore.util; - var opts = { - network: networks.testnet - }; - - var p = console.log; - - var wk = new WalletKey(opts); - wk.generate(); - var wkObj = wk.storeObj(); - - var s = Script.createPubKeyOut(wk.privKey.public); - p('\nScript PubKey:'); - p('\tHex : ' + buffertools.toHex(s.buffer)); - p('\tHuman : ' + s.toHumanReadable()); - p('\tKey -------------------------------'); - console.log('\tPrivate: ' + wkObj.priv); - console.log('\tPublic : ' + wkObj.pub); - console.log('\tAddr : ' + wkObj.addr); - - s = Script.createPubKeyHashOut(wk.privKey.public); - p('\nScript PubKeyHash:'); - p('\tHex : ' + buffertools.toHex(s.buffer)); - p('\tHuman : ' + s.toHumanReadable()); - p('\tKey -------------------------------'); - console.log('\tPrivate: ' + wkObj.priv); - console.log('\tPublic : ' + wkObj.pub); - console.log('\tAddr : ' + wkObj.addr); - - var wks = []; - var pubs = []; - for (var i = 0; i < 5; i++) { - wks[i] = new WalletKey(opts); - wks[i].generate(); - pubs.push(wks[i].privKey.public); - } - - s = Script.createMultisig(3, pubs); - p('\nScript MultiSig (3 out of 5 required signatures):'); - p('\tHex : ' + buffertools.toHex(s.buffer)); - p('\tHuman : ' + s.toHumanReadable()); - - for (i = 0; i < 5; i++) { - wkObj = wks[i].storeObj(); - p('\tKey [' + i + '] -------------------------------'); - console.log('\tPrivate: ' + wkObj.priv); - console.log('\tPublic : ' + wkObj.pub); - console.log('\tAddr : ' + wkObj.addr); - } - - var hash = util.sha256ripe160(s.buffer); - - s = Script.createP2SH(hash); - p('\nScript P2SH:'); - p('\tHex : ' + buffertools.toHex(s.buffer)); - p('\tHuman : ' + s.toHumanReadable()); - p('\tScript Hash: ' + buffertools.toHex(hash)); - var a = new Address(networks.livenet.P2SHVersion, hash); - p('\tp2sh Addr: ' + a.toString()); - - }; - - module.exports.run = run; - if (require.main === module) { - run(); - } - -#ECIES.js - var run = function() { - bitcore = typeof(bitcore) === 'undefined' ? require('../bitcore') : bitcore; - - console.log('ECIES: Elliptic Curve Integrated Encryption Scheme'); - console.log('A way of encrypting with a public key and decrypting with a private key.'); - - var key = bitcore.Key.generateSync(); - console.log('Private key: ' + key.private.toString('hex')); - console.log('Public key: ' + key.public.toString('hex')); - - var message = new Buffer('This is a message to be encrypted'); - console.log('Message: "' + message.toString() + '"'); - - var encrypted = bitcore.ECIES.encrypt(key.public, message); - console.log('Encrypted (with public key): ' + encrypted.toString('hex')); - - var decrypted = bitcore.ECIES.decrypt(key.private, encrypted); - console.log('Decrypted (with private key): "' + decrypted.toString() + '"'); - }; - - - // This is just for browser & mocha compatibility - if (typeof module !== 'undefined') { - module.exports.run = run; - if (require.main === module) { - run(); - } - } else { - run(); - } - -#ElectrumMPK.js - var Electrum = require('../lib/Electrum'); - var Address = require('../lib/Address'); - - var mpk = '92eea4d2f5263651db9e3222caded1fd4c89772f79a7c03fb6afc00e9d2c9d2ed9b86c2c95fc1171e49163079dacb7f048b3c509a27a490e1df9e7128362d468'; - - mpk = new Electrum(mpk); - - var key0 = mpk.generatePubKey(0); - var addr0 = Address.fromPubKey(key0); - - console.log(addr0.as('base58')); - -#HierarchicalKey.js - var run = function() { - bitcore = typeof(bitcore) === 'undefined' ? require('../bitcore') : bitcore; - var HierarchicalKey = bitcore.HierarchicalKey; - var Address = bitcore.Address; - var networks = bitcore.networks; - var coinUtil = bitcore.util; - var crypto = require('crypto'); - - console.log('HierarchicalKey: Hierarchical Deterministic Wallets (BIP32)'); - console.log('https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki\n'); - console.log('1) Make new hkey from randomly generated new seed'); - - var randomBytes = crypto.randomBytes(32); - var hkey = HierarchicalKey.seed(randomBytes); - console.log('master extended private key: ' + hkey.extendedPrivateKeyString()); - console.log('master extended public key: ' + hkey.extendedPublicKeyString()); - console.log('m/0/3/5 extended private key: ' + hkey.derive('m/0/3/5').extendedPrivateKeyString()); - console.log('m/0/3/5 extended public key: ' + hkey.derive('m/0/3/5').extendedPublicKeyString()); - console.log(); - - console.log('2) Make new hkey from known seed'); - var knownBytes = coinUtil.sha256('do not use this password as a brain wallet'); - var hkey = HierarchicalKey.seed(knownBytes); - console.log('master extended private key: ' + hkey.extendedPrivateKeyString()); - console.log('master extended public key: ' + hkey.extendedPublicKeyString()); - console.log('m/0/3/5 extended private key: ' + hkey.derive('m/0/3/5').extendedPrivateKeyString()); - console.log('m/0/3/5 extended public key: ' + hkey.derive('m/0/3/5').extendedPublicKeyString()); - console.log(); - - console.log('3) Make new hkey from known master private key'); - var knownMasterPrivateKey = 'xprv9s21ZrQH143K2LvayFZWVVTomiDKheKWvnupDB8fmjKwxkKG47uvzmFa3vCXoy9fxPJhRYsU19apVfexvMeLpJQuF2XtX1zRF3eao9GqqaQ'; - var hkey = new HierarchicalKey(knownMasterPrivateKey); - console.log('master extended private key: ' + hkey.extendedPrivateKeyString()); - console.log('master extended public key: ' + hkey.extendedPublicKeyString()); - console.log('m/0/3/5 extended private key: ' + hkey.derive('m/0/3/5').extendedPrivateKeyString()); - console.log('m/0/3/5 extended public key: ' + hkey.derive('m/0/3/5').extendedPublicKeyString()); - console.log(); - - console.log('4) Make new hkey from known master public key'); - var knownMasterPublicKey = 'xpub661MyMwAqRbcGpiFufipqsKKBG1NHNwfJKishAEFNqJ6ryLcKeKyFNEZces7gMWd4XGg4uUhXy8DS64o1oPGUECVHeLq957Txjwagxt475H'; - var hkey = new HierarchicalKey(knownMasterPublicKey); - console.log('master extended private key: cannot derive'); - console.log('master extended public key: ' + hkey.extendedPublicKeyString()); - console.log('m/0/3/5 extended private key: cannot derive'); - console.log('m/0/3/5 extended public key: ' + hkey.derive('m/0/3/5').extendedPublicKeyString()); - console.log(); - - console.log('5) Make new hkey from known derived public key'); - var knownPublicKey = 'xpub6CZei1p2zk68UwkcBDqzRonLHJWAiPZZ58sMgHJAn9fmpmnPayVEAvAs3XvTSUMZ1J8dNaxnv4wnt7YpRKr6BsqeWbW8msqeuuhiSzsQEC3'; - var hkey = new HierarchicalKey(knownPublicKey); - console.log('master extended private key: cannot derive'); - console.log('master extended public key: ' + hkey.extendedPublicKeyString()); - console.log('m/0/3/5 extended private key: cannot derive'); - console.log('m/0/3/5 extended public key: ' + hkey.derive('m/0/3/5').extendedPublicKeyString()); - console.log(); - - console.log('6) Make a bunch of new addresses from known public key'); - var knownPublicKey = 'xpub6CZei1p2zk68UwkcBDqzRonLHJWAiPZZ58sMgHJAn9fmpmnPayVEAvAs3XvTSUMZ1J8dNaxnv4wnt7YpRKr6BsqeWbW8msqeuuhiSzsQEC3'; - var hkey = new HierarchicalKey(knownPublicKey); - console.log('m/0 address: ' + Address.fromPubKey(hkey.derive('m/0').eckey.public).toString()); - //console.log('m/1 extended public key: ' + hkey.derive('m/1').extendedPublicKeyString()); - console.log('m/1 address: ' + Address.fromPubKey(hkey.derive('m/1').eckey.public).toString()); - //console.log('m/2 extended public key: ' + hkey.derive('m/2').extendedPublicKeyString()); - console.log('m/2 address: ' + Address.fromPubKey(hkey.derive('m/2').eckey.public).toString()); - //console.log('m/3 extended public key: ' + hkey.derive('m/3').extendedPublicKeyString()); - console.log('m/3 address: ' + Address.fromPubKey(hkey.derive('m/3').eckey.public).toString()); - console.log('...'); - //console.log('m/100 extended public key: ' + hkey.derive('m/100').extendedPublicKeyString()); - console.log('m/100 address: ' + Address.fromPubKey(hkey.derive('m/100').eckey.public).toString()); - console.log(); - - }; - - - // This is just for browser & mocha compatibility - if (typeof module !== 'undefined') { - module.exports.run = run; - if (require.main === module) { - run(); - } - } else { - run(); - } - -#PayToScriptHashAddress.js - var bitcore = require('../bitcore'); - var Address = bitcore.Address; - var bitcoreUtil = bitcore.util; - var Script = bitcore.Script; - var network = bitcore.networks.livenet; - - - var script = ''; // write down your script here - var s = Script.fromHumanReadable(script); - var hash = bitcoreUtil.sha256ripe160(s.getBuffer()); - var version = network.addressScript; - - var addr = new Address(version, hash); - var addrStr = addr.as('base58'); - - // This outputs the "address" of thescript - console.log(addrStr); - -#PeerDiscovery.js - var PeerManager = require('../lib/PeerManager'); - var peerman = new PeerManager(); - - peerman.discover({ - limit: 12 - }).start(); - -#PeerManager.js - 'use strict'; - - var run = function() { - // Replace '../bitcore' with 'bitcore' if you use this code elsewhere. - var bitcore = require('../bitcore'); - var Peer = bitcore.Peer; - var PeerManager = bitcore.PeerManager; - - var handleBlock = function(info) { - console.log('** Block Received **'); - console.log(info.message); - }; - - var handleTx = function(info) { - var tx = info.message.tx.getStandardizedObject(); - - console.log('** TX Received **'); - console.log(tx); - }; - - var handleInv = function(info) { - console.log('** Inv **'); - console.log(info.message); - - var invs = info.message.invs; - info.conn.sendGetData(invs); - }; - - var peerman = new PeerManager({ - network: 'testnet' - }); - - peerman.addPeer(new Peer('127.0.0.1', 18333)); - - peerman.on('connection', function(conn) { - conn.on('inv', handleInv); - conn.on('block', handleBlock); - conn.on('tx', handleTx); - }); - - peerman.start(); - }; - - module.exports.run = run; - if (require.main === module) { - run(); - } - -#Rpc.js - 'use strict'; - - var run = function() { - // Replace '../bitcore' with 'bitcore' if you use this code elsewhere. - var bitcore = require('../bitcore'); - var RpcClient = bitcore.RpcClient; - var hash = '0000000000b6288775bbd326bedf324ca8717a15191da58391535408205aada4'; - - var config = { - protocol: 'http', - user: 'user', - pass: 'pass', - host: '127.0.0.1', - port: '18332', - }; - - var rpc = new RpcClient(config); - - rpc.getBlock(hash, function(err, ret) { - if (err) { - console.error('An error occured fetching block', hash); - console.error(err); - return; - } - console.log(ret); - }); - }; - - module.exports.run = run; - if (require.main === module) { - run(); - } - -#Script.js - 'use strict'; - - var run = function() { - // Replace '../bitcore' with 'bitcore' if you use this code elsewhere. - var bitcore = require('../bitcore'); - var Address = bitcore.Address; - var coinUtil = bitcore.util; - var Script = bitcore.Script; - var network = bitcore.networks.testnet; - - var getAddrStr = function(s) { - var addrStrs = []; - var type = s.classify(); - var addr; - - switch (type) { - case Script.TX_PUBKEY: - var chunk = s.captureOne(); - addr = new Address(network.addressVersion, coinUtil.sha256ripe160(chunk)); - addrStrs.push(addr.toString()); - break; - case Script.TX_PUBKEYHASH: - addr = new Address(network.addressVersion, s.captureOne()); - addrStrs.push(addr.toString()); - break; - case Script.TX_SCRIPTHASH: - addr = new Address(network.P2SHVersion, s.captureOne()); - addrStrs.push(addr.toString()); - break; - case Script.TX_MULTISIG: - var chunks = s.capture(); - chunks.forEach(function(chunk) { - var a = new Address(network.addressVersion, coinUtil.sha256ripe160(chunk)); - addrStrs.push(a.toString()); - }); - break; - case Script.TX_UNKNOWN: - console.log('tx type unkown'); - break; - } - return addrStrs; - }; - - var script = 'DUP HASH160 0x14 0x3744841e13b90b4aca16fe793a7f88da3a23cc71 EQUALVERIFY CHECKSIG'; - var s = Script.fromHumanReadable(script); - console.log(getAddrStr(s)[0]); // mkZBYBiq6DNoQEKakpMJegyDbw2YiNQnHT - }; - - module.exports.run = run; - if (require.main === module) { - run(); - } - -#SendTx.js - 'use strict'; - - var run = function() { - // Replace '../bitcore' with 'bitcore' if you use this code elsewhere. - var bitcore = require('../bitcore'); - var Peer = bitcore.Peer; - - var TransactionBuilder = bitcore.TransactionBuilder; - var PeerManager = bitcore.PeerManager; - - // Unspent transactions can be found via the insight.bitcore.io or blockchain.info APIs - var unspent = [{ - 'txid': '707108b5ba4f78dc951df4647a03365bf36432ea57fb641676045c5044daaea7', - 'vout': 0, - 'address': 'n3QDC7DzsMmN4mcyp3k7XGPX7zFXXHG387', - 'scriptPubKey': '76a914f00c4a92ee2314ab08ac0283dc8d07d9bf2be32388ac', - 'amount': 0.12345600, - 'confirmations': 43537 - }, { - 'txid': '87a158d32833cb555aea27b6a21af569ccaeb8f9b19691e05f1e6c2b3440bdb3', - 'vout': 1, - 'address': 'mxdrp9s4mVxS9X4RBYiLe99v59V81XA5C3', - 'scriptPubKey': '76a914bbc87986da6b17c7876db4efacf59a95e14f6cf588ac', - 'amount': 0.05749800, - 'confirmations': 43536 - } - - ]; - - // Private keys in WIF format (see TransactionBuilder.js for other options) - var keys = [ - 'cQA75LXhV5JkMT8wkkqjR87SnHK4doh3c21p7PAd5tp8tc1tRBAY', - 'cRz85dz9AiDieRpEwoucfXXQa1jdHHghcv6YnnVVGZ3MQyR1X4u2', - 'cSq7yo4fvsbMyWVN945VUGUWMaSazZPWqBVJZyoGsHmNq6W4HVBV', - 'cPa87VgwZfowGZYaEenoQeJgRfKW6PhZ1R65EHTkN1K19cSvc92G', - 'cPQ9DSbBRLva9av5nqeF5AGrh3dsdW8p2E5jS4P8bDWZAoQTeeKB' - ]; - - var peerman = new PeerManager({ - network: 'testnet' - }); - peerman.addPeer(new Peer('127.0.0.1', 18333)); - - peerman.on('connect', function() { - var conn = peerman.getActiveConnection(); - if (conn) { - // define transaction output - var outs = [{ - address: 'mhNCT9TwZAGF1tLPpZdqfkTmtBkY282YDW', - amount: 0.1337 - }]; - // set change address - var opts = { - remainderOut: { - address: 'n4g2TFaQo8UgedwpkYdcQFF6xE2Ei9Czvy' - } - }; - var tx = new TransactionBuilder(opts) - .setUnspent(unspent) - .setOutputs(outs) - .sign(keys) - .build(); - - /* Create and signing can be done in multiple steps: - * - * var builder = new bitcore.TransactionBuilder(opts) - * .setUnspent(utxos) - * .setOutputs(outs); - * - * // Sign with the first key - * builder.sign(key1); - * var tx = builder.build(); // Partially signed transaction - * - * // Sign with the second key - * builder.sign(key2); - * if (builder.isFullySigned()){ - * var tx = builder.build(); - * } - * - * var selectedUnspent = build.getSelectedUnspent(); // Retrieve selected unspent outputs from the transaction - */ - - var txid = tx.getHash().toString('hex'); - console.log('Created transaction with txid ' + txid); - var raw_tx = tx.serialize().toString('hex'); - console.log('Transaction raw hex dump:'); - console.log('-------------------------------------'); - console.log(raw_tx); - console.log('-------------------------------------'); - // finally, send transaction to the bitcoin network - conn.sendTx(tx); - - // for now, the network won't respond in any case - // (transaction accepted, transaction rejected) - // in the future, we may listen to 'reject' message - // see https://gist.github.com/gavinandresen/7079034 - } - }); - - peerman.start(); - - }; - - module.exports.run = run; - if (require.main === module) { - run(); - } - -#SimpleP2Pmonitor.js - /** - * This is a simple script that will display network messages. - * It users the Peer / Connection classes directly instead of - * relying on PeerManager. - */ - - // replace by require('bitcore') if you use somewhere else - var bitcore = require('../'); - - //bitcore.config.logger = 'debug'; - - var Peer = bitcore.Peer, - Connection = bitcore.Connection; - - var peer = new Peer('127.0.0.1', 8333); - - var socket = peer.createConnection(); - - var con = new Connection(socket, peer); - - con.on('error', function(msg) { - var peer = msg.peer, - err = msg.err; - console.error('Error connecting to peer', peer.host + ':' + peer.port, '(' + err.message + ')'); - }); - - con.on('disconnect', function(msg) { - console.log('disconnect: ', msg); - }); - - con.on('connect', function(msg) { - console.log('Connected to %s', msg.peer.host + ':' + msg.peer.port); - }); - - /* Listen P2P messages */ - - // Make a log function available to all listeners - // The log function is just like console.log except it prefixes - // messages with [host:port] - function listen(event_name, fn) { - con.on(event_name, function(event) { - fn(event, function() { - var args = Array.prototype.slice.call(arguments); - var str = args.shift(); - str = '[%s:%s] ' + str; - args = [str, event.peer.host, event.peer.port].concat(args); - console.log.apply(console, args); - }); - }); - } - - listen('getaddr', function(event, log) { - log('Received message getaddr'); - log(event); - }); - - listen('verack', function(event, log) { - log('Received message verack'); - }); - - listen('version', function(event, log) { - log('Received message version (%s)', event.message.version); - }); - - listen('addr', function(event, log) { - log('Received message addr (%s addresses)', event.message.addrs.length); - }); - - listen('inv', function(event, log) { - log('Received message inv (%s invs)', event.message.count); - console.log(event.message.invs); - }); - -#VanityAddress.js - 'use strict'; - - - var run = function() { - // Replace '../bitcore' with 'bitcore' if you use this code elsewhere. - var bitcore = require('../bitcore'); - var Key = bitcore.Key; - var Address = bitcore.Address; - - // config your regular expression - var re = /[0-9]{6}$/; // ends in 6 digits - - var a, k, m; - while (true) { - k = Key.generateSync(); - a = Address.fromKey(k); - m = a.toString().match(re); - if (m) break; - } - console.log('Address: ' + a.toString()); - console.log('Private Key: ' + k.private.toString('hex')); - - }; - - module.exports.run = run; - if (require.main === module) { - run(); - } - diff --git a/examples/identity.js b/examples/identity.js deleted file mode 100644 index c17bc21c1..000000000 --- a/examples/identity.js +++ /dev/null @@ -1,20 +0,0 @@ -var Identity = require('../lib/identity'); -var KeyPair = require('../lib/keypair'); -var Hash = require('../lib/hash'); - -var keypair = new KeyPair().fromRandom(); - -console.log( 'keypair:' , keypair ); -console.log( 'private key:' , keypair.privkey.toString('hex') ); -console.log( 'public key:' , keypair.pubkey.toString('hex') ); -console.log( 'public hash:' , Hash.sha256ripemd160( keypair.pubkey.toBuffer() ).toString('hex') ); - - -var identity = new Identity().fromPubkey( keypair.pubkey ); - -keypair.pubkey.compressed = false; -var identityComp = new Identity().fromPubkey( keypair.pubkey ); - -console.log( 'identity:' , identity ); -console.log( 'identity string:' , identity.toString() ); -console.log( 'identity string, compressed:' , identityComp.toString() ); diff --git a/index.js b/index.js index 144b8cd7e..447ac870c 100644 --- a/index.js +++ b/index.js @@ -1,44 +1,35 @@ var bitcore = module.exports; -//main bitcoin library +// protocol +bitcore.Constants = require('./lib/protocol/constants'); +bitcore.Base58 = require('./lib/protocol/base58'); +bitcore.Base58Check = require('./lib/protocol/base58check'); +bitcore.BufferReader = require('./lib/protocol/bufferreader'); +bitcore.BufferWriter = require('./lib/protocol/bufferwriter'); +bitcore.Varint = require('./lib/protocol/varint'); + +// crypto +bitcore.BN = require('./lib/crypto/bn'); +bitcore.ECDSA = require('./lib/crypto/ecdsa'); +bitcore.Hash = require('./lib/crypto/hash'); +bitcore.Random = require('./lib/crypto/random'); +bitcore.Point = require('./lib/crypto/point'); + +// main bitcoin library bitcore.Address = require('./lib/address'); -bitcore.Base58 = require('./lib/base58'); -bitcore.Base58Check = require('./lib/base58check'); bitcore.BIP32 = require('./lib/bip32'); bitcore.Block = require('./lib/block'); bitcore.Blockheader = require('./lib/blockheader'); -bitcore.BN = require('./lib/bn'); -bitcore.BufferReader = require('./lib/bufferreader'); -bitcore.BufferWriter = require('./lib/bufferwriter'); -bitcore.Constants = require('./lib/constants'); -bitcore.ECDSA = require('./lib/ecdsa'); -bitcore.Hash = require('./lib/hash'); -bitcore.Identity = require('./lib/identity'); -bitcore.KDF = require('./lib/kdf'); bitcore.Keypair = require('./lib/keypair'); -bitcore.Message = require('./lib/message'); bitcore.Opcode = require('./lib/opcode'); -bitcore.Point = require('./lib/point'); bitcore.Privkey = require('./lib/privkey'); bitcore.Pubkey = require('./lib/pubkey'); -bitcore.Random = require('./lib/random'); bitcore.Script = require('./lib/script'); bitcore.Signature = require('./lib/signature'); bitcore.Transaction = require('./lib/transaction'); bitcore.Txin = require('./lib/txin'); bitcore.Txout = require('./lib/txout'); -bitcore.Varint = require('./lib/varint'); -//experimental, nonstandard, or unstable features -bitcore.expmt = {}; -bitcore.expmt.AES = require('./lib/expmt/aes'); -bitcore.expmt.AESCBC = require('./lib/expmt/aescbc'); -bitcore.expmt.CBC = require('./lib/expmt/cbc'); -bitcore.expmt.ECIES = require('./lib/expmt/ecies'); -bitcore.expmt.StealthAddress = require('./lib/expmt/stealthaddress'); -bitcore.expmt.Stealthkey = require('./lib/expmt/stealthkey'); -bitcore.expmt.StealthMessage = require('./lib/expmt/stealthmessage'); -bitcore.expmt.StealthTx = require('./lib/expmt/stealthtx'); //dependencies, subject to change bitcore.deps = {}; diff --git a/lib/address.js b/lib/address.js index 82865fc43..43369bdd7 100644 --- a/lib/address.js +++ b/lib/address.js @@ -1,6 +1,8 @@ -var base58check = require('./base58check'); -var constants = require('./constants'); -var Hash = require('./hash'); +'use strict'; + +var base58check = require('./protocol/base58check'); +var constants = require('./protocol/constants'); +var Hash = require('./crypto/hash'); function Address(buf) { if (!(this instanceof Address)) @@ -14,7 +16,7 @@ function Address(buf) { var obj = buf; this.set(obj); } -}; +} Address.prototype.set = function(obj) { this.hashbuf = obj.hashbuf || this.hashbuf || null; diff --git a/lib/bip32.js b/lib/bip32.js index 2ee5d6e4a..34bfe5cc1 100644 --- a/lib/bip32.js +++ b/lib/bip32.js @@ -1,12 +1,14 @@ -var Base58Check = require('./base58check'); -var Hash = require('./hash'); +'use strict'; + +var Base58Check = require('./protocol/base58check'); +var constants = require('./protocol/constants'); +var Hash = require('./crypto/hash'); +var Point = require('./crypto/point'); +var Random = require('./crypto/random'); +var BN = require('./crypto/bn'); var Keypair = require('./keypair'); var Pubkey = require('./pubkey'); var Privkey = require('./privkey'); -var Point = require('./point'); -var Random = require('./random'); -var BN = require('./bn'); -var constants = require('./constants'); var BIP32 = function BIP32(obj) { if (!(this instanceof BIP32)) @@ -17,7 +19,7 @@ var BIP32 = function BIP32(obj) { } else if (obj ) { this.set(obj); } -} +}; BIP32.prototype.set = function(obj) { this.version = typeof obj.version !== 'undefined' ? obj.version : this.version; diff --git a/lib/block.js b/lib/block.js index 4798945ff..bf09b007a 100644 --- a/lib/block.js +++ b/lib/block.js @@ -1,9 +1,11 @@ +'use strict'; + +var Hash = require('./crypto/hash'); +var BufferReader = require('./protocol/bufferreader'); +var BufferWriter = require('./protocol/bufferwriter'); +var Varint = require('./protocol/varint'); var Transaction = require('./transaction'); -var BufferReader = require('./bufferreader'); -var BufferWriter = require('./bufferwriter'); var Blockheader = require('./blockheader'); -var Varint = require('./varint'); -var Hash = require('./hash'); var Block = function Block(magicnum, blocksize, blockheader, txsvi, txs) { if (!(this instanceof Block)) diff --git a/lib/blockheader.js b/lib/blockheader.js index b24412e5d..81d7308e5 100644 --- a/lib/blockheader.js +++ b/lib/blockheader.js @@ -1,5 +1,7 @@ -var BufferReader = require('./bufferreader'); -var BufferWriter = require('./bufferwriter'); +'use strict'; + +var BufferReader = require('./protocol/bufferreader'); +var BufferWriter = require('./protocol/bufferwriter'); var Blockheader = function Blockheader(version, prevblockidbuf, merklerootbuf, time, bits, nonce) { if (!(this instanceof Blockheader)) @@ -20,7 +22,7 @@ var Blockheader = function Blockheader(version, prevblockidbuf, merklerootbuf, t var obj = version; this.set(obj); } -} +}; Blockheader.prototype.set = function(obj) { this.version = typeof obj.version !== 'undefined' ? obj.version : this.version; diff --git a/lib/bn.js b/lib/crypto/bn.js similarity index 99% rename from lib/bn.js rename to lib/crypto/bn.js index 6b71b67ac..60874d9fa 100644 --- a/lib/bn.js +++ b/lib/crypto/bn.js @@ -1,3 +1,5 @@ +'use strict'; + var _BN = require('bn.js'); var BN = function BN_extended(n) { diff --git a/lib/ecdsa.js b/lib/crypto/ecdsa.js similarity index 97% rename from lib/ecdsa.js rename to lib/crypto/ecdsa.js index ddb4af6f9..d68f82d87 100644 --- a/lib/ecdsa.js +++ b/lib/crypto/ecdsa.js @@ -1,9 +1,11 @@ +'use strict'; + var BN = require('./bn'); var Point = require('./point'); -var Signature = require('./signature'); -var Keypair = require('./keypair'); -var Pubkey = require('./pubkey'); var Random = require('./random'); +var Pubkey = require('../pubkey'); +var Keypair = require('../keypair'); +var Signature = require('../signature'); var ECDSA = function ECDSA(obj) { if (!(this instanceof ECDSA)) diff --git a/lib/hash.js b/lib/crypto/hash.js similarity index 99% rename from lib/hash.js rename to lib/crypto/hash.js index d3251a9a6..7ca569247 100644 --- a/lib/hash.js +++ b/lib/crypto/hash.js @@ -1,3 +1,5 @@ +'use strict'; + var hashjs = require('hash.js'); var sha512 = require('sha512'); diff --git a/lib/point.js b/lib/crypto/point.js similarity index 98% rename from lib/point.js rename to lib/crypto/point.js index 42967057b..d457b181e 100644 --- a/lib/point.js +++ b/lib/crypto/point.js @@ -1,3 +1,5 @@ +'use strict'; + var BN = require('./bn'); var elliptic = require('elliptic'); diff --git a/lib/random.js b/lib/crypto/random.js similarity index 96% rename from lib/random.js rename to lib/crypto/random.js index 77573a8fd..f36968559 100644 --- a/lib/random.js +++ b/lib/crypto/random.js @@ -1,5 +1,7 @@ +'use strict'; + function Random() { -}; +} /* secure random bytes that sometimes throws an error due to lack of entropy */ Random.getRandomBuffer = function(size) { @@ -12,7 +14,7 @@ Random.getRandomBuffer = function(size) { Random.getRandomBufferNode = function(size) { var crypto = require('crypto'); return crypto.randomBytes(size); -} +}; Random.getRandomBufferBrowser = function(size) { if (!window.crypto && !window.msCrypto) @@ -36,11 +38,12 @@ Random.getRandomBufferBrowser = function(size) { Random.getPseudoRandomBuffer = function(size) { var b32 = 0x100000000; var b = new Buffer(size); + var r; for (var i = 0; i <= size; i++) { var j = Math.floor(i / 4); var k = i - j * 4; - if (k == 0) { + if (k === 0) { r = Math.random() * b32; b[i] = r & 0xff; } else { diff --git a/lib/expmt/aes.js b/lib/expmt/aes.js deleted file mode 100644 index 4117ed412..000000000 --- a/lib/expmt/aes.js +++ /dev/null @@ -1,47 +0,0 @@ -var aes = require('aes'); - -var AES = function AES() { -}; - -AES.encrypt = function(messagebuf, keybuf) { - var key = AES.buf2words(keybuf); - var message = AES.buf2words(messagebuf); - var a = new aes(key); - var enc = a.encrypt(message); - var encbuf = AES.words2buf(enc); - return encbuf; -}; - -AES.decrypt = function(encbuf, keybuf) { - var enc = AES.buf2words(encbuf); - var key = AES.buf2words(keybuf); - var a = new aes(key); - var message = a.decrypt(enc); - var messagebuf = AES.words2buf(message); - return messagebuf; -}; - -AES.buf2words = function(buf) { - if (buf.length % 4) - throw new Error('buf length must be a multiple of 4'); - - var words = []; - - for (var i = 0; i < buf.length / 4; i++) { - words.push(buf.readUInt32BE(i * 4)); - }; - - return words; -}; - -AES.words2buf = function(words) { - var buf = new Buffer(words.length * 4); - - for (var i = 0; i < words.length; i++) { - buf.writeUInt32BE(words[i], i * 4); - }; - - return buf; -}; - -module.exports = AES; diff --git a/lib/expmt/aescbc.js b/lib/expmt/aescbc.js deleted file mode 100644 index 401cb78d3..000000000 --- a/lib/expmt/aescbc.js +++ /dev/null @@ -1,34 +0,0 @@ -var AES = require('./aes'); -var CBC = require('./cbc'); -var Random = require('../random'); -var Hash = require('../hash'); - -// Symmetric encryption with AES and CBC convenience class -var AESCBC = function AESCBC() { -}; - -AESCBC.encrypt = function(messagebuf, passwordstr) { - var cipherkeybuf = Hash.sha256(new Buffer(passwordstr)); - return AESCBC.encryptCipherkey(messagebuf, cipherkeybuf); -}; - -AESCBC.decrypt = function(encbuf, passwordstr) { - var cipherkeybuf = Hash.sha256(new Buffer(passwordstr)); - return AESCBC.decryptCipherkey(encbuf, cipherkeybuf); -}; - -AESCBC.encryptCipherkey = function(messagebuf, cipherkeybuf, ivbuf) { - ivbuf = ivbuf || Random.getRandomBuffer(128 / 8); - var ctbuf = CBC.encrypt(messagebuf, ivbuf, AES, cipherkeybuf); - var encbuf = Buffer.concat([ivbuf, ctbuf]); - return encbuf; -}; - -AESCBC.decryptCipherkey = function(encbuf, cipherkeybuf) { - var ivbuf = encbuf.slice(0, 128 / 8); - var ctbuf = encbuf.slice(128 / 8); - var messagebuf = CBC.decrypt(ctbuf, ivbuf, AES, cipherkeybuf); - return messagebuf; -}; - -module.exports = AESCBC; diff --git a/lib/expmt/cbc.js b/lib/expmt/cbc.js deleted file mode 100644 index f509ba81f..000000000 --- a/lib/expmt/cbc.js +++ /dev/null @@ -1,138 +0,0 @@ -var Random = require('../random'); - -// Cipher Block Chaining -// http://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Cipher-block_chaining_.28CBC.29 -var CBC = function CBC(blockcipher, cipherkeybuf, ivbuf) { - if (!(this instanceof CBC)) - return new CBC(blockcipher, cipherkeybuf, ivbuf); - - this.blockcipher = blockcipher; - this.cipherkeybuf = cipherkeybuf; - this.ivbuf = ivbuf; -}; - -CBC.buf2blockbufs = function(buf, blocksize) { - var bytesize = blocksize / 8; - var blockbufs = []; - - for (var i = 0; i <= buf.length / bytesize; i++) { - var blockbuf = buf.slice(i * bytesize, i * bytesize + bytesize); - - if (blockbuf.length < blocksize) - blockbuf = CBC.pkcs7pad(blockbuf, blocksize); - - blockbufs.push(blockbuf); - } - - return blockbufs; -}; - -CBC.blockbufs2buf = function(blockbufs, blocksize) { - var bytesize = blocksize / 8; - - var last = blockbufs[blockbufs.length - 1]; - last = CBC.pkcs7unpad(last); - blockbufs[blockbufs.length - 1] = last; - - var buf = Buffer.concat(blockbufs); - - return buf; -}; - -CBC.encrypt = function(messagebuf, ivbuf, blockcipher, cipherkeybuf) { - var blocksize = ivbuf.length * 8; - var blockbufs = CBC.buf2blockbufs(messagebuf, blocksize); - var encbufs = CBC.encryptblocks(blockbufs, ivbuf, blockcipher, cipherkeybuf); - var encbuf = Buffer.concat(encbufs); - return encbuf; -}; - -CBC.decrypt = function(encbuf, ivbuf, blockcipher, cipherkeybuf) { - var blocksize = ivbuf.length * 8; - var bytesize = ivbuf.length; - var encbufs = []; - for (var i = 0; i < encbuf.length / bytesize; i++) { - encbufs.push(encbuf.slice(i * bytesize, i * bytesize + bytesize)); - } - var blockbufs = CBC.decryptblocks(encbufs, ivbuf, blockcipher, cipherkeybuf); - var buf = CBC.blockbufs2buf(blockbufs, blocksize); - return buf; -}; - -CBC.encryptblock = function(blockbuf, ivbuf, blockcipher, cipherkeybuf) { - var xorbuf = CBC.xorbufs(blockbuf, ivbuf); - var encbuf = blockcipher.encrypt(xorbuf, cipherkeybuf); - return encbuf; -}; - -CBC.decryptblock = function(encbuf, ivbuf, blockcipher, cipherkeybuf) { - var xorbuf = blockcipher.decrypt(encbuf, cipherkeybuf); - var blockbuf = CBC.xorbufs(xorbuf, ivbuf); - return blockbuf; -}; - -CBC.encryptblocks = function(blockbufs, ivbuf, blockcipher, cipherkeybuf) { - var encbufs = []; - - for (var i = 0; i < blockbufs.length; i++) { - var blockbuf = blockbufs[i]; - var encbuf = CBC.encryptblock(blockbuf, ivbuf, blockcipher, cipherkeybuf); - - encbufs.push(encbuf); - - ivbuf = encbuf; - } - - return encbufs; -}; - -CBC.decryptblocks = function(encbufs, ivbuf, blockcipher, cipherkeybuf) { - var blockbufs = []; - - for (var i = 0; i < encbufs.length; i++) { - var encbuf = encbufs[i]; - var blockbuf = CBC.decryptblock(encbuf, ivbuf, blockcipher, cipherkeybuf); - - blockbufs.push(blockbuf); - - ivbuf = encbuf; - } - - return blockbufs; -}; - -CBC.pkcs7pad = function(buf, blocksize) { - var bytesize = blocksize / 8; - var padbytesize = bytesize - buf.length; - var pad = new Buffer(padbytesize); - pad.fill(padbytesize); - var paddedbuf = Buffer.concat([buf, pad]); - return paddedbuf; -}; - -CBC.pkcs7unpad = function(paddedbuf, blocksize) { - var bytesize = blocksize / 8; - var padbytesize = bytesize - paddedbuf.length; - var padlength = paddedbuf[paddedbuf.length - 1]; - var padbuf = paddedbuf.slice(paddedbuf.length - padlength, paddedbuf.length); - var padbuf2 = new Buffer(padlength); - padbuf2.fill(padlength); - if (padbuf.toString('hex') !== padbuf2.toString('hex')) - throw new Error('invalid padding'); - return paddedbuf.slice(0, paddedbuf.length - padlength); -}; - -CBC.xorbufs = function(buf1, buf2) { - if (buf1.length !== buf2.length) - throw new Error('bufs must have the same length'); - - var buf = new Buffer(buf1.length); - - for (var i = 0; i < buf1.length; i++) { - buf[i] = buf1[i] ^ buf2[i]; - } - - return buf; -}; - -module.exports = CBC; diff --git a/lib/expmt/ecies.js b/lib/expmt/ecies.js deleted file mode 100644 index 4eb4bff23..000000000 --- a/lib/expmt/ecies.js +++ /dev/null @@ -1,55 +0,0 @@ -var AESCBC = require('./aescbc'); -var Keypair = require('../keypair'); -var Point = require('../point'); -var Hash = require('../hash'); -var Pubkey = require('../pubkey'); -var Privkey = require('../privkey'); - -// http://en.wikipedia.org/wiki/Integrated_Encryption_Scheme -var ECIES = function ECIES() { - if (!(this instanceof ECIES)) - return new ECIES(); -}; - -ECIES.encrypt = function(messagebuf, topubkey, fromkeypair, ivbuf) { - if (!fromkeypair) - fromkeypair = Keypair().fromRandom(); - var r = fromkeypair.privkey.bn; - var R = fromkeypair.pubkey.point; - var Rpubkey = fromkeypair.pubkey; - var Rbuf = Rpubkey.toDER(true); - var KB = topubkey.point; - var P = KB.mul(r); - var S = P.getX(); - var Sbuf = S.toBuffer({size: 32}); - var kEkM = Hash.sha512(Sbuf); - var kE = kEkM.slice(0, 32); - var kM = kEkM.slice(32, 64); - var c = AESCBC.encryptCipherkey(messagebuf, kE, ivbuf); - var d = Hash.sha256hmac(c, kM); - var encbuf = Buffer.concat([Rbuf, c, d]); - return encbuf; -}; - -ECIES.decrypt = function(encbuf, toprivkey) { - var kB = toprivkey.bn; - var frompubkey = Pubkey().fromDER(encbuf.slice(0, 33)); - var R = frompubkey.point; - var P = R.mul(kB); - if (P.eq(new Point())) - throw new Error('P equals 0'); - var S = P.getX(); - var Sbuf = S.toBuffer({size: 32}); - var kEkM = Hash.sha512(Sbuf); - var kE = kEkM.slice(0, 32); - var kM = kEkM.slice(32, 64); - var c = encbuf.slice(33, encbuf.length - 32); - var d = encbuf.slice(encbuf.length - 32, encbuf.length); - var d2 = Hash.sha256hmac(c, kM); - if (d.toString('hex') !== d2.toString('hex')) - throw new Error('Invalid checksum'); - var messagebuf = AESCBC.decryptCipherkey(c, kE); - return messagebuf; -}; - -module.exports = ECIES; diff --git a/lib/expmt/stealthaddress.js b/lib/expmt/stealthaddress.js deleted file mode 100644 index 663516729..000000000 --- a/lib/expmt/stealthaddress.js +++ /dev/null @@ -1,127 +0,0 @@ -var Stealthkey = require('./stealthkey'); -var Base58check = require('../base58check'); -var Pubkey = require('../pubkey'); -var KDF = require('../kdf'); -var BufferWriter = require('../bufferwriter'); -var BufferReader = require('../bufferreader'); - -var StealthAddress = function StealthAddress(addrstr) { - if (!(this instanceof StealthAddress)) - return new StealthAddress(addrstr); - - if (typeof addrstr === 'string') { - this.fromString(addrstr) - } - else if (Buffer.isBuffer(addrstr)) { - var buf = addrstr; - this.fromBuffer(buf); - } - else if (addrstr) { - var obj = addrstr; - this.set(obj); - } -}; - -StealthAddress.mainver = 42; -StealthAddress.testver = 43; - -StealthAddress.prototype.set = function(obj) { - this.payloadPubkey = obj.payloadPubkey || this.payloadPubkey; - this.scanPubkey = obj.scanPubkey || this.scanPubkey; - return this; -}; - -StealthAddress.prototype.fromJSON = function(json) { - this.fromString(json); - return this; -}; - -StealthAddress.prototype.toJSON = function() { - return this.toString(); -}; - -StealthAddress.prototype.fromStealthkey = function(stealthkey) { - this.set({ - payloadPubkey: stealthkey.payloadKeypair.pubkey, - scanPubkey: stealthkey.scanKeypair.pubkey - }); - return this; -}; - -StealthAddress.prototype.fromBuffer = function(buf) { - var parsed = StealthAddress.parseDWBuffer(buf); - if ((parsed.version !== StealthAddress.mainver) && (parsed.version !== StealthAddress.testver)) - throw new Error('Invalid version'); - if (parsed.options !== 0) - throw new Error('Invalid options'); - if (!parsed.scanPubkey) - throw new Error('Invalid scanPubkey'); - if (parsed.payloadPubkeys.length !== 1) - throw new Error('Must have exactly one payloadPubkey'); - if (parsed.nSigs !== 1) - throw new Error('Must require exactly one signature'); - if (parsed.prefix.toString() !== "") - throw new Error('Only blank prefixes supported'); - this.scanPubkey = parsed.scanPubkey; - this.payloadPubkey = parsed.payloadPubkeys[0]; - return this; -}; - -StealthAddress.prototype.fromString = function(str) { - return this.fromBuffer(Base58check(str).toBuffer()); -}; - -StealthAddress.prototype.getSharedKeypair = function(senderKeypair) { - var sharedSecretPoint = this.scanPubkey.point.mul(senderKeypair.privkey.bn); - var sharedSecretPubkey = Pubkey(sharedSecretPoint); - var buf = sharedSecretPubkey.toDER(true); - var sharedKeypair = KDF.sha256hmac2keypair(buf); - - return sharedKeypair; -}; - -StealthAddress.prototype.getReceivePubkey = function(senderKeypair) { - var sharedKeypair = this.getSharedKeypair(senderKeypair); - var pubkey = Pubkey(this.payloadPubkey.point.add(sharedKeypair.pubkey.point)); - - return pubkey; -}; - -StealthAddress.prototype.toBuffer = function(networkstr) { - if (networkstr === 'testnet') - var version = StealthAddress.testver; - else - var version = StealthAddress.mainver; - var bw = new BufferWriter(); - bw.writeUInt8(version); - bw.writeUInt8(0); //options - bw.write(this.scanPubkey.toDER(true)); - bw.writeUInt8(1); //number of payload keys - we only support 1 (not multisig) - bw.write(this.payloadPubkey.toDER(true)); - bw.writeUInt8(1); //number of signatures - we only support 1 (not multisig) - bw.writeUInt8(0); //prefix length - we do not support prefix yet - var buf = bw.concat(); - return buf; -}; - -StealthAddress.prototype.toString = function(networkstr) { - return Base58check(this.toBuffer(networkstr)).toString(); -}; - -StealthAddress.parseDWBuffer = function(buf) { - var br = new BufferReader(buf); - var parsed = {}; - parsed.version = br.readUInt8(); - parsed.options = br.readUInt8(); - parsed.scanPubkey = Pubkey().fromBuffer(br.read(33)); - parsed.nPayloadPubkeys = br.readUInt8(); - parsed.payloadPubkeys = []; - for (var i = 0; i < parsed.nPayloadPubkeys; i++) - parsed.payloadPubkeys.push(Pubkey().fromBuffer(br.read(33))); - parsed.nSigs = br.readUInt8(); - parsed.nPrefix = br.readUInt8(); - parsed.prefix = br.read(parsed.nPrefix / 8); - return parsed; -}; - -module.exports = StealthAddress; diff --git a/lib/expmt/stealthkey.js b/lib/expmt/stealthkey.js deleted file mode 100644 index 7cf4d0f2a..000000000 --- a/lib/expmt/stealthkey.js +++ /dev/null @@ -1,88 +0,0 @@ -var Keypair = require('../keypair'); -var Privkey = require('../privkey'); -var Pubkey = require('../pubkey'); -var Point = require('../point'); -var Hash = require('../hash'); -var KDF = require('../kdf'); - -var Stealthkey = function Stealthkey(payloadKeypair, scanKeypair) { - if (!(this instanceof Stealthkey)) - return new Stealthkey(payloadKeypair, scanKeypair); - - if (payloadKeypair instanceof Keypair) { - this.set({ - payloadKeypair: payloadKeypair, - scanKeypair: scanKeypair - }); - } - else if (payloadKeypair) { - var obj = payloadKeypair; - this.set(obj); - } -}; - -Stealthkey.prototype.set = function(obj) { - this.payloadKeypair = obj.payloadKeypair || this.payloadKeypair; - this.scanKeypair = obj.scanKeypair || this.scanKeypair; - return this; -}; - -Stealthkey.prototype.fromJSON = function(json) { - this.set({ - payloadKeypair: Keypair().fromJSON(json.payloadKeypair), - scanKeypair: Keypair().fromJSON(json.scanKeypair) - }); - return this; -}; - -Stealthkey.prototype.toJSON = function() { - return { - payloadKeypair: this.payloadKeypair.toJSON(), - scanKeypair: this.scanKeypair.toJSON() - }; -}; - -Stealthkey.prototype.fromRandom = function() { - this.payloadKeypair = Keypair().fromRandom(); - this.scanKeypair = Keypair().fromRandom(); - - return this; -}; - -Stealthkey.prototype.getSharedKeypair = function(senderPubkey) { - var sharedSecretPoint = senderPubkey.point.mul(this.scanKeypair.privkey.bn); - var sharedSecretPubkey = Pubkey({point: sharedSecretPoint}); - var buf = sharedSecretPubkey.toDER(true); - var sharedKeypair = KDF.sha256hmac2keypair(buf); - - return sharedKeypair; -}; - -Stealthkey.prototype.getReceivePubkey = function(senderPubkey) { - var sharedKeypair = this.getSharedKeypair(senderPubkey); - var pubkey = Pubkey({point: this.payloadKeypair.pubkey.point.add(sharedKeypair.pubkey.point)}); - - return pubkey; -}; - -Stealthkey.prototype.getReceiveKeypair = function(senderPubkey) { - var sharedKeypair = this.getSharedKeypair(senderPubkey); - var privkey = Privkey({bn: this.payloadKeypair.privkey.bn.add(sharedKeypair.privkey.bn).mod(Point.getN())}); - var key = Keypair({privkey: privkey}); - key.privkey2pubkey(); - - return key; -}; - -Stealthkey.prototype.isForMe = function(senderPubkey, myPossiblePubkeyhashbuf) { - var pubkey = this.getReceivePubkey(senderPubkey); - var pubkeybuf = pubkey.toDER(true); - var pubkeyhash = Hash.sha256ripemd160(pubkeybuf); - - if (pubkeyhash.toString('hex') === myPossiblePubkeyhashbuf.toString('hex')) - return true; - else - return false; -}; - -module.exports = Stealthkey; diff --git a/lib/expmt/stealthmessage.js b/lib/expmt/stealthmessage.js deleted file mode 100644 index 71efd61a5..000000000 --- a/lib/expmt/stealthmessage.js +++ /dev/null @@ -1,85 +0,0 @@ -var Stealthkey = require('./stealthkey'); -var StealthAddress = require('./stealthaddress'); -var ECIES = require('./ecies'); -var Message = require('../message'); -var Keypair = require('../keypair'); -var Address = require('../address'); -var Pubkey = require('../pubkey'); - -var StealthMessage = function StealthMessage(obj) { - if (!(this instanceof StealthMessage)) - return new StealthMessage(obj); - if (obj) - this.set(obj); -}; - -StealthMessage.prototype.set = function(obj) { - this.messagebuf = obj.messagebuf || this.messagebuf; - this.encbuf = obj.encbuf || this.encbuf; - this.toStealthAddress = obj.toStealthAddress || this.toStealthAddress; - this.toStealthkey = obj.toStealthkey || this.toStealthkey; - this.fromKeypair = obj.fromKeypair || this.fromKeypair; - this.receiveAddress = obj.receiveAddress || this.receiveAddress; - return this; -}; - -StealthMessage.encrypt = function(messagebuf, toStealthAddress, fromKeypair, ivbuf) { - var sm = StealthMessage().set({ - messagebuf: messagebuf, - toStealthAddress: toStealthAddress, - fromKeypair: fromKeypair - }); - sm.encrypt(ivbuf); - var buf = Buffer.concat([ - sm.receiveAddress.hashbuf, - sm.fromKeypair.pubkey.toDER(true), - sm.encbuf - ]); - return buf; -}; - -StealthMessage.decrypt = function(buf, toStealthkey) { - var sm = StealthMessage().set({ - toStealthkey: toStealthkey, - receiveAddress: Address().set({hashbuf: buf.slice(0, 20)}), - fromKeypair: Keypair().set({pubkey: Pubkey().fromDER(buf.slice(20, 20 + 33))}), - encbuf: buf.slice(20 + 33) - }); - return sm.decrypt().messagebuf; -}; - -StealthMessage.isForMe = function(buf, toStealthkey) { - var sm = StealthMessage().set({ - toStealthkey: toStealthkey, - receiveAddress: Address().set({hashbuf: buf.slice(0, 20)}), - fromKeypair: Keypair().set({pubkey: Pubkey().fromDER(buf.slice(20, 20 + 33))}), - encbuf: buf.slice(20 + 33) - }); - return sm.isForMe(); -}; - -StealthMessage.prototype.encrypt = function(ivbuf) { - if (!this.fromKeypair) - this.fromKeypair = Keypair().fromRandom(); - var receivePubkey = this.toStealthAddress.getReceivePubkey(this.fromKeypair); - this.receiveAddress = Address().fromPubkey(receivePubkey); - this.encbuf = ECIES.encrypt(this.messagebuf, receivePubkey, this.fromKeypair, ivbuf); - return this; -}; - -StealthMessage.prototype.decrypt = function() { - var receiveKeypair = this.toStealthkey.getReceiveKeypair(this.fromKeypair.pubkey); - this.messagebuf = ECIES.decrypt(this.encbuf, receiveKeypair.privkey); - return this; -}; - -StealthMessage.prototype.isForMe = function() { - var receivePubkey = this.toStealthkey.getReceivePubkey(this.fromKeypair.pubkey); - var receiveAddress = Address().fromPubkey(receivePubkey); - if (receiveAddress.toString('hex') === this.receiveAddress.toString('hex')) - return true; - else - return false; -}; - -module.exports = StealthMessage; diff --git a/lib/expmt/stealthtx.js b/lib/expmt/stealthtx.js deleted file mode 100644 index 01331669d..000000000 --- a/lib/expmt/stealthtx.js +++ /dev/null @@ -1,69 +0,0 @@ -var StealthAddress = require('./stealthaddress'); -var StealthKey = require('./stealthkey'); -var Transaction = require('../transaction'); -var Pubkey = require('../pubkey'); - -var StealthTx = function StealthTx(tx, sa, sk) { - if (!(this instanceof StealthTx)) - return new StealthTx(tx, sa, sk); - if (tx instanceof Transaction) { - this.tx = tx; - this.sa = sa; - this.sk = sk; - } else if (tx) { - var obj = tx; - this.set(obj); - } -}; - -StealthTx.prototype.set = function(obj) { - this.sk = obj.sk || this.sk; - this.sa = obj.sa || this.sa; - this.tx = obj.tx || this.tx; - return this; -}; - -StealthTx.prototype.isForMe = function() { - if (!this.notMine()) - return true; - else - return false; -}; - -StealthTx.prototype.notMine = function() { - var err; - if (err = this.notStealth()) - return "Not stealth: " + err; - var txopbuf = this.tx.txouts[0].script.chunks[1].buf; - var parsed = StealthTx.parseOpReturnData(txopbuf); - var pubkey = parsed.pubkey; - var pubkeyhashbuf = this.tx.txouts[1].script.chunks[2].buf; - var sk = this.sk; - if (sk.isForMe(pubkey, pubkeyhashbuf)) { - return false; - } else { - return "StealthTx not mine"; - } -}; - -//For now, we only support a very limited variety of stealth tx -StealthTx.prototype.notStealth = function() { - var txouts = this.tx.txouts; - if (!(txouts.length >= 2)) - return "Not enough txouts"; - if (!txouts[0].script.isOpReturn()) - return "First txout is not OP_RETURN"; - if (!txouts[1].script.isPubkeyhashOut()) - return "Second txout is not pubkeyhash"; - return false; -}; - -StealthTx.parseOpReturnData = function(buf) { - var parsed = {}; - parsed.version = buf[0]; - parsed.noncebuf = buf.slice(1, 5); - parsed.pubkey = Pubkey().fromBuffer(buf.slice(5, 5 + 33)); - return parsed; -}; - -module.exports = StealthTx; diff --git a/lib/identity.js b/lib/identity.js deleted file mode 100644 index d6c8c535f..000000000 --- a/lib/identity.js +++ /dev/null @@ -1,117 +0,0 @@ -var base58check = require('./base58check'); -var constants = require('./constants'); -var Hash = require('./hash'); - -function Identity(buf) { - // TODO: instantiate identities without providing any configuration - if (!(this instanceof Identity)) return new Identity(buf); - - if (Buffer.isBuffer(buf)) { - this.fromBuffer(buf); - } else if (typeof buf === 'string') { - var str = buf; - this.fromString(str); - } else if (buf) { - var obj = buf; - this.set(obj); - } -}; - -Identity.prototype.set = function(obj) { - this.hashbuf = obj.hashbuf || this.hashbuf || null; - this.networkstr = obj.networkstr || this.networkstr || 'ephemeral'; - this.typestr = obj.typestr || this.typestr || 'identity'; - return this; -}; - -Identity.prototype.fromBuffer = function(buf) { - // Identities are prefix + type + key - if (buf.length !== 1 + 1 + 20) - throw new Error('Identity buffers must be exactly 22 bytes (was '+buf.length+')'); - - var prefix = buf[0]; - var version = buf[1]; - - if (version === constants['ephemeral']['identity']) { - this.networkstr = 'ephemeral'; - this.typestr = 'identity'; - } else if (version === constants['mainnet']['identity']) { - this.networkstr = 'mainnet'; - this.typestr = 'identity'; - } else if (version === constants['testnet']['identity']) { - this.networkstr = 'testnet'; - this.typestr = 'identity'; - } else { - this.networkstr = 'unknown'; - this.typestr = 'unknown'; - } - - if (prefix !== constants['ephemeral']['prefix']) - throw new Error('Identity buffers must contain an identity prefix ('+constants['ephemeral']['prefix']+', was '+ prefix.toString() + ')'); - - this.hashbuf = buf.slice( 2 ); - - return this; -}; - -Identity.prototype.fromHashbuf = function(hashbuf, networkstr, typestr) { - if (hashbuf.length !== 20) - throw new Error('hashbuf must be exactly 20 bytes'); - this.hashbuf = hashbuf; - this.networkstr = networkstr || 'ephemeral'; - this.typestr = typestr || 'identity'; - return this; -}; - -Identity.prototype.fromPubkey = function(pubkey, networkstr) { - this.hashbuf = Hash.sha256ripemd160( pubkey.toBuffer() ); - this.networkstr = networkstr || 'ephemeral'; - this.typestr = 'identity'; - return this; -}; - -Identity.prototype.fromString = function(str) { - var buf = base58check.decode(str); - return this.fromBuffer(buf); -} - -Identity.isValid = function(addrstr) { - try { - var address = new Identity().fromString( addrstr ); - } catch (e) { - return false; - } - return address.isValid(); -}; - -Identity.prototype.isValid = function() { - try { - this.validate(); - return true; - } catch (e) { - return false; - } -}; - -Identity.prototype.toBuffer = function() { - var prefix = new Buffer([ constants[ this.networkstr ][ 'prefix' ] ]) - var version = new Buffer([ constants[ this.networkstr ][ this.typestr ] ]);; - var buf = Buffer.concat([ prefix , version, this.hashbuf ]); - return buf; -}; - -Identity.prototype.toString = function() { - return base58check.encode( this.toBuffer() ); -}; - -Identity.prototype.validate = function() { - if (!Buffer.isBuffer(this.hashbuf) || this.hashbuf.length !== 20) - throw new Error('hash must be a buffer of 20 bytes'); - if (['ephemeral', 'mainnet', 'testnet'].indexOf( this.networkstr )) - throw new Error('networkstr must be "ephemeral", "mainnet", or "testnet"'); - if (this.typestr !== 'identity') - throw new Error('typestr must be "identity"'); - return this; -}; - -module.exports = Identity; diff --git a/lib/kdf.js b/lib/kdf.js deleted file mode 100644 index 6a826eacd..000000000 --- a/lib/kdf.js +++ /dev/null @@ -1,31 +0,0 @@ -var Bn = require('./bn'); -var Privkey = require('./privkey'); -var Point = require('./point'); -var Keypair = require('./keypair'); -var Hash = require('./hash'); - -function KDF() { -}; - -KDF.buf2keypair = function(buf) { - return KDF.sha256hmac2keypair(buf); -}; - -KDF.sha256hmac2keypair = function(buf) { - var privkey = KDF.sha256hmac2privkey(buf); - var keypair = Keypair().fromPrivkey(privkey); - return keypair; -}; - -KDF.sha256hmac2privkey = function(buf) { - var bn; - var concat = new Buffer([]); - do { - var hash = Hash.sha256hmac(buf, concat); - var bn = Bn.fromBuffer(hash); - concat = Buffer.concat([concat, new Buffer(0)]); - } while(!bn.lt(Point.getN())); - return new Privkey({bn: bn}); -}; - -module.exports = KDF; diff --git a/lib/keypair.js b/lib/keypair.js index 5d0da56e8..50b38bcf6 100644 --- a/lib/keypair.js +++ b/lib/keypair.js @@ -1,3 +1,5 @@ +'use strict'; + var Privkey = require('./privkey'); var Pubkey = require('./pubkey'); diff --git a/lib/message.js b/lib/message.js deleted file mode 100644 index 1a32f0b75..000000000 --- a/lib/message.js +++ /dev/null @@ -1,92 +0,0 @@ -var ECDSA = require('./ecdsa'); -var Keypair = require('./keypair'); -var BufferWriter = require('./bufferwriter'); -var Hash = require('./hash'); -var Address = require('./address'); -var Signature = require('./signature'); - -var Message = function Message(obj) { - if (!(this instanceof Message)) - return new Message(obj); - if (obj) - this.set(obj); -}; - -Message.prototype.set = function(obj) { - this.messagebuf = obj.messagebuf || this.messagebuf; - this.keypair = obj.keypair || this.keypair; - this.sig = obj.sig || this.sig; - this.address = obj.address || this.address; - this.verified = typeof obj.verified !== 'undefined' ? obj.verified : this.verified; - return this; -}; - -Message.magicBytes = new Buffer('Bitcoin Signed Message:\n'); - -Message.magicHash = function(messagebuf) { - if (!Buffer.isBuffer(messagebuf)) - throw new Error('messagebuf must be a buffer'); - var bw = new BufferWriter(); - bw.writeVarintNum(Message.magicBytes.length); - bw.write(Message.magicBytes); - bw.writeVarintNum(messagebuf.length); - bw.write(messagebuf); - var buf = bw.concat(); - - var hashbuf = Hash.sha256sha256(buf); - - return hashbuf; -}; - -Message.sign = function(messagebuf, keypair) { - var m = Message({messagebuf: messagebuf, keypair: keypair}); - m.sign(); - var sigbuf = m.sig.toCompact(); - var sigstr = sigbuf.toString('base64'); - return sigstr; -}; - -Message.verify = function(messagebuf, sigstr, address) { - var sigbuf = new Buffer(sigstr, 'base64'); - var message = new Message(); - message.messagebuf = messagebuf; - message.sig = Signature().fromCompact(sigbuf); - message.address = address; - - return message.verify().verified; -}; - -Message.prototype.sign = function() { - var hashbuf = Message.magicHash(this.messagebuf); - var ecdsa = ECDSA({hashbuf: hashbuf, keypair: this.keypair}); - ecdsa.signRandomK(); - ecdsa.calci(); - this.sig = ecdsa.sig; - return this; -}; - -Message.prototype.verify = function() { - var hashbuf = Message.magicHash(this.messagebuf); - - var ecdsa = new ECDSA(); - ecdsa.hashbuf = hashbuf; - ecdsa.sig = this.sig; - ecdsa.keypair = new Keypair(); - ecdsa.keypair.pubkey = ecdsa.sig2pubkey(); - - if (!ecdsa.verify()) { - this.verified = false; - return this; - } - - var address = Address().fromPubkey(ecdsa.keypair.pubkey, undefined, this.sig.compressed); - //TODO: what if livenet/testnet mismatch? - if (address.hashbuf.toString('hex') === this.address.hashbuf.toString('hex')) - this.verified = true; - else - this.verified = false; - - return this; -}; - -module.exports = Message; diff --git a/lib/opcode.js b/lib/opcode.js index c53bdc889..323c154d7 100644 --- a/lib/opcode.js +++ b/lib/opcode.js @@ -1,3 +1,5 @@ +'use strict'; + function Opcode(num) { if (!(this instanceof Opcode)) return new Opcode(num); diff --git a/lib/privkey.js b/lib/privkey.js index 12f0b7cdf..a7ed8709a 100644 --- a/lib/privkey.js +++ b/lib/privkey.js @@ -1,8 +1,10 @@ -var BN = require('./bn'); -var Point = require('./point'); -var constants = require('./constants'); -var base58check = require('./base58check'); -var Random = require('./random'); +'use strict'; + +var BN = require('./crypto/bn'); +var Point = require('./crypto/point'); +var Random = require('./crypto/random'); +var constants = require('./protocol/constants'); +var base58check = require('./protocol/base58check'); var Privkey = function Privkey(bn) { if (!(this instanceof Privkey)) diff --git a/lib/base58.js b/lib/protocol/base58.js similarity index 98% rename from lib/base58.js rename to lib/protocol/base58.js index cf1357314..8e7409ea7 100644 --- a/lib/base58.js +++ b/lib/protocol/base58.js @@ -1,3 +1,5 @@ +'use strict'; + var bs58 = require('bs58'); var Base58 = function Base58(obj) { diff --git a/lib/base58check.js b/lib/protocol/base58check.js similarity index 95% rename from lib/base58check.js rename to lib/protocol/base58check.js index 7b5fb954b..68d00d668 100644 --- a/lib/base58check.js +++ b/lib/protocol/base58check.js @@ -1,5 +1,7 @@ +'use strict'; + var base58 = require('./base58'); -var sha256sha256 = require('./hash').sha256sha256; +var sha256sha256 = require('../crypto/hash').sha256sha256; var Base58Check = function Base58Check(obj) { if (!(this instanceof Base58Check)) diff --git a/lib/bufferreader.js b/lib/protocol/bufferreader.js similarity index 98% rename from lib/bufferreader.js rename to lib/protocol/bufferreader.js index bc2d6c67e..c601f3255 100644 --- a/lib/bufferreader.js +++ b/lib/protocol/bufferreader.js @@ -1,4 +1,6 @@ -var BN = require('./bn'); +'use strict'; + +var BN = require('../crypto/bn'); var BufferReader = function BufferReader(buf) { if (!(this instanceof BufferReader)) diff --git a/lib/bufferwriter.js b/lib/protocol/bufferwriter.js similarity index 99% rename from lib/bufferwriter.js rename to lib/protocol/bufferwriter.js index a61ff901e..3258aaab0 100644 --- a/lib/bufferwriter.js +++ b/lib/protocol/bufferwriter.js @@ -1,4 +1,4 @@ -var BN = require('./bn'); +'use strict'; var BufferWriter = function BufferWriter(obj) { if (!(this instanceof BufferWriter)) diff --git a/lib/constants.js b/lib/protocol/constants.js similarity index 96% rename from lib/constants.js rename to lib/protocol/constants.js index 2c8b7e3e8..f358816a7 100644 --- a/lib/constants.js +++ b/lib/protocol/constants.js @@ -1,3 +1,5 @@ +'use strict'; + exports.mainnet = { pubkeyhash: 0x00, identity: 0x0f, @@ -23,4 +25,4 @@ exports.testnet = { exports.ephemeral = { prefix: 0x0f, identity: 0x02 -} +}; diff --git a/lib/varint.js b/lib/protocol/varint.js similarity index 96% rename from lib/varint.js rename to lib/protocol/varint.js index 5fc367a14..0ba7f106a 100644 --- a/lib/varint.js +++ b/lib/protocol/varint.js @@ -1,6 +1,8 @@ +'use strict'; + var BufferWriter = require('./bufferwriter'); var BufferReader = require('./bufferreader'); -var BN = require('./bn'); +var BN = require('../crypto/bn'); var Varint = function Varint(buf) { if (!(this instanceof Varint)) diff --git a/lib/pubkey.js b/lib/pubkey.js index 3a1bc4457..d9062a4ff 100644 --- a/lib/pubkey.js +++ b/lib/pubkey.js @@ -1,6 +1,7 @@ -var Point = require('./point'); -var bn = require('./bn'); -var privkey = require('./privkey'); +'use strict'; + +var Point = require('./crypto/point'); +var bn = require('./crypto/bn'); var Pubkey = function Pubkey(point) { if (!(this instanceof Pubkey)) diff --git a/lib/script.js b/lib/script.js index 149946c58..377c41a0f 100644 --- a/lib/script.js +++ b/lib/script.js @@ -1,5 +1,7 @@ -var BufferReader = require('./bufferreader'); -var BufferWriter = require('./bufferwriter'); +'use strict'; + +var BufferReader = require('./protocol/bufferreader'); +var BufferWriter = require('./protocol/bufferwriter'); var Opcode = require('./opcode'); var Script = function Script(buf) { diff --git a/lib/signature.js b/lib/signature.js index d4e01e3cc..55d764cab 100644 --- a/lib/signature.js +++ b/lib/signature.js @@ -1,4 +1,6 @@ -var BN = require('./bn'); +'use strict'; + +var BN = require('./crypto/bn'); var Signature = function Signature(r, s) { if (!(this instanceof Signature)) @@ -25,12 +27,14 @@ Signature.prototype.set = function(obj) { Signature.prototype.fromCompact = function(buf) { var compressed = true; + var i = buf.slice(0, 1)[0] - 27 - 4; + //TODO: handle uncompressed pubkeys + /* if (i < 0) { - var compressed = false; + compressed = false; i = i + 4; } - - var i = buf.slice(0, 1)[0] - 27 - 4; //TODO: handle uncompressed pubkeys + */ var b2 = buf.slice(1, 33); var b3 = buf.slice(33, 65); diff --git a/lib/transaction.js b/lib/transaction.js index ffa3ffa12..4959b8fe9 100644 --- a/lib/transaction.js +++ b/lib/transaction.js @@ -1,9 +1,11 @@ +'use strict'; + +var BufferWriter = require('./protocol/bufferwriter'); +var BufferReader = require('./protocol/bufferreader'); +var Varint = require('./protocol/varint'); +var Hash = require('./crypto/hash'); var Txin = require('./txin'); var Txout = require('./txout'); -var BufferWriter = require('./bufferwriter'); -var BufferReader = require('./bufferreader'); -var Varint = require('./varint'); -var Hash = require('./hash'); var Transaction = function Transaction(version, txinsvi, txins, txoutsvi, txouts, nlocktime) { if (!(this instanceof Transaction)) diff --git a/lib/txin.js b/lib/txin.js index 4eba87b4c..0b7085754 100644 --- a/lib/txin.js +++ b/lib/txin.js @@ -1,6 +1,8 @@ -var BufferReader = require('./bufferreader'); -var BufferWriter = require('./bufferwriter'); -var Varint = require('./varint'); +'use strict'; + +var BufferReader = require('./protocol/bufferreader'); +var BufferWriter = require('./protocol/bufferwriter'); +var Varint = require('./protocol/varint'); var Script = require('./script'); var Txin = function Txin(txidbuf, txoutnum, scriptvi, script, seqnum) { diff --git a/lib/txout.js b/lib/txout.js index 05a4c73b8..05a825d18 100644 --- a/lib/txout.js +++ b/lib/txout.js @@ -1,7 +1,9 @@ -var BN = require('./bn'); -var BufferReader = require('./bufferreader'); -var BufferWriter = require('./bufferwriter'); -var Varint = require('./varint'); +'use strict'; + +var BN = require('./crypto/bn'); +var BufferReader = require('./protocol/bufferreader'); +var BufferWriter = require('./protocol/bufferwriter'); +var Varint = require('./protocol/varint'); var Script = require('./script'); var Txout = function Txout(valuebn, scriptvi, script) { diff --git a/package.json b/package.json index 4aa236434..a2f116cc4 100644 --- a/package.json +++ b/package.json @@ -72,9 +72,13 @@ "sha512": "=0.0.1" }, "devDependencies": { + "browserify": "~5.9.1", "chai": "~1.9.1", - "mocha": "~1.21.0", - "browserify": "~5.9.1" + "grunt": "^0.4.5", + "grunt-contrib-watch": "^0.6.1", + "grunt-markdown": "^0.6.1", + "grunt-shell": "^1.1.1", + "mocha": "~1.21.0" }, "license": "MIT" } diff --git a/test/address.js b/test/address.js index 7a95fc824..7eecc1074 100644 --- a/test/address.js +++ b/test/address.js @@ -1,8 +1,11 @@ +'use strict'; + var should = require('chai').should(); -var constants = require('../lib/constants'); -var Pubkey = require('../lib/pubkey'); -var Address = require('../lib/address'); -var Script = require('../lib/script'); + +var bitcore = require('..'); +var Pubkey = bitcore.Pubkey; +var Address = bitcore.Address; +var Script = bitcore.Script; describe('Address', function() { var pubkeyhash = new Buffer('3c3fa3d4adcaf8f52d5b1843975e122548269937', 'hex'); @@ -12,9 +15,9 @@ describe('Address', function() { it('should create a new address object', function() { var address = new Address(); should.exist(address); - address = Address(buf); + address = new Address(buf); should.exist(address); - address = Address(str); + address = new Address(str); should.exist(address); }); diff --git a/test/aes.js b/test/aes.js deleted file mode 100644 index be09a4cb6..000000000 --- a/test/aes.js +++ /dev/null @@ -1,85 +0,0 @@ -var should = require('chai').should(); -var Hash = require('../lib/hash'); -var AES = require('../lib/expmt/aes'); - -describe('AES', function() { - var m128 = Hash.sha256(new Buffer('test1')).slice(0, 128 / 8); - - var k128 = Hash.sha256(new Buffer('test2')).slice(0, 128 / 8); - var k192 = Hash.sha256(new Buffer('test2')).slice(0, 192 / 8); - var k256 = Hash.sha256(new Buffer('test2')).slice(0, 256 / 8); - - var e128 = new Buffer('3477e13884125038f4dc24e9d2cfbbc7', 'hex'); - var e192 = new Buffer('b670954c0e2da1aaa5f9063de04eb961', 'hex'); - var e256 = new Buffer('dd2ce24581183a4a7c0b1068f8bc79f0', 'hex'); - - - describe('@encrypt', function() { - - it('should encrypt with a 128 bit key', function() { - var encbuf = AES.encrypt(m128, k128); - encbuf.toString('hex').should.equal(e128.toString('hex')); - }); - - it('should encrypt with a 192 bit key', function() { - var encbuf = AES.encrypt(m128, k192); - encbuf.toString('hex').should.equal(e192.toString('hex')); - }); - - it('should encrypt with a 256 bit key', function() { - var encbuf = AES.encrypt(m128, k256); - encbuf.toString('hex').should.equal(e256.toString('hex')); - }); - - }); - - describe('@decrypt', function() { - - it('should encrypt/decrypt with a 128 bit key', function() { - var encbuf = AES.encrypt(m128, k128); - var m = AES.decrypt(encbuf, k128); - m.toString('hex').should.equal(m128.toString('hex')); - }); - - it('should encrypt/decrypt with a 192 bit key', function() { - var encbuf = AES.encrypt(m128, k192); - var m = AES.decrypt(encbuf, k192); - m.toString('hex').should.equal(m128.toString('hex')); - }); - - it('should encrypt/decrypt with a 256 bit key', function() { - var encbuf = AES.encrypt(m128, k256); - var m = AES.decrypt(encbuf, k256); - m.toString('hex').should.equal(m128.toString('hex')); - }); - - }); - - describe('@buf2words', function() { - - it('should convert this 4 length buffer into an array', function() { - var buf = new Buffer([0, 0, 0, 0]); - var words = AES.buf2words(buf); - words.length.should.equal(1); - }); - - it('should throw an error on this 5 length buffer', function() { - var buf = new Buffer([0, 0, 0, 0, 0]); - (function() { - var words = AES.buf2words(buf); - }).should.throw(); - }); - - }); - - describe('@words2buf', function() { - - it('should convert this array into a buffer', function() { - var a = [100, 0]; - var buf = AES.words2buf(a); - buf.length.should.equal(8); - }); - - }); - -}); diff --git a/test/aescbc.js b/test/aescbc.js deleted file mode 100644 index a1dca5981..000000000 --- a/test/aescbc.js +++ /dev/null @@ -1,73 +0,0 @@ -var should = require('chai').should(); -var AESCBC = require('../lib/expmt/aescbc'); - -describe('AESCBC', function() { - - describe('@encrypt', function() { - - it('should return encrypt one block', function() { - var password = "password"; - var messagebuf = new Buffer(128 / 8 - 1); - messagebuf.fill(0); - var encbuf = AESCBC.encrypt(messagebuf, password); - encbuf.length.should.equal(128 / 8 + 128 / 8); - }); - - }); - - describe('@decrypt', function() { - - it('should decrypt that which was encrypted', function() { - var password = "password"; - var messagebuf = new Buffer(128 / 8 - 1); - messagebuf.fill(0); - var encbuf = AESCBC.encrypt(messagebuf, password); - var messagebuf2 = AESCBC.decrypt(encbuf, password); - messagebuf2.toString('hex').should.equal(messagebuf.toString('hex')); - }); - - }); - - describe('@encryptCipherkey', function() { - - it('should return encrypt one block', function() { - var cipherkeybuf = new Buffer(256 / 8); - cipherkeybuf.fill(0x10); - var ivbuf = new Buffer(128 / 8); - ivbuf.fill(0); - var messagebuf = new Buffer(128 / 8 - 1); - messagebuf.fill(0); - var encbuf = AESCBC.encryptCipherkey(messagebuf, cipherkeybuf, ivbuf); - encbuf.length.should.equal(128 / 8 + 128 / 8); - }); - - it('should return encrypt two blocks', function() { - var cipherkeybuf = new Buffer(256 / 8); - cipherkeybuf.fill(0x10); - var ivbuf = new Buffer(128 / 8); - ivbuf.fill(0); - var messagebuf = new Buffer(128 / 8); - messagebuf.fill(0); - var encbuf = AESCBC.encryptCipherkey(messagebuf, cipherkeybuf, ivbuf); - encbuf.length.should.equal(128 / 8 + 128 / 8 + 128 / 8); - }); - - }); - - describe('@decryptCipherkey', function() { - - it('should decrypt that which was encrypted', function() { - var cipherkeybuf = new Buffer(256 / 8); - cipherkeybuf.fill(0x10); - var ivbuf = new Buffer(128 / 8); - ivbuf.fill(0); - var messagebuf = new Buffer(128 / 8); - messagebuf.fill(0); - var encbuf = AESCBC.encryptCipherkey(messagebuf, cipherkeybuf, ivbuf); - var messagebuf2 = AESCBC.decryptCipherkey(encbuf, cipherkeybuf); - messagebuf2.toString('hex').should.equal(messagebuf.toString('hex')); - }); - - }); - -}); diff --git a/test/bip32.js b/test/bip32.js index a843c48b1..346433875 100644 --- a/test/bip32.js +++ b/test/bip32.js @@ -1,12 +1,15 @@ +'use strict'; + var should = require('chai').should(); -var constants = require('../lib/constants'); -var BIP32 = require('../lib/bip32'); +var bitcore = require('..'); +var constants = bitcore.constants; +var BIP32 = bitcore.BIP32; describe('BIP32', function() { //test vectors: https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki var vector1_master = '000102030405060708090a0b0c0d0e0f'; - var vector1_m_public = 'xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8' + var vector1_m_public = 'xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8'; var vector1_m_private = 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi'; var vector1_m0h_public = 'xpub68Gmy5EdvgibQVfPdqkBBCHxA5htiqg55crXYuXoQRKfDBFA1WEjWgP6LHhwBZeNK1VTsfTFUHCdrfp1bgwQ9xv5ski8PX9rL2dZXvgGDnw'; var vector1_m0h_private = 'xprv9uHRZZhk6KAJC1avXpDAp4MDc3sQKNxDiPvvkX8Br5ngLNv1TxvUxt4cV1rGL5hj6KCesnDYUhd7oWgT11eZG7XnxHrnYeSvkzY7d2bhkJ7'; diff --git a/test/block.js b/test/block.js index 85de6e97e..6daeccf6a 100644 --- a/test/block.js +++ b/test/block.js @@ -1,25 +1,25 @@ -var Blockheader = require('../lib/blockheader'); -var Block = require('../lib/block'); -var BufferWriter = require('../lib/bufferwriter'); -var BufferReader = require('../lib/bufferreader'); -var Varint = require('../lib/varint'); +'use strict'; + var should = require('chai').should(); -var Transaction = require('../lib/transaction'); +var bitcore = require('..'); +var Blockheader = bitcore.Blockheader; +var Block = bitcore.Block; +var BufferReader = bitcore.BufferReader; +var Varint = bitcore.Varint; +var Transaction = bitcore.Transaction; describe('Block', function() { - var txidhex = '8c9aa966d35bfeaf031409e0001b90ccdafd8d859799eb945a3c515b8260bcf2'; var txhex = '01000000029e8d016a7b0dc49a325922d05da1f916d1e4d4f0cb840c9727f3d22ce8d1363f000000008c493046022100e9318720bee5425378b4763b0427158b1051eec8b08442ce3fbfbf7b30202a44022100d4172239ebd701dae2fbaaccd9f038e7ca166707333427e3fb2a2865b19a7f27014104510c67f46d2cbb29476d1f0b794be4cb549ea59ab9cc1e731969a7bf5be95f7ad5e7f904e5ccf50a9dc1714df00fbeb794aa27aaff33260c1032d931a75c56f2ffffffffa3195e7a1ab665473ff717814f6881485dc8759bebe97e31c301ffe7933a656f020000008b48304502201c282f35f3e02a1f32d2089265ad4b561f07ea3c288169dedcf2f785e6065efa022100e8db18aadacb382eed13ee04708f00ba0a9c40e3b21cf91da8859d0f7d99e0c50141042b409e1ebbb43875be5edde9c452c82c01e3903d38fa4fd89f3887a52cb8aea9dc8aec7e2c9d5b3609c03eb16259a2537135a1bf0f9c5fbbcbdbaf83ba402442ffffffff02206b1000000000001976a91420bb5c3bfaef0231dc05190e7f1c8e22e098991e88acf0ca0100000000001976a9149e3e2d23973a04ec1b02be97c30ab9f2f27c3b2c88ac00000000'; var txbuf = new Buffer(txhex, 'hex'); - var tx = Transaction().fromBuffer(txbuf); var magicnum = 0xd9b4bef9; var blocksize = 50; - bhhex = '0100000005050505050505050505050505050505050505050505050505050505050505050909090909090909090909090909090909090909090909090909090909090909020000000300000004000000'; - bhbuf = new Buffer(bhhex, 'hex'); - var bh = Blockheader().fromBuffer(bhbuf); - var txsvi = Varint(1); - var txs = [Transaction().fromBuffer(txbuf)]; - var block = Block().set({ + var bhhex = '0100000005050505050505050505050505050505050505050505050505050505050505050909090909090909090909090909090909090909090909090909090909090909020000000300000004000000'; + var bhbuf = new Buffer(bhhex, 'hex'); + var bh = new Blockheader().fromBuffer(bhbuf); + var txsvi = new Varint(1); + var txs = [new Transaction().fromBuffer(txbuf)]; + var block = new Block().set({ magicnum: magicnum, blocksize: blocksize, blockheader: bh, diff --git a/test/blockheader.js b/test/blockheader.js index 6fec516bc..6647f0ede 100644 --- a/test/blockheader.js +++ b/test/blockheader.js @@ -1,7 +1,9 @@ -var Blockheader = require('../lib/blockheader'); -var BufferWriter = require('../lib/bufferwriter'); -var BufferReader = require('../lib/bufferreader'); +'use strict'; + var should = require('chai').should(); +var bitcore = require('..'); +var Blockheader = bitcore.Blockheader; +var BufferReader = bitcore.BufferReader; describe('Blockheader', function() { @@ -22,8 +24,8 @@ describe('Blockheader', function() { bits: bits, nonce: nonce }); - bhhex = '0100000005050505050505050505050505050505050505050505050505050505050505050909090909090909090909090909090909090909090909090909090909090909020000000300000004000000'; - bhbuf = new Buffer(bhhex, 'hex'); + var bhhex = '0100000005050505050505050505050505050505050505050505050505050505050505050909090909090909090909090909090909090909090909090909090909090909020000000300000004000000'; + var bhbuf = new Buffer(bhhex, 'hex'); it('should make a new blockheader', function() { var blockheader = new Blockheader(); diff --git a/test/cbc.js b/test/cbc.js deleted file mode 100644 index 1b163f50c..000000000 --- a/test/cbc.js +++ /dev/null @@ -1,317 +0,0 @@ -var AES = require('../lib/expmt/aes'); -var should = require('chai').should(); -var CBC = require('../lib/expmt/cbc'); - -describe('CBC', function() { - - it('should return a new CBC', function() { - var cbc = new CBC(); - should.exist(cbc); - }) - - it('should return a new CBC when called without "new"', function() { - var cbc = new CBC(); - should.exist(cbc); - }); - - describe('@buf2blockbufs', function() { - - it('should convert this buffer into one block', function() { - var buf = new Buffer(16 - 1); - buf.fill(0); - var blockbufs = CBC.buf2blockbufs(buf, 16 * 8); - blockbufs.length.should.equal(1); - blockbufs[0].toString('hex').should.equal('00000000000000000000000000000001'); - }); - - it('should convert this buffer into two blocks', function() { - var buf = new Buffer(16); - buf.fill(0); - var blockbufs = CBC.buf2blockbufs(buf, 16 * 8); - blockbufs.length.should.equal(2); - blockbufs[0].toString('hex').should.equal('00000000000000000000000000000000'); - blockbufs[1].toString('hex').should.equal('10101010101010101010101010101010'); - }); - - }); - - describe('@buf2blockbufs', function() { - - it('should convert this buffer into one block and back into the same buffer', function() { - var buf = new Buffer(16 - 1); - buf.fill(0); - var blockbufs = CBC.buf2blockbufs(buf, 16 * 8); - var buf2 = CBC.blockbufs2buf(blockbufs, 16 * 8); - buf2.toString('hex').should.equal(buf.toString('hex')); - }); - - it('should convert this buffer into two blocks and back into the same buffer', function() { - var buf = new Buffer(16); - buf.fill(0); - var blockbufs = CBC.buf2blockbufs(buf, 16 * 8); - var buf2 = CBC.blockbufs2buf(blockbufs, 16 * 8); - buf2.toString('hex').should.equal(buf.toString('hex')); - }); - - }); - - describe('@encrypt', function() { - - it('should return this known value', function() { - var messagebuf1 = new Buffer(128 / 8); - messagebuf1.fill(0); - var messagebuf2 = new Buffer(128 / 8); - messagebuf2.fill(0x10); - var messagebuf = Buffer.concat([messagebuf1, messagebuf2]); - var ivbuf = new Buffer(128 / 8); - ivbuf.fill(0x10); - var cipherkeybuf = new Buffer(128 / 8); - cipherkeybuf.fill(0); - var blockcipher = {}; - blockcipher.encrypt = function(messagebuf, cipherkeybuf) { - return messagebuf; - }; - blockcipher.decrypt = function(messagebuf, cipherkeybuf) { - return messagebuf; - }; - var encbuf = CBC.encrypt(messagebuf, ivbuf, blockcipher, cipherkeybuf); - var buf2 = CBC.decrypt(encbuf, ivbuf, blockcipher, cipherkeybuf); - }); - - it('should return this shorter known value', function() { - var messagebuf1 = new Buffer(128 / 8); - messagebuf1.fill(0); - var messagebuf2 = new Buffer(120 / 8); - messagebuf2.fill(0x10); - var messagebuf = Buffer.concat([messagebuf1, messagebuf2]); - var ivbuf = new Buffer(128 / 8); - ivbuf.fill(0x10); - var cipherkeybuf = new Buffer(128 / 8); - cipherkeybuf.fill(0); - var blockcipher = {}; - blockcipher.encrypt = function(messagebuf, cipherkeybuf) { - return messagebuf; - }; - blockcipher.decrypt = function(messagebuf, cipherkeybuf) { - return messagebuf; - }; - var encbuf = CBC.encrypt(messagebuf, ivbuf, blockcipher, cipherkeybuf); - var buf2 = CBC.decrypt(encbuf, ivbuf, blockcipher, cipherkeybuf); - }); - - it('should return this shorter known value', function() { - var messagebuf1 = new Buffer(128 / 8); - messagebuf1.fill(0); - var messagebuf2 = new Buffer(136 / 8); - messagebuf2.fill(0x10); - var messagebuf = Buffer.concat([messagebuf1, messagebuf2]); - var ivbuf = new Buffer(128 / 8); - ivbuf.fill(0x10); - var cipherkeybuf = new Buffer(128 / 8); - cipherkeybuf.fill(0); - var blockcipher = {}; - blockcipher.encrypt = function(messagebuf, cipherkeybuf) { - return messagebuf; - }; - blockcipher.decrypt = function(messagebuf, cipherkeybuf) { - return messagebuf; - }; - var encbuf = CBC.encrypt(messagebuf, ivbuf, blockcipher, cipherkeybuf); - var buf2 = CBC.decrypt(encbuf, ivbuf, blockcipher, cipherkeybuf); - }); - - it('should encrypt something with AES', function() { - var messagebuf1 = new Buffer(128 / 8); - messagebuf1.fill(0); - var messagebuf2 = new Buffer(128 / 8); - messagebuf2.fill(0x10); - var messagebuf = Buffer.concat([messagebuf1, messagebuf2]); - var ivbuf = new Buffer(128 / 8); - ivbuf.fill(0x10); - var cipherkeybuf = new Buffer(128 / 8); - cipherkeybuf.fill(0); - var blockcipher = AES; - var encbuf = CBC.encrypt(messagebuf, ivbuf, blockcipher, cipherkeybuf); - var buf2 = CBC.decrypt(encbuf, ivbuf, blockcipher, cipherkeybuf); - buf2.toString('hex').should.equal(messagebuf.toString('hex')); - }); - - }); - - describe('@decrypt', function() { - - it('should properly decrypt an encrypted message', function() { - var messagebuf1 = new Buffer(128 / 8); - messagebuf1.fill(0); - var messagebuf2 = new Buffer(128 / 8); - messagebuf2.fill(0x10); - var messagebuf = Buffer.concat([messagebuf1, messagebuf2]); - var ivbuf = new Buffer(128 / 8); - ivbuf.fill(0x10); - var cipherkeybuf = new Buffer(128 / 8); - cipherkeybuf.fill(0); - var blockcipher = {}; - blockcipher.encrypt = function(messagebuf, cipherkeybuf) { - return messagebuf; - }; - blockcipher.decrypt = function(messagebuf, cipherkeybuf) { - return messagebuf; - }; - var encbuf = CBC.encrypt(messagebuf, ivbuf, blockcipher, cipherkeybuf); - var messagebuf2 = CBC.decrypt(encbuf, ivbuf, blockcipher, cipherkeybuf); - messagebuf2.toString('hex').should.equal(messagebuf.toString('hex')); - }); - - }); - - describe('@encryptblock', function() { - - it('should return this known value', function() { - var messagebuf = new Buffer(128 / 8); - messagebuf.fill(0); - var ivbuf = new Buffer(128 / 8); - ivbuf.fill(0x10); - var cipherkeybuf = new Buffer(128 / 8); - cipherkeybuf.fill(0); - var blockcipher = {}; - blockcipher.encrypt = function(messagebuf, cipherkeybuf) { - return messagebuf; - }; - var enc = CBC.encryptblock(messagebuf, ivbuf, blockcipher, cipherkeybuf); - enc.toString('hex').should.equal(ivbuf.toString('hex')); - }); - - it('should return this other known value', function() { - var messagebuf = new Buffer(128 / 8); - messagebuf.fill(0x10); - var ivbuf = new Buffer(128 / 8); - ivbuf.fill(0x10); - var cipherkeybuf = new Buffer(128 / 8); - cipherkeybuf.fill(0); - var blockcipher = {}; - blockcipher.encrypt = function(messagebuf, cipherkeybuf) { - return messagebuf; - }; - var enc = CBC.encryptblock(messagebuf, ivbuf, blockcipher, cipherkeybuf); - enc.toString('hex').should.equal('00000000000000000000000000000000'); - }); - - }); - - describe('@decryptblock', function() { - - it('should decrypt an encrypted block', function() { - var messagebuf = new Buffer(128 / 8); - messagebuf.fill(0); - var ivbuf = new Buffer(128 / 8); - ivbuf.fill(0x10); - var cipherkeybuf = new Buffer(128 / 8); - cipherkeybuf.fill(0); - var blockcipher = {}; - blockcipher.encrypt = function(messagebuf, cipherkeybuf) { - return messagebuf; - }; - blockcipher.decrypt = function(messagebuf, cipherkeybuf) { - return messagebuf; - }; - var encbuf = CBC.encryptblock(messagebuf, ivbuf, blockcipher, cipherkeybuf); - var buf = CBC.decryptblock(encbuf, ivbuf, blockcipher, cipherkeybuf); - buf.toString('hex').should.equal(messagebuf.toString('hex')); - }); - - }); - - describe('@encryptblocks', function() { - - it('should return this known value', function() { - var messagebuf1 = new Buffer(128 / 8); - messagebuf1.fill(0); - var messagebuf2 = new Buffer(128 / 8); - messagebuf2.fill(0x10); - var ivbuf = new Buffer(128 / 8); - ivbuf.fill(0x10); - var cipherkeybuf = new Buffer(128 / 8); - cipherkeybuf.fill(0); - var blockcipher = {} - blockcipher.encrypt = function(messagebuf, cipherkeybuf) { - return messagebuf; - }; - var encbufs = CBC.encryptblocks([messagebuf1, messagebuf2], ivbuf, blockcipher, cipherkeybuf); - encbufs[0].toString('hex').should.equal('10101010101010101010101010101010'); - encbufs[1].toString('hex').should.equal('00000000000000000000000000000000'); - }); - - }); - - describe('@decryptblocks', function() { - - it('should decrypt encrypted blocks', function() { - var messagebuf1 = new Buffer(128 / 8); - messagebuf1.fill(0); - var messagebuf2 = new Buffer(128 / 8); - messagebuf2.fill(0x10); - var ivbuf = new Buffer(128 / 8); - ivbuf.fill(0x10); - var cipherkeybuf = new Buffer(128 / 8); - cipherkeybuf.fill(0); - var blockcipher = {} - blockcipher.encrypt = function(messagebuf, cipherkeybuf) { - return messagebuf; - }; - blockcipher.decrypt = function(messagebuf, cipherkeybuf) { - return messagebuf; - }; - var encbufs = CBC.encryptblocks([messagebuf1, messagebuf2], ivbuf, blockcipher, cipherkeybuf); - var bufs = CBC.decryptblocks(encbufs, ivbuf, blockcipher, cipherkeybuf); - bufs[0].toString('hex').should.equal(messagebuf1.toString('hex')); - bufs[1].toString('hex').should.equal(messagebuf2.toString('hex')); - }); - - }); - - describe('@pkcs7pad', function() { - - it('should pad this 32 bit buffer to 128 bits with the number 128/8 - 32/8', function() { - var buf = new Buffer(32 / 8); - buf.fill(0); - var padbuf = CBC.pkcs7pad(buf, 128); - padbuf.length.should.equal(128 / 8); - padbuf[32 / 8].should.equal(128 / 8 - 32 / 8); - padbuf[32 / 8 + 1].should.equal(128 / 8 - 32 / 8); - // ... - padbuf[32 / 8 + 128 / 8 - 32 / 8 - 1].should.equal(128 / 8 - 32 / 8); - }); - - }); - - describe('@pkcs7unpad', function() { - - it('should unpad this padded 32 bit buffer', function() { - var buf = new Buffer(32 / 8); - buf.fill(0); - var paddedbuf = CBC.pkcs7pad(buf, 128); - var unpaddedbuf = CBC.pkcs7unpad(paddedbuf, 128); - unpaddedbuf.toString('hex').should.equal(buf.toString('hex')); - }); - - }); - - describe('@xorbufs', function() { - - it('should xor 1 and 0', function() { - var buf1 = new Buffer([1]); - var buf2 = new Buffer([0]); - var buf = CBC.xorbufs(buf1, buf2); - buf[0].should.equal(1); - }); - - it('should xor 1 and 1', function() { - var buf1 = new Buffer([1]); - var buf2 = new Buffer([1]); - var buf = CBC.xorbufs(buf1, buf2); - buf[0].should.equal(0); - }); - - }); - -}); diff --git a/test/bn.js b/test/crypto/bn.js similarity index 96% rename from test/bn.js rename to test/crypto/bn.js index 03ef279a1..7a72b5d27 100644 --- a/test/bn.js +++ b/test/crypto/bn.js @@ -1,7 +1,8 @@ -var chai = chai || require('chai'); -var should = chai.should(); -var assert = chai.assert; -var BN = require('../lib/bn'); +'use strict'; + +var should = require('chai').should(); +var bitcore = require('../..'); +var BN = bitcore.BN; describe('BN', function() { it('should create a bn', function() { @@ -18,7 +19,7 @@ describe('BN', function() { it('should parse numbers below and at bn.js internal word size', function() { var bn = new BN(Math.pow(2, 26) - 1); bn.toString().should.equal((Math.pow(2, 26) - 1).toString()); - var bn = new BN(Math.pow(2, 26)); + bn = new BN(Math.pow(2, 26)); bn.toString().should.equal((Math.pow(2, 26)).toString()); }); diff --git a/test/ecdsa.js b/test/crypto/ecdsa.js similarity index 95% rename from test/ecdsa.js rename to test/crypto/ecdsa.js index 5de755ed6..f9436a30f 100644 --- a/test/ecdsa.js +++ b/test/crypto/ecdsa.js @@ -1,17 +1,21 @@ -var ECDSA = require('../lib/ecdsa'); -var Hash = require('../lib/hash'); -var Keypair = require('../lib/keypair'); -var Privkey = require('../lib/privkey'); -var Pubkey = require('../lib/pubkey'); -var Signature = require('../lib/signature'); -var BN = require('../lib/bn'); -var point = require('../lib/point'); -var should = require('chai').should(); +'use strict'; -describe("ECDSA", function() { +var should = require('chai').should(); +var bitcore = require('../..'); +var ECDSA = bitcore.ECDSA; +var Hash = bitcore.Hash; +var Keypair = bitcore.Keypair; +var Privkey = bitcore.Privkey; +var Pubkey = bitcore.Pubkey; +var Signature = bitcore.Signature; +var BN = bitcore.BN; +var point = bitcore.Point; + +describe('ECDSA', function() { it('should create a blank ecdsa', function() { var ecdsa = new ECDSA(); + should.exist(ecdsa); }); var ecdsa = new ECDSA(); diff --git a/test/hash.js b/test/crypto/hash.js similarity index 96% rename from test/hash.js rename to test/crypto/hash.js index f146a6235..816aa6b4f 100644 --- a/test/hash.js +++ b/test/crypto/hash.js @@ -1,9 +1,12 @@ -var should = require('chai').should(); -var Hash = require('../lib/hash'); +'use strict'; + +require('chai').should(); +var bitcore = require('../..'); +var Hash = bitcore.Hash; describe('Hash', function() { var buf = new Buffer([0, 1, 2, 3, 253, 254, 255]); - var str = "test string"; + var str = 'test string'; describe('#sha256', function() { diff --git a/test/point.js b/test/crypto/point.js similarity index 97% rename from test/point.js rename to test/crypto/point.js index 6c9c8189f..b8d2406a0 100644 --- a/test/point.js +++ b/test/crypto/point.js @@ -1,6 +1,9 @@ +'use strict'; + var should = require('chai').should(); -var point = require('../lib/point'); -var BN = require('../lib/bn'); +var bitcore = require('../..'); +var point = bitcore.Point; +var BN = bitcore.BN; describe('Point', function() { diff --git a/test/random.js b/test/crypto/random.js similarity index 94% rename from test/random.js rename to test/crypto/random.js index be715ba48..e92afcc98 100644 --- a/test/random.js +++ b/test/crypto/random.js @@ -1,5 +1,8 @@ +'use strict'; + var should = require('chai').should(); -var Random = require('../lib/random'); +var bitcore = require('../..'); +var Random = bitcore.Random; describe('Random', function() { @@ -21,7 +24,7 @@ describe('Random', function() { var hexs = []; for (var i = 0; i < 100; i++) hexs[i] = Random.getRandomBuffer(8).toString('hex'); - for (var i = 0; i < 100; i++) + for (i = 0; i < 100; i++) for (var j = i + 1; j < 100; j++) hexs[i].should.not.equal(hexs[j]); }); diff --git a/test/ecies.js b/test/ecies.js deleted file mode 100644 index 5e8078bd7..000000000 --- a/test/ecies.js +++ /dev/null @@ -1,52 +0,0 @@ -var ECIES = require('../lib/expmt/ecies'); -var should = require('chai').should(); -var Keypair = require('../lib/keypair'); -var Hash = require('../lib/hash'); - -describe('#ECIES', function() { - - it('should make a new ECIES object', function() { - var ecies = new ECIES(); - should.exist(ecies); - }); - - it('should make a new ECIES object when called without "new"', function() { - var ecies = ECIES(); - should.exist(ecies); - }); - - var fromkey = Keypair().fromRandom(); - var tokey = Keypair().fromRandom(); - var messagebuf = Hash.sha256(new Buffer('my message is the hash of this string')); - - describe('@encrypt', function() { - - it('should return a buffer', function() { - var encbuf = ECIES.encrypt(messagebuf, tokey.pubkey, fromkey); - Buffer.isBuffer(encbuf).should.equal(true); - }); - - it('should return a buffer if fromkey is not present', function() { - var encbuf = ECIES.encrypt(messagebuf, tokey.pubkey); - Buffer.isBuffer(encbuf).should.equal(true); - }); - - }); - - describe('@decrypt', function() { - - it('should decrypt that which was encrypted', function() { - var encbuf = ECIES.encrypt(messagebuf, tokey.pubkey, fromkey); - var messagebuf2 = ECIES.decrypt(encbuf, tokey.privkey); - messagebuf2.toString('hex').should.equal(messagebuf.toString('hex')); - }); - - it('should decrypt that which was encrypted if fromkeypair was randomly generated', function() { - var encbuf = ECIES.encrypt(messagebuf, tokey.pubkey); - var messagebuf2 = ECIES.decrypt(encbuf, tokey.privkey); - messagebuf2.toString('hex').should.equal(messagebuf.toString('hex')); - }); - - }); - -}); diff --git a/test/examples.js b/test/examples.js index 4216361f7..859102614 100644 --- a/test/examples.js +++ b/test/examples.js @@ -1,4 +1,6 @@ -if (process.browser) +'use strict'; + +if (process.browser || true) // no examples yet return; //examples are loaded from files, which doesn't work in the browser var should = require('chai').should(); @@ -9,11 +11,8 @@ describe('Examples', function() { var filenames = fs.readdirSync(__dirname + '/../examples/'); filenames.forEach(function(filename) { - if (filename.slice(filename.length - 3) === '.js') { - describe(filename, function() { - it('should not throw any errors', function() { (function() { var save = console.log; @@ -22,11 +21,7 @@ describe('Examples', function() { console.log = save; }).should.not.throw(); }); - }); - } - }); - }); diff --git a/test/identity.js b/test/identity.js deleted file mode 100644 index 4e2f75b5f..000000000 --- a/test/identity.js +++ /dev/null @@ -1,171 +0,0 @@ -var should = require('chai').should(); -var constants = require('../lib/constants'); -var PubKey = require('../lib/pubkey'); -var Identity = require('../lib/identity'); - -describe('Identity', function() { - - var knownPrivKey = 'L3e3ZneXzGw2wyyRoUxKGGrHCBhBE3uPMvQDXPaJTom4d4ogRxvC'; - var knownPubKey = '02ff0c643214634691e6f1c5044df79f7002c404407c8db1897484017e1082f182'; - var knownPubKeyHash = 'bceb8b52237d7a6c09e9aaedcf26cf387530d23e'; - var knownIdent = 'TfEmMAA5PSQRRJgiZka8y6B5x1pABHe6BVv'; - var knownIdentComp = 'TfDBCwB4ciatE4Kx3r1TK5kfCTxrrpG1H8J'; - - var pubkeyhash = new Buffer( knownPubKeyHash , 'hex'); - - //var buf = Buffer.concat([ new Buffer([0]), new Buffer([0]), pubkeyhash ]); - // note: key is wrong string until I figure out how to duplicate the generation of short keys - //var buf = Buffer.concat([ new Buffer( 0x0f ) , new Buffer( 0x02 ) , pubkeyhash ]) - var buf = Buffer.concat([ new Buffer([0x0f]) , new Buffer([0x02]) , pubkeyhash ]) - var str = knownIdent; - - it('should create a new identity object', function() { - var identity = new Identity(); - should.exist(identity); - identity = Identity(buf); - should.exist(identity); - identity = Identity(str); - should.exist(identity); - }); - - describe('@isValid', function() { - - it('should validate this valid identity string', function() { - Identity.isValid( str ).should.equal( true ); - }); - - it('should invalidate this valid identity string', function() { - Identity.isValid(str.substr(1)).should.equal(false); - }); - - }); - - describe('#fromBuffer', function() { - - it('should make an identity from a buffer', function() { - Identity().fromBuffer(buf).toString().should.equal(str); - }); - - }); - - describe('#fromHashbuf', function() { - - it('should make an identity from a hashbuf', function() { - Identity().fromHashbuf(pubkeyhash).toString().should.equal(str); - var a = Identity().fromHashbuf(pubkeyhash, 'testnet', 'scripthash'); - a.networkstr.should.equal('testnet'); - a.typestr.should.equal('scripthash'); - }); - - it('should throw an error for invalid length hashbuf', function() { - (function() { - Identity().fromHashbuf(buf); - }).should.throw('hashbuf must be exactly 20 bytes'); - }); - - }); - - describe('#fromPubkey', function() { - - it('should make this identity from a compressed pubkey', function() { - var pubkey = new PubKey(); - pubkey.fromDER(new Buffer( knownPubKey , 'hex')); - var identity = new Identity(); - identity.fromPubkey(pubkey); - identity.toString().should.equal( knownIdent ); - }); - - it('should make this identity from an uncompressed pubkey', function() { - var pubkey = new PubKey(); - pubkey.fromDER(new Buffer( knownPubKey , 'hex')); - var identity = new Identity(); - pubkey.compressed = false; - identity.fromPubkey(pubkey, 'ephemeral'); - identity.toString().should.equal( knownIdentComp ); - }); - - }); - - describe('#fromString', function() { - - it('should derive from this known ephemeral identity string', function() { - var identity = new Identity(); - identity.fromString( str ); - identity.toBuffer().slice(2).toString('hex').should.equal(pubkeyhash.toString('hex')); - }); - - }); - - describe('#isValid', function() { - - it('should describe this valid identity as valid', function() { - var identity = new Identity(); - identity.fromString( knownIdent ); - identity.isValid().should.equal(true); - }); - - it('should describe this identity with unknown network as invalid', function() { - var identity = new Identity(); - identity.fromString( knownIdent ); - identity.networkstr = 'unknown'; - identity.isValid().should.equal(false); - }); - - it('should describe this identity with unknown type as invalid', function() { - var identity = new Identity(); - identity.fromString( knownIdent ); - identity.typestr = 'unknown'; - identity.isValid().should.equal(false); - }); - - }); - - describe('#toBuffer', function() { - - it('should output this known hash', function() { - var identity = new Identity(); - identity.fromString(str); - identity.toBuffer().slice(2).toString('hex').should.equal(pubkeyhash.toString('hex')); - }); - - }); - - describe('#toString', function() { - - it('should output the same thing that was input', function() { - var identity = new Identity(); - identity.fromString(str); - identity.toString().should.equal(str); - }); - - }); - - describe('#validate', function() { - - it('should not throw an error on this valid identity', function() { - var identity = new Identity(); - identity.fromString(str); - should.exist(identity.validate()); - }); - - it('should throw an error on this invalid network', function() { - var identity = new Identity(); - identity.fromString(str); - identity.networkstr = 'unknown'; - (function() { - identity.validate(); - }).should.throw('networkstr must be "ephemeral", "mainnet", or "testnet"'); - }); - - it('should throw an error on this invalid type', function() { - var identity = new Identity(); - identity.fromString(str); - identity.typestr = 'unknown'; - (function() { - identity.validate(); - }).should.throw('typestr must be "identity"'); - }); - - }); - -}); diff --git a/test/kdf.js b/test/kdf.js deleted file mode 100644 index c334dd28c..000000000 --- a/test/kdf.js +++ /dev/null @@ -1,39 +0,0 @@ -var should = require('chai').should(); -var KDF = require('../lib/kdf'); -var Hash = require('../lib/hash'); - -describe('KDF', function() { - - describe('#buf2keypair', function() { - - it('should compute these known values', function() { - var buf = Hash.sha256(new Buffer('test')); - var keypair = KDF.buf2keypair(buf); - keypair.privkey.toString().should.equal('KxxVszVMFLGzmxpxR7sMSaWDmqMKLVhKebX5vZbGHyuR8spreQ7V'); - keypair.pubkey.toString().should.equal('03774f761ae89a0d2fda0d532bad62286ae8fcda9bc38c060036296085592a97c1'); - }); - - }); - - describe('#sha256hmac2keypair', function() { - - it('should compute these known values', function() { - var buf = Hash.sha256(new Buffer('test')); - var keypair = KDF.sha256hmac2keypair(buf); - keypair.privkey.toString().should.equal('KxxVszVMFLGzmxpxR7sMSaWDmqMKLVhKebX5vZbGHyuR8spreQ7V'); - keypair.pubkey.toString().should.equal('03774f761ae89a0d2fda0d532bad62286ae8fcda9bc38c060036296085592a97c1'); - }); - - }); - - describe('#sha256hmac2privkey', function() { - - it('should compute this known privkey', function() { - var buf = Hash.sha256(new Buffer('test')); - var privkey = KDF.sha256hmac2privkey(buf); - privkey.toString().should.equal('KxxVszVMFLGzmxpxR7sMSaWDmqMKLVhKebX5vZbGHyuR8spreQ7V'); - }); - - }); - -}); diff --git a/test/keypair.js b/test/keypair.js index f6dbe61a0..0d9c23f00 100644 --- a/test/keypair.js +++ b/test/keypair.js @@ -1,12 +1,14 @@ +'use strict'; + var should = require('chai').should(); -var bn = require('../lib/bn'); -var point = require('../lib/point'); -var Privkey = require('../lib/privkey'); -var Pubkey = require('../lib/pubkey'); -var Keypair = require('../lib/keypair'); +var bitcore = require('..'); +var bn = bitcore.BN; +var Privkey = bitcore.Privkey; +var Pubkey = bitcore.Pubkey; +var Keypair = bitcore.Keypair; describe('Keypair', function() { - + it('should make a blank key', function() { var key = new Keypair(); should.exist(key); @@ -15,16 +17,21 @@ describe('Keypair', function() { it('should make a key with a priv and pub', function() { var priv = new Privkey(); var pub = new Pubkey(); - var key = new Keypair({privkey: priv, pubkey: pub}); + var key = new Keypair({ + privkey: priv, + pubkey: pub + }); should.exist(key); should.exist(key.privkey); should.exist(key.pubkey); }); - describe("#set", function() { - + describe('#set', function() { + it('should make a new priv and pub', function() { - should.exist(Keypair().set({privkey: Privkey()}).privkey); + should.exist(new Keypair().set({ + privkey: new Privkey() + }).privkey); }); }); @@ -58,7 +65,7 @@ describe('Keypair', function() { }); describe("#fromPrivkey", function() { - + it('should make a new key from a privkey', function() { should.exist(Keypair().fromPrivkey(Privkey().fromRandom()).pubkey); }); @@ -66,7 +73,7 @@ describe('Keypair', function() { }); describe("#fromRandom", function() { - + it('should make a new priv and pub, should be compressed, mainnet', function() { var key = new Keypair(); key.fromRandom(); @@ -83,7 +90,7 @@ describe('Keypair', function() { }); describe("#fromString()", function() { - + it('should recover a key creating with toString', function() { var key = new Keypair(); key.fromRandom(); @@ -122,12 +129,14 @@ describe('Keypair', function() { }); describe("#privkey2pubkey", function() { - + it('should convert this known Privkey to known Pubkey', function() { var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff'; var pubhex = '02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc'; var key = new Keypair(); - key.privkey = new Privkey({bn: bn(new Buffer(privhex, 'hex'))}); + key.privkey = new Privkey({ + bn: bn(new Buffer(privhex, 'hex')) + }); key.privkey2pubkey(); key.pubkey.toString().should.equal(pubhex); }); @@ -135,7 +144,9 @@ describe('Keypair', function() { it('should convert this known Privkey to known Pubkey and preserve compressed=true', function() { var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff'; var key = new Keypair(); - key.privkey = new Privkey({bn: bn(new Buffer(privhex, 'hex'))}); + key.privkey = new Privkey({ + bn: bn(new Buffer(privhex, 'hex')) + }); key.privkey.compressed = true; key.privkey2pubkey(); key.pubkey.compressed.should.equal(true); @@ -144,7 +155,9 @@ describe('Keypair', function() { it('should convert this known Privkey to known Pubkey and preserve compressed=true', function() { var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff'; var key = new Keypair(); - key.privkey = new Privkey({bn: bn(new Buffer(privhex, 'hex'))}); + key.privkey = new Privkey({ + bn: bn(new Buffer(privhex, 'hex')) + }); key.privkey.compressed = false; key.privkey2pubkey(); key.pubkey.compressed.should.equal(false); @@ -153,7 +166,7 @@ describe('Keypair', function() { }); describe("#toString()", function() { - + it('should exist', function() { var key = new Keypair(); key.fromRandom(); diff --git a/test/message.js b/test/message.js deleted file mode 100644 index 713827149..000000000 --- a/test/message.js +++ /dev/null @@ -1,107 +0,0 @@ -var Address = require('../lib/address'); -var Message = require('../lib/message'); -var Keypair = require('../lib/keypair'); -var should = require('chai').should(); - -describe('Message', function() { - - it('should make a new message', function() { - var message = new Message(); - should.exist(message); - }); - - it('should make a new message when called without "new"', function() { - var message = Message(); - should.exist(message); - }); - - describe('#set', function() { - - it('should set the messagebuf', function() { - var messagebuf = new Buffer('message'); - should.exist(Message().set({messagebuf: messagebuf}).messagebuf); - }); - - }); - - describe('@sign', function() { - var messagebuf = new Buffer('this is my message'); - var keypair = Keypair().fromRandom(); - - it('should return a base64 string', function() { - var sigstr = Message.sign(messagebuf, keypair); - var sigbuf = new Buffer(sigstr, 'base64'); - sigbuf.length.should.equal(1 + 32 + 32); - }); - - it('should sign with a compressed pubkey', function() { - var keypair = Keypair().fromRandom(); - keypair.pubkey.compressed = true; - var sigstr = Message.sign(messagebuf, keypair); - var sigbuf = new Buffer(sigstr, 'base64'); - sigbuf[0].should.be.above(27 + 4 - 1); - sigbuf[0].should.be.below(27 + 4 + 4 - 1); - }); - - it('should sign with an uncompressed pubkey', function() { - var keypair = Keypair().fromRandom(); - keypair.pubkey.compressed = false; - var sigstr = Message.sign(messagebuf, keypair); - var sigbuf = new Buffer(sigstr, 'base64'); - sigbuf[0].should.be.above(27 - 1); - sigbuf[0].should.be.below(27 + 4 - 1); - }); - - }); - - describe('@verify', function() { - var messagebuf = new Buffer('this is my message'); - var keypair = Keypair().fromRandom(); - - it('should verify a signed message', function() { - var sigstr = Message.sign(messagebuf, keypair); - var addr = Address().fromPubkey(keypair.pubkey); - Message.verify(messagebuf, sigstr, addr).should.equal(true); - }); - - it('should verify this known good signature', function() { - var addrstr = '1CKTmxj6DjGrGTfbZzVxnY4Besbv8oxSZb'; - var address = Address().fromString(addrstr); - var sigstr = 'IOrTlbNBI0QO990xOw4HAjnvRl/1zR+oBMS6HOjJgfJqXp/1EnFrcJly0UcNelqJNIAH4f0abxOZiSpYmenMH4M='; - Message.verify(messagebuf, sigstr, address); - }); - - }); - - describe('#sign', function() { - var messagebuf = new Buffer('this is my message'); - var keypair = Keypair().fromRandom(); - - it('should sign a message', function() { - var message = new Message(); - message.messagebuf = messagebuf; - message.keypair = keypair; - message.sign(); - var sig = message.sig; - should.exist(sig); - }); - - }); - - describe('#verify', function() { - var messagebuf = new Buffer('this is my message'); - var keypair = Keypair().fromRandom(); - - it('should verify a message that was just signed', function() { - var message = new Message(); - message.messagebuf = messagebuf; - message.keypair = keypair; - message.address = Address().fromPubkey(keypair.pubkey); - message.sign(); - message.verify(); - message.verified.should.equal(true); - }); - - }); - -}); diff --git a/test/opcode.js b/test/opcode.js index e531c9a27..eb65420bd 100644 --- a/test/opcode.js +++ b/test/opcode.js @@ -1,10 +1,14 @@ +'use strict'; + var should = require('chai').should(); -var Opcode = require('../lib/opcode'); +var bitcore = require('..'); +var Opcode = bitcore.Opcode; describe('Opcode', function() { it('should create a new Opcode', function() { var opcode = new Opcode(5); + should.exist(opcode); }); it('should convert to a string with this handy syntax', function() { diff --git a/test/privkey.js b/test/privkey.js index 745e72b73..d9726873f 100644 --- a/test/privkey.js +++ b/test/privkey.js @@ -1,8 +1,10 @@ -var Privkey = require('../lib/privkey'); -var base58check = require('../lib/base58check'); -var BN = require('../lib/bn'); -var Point = require('../lib/point'); +'use strict'; + var should = require('chai').should(); +var bitcore = require('..'); +var Privkey = bitcore.Privkey; +var BN = bitcore.BN; +var Point = bitcore.Point; describe('Privkey', function() { var hex = '96c132224121b509b7d0a16245e957d9192609c5637c6228311287b1be21627a'; @@ -11,7 +13,7 @@ describe('Privkey', function() { var enctu = '92jJzK4tbURm1C7udQXxeCBvXHoHJstDXRxAMouPG1k1XUaXdsu'; var encmainnet = 'L2Gkw3kKJ6N24QcDuH4XDqt9cTqsKTVNDGz1CRZhk9cq4auDUbJy'; var encmu = '5JxgQaFM1FMd38cd14e3mbdxsdSa9iM2BV6DHBYsvGzxkTNQ7Un'; - + it('should create an empty private key', function() { var privkey = new Privkey(); should.exist(privkey); @@ -24,24 +26,38 @@ describe('Privkey', function() { }); it('should create a mainnet private key', function() { - var privkey = new Privkey({bn: BN.fromBuffer(buf), networkstr: 'mainnet', compressed: true}); + var privkey = new Privkey({ + bn: BN.fromBuffer(buf), + networkstr: 'mainnet', + compressed: true + }); privkey.toString().should.equal(encmainnet); }); it('should create an uncompressed testnet private key', function() { - var privkey = new Privkey({bn: BN.fromBuffer(buf), networkstr: 'testnet', compressed: false}); + var privkey = new Privkey({ + bn: BN.fromBuffer(buf), + networkstr: 'testnet', + compressed: false + }); privkey.toString().should.equal(enctu); }); it('should create an uncompressed mainnet private key', function() { - var privkey = new Privkey({bn: BN.fromBuffer(buf), networkstr: 'mainnet', compressed: false}); + var privkey = new Privkey({ + bn: BN.fromBuffer(buf), + networkstr: 'mainnet', + compressed: false + }); privkey.toString().should.equal(encmu); }); describe('#set', function() { - + it('should set bn', function() { - should.exist(Privkey().set({bn: BN.fromBuffer(buf)}).bn); + should.exist(Privkey().set({ + bn: BN.fromBuffer(buf) + }).bn); }); }); @@ -67,7 +83,7 @@ describe('Privkey', function() { }); describe('#fromRandom', function() { - + it('should set bn gt 0 and lt n, and should be compressed', function() { var privkey = Privkey().fromRandom(); privkey.bn.gt(BN(0)).should.equal(true); diff --git a/test/base58.js b/test/protocol/base58.js similarity index 88% rename from test/base58.js rename to test/protocol/base58.js index 11fe97607..a99a2c71e 100644 --- a/test/base58.js +++ b/test/protocol/base58.js @@ -1,9 +1,12 @@ -var Base58 = require('../lib/base58'); +'use strict'; + var should = require('chai').should(); +var bitcore = require('../..'); +var Base58 = bitcore.Base58; describe('Base58', function() { var buf = new Buffer([0, 1, 2, 3, 253, 254, 255]); - var enc = "1W7N4RuG"; + var enc = '1W7N4RuG'; it('should make an instance with "new"', function() { var b58 = new Base58(); @@ -17,13 +20,15 @@ describe('Base58', function() { it('should allow this handy syntax', function() { Base58(buf).toString().should.equal(enc); - Base58(enc).toBuffer().toString('hex').should.equal(buf.toString('hex')) + Base58(enc).toBuffer().toString('hex').should.equal(buf.toString('hex')); }); describe('#set', function() { - + it('should set a blank buffer', function() { - Base58().set({buf: new Buffer([])}); + Base58().set({ + buf: new Buffer([]) + }); }); }); @@ -57,7 +62,7 @@ describe('Base58', function() { }); describe('#fromBuffer', function() { - + it('should not fail', function() { should.exist(Base58().fromBuffer(buf)); }); @@ -80,7 +85,9 @@ describe('Base58', function() { describe('#toBuffer', function() { it('should return the buffer', function() { - var b58 = Base58({buf: buf}); + var b58 = Base58({ + buf: buf + }); b58.buf.toString('hex').should.equal(buf.toString('hex')); }); @@ -89,7 +96,9 @@ describe('Base58', function() { describe('#toString', function() { it('should return the buffer', function() { - var b58 = Base58({buf: buf}); + var b58 = Base58({ + buf: buf + }); b58.toString().should.equal(enc); }); diff --git a/test/base58check.js b/test/protocol/base58check.js similarity index 95% rename from test/base58check.js rename to test/protocol/base58check.js index bc7081fac..04dd21a6f 100644 --- a/test/base58check.js +++ b/test/protocol/base58check.js @@ -1,10 +1,13 @@ +'use strict'; + var should = require('chai').should(); -var Base58Check = require('../lib/base58check'); -var base58 = require('../lib/base58'); +var bitcore = require('../..'); +var Base58Check = bitcore.Base58Check; +var base58 = bitcore.Base58; describe('Base58Check', function() { var buf = new Buffer([0, 1, 2, 3, 253, 254, 255]); - var enc = "14HV44ipwoaqfg"; + var enc = '14HV44ipwoaqfg'; it('should make an instance with "new"', function() { var b58 = new Base58Check(); diff --git a/test/bufferreader.js b/test/protocol/bufferreader.js similarity index 98% rename from test/bufferreader.js rename to test/protocol/bufferreader.js index ac0df7120..f92f59c29 100644 --- a/test/bufferreader.js +++ b/test/protocol/bufferreader.js @@ -1,7 +1,10 @@ -var BufferWriter = require('../lib/bufferwriter'); -var BufferReader = require('../lib/bufferreader'); +'use strict'; + var should = require('chai').should(); -var BN = require('../lib/bn'); +var bitcore = require('../..'); +var BufferWriter = bitcore.BufferWriter; +var BufferReader = bitcore.BufferReader; +var BN = bitcore.BN; describe('BufferReader', function() { diff --git a/test/bufferwriter.js b/test/protocol/bufferwriter.js similarity index 96% rename from test/bufferwriter.js rename to test/protocol/bufferwriter.js index fa4b2b915..e944a430b 100644 --- a/test/bufferwriter.js +++ b/test/protocol/bufferwriter.js @@ -1,7 +1,10 @@ -var BufferWriter = require('../lib/bufferwriter'); -var BufferReader = require('../lib/bufferreader'); -var BN = require('../lib/bn'); +'use strict'; + +var bitcore = require('../..'); var should = require('chai').should(); +var BufferWriter = bitcore.BufferWriter; +var BufferReader = bitcore.BufferReader; +var BN = bitcore.BN; describe('BufferWriter', function() { @@ -15,7 +18,6 @@ describe('BufferWriter', function() { it('set bufs', function() { var buf1 = new Buffer([0]); var buf2 = new Buffer([1]); - var bufs = [buf1, buf2]; var bw = new BufferWriter().set({bufs: [buf1, buf2]}); bw.concat().toString('hex').should.equal('0001'); }); diff --git a/test/protocol/constants.js b/test/protocol/constants.js new file mode 100644 index 000000000..e5604d52e --- /dev/null +++ b/test/protocol/constants.js @@ -0,0 +1,18 @@ +'use strict'; + +var should = require('chai').should(); +var bitcore = require('../../'); +var constants = bitcore.Constants; + +describe('Constants', function() { + + it('should contain all constants for livenet and testnet', function() { + for (var key in constants.livenet) { + if (constants.livenet.hasOwnProperty(key)) { + constants.testnet.hasOwnProperty(key).should.equal(true); + } + } + + }); + +}); diff --git a/test/varint.js b/test/protocol/varint.js similarity index 92% rename from test/varint.js rename to test/protocol/varint.js index 85a96442a..dd67a501a 100644 --- a/test/varint.js +++ b/test/protocol/varint.js @@ -1,8 +1,11 @@ -var BN = require('../lib/bn'); +'use strict'; + var should = require('chai').should(); -var BufferReader = require('../lib/bufferreader'); -var BufferWriter = require('../lib/bufferwriter'); -var Varint = require('../lib/varint'); +var bitcore = require('../..'); +var BN = bitcore.BN; +var BufferReader = bitcore.BufferReader; +var BufferWriter = bitcore.BufferWriter; +var Varint = bitcore.Varint; describe('Varint', function() { @@ -95,7 +98,7 @@ describe('Varint', function() { describe('#toBuffer', function() { it('should return a buffer', function() { - buf = BufferWriter().writeVarintNum(5).concat(); + var buf = BufferWriter().writeVarintNum(5).concat(); var varint = Varint(buf); varint.toBuffer().toString('hex').should.equal(buf.toString('hex')); }); diff --git a/test/pubkey.js b/test/pubkey.js index bc4f16725..3feb58e17 100644 --- a/test/pubkey.js +++ b/test/pubkey.js @@ -1,8 +1,11 @@ +'use strict'; + var should = require('chai').should(); -var Pubkey = require('../lib/pubkey'); -var Point = require('../lib/point'); -var Bn = require('../lib/bn'); -var Privkey = require('../lib/privkey'); +var bitcore = require('..'); +var Pubkey = bitcore.Pubkey; +var Point = bitcore.Point; +var Bn = bitcore.BN; +var Privkey = bitcore.Privkey; describe('Pubkey', function() { @@ -114,7 +117,7 @@ describe('Pubkey', function() { describe('#fromString', function() { it('should parse this known valid public key', function() { - pk = new Pubkey(); + var pk = new Pubkey(); pk.fromString('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341'); pk.point.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a'); pk.point.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341'); diff --git a/test/script.js b/test/script.js index 83f1b148e..601fdc4d1 100644 --- a/test/script.js +++ b/test/script.js @@ -1,13 +1,15 @@ -var Script = require('../lib/script'); +'use strict'; + var should = require('chai').should(); -var Opcode = require('../lib/opcode'); -var BufferReader = require('../lib/bufferreader'); -var BufferWriter = require('../lib/bufferwriter'); +var bitcore = require('..'); +var Script = bitcore.Script; +var Opcode = bitcore.Opcode; describe('Script', function() { it('should make a new script', function() { var script = new Script(); + should.exist(script); }); describe('#fromBuffer', function() { diff --git a/test/signature.js b/test/signature.js index 2e7016513..9bf32f596 100644 --- a/test/signature.js +++ b/test/signature.js @@ -1,6 +1,9 @@ -var BN = require('../lib/bn'); +'use strict'; + var should = require('chai').should(); -var Signature = require('../lib/signature'); +var bitcore = require('..'); +var BN = bitcore.BN; +var Signature = bitcore.Signature; describe('Signature', function() { @@ -19,15 +22,17 @@ describe('Signature', function() { }); describe('#set', function() { - + it('should set compressed', function() { - should.exist(Signature().set({compressed: true})); + should.exist(Signature().set({ + compressed: true + })); }); }); describe('#fromCompact', function() { - + it('should create a signature from a compressed signature', function() { var blank = new Buffer(32); blank.fill(0); @@ -35,7 +40,7 @@ describe('Signature', function() { new Buffer([0 + 27 + 4]), blank, blank - ]); + ]); var sig = new Signature(); sig.fromCompact(compressed); sig.r.cmp(0).should.equal(0); @@ -45,27 +50,35 @@ describe('Signature', function() { }); describe('#fromDER', function() { - + var buf = new Buffer('3044022075fc517e541bd54769c080b64397e32161c850f6c1b2b67a5c433affbb3e62770220729e85cc46ffab881065ec07694220e71d4df9b2b8c8fd12c3122cf3a5efbcf2', 'hex'); it('should parse this DER format signature', function() { var sig = new Signature(); sig.fromDER(buf); - sig.r.toBuffer({size: 32}).toString('hex').should.equal('75fc517e541bd54769c080b64397e32161c850f6c1b2b67a5c433affbb3e6277'); - sig.s.toBuffer({size: 32}).toString('hex').should.equal('729e85cc46ffab881065ec07694220e71d4df9b2b8c8fd12c3122cf3a5efbcf2'); + sig.r.toBuffer({ + size: 32 + }).toString('hex').should.equal('75fc517e541bd54769c080b64397e32161c850f6c1b2b67a5c433affbb3e6277'); + sig.s.toBuffer({ + size: 32 + }).toString('hex').should.equal('729e85cc46ffab881065ec07694220e71d4df9b2b8c8fd12c3122cf3a5efbcf2'); }); }); describe('#fromString', function() { - + var buf = new Buffer('3044022075fc517e541bd54769c080b64397e32161c850f6c1b2b67a5c433affbb3e62770220729e85cc46ffab881065ec07694220e71d4df9b2b8c8fd12c3122cf3a5efbcf2', 'hex'); it('should parse this DER format signature in hex', function() { var sig = new Signature(); sig.fromString(buf.toString('hex')); - sig.r.toBuffer({size: 32}).toString('hex').should.equal('75fc517e541bd54769c080b64397e32161c850f6c1b2b67a5c433affbb3e6277'); - sig.s.toBuffer({size: 32}).toString('hex').should.equal('729e85cc46ffab881065ec07694220e71d4df9b2b8c8fd12c3122cf3a5efbcf2'); + sig.r.toBuffer({ + size: 32 + }).toString('hex').should.equal('75fc517e541bd54769c080b64397e32161c850f6c1b2b67a5c433affbb3e6277'); + sig.s.toBuffer({ + size: 32 + }).toString('hex').should.equal('729e85cc46ffab881065ec07694220e71d4df9b2b8c8fd12c3122cf3a5efbcf2'); }); }); @@ -127,7 +140,10 @@ describe('Signature', function() { it('should convert these known r and s values into a known signature', function() { var r = BN('63173831029936981022572627018246571655303050627048489594159321588908385378810'); var s = BN('4331694221846364448463828256391194279133231453999942381442030409253074198130'); - var sig = new Signature({r: r, s: s}); + var sig = new Signature({ + r: r, + s: s + }); var der = sig.toDER(r, s); der.toString('hex').should.equal('30450221008bab1f0a2ff2f9cb8992173d8ad73c229d31ea8e10b0f4d4ae1a0d8ed76021fa02200993a6ec81755b9111762fc2cf8e3ede73047515622792110867d12654275e72'); }); @@ -139,7 +155,10 @@ describe('Signature', function() { it('should convert this signature in to hex DER', function() { var r = BN('63173831029936981022572627018246571655303050627048489594159321588908385378810'); var s = BN('4331694221846364448463828256391194279133231453999942381442030409253074198130'); - var sig = new Signature({r: r, s: s}); + var sig = new Signature({ + r: r, + s: s + }); var hex = sig.toString(); hex.should.equal('30450221008bab1f0a2ff2f9cb8992173d8ad73c229d31ea8e10b0f4d4ae1a0d8ed76021fa02200993a6ec81755b9111762fc2cf8e3ede73047515622792110867d12654275e72'); }); diff --git a/test/stealthaddress.js b/test/stealthaddress.js deleted file mode 100644 index 3b8e3e88b..000000000 --- a/test/stealthaddress.js +++ /dev/null @@ -1,149 +0,0 @@ -var StealthAddress = require('../lib/expmt/stealthaddress'); -var should = require('chai').should(); -var Stealthkey = require('../lib/expmt/stealthkey'); -var Keypair = require('../lib/keypair'); -var Privkey = require('../lib/privkey'); -var Pubkey = require('../lib/pubkey'); -var BN = require('../lib/bn'); -var Hash = require('../lib/hash'); -var Base58check = require('../lib/base58check'); - -describe('StealthAddress', function() { - - var stealthkey = Stealthkey(); - stealthkey.payloadKeypair = Keypair(); - stealthkey.payloadKeypair.privkey = Privkey(); - stealthkey.payloadKeypair.privkey.bn = BN().fromBuffer(Hash.sha256(new Buffer('test 1'))); - stealthkey.payloadKeypair.privkey2pubkey(); - stealthkey.scanKeypair = Keypair(); - stealthkey.scanKeypair.privkey = Privkey(); - stealthkey.scanKeypair.privkey.bn = BN().fromBuffer(Hash.sha256(new Buffer('test 2'))); - stealthkey.scanKeypair.privkey2pubkey(); - - var senderKeypair = Keypair(); - senderKeypair.privkey = Privkey(); - senderKeypair.privkey.bn = BN().fromBuffer(Hash.sha256(new Buffer('test 3'))); - senderKeypair.privkey2pubkey(); - - var addressString = 'vJmtuUb8ysKiM1HtHQF23FGfjGAKu5sM94UyyjknqhJHNdj5CZzwtpGzeyaATQ2HvuzomNVtiwsTJSWzzCBgCTtUZbRFpzKVq9MAUr'; - var dwhex = '2a0002697763d7e9becb0c180083738c32c05b0e2fee26d6278020c06bbb04d5f66b32010362408459041e0473298af3824dbabe4d2b7f846825ed4d1c2e2c670c07cb275d0100'; - var dwbuf = new Buffer(dwhex, 'hex'); - - it('should make a new stealth address', function() { - var sa = new StealthAddress(); - should.exist(sa); - sa = StealthAddress(); - should.exist(sa); - sa = StealthAddress(addressString); - should.exist(sa); - sa = StealthAddress(Base58check.decode(addressString)); - should.exist(sa); - }); - - describe('#fromJSON', function() { - - it('should give a stealthkey address with the right pubkeys', function() { - var sa = new StealthAddress(); - sa.fromJSON(addressString); - sa.payloadPubkey.toString().should.equal(stealthkey.payloadKeypair.pubkey.toString()); - sa.scanPubkey.toString().should.equal(stealthkey.scanKeypair.pubkey.toString()); - }); - - }); - - describe('#toJSON', function() { - - it('should return this known address string', function() { - StealthAddress().fromJSON(addressString).toJSON().should.equal(addressString); - }); - - }); - - describe('#fromBuffer', function() { - - it('should parse this DW buffer', function() { - StealthAddress().fromBuffer(new Buffer(dwhex, 'hex')).toBuffer().toString('hex').should.equal(dwhex); - }); - - }); - - describe('#fromString', function() { - - it('should parse this DW buffer', function() { - StealthAddress().fromString(Base58check(new Buffer(dwhex, 'hex')).toString()).toBuffer().toString('hex').should.equal(dwhex); - }); - - }); - - describe('#getSharedKeypair', function() { - - it('should return a key', function() { - var sa = new StealthAddress(); - sa.payloadPubkey = stealthkey.payloadKeypair.pubkey; - sa.scanPubkey = stealthkey.scanKeypair.pubkey; - var key = sa.getSharedKeypair(senderKeypair); - (key instanceof Keypair).should.equal(true); - }); - - it('should return the same key as Stealthkey.prototype.getSharedKeypair', function() { - var sa = new StealthAddress(); - sa.payloadPubkey = stealthkey.payloadKeypair.pubkey; - sa.scanPubkey = stealthkey.scanKeypair.pubkey; - var key = sa.getSharedKeypair(senderKeypair); - - var key2 = stealthkey.getSharedKeypair(senderKeypair.pubkey); - key.toString().should.equal(key2.toString()); - }); - - }); - - describe('#getReceivePubkey', function() { - - it('should return a pubkey', function() { - var pubkey = StealthAddress().fromStealthkey(stealthkey).getReceivePubkey(senderKeypair); - (pubkey instanceof Pubkey).should.equal(true); - }); - - it('should return the same pubkey as getReceivePubkey', function() { - var pubkey = StealthAddress().fromStealthkey(stealthkey).getReceivePubkey(senderKeypair); - var pubkey2 = stealthkey.getReceivePubkey(senderKeypair.pubkey); - pubkey2.toString().should.equal(pubkey.toString()); - }); - - }); - - describe('#toBuffer', function() { - - it('should return this known address buffer', function() { - var buf = Base58check.decode(addressString); - StealthAddress().fromBuffer(dwbuf).toBuffer().toString('hex').should.equal(dwhex); - }); - - }); - - describe('#toString', function() { - - it('should return this known address buffer', function() { - var buf = Base58check.decode(addressString); - StealthAddress().fromBuffer(buf).toString().should.equal(Base58check(new Buffer(dwhex, 'hex')).toString()); - }); - - }); - - describe('@parseDWBuffer', function() { - - it('should parse this known DW buffer', function() { - var buf = new Buffer(dwhex, 'hex'); - var parsed = StealthAddress.parseDWBuffer(buf); - parsed.version.should.equal(42); - parsed.options.should.equal(0); - parsed.scanPubkey.toString().should.equal('02697763d7e9becb0c180083738c32c05b0e2fee26d6278020c06bbb04d5f66b32'); - parsed.nPayloadPubkeys.should.equal(1); - parsed.payloadPubkeys[0].toString().should.equal('0362408459041e0473298af3824dbabe4d2b7f846825ed4d1c2e2c670c07cb275d'); - parsed.nSigs.should.equal(1); - parsed.prefix.toString().should.equal(''); - }); - - }); - -}); diff --git a/test/stealthkey.js b/test/stealthkey.js deleted file mode 100644 index 17eed96d8..000000000 --- a/test/stealthkey.js +++ /dev/null @@ -1,138 +0,0 @@ -var should = require('chai').should(); -var Stealthkey = require('../lib/expmt/stealthkey'); -var Keypair = require('../lib/keypair'); -var Privkey = require('../lib/privkey'); -var Pubkey = require('../lib/pubkey'); -var BN = require('../lib/bn'); -var Hash = require('../lib/hash'); - -describe('Stealthkey', function() { - - var stealthkey = Stealthkey(); - stealthkey.payloadKeypair = Keypair(); - stealthkey.payloadKeypair.privkey = Privkey(); - stealthkey.payloadKeypair.privkey.bn = BN().fromBuffer(Hash.sha256(new Buffer('test 1'))); - stealthkey.payloadKeypair.privkey2pubkey(); - stealthkey.scanKeypair = Keypair(); - stealthkey.scanKeypair.privkey = Privkey(); - stealthkey.scanKeypair.privkey.bn = BN().fromBuffer(Hash.sha256(new Buffer('test 2'))); - stealthkey.scanKeypair.privkey2pubkey(); - - var senderKeypair = Keypair(); - senderKeypair.privkey = Privkey(); - senderKeypair.privkey.bn = BN().fromBuffer(Hash.sha256(new Buffer('test 3'))); - senderKeypair.privkey2pubkey(); - - it('should create a new stealthkey', function() { - var stealthkey = new Stealthkey(); - should.exist(stealthkey); - }); - - it('should create a new stealthkey without using "new"', function() { - var stealthkey = Stealthkey(); - should.exist(stealthkey); - }); - - it('should create a new stealthkey with both keypairs in the constructor', function() { - var keypair1 = Keypair(); - var keypair2 = Keypair(); - var stealthkey = Stealthkey(keypair1, keypair2); - should.exist(stealthkey.payloadKeypair); - should.exist(stealthkey.scanKeypair); - }); - - describe('#set', function() { - - it('should set payload key', function() { - should.exist(Stealthkey().set({payloadKeypair: stealthkey.payloadKeypair}).payloadKeypair); - }); - - }); - - describe('#fromJSON', function() { - - it('should make a stealthkey from this JSON', function() { - var sk = Stealthkey().fromJSON({ - payloadKeypair: stealthkey.payloadKeypair.toJSON(), - scanKeypair: stealthkey.scanKeypair.toJSON() - }); - sk.payloadKeypair.toString().should.equal(stealthkey.payloadKeypair.toString()); - sk.scanKeypair.toString().should.equal(stealthkey.scanKeypair.toString()); - }); - - }); - - describe('#toJSON', function() { - - it('should convert this stealthkey to json', function() { - var json = stealthkey.toJSON() - var json2 = Stealthkey().fromJSON(json).toJSON(); - json.payloadKeypair.privkey.should.equal(json2.payloadKeypair.privkey); - json.scanKeypair.privkey.should.equal(json2.scanKeypair.privkey); - }); - - }); - - describe('#fromRandom', function() { - - it('should create a new stealthkey from random', function() { - var stealthkey = Stealthkey().fromRandom(); - should.exist(stealthkey.payloadKeypair.privkey.bn.gt(0)); - should.exist(stealthkey.scanKeypair.privkey.bn.gt(0)); - }); - - }); - - describe('#getSharedKeypair', function() { - - it('should return a key', function() { - var key = stealthkey.getSharedKeypair(senderKeypair.pubkey); - (key instanceof Keypair).should.equal(true); - }); - - }); - - describe('#getReceivePubkey', function() { - - it('should return a pubkey', function() { - var pubkey = stealthkey.getReceivePubkey(senderKeypair.pubkey); - (pubkey instanceof Pubkey).should.equal(true); - }); - - }); - - describe('#getReceiveKeypair', function() { - - it('should return a key', function() { - var key = stealthkey.getReceiveKeypair(senderKeypair.pubkey); - (key instanceof Keypair).should.equal(true); - }); - - it('should return a key with the same pubkey as getReceivePubkey', function() { - var key = stealthkey.getReceiveKeypair(senderKeypair.pubkey); - var pubkey = stealthkey.getReceivePubkey(senderKeypair.pubkey); - key.pubkey.toString().should.equal(pubkey.toString()); - }); - - it('should return private key with length 32 or less', function() { - var key = stealthkey.getReceiveKeypair(senderKeypair.pubkey); - key.privkey.bn.toBuffer().length.should.be.below(33); - }); - - }); - - describe('#isForMe', function() { - - it('should return true if it (the transaction or message) is for me', function() { - var pubkeyhash = new Buffer('3cb64fa6ee9b3e8754e3e2bd033bf61048604a99', 'hex'); - stealthkey.isForMe(senderKeypair.pubkey, pubkeyhash).should.equal(true); - }); - - it('should return false if it (the transaction or message) is not for me', function() { - var pubkeyhash = new Buffer('00b64fa6ee9b3e8754e3e2bd033bf61048604a99', 'hex'); - stealthkey.isForMe(senderKeypair.pubkey, pubkeyhash).should.equal(false); - }); - - }); - -}); diff --git a/test/stealthmessage.js b/test/stealthmessage.js deleted file mode 100644 index 16b2139e1..000000000 --- a/test/stealthmessage.js +++ /dev/null @@ -1,139 +0,0 @@ -var Keypair = require('../lib/keypair'); -var StealthMessage = require('../lib/expmt/stealthmessage'); -var Stealthkey = require('../lib/expmt/stealthkey'); -var StealthAddress = require('../lib/expmt/stealthaddress'); -var KDF = require('../lib/kdf'); -var Hash = require('../lib/hash'); -var should = require('chai').should(); -var Address = require('../lib/address'); - -describe('StealthMessage', function() { - - var payloadKeypair = KDF.buf2keypair(new Buffer('key1')); - var scanKeypair = KDF.buf2keypair(new Buffer('key2')); - var fromKeypair = KDF.buf2keypair(new Buffer('key3')); - var enchex = 'f557994f16d0d628fa4fdb4ab3d7e0bc5f2754f20381c7831a20c7c9ec88dcf092ea3683261798ccda991ed65a3a54a036d8125dec0381c7831a20c7c9ec88dcf092ea3683261798ccda991ed65a3a54a036d8125dec9f86d081884c7d659a2feaa0c55ad01560ba2904d3bc8395b6c4a6f87648edb33db6a22170e5e26f340c7ba943169210234cd6a753ad13919b0ab7d678b47b5e7d63e452382de2c2590fb57ef048f7b3'; - var encbuf = new Buffer(enchex, 'hex'); - var ivbuf = Hash.sha256(new Buffer('test')).slice(0, 128 / 8); - var sk = Stealthkey().set({payloadKeypair: payloadKeypair, scanKeypair: scanKeypair}); - var sa = StealthAddress().fromStealthkey(sk); - var messagebuf = new Buffer('this is my message'); - - it('should make a new stealthmessage', function() { - var sm = new StealthMessage(); - should.exist(sm); - sm = StealthMessage() - should.exist(sm); - }); - - it('should allow "set" style syntax', function() { - var encbuf = StealthMessage().set({ - messagebuf: messagebuf, - toStealthAddress: sa - }).encrypt().encbuf; - should.exist(encbuf); - encbuf.length.should.equal(113); - }); - - describe('#set', function() { - - it('should set the messagebuf', function() { - var sm = StealthMessage().set({messagebuf: messagebuf}); - should.exist(sm.messagebuf); - }); - - }); - - describe('@encrypt', function() { - - it('should encrypt a message', function() { - var encbuf = StealthMessage.encrypt(messagebuf, sa); - encbuf.length.should.equal(166); - }); - - it('should encrypt a message with this fromKeypair and ivbuf the same each time', function() { - var encbuf = StealthMessage.encrypt(messagebuf, sa, fromKeypair, ivbuf); - encbuf.length.should.equal(166); - encbuf.toString('hex').should.equal(enchex); - }); - - }); - - describe('@decrypt', function() { - - it('should decrypt this known message correctly', function() { - var messagebuf2 = StealthMessage.decrypt(encbuf, sk); - messagebuf2.toString('hex').should.equal(messagebuf.toString('hex')); - }); - - }); - - describe('@isForMe', function() { - - it('should know that this message is for me', function() { - StealthMessage.isForMe(encbuf, sk).should.equal(true); - }); - - it('should know that this message is for me even if my payloadPrivkey is not present', function() { - var sk2 = new Stealthkey(); - sk2.scanKeypair = sk.scanKeypair; - sk2.payloadKeypair = Keypair().set({pubkey: sk.payloadKeypair.pubkey}); - should.not.exist(sk2.payloadKeypair.privkey); - StealthMessage.isForMe(encbuf, sk2).should.equal(true); - }); - - }); - - describe('#encrypt', function() { - - it('should encrypt this message', function() { - var sm = StealthMessage().set({ - messagebuf: messagebuf, - toStealthAddress: sa, - fromKeypair: fromKeypair - }); - sm.encrypt().encbuf.length.should.equal(113); - }); - - }); - - describe('#decrypt', function() { - - it('should decrypt that which was encrypted', function() { - var sm = StealthMessage().set({ - messagebuf: messagebuf, - toStealthAddress: sa - }).encrypt(); - var messagebuf2 = StealthMessage().set({ - encbuf: sm.encbuf, - fromKeypair: sm.fromKeypair, - toStealthkey: sk - }).decrypt().messagebuf; - messagebuf2.toString('hex').should.equal(messagebuf.toString('hex')); - }); - - }); - - describe('#isForMe', function() { - - it('should know that this message is for me', function() { - StealthMessage().set({ - encbuf: encbuf, - toStealthkey: sk, - fromKeypair: fromKeypair, - receiveAddress: Address().set({hashbuf: encbuf.slice(0, 20)}) - }).isForMe().should.equal(true); - }); - - it('should know that this message is not for me', function() { - StealthMessage().set({ - encbuf: encbuf, - toStealthkey: sk, - fromKeypair: fromKeypair, - receiveAddress: Address().set({hashbuf: encbuf.slice(0, 20)}) - }).isForMe().should.equal(true); - }); - - }); - -}); diff --git a/test/stealthtx.js b/test/stealthtx.js deleted file mode 100644 index f00d73ef9..000000000 --- a/test/stealthtx.js +++ /dev/null @@ -1,68 +0,0 @@ -var should = require('chai').should(); -var Txout = require('../lib/txout'); -var Stealthkey = require('../lib/expmt/stealthkey'); -var StealthTx = require('../lib/expmt/stealthtx'); -var Transaction = require('../lib/transaction'); -var Varint = require('../lib/varint'); - -describe('StealthTx', function() { - - var txhex = '0100000001c828ccce36eca04f96321ad488528af86c7598e67157c4f8e2f462a9e0e3af5f010000006a47304402204525eef6a56cc57fb184e53efdfdc1086d5265da21480d55c2184536440a64f70220349cdc6c66a8507dde0d172fe64aeb57ae56e014b50315f615086a6b85c5424e012102c0633ddb6bf2a8686e2ba4ce8026c94e1e27ef12e73f8fed6d6d2b97199f9b74ffffffff020000000000000000286a2606deadbeef0365b5a5b0ba059666e907b0b5e07b37fdb162d1399ed829315491fe1f30c87b3f905f0100000000001976a9142042d5e7ef9e82346419fbfe7df5ae52fe4bea3c88ac00000000'; - var txbuf = new Buffer(txhex, 'hex'); - var txidhex = '66da969fff214c329e27062beaf3baf20ed035801559b31f3e868c2de4cdfc5b'; - var tx = Transaction(txbuf); - - it('should make a new StealthTx', function() { - var stx = new StealthTx(); - should.exist(stx); - stx = StealthTx(); - should.exist(stx); - }); - - describe('#isForMe', function() { - - it('should return false for this known tx and random stealthkey', function() { - var sk = Stealthkey().fromRandom(); - var stx = StealthTx().set({sk: sk, tx: tx}); - stx.isForMe().should.equal(false); - }); - - }); - - describe('#notMine', function() { - - it('should return true for this known tx and random stealthkey', function() { - var sk = Stealthkey().fromRandom(); - var stx = StealthTx().set({sk: sk, tx: tx}); - stx.notMine().should.equal("StealthTx not mine"); - }); - - }); - - describe('#notStealth', function() { - - it('should know this is a stealth tx', function() { - var stx = StealthTx().set({tx: tx}); - stx.notStealth().should.equal(false); - }); - - it('should know this is not a stealth tx', function() { - var tx2 = Transaction(tx); - tx2.txouts.pop(); - tx2.txoutsvi = Varint(1); - var stx = StealthTx().set({tx: tx2}); - stx.notStealth().should.equal("Not enough txouts"); - }); - - }); - - describe('@parseOpReturnData', function() { - var txout = tx.txouts[0]; - var buf = txout.script.chunks[1].buf; - var parsed = StealthTx.parseOpReturnData(buf); - (typeof parsed.version).should.equal('number'); - parsed.noncebuf.length.should.be.above(0); - parsed.pubkey.toBuffer().length.should.equal(33); - }); - -}); diff --git a/test/transaction.js b/test/transaction.js index c5e6a9d4c..f797e069f 100644 --- a/test/transaction.js +++ b/test/transaction.js @@ -1,10 +1,12 @@ -var Varint = require('../lib/varint'); -var Transaction = require('../lib/transaction'); -var Txin = require('../lib/txin'); -var Txout = require('../lib/txout'); +'use strict'; + var should = require('chai').should(); -var BufferReader = require('../lib/bufferreader'); -var BufferWriter = require('../lib/bufferwriter'); +var bitcore = require('..'); +var Varint = bitcore.Varint; +var Transaction = bitcore.Transaction; +var Txin = bitcore.Txin; +var Txout = bitcore.Txout; +var BufferReader = bitcore.BufferReader; describe('Transaction', function() { @@ -32,7 +34,7 @@ describe('Transaction', function() { should.exist(tx); Transaction(txbuf).toBuffer().toString('hex').should.equal(txhex); - + //should set known defaults tx.version.should.equal(1); tx.txinsvi.toNumber().should.equal(0); @@ -43,7 +45,7 @@ describe('Transaction', function() { }); describe('#initialize', function() { - + it('should set these known defaults', function() { var tx = new Transaction(); tx.initialize(); @@ -114,7 +116,7 @@ describe('Transaction', function() { }); describe('#fromBuffer', function() { - + it('should recover from this known tx', function() { Transaction().fromBuffer(txbuf).toBuffer().toString('hex').should.equal(txhex); }); @@ -126,7 +128,7 @@ describe('Transaction', function() { }); describe('#fromBufferReader', function() { - + it('should recover from this known tx', function() { Transaction().fromBufferReader(BufferReader(txbuf)).toBuffer().toString('hex').should.equal(txhex); }); @@ -134,7 +136,7 @@ describe('Transaction', function() { }); describe('#toBuffer', function() { - + it('should produce this known tx', function() { Transaction().fromBuffer(txbuf).toBuffer().toString('hex').should.equal(txhex); }); @@ -142,7 +144,7 @@ describe('Transaction', function() { }); describe('#toBufferWriter', function() { - + it('should produce this known tx', function() { Transaction().fromBuffer(txbuf).toBufferWriter().concat().toString('hex').should.equal(txhex); }); @@ -169,7 +171,7 @@ describe('Transaction', function() { }); describe('#pushin', function() { - + it('should add an input', function() { var txin = Txin(); var tx = Transaction(); @@ -181,7 +183,7 @@ describe('Transaction', function() { }); describe('#pushout', function() { - + it('should add an output', function() { var txout = Txout(); var tx = Transaction(); diff --git a/test/txin.js b/test/txin.js index 6a4a5b967..e9738e9c2 100644 --- a/test/txin.js +++ b/test/txin.js @@ -1,8 +1,11 @@ +'use strict'; + var should = require('chai').should(); -var Script = require('../lib/script'); -var Txin = require('../lib/txin'); -var Varint = require('../lib/varint'); -var BufferReader = require('../lib/bufferreader'); +var bitcore = require('..'); +var Script = bitcore.Script; +var Txin = bitcore.Txin; +var Varint = bitcore.Varint; +var BufferReader = bitcore.BufferReader; describe('Txin', function() { diff --git a/test/txout.js b/test/txout.js index 2f186306b..5af90281f 100644 --- a/test/txout.js +++ b/test/txout.js @@ -1,21 +1,18 @@ +'use strict'; + var should = require('chai').should(); -var BN = require('../lib/bn'); -var Txout = require('../lib/txout'); -var Script = require('../lib/script'); -var Varint = require('../lib/varint'); -var BufferReader = require('../lib/bufferreader'); -var BufferWriter = require('../lib/bufferwriter'); +var bitcore = require('..'); +var BN = bitcore.BN; +var Txout = bitcore.Txout; +var Script = bitcore.Script; +var Varint = bitcore.Varint; +var BufferReader = bitcore.BufferReader; describe('Txout', function() { var valuebn = BN(5); - var script = Script().fromString("OP_CHECKMULTISIG"); + var script = Script().fromString('OP_CHECKMULTISIG'); var scriptvi = Varint(script.toBuffer().length); - var txout = new Txout().set({ - valuebn: valuebn, - scriptvi: scriptvi, - script: script - }); it('should make a new txout', function() { var txout = new Txout();