Skip to content

Commit

Permalink
merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
Evanfeenstra committed Sep 12, 2023
2 parents f6cfb84 + 8d02cde commit 3289c70
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
21 changes: 20 additions & 1 deletion signer/src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,42 @@ pub fn entropy_from_mnemonic(mn: &str) -> anyhow::Result<Vec<u8>> {
Ok(e)
}

pub fn mnemonic_to_seed(mn: &str) -> anyhow::Result<Vec<u8>> {
let mn = bip39::Mnemonic::parse_normalized(mn)
.map_err(|e| anyhow::anyhow!("Mnemonic::parse_normalized failed {:?}", e))?;
// Do like CLN does, chop off the last 32 bytes
let e = mn.to_seed_normalized("")[..32].to_vec();
Ok(e)
}

#[cfg(test)]
mod tests {
use crate::derive::*;

fn entropy() -> [u8; 32] {
[1; 32]
}

fn seed() -> [u8; 32] {
[1; 32]
}

#[test]
fn test_mnemonic() {
let entropy = seed();
let entropy = entropy();
let mn = mnemonic_from_entropy(&entropy).expect("nope");
assert_eq!(mn, "absurd amount doctor acoustic avoid letter advice cage absurd amount doctor acoustic avoid letter advice cage absurd amount doctor acoustic avoid letter advice comic");
let en = entropy_from_mnemonic(&mn).expect("fail");
assert_eq!(&en[..], &entropy);
}

#[test]
fn test_mnemonic_to_seed() {
let seed = mnemonic_to_seed("forget parent wage payment cotton excite venue into era crouch because twin bargain flash library fever raise chunk suit evil jar perfect almost supreme").expect("fail");
let vector = [0xdf, 0x58, 0x5d, 0x7e, 0xdb, 0xf9, 0x86, 0x3e, 0x42, 0xef, 0xc1, 0xef, 0x00, 0xb1, 0xd1, 0x0d, 0x9c, 0x6b, 0xb7, 0xb3, 0xff, 0xea, 0x27, 0x2a, 0x48, 0x43, 0x0e, 0x8a, 0x3e, 0x4b, 0x60, 0x0b];
assert_eq!(seed, vector);
}

#[test]
fn test_derive() {
use vls_protocol_signer::lightning_signer::bitcoin::Network;
Expand Down
15 changes: 10 additions & 5 deletions sphinx-ffi/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

- net: "regtest", "signet", "testnet", or "bitcoin"
- seed: 32-byte hex
- return Keys{secret: String, pubkey: String}
- returns Keys{secret: String, pubkey: String}

**`mnemonic_from_entropy(seed: String)`**

Expand All @@ -38,13 +38,18 @@
**`entropy_from_mnemonic(mnemonic: String)`**

- mnemonic: 24 words separated by spaces
- return a 32-byte hex seed
- returns 32-byte hex entropy

**`mnemonic_to_seed(mnemonic: String)`**

- mnemonic: 24 words separated by spaces
- returns 32-byte hex seed

**`make_auth_token(now: number, secret: String)`**

- now: 10-digit UNIX timestamp
- secret: 32-byte hex
- return auth_token string
- returns auth_token string

### control messages

Expand All @@ -53,12 +58,12 @@
- json: JSON string of the [ControlMessage](https://github.com/stakwork/sphinx-rs/blob/master/glyph/src/types.rs#L7) enum. An object (dictionary) with one key, which is the enum name. The value is the value inside the enum, or `null`. Example: `{Nonce:null}` or `{UpdatePolicy:{msat_per_interval:0,interval:"daily",htlc_limit_msat:0}}`
- secret: the secret key returned from `node_keys`
- nonce: A number that you need to persist. For every request it needs to be greater than the last request.
- return the bytes (hex string) to send to the signer
- returns the bytes (hex string) to send to the signer

**`parse_response(res: String)`**

- res: a hex string returned from the signer after sending the `build_request`.
- return a JSON string so you can easily see what's inside
- returns a JSON string so you can easily see what's inside

### signer

Expand Down
15 changes: 15 additions & 0 deletions sphinx-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ pub fn entropy_from_mnemonic(mnemonic: String) -> Result<String> {
Ok(hex::encode(m))
}

pub fn mnemonic_to_seed(mnemonic: String) -> Result<String> {
let m = derive::mnemonic_to_seed(&mnemonic).map_err(|e| SphinxError::BadSecret {
r: format!("{:?}", e),
})?;
Ok(hex::encode(m))
}

#[cfg(test)]
mod tests {
use crate::*;
Expand Down Expand Up @@ -192,4 +199,12 @@ mod tests {
);
Ok(())
}

#[test]
fn test_mnemonic_to_seed() -> Result<()> {
let seed = mnemonic_to_seed("forget parent wage payment cotton excite venue into era crouch because twin bargain flash library fever raise chunk suit evil jar perfect almost supreme".to_string()).expect("fail");
let vector = "df585d7edbf9863e42efc1ef00b1d10d9c6bb7b3ffea272a48430e8a3e4b600b";
assert_eq!(seed, vector);
Ok(())
}
}
5 changes: 5 additions & 0 deletions sphinx-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ pub fn entropy_from_mnemonic(mnemonic: &str) -> Result<String> {
Ok(cy::entropy_from_mnemonic(mnemonic.to_string())?)
}

#[wasm_bindgen]
pub fn mnemonic_to_seed(mnemonic: &str) -> Result<String> {
Ok(cy::mnemonic_to_seed(mnemonic.to_string())?)
}

#[wasm_bindgen]
pub fn make_auth_token(ts: u32, secret: &str) -> Result<String> {
Ok(cy::make_auth_token(ts, secret.to_string())?)
Expand Down

0 comments on commit 3289c70

Please sign in to comment.