Skip to content

Commit

Permalink
update bindings to use vls ky manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Evanfeenstra committed Oct 17, 2023
1 parent b5ca72b commit 6793985
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 51 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 = "ae357a141b4f8027517ac15266182a7a1b4ddd74" }
sphinx = { git = "https://github.com/stakwork/sphinx", rev = "68a33f994c0e31e58fe12ffeec93eb1baabf77af" }
uniffi = { version = "0.24.1", optional = true }
hex = { version = "0.4.3", default-features = false }
thiserror = "1.0.31"
Expand Down
68 changes: 44 additions & 24 deletions sphinx-ffi/src/onion.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,66 @@
use crate::{Result, SphinxError};
use sphinx::{KeysManager, PublicKey, Secp256k1};
use sphinx::{
KeyDerivationStyle, MyKeysManager, Network, NowStartingTimeFactory, PublicKey, Secp256k1,
};
use std::convert::TryInto;
use std::str::FromStr;

pub fn sha_256(msg: Vec<u8>) -> String {
hex::encode(sphinx::sha_256(&msg))
}

pub fn sign_ms(seed: String, time: String) -> Result<String> {
let km = make_keys_manager(&seed, &time)?;
let sig =
sphinx::sig::sign_message(time.as_bytes(), &km.get_node_secret_key()).map_err(|_| {
SphinxError::BadCiper {
r: "sign failed".to_string(),
}
})?;
pub fn sign_ms(seed: String, time: String, network: String) -> Result<String> {
let km = make_keys_manager(&seed, &time, &network)?;
let sig = sphinx::sig::sign_message(time.as_bytes(), &km.get_node_secret()).map_err(|_| {
SphinxError::BadCiper {
r: "sign failed".to_string(),
}
})?;
Ok(hex::encode(sig))
}

pub fn pubkey_from_seed(seed: String, time: String) -> Result<String> {
let km = make_keys_manager(&seed, &time)?;
pub fn pubkey_from_seed(seed: String, time: String, network: String) -> Result<String> {
let km = make_keys_manager(&seed, &time, &network)?;
let secp_ctx = sphinx::Secp256k1::new();
let pubkey = PublicKey::from_secret_key(&secp_ctx, &km.get_node_secret_key());
let pubkey = PublicKey::from_secret_key(&secp_ctx, &km.get_node_secret());
Ok(hex::encode(pubkey.serialize()))
}

pub fn create_onion(seed: String, time: String, hops: String, payload: Vec<u8>) -> Result<Vec<u8>> {
let km = make_keys_manager(&seed, &time)?;
pub fn create_onion(
seed: String,
time: String,
hops: String,
network: String,
payload: Vec<u8>,
) -> Result<Vec<u8>> {
let km = make_keys_manager(&seed, &time, &network)?;
let hops = parse_hops(&hops)?;
let (_, data) = run_create_onion_bytes(&km, hops, &payload)?;
Ok(data)
}

pub fn peel_onion(seed: String, time: String, payload: Vec<u8>) -> Result<Vec<u8>> {
let km = make_keys_manager(&seed, &time)?;
pub fn peel_onion(
seed: String,
time: String,
network: String,
payload: Vec<u8>,
) -> Result<Vec<u8>> {
let km = make_keys_manager(&seed, &time, &network)?;
Ok(run_peel_onion_bytes(&km, &payload)?)
}

pub fn create_keysend(
seed: String,
time: String,
network: String,
hops: String,
msat: u64,
rhash: String,
payload: Vec<u8>,
curr_height: u32,
preimage: String,
) -> Result<Vec<u8>> {
let km = make_keys_manager(&seed, &time)?;
let km = make_keys_manager(&seed, &time, &network)?;
let hops = parse_hops(&hops)?;
let payment_hash = parse_hash(&rhash)?;
let preimage = parse_preimage(&preimage)?;
Expand All @@ -65,19 +79,25 @@ pub fn create_keysend(
pub fn peel_payment(
seed: String,
time: String,
network: String,
payload: Vec<u8>,
rhash: String,
) -> Result<Vec<u8>> {
let km = make_keys_manager(&seed, &time)?;
let km = make_keys_manager(&seed, &time, &network)?;
let payment_hash = parse_hash(&rhash)?;
Ok(run_peel_payment_bytes(&km, &payload, payment_hash)?)
}

fn make_keys_manager(seed: &str, time: &str) -> Result<KeysManager> {
fn make_keys_manager(seed: &str, time: &str, network: &str) -> Result<MyKeysManager> {
let seed = parse_seed(seed)?;
let ts = parse_u64(time)?;
let time = std::time::Duration::from_millis(ts);
Ok(KeysManager::new(&seed, time.as_secs(), time.subsec_nanos()))
let nstf = NowStartingTimeFactory::new(time);
let net = Network::from_str(network).map_err(|_| SphinxError::BadArgs {
r: "invalid network".to_string(),
})?;
let style = KeyDerivationStyle::Native;
Ok(MyKeysManager::new(style, &seed, net, &nstf))
}

fn parse_u64(time: &str) -> Result<u64> {
Expand Down Expand Up @@ -119,7 +139,7 @@ fn parse_hops(hops: &str) -> Result<Vec<sphinx::Hop>> {
}

fn run_create_onion_bytes(
km: &KeysManager,
km: &MyKeysManager,
hops: Vec<sphinx::Hop>,
pld: &[u8],
) -> Result<(PublicKey, Vec<u8>)> {
Expand All @@ -131,7 +151,7 @@ fn run_create_onion_bytes(
}

fn run_create_keysend_bytes(
km: &KeysManager,
km: &MyKeysManager,
hops: Vec<sphinx::Hop>,
value: u64,
rhash: [u8; 32],
Expand All @@ -153,15 +173,15 @@ fn run_create_keysend_bytes(
})?)
}

fn run_peel_onion_bytes(km: &KeysManager, pld: &[u8]) -> Result<Vec<u8>> {
fn run_peel_onion_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: &KeysManager, pld: &[u8], rhash: [u8; 32]) -> Result<Vec<u8>> {
fn run_peel_payment_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),
Expand Down
34 changes: 20 additions & 14 deletions sphinx-ffi/src/sphinxrs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -881,24 +881,26 @@ public func `sha256`(`msg`: Data) -> String {
)
}

public func `createOnion`(`seed`: String, `time`: String, `hops`: String, `payload`: Data) throws -> Data {
public func `createOnion`(`seed`: String, `time`: String, `network`: String, `hops`: String, `payload`: Data) throws -> Data {
return try FfiConverterData.lift(
try rustCallWithError(FfiConverterTypeSphinxError.lift) {
uniffi_sphinxrs_fn_func_create_onion(
FfiConverterString.lower(`seed`),
FfiConverterString.lower(`time`),
FfiConverterString.lower(`network`),
FfiConverterString.lower(`hops`),
FfiConverterData.lower(`payload`),$0)
}
)
}

public func `createKeysend`(`seed`: String, `time`: String, `hops`: String, `msat`: UInt64, `rhash`: String, `payload`: Data, `currHeight`: UInt32, `preimage`: String) throws -> Data {
public func `createKeysend`(`seed`: String, `time`: String, `network`: String, `hops`: String, `msat`: UInt64, `rhash`: String, `payload`: Data, `currHeight`: UInt32, `preimage`: String) throws -> Data {
return try FfiConverterData.lift(
try rustCallWithError(FfiConverterTypeSphinxError.lift) {
uniffi_sphinxrs_fn_func_create_keysend(
FfiConverterString.lower(`seed`),
FfiConverterString.lower(`time`),
FfiConverterString.lower(`network`),
FfiConverterString.lower(`hops`),
FfiConverterUInt64.lower(`msat`),
FfiConverterString.lower(`rhash`),
Expand All @@ -909,45 +911,49 @@ public func `createKeysend`(`seed`: String, `time`: String, `hops`: String, `msa
)
}

public func `peelOnion`(`seed`: String, `time`: String, `payload`: Data) throws -> Data {
public func `peelOnion`(`seed`: String, `time`: String, `network`: String, `payload`: Data) throws -> Data {
return try FfiConverterData.lift(
try rustCallWithError(FfiConverterTypeSphinxError.lift) {
uniffi_sphinxrs_fn_func_peel_onion(
FfiConverterString.lower(`seed`),
FfiConverterString.lower(`time`),
FfiConverterString.lower(`network`),
FfiConverterData.lower(`payload`),$0)
}
)
}

public func `peelPayment`(`seed`: String, `time`: String, `payload`: Data, `preimage`: String) throws -> Data {
public func `peelPayment`(`seed`: String, `time`: String, `network`: String, `payload`: Data, `preimage`: String) throws -> Data {
return try FfiConverterData.lift(
try rustCallWithError(FfiConverterTypeSphinxError.lift) {
uniffi_sphinxrs_fn_func_peel_payment(
FfiConverterString.lower(`seed`),
FfiConverterString.lower(`time`),
FfiConverterString.lower(`network`),
FfiConverterData.lower(`payload`),
FfiConverterString.lower(`preimage`),$0)
}
)
}

public func `signMs`(`seed`: String, `time`: String) throws -> String {
public func `signMs`(`seed`: String, `time`: String, `network`: String) throws -> String {
return try FfiConverterString.lift(
try rustCallWithError(FfiConverterTypeSphinxError.lift) {
uniffi_sphinxrs_fn_func_sign_ms(
FfiConverterString.lower(`seed`),
FfiConverterString.lower(`time`),$0)
FfiConverterString.lower(`time`),
FfiConverterString.lower(`network`),$0)
}
)
}

public func `pubkeyFromSeed`(`seed`: String, `time`: String) throws -> String {
public func `pubkeyFromSeed`(`seed`: String, `time`: String, `network`: String) throws -> String {
return try FfiConverterString.lift(
try rustCallWithError(FfiConverterTypeSphinxError.lift) {
uniffi_sphinxrs_fn_func_pubkey_from_seed(
FfiConverterString.lower(`seed`),
FfiConverterString.lower(`time`),$0)
FfiConverterString.lower(`time`),
FfiConverterString.lower(`network`),$0)
}
)
}
Expand Down Expand Up @@ -1009,22 +1015,22 @@ private var initializationResult: InitializationResult {
if (uniffi_sphinxrs_checksum_func_sha_256() != 54805) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_sphinxrs_checksum_func_create_onion() != 39744) {
if (uniffi_sphinxrs_checksum_func_create_onion() != 27807) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_sphinxrs_checksum_func_create_keysend() != 11203) {
if (uniffi_sphinxrs_checksum_func_create_keysend() != 38040) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_sphinxrs_checksum_func_peel_onion() != 54527) {
if (uniffi_sphinxrs_checksum_func_peel_onion() != 6042) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_sphinxrs_checksum_func_peel_payment() != 5916) {
if (uniffi_sphinxrs_checksum_func_peel_payment() != 37944) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_sphinxrs_checksum_func_sign_ms() != 65056) {
if (uniffi_sphinxrs_checksum_func_sign_ms() != 34795) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_sphinxrs_checksum_func_pubkey_from_seed() != 40652) {
if (uniffi_sphinxrs_checksum_func_pubkey_from_seed() != 33074) {
return InitializationResult.apiChecksumMismatch
}

Expand Down
12 changes: 6 additions & 6 deletions sphinx-ffi/src/sphinxrs.udl
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ namespace sphinxrs {
VlsResponse run(string topic, string args, bytes state, bytes msg1, u16? expected_sequence);
string sha_256(bytes msg);
[Throws=SphinxError]
bytes create_onion(string seed, string time, string hops, bytes payload);
bytes create_onion(string seed, string time, string network, string hops, bytes payload);
[Throws=SphinxError]
bytes create_keysend(string seed, string time, string hops, u64 msat, string rhash, bytes payload, u32 curr_height, string preimage);
bytes create_keysend(string seed, string time, string network, string hops, u64 msat, string rhash, bytes payload, u32 curr_height, string preimage);
[Throws=SphinxError]
bytes peel_onion(string seed, string time, bytes payload);
bytes peel_onion(string seed, string time, string network, bytes payload);
[Throws=SphinxError]
bytes peel_payment(string seed, string time, bytes payload, string preimage);
bytes peel_payment(string seed, string time, string network, bytes payload, string preimage);
[Throws=SphinxError]
string sign_ms(string seed, string time);
string sign_ms(string seed, string time, string network);
[Throws=SphinxError]
string pubkey_from_seed(string seed, string time);
string pubkey_from_seed(string seed, string time, string network);
};
12 changes: 6 additions & 6 deletions sphinx-ffi/src/sphinxrsFFI.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,17 @@ RustBuffer uniffi_sphinxrs_fn_func_run(RustBuffer topic, RustBuffer args, RustBu
);
RustBuffer uniffi_sphinxrs_fn_func_sha_256(RustBuffer msg, RustCallStatus *_Nonnull out_status
);
RustBuffer uniffi_sphinxrs_fn_func_create_onion(RustBuffer seed, RustBuffer time, RustBuffer hops, RustBuffer payload, RustCallStatus *_Nonnull out_status
RustBuffer uniffi_sphinxrs_fn_func_create_onion(RustBuffer seed, RustBuffer time, RustBuffer network, RustBuffer hops, RustBuffer payload, RustCallStatus *_Nonnull out_status
);
RustBuffer uniffi_sphinxrs_fn_func_create_keysend(RustBuffer seed, RustBuffer time, 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(RustBuffer seed, 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_peel_onion(RustBuffer seed, RustBuffer time, RustBuffer payload, RustCallStatus *_Nonnull out_status
RustBuffer uniffi_sphinxrs_fn_func_peel_onion(RustBuffer seed, RustBuffer time, RustBuffer network, RustBuffer payload, RustCallStatus *_Nonnull out_status
);
RustBuffer uniffi_sphinxrs_fn_func_peel_payment(RustBuffer seed, RustBuffer time, RustBuffer payload, RustBuffer preimage, RustCallStatus *_Nonnull out_status
RustBuffer uniffi_sphinxrs_fn_func_peel_payment(RustBuffer seed, RustBuffer time, RustBuffer network, RustBuffer payload, RustBuffer preimage, RustCallStatus *_Nonnull out_status
);
RustBuffer uniffi_sphinxrs_fn_func_sign_ms(RustBuffer seed, RustBuffer time, RustCallStatus *_Nonnull out_status
RustBuffer uniffi_sphinxrs_fn_func_sign_ms(RustBuffer seed, RustBuffer time, RustBuffer network, RustCallStatus *_Nonnull out_status
);
RustBuffer uniffi_sphinxrs_fn_func_pubkey_from_seed(RustBuffer seed, RustBuffer time, RustCallStatus *_Nonnull out_status
RustBuffer uniffi_sphinxrs_fn_func_pubkey_from_seed(RustBuffer seed, RustBuffer time, RustBuffer network, RustCallStatus *_Nonnull out_status
);
RustBuffer ffi_sphinxrs_rustbuffer_alloc(int32_t size, RustCallStatus *_Nonnull out_status
);
Expand Down

0 comments on commit 6793985

Please sign in to comment.