forked from bitpay/insight
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from dashevo/fix/search
fix(search): adds utils for hash test + fix search issue
- Loading branch information
Showing
10 changed files
with
216 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,90 @@ | ||
'use strict'; | ||
|
||
angular.module('insight.search').controller('SearchController', | ||
function($scope, $routeParams, $location, $timeout, Global, Block, Transaction, Address, BlockByHeight) { | ||
$scope.global = Global; | ||
$scope.loading = false; | ||
angular.module('insight.search') | ||
.controller('SearchController', | ||
function ($scope, $rootScope, $routeParams, $location, $timeout, Global, Block, Transaction, Address, BlockHashValidator, TransactionHashValidator, AddressValidator, BlockByHeight) { | ||
$scope.global = Global; | ||
$scope.loading = false; | ||
|
||
var _badQuery = function() { | ||
$scope.badQuery = true; | ||
var currentNetwork = $rootScope.network; | ||
|
||
$timeout(function() { | ||
$scope.badQuery = false; | ||
}, 2000); | ||
}; | ||
var _badQuery = function () { | ||
$scope.badQuery = true; | ||
|
||
var _resetSearch = function() { | ||
$scope.q = ''; | ||
$scope.loading = false; | ||
}; | ||
$timeout(function () { | ||
$scope.badQuery = false; | ||
}, 2000); | ||
}; | ||
|
||
$scope.search = function() { | ||
var q = $scope.q; | ||
$scope.badQuery = false; | ||
$scope.loading = true; | ||
var _resetSearch = function () { | ||
$scope.q = ''; | ||
$scope.loading = false; | ||
}; | ||
|
||
Block.get({ | ||
blockHash: q | ||
}, function() { | ||
_resetSearch(); | ||
$location.path('block/' + q); | ||
}, function() { //block not found, search on TX | ||
Transaction.get({ | ||
txId: q | ||
}, function() { | ||
_resetSearch(); | ||
$location.path('tx/' + q); | ||
}, function() { //tx not found, search on Address | ||
Address.get({ | ||
addrStr: q | ||
}, function() { | ||
_resetSearch(); | ||
$location.path('address/' + q); | ||
}, function() { // block by height not found | ||
if (isFinite(q)) { // ensure that q is a finite number. A logical height value. | ||
BlockByHeight.get({ | ||
blockHeight: q | ||
}, function(hash) { | ||
_resetSearch(); | ||
$location.path('/block/' + hash.blockHash); | ||
}, function() { //not found, fail :( | ||
$scope.loading = false; | ||
_badQuery(); | ||
}); | ||
} | ||
else { | ||
$scope.loading = false; | ||
_badQuery(); | ||
} | ||
}); | ||
}); | ||
}); | ||
}; | ||
$scope.search = function () { | ||
var q = $scope.q; | ||
$scope.badQuery = false; | ||
$scope.loading = true; | ||
var isBlockHeight = isFinite(q); | ||
var isBlockHash = BlockHashValidator.test(q, currentNetwork); | ||
var isTransactionHash = TransactionHashValidator.test(q); | ||
var isAddress = AddressValidator.test(q); | ||
|
||
var badQueryLoadHandler = function () { | ||
$scope.loading = false; | ||
_badQuery(); | ||
}; | ||
|
||
var fetchAndRedirectTransactionSearch = function(){ | ||
return Transaction.get({ | ||
txId: q | ||
}, function () { | ||
_resetSearch(); | ||
$location.path('/tx/' + q); | ||
}, badQueryLoadHandler); | ||
}; | ||
|
||
}); | ||
var fetchAndRedirectBlockHeightSearch = function () { | ||
return BlockByHeight.get({ | ||
blockHeight: q | ||
}, function (hash) { | ||
_resetSearch(); | ||
$location.path('/block/' + hash.blockHash); | ||
}, badQueryLoadHandler); | ||
}; | ||
var fetchAndRedirectAddressSearch = function () { | ||
return Address.get({ | ||
addrStr: q | ||
}, function () { | ||
_resetSearch(); | ||
$location.path('address/' + q); | ||
}, badQueryLoadHandler); | ||
}; | ||
var fetchAndRedirectBlockSearch = function () { | ||
// Block hashes are identified by expecting 10 trailing zeroes as prefix (see difficulty) | ||
// If we are in the 1/Inf case of a txhash starting with ten zeroes, we will fallback on tx | ||
return Block.get({ | ||
blockHash: q | ||
}, function (res) { | ||
if(res.status === 404){ | ||
return fetchAndRedirectTransactionSearch(); | ||
} | ||
_resetSearch(); | ||
$location.path('/block/' + q); | ||
}, fetchAndRedirectTransactionSearch); | ||
}; | ||
|
||
if (isBlockHeight) { | ||
fetchAndRedirectBlockHeightSearch(); | ||
} else if (isAddress) { | ||
fetchAndRedirectAddressSearch(); | ||
} else if (isBlockHash) { | ||
fetchAndRedirectBlockSearch(); | ||
} else if (isTransactionHash) { | ||
fetchAndRedirectTransactionSearch(); | ||
} else { | ||
badQueryLoadHandler(); | ||
} | ||
}; | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,32 @@ | ||
'use strict'; | ||
|
||
angular.module('insight.address').factory('Address', | ||
function($resource) { | ||
return $resource(window.apiPrefix + '/addr/:addrStr/?noTxList=1', { | ||
addrStr: '@addStr' | ||
}, { | ||
get: { | ||
method: 'GET', | ||
interceptor: { | ||
response: function (res) { | ||
return res.data; | ||
}, | ||
responseError: function (res) { | ||
if (res.status === 404) { | ||
return res; | ||
angular.module('insight.address') | ||
.factory('Address', | ||
function ($resource) { | ||
return $resource(window.apiPrefix + '/addr/:addrStr/?noTxList=1', { | ||
addrStr: '@addStr' | ||
}, { | ||
get: { | ||
method: 'GET', | ||
interceptor: { | ||
response: function (res) { | ||
return res.data; | ||
}, | ||
responseError: function (res) { | ||
if (res.status === 404) { | ||
return res; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
}); | ||
}); | ||
}) | ||
.factory('AddressValidator', | ||
function () { | ||
return { | ||
test: function (addressStr) { | ||
|
||
|
||
return /^[XxYy][1-9A-Za-z][^OIl]{20,40}/.test(addressStr); | ||
} | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use strict'; | ||
|
||
angular.module('insight.hash') | ||
.factory('HashValidator', | ||
function () { | ||
return { | ||
test64: function (hashStr) { | ||
return typeof hashStr === 'string' && /^(0x)?[0-9a-f]{64}$/i.test(hashStr); | ||
}, | ||
test66: function (hashStr) { | ||
return typeof hashStr === 'string' && /^(0x)?[0-9a-f]{66}$/i.test(hashStr); | ||
} | ||
}; | ||
}); |
Oops, something went wrong.