Skip to content

Commit

Permalink
Initial lift
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Jan 3, 2025
1 parent c37183b commit 5e43f46
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 24 deletions.
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
21 changes: 14 additions & 7 deletions crates/sage-wallet/src/wallet/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::collections::HashMap;

use chia::{
bls::{
master_to_wallet_unhardened_intermediate, sign, DerivableKey, PublicKey, SecretKey,
Signature,
master_to_wallet_hardened_intermediate, master_to_wallet_unhardened_intermediate, sign,
DerivableKey, PublicKey, SecretKey, Signature,
},
protocol::{CoinSpend, SpendBundle},
puzzles::DeriveSynthetic,
Expand Down Expand Up @@ -73,21 +73,28 @@ impl Wallet {
return Err(WalletError::SecpNotSupported);
};
let pk = required.public_key;
let Some(index) = self.db.synthetic_key_index(pk).await? else {
let Some(info) = self.db.synthetic_key_info(pk).await? else {
if partial {
continue;
}
return Err(WalletError::UnknownPublicKey);
};
indices.insert(pk, index);
indices.insert(pk, info);
}

let intermediate_sk = master_to_wallet_unhardened_intermediate(&master_sk);
let unhardened_intermediate_sk = master_to_wallet_unhardened_intermediate(&master_sk);
let hardened_intermediate_sk = master_to_wallet_hardened_intermediate(&master_sk);

let secret_keys: HashMap<PublicKey, SecretKey> = indices
.iter()
.map(|(pk, index)| {
let sk = intermediate_sk.derive_unhardened(*index).derive_synthetic();
.map(|(pk, info)| {
let sk = if info.hardened {
hardened_intermediate_sk.derive_hardened(info.index)
} else {
unhardened_intermediate_sk.derive_unhardened(info.index)
}
.derive_synthetic();

(*pk, sk)
})
.collect();
Expand Down
2 changes: 1 addition & 1 deletion crates/sage/src/endpoints/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl Sage {

let derivations = wallet
.db
.unhardened_derivations(req.limit, req.offset)
.derivations(false, req.limit, req.offset)
.await?
.into_iter()
.map(|row| {
Expand Down
11 changes: 8 additions & 3 deletions crates/sage/src/endpoints/wallet_connect.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use chia::{
bls::{master_to_wallet_unhardened, sign},
bls::{master_to_wallet_hardened, master_to_wallet_unhardened, sign},
clvm_utils::ToTreeHash,
protocol::{Bytes, Coin, CoinSpend, SpendBundle},
puzzles::{cat::CatArgs, standard::StandardArgs, DeriveSynthetic, Proof},
Expand Down Expand Up @@ -330,7 +330,7 @@ impl Sage {
let wallet = self.wallet()?;

let public_key = parse_public_key(req.public_key)?;
let Some(index) = wallet.db.synthetic_key_index(public_key).await? else {
let Some(info) = wallet.db.synthetic_key_info(public_key).await? else {
return Err(Error::InvalidKey);
};

Expand All @@ -340,7 +340,12 @@ impl Sage {
return Err(Error::NoSigningKey);
};

let secret_key = master_to_wallet_unhardened(&master_sk, index).derive_synthetic();
let secret_key = if info.hardened {
master_to_wallet_hardened(&master_sk, info.index)
} else {
master_to_wallet_unhardened(&master_sk, info.index)
}
.derive_synthetic();

let decoded_message = Bytes::from(hex::decode(&req.message)?);
let signature = sign(
Expand Down

0 comments on commit 5e43f46

Please sign in to comment.