Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
self.recoveryForm = function () {
// 首先拼接一下 12个 助记词
self.strMnemonic();
if (self.inputMnemonic) {
self.error = '';
// 首先转换成小写
self.inputMnemonic = self.inputMnemonic.toLowerCase();
if ((self.inputMnemonic.split(' ').length % 3 === 0) && Mnemonic.isValid(self.inputMnemonic)) {
self.scanning = true;
// 向内存中写入2
// self.haschoosen();
// 模态框 显示出来
self.show = true;
if (self.bLight) {
scanForAddressesAndWalletsInLightClient(self.inputMnemonic, cleanAndAddWalletsAndAddresses);
} else {
scanForAddressesAndWallets(self.inputMnemonic, cleanAndAddWalletsAndAddresses);
}
} else {
self.error = 'Seed is not valid';
rl.question('Passphrase for your private keys: ', function(passphrase){
rl.close();
if (process.stdout.moveCursor) process.stdout.moveCursor(0, -1);
if (process.stdout.clearLine) process.stdout.clearLine();
var deviceTempPrivKey = crypto.randomBytes(32);
var devicePrevTempPrivKey = crypto.randomBytes(32);
var mnemonic = new Mnemonic(); // generates new mnemonic
while (!Mnemonic.isValid(mnemonic.toString()))
mnemonic = new Mnemonic();
writeKeys(mnemonic.phrase, deviceTempPrivKey, devicePrevTempPrivKey, function(){
console.log('keys created');
var xPrivKey = mnemonic.toHDPrivateKey(passphrase);
createWallet(xPrivKey, function(){
onDone(mnemonic.phrase, passphrase, deviceTempPrivKey, devicePrevTempPrivKey);
});
});
});
});
export const isValidPassphrase = (passphrase) => {
const normalizedValue = passphrase.replace(/ +/g, ' ').trim();
let isValid;
try {
isValid = normalizedValue.split(' ').length >= 12 && mnemonic.isValid(normalizedValue);
} catch (e) {
// If the mnemonic check throws an error, we assume that the
// passphrase being entered isn't valid
isValid = false;
}
return isValid;
};
function generateKey() {
let deviceTempPrivKey = crypto.randomBytes(32);
let devicePrevTempPrivKey = crypto.randomBytes(32);
let mnemonic = new Mnemonic(); // generates new mnemonic
while (!Mnemonic.isValid(mnemonic.toString())) {
mnemonic = new Mnemonic();
}
return {mnemonic, deviceTempPrivKey, devicePrevTempPrivKey};
}
var validator = function (value) {
try {
value = value.split('-').join(' ');
if (Mnemonic.isValid(value)) {
ctrl.$setValidity('validMnemonic', true);
return value;
} else {
ctrl.$setValidity('validMnemonic', false);
return value;
}
} catch (ex) {
ctrl.$setValidity('validMnemonic', false);
return value;
}
};
ctrl.$parsers.unshift(validator);
function create() {
self.inputMnemonic = self.value;
if (self.inputMnemonic) {
self.error = '';
self.inputMnemonic = self.inputMnemonic.toLowerCase();
if (self.inputMnemonic == fc.credentials.mnemonic) {
self.error = 'can not recover the same wallet';
return;
}
if ((self.inputMnemonic.split(' ').length % 3 === 0) && Mnemonic.isValid(self.inputMnemonic)) {
self.scanning = true;
if (self.bLight) {
setTimeout(function () {
scanForAddressesAndWalletsInLightClient(self.inputMnemonic, cleanAndAddWalletsAndAddresses);
}, 3 * 1000);
} else {
scanForAddressesAndWallets(self.inputMnemonic, cleanAndAddWalletsAndAddresses);
}
} else {
self.error = 'Seed is not valid';
}
}
}
create();
function expandMnemonic(mnemonic) {
var addrInfo = {};
mnemonic = mnemonic.toLowerCase().split('-').join(' ');
if ((mnemonic.split(' ').length % 3 !== 0) || !Mnemonic.isValid(mnemonic)) {
throw new Error("invalid mnemonic: "+mnemonic);
}
mnemonic = new Mnemonic(mnemonic);
addrInfo.xPrivKey = mnemonic.toHDPrivateKey().derive("m/44'/0'/0'/0/0");
addrInfo.pubkey = addrInfo.xPrivKey.publicKey.toBuffer().toString("base64");
addrInfo.definition = ["sig", {"pubkey": addrInfo.pubkey}];
addrInfo.address = objectHash.getChash160(addrInfo.definition);
return addrInfo;
}
var validator = function(value) {
try {
value = value.split('-').join(' ');
if (Mnemonic.isValid(value)) {
ctrl.$setValidity('validMnemonic', true);
return value;
} else {
ctrl.$setValidity('validMnemonic', false);
return value;
}
} catch(ex) {
ctrl.$setValidity('validMnemonic', false);
return value;
}
};
ctrl.$parsers.unshift(validator);
Credentials.createWithMnemonic = function(network, passphrase, language, account) {
_checkNetwork(network);
if (!wordsForLang[language])
throw new Error('Unsupported language');
$.shouldBeNumber(account);
var m = new Mnemonic(wordsForLang[language]);
while (!Mnemonic.isValid(m.toString())) {
console.log('--- retrying mnemonic generation');
m = new Mnemonic(wordsForLang[language])
};
var x = new Credentials();
x.network = network;
x.account = account;
x.xPrivKey = m.toHDPrivateKey(passphrase, network).toString();
x._expand();
x.mnemonic = m.phrase;
x.mnemonicHasPassphrase = !!passphrase;
console.log("credentials: createWithMnemonic " + network + ', ' + passphrase + ',' + language + ',' + account);
return x;
};
rl.question('mnemonic:', (mnemonic_phrase) => {
mnemonic_phrase = mnemonic_phrase.trim().toLowerCase();
if ((mnemonic_phrase.split(' ').length % 3 === 0) && Mnemonic.isValid(mnemonic_phrase)) {
let deviceTempPrivKey = crypto.randomBytes(32);
let devicePrevTempPrivKey = crypto.randomBytes(32);
writeKeys(mnemonic_phrase, deviceTempPrivKey, devicePrevTempPrivKey, () => {
getKeys(callback)
})
} else {
throw new Error('Incorrect mnemonic phrase!')
}
});
} else {