Skip to content

Commit

Permalink
create_keysend_msg, peel_payment_msg
Browse files Browse the repository at this point in the history
  • Loading branch information
Evanfeenstra committed Nov 7, 2023
1 parent 69fc7ac commit 4508e88
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 27 deletions.
2 changes: 1 addition & 1 deletion sphinx-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ lightning = { git = "https://github.com/Evanfeenstra/rust-lightning", rev = "3f5
sphinx-crypter = { path = "../crypter" }
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 = "456a7732f371b4ad1848d6595d54849fc0c494d7", features = ["msg"] }
sphinx = { git = "https://github.com/stakwork/sphinx", rev = "be7979b035b6e34940f183bfb292b1f95017ae96", features = ["msg"] }
# sphinx = { path = "../../sphinx/sphinx" }
uniffi = { version = "0.24.1", optional = true }
hex = { version = "0.4.3", default-features = false }
Expand Down
108 changes: 87 additions & 21 deletions sphinx-ffi/src/onion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ pub fn pubkey_from_seed(seed: String, idx: u32, time: String, network: String) -
Ok(hex::encode(pubkey.serialize()))
}

fn create_onion_inner(km: &MyKeysManager, hops: String, payload: Vec<u8>) -> Result<Vec<u8>> {
let hops = parse_hops(&hops)?;
let (_, data) = run_create_onion_bytes(&km, hops, &payload)?;
Ok(data)
}

