Skip to content

Commit

Permalink
updated sphinx bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
Evanfeenstra committed Dec 18, 2023
1 parent a4f317c commit f18f6e4
Show file tree
Hide file tree
Showing 9 changed files with 1,436 additions and 130 deletions.
18 changes: 12 additions & 6 deletions sphinx-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@ lightning = { git = "https://github.com/Evanfeenstra/rust-lightning", rev = "3f5

[dependencies]
sphinx-crypter = { path = "../crypter" }
sphinx-signer = { path = "../signer", default-features = false, features = ["persist", "no-native"] }
sphinx-signer = { path = "../signer", default-features = false, features = [
"persist",
"no-native",
] }
sphinx-glyph = { path = "../glyph", default-features = false }
sphinx = { git = "https://github.com/stakwork/sphinx", rev = "ee62c9305006a7e33eccd379a5749beff25e5eff", features = ["msg"] }
sphinx = { git = "https://github.com/stakwork/sphinx", rev = "a68d83b7a697403b52e9d26cfc2260627185942d", features = [
"msg",
"bindings",
] }
# sphinx = { path = "../../sphinx/sphinx" }
uniffi = { version = "0.24.1", optional = true }
hex = { version = "0.4.3", default-features = false }
Expand All @@ -33,10 +39,10 @@ rmp-utils = { version = "0.1.0", path = "../rmp-utils" }
uniffi = { version = "0.24.1", optional = true, features = ["build"] }

[profile.release]
opt-level = 'z' # Optimize for size.
lto = true # Enable Link Time Optimization
codegen-units = 1 # Reduce number of codegen units to increase optimizations.
debug = true # Enable debug symbols. For example, we can use `dwarfdump` to check crash traces.
opt-level = 'z' # Optimize for size.
lto = true # Enable Link Time Optimization
codegen-units = 1 # Reduce number of codegen units to increase optimizations.
debug = true # Enable debug symbols. For example, we can use `dwarfdump` to check crash traces.

[[bin]]
name = "uniffi-bindgen"
Expand Down
189 changes: 189 additions & 0 deletions sphinx-ffi/src/auto.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
use crate::{Result, SphinxError};
use sphinx::bindings;
use sphinx::serde_json;

pub struct RunReturn {
pub topic_0: Option<String>,
pub payload_0: Option<Vec<u8>>,
pub topic_1: Option<String>,
pub payload_1: Option<Vec<u8>>,
pub topic_2: Option<String>,
pub payload_2: Option<Vec<u8>>,
pub state_mp: Option<Vec<u8>>,
pub msg: Option<String>,
pub msg_uuid: Option<String>,
pub msg_index: Option<String>,
pub msg_sender: Option<String>,
pub new_balance: Option<u64>,
pub my_contact_info: Option<String>,
pub sent_status: Option<String>,
pub settled_status: Option<String>,
pub error: Option<String>,
}

pub fn set_network(net: String) -> Result<RunReturn> {
Ok(bindings::set_network(&net)
.map_err(|e| SphinxError::SetNetworkFailed { r: e.to_string() })?
.into())
}

pub fn set_blockheight(bh: u32) -> Result<RunReturn> {
Ok(bindings::set_blockheight(bh)
.map_err(|e| SphinxError::SetBlockheightFailed { r: e.to_string() })?
.into())
}

pub fn add_contact(
seed: String,
unique_time: String,
full_state: Vec<u8>,
to_pubkey: String,
route_hint: String,
my_alias: String,
my_img: String,
amt_msat: u64,
) -> Result<RunReturn> {
Ok(bindings::add_contact(
&seed,
&unique_time,
&full_state,
&to_pubkey,
&route_hint,
&my_alias,
&my_img_opt(&my_img),
amt_msat,
)
.map_err(|e| SphinxError::AddContactFailed { r: e.to_string() })?
.into())
}

pub fn get_contact(full_state: Vec<u8>, contact_pubkey: String) -> Result<String> {
let c = bindings::get_contact(&full_state, &contact_pubkey).map_err(|_| {
SphinxError::GetContactFailed {
r: format!("get_contact failed for pubkey: {}", &contact_pubkey),
}
})?;
let json = serde_json::to_string(&c).map_err(|_| SphinxError::GetContactFailed {
r: format!("get_contact serialization failed"),
})?;
Ok(json)
}

pub fn list_contacts(full_state: Vec<u8>) -> Result<String> {
let cs = bindings::list_contacts(&full_state).map_err(|_| SphinxError::GetContactFailed {
r: format!("list_contacts failed"),
})?;
let json = serde_json::to_string(&cs).map_err(|_| SphinxError::GetContactFailed {
r: format!("list_contacts serialization failed"),
})?;
Ok(json)
}

pub fn get_subscription_topic(
seed: String,
unique_time: String,
full_state: Vec<u8>,
) -> Result<String> {
Ok(
bindings::get_subscription_topic(&seed, &unique_time, &full_state)
.map_err(|e| SphinxError::HandleFailed { r: e.to_string() })?,
)
}

pub fn initial_setup(seed: String, unique_time: String, full_state: Vec<u8>) -> Result<RunReturn> {
Ok(bindings::initial_setup(&seed, &unique_time, &full_state)
.map_err(|e| SphinxError::HandleFailed { r: e.to_string() })?
.into())
}

pub fn fetch_msgs(
seed: String,
unique_time: String,
full_state: Vec<u8>,
last_msg_idx: u64,
limit: Option<u32>,
) -> Result<RunReturn> {
Ok(
bindings::fetch_msgs(&seed, &unique_time, &full_state, last_msg_idx, limit)
.map_err(|e| SphinxError::FetchMsgsFailed { r: e.to_string() })?
.into(),
)
}

pub fn handle(
topic: String,
payload: Vec<u8>,
seed: String,
unique_time: String,
full_state: Vec<u8>,
my_alias: String,
my_img: String,
) -> Result<RunReturn> {
Ok(bindings::handle(
&topic,
&payload,
&seed,
&unique_time,
&full_state,
&my_alias,
&my_img_opt(&my_img),
)
.map_err(|e| SphinxError::HandleFailed { r: e.to_string() })?
.into())
}

pub fn send(
seed: String,
unique_time: String,
to: String,
msg_type: u8,
msg_json: String,
full_state: Vec<u8>,
my_alias: String,
my_img: String,
amt_msat: u64,
) -> Result<RunReturn> {
Ok(bindings::send(
&seed,
&unique_time,
&to,
msg_type,
&msg_json,
&full_state,
&my_alias,
&my_img_opt(&my_img),
amt_msat,
)
.map_err(|e| SphinxError::SendFailed { r: e.to_string() })?
.into())
}

fn my_img_opt(my_img: &str) -> Option<&str> {
match my_img {
"" => None,
_ => Some(my_img),
}
}

impl From<bindings::RunReturn> for RunReturn {
fn from(rr: bindings::RunReturn) -> Self {
RunReturn {
topic_0: rr.topic_0,
payload_0: rr.payload_0,
topic_1: rr.topic_1,
payload_1: rr.payload_1,
topic_2: rr.topic_2,
payload_2: rr.payload_2,
state_mp: rr.state_mp,
msg: rr.msg,
msg_uuid: rr.msg_uuid,
msg_index: rr.msg_index,
msg_sender: rr.msg_sender,
new_balance: rr.new_balance,
my_contact_info: rr.my_contact_info,
sent_status: rr.sent_status,
settled_status: rr.settled_status,
error: rr.error,
}
}
}
17 changes: 17 additions & 0 deletions sphinx-ffi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod auto;
mod control;
mod onion;
mod parse;
Expand All @@ -9,6 +10,8 @@ pub use signer::*;

pub use onion::*;

pub use auto::*;

use sphinx_crypter::chacha::{decrypt as chacha_decrypt, encrypt as chacha_encrypt};
use sphinx_crypter::ecdh::derive_shared_secret_from_slice;
use sphinx_crypter::secp256k1::{PublicKey, Secp256k1, SecretKey};
Expand Down Expand Up @@ -62,6 +65,20 @@ pub enum SphinxError {
BadChildIndex { r: String },
#[error("Bad Msg: {r}")]
BadMsg { r: String },
#[error("AddContactFailed: {r}")]
AddContactFailed { r: String },
#[error("GetContactFailed: {r}")]
GetContactFailed { r: String },
#[error("HandleFailed: {r}")]
HandleFailed { r: String },
#[error("FetchMsgsFailed: {r}")]
FetchMsgsFailed { r: String },
#[error("SendFailed: {r}")]
SendFailed { r: String },
#[error("SetNetworkFailed: {r}")]
SetNetworkFailed { r: String },
#[error("SetBlockheightFailed: {r}")]
SetBlockheightFailed { r: String },
}

pub fn pubkey_from_secret_key(my_secret_key: String) -> Result<String> {
Expand Down
32 changes: 16 additions & 16 deletions sphinx-ffi/src/onion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn root_sign_ms(seed: String, time: String, net: String) -> Result<String> {
Ok(hex::encode(sig))
}

pub fn sign_ms(seed: String, idx: u32, time: String, network: String) -> Result<String> {
pub fn sign_ms(seed: String, idx: u64, time: String, network: String) -> Result<String> {
let idx = idx_to_idx(idx)?;
let km = make_keys_manager(&seed, idx, &time, &network)?;
let sig = sphinx::sig::sign_message(time.as_bytes(), &km.get_node_secret()).map_err(|_| {
Expand All @@ -37,7 +37,7 @@ pub fn sign_ms(seed: String, idx: u32, time: String, network: String) -> Result<

pub fn sign_bytes(
seed: String,
idx: u32,
idx: u64,
time: String,
network: String,
msg: Vec<u8>,
Expand All @@ -52,7 +52,7 @@ pub fn sign_bytes(
Ok(hex::encode(sig))
}

pub fn pubkey_from_seed(seed: String, idx: u32, time: String, network: String) -> Result<String> {
pub fn pubkey_from_seed(seed: String, idx: u64, time: String, network: String) -> Result<String> {
let idx = idx_to_idx(idx)?;
let km = make_keys_manager(&seed, idx, &time, &network)?;
let pubkey = km.get_node_pubkey();
Expand All @@ -67,7 +67,7 @@ fn create_onion_inner(km: &MyKeysManager, hops: String, payload: Vec<u8>) -> Res

pub fn create_onion(
seed: String,
idx: u32,
idx: u64,
time: String,
network: String,
hops: String,
Expand All @@ -81,7 +81,7 @@ pub fn create_onion(

pub fn create_onion_msg(
seed: String,
idx: u32,
idx: u64,
time: String,
network: String,
hops: String,
Expand All @@ -97,7 +97,7 @@ pub fn create_onion_msg(

pub fn peel_onion(
seed: String,
idx: u32,
idx: u64,
time: String,
network: String,
payload: Vec<u8>,
Expand All @@ -109,15 +109,15 @@ pub fn peel_onion(

pub fn peel_onion_msg(
seed: String,
idx: u32,
idx: u64,
time: String,
network: String,
payload: Vec<u8>,
) -> Result<String> {
let idx = idx_to_idx(idx)?;
let km = make_keys_manager(&seed, idx, &time, &network)?;
let bytes = run_peel_received_onion_to_bytes(&km, &payload)?;
let msg = sphinx::msg::parse_sphinx_msg_to_json(&bytes, None)
let msg = sphinx::msg::parse_sphinx_msg_to_json(&bytes, 0)
.map_err(|e| SphinxError::BadMsg { r: e.to_string() })?;
Ok(msg)
}
Expand All @@ -140,7 +140,7 @@ pub fn create_keysend_inner(

pub fn create_keysend(
seed: String,
idx: u32,
idx: u64,
time: String,
network: String,
hops: String,
Expand All @@ -158,7 +158,7 @@ pub fn create_keysend(

pub fn create_keysend_msg(
seed: String,
idx: u32,
idx: u64,
time: String,
network: String,
hops: String,
Expand All @@ -178,7 +178,7 @@ pub fn create_keysend_msg(

pub fn peel_payment(
seed: String,
idx: u32,
idx: u64,
time: String,
network: String,
payload: Vec<u8>,
Expand All @@ -200,7 +200,7 @@ pub fn peel_payment(

pub fn peel_payment_msg(
seed: String,
idx: u32,
idx: u64,
time: String,
network: String,
payload: Vec<u8>,
Expand All @@ -213,12 +213,12 @@ pub fn peel_payment_msg(
let payment_hash = parse_hash(&rhash)?;
let (_amt, _preimage, bytes) =
run_peel_received_payment_to_bytes(&km, &payload, payment_hash, cur_height, cltv_expiry)?;
let msg = sphinx::msg::parse_sphinx_msg_to_json(&bytes, None)
let msg = sphinx::msg::parse_sphinx_msg_to_json(&bytes, 0)
.map_err(|e| SphinxError::BadMsg { r: e.to_string() })?;
Ok(msg)
}

fn idx_to_idx(idx: u32) -> Result<Option<isize>> {
fn idx_to_idx(idx: u64) -> Result<Option<i64>> {
Ok(Some(idx.try_into().map_err(|_| {
SphinxError::BadChildIndex {
r: "infallible".to_string(),
Expand All @@ -228,7 +228,7 @@ fn idx_to_idx(idx: u32) -> Result<Option<isize>> {

fn make_keys_manager(
seed: &str,
idx: Option<isize>,
idx: Option<i64>,
time: &str,
network: &str,
) -> Result<MyKeysManager> {
Expand All @@ -237,7 +237,7 @@ fn make_keys_manager(
let net = Network::from_str(network).map_err(|_| SphinxError::BadArgs {
r: "invalid network".to_string(),
})?;
let mut mkm = make_signer(&seed, idx, ts, net);
let mkm = make_signer(&seed, idx, ts, net);
Ok(mkm)
}

Expand Down
Loading

0 comments on commit f18f6e4

Please sign in to comment.