<commit_before>import WalletDb from "stores/WalletDb"
import WalletUnlockActions from "actions/WalletUnlockActions"
import ApplicationApi from "../rpc_api/ApplicationApi"
import PrivateKey from "../ecc/key_private"
import Apis from "../rpc_api/ApiInstances"
import ops from "../chain/signed_transaction"
import chain_types from "../chain/chain_types"
import lookup from "chain/lookup"
import PublicKey from "ecc/key_public"
import Address from "ecc/address"
import alt from "alt-instance"
import iDB from "idb-instance"
import Immutable from "immutable"

var application_api = new ApplicationApi()
var api = Apis.instance()
//var fetch = require('node-fetch')

class WalletActions {

    /** Restore and make active a new wallet_object. */
    restore(wallet_name = "default", wallet_object) {
        wallet_name = wallet_name.toLowerCase()
        this.dispatch({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) {
        if( ! wallet_name) wallet_name = "default"
        return new Promise( resolve => {
            this.dispatch({wallet_name, create_wallet_password, resolve})
        })
    }
    
    createAccount(
        account_name,
        registrar,
        referrer,
        referrer_percent = 100
    ) {
        if( WalletDb.isLocked()) {
            var error = "wallet locked"
            //this.actions.brainKeyAccountCreateError( error )
            return Promise.reject( error )
        }
        
        var [owner_private, active_private] = WalletDb.generateKeys();

        var updateWallet = ()=> {
            var transaction = WalletDb.transaction_update_keys()
            var p = WalletDb.saveKeys(
                [ owner_private, active_private ],
                transaction
            )
            var p2 = WalletDb.incrementBrainKeySequence(transaction)
            return Promise.all([p,p2]).catch( error => transaction.abort() )
        };

        let create_account_with_brain_key = () => {
            return application_api.create_account_with_brain_key(
                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( () => updateWallet() )
            // {
            //     return updateWallet().then(()=> {
            //         //this.actions.brainKeyAccountCreated())
            //     }).catch(  error => {
            //         //this.actions.brainKeyAccountCreateError(error);
            //         throw error;
            //     });
            // })
        };

        if(registrar) {
            // using another user's account as registrar
            return create_account_with_brain_key();
        } else {
            // using faucet
            let hostname = "localhost", protocol;
            try {
                hostname = window.location.hostname;
                protocol = window.location.protocol === "https:" ? "https://" : "http://";
            } catch(e) {}
            let port = (hostname === "localhost" || hostname.indexOf("192.168.") === 0) ? ":3000" : "";
            let create_account_promise = fetch(protocol + hostname + port + "/api/v1/accounts", {
                method: 'post',
                mode: 'cors',
                headers: {
                    "Accept": "application/json",
                    "Content-type": "application/json"
                },
                body: JSON.stringify({<commit_msg>hard coding graphene.bitshares.org as default facuet<commit_after>import WalletDb from "stores/WalletDb"
import WalletUnlockActions from "actions/WalletUnlockActions"
import ApplicationApi from "../rpc_api/ApplicationApi"
import PrivateKey from "../ecc/key_private"
import Apis from "../rpc_api/ApiInstances"
import ops from "../chain/signed_transaction"
import chain_types from "../chain/chain_types"
import lookup from "chain/lookup"
import PublicKey from "ecc/key_public"
import Address from "ecc/address"
import alt from "alt-instance"
import iDB from "idb-instance"
import Immutable from "immutable"

var application_api = new ApplicationApi()
var api = Apis.instance()
//var fetch = require('node-fetch')

class WalletActions {

    /** Restore and make active a new wallet_object. */
    restore(wallet_name = "default", wallet_object) {
        wallet_name = wallet_name.toLowerCase()
        this.dispatch({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) {
        if( ! wallet_name) wallet_name = "default"
        return new Promise( resolve => {
            this.dispatch({wallet_name, create_wallet_password, resolve})
        })
    }
    
    createAccount(
        account_name,
        registrar,
        referrer,
        referrer_percent = 100
    ) {
        if( WalletDb.isLocked()) {
            var error = "wallet locked"
            //this.actions.brainKeyAccountCreateError( error )
            return Promise.reject( error )
        }
        
        var [owner_private, active_private] = WalletDb.generateKeys();

        var updateWallet = ()=> {
            var transaction = WalletDb.transaction_update_keys()
            var p = WalletDb.saveKeys(
                [ owner_private, active_private ],
                transaction
            )
            var p2 = WalletDb.incrementBrainKeySequence(transaction)
            return Promise.all([p,p2]).catch( error => transaction.abort() )
        };

        let create_account_with_brain_key = () => {
            return application_api.create_account_with_brain_key(
                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( () => updateWallet() )
            // {
            //     return updateWallet().then(()=> {
            //         //this.actions.brainKeyAccountCreated())
            //     }).catch(  error => {
            //         //this.actions.brainKeyAccountCreateError(error);
            //         throw error;
            //     });
            // })
        };

        if(registrar) {
            // using another user's account as registrar
            return create_account_with_brain_key();
        } else {
            // using faucet
          /*
            let hostname = "localhost", protocol;
            try {
                hostname = window.location.hostname;
                protocol = window.location.protocol === "https:" ? "https://" : "http://";
            } catch(e) {}
            let port = (hostname === "localhost" || hostname.indexOf("192.168.") === 0) ? ":3000" : "";
            */
            let create_account_promise = fetch("https://graphene.bitshares.org/api/v1/accounts", {
                method: 'post',
                mode: 'cors',
                headers: {
                    "Accept": "application/json",
                    "Content-type": "application/json"
                },
                body: JSON.stringify({
{'fraction_non_alphanumeric': 0.11378329454357383, 'fraction_numerical': 0.004396172743728989, 'mean_word_length': 1.9299242424242424, 'pattern_counts': {'":': 6, '<': 11, '<?xml version=': 0, '>': 25, 'https://': 3, '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': '2862776', 'n_tokens_mistral': 2099, 'n_tokens_neox': 2078, 'n_words': 451}