Created
March 2, 2017 11:52
-
-
Save mpolci/5c3427fd287c2e32516fb896e7ff4771 to your computer and use it in GitHub Desktop.
BIP39 Mnemonic words advice directive
This file contains hidden or 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
'use strict'; | |
angular.module('copayAddon.twinWallet').directive('twMnemonicAdvice', function () { | |
return { | |
restrict: 'AE', | |
transclude: true, | |
scope: { | |
prefix: '=', | |
language: '=', | |
words: '=' | |
}, | |
//controllerAs: 'advice', | |
controller: ['$scope', 'lodash', 'bitcoreMnemonic', function ($scope, lodash, bitcoreMnemonic) { | |
//TODO automatic language selection | |
var lang = 'ENGLISH' | |
if ($scope.language) $scope.language = lang; | |
var words = bitcoreMnemonic.Words[lang]; | |
//TODO optimize! this code scans the word list 3 times | |
var index = lodash(words) | |
.groupBy(function(w) { return w.charAt(0); }) | |
.mapValues(function(e) { return lodash(e) | |
.groupBy(function(w) { return w.charAt(1); }) | |
.mapValues(function(e) { return lodash(e) | |
.groupBy(function(w) { return w.charAt(2); }) | |
.value(); | |
}).value(); | |
}).value(); | |
$scope.$watch('prefix', function (newVal) { | |
var sl = newVal && newVal.length; | |
if (!sl) { | |
$scope.words = ''; | |
return; | |
} | |
var found = index; | |
var i=0; | |
for (; i < 3 && i<sl; i++){ | |
if (found) | |
found = found[newVal.charAt(i)]; | |
} | |
//TODO limit array elements on long list of matching words | |
for (; i<3; i++) { | |
found = lodash.reduce(found, function(total, v) { return total.concat(lodash.values(v)) }, []); | |
} | |
if (sl > 3) { | |
found = lodash.filter(found, function(v) { return v.lastIndexOf(newVal, 0) === 0 }); | |
} | |
$scope.words = found; | |
}); | |
}], | |
template: '<div ng-transclude></div>' | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment