store active accound in db
This commit is contained in:
parent
43c348408b
commit
8dcd064b3e
|
@ -152,8 +152,6 @@ struct CResult_u8 migrate_data_db(uint8_t coin);
|
||||||
|
|
||||||
void set_active(uint8_t active);
|
void set_active(uint8_t active);
|
||||||
|
|
||||||
void set_active_account(uint8_t coin, uint32_t id);
|
|
||||||
|
|
||||||
void set_coin_lwd_url(uint8_t coin, char *lwd_url);
|
void set_coin_lwd_url(uint8_t coin, char *lwd_url);
|
||||||
|
|
||||||
char *get_lwd_url(uint8_t coin);
|
char *get_lwd_url(uint8_t coin);
|
||||||
|
@ -281,7 +279,9 @@ struct CResult_____c_char derive_zip32(uint8_t coin,
|
||||||
|
|
||||||
struct CResult______u8 get_account_list(uint8_t coin);
|
struct CResult______u8 get_account_list(uint8_t coin);
|
||||||
|
|
||||||
struct CResult_u32 get_available_account_id(uint8_t coin, uint32_t id);
|
struct CResult_u32 get_active_account(uint8_t coin);
|
||||||
|
|
||||||
|
struct CResult_u8 set_active_account(uint8_t coin, uint32_t id);
|
||||||
|
|
||||||
struct CResult_____c_char get_t_addr(uint8_t coin, uint32_t id);
|
struct CResult_____c_char get_t_addr(uint8_t coin, uint32_t id);
|
||||||
|
|
||||||
|
|
|
@ -154,11 +154,6 @@ pub unsafe extern "C" fn set_active(active: u8) {
|
||||||
crate::coinconfig::set_active(active);
|
crate::coinconfig::set_active(active);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
|
||||||
pub unsafe extern "C" fn set_active_account(coin: u8, id: u32) {
|
|
||||||
crate::coinconfig::set_active_account(coin, id);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe extern "C" fn set_coin_lwd_url(coin: u8, lwd_url: *mut c_char) {
|
pub unsafe extern "C" fn set_coin_lwd_url(coin: u8, lwd_url: *mut c_char) {
|
||||||
from_c_str!(lwd_url);
|
from_c_str!(lwd_url);
|
||||||
|
@ -805,10 +800,7 @@ pub unsafe extern "C" fn derive_zip32(
|
||||||
to_cresult_str(res())
|
to_cresult_str(res())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn with_account<T, F: Fn(&Connection) -> anyhow::Result<T>>(
|
fn with_account<T, F: Fn(&Connection) -> anyhow::Result<T>>(coin: u8, f: F) -> anyhow::Result<T> {
|
||||||
coin: u8,
|
|
||||||
f: F,
|
|
||||||
) -> anyhow::Result<T> {
|
|
||||||
let c = CoinConfig::get(coin);
|
let c = CoinConfig::get(coin);
|
||||||
let db = c.db()?;
|
let db = c.db()?;
|
||||||
let connection = &db.connection;
|
let connection = &db.connection;
|
||||||
|
@ -825,14 +817,24 @@ pub unsafe extern "C" fn get_account_list(coin: u8) -> CResult<*const u8> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe extern "C" fn get_available_account_id(coin: u8, id: u32) -> CResult<u32> {
|
pub unsafe extern "C" fn get_active_account(coin: u8) -> CResult<u32> {
|
||||||
let res = |connection: &Connection| {
|
let res = |connection: &Connection| {
|
||||||
let new_id = crate::db::read::get_available_account_id(connection, id)?;
|
let new_id = crate::db::read::get_active_account(connection)?;
|
||||||
Ok(new_id)
|
Ok(new_id)
|
||||||
};
|
};
|
||||||
to_cresult(with_account(coin, res))
|
to_cresult(with_account(coin, res))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub unsafe extern "C" fn set_active_account(coin: u8, id: u32) -> CResult<u8> {
|
||||||
|
let res = |connection: &Connection| {
|
||||||
|
crate::coinconfig::set_active_account(coin, id);
|
||||||
|
crate::db::read::set_active_account(connection, id)?;
|
||||||
|
Ok(0)
|
||||||
|
};
|
||||||
|
to_cresult(with_account(coin, res))
|
||||||
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe extern "C" fn get_t_addr(coin: u8, id: u32) -> CResult<*mut c_char> {
|
pub unsafe extern "C" fn get_t_addr(coin: u8, id: u32) -> CResult<*mut c_char> {
|
||||||
let res = |connection: &Connection| {
|
let res = |connection: &Connection| {
|
||||||
|
|
|
@ -34,7 +34,7 @@ pub fn reset_db(connection: &Connection) -> anyhow::Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
const LATEST_VERSION: u32 = 6;
|
const LATEST_VERSION: u32 = 7;
|
||||||
|
|
||||||
pub fn init_db(connection: &Connection, network: &Network, has_ua: bool) -> anyhow::Result<()> {
|
pub fn init_db(connection: &Connection, network: &Network, has_ua: bool) -> anyhow::Result<()> {
|
||||||
connection.execute(
|
connection.execute(
|
||||||
|
@ -284,6 +284,15 @@ pub fn init_db(connection: &Connection, network: &Network, has_ua: bool) -> anyh
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if version < 7 {
|
||||||
|
connection.execute(
|
||||||
|
"CREATE TABLE IF NOT EXISTS properties (
|
||||||
|
name TEXT PRIMARY KEY,
|
||||||
|
value TEXT NOT NULL)",
|
||||||
|
[],
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
|
||||||
if version != LATEST_VERSION {
|
if version != LATEST_VERSION {
|
||||||
update_schema_version(connection, LATEST_VERSION)?;
|
update_schema_version(connection, LATEST_VERSION)?;
|
||||||
connection.cache_flush()?;
|
connection.cache_flush()?;
|
||||||
|
|
|
@ -38,6 +38,33 @@ pub fn get_account_list(connection: &Connection) -> Result<Vec<u8>> {
|
||||||
Ok(data)
|
Ok(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_active_account(connection: &Connection) -> Result<u32> {
|
||||||
|
let id = connection
|
||||||
|
.query_row(
|
||||||
|
"SELECT value FROM properties WHERE name = 'account'",
|
||||||
|
[],
|
||||||
|
|row| {
|
||||||
|
let value: String = row.get(0)?;
|
||||||
|
let value: u32 = value.parse().unwrap();
|
||||||
|
Ok(value)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.optional()?
|
||||||
|
.unwrap_or(0);
|
||||||
|
let id = get_available_account_id(connection, id)?;
|
||||||
|
set_active_account(connection, id)?;
|
||||||
|
Ok(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_active_account(connection: &Connection, id: u32) -> Result<()> {
|
||||||
|
connection.execute(
|
||||||
|
"INSERT INTO properties(name, value) VALUES ('account',?1) \
|
||||||
|
ON CONFLICT (name) DO UPDATE SET value = excluded.value",
|
||||||
|
[id],
|
||||||
|
)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_available_account_id(connection: &Connection, id: u32) -> Result<u32> {
|
pub fn get_available_account_id(connection: &Connection, id: u32) -> Result<u32> {
|
||||||
let r = connection
|
let r = connection
|
||||||
.query_row("SELECT 1 FROM accounts WHERE id_account = ?1", [id], |_| {
|
.query_row("SELECT 1 FROM accounts WHERE id_account = ?1", [id], |_| {
|
||||||
|
@ -57,15 +84,17 @@ pub fn get_available_account_id(connection: &Connection, id: u32) -> Result<u32>
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_t_addr(connection: &Connection, id: u32) -> Result<String> {
|
pub fn get_t_addr(connection: &Connection, id: u32) -> Result<String> {
|
||||||
let address = connection.query_row(
|
let address = connection
|
||||||
"SELECT address FROM taddrs WHERE account = ?1",
|
.query_row(
|
||||||
[id],
|
"SELECT address FROM taddrs WHERE account = ?1",
|
||||||
|row| {
|
[id],
|
||||||
let address: String = row.get(0)?;
|
|row| {
|
||||||
Ok(address)
|
let address: String = row.get(0)?;
|
||||||
},
|
Ok(address)
|
||||||
)?;
|
},
|
||||||
Ok(address)
|
)
|
||||||
|
.optional()?;
|
||||||
|
Ok(address.unwrap_or(String::new()))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_sk(connection: &Connection, id: u32) -> Result<String> {
|
pub fn get_sk(connection: &Connection, id: u32) -> Result<String> {
|
||||||
|
|
Loading…
Reference in New Issue