rpc: do not use Address.getHash.

This commit is contained in:
Christopher Jeffrey 2017-05-21 21:42:35 -07:00
parent 7d61cda3aa
commit 76c20eddf1
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 18 additions and 27 deletions

View File

@ -2052,17 +2052,18 @@ RPC.prototype.verifyMessage = co(function* verifyMessage(args, help) {
var b58 = valid.str(0, '');
var sig = valid.buf(1, null, 'base64');
var msg = valid.str(2);
var hash = Address.getHash(b58);
var key;
var addr, key;
if (help || args.length !== 3) {
throw new RPCError(errs.MISC_ERROR,
'verifymessage "bitcoinaddress" "signature" "message"');
}
if (!hash || !sig || !msg)
if (!sig || !msg)
throw new RPCError(errs.TYPE_ERROR, 'Invalid parameters.');
addr = parseAddress(b58, this.network);
msg = new Buffer(MAGIC_STRING + msg, 'utf8');
msg = crypto.hash256(msg);
@ -2073,7 +2074,7 @@ RPC.prototype.verifyMessage = co(function* verifyMessage(args, help) {
key = crypto.hash160(key);
return crypto.ccmp(key, hash);
return crypto.ccmp(key, addr.hash);
});
RPC.prototype.signMessageWithPrivkey = co(function* signMessageWithPrivkey(args, help) {

View File

@ -236,15 +236,12 @@ RPC.prototype.dumpPrivKey = co(function* dumpPrivKey(args, help) {
var wallet = this.wallet;
var valid = new Validator([args]);
var addr = valid.str(0, '');
var hash = Address.getHash(addr, 'hex');
var ring;
var hash, ring;
if (help || args.length !== 1)
throw new RPCError(errs.MISC_ERROR, 'dumpprivkey "bitcoinaddress"');
if (!hash)
throw new RPCError(errs.INVALID_ADDRESS_OR_KEY, 'Invalid address.');
hash = parseHash(addr, this.network);
ring = yield wallet.getPrivateKey(hash);
if (!ring)
@ -360,15 +357,12 @@ RPC.prototype.getAccount = co(function* getAccount(args, help) {
var wallet = this.wallet;
var valid = new Validator([args]);
var addr = valid.str(0, '');
var hash = Address.getHash(addr, 'hex');
var path;
var hash, path;
if (help || args.length !== 1)
throw new RPCError(errs.MISC_ERROR, 'getaccount "bitcoinaddress"');
if (!hash)
throw new RPCError(errs.TYPE_ERROR, 'Invalid address.');
hash = parseHash(addr, this.network);
path = yield wallet.getPath(hash);
if (!path)
@ -518,19 +512,16 @@ RPC.prototype.getReceivedByAddress = co(function* getReceivedByAddress(args, hel
var valid = new Validator([args]);
var addr = valid.str(0, '');
var minconf = valid.u32(1, 0);
var hash = Address.getHash(addr, 'hex');
var height = this.wdb.state.height;
var total = 0;
var i, j, wtx, output, txs;
var i, j, hash, wtx, output, txs;
if (help || args.length < 1 || args.length > 2) {
throw new RPCError(errs.MISC_ERROR,
'getreceivedbyaddress "bitcoinaddress" ( minconf )');
}
if (!hash)
throw new RPCError(errs.TYPE_ERROR, 'Invalid address');
hash = parseHash(addr, this.network);
txs = yield wallet.getHistory();
for (i = 0; i < txs.length; i++) {
@ -1217,10 +1208,7 @@ RPC.prototype.listUnspent = co(function* listUnspent(args, help) {
valid = new Validator([addrs]);
for (i = 0; i < addrs.length; i++) {
address = valid.str(i, '');
hash = Address.getHash(address, 'hex');
if (!hash)
throw new RPCError(errs.INVALID_ADDRESS_OR_KEY, 'Invalid address.');
hash = parseHash(address, this.network);
if (map[hash])
throw new RPCError(errs.INVALID_PARAMETER, 'Duplicate address.');
@ -1491,10 +1479,7 @@ RPC.prototype.signMessage = co(function* signMessage(args, help) {
'signmessage "bitcoinaddress" "message"');
}
addr = Address.getHash(addr, 'hex');
if (!addr)
throw new RPCError(errs.INVALID_ADDRESS_OR_KEY, 'Invalid address.');
addr = parseHash(addr, this.network);
ring = yield wallet.getKey(addr);
@ -1672,6 +1657,11 @@ RPC.prototype.setLogLevel = co(function* setLogLevel(args, help) {
* Helpers
*/
function parseHash(raw, network) {
var addr = parseAddress(raw, network);
return addr.getHash('hex');
}
function parseAddress(raw, network) {
try {
return Address.fromString(raw, network);