Skip to content

Instantly share code, notes, and snippets.

@acastellana
Created January 27, 2017 14:17
Show Gist options
  • Save acastellana/c87e57fe887b0553f85df788c1fc5837 to your computer and use it in GitHub Desktop.
Save acastellana/c87e57fe887b0553f85df788c1fc5837 to your computer and use it in GitHub Desktop.
/**
* createBrainWallet(seed) creates a new brain wallet using the seed as name and passphrase and returns it's main account
*/
createBrainWallet(seed) {
var deferred = this._q.defer();
var promise = deferred.promise;
// Seed can't be empty
if (!seed) {
this._Alert.missingtransferData(); // TODO: Add proper alert
return;
}
// Create the brain wallet from the seed
this._WalletBuilder.createBrainWallet(seed, seed, this.network).then((wallet) => {
this._$timeout(() => {
if (wallet) {
var mainAccount = {};
mainAccount.address = wallet.accounts[0].address;
mainAccount.password = seed;
mainAccount.privateKey = "";
// Decrypt/generate private key and check it. Returned private key is contained into mainAccount
if (!CryptoHelpers.passwordToPrivatekeyClear(mainAccount, wallet.accounts[0], wallet.accounts[0].algo, false)) {
this._Alert.invalidPassword();
return;
}
mainAccount.publicKey = KeyPair.create(mainAccount.privateKey).publicKey.toString();
// On success concat new wallet to local storage wallets
this._storage.wallets = this._storage.wallets.concat(wallet);
this._Alert.createWalletSuccess();
deferred.resolve(mainAccount);
}
}, 10);
},
(err) => {
this._Alert.createWalletFailed(err);
deferred.reject(false);
});
return deferred.promise;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment