From 3d6b66e811d5fd6ceec506e34c19d047df4a8bb6 Mon Sep 17 00:00:00 2001 From: Wei Lu Date: Tue, 11 Mar 2014 09:52:48 +0800 Subject: [PATCH] move convert methods from util to convert --- src/address.js | 4 +-- src/base58.js | 7 ++--- src/convert.js | 58 +++++++++++++++++++++++++++++++++++++++ src/ecdsa.js | 15 +++++----- src/eckey.js | 18 ++++++------ src/hdwallet.js | 14 +++++----- src/message.js | 17 ++++++------ src/script.js | 8 +++--- src/transaction.js | 44 +++++++++++++++--------------- src/util.js | 68 ++++------------------------------------------ src/wallet.js | 10 ++----- test/base58.js | 6 ++-- test/convert.js | 27 ++++++++++++++++++ test/misc.js | 4 +-- test/util.js | 33 ---------------------- 15 files changed, 161 insertions(+), 172 deletions(-) delete mode 100644 test/util.js diff --git a/src/address.js b/src/address.js index 79720d4..6f6295d 100644 --- a/src/address.js +++ b/src/address.js @@ -1,5 +1,5 @@ var base58 = require('./base58'); -var conv = require('./convert'); +var convert = require('./convert'); var util = require('./util'); var mainnet = require('./network').mainnet.addressVersion; @@ -12,7 +12,7 @@ var Address = function (bytes, version) { else if (typeof bytes === 'string') { this.hash = bytes.length <= 35 ? base58.checkDecode(bytes) - : bytes.length <= 40 ? conv.hexToBytes(bytes) + : bytes.length <= 40 ? convert.hexToBytes(bytes) : util.error('invalid or unrecognized input'); this.version = version || this.hash.version || mainnet; diff --git a/src/base58.js b/src/base58.js index ec584c0..47e8020 100644 --- a/src/base58.js +++ b/src/base58.js @@ -5,8 +5,7 @@ var BigInteger = require('./jsbn/jsbn'); var Crypto = require('crypto-js'); var SHA256 = Crypto.SHA256; var WordArray = Crypto.lib.WordArray; -var conv = require('./convert'); -var util = require('./util'); +var convert = require('./convert'); var alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; var base = BigInteger.valueOf(58); @@ -101,8 +100,8 @@ module.exports.checkDecode = function(input) { } function getChecksum(bytes) { - var wordArray = util.bytesToWordArray(bytes) - return conv.hexToBytes(SHA256(SHA256(wordArray)).toString()).slice(0,4); + var wordArray = convert.bytesToWordArray(bytes) + return convert.hexToBytes(SHA256(SHA256(wordArray)).toString()).slice(0,4); } module.exports.getChecksum = getChecksum diff --git a/src/convert.js b/src/convert.js index 5db63ab..27154be 100644 --- a/src/convert.js +++ b/src/convert.js @@ -1,3 +1,5 @@ +var Crypto = require('crypto-js'); +var WordArray = Crypto.lib.WordArray; var base64map = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; exports.lpad = function lpad(str, padString, length) { @@ -100,3 +102,59 @@ exports.stringToBytes = function(string) { return x.charCodeAt(0) }); } + +/** + * Create a byte array representing a number with the given length + */ +exports.numToBytes = function(num, bytes) { + if (bytes === undefined) bytes = 8; + if (bytes === 0) return []; + return [num % 256].concat(module.exports.numToBytes(Math.floor(num / 256), bytes - 1)); +} + +/** + * Convert a byte array to the number that it represents + */ +exports.bytesToNum = function(bytes) { + if (bytes.length === 0) return 0; + return bytes[0] + 256 * module.exports.bytesToNum(bytes.slice(1)); +} + +/** + * Turn an integer into a "var_int". + * + * "var_int" is a variable length integer used by Bitcoin's binary format. + * + * Returns a byte array. + */ +exports.numToVarInt = function(num) { + if (num < 253) return [num]; + if (num < 65536) return [253].concat(exports.numToBytes(num, 2)); + if (num < 4294967296) return [254].concat(exports.numToBytes(num, 4)); + return [253].concat(exports.numToBytes(num, 8)); +} + +exports.bytesToWords = function (bytes) { + var words = []; + for (var i = 0, b = 0; i < bytes.length; i++, b += 8) { + words[b >>> 5] |= bytes[i] << (24 - b % 32); + } + return words; +} + +exports.wordsToBytes = function (words) { + var bytes = []; + for (var b = 0; b < words.length * 32; b += 8) { + bytes.push((words[b >>> 5] >>> (24 - b % 32)) & 0xFF); + } + return bytes; +} + +exports.bytesToWordArray = function (bytes) { + return new WordArray.init(exports.bytesToWords(bytes), bytes.length) +} + +exports.wordArrayToBytes = function (wordArray) { + return exports.wordsToBytes(wordArray.words) +} + diff --git a/src/ecdsa.js b/src/ecdsa.js index 0b4e3c2..f189148 100644 --- a/src/ecdsa.js +++ b/src/ecdsa.js @@ -1,8 +1,7 @@ var sec = require('./jsbn/sec'); -var util = require('./util'); var SecureRandom = require('./jsbn/rng'); var BigInteger = require('./jsbn/jsbn'); -var conv = require('./convert') +var convert = require('./convert') var HmacSHA256 = require('crypto-js/hmac-sha256'); var ECPointFp = require('./jsbn/ec').ECPointFp; @@ -43,16 +42,16 @@ function deterministicGenerateK(hash,key) { var kArr = []; for (var i = 0;i < 32;i++) vArr.push(1); for (var i = 0;i < 32;i++) kArr.push(0); - var v = util.bytesToWordArray(vArr) - var k = util.bytesToWordArray(kArr) + var v = convert.bytesToWordArray(vArr) + var k = convert.bytesToWordArray(kArr) - k = HmacSHA256(util.bytesToWordArray(vArr.concat([0]).concat(key).concat(hash)), k) + k = HmacSHA256(convert.bytesToWordArray(vArr.concat([0]).concat(key).concat(hash)), k) v = HmacSHA256(v, k) - vArr = util.wordArrayToBytes(v) - k = HmacSHA256(util.bytesToWordArray(vArr.concat([1]).concat(key).concat(hash)), k) + vArr = convert.wordArrayToBytes(v) + k = HmacSHA256(convert.bytesToWordArray(vArr.concat([1]).concat(key).concat(hash)), k) v = HmacSHA256(v,k) v = HmacSHA256(v,k) - vArr = util.wordArrayToBytes(v) + vArr = convert.wordArrayToBytes(v) return BigInteger.fromByteArrayUnsigned(vArr); } diff --git a/src/eckey.js b/src/eckey.js index a729d89..62b8f87 100644 --- a/src/eckey.js +++ b/src/eckey.js @@ -2,7 +2,7 @@ var BigInteger = require('./jsbn/jsbn'); var sec = require('./jsbn/sec'); var base58 = require('./base58'); var util = require('./util'); -var conv = require('./convert'); +var convert = require('./convert'); var Address = require('./address'); var ecdsa = require('./ecdsa'); var ECPointFp = require('./jsbn/ec').ECPointFp; @@ -33,12 +33,12 @@ ECKey.prototype.import = function (input,compressed,version) { : input instanceof BigInteger ? input.mod(ecparams.getN()) : Array.isArray(input) ? fromBin(input.slice(0,32)) : typeof input != "string" ? null - : input.length == 44 ? fromBin(conv.base64ToBytes(input)) + : input.length == 44 ? fromBin(convert.base64ToBytes(input)) : input.length == 51 && input[0] == '5' ? fromBin(base58.checkDecode(input)) : input.length == 51 && input[0] == '9' ? fromBin(base58.checkDecode(input)) : input.length == 52 && has('LK',input[0]) ? fromBin(base58.checkDecode(input).slice(0,32)) : input.length == 52 && input[0] == 'c' ? fromBin(base58.checkDecode(input).slice(0,32)) - : has([64,65],input.length) ? fromBin(conv.hexToBytes(input.slice(0,64))) + : has([64,65],input.length) ? fromBin(convert.hexToBytes(input.slice(0,64))) : null this.compressed = @@ -86,7 +86,7 @@ ECKey.prototype['export'] = function(format) { }; ECKey.prototype.toBin = function() { - return conv.bytesToString(this.toBytes()) + return convert.bytesToString(this.toBytes()) } ECKey.prototype.toBase58 = function() { @@ -96,7 +96,7 @@ ECKey.prototype.toBase58 = function() { ECKey.prototype.toWif = ECKey.prototype.toBase58 ECKey.prototype.toHex = function() { - return conv.bytesToHex(this.toBytes()) + return convert.bytesToHex(this.toBytes()) } ECKey.prototype.toBytes = function() { @@ -106,7 +106,7 @@ ECKey.prototype.toBytes = function() { } ECKey.prototype.toBase64 = function() { - return conv.bytesToBase64(this.toBytes()) + return convert.bytesToBase64(this.toBytes()) } ECKey.prototype.toString = ECKey.prototype.toBase58 @@ -146,7 +146,7 @@ ECPubKey.prototype.import = function(input,compressed,version) { input instanceof ECPointFp ? input : input instanceof ECKey ? ecparams.getG().multiply(input.priv) : input instanceof ECPubKey ? input.pub - : typeof input == "string" ? decode(conv.hexToBytes(input)) + : typeof input == "string" ? decode(convert.hexToBytes(input)) : Array.isArray(input) ? decode(input) : ecparams.getG().multiply(ecdsa.getBigRandom(ecparams.getN())) @@ -182,11 +182,11 @@ ECPubKey.prototype.toBytes = function(compressed) { } ECPubKey.prototype.toHex = function() { - return conv.bytesToHex(this.toBytes()) + return convert.bytesToHex(this.toBytes()) } ECPubKey.prototype.toBin = function() { - return conv.bytesToString(this.toBytes()) + return convert.bytesToString(this.toBytes()) } ECPubKey.prototype.toBase58 = function() { diff --git a/src/hdwallet.js b/src/hdwallet.js index 10d5a6b..309955d 100644 --- a/src/hdwallet.js +++ b/src/hdwallet.js @@ -13,8 +13,8 @@ var Network = require('./network') var HDWallet = module.exports = function(seed, network) { if (seed === undefined) return - var seedWords = util.bytesToWordArray(convert.stringToBytes(seed)) - var I = util.wordArrayToBytes(HmacSHA512(seedWords, 'Bitcoin seed')) + var seedWords = convert.bytesToWordArray(convert.stringToBytes(seed)) + var I = convert.wordArrayToBytes(HmacSHA512(seedWords, 'Bitcoin seed')) this.chaincode = I.slice(32) this.network = network || 'mainnet' if(!Network.hasOwnProperty(this.network)) { @@ -72,7 +72,7 @@ HDWallet.fromBytes = function(input) { // 4 byte: version bytes (mainnet: 0x0488B21E public, 0x0488ADE4 private; // testnet: 0x043587CF public, 0x04358394 private) var versionBytes = input.slice(0, 4) - var versionWord = util.bytesToWords(versionBytes)[0] + var versionWord = convert.bytesToWords(versionBytes)[0] var type for(var name in Network) { @@ -97,7 +97,7 @@ HDWallet.fromBytes = function(input) { // 4 bytes: child number. This is the number i in xi = xpar/i, with xi the key being serialized. // This is encoded in MSB order. (0x00000000 if master key) - hd.index = util.bytesToNum(input.slice(9, 13).reverse()) + hd.index = convert.bytesToNum(input.slice(9, 13).reverse()) assert(hd.depth > 0 || hd.index === 0) // 32 bytes: the chain code @@ -134,7 +134,7 @@ HDWallet.prototype.toBytes = function(priv) { // 4 byte: version bytes (mainnet: 0x0488B21E public, 0x0488ADE4 private; testnet: 0x043587CF public, // 0x04358394 private) var version = Network[this.network].hdVersions[priv ? 'priv' : 'pub'] - var vBytes = util.wordsToBytes([version]) + var vBytes = convert.wordsToBytes([version]) buffer = buffer.concat(vBytes) assert.equal(buffer.length, 4) @@ -150,7 +150,7 @@ HDWallet.prototype.toBytes = function(priv) { // 4 bytes: child number. This is the number i in xi = xpar/i, with xi the key being serialized. // This is encoded in MSB order. (0x00000000 if master key) - buffer = buffer.concat(util.numToBytes(this.index, 4).reverse()) + buffer = buffer.concat(convert.numToBytes(this.index, 4).reverse()) assert.equal(buffer.length, 4 + 1 + 4 + 4) // 32 bytes: the chain code @@ -184,7 +184,7 @@ HDWallet.prototype.toBase58 = function(priv) { HDWallet.prototype.derive = function(i) { var I - , iBytes = util.numToBytes(i, 4).reverse() + , iBytes = convert.numToBytes(i, 4).reverse() , cPar = this.chaincode , usePriv = i >= HDWallet.HIGHEST_BIT , SHA512 = Crypto.algo.SHA512 diff --git a/src/message.js b/src/message.js index 2c0f03e..c817632 100644 --- a/src/message.js +++ b/src/message.js @@ -2,21 +2,20 @@ var SHA256 = require('crypto-js/sha256'); var ecdsa = require('./ecdsa'); -var conv = require('./convert'); -var util = require('./util'); +var convert = require('./convert'); var Message = {}; Message.magicPrefix = "Bitcoin Signed Message:\n"; Message.makeMagicMessage = function (message) { - var magicBytes = conv.stringToBytes(Message.magicPrefix); - var messageBytes = conv.stringToBytes(message); + var magicBytes = convert.stringToBytes(Message.magicPrefix); + var messageBytes = convert.stringToBytes(message); var buffer = []; - buffer = buffer.concat(util.numToVarInt(magicBytes.length)); + buffer = buffer.concat(convert.numToVarInt(magicBytes.length)); buffer = buffer.concat(magicBytes); - buffer = buffer.concat(util.numToVarInt(messageBytes.length)); + buffer = buffer.concat(convert.numToVarInt(messageBytes.length)); buffer = buffer.concat(messageBytes); return buffer; @@ -24,7 +23,7 @@ Message.makeMagicMessage = function (message) { Message.getHash = function (message) { var buffer = Message.makeMagicMessage(message); - return util.wordArrayToBytes(SHA256(SHA256(util.bytesToWordArray(buffer)))); + return convert.wordArrayToBytes(SHA256(SHA256(convert.bytesToWordArray(buffer)))); }; Message.signMessage = function (key, message, compressed) { @@ -48,11 +47,11 @@ Message.signMessage = function (key, message, compressed) { sig = [i].concat(rBa).concat(sBa); - return conv.bytesToHex(sig); + return convert.bytesToHex(sig); }; Message.verifyMessage = function (address, sig, message) { - sig = conv.hexToBytes(sig); + sig = convert.hexToBytes(sig); sig = ecdsa.parseSigCompact(sig); var hash = Message.getHash(message); diff --git a/src/script.js b/src/script.js index aa99322..8c28012 100644 --- a/src/script.js +++ b/src/script.js @@ -1,6 +1,6 @@ var Opcode = require('./opcode'); var util = require('./util'); -var conv = require('./convert'); +var convert = require('./convert'); var Address = require('./address'); var network = require('./network'); @@ -13,7 +13,7 @@ var Script = function(data) { }; Script.fromHex = function(data) { - return new Script(conv.hexToBytes(data)) + return new Script(convert.hexToBytes(data)) }; Script.fromPubKey = function(str) { @@ -23,7 +23,7 @@ Script.fromPubKey = function(str) { if (Opcode.map.hasOwnProperty(s[i])) { script.writeOp(Opcode.map[s[i]]); } else { - script.writeBytes(conv.hexToBytes(s[i])); + script.writeBytes(convert.hexToBytes(s[i])); } } return script; @@ -36,7 +36,7 @@ Script.fromScriptSig = function(str) { if (Opcode.map.hasOwnProperty(s[i])) { script.writeOp(Opcode.map[s[i]]); } else { - script.writeBytes(conv.hexToBytes(s[i])); + script.writeBytes(convert.hexToBytes(s[i])); } } return script; diff --git a/src/transaction.js b/src/transaction.js index 5ffd0b7..527a945 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -1,7 +1,7 @@ var BigInteger = require('./jsbn/jsbn'); var Script = require('./script'); var util = require('./util'); -var conv = require('./convert'); +var convert = require('./convert'); var Wallet = require('./wallet'); var ECKey = require('./eckey').ECKey; var ECDSA = require('./ecdsa'); @@ -120,36 +120,36 @@ Transaction.prototype.addOutput = function (address, value) { */ Transaction.prototype.serialize = function () { var buffer = []; - buffer = buffer.concat(util.numToBytes(parseInt(this.version),4)); - buffer = buffer.concat(util.numToVarInt(this.ins.length)); + buffer = buffer.concat(convert.numToBytes(parseInt(this.version),4)); + buffer = buffer.concat(convert.numToVarInt(this.ins.length)); for (var i = 0; i < this.ins.length; i++) { var txin = this.ins[i]; // Why do blockchain.info, blockexplorer.com, sx and just about everybody // else use little-endian hashes? No idea... - buffer = buffer.concat(conv.hexToBytes(txin.outpoint.hash).reverse()); + buffer = buffer.concat(convert.hexToBytes(txin.outpoint.hash).reverse()); - buffer = buffer.concat(util.numToBytes(parseInt(txin.outpoint.index),4)); + buffer = buffer.concat(convert.numToBytes(parseInt(txin.outpoint.index),4)); var scriptBytes = txin.script.buffer; - buffer = buffer.concat(util.numToVarInt(scriptBytes.length)); + buffer = buffer.concat(convert.numToVarInt(scriptBytes.length)); buffer = buffer.concat(scriptBytes); - buffer = buffer.concat(util.numToBytes(parseInt(txin.sequence),4)); + buffer = buffer.concat(convert.numToBytes(parseInt(txin.sequence),4)); } - buffer = buffer.concat(util.numToVarInt(this.outs.length)); + buffer = buffer.concat(convert.numToVarInt(this.outs.length)); for (var i = 0; i < this.outs.length; i++) { var txout = this.outs[i]; - buffer = buffer.concat(util.numToBytes(txout.value,8)); + buffer = buffer.concat(convert.numToBytes(txout.value,8)); var scriptBytes = txout.script.buffer; - buffer = buffer.concat(util.numToVarInt(scriptBytes.length)); + buffer = buffer.concat(convert.numToVarInt(scriptBytes.length)); buffer = buffer.concat(scriptBytes); } - buffer = buffer.concat(util.numToBytes(parseInt(this.lock_time),4)); + buffer = buffer.concat(convert.numToBytes(parseInt(this.lock_time),4)); return buffer; }; Transaction.prototype.serializeHex = function() { - return conv.bytesToHex(this.serialize()); + return convert.bytesToHex(this.serialize()); } var OP_CODESEPARATOR = 171; @@ -205,7 +205,7 @@ function (connectedScript, inIndex, hashType) var buffer = txTmp.serialize(); - buffer = buffer.concat(util.numToBytes(parseInt(hashType),4)); + buffer = buffer.concat(convert.numToBytes(parseInt(hashType),4)); return Message.getHash(buffer) }; @@ -378,12 +378,12 @@ Transaction.prototype.calcImpact = function (wallet) { // Calculate credit to us from all outputs var valueOut = this.outs.filter(function(o) { - return wallet.hasHash(conv.bytesToHex(o.script.simpleOutPubKeyHash())); + return wallet.hasHash(convert.bytesToHex(o.script.simpleOutPubKeyHash())); }) .reduce(function(t,o) { return t+o.value },0); var valueIn = this.ins.filter(function(i) { - return wallet.hasHash(conv.bytesToHex(i.script.simpleInPubKeyHash())) + return wallet.hasHash(convert.bytesToHex(i.script.simpleInPubKeyHash())) && wallet.txIndex[i.outpoint.hash]; }) .reduce(function(t,i) { @@ -409,7 +409,7 @@ Transaction.prototype.calcImpact = function (wallet) { Transaction.deserialize = function(buffer) { if (typeof buffer == "string") { - buffer = conv.hexToBytes(buffer) + buffer = convert.hexToBytes(buffer) } var pos = 0; var readAsInt = function(bytes) { @@ -441,7 +441,7 @@ Transaction.deserialize = function(buffer) { for (var i = 0; i < ins; i++) { obj.ins.push({ outpoint: { - hash: conv.bytesToHex(readBytes(32).reverse()), + hash: convert.bytesToHex(readBytes(32).reverse()), index: readAsInt(4) }, script: new Script(readVarString()), @@ -451,7 +451,7 @@ Transaction.deserialize = function(buffer) { var outs = readVarInt(); for (var i = 0; i < outs; i++) { obj.outs.push({ - value: util.bytesToNum(readBytes(8)), + value: convert.bytesToNum(readBytes(8)), script: new Script(readVarString()) }); } @@ -523,8 +523,8 @@ Transaction.prototype.applyMultisigs = function(index, script, sigs, type) { Transaction.prototype.validateSig = function(index, script, sig, pub) { script = new Script(script); var hash = this.hashTransactionForSignature(script,index,1); - return ECDSA.verify(hash, conv.coerceToBytes(sig), - conv.coerceToBytes(pub)); + return ECDSA.verify(hash, convert.coerceToBytes(sig), + convert.coerceToBytes(pub)); } @@ -561,7 +561,7 @@ var TransactionOut = function (data) { this.script = data.script instanceof Script ? data.script.clone() : Array.isArray(data.script) ? new Script(data.script) - : typeof data.script == "string" ? new Script(conv.hexToBytes(data.script)) + : typeof data.script == "string" ? new Script(convert.hexToBytes(data.script)) : data.scriptPubKey ? Script.fromScriptSig(data.scriptPubKey) : data.address ? Script.createOutputScript(data.address) : new Script(); @@ -569,7 +569,7 @@ var TransactionOut = function (data) { if (this.script.buffer.length > 0) this.address = this.script.toAddress(); this.value = - Array.isArray(data.value) ? util.bytesToNum(data.value) + Array.isArray(data.value) ? convert.bytesToNum(data.value) : "string" == typeof data.value ? parseInt(data.value) : data.value instanceof BigInteger ? parseInt(data.value.toString()) : data.value; diff --git a/src/util.js b/src/util.js index bc61ec7..bdebae8 100644 --- a/src/util.js +++ b/src/util.js @@ -1,63 +1,8 @@ +var convert = require('./convert.js') var Crypto = require('crypto-js'); var RIPEMD160 = Crypto.RIPEMD160; var SHA256 = Crypto.SHA256; var HMAC= Crypto.algo.HMAC; -var WordArray = Crypto.lib.WordArray; - -/** - * Create a byte array representing a number with the given length - */ -exports.numToBytes = function(num, bytes) { - if (bytes === undefined) bytes = 8; - if (bytes === 0) return []; - return [num % 256].concat(module.exports.numToBytes(Math.floor(num / 256), bytes - 1)); -} - -/** - * Convert a byte array to the number that it represents - */ -exports.bytesToNum = function(bytes) { - if (bytes.length === 0) return 0; - return bytes[0] + 256 * module.exports.bytesToNum(bytes.slice(1)); -} - -/** - * Turn an integer into a "var_int". - * - * "var_int" is a variable length integer used by Bitcoin's binary format. - * - * Returns a byte array. - */ -exports.numToVarInt = function(num) { - if (num < 253) return [num]; - if (num < 65536) return [253].concat(exports.numToBytes(num, 2)); - if (num < 4294967296) return [254].concat(exports.numToBytes(num, 4)); - return [253].concat(exports.numToBytes(num, 8)); -} - -exports.bytesToWords = function (bytes) { - var words = []; - for (var i = 0, b = 0; i < bytes.length; i++, b += 8) { - words[b >>> 5] |= bytes[i] << (24 - b % 32); - } - return words; -} - -exports.wordsToBytes = function (words) { - var bytes = []; - for (var b = 0; b < words.length * 32; b += 8) { - bytes.push((words[b >>> 5] >>> (24 - b % 32)) & 0xFF); - } - return bytes; -} - -exports.bytesToWordArray = function (bytes) { - return new WordArray.init(exports.bytesToWords(bytes), bytes.length) -} - -exports.wordArrayToBytes = function (wordArray) { - return exports.wordsToBytes(wordArray.words) -} /** * Calculate RIPEMD160(SHA256(data)). @@ -66,15 +11,14 @@ exports.wordArrayToBytes = function (wordArray) { * array. */ exports.sha256ripe160 = function (data) { - var wordArray = RIPEMD160(SHA256(exports.bytesToWordArray(data))) - return exports.wordArrayToBytes(wordArray) + var wordArray = RIPEMD160(SHA256(convert.bytesToWordArray(data))) + return convert.wordArrayToBytes(wordArray) } - exports.HmacFromBytesToBytes = function (hasher, message, key) { - var hmac = HMAC.create(hasher, exports.bytesToWordArray(key)) - hmac.update(exports.bytesToWordArray(message)) - return exports.wordArrayToBytes(hmac.finalize()) + var hmac = HMAC.create(hasher, convert.bytesToWordArray(key)) + hmac.update(convert.bytesToWordArray(message)) + return convert.wordArrayToBytes(hmac.finalize()) } exports.error = function(msg) { diff --git a/src/wallet.js b/src/wallet.js index bf6efe2..d203a54 100644 --- a/src/wallet.js +++ b/src/wallet.js @@ -1,16 +1,12 @@ var Script = require('./script'); var ECKey = require('./eckey').ECKey; -var conv = require('./convert'); -var util = require('./util'); +var convert = require('./convert'); var assert = require('assert'); - var BigInteger = require('./jsbn/jsbn'); - var Transaction = require('./transaction').Transaction; var TransactionIn = require('./transaction').TransactionIn; var TransactionOut = require('./transaction').TransactionOut; var HDWallet = require('./hdwallet.js') - var SecureRandom = require('./jsbn/rng'); var rng = new SecureRandom(); @@ -43,7 +39,7 @@ var Wallet = function (seed, options) { if (!seed) { var seedBytes = new Array(32); rng.nextBytes(seedBytes); - seed = conv.bytesToString(seedBytes) + seed = convert.bytesToString(seedBytes) } masterkey = new HDWallet(seed, network); keys = [] @@ -63,7 +59,7 @@ var Wallet = function (seed, options) { // Processes a transaction object // If "verified" is true, then we trust the transaction as "final" this.processTx = function(tx, verified) { - var txhash = conv.bytesToHex(tx.getHash()) + var txhash = convert.bytesToHex(tx.getHash()) for (var i = 0; i < tx.outs.length; i++) { if (this.addresses.indexOf(tx.outs[i].address.toString()) >= 0) { me.outputs[txhash+':'+i] = { diff --git a/test/base58.js b/test/base58.js index 772214e..c95bfd7 100644 --- a/test/base58.js +++ b/test/base58.js @@ -1,14 +1,14 @@ /* global describe, it */ var assert = require('assert'); var base58 = require('../').base58; -var conv = require('../').convert; +var convert = require('../').convert; describe('base58', function() { describe('decode', function() { it('validates known examples', function() { var enc = '5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ'; var hex = '800c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d507a5b8d'; - assert.deepEqual(base58.decode(enc), conv.hexToBytes(hex)); + assert.deepEqual(base58.decode(enc), convert.hexToBytes(hex)); }) }) @@ -16,7 +16,7 @@ describe('base58', function() { it('handles known examples', function() { var enc = '5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ'; var hex = '800c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d507a5b8d'; - assert.equal(base58.encode(conv.hexToBytes(hex)), enc); + assert.equal(base58.encode(convert.hexToBytes(hex)), enc); }) }) diff --git a/test/convert.js b/test/convert.js index 2e8b510..b1c6001 100644 --- a/test/convert.js +++ b/test/convert.js @@ -44,4 +44,31 @@ describe('convert', function() { assert.equal(b64('foobar'), 'Zm9vYmFy') }) }) + + describe('byte array and word array conversions', function(){ + var bytes, wordArray; + + beforeEach(function(){ + bytes = [ + 98, 233, 7, 177, 92, 191, 39, 213, 66, 83, + 153, 235, 246, 240, 251, 80, 235, 184, 143, 24 + ] + wordArray = { + words: [1659439025, 1556031445, 1112775147, -151979184, -340226280], + sigBytes: 20 + } + }) + + describe('bytesToWords', function() { + it('works', function() { + assert.deepEqual(convert.bytesToWordArray(bytes), wordArray) + }) + }) + + describe('bytesToWords', function() { + it('works', function() { + assert.deepEqual(convert.wordArrayToBytes(wordArray), bytes) + }) + }) + }) }) diff --git a/test/misc.js b/test/misc.js index e1d0adc..2c85b89 100644 --- a/test/misc.js +++ b/test/misc.js @@ -10,10 +10,10 @@ var rng = new SecureRandom(); var ecparams = sec('secp256k1'); var ECPointFp = bitcoinjs.ECPointFp; -var util = require('../src/util'); +var convert = require('../src/convert'); function sha256FromBytesToBytes(message){ - return util.wordArrayToBytes(SHA256(util.bytesToWordArray(message))) + return convert.wordArrayToBytes(SHA256(convert.bytesToWordArray(message))) } it('Keys & Key Management', function () { diff --git a/test/util.js b/test/util.js deleted file mode 100644 index f76b5a5..0000000 --- a/test/util.js +++ /dev/null @@ -1,33 +0,0 @@ -var util = require('../src/util.js') -var assert = require('assert') - -describe('util', function() { - - describe('byte array and word array conversions', function(){ - var bytes, wordArray; - - beforeEach(function(){ - bytes = [ - 98, 233, 7, 177, 92, 191, 39, 213, 66, 83, - 153, 235, 246, 240, 251, 80, 235, 184, 143, 24 - ] - wordArray = { - words: [1659439025, 1556031445, 1112775147, -151979184, -340226280], - sigBytes: 20 - } - }) - - describe('bytesToWords', function() { - it('works', function() { - assert.deepEqual(util.bytesToWordArray(bytes), wordArray) - }) - }) - - describe('bytesToWords', function() { - it('works', function() { - assert.deepEqual(util.wordArrayToBytes(wordArray), bytes) - }) - }) - }) - -})