Skip to content

Commit

Permalink
feat: generate deterministic secrets (#249)
Browse files Browse the repository at this point in the history
* feat: generate deterministic secrets

* fix: wasm build

* chore: cleanup

* feat: convert hex keyset-id to int

* chore: cleanup

* chore: store keysets in db (WIP)

* chore: use upsert for adding/updating keysets

* feat: store seed in db

* chore: use deterministic secrets for mint and split

* fix: tests
  • Loading branch information
ngutech21 authored Mar 29, 2024
1 parent a80fbfb commit 5a1b5e5
Show file tree
Hide file tree
Showing 13 changed files with 718 additions and 69 deletions.
171 changes: 169 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions moksha-core/src/dhke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ impl Dhke {
.ok_or(MokshaCoreError::NoValidPointFound)
}

// FIXME: use SecretKey instead of &[u8] for blinding factor
pub fn step1_alice(
&self,
secret_msg: impl Into<String>,
Expand Down
10 changes: 10 additions & 0 deletions moksha-core/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ pub enum CurrencyUnit {
Usd,
}

impl From<String> for CurrencyUnit {
fn from(unit: String) -> Self {
match unit.to_lowercase().as_str() {
"sat" => Self::Sat,
"usd" => Self::Usd,
_ => panic!("Unknown currency unit: {}", unit),
}
}
}

impl Display for CurrencyUnit {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expand Down
4 changes: 4 additions & 0 deletions moksha-wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ async-trait = "0.1.77"
lightning-invoice = "0.29.0"
url = "2.5.0"
dirs = "5.0.1"
bip32 = { version = "0.5.1", features = ["secp256k1", "std"] }
bip39 = "2.0.0"
hex = "0.4.3"
rand = "0.8.5"

[target.'cfg(target_family = "wasm")'.dependencies]
gloo-net = { version = "0.5.0" }
Expand Down
8 changes: 6 additions & 2 deletions moksha-wallet/migrations/20230530061910_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ CREATE TABLE IF NOT EXISTS proofs (
);

CREATE TABLE IF NOT EXISTS keysets (
id TEXT NOT NULL,
id INTEGER PRIMARY KEY AUTOINCREMENT,
mint_url TEXT NOT NULL,
keyset_id TEXT NOT NULL,
currency_unit TEXT NOT NULL,
active BOOL DEFAULT TRUE,
UNIQUE (id, mint_url)
last_index INTEGER,
public_keys JSON CHECK (json_valid(public_keys)),
UNIQUE (keyset_id, mint_url)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- Add migration script here
CREATE TABLE seed (
id INTEGER PRIMARY KEY CHECK (id = 1),
seed_words TEXT NOT NULL
-- other columns
);
16 changes: 16 additions & 0 deletions moksha-wallet/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub enum MokshaWalletError {
#[cfg(not(target_arch = "wasm32"))]
#[error("Sqlite Error {0}")]
Sqlite(#[from] sqlx::sqlite::SqliteError),

#[error("Utf8 Error {0}")]
Utf8(#[from] FromUtf8Error),

Expand All @@ -62,4 +63,19 @@ pub enum MokshaWalletError {

#[error("Unsupported version: Only mints with /v1 api are supported")]
UnsupportedApiVersion,

#[error("Bip32Error {0}")]
Bip32(#[from] bip32::Error),

#[error("Bip39Error {0}")]
Bip39(#[from] bip39::Error),

#[error("Secp256k1 {0}")]
Secp256k1(#[from] secp256k1::Error),

#[error("Primarykey not set for keyset")]
IdNotSet,

#[error("Found multiple seeds in the database. This is not supported.")]
MultipleSeeds,
}
1 change: 1 addition & 0 deletions moksha-wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ pub mod config_path;
pub mod error;
pub mod http;
pub mod localstore;
pub mod secret;
pub mod wallet;
Loading

0 comments on commit 5a1b5e5

Please sign in to comment.