Matias Alejo Garcia
10 years ago
10 changed files with 162 additions and 125 deletions
@ -1,25 +0,0 @@ |
|||||
|
|
||||
var _ = require('lodash'); |
|
||||
|
|
||||
var Bitcore = require('bitcore'); |
|
||||
var BitcoreAddress = Bitcore.Address; |
|
||||
|
|
||||
function BitcoinUtils () {}; |
|
||||
|
|
||||
BitcoinUtils.deriveAddress = function(publicKeyRing, path, m, network) { |
|
||||
|
|
||||
var publicKeys = _.map(publicKeyRing, function(xPubKey) { |
|
||||
var xpub = new Bitcore.HDPublicKey(xPubKey); |
|
||||
return xpub.derive(path).publicKey; |
|
||||
}); |
|
||||
|
|
||||
var bitcoreAddress = BitcoreAddress.createMultisig(publicKeys, m, network); |
|
||||
|
|
||||
return { |
|
||||
address: bitcoreAddress.toString(), |
|
||||
path: path, |
|
||||
publicKeys: _.invoke(publicKeys, 'toString'), |
|
||||
}; |
|
||||
}; |
|
||||
|
|
||||
module.exports = BitcoinUtils; |
|
@ -1,43 +0,0 @@ |
|||||
var _ = require('lodash'); |
|
||||
var Bitcore = require('bitcore'); |
|
||||
var PrivateKey = Bitcore.PrivateKey; |
|
||||
var PublicKey = Bitcore.PublicKey; |
|
||||
var Signature = Bitcore.crypto.Signature; |
|
||||
var ECDSA = Bitcore.crypto.ECDSA; |
|
||||
var Hash = Bitcore.crypto.Hash; |
|
||||
var BufferReader = Bitcore.encoding.BufferReader; |
|
||||
|
|
||||
|
|
||||
|
|
||||
var SignUtils = function() {}; |
|
||||
|
|
||||
/* TODO: It would be nice to be compatible with bitcoind signmessage. How |
|
||||
* the hash is calculated there? */ |
|
||||
SignUtils.hash = function(text) { |
|
||||
var buf = new Buffer(text); |
|
||||
var ret = Hash.sha256sha256(buf); |
|
||||
ret = new BufferReader(ret).readReverse(); |
|
||||
return ret; |
|
||||
}; |
|
||||
|
|
||||
|
|
||||
SignUtils.sign = function(text, privKey) { |
|
||||
var priv = new PrivateKey(privKey); |
|
||||
var hash = SignUtils.hash(text); |
|
||||
return ECDSA.sign(hash, priv, 'little').toString(); |
|
||||
}; |
|
||||
|
|
||||
|
|
||||
SignUtils.verify = function(text, signature, pubKey) { |
|
||||
var pub = new PublicKey(pubKey); |
|
||||
var hash = SignUtils.hash(text); |
|
||||
|
|
||||
try { |
|
||||
var sig = new Signature.fromString(signature); |
|
||||
return ECDSA.verify(hash, sig, pub, 'little'); |
|
||||
} catch (e) { |
|
||||
return false; |
|
||||
} |
|
||||
}; |
|
||||
|
|
||||
module.exports = SignUtils; |
|
@ -0,0 +1,59 @@ |
|||||
|
var _ = require('lodash'); |
||||
|
var Bitcore = require('bitcore'); |
||||
|
var Address = Bitcore.Address; |
||||
|
var PrivateKey = Bitcore.PrivateKey; |
||||
|
var PublicKey = Bitcore.PublicKey; |
||||
|
var crypto = Bitcore.crypto; |
||||
|
|
||||
|
function WalletUtils() {}; |
||||
|
|
||||
|
/* TODO: It would be nice to be compatible with bitcoind signmessage. How |
||||
|
* the hash is calculated there? */ |
||||
|
WalletUtils.hashMessage = function(text) { |
||||
|
var buf = new Buffer(text); |
||||
|
var ret = crypto.Hash.sha256sha256(buf); |
||||
|
ret = new Bitcore.encoding.BufferReader(ret).readReverse(); |
||||
|
return ret; |
||||
|
}; |
||||
|
|
||||
|
|
||||
|
WalletUtils.signMessage = function(text, privKey) { |
||||
|
var priv = new PrivateKey(privKey); |
||||
|
var hash = WalletUtils.hashMessage(text); |
||||
|
return crypto.ECDSA.sign(hash, priv, 'little').toString(); |
||||
|
}; |
||||
|
|
||||
|
|
||||
|
WalletUtils.verifyMessage = function(text, signature, pubKey) { |
||||
|
var pub = new PublicKey(pubKey); |
||||
|
var hash = WalletUtils.hashMessage(text); |
||||
|
|
||||
|
try { |
||||
|
var sig = new crypto.Signature.fromString(signature); |
||||
|
return crypto.ECDSA.verify(hash, sig, pub, 'little'); |
||||
|
} catch (e) { |
||||
|
return false; |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
WalletUtils.deriveAddress = function(publicKeyRing, path, m, network) { |
||||
|
|
||||
|
var publicKeys = _.map(publicKeyRing, function(xPubKey) { |
||||
|
var xpub = new Bitcore.HDPublicKey(xPubKey); |
||||
|
return xpub.derive(path).publicKey; |
||||
|
}); |
||||
|
|
||||
|
var bitcoreAddress = Address.createMultisig(publicKeys, m, network); |
||||
|
|
||||
|
return { |
||||
|
address: bitcoreAddress.toString(), |
||||
|
path: path, |
||||
|
publicKeys: _.invoke(publicKeys, 'toString'), |
||||
|
}; |
||||
|
}; |
||||
|
|
||||
|
WalletUtils.getProposalHash = function(toAddress, amount, message) { |
||||
|
return toAddress + '|' + amount + '|' + (message || ''); |
||||
|
}; |
||||
|
|
||||
|
module.exports = WalletUtils; |
Loading…
Reference in new issue