<commit_before>import WalletDb from "stores/WalletDb";
import WalletUnlockActions from "actions/WalletUnlockActions";
import CachedPropertyActions from "actions/CachedPropertyActions";
import ApplicationApi from "api/ApplicationApi";
import {TransactionBuilder, FetchChain} from "bitsharesjs/es";
import {Apis} from "bitsharesjs-ws";
import alt from "alt-instance";
import SettingsStore from "stores/SettingsStore";

let application_api = new ApplicationApi();

class WalletActions {

    /** Restore and make active a new wallet_object. */
    restore(wallet_name = "default", wallet_object) {
        wallet_name = wallet_name.toLowerCase();
        return {wallet_name, wallet_object};
    }

    /** Make an existing wallet active or create a wallet (and make it active).
        If <b>wallet_name</b> does not exist, provide a <b>create_wallet_password</b>.
    */
    setWallet(wallet_name, create_wallet_password, brnkey) {
        WalletUnlockActions.lock();
        if( !wallet_name) wallet_name = "default";
        return (dispatch) => {
            return new Promise( resolve => {
                dispatch({wallet_name, create_wallet_password, brnkey, resolve});
            });
        };
    }

    setBackupDate() {
        CachedPropertyActions.set("backup_recommended", false);
        return true;
    }

    setBrainkeyBackupDate() {
        return true;
    }

    createAccountWithPassword( account_name, password, registrar, referrer, referrer_percent, refcode ) {
        let {privKey : owner_private} = WalletDb.generateKeyFromPassword(account_name, "owner", password);
        let {privKey: active_private} = WalletDb.generateKeyFromPassword(account_name, "active", password);
        //let memo_private = WalletDb.generateNextKey()

        return new Promise((resolve, reject) => {
            let create_account = () => {
                return application_api.create_account(
                    owner_private.private_key.toPublicKey().toPublicKeyString(),
                    active_private.private_key.toPublicKey().toPublicKeyString(),
                    account_name,
                    registrar, //registrar_id,
                    referrer, //referrer_id,
                    referrer_percent, //referrer_percent,
                    true //broadcast
                ).then(resolve).catch(reject);
            };

            if(registrar) {
                // using another user's account as registrar
                return create_account();
            } else {
                // using faucet

                let faucetAddress = SettingsStore.getSetting("faucet_address");
                if (window && window.location && window.location.protocol === "https:") {
                    faucetAddress = faucetAddress.replace(/http:\/\//, "https://");
                }

                let create_account_promise = fetch( faucetAddress + "/api/v1/accounts", {
                    method: "post",
                    mode: "cors",
                    headers: {
                        "Accept": "application/json",
                        "Content-type": "application/json"
                    },
                    body: JSON.stringify({
                        "account": {
                            "name": account_name,
                            "owner_key": owner_private.toPublicKey().toPublicKeyString(),
                            "active_key": active_private.toPublicKey().toPublicKeyString(),
                            "memo_key": active_private.toPublicKey().toPublicKeyString(),
                            //"memo_key": memo_private.private_key.toPublicKey().toPublicKeyString(),
                            "refcode": refcode,
                            "referrer": referrer
                        }
                    })
                }).then(r => resolve(r.json()));

                return create_account_promise.then(result => {
                    if (result.error) {
                        reject(result.error);
                    }
                    resolve(result);
                }).catch(error => {
                    reject(error);
                });
            }
        });

    }

    createAccount( account_name, registrar, referrer, referrer_percent, refcode ) {
        if( WalletDb.isLocked()) {
            let error = "wallet locked";
            //this.actions.brainKeyAccountCreateError( error )
            return Promise.reject( error );
        }
        let owner_private = WalletDb.generateNextKey();
        let active_private = WalletDb.generateNextKey();
        //let memo_private = WalletDb.generateNextKey()<commit_msg>Fix account create with faucet not rejecting errors properly<commit_after>import WalletDb from "stores/WalletDb";
import WalletUnlockActions from "actions/WalletUnlockActions";
import CachedPropertyActions from "actions/CachedPropertyActions";
import ApplicationApi from "api/ApplicationApi";
import {TransactionBuilder, FetchChain} from "bitsharesjs/es";
import {Apis} from "bitsharesjs-ws";
import alt from "alt-instance";
import SettingsStore from "stores/SettingsStore";

let application_api = new ApplicationApi();

class WalletActions {

    /** Restore and make active a new wallet_object. */
    restore(wallet_name = "default", wallet_object) {
        wallet_name = wallet_name.toLowerCase();
        return {wallet_name, wallet_object};
    }

    /** Make an existing wallet active or create a wallet (and make it active).
        If <b>wallet_name</b> does not exist, provide a <b>create_wallet_password</b>.
    */
    setWallet(wallet_name, create_wallet_password, brnkey) {
        WalletUnlockActions.lock();
        if( !wallet_name) wallet_name = "default";
        return (dispatch) => {
            return new Promise( resolve => {
                dispatch({wallet_name, create_wallet_password, brnkey, resolve});
            });
        };
    }

    setBackupDate() {
        CachedPropertyActions.set("backup_recommended", false);
        return true;
    }

    setBrainkeyBackupDate() {
        return true;
    }

    createAccountWithPassword( account_name, password, registrar, referrer, referrer_percent, refcode ) {
        let {privKey : owner_private} = WalletDb.generateKeyFromPassword(account_name, "owner", password);
        let {privKey: active_private} = WalletDb.generateKeyFromPassword(account_name, "active", password);
        //let memo_private = WalletDb.generateNextKey()

        return new Promise((resolve, reject) => {
            let create_account = () => {
                return application_api.create_account(
                    owner_private.private_key.toPublicKey().toPublicKeyString(),
                    active_private.private_key.toPublicKey().toPublicKeyString(),
                    account_name,
                    registrar, //registrar_id,
                    referrer, //referrer_id,
                    referrer_percent, //referrer_percent,
                    true //broadcast
                ).then(resolve).catch(reject);
            };

            if(registrar) {
                // using another user's account as registrar
                return create_account();
            } else {
                // using faucet

                let faucetAddress = SettingsStore.getSetting("faucet_address");
                if (window && window.location && window.location.protocol === "https:") {
                    faucetAddress = faucetAddress.replace(/http:\/\//, "https://");
                }

                let create_account_promise = fetch( faucetAddress + "/api/v1/accounts", {
                    method: "post",
                    mode: "cors",
                    headers: {
                        "Accept": "application/json",
                        "Content-type": "application/json"
                    },
                    body: JSON.stringify({
                        "account": {
                            "name": account_name,
                            "owner_key": owner_private.toPublicKey().toPublicKeyString(),
                            "active_key": active_private.toPublicKey().toPublicKeyString(),
                            "memo_key": active_private.toPublicKey().toPublicKeyString(),
                            //"memo_key": memo_private.private_key.toPublicKey().toPublicKeyString(),
                            "refcode": refcode,
                            "referrer": referrer
                        }
                    })
                }).then(r => r.json().then(res => {
                    if (!res || (res && res.error)) {
                        reject(res.error);
                    } else {
                        resolve(res);
                    }
                })).catch(reject);

                return create_account_promise.then(result => {
                    if (result && result.error) {
                        reject(result.error);
                    } else {
                        resolve(result);
                    }
                }).catch(error => {
                    reject(error);
                });
            }
        });

    }

    createAccount( account_name, registrar, referrer, referrer_percent, refcode ) {
        if( WalletDb.isLocked()) {
            let error = "wallet locked";
            //this.actions.brainKeyAccountCreateError( error )
            return Promise.reject( error );
        }
        let owner_private = WalletDb.generateNextKey();
        let active_private = WalletDb.generateNextKey();
        //let memo_private = WalletDb.generateNextKey()
{'fraction_non_alphanumeric': 0.10503762541806021, 'fraction_numerical': 0.00020903010033444816, 'mean_word_length': 1.7434059633027523, 'pattern_counts': {'":': 20, '<': 11, '<?xml version=': 0, '>': 26, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5303414', 'n_tokens_mistral': 2445, 'n_tokens_neox': 2380, 'n_words': 483}