// initialize a single wallet based on wallet storage strategy
initialize a single wallet based on wallet storage strategy
initialize a single wallet based on wallet storage strategy
fn create_wallet(&self, id: &str, config: &PostgresConfig, credentials: &PostgresCredentials, metadata: &[u8]) -> Result<(), WalletStorageError> {
        // create database for wallet
        // if admin user and password aren't provided then bail
        if credentials.admin_account == None || credentials.admin_password == None {
            return Ok(());
        }
        debug!("Entering create_wallet");
        debug!("setting up the admin_postgres_url");
        let url_base = PostgresStorageType::_admin_postgres_url(&config, &credentials);
        let url = PostgresStorageType::_postgres_url(id, &config, &credentials);
        debug!("connecting to postgres, url_base: {:?}", url_base);
        let conn = postgres::Connection::connect(&url_base[..], config.tls())?;
        debug!("creating wallets DB");
        let create_db_sql = str::replace(_CREATE_WALLET_DATABASE, "$1", id);
        // ignore errors at this step, in case the database has been pre-created by the DBA
        // if the create db fails, the user login/table creation will fail
        let _err = conn.execute(&create_db_sql, &[]);
        conn.finish()?;
        debug!("connecting to wallet as user");
        let conn = match postgres::Connection::connect(&url[..], config.tls()) {
            Ok(conn) => conn,
            Err(error) => {
                return Err(WalletStorageError::IOError(format!("Error occurred while connecting to wallet schema: {}", error)));
            }
        };
        // select metadata for this wallet to ensure it DOESN'T exist
        let mut schema_result = {
            let rows = conn.query(
                "SELECT value FROM metadata",
                &[]);
            match rows {
                Ok(rows_data) => {
                    match rows_data.iter().next() {
                        Some(_) => {
                            error!("Metadata was found for wallet id '{}' which indicates this wallet already exists.", id);
                            Err(WalletStorageError::AlreadyExists)
                        },
                        None => Ok(())
                    }
                },
                Err(_) => Ok(())
            }
        };
        debug!("setting up multi schema");
        for sql in &_CREATE_SCHEMA {
            match schema_result {
                Ok(_) => schema_result = match conn.execute(sql, &[]) {
                    Ok(_) => Ok(()),
                    Err(error) => {
                        Err(WalletStorageError::IOError(format!("Error occurred while creating wallet schema: {}", error)))
                    }
                },
                _ => ()
            }
        };
        debug!("inserting the keys");
        let ret = match schema_result {
            Ok(_) => {
                match conn.execute("INSERT INTO metadata(value) VALUES($1)
                                    ON CONFLICT (value) DO UPDATE SET value = excluded.value",
                                   &[&metadata]) {
                    Ok(_) => Ok(()),
                    Err(error) => {
                        //std::fs::remove_file(db_path)?;
                        Err(WalletStorageError::IOError(format!("Error occurred while inserting the keys: {}", error)))
                    }
                }
            }
            Err(error) => Err(error)
        };
        conn.finish()?;
        ret
    }
fn create_wallet ( & self , id : & str , config : & PostgresConfig , credentials : & PostgresCredentials , metadata : & [ u8 ] ) -> Result < ( ) , WalletStorageError > { // create database for wallet // if admin user and password aren't provided then bail if credentials . admin_account == None || credentials . admin_password == None { return Ok ( ( ) ) ; } debug ! ( " " ) ; debug ! ( " " ) ; let url_base = PostgresStorageType :: _admin_postgres_url ( & config , & credentials ) ; let url = PostgresStorageType :: _postgres_url ( id , & config , & credentials ) ; debug ! ( " " url_base ) ; let conn = postgres :: Connection :: connect ( & url_base [ .. ] , config . tls ( ) ) ? ; debug ! ( " " ) ; let create_db_sql = str :: replace ( _CREATE_WALLET_DATABASE , " " , id ) ; // ignore errors at this step, in case the database has been pre-created by the DBA // if the create db fails, the user login/table creation will fail let _err = conn . execute ( & create_db_sql , & [ ] ) ; conn . finish ( ) ? ; debug ! ( " " ) ; let conn = match postgres :: Connection :: connect ( & url [ .. ] , config . tls ( ) ) { Ok ( conn ) => conn , Err ( error ) => { return Err ( WalletStorageError :: IOError ( format ! ( " " error ) ) ) ; } } ; // select metadata for this wallet to ensure it DOESN'T exist let mut schema_result = { let rows = conn . query ( " " , & [ ] ) ; match rows { Ok ( rows_data ) => { match rows_data . iter ( ) . next ( ) { Some ( _ ) => { error ! ( " " id ) ; Err ( WalletStorageError :: AlreadyExists ) } , None => Ok ( ( ) ) } } , Err ( _ ) => Ok ( ( ) ) } } ; debug ! ( " " ) ; for sql in & _CREATE_SCHEMA { match schema_result { Ok ( _ ) => schema_result = match conn . execute ( sql , & [ ] ) { Ok ( _ ) => Ok ( ( ) ) , Err ( error ) => { Err ( WalletStorageError :: IOError ( format ! ( " " error ) ) ) } } , _ => ( ) } } ; debug ! ( " " ) ; let ret = match schema_result { Ok ( _ ) => { match conn . execute ( " " , & [ & metadata ] ) { Ok ( _ ) => Ok ( ( ) ) , Err ( error ) => { //std::fs::remove_file(db_path)?; Err ( WalletStorageError :: IOError ( format ! ( " " error ) ) ) } } } Err ( error ) => Err ( error ) } ; conn . finish ( ) ? ; ret }
initialize a single wallet based on wallet storage strategy
initialize a single wallet based on wallet storage strategy
&self
id
&str
config
&PostgresConfig
credentials
&PostgresCredentials
metadata
&[u8]