Skip to content

Commit 1f99724

Browse files
committed
Start implementing hashing for private keys
1 parent ab1f434 commit 1f99724

File tree

9 files changed

+79
-13
lines changed

9 files changed

+79
-13
lines changed

Cargo.lock

+34
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ tracing-subscriber = "0.3.18"
3131
serde = { version = "1.0.209", features = ["derive"] }
3232
serde_json = "1.0.127"
3333
thiserror = "1.0.63"
34+
argon2 = "0.5.3"
3435

3536
[dependencies]
3637
kromer-economy-api = { path = "api" }

api/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ kromer-economy-entity = { path = "../entity" }
1414
kromer-economy-migration = { path = "../migration" }
1515
tracing.workspace = true
1616
tracing-subscriber.workspace = true
17-
thiserror.workspace = true
17+
thiserror.workspace = true
18+
argon2.workspace = true

api/src/routes/v1/names.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,11 @@ async fn register_name(
126126
let conn = &state.conn;
127127

128128
// TODO: Implement proper authentication and address get from private key
129-
let _private_key = &body.private_key;
130-
let owner_address = "TODO_GET_ADDRESS_FROM_PRIVATE_KEY"; // TODO: Get address from private key, should return address model.
129+
let private_key = &body.private_key;
130+
let owner = AddressController::get_from_private_key(conn, private_key)
131+
.await
132+
.map_err(KromerError::Database)?
133+
.ok_or_else(|| KromerError::Address(AddressError::AuthFailed))?;
131134

132135
let name_available = NameController::is_name_available(conn, &name)
133136
.await
@@ -137,15 +140,9 @@ async fn register_name(
137140
return Err(KromerError::Name(NameError::NameTaken(name)));
138141
}
139142

140-
let owner = AddressController::fetch_address(conn, owner_address, false)
141-
.await
142-
.map_err(KromerError::Database)?
143-
.ok_or_else(|| KromerError::Address(AddressError::NotFound(owner_address.to_string())))?;
144-
145-
// TODO: Check if the user has enough balance to register the name
146-
// if owner.balance < state.name_cost {
147-
// return Err(KromerError::Name(NameError::InsufficientFunds));
148-
// }
143+
if owner.balance < state.name_cost {
144+
return Err(KromerError::Name(NameError::InsufficientBalance));
145+
}
149146

150147
let registration = NameRegistration { name, owner };
151148

entity/src/addresses.rs

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub struct Model {
1919
pub private_key: Option<String>,
2020
pub alert: Option<String>,
2121
pub locked: bool,
22+
pub salt: Option<String>,
2223
}
2324

2425
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]

entity/src/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.1
2+
3+
pub mod prelude;
4+
5+
pub mod addresses;
6+
pub mod names;
7+
pub mod transactions;

migration/src/m20220101_000001_create_table.rs

+2
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ impl MigrationTrait for Migration {
179179
.null()
180180
.take(),
181181
)
182+
.col(ColumnDef::new(Addresses::Salt).char_len(16).null())
182183
.col(
183184
ColumnDef::new(Addresses::Alert)
184185
.string_len(1024)
@@ -280,6 +281,7 @@ enum Addresses {
280281
TotalOut,
281282
FirstSeen,
282283
PrivateKey,
284+
Salt,
283285
Alert,
284286
Locked,
285287
}

service/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ edition = "2021"
55

66
[dependencies]
77
sea-orm.workspace = true
8-
kromer-economy-entity = { path = "../entity" }
8+
kromer-economy-entity = { path = "../entity" }
9+
argon2.workspace = true

service/src/controller/address.rs

+22
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,26 @@ impl AddressController {
151151
.all(conn)
152152
.await
153153
}
154+
155+
/// Fetches the address from the database when given a private key
156+
///
157+
/// # Arguments
158+
/// * `conn` - The database connection
159+
/// * `private_key` - The private key to fetch the address for
160+
///
161+
/// # Examples
162+
/// ```
163+
/// let address = AddressController::get_from_private_key(&db, "pasetorules!").await?;
164+
/// ```
165+
pub async fn get_from_private_key(
166+
conn: &DbConn,
167+
private_key: &str,
168+
) -> Result<Option<addresses::Model>, DbErr> {
169+
// Address::find()
170+
// .filter(addresses::Column::PrivateKey.eq(private_key))
171+
// .one(conn)
172+
// .await
173+
174+
todo!()
175+
}
154176
}

0 commit comments

Comments
 (0)