From 5fba6c7f753971c95e35fc87a7569a594678b663 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Fri, 30 Oct 2015 10:34:13 -0300 Subject: [PATCH] redefine constants --- lib/server.js | 25 +++++++++++++++++++------ lib/utils.js | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/lib/server.js b/lib/server.js index e9805f4..32e2918 100644 --- a/lib/server.js +++ b/lib/server.js @@ -51,6 +51,19 @@ function WalletService() { this.notifyTicker = 0; }; +WalletService.SCRIPT_TYPES = { + P2SH: 'P2SH', + P2PKH: 'P2PKH', +}; +WalletService.DERIVATION_STRATEGIES = { + BIP44: 'BIP44', + BIP45: 'BIP45', +}; + +WalletService.DEFAULT_FEE_PER_KB = 10000; +WalletService.MIN_FEE_PER_KB = 0; +WalletService.MAX_FEE_PER_KB = 1000000; +WalletService.MAX_TX_FEE = 1 * 1e8; WalletService.MAX_KEYS = 100; @@ -234,8 +247,8 @@ WalletService.prototype.createWallet = function(opts, cb) { opts.supportBIP44AndP2PKH = _.isBoolean(opts.supportBIP44AndP2PKH) ? opts.supportBIP44AndP2PKH : true; - var derivationStrategy = opts.supportBIP44AndP2PKH ? WalletUtils.DERIVATION_STRATEGIES.BIP44 : WalletUtils.DERIVATION_STRATEGIES.BIP45; - var addressType = (opts.n == 1 && opts.supportBIP44AndP2PKH) ? WalletUtils.SCRIPT_TYPES.P2PKH : WalletUtils.SCRIPT_TYPES.P2SH; + var derivationStrategy = opts.supportBIP44AndP2PKH ? WalletService.DERIVATION_STRATEGIES.BIP44 : WalletService.DERIVATION_STRATEGIES.BIP45; + var addressType = (opts.n == 1 && opts.supportBIP44AndP2PKH) ? WalletService.SCRIPT_TYPES.P2PKH : WalletService.SCRIPT_TYPES.P2SH; try { pubKey = new PublicKey.fromString(opts.pubKey); @@ -593,12 +606,12 @@ WalletService.prototype.joinWallet = function(opts, cb) { if (opts.supportBIP44AndP2PKH) { // New client trying to join legacy wallet - if (wallet.derivationStrategy == WalletUtils.DERIVATION_STRATEGIES.BIP45) { + if (wallet.derivationStrategy == WalletService.DERIVATION_STRATEGIES.BIP45) { return cb(new ClientError('The wallet you are trying to join was created with an older version of the client app.')); } } else { // Legacy client trying to join new wallet - if (wallet.derivationStrategy == WalletUtils.DERIVATION_STRATEGIES.BIP44) { + if (wallet.derivationStrategy == WalletService.DERIVATION_STRATEGIES.BIP44) { return cb(new ClientError(Errors.codes.UPGRADE_NEEDED, 'To join this wallet you need to upgrade your client app.')); } } @@ -1212,8 +1225,8 @@ WalletService.prototype.createTx = function(opts, cb) { valid: false })) return; - var feePerKb = opts.feePerKb || WalletUtils.DEFAULT_FEE_PER_KB; - if (feePerKb < WalletUtils.MIN_FEE_PER_KB || feePerKb > WalletUtils.MAX_FEE_PER_KB) + var feePerKb = opts.feePerKb || WalletService.DEFAULT_FEE_PER_KB; + if (feePerKb < WalletService.MIN_FEE_PER_KB || feePerKb > WalletService.MAX_FEE_PER_KB) return cb(new ClientError('Invalid fee per KB value')); self._runLocked(cb, function(cb) { diff --git a/lib/utils.js b/lib/utils.js index aadc018..96de0f2 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -22,4 +22,43 @@ Utils.strip = function(number) { return (parseFloat(number.toPrecision(12))); } + +/* TODO: It would be nice to be compatible with bitcoind signmessage. How + * the hash is calculated there? */ +Utils.hashMessage = function(text) { + $.checkArgument(text); + var buf = new Buffer(text); + var ret = crypto.Hash.sha256sha256(buf); + ret = new Bitcore.encoding.BufferReader(ret).readReverse(); + return ret; +}; + + +Utils.signMessage = function(text, privKey) { + $.checkArgument(text); + var priv = new PrivateKey(privKey); + var hash = Utils.hashMessage(text); + return crypto.ECDSA.sign(hash, priv, 'little').toString(); +}; + + +Utils.verifyMessage = function(text, signature, pubKey) { + $.checkArgument(text); + $.checkArgument(pubKey); + + if (!signature) + return false; + + var pub = new PublicKey(pubKey); + var hash = Utils.hashMessage(text); + + try { + var sig = new crypto.Signature.fromString(signature); + return crypto.ECDSA.verify(hash, sig, pub, 'little'); + } catch (e) { + return false; + } +}; + + module.exports = Utils;