Skip to content

Commit

Permalink
Merge pull request #227 from xch-dev/hardened-keys
Browse files Browse the repository at this point in the history
Hardened keys
  • Loading branch information
Rigidity authored Jan 3, 2025
2 parents c37183b + e39ec6f commit 8100139
Show file tree
Hide file tree
Showing 30 changed files with 747 additions and 263 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

40 changes: 0 additions & 40 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ itertools = "0.13.0"
anyhow = "1.0.86"
thiserror = "1.0.63"
hex-literal = "0.4.1"
rayon = "1.10.0"
once_cell = "1.19.0"
num-traits = "0.2.19"
paste = "1.0.15"
Expand Down
9 changes: 9 additions & 0 deletions crates/sage-api/src/requests/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,12 @@ pub struct UpdateNft {

#[derive(Debug, Clone, Copy, Serialize, Deserialize, Type)]
pub struct UpdateNftResponse {}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, Type)]
pub struct IncreaseDerivationIndex {
pub hardened: bool,
pub index: u32,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, Type)]
pub struct IncreaseDerivationIndexResponse {}
2 changes: 2 additions & 0 deletions crates/sage-api/src/requests/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use crate::{

#[derive(Debug, Clone, Copy, Serialize, Deserialize, Type)]
pub struct GetDerivations {
#[serde(default)]
pub hardened: bool,
pub offset: u32,
pub limit: u32,
}
Expand Down
6 changes: 6 additions & 0 deletions crates/sage-api/src/requests/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ pub struct Resync {
pub fingerprint: u32,
#[serde(default)]
pub delete_offer_files: bool,
#[serde(default)]
pub delete_unhardened_derivations: bool,
#[serde(default)]
pub delete_hardened_derivations: bool,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, Type)]
Expand All @@ -41,6 +45,8 @@ pub struct GenerateMnemonicResponse {
pub struct ImportKey {
pub name: String,
pub key: String,
#[serde(default)]
pub derivation_index: u32,
#[serde(default = "yes")]
pub save_secrets: bool,
#[serde(default = "yes")]
Expand Down
1 change: 1 addition & 0 deletions crates/sage-cli/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ routes!(
update_cat await: UpdateCat = "/update_cat",
update_did await: UpdateDid = "/update_did",
update_nft await: UpdateNft = "/update_nft",
increase_derivation_index await: IncreaseDerivationIndex = "/increase_derivation_index",
);

async fn start_rpc(path: PathBuf) -> Result<()> {
Expand Down
43 changes: 30 additions & 13 deletions crates/sage-database/src/derivations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@ use crate::{
into_row, to_bytes, to_bytes32, Database, DatabaseTx, DerivationRow, DerivationSql, Result,
};

#[derive(Debug, Clone, Copy)]
pub struct SyntheticKeyInfo {
pub index: u32,
pub hardened: bool,
}

impl Database {
pub async fn unhardened_derivations(
pub async fn derivations(
&self,
hardened: bool,
limit: u32,
offset: u32,
) -> Result<Vec<DerivationRow>> {
unhardened_derivations(&self.pool, limit, offset).await
derivations(&self.pool, hardened, limit, offset).await
}

pub async fn p2_puzzle_hashes(&self) -> Result<Vec<Bytes32>> {
Expand All @@ -22,8 +29,11 @@ impl Database {
synthetic_key(&self.pool, p2_puzzle_hash).await
}

pub async fn synthetic_key_index(&self, synthetic_key: PublicKey) -> Result<Option<u32>> {
synthetic_key_index(&self.pool, synthetic_key).await
pub async fn synthetic_key_info(
&self,
synthetic_key: PublicKey,
) -> Result<Option<SyntheticKeyInfo>> {
synthetic_key_info(&self.pool, synthetic_key).await
}

pub async fn is_p2_puzzle_hash(&self, p2_puzzle_hash: Bytes32) -> Result<bool> {
Expand Down Expand Up @@ -148,19 +158,21 @@ async fn p2_puzzle_hashes(conn: impl SqliteExecutor<'_>) -> Result<Vec<Bytes32>>
.collect::<Result<_>>()
}

async fn unhardened_derivations(
async fn derivations(
conn: impl SqliteExecutor<'_>,
hardened: bool,
limit: u32,
offset: u32,
) -> Result<Vec<DerivationRow>> {
sqlx::query_as!(
DerivationSql,
"
SELECT * FROM `derivations`
WHERE `hardened` = 0
WHERE `hardened` = ?
ORDER BY `index` ASC
LIMIT ? OFFSET ?
",
hardened,
limit,
offset
)
Expand Down Expand Up @@ -190,25 +202,30 @@ async fn synthetic_key(
Ok(PublicKey::from_bytes(&to_bytes(bytes)?)?)
}

async fn synthetic_key_index(
async fn synthetic_key_info(
conn: impl SqliteExecutor<'_>,
synthetic_key: PublicKey,
) -> Result<Option<u32>> {
) -> Result<Option<SyntheticKeyInfo>> {
let synthetic_key = synthetic_key.to_bytes();
let synthetic_key_ref = synthetic_key.as_ref();
Ok(sqlx::query!(

sqlx::query!(
"
SELECT `index`
SELECT `index`, `hardened`
FROM `derivations`
WHERE `synthetic_key` = ?
AND `hardened` = 0
",
synthetic_key_ref
)
.fetch_optional(conn)
.await?
.map(|row| row.index.try_into())
.transpose()?)
.map(|row| {
Ok(SyntheticKeyInfo {
index: row.index.try_into()?,
hardened: row.hardened,
})
})
.transpose()
}

async fn p2_puzzle_hash(
Expand Down
1 change: 0 additions & 1 deletion crates/sage-wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ tokio = { workspace = true, features = ["time"] }
itertools = { workspace = true }
futures-util = { workspace = true }
futures-lite = { workspace = true }
rayon = { workspace = true }
reqwest = { workspace = true, default-features = false, features = ["http2", "rustls-tls-webpki-roots", "json"] }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
Expand Down
Loading

0 comments on commit 8100139

Please sign in to comment.