transfer function

master
Tom Alexander 5 years ago
parent 38c67e3c27
commit 121f3bb9cd

@ -17,7 +17,6 @@ use rustc_serialize::base64;
use rustc_serialize::base64::{FromBase64, ToBase64}; use rustc_serialize::base64::{FromBase64, ToBase64};
use std::convert::TryFrom; use std::convert::TryFrom;
use std::convert::TryInto; use std::convert::TryInto;
use std::error::Error;
use std::io; use std::io;
pub struct EncryptedValue { pub struct EncryptedValue {

@ -100,20 +100,34 @@ impl DbHandle {
} }
pub fn write_note(&mut self, master_key: [u8; 32], note: Note) { pub fn write_note(&mut self, master_key: [u8; 32], note: Note) {
let existing_notes = self.read_notes(master_key).unwrap();
let namespace_id = self.get_namespace_id(&note.namespace, master_key).unwrap(); let namespace_id = self.get_namespace_id(&note.namespace, master_key).unwrap();
let tx = self.conn.transaction().unwrap();
for existing_note in existing_notes {
if existing_note.namespace == note.namespace
&& existing_note.category == note.category
&& existing_note.title == note.title
{
tx.execute("DELETE FROM notes WHERE id=$1;", params![existing_note.id])
.unwrap();
}
}
let encrypted_title = crypt::encrypt_value(&note.title, master_key); let encrypted_title = crypt::encrypt_value(&note.title, master_key);
let encrypted_value = crypt::encrypt_value(&note.value, master_key); let encrypted_value = crypt::encrypt_value(&note.value, master_key);
self.conn tx.execute(
.execute( "INSERT INTO notes (namespace, category, title, value) VALUES ($1, $2, $3, $4);",
"INSERT INTO notes (namespace, category, title, value) VALUES ($1, $2, $3, $4);", params![
params![ namespace_id,
namespace_id, note.category,
note.category, encrypted_title,
encrypted_title, encrypted_value
encrypted_value ],
], )
) .unwrap();
.unwrap();
let _ = tx.commit().unwrap();
} }
pub fn read_notes(&mut self, master_key: [u8; 32]) -> rusqlite::Result<Vec<Note>> { pub fn read_notes(&mut self, master_key: [u8; 32]) -> rusqlite::Result<Vec<Note>> {

@ -143,12 +143,18 @@ fn set(mut db_conn: db::DbHandle, master_key: [u8; 32]) {
} }
fn transfer(mut db_conn: db::DbHandle, master_key: [u8; 32]) { fn transfer(mut db_conn: db::DbHandle, master_key: [u8; 32]) {
for host in db_conn for account in db_conn.list_accounts(master_key).into_iter() {
.list_accounts(master_key) let new_note = db::Note {
.into_iter() id: 0,
.map(|account: db::Account| account.host) namespace: "main".to_owned(),
{ category: "account".to_owned(),
println!("{}", host); title: account.host,
value: format!(
"username: {}\npassword:{}\n",
account.user, account.password
),
};
db_conn.write_note(master_key, new_note);
} }
} }

Loading…
Cancel
Save