pub fn create_onion(
seed: String,
idx: u32,
Expand All @@ -52,8 +58,7 @@ pub fn create_onion(
) -> Result<Vec<u8>> {
let idx = idx_to_idx(idx)?;
let km = make_keys_manager(&seed, idx, &time, &network)?;
let hops = parse_hops(&hops)?;
let (_, data) = run_create_onion_bytes(&km, hops, &payload)?;
let data = create_onion_inner(&km, hops, payload)?;
Ok(data)
}

Expand All @@ -67,10 +72,9 @@ pub fn create_onion_msg(
) -> Result<Vec<u8>> {
let idx = idx_to_idx(idx)?;
let km = make_keys_manager(&seed, idx, &time, &network)?;
let hops = parse_hops(&hops)?;
let payload = sphinx::msg::create_sphinx_msg_from_json(&km, &msg_json)
.map_err(|e| SphinxError::BadMsg { r: e.to_string() })?;
let (_, data) = run_create_onion_bytes(&km, hops, &payload)?;
let data = create_onion_inner(&km, hops, payload)?;
Ok(data)
}

Expand All @@ -83,7 +87,7 @@ pub fn peel_onion(
) -> Result<Vec<u8>> {
let idx = idx_to_idx(idx)?;
let km = make_keys_manager(&seed, idx, &time, &network)?;
Ok(run_peel_onion_bytes(&km, &payload)?)
Ok(run_peel_onion_to_bytes(&km, &payload)?)
}

pub fn peel_onion_msg(
Expand All @@ -95,12 +99,28 @@ pub fn peel_onion_msg(
) -> Result<String> {
let idx = idx_to_idx(idx)?;
let km = make_keys_manager(&seed, idx, &time, &network)?;
let bytes = run_peel_onion_bytes(&km, &payload)?;
let bytes = run_peel_received_onion_to_bytes(&km, &payload)?;
let msg = sphinx::msg::parse_sphinx_msg_to_json(&bytes, None)
.map_err(|e| SphinxError::BadMsg { r: e.to_string() })?;
Ok(msg)
}

pub fn create_keysend_inner(
km: &MyKeysManager,
hops: String,
msat: u64,
rhash: String,
payload: Vec<u8>,
curr_height: u32,
preimage: String,
) -> Result<Vec<u8>> {
let hops = parse_hops(&hops)?;
let ph = parse_hash(&rhash)?;
let prmg = parse_preimage(&preimage)?;
let data = run_create_keysend_bytes(km, hops, msat, ph, &payload, curr_height, prmg)?;
Ok(data.to_vec())
}

pub fn create_keysend(
seed: String,
idx: u32,
Expand All @@ -115,18 +135,27 @@ pub fn create_keysend(
) -> Result<Vec<u8>> {
let idx = idx_to_idx(idx)?;
let km = make_keys_manager(&seed, idx, &time, &network)?;
let hops = parse_hops(&hops)?;
let payment_hash = parse_hash(&rhash)?;
let preimage = parse_preimage(&preimage)?;
let data = run_create_keysend_bytes(
&km,
hops,
msat,
payment_hash,
&payload,
curr_height,
preimage,
)?;
let data = create_keysend_inner(&km, hops, msat, rhash, payload, curr_height, preimage)?;
Ok(data.to_vec())
}

pub fn create_keysend_msg(
seed: String,
idx: u32,
time: String,
network: String,
hops: String,
msat: u64,
rhash: String,
msg_json: String,
curr_height: u32,
preimage: String,
) -> Result<Vec<u8>> {
let idx = idx_to_idx(idx)?;
let km = make_keys_manager(&seed, idx, &time, &network)?;
let payload = sphinx::msg::create_sphinx_msg_from_json(&km, &msg_json)
.map_err(|e| SphinxError::BadMsg { r: e.to_string() })?;
let data = create_keysend_inner(&km, hops, msat, rhash, payload, curr_height, preimage)?;
Ok(data.to_vec())
}

Expand All @@ -141,7 +170,24 @@ pub fn peel_payment(
let idx = idx_to_idx(idx)?;
let km = make_keys_manager(&seed, idx, &time, &network)?;
let payment_hash = parse_hash(&rhash)?;
Ok(run_peel_payment_bytes(&km, &payload, payment_hash)?)
Ok(run_peel_payment_to_bytes(&km, &payload, payment_hash)?)
}

pub fn peel_payment_msg(
seed: String,
idx: u32,
time: String,
network: String,
payload: Vec<u8>,
rhash: String,
) -> Result<String> {
let idx = idx_to_idx(idx)?;
let km = make_keys_manager(&seed, idx, &time, &network)?;
let payment_hash = parse_hash(&rhash)?;
let (_amt, _preimage, bytes) = run_peel_received_payment_to_bytes(&km, &payload, payment_hash)?;
let msg = sphinx::msg::parse_sphinx_msg_to_json(&bytes, None)
.map_err(|e| SphinxError::BadMsg { r: e.to_string() })?;
Ok(msg)
}

fn idx_to_idx(idx: u32) -> Result<Option<isize>> {
Expand Down Expand Up @@ -240,18 +286,38 @@ fn run_create_keysend_bytes(
})?)
}

fn run_peel_onion_bytes(km: &MyKeysManager, pld: &[u8]) -> Result<Vec<u8>> {
fn run_peel_onion_to_bytes(km: &MyKeysManager, pld: &[u8]) -> Result<Vec<u8>> {
Ok(
sphinx::peel_onion_to_bytes(km, pld).map_err(|e| SphinxError::Decrypt {
r: format!("{:?}", e),
})?,
)
}

fn run_peel_payment_bytes(km: &MyKeysManager, pld: &[u8], rhash: [u8; 32]) -> Result<Vec<u8>> {
fn run_peel_received_onion_to_bytes(km: &MyKeysManager, pld: &[u8]) -> Result<Vec<u8>> {
Ok(
sphinx::peel_received_onion(km, pld).map_err(|e| SphinxError::Decrypt {
r: format!("{:?}", e),
})?,
)
}

fn run_peel_payment_to_bytes(km: &MyKeysManager, pld: &[u8], rhash: [u8; 32]) -> Result<Vec<u8>> {
Ok(
sphinx::peel_payment_onion_to_bytes(km, pld, rhash).map_err(|e| SphinxError::Decrypt {
r: format!("{:?}", e),
})?,
)
}

fn run_peel_received_payment_to_bytes(
km: &MyKeysManager,
pld: &[u8],
rhash: [u8; 32],
) -> Result<(u64, String, Vec<u8>)> {
Ok(
sphinx::peel_received_payment(km, pld, rhash).map_err(|e| SphinxError::Decrypt {
r: format!("{:?}", e),
})?,
)
}
44 changes: 41 additions & 3 deletions sphinx-ffi/src/sphinxrs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,24 @@ public func `createKeysend`(`seed`: String, `idx`: UInt32, `time`: String, `netw
)
}

public func `createKeysendMsg`(`seed`: String, `idx`: UInt32, `time`: String, `network`: String, `hops`: String, `msat`: UInt64, `rhash`: String, `msgJson`: String, `currHeight`: UInt32, `preimage`: String) throws -> Data {
return try FfiConverterData.lift(
try rustCallWithError(FfiConverterTypeSphinxError.lift) {
uniffi_sphinxrs_fn_func_create_keysend_msg(
FfiConverterString.lower(`seed`),
FfiConverterUInt32.lower(`idx`),
FfiConverterString.lower(`time`),
FfiConverterString.lower(`network`),
FfiConverterString.lower(`hops`),
FfiConverterUInt64.lower(`msat`),
FfiConverterString.lower(`rhash`),
FfiConverterString.lower(`msgJson`),
FfiConverterUInt32.lower(`currHeight`),
FfiConverterString.lower(`preimage`),$0)
}
)
}

public func `peelOnion`(`seed`: String, `idx`: UInt32, `time`: String, `network`: String, `payload`: Data) throws -> Data {
return try FfiConverterData.lift(
try rustCallWithError(FfiConverterTypeSphinxError.lift) {
Expand All @@ -971,7 +989,7 @@ public func `peelOnionMsg`(`seed`: String, `idx`: UInt32, `time`: String, `netwo
)
}

public func `peelPayment`(`seed`: String, `idx`: UInt32, `time`: String, `network`: String, `payload`: Data, `preimage`: String) throws -> Data {
public func `peelPayment`(`seed`: String, `idx`: UInt32, `time`: String, `network`: String, `payload`: Data, `rhash`: String) throws -> Data {
return try FfiConverterData.lift(
try rustCallWithError(FfiConverterTypeSphinxError.lift) {
uniffi_sphinxrs_fn_func_peel_payment(
Expand All @@ -980,7 +998,21 @@ public func `peelPayment`(`seed`: String, `idx`: UInt32, `time`: String, `networ
FfiConverterString.lower(`time`),
FfiConverterString.lower(`network`),
FfiConverterData.lower(`payload`),
FfiConverterString.lower(`preimage`),$0)
FfiConverterString.lower(`rhash`),$0)
}
)
}

public func `peelPaymentMsg`(`seed`: String, `idx`: UInt32, `time`: String, `network`: String, `payload`: Data, `rhash`: String) throws -> String {
return try FfiConverterString.lift(
try rustCallWithError(FfiConverterTypeSphinxError.lift) {
uniffi_sphinxrs_fn_func_peel_payment_msg(
FfiConverterString.lower(`seed`),
FfiConverterUInt32.lower(`idx`),
FfiConverterString.lower(`time`),
FfiConverterString.lower(`network`),
FfiConverterData.lower(`payload`),
FfiConverterString.lower(`rhash`),$0)
}
)
}
Expand Down Expand Up @@ -1097,13 +1129,19 @@ private var initializationResult: InitializationResult {
if (uniffi_sphinxrs_checksum_func_create_keysend() != 11970) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_sphinxrs_checksum_func_create_keysend_msg() != 36652) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_sphinxrs_checksum_func_peel_onion() != 38236) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_sphinxrs_checksum_func_peel_onion_msg() != 57967) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_sphinxrs_checksum_func_peel_payment() != 55979) {
if (uniffi_sphinxrs_checksum_func_peel_payment() != 42434) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_sphinxrs_checksum_func_peel_payment_msg() != 11902) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_sphinxrs_checksum_func_sign_ms() != 54469) {
Expand Down
6 changes: 5 additions & 1 deletion sphinx-ffi/src/sphinxrs.udl
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,15 @@ namespace sphinxrs {
[Throws=SphinxError]
bytes create_keysend(string seed, u32 idx, string time, string network, string hops, u64 msat, string rhash, bytes payload, u32 curr_height, string preimage);
[Throws=SphinxError]
bytes create_keysend_msg(string seed, u32 idx, string time, string network, string hops, u64 msat, string rhash, string msg_json, u32 curr_height, string preimage);
[Throws=SphinxError]
bytes peel_onion(string seed, u32 idx, string time, string network, bytes payload);
[Throws=SphinxError]
string peel_onion_msg(string seed, u32 idx, string time, string network, bytes payload);
[Throws=SphinxError]
bytes peel_payment(string seed, u32 idx, string time, string network, bytes payload, string preimage);
bytes peel_payment(string seed, u32 idx, string time, string network, bytes payload, string rhash);
[Throws=SphinxError]
string peel_payment_msg(string seed, u32 idx, string time, string network, bytes payload, string rhash);
[Throws=SphinxError]
string sign_ms(string seed, u32 idx, string time, string network);
[Throws=SphinxError]
Expand Down
12 changes: 11 additions & 1 deletion sphinx-ffi/src/sphinxrsFFI.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,15 @@ RustBuffer uniffi_sphinxrs_fn_func_create_onion_msg(RustBuffer seed, uint32_t id
);
RustBuffer uniffi_sphinxrs_fn_func_create_keysend(RustBuffer seed, uint32_t idx, RustBuffer time, RustBuffer network, RustBuffer hops, uint64_t msat, RustBuffer rhash, RustBuffer payload, uint32_t curr_height, RustBuffer preimage, RustCallStatus *_Nonnull out_status
);
RustBuffer uniffi_sphinxrs_fn_func_create_keysend_msg(RustBuffer seed, uint32_t idx, RustBuffer time, RustBuffer network, RustBuffer hops, uint64_t msat, RustBuffer rhash, RustBuffer msg_json, uint32_t curr_height, RustBuffer preimage, RustCallStatus *_Nonnull out_status
);
RustBuffer uniffi_sphinxrs_fn_func_peel_onion(RustBuffer seed, uint32_t idx, RustBuffer time, RustBuffer network, RustBuffer payload, RustCallStatus *_Nonnull out_status
);
RustBuffer uniffi_sphinxrs_fn_func_peel_onion_msg(RustBuffer seed, uint32_t idx, RustBuffer time, RustBuffer network, RustBuffer payload, RustCallStatus *_Nonnull out_status
);
RustBuffer uniffi_sphinxrs_fn_func_peel_payment(RustBuffer seed, uint32_t idx, RustBuffer time, RustBuffer network, RustBuffer payload, RustBuffer preimage, RustCallStatus *_Nonnull out_status
RustBuffer uniffi_sphinxrs_fn_func_peel_payment(RustBuffer seed, uint32_t idx, RustBuffer time, RustBuffer network, RustBuffer payload, RustBuffer rhash, RustCallStatus *_Nonnull out_status
);
RustBuffer uniffi_sphinxrs_fn_func_peel_payment_msg(RustBuffer seed, uint32_t idx, RustBuffer time, RustBuffer network, RustBuffer payload, RustBuffer rhash, RustCallStatus *_Nonnull out_status
);
RustBuffer uniffi_sphinxrs_fn_func_sign_ms(RustBuffer seed, uint32_t idx, RustBuffer time, RustBuffer network, RustCallStatus *_Nonnull out_status
);
Expand Down Expand Up @@ -169,6 +173,9 @@ uint16_t uniffi_sphinxrs_checksum_func_create_onion_msg(void
);
uint16_t uniffi_sphinxrs_checksum_func_create_keysend(void

);
uint16_t uniffi_sphinxrs_checksum_func_create_keysend_msg(void

);
uint16_t uniffi_sphinxrs_checksum_func_peel_onion(void

Expand All @@ -178,6 +185,9 @@ uint16_t uniffi_sphinxrs_checksum_func_peel_onion_msg(void
);
uint16_t uniffi_sphinxrs_checksum_func_peel_payment(void

);
uint16_t uniffi_sphinxrs_checksum_func_peel_payment_msg(void

);
uint16_t uniffi_sphinxrs_checksum_func_sign_ms(void

Expand Down

0 comments on commit 4508e88

Please sign in to comment.