Skip to content

Commit

Permalink
child indexes in bindings, root_sign_ms, xpub_from_seed
Browse files Browse the repository at this point in the history
  • Loading branch information
Evanfeenstra committed Nov 3, 2023
1 parent 1b6b891 commit 4bd68a9
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 39 deletions.
3 changes: 2 additions & 1 deletion sphinx-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ 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 = "2cf75b38e1edf52d4203255ab3ea858d5fbec1a2" }
sphinx = { git = "https://github.com/stakwork/sphinx", rev = "e8ba3efe0039e866b16b45dbaf59a1f80df3907f" }
# sphinx = { path = "../../sphinx/sphinx" }
uniffi = { version = "0.24.1", optional = true }
hex = { version = "0.4.3", default-features = false }
thiserror = "1.0.31"
Expand Down
2 changes: 2 additions & 0 deletions sphinx-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ pub enum SphinxError {
LssFailed { r: String },
#[error("VLS Failed: {r}")]
VlsFailed { r: String },
#[error("Bad Child Index: {r}")]
BadChildIndex { r: String },
}

pub fn pubkey_from_secret_key(my_secret_key: String) -> Result<String> {
Expand Down
69 changes: 55 additions & 14 deletions sphinx-ffi/src/onion.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
use crate::{Result, SphinxError};
use sphinx::{
KeyDerivationStyle, MyKeysManager, Network, NowStartingTimeFactory, PublicKey, Secp256k1,
};
use sphinx::bip32::XKey;
use sphinx::{KeyDerivationStyle, MyKeysManager, Network, NowStartingTimeFactory, PublicKey};
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, network: String) -> Result<String> {
let km = make_keys_manager(&seed, &time, &network)?;
pub fn xpub_from_seed(seed: String, time: String, net: String) -> Result<String> {
let km = make_keys_manager(&seed, Some(-1), &time, &net)?;
let xpub = km.root_xpub();
Ok(xpub.to_string())
}

pub fn root_sign_ms(seed: String, time: String, net: String) -> Result<String> {
let km = make_keys_manager(&seed, Some(-1), &time, &net)?;
let sig = sphinx::sig::sign_message(time.as_bytes(), &km.get_node_secret()).map_err(|_| {
SphinxError::BadCiper {
r: "sign failed".to_string(),
Expand All @@ -19,38 +24,54 @@ pub fn sign_ms(seed: String, time: String, network: String) -> Result<String> {
Ok(hex::encode(sig))
}

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();
pub fn sign_ms(seed: String, idx: u32, 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(|_| {
SphinxError::BadCiper {
r: "sign failed".to_string(),
}
})?;
Ok(hex::encode(sig))
}

pub fn pubkey_from_seed(seed: String, idx: u32, 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();
Ok(hex::encode(pubkey.serialize()))
}

pub fn create_onion(
seed: String,
idx: u32,
time: String,
network: String,
hops: String,
payload: Vec<u8>,
) -> Result<Vec<u8>> {
let km = make_keys_manager(&seed, &time, &network)?;
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)?;
Ok(data)
}

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

pub fn create_keysend(
seed: String,
idx: u32,
time: String,
network: String,
hops: String,
Expand All @@ -60,7 +81,8 @@ pub fn create_keysend(
curr_height: u32,
preimage: String,
) -> Result<Vec<u8>> {
let km = make_keys_manager(&seed, &time, &network)?;
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)?;
Expand All @@ -78,17 +100,32 @@ pub fn create_keysend(

pub fn peel_payment(
seed: String,
idx: u32,
time: String,
network: String,
payload: Vec<u8>,
rhash: String,
) -> Result<Vec<u8>> {
let km = make_keys_manager(&seed, &time, &network)?;
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)?)
}

fn make_keys_manager(seed: &str, time: &str, network: &str) -> Result<MyKeysManager> {
fn idx_to_idx(idx: u32) -> Result<Option<isize>> {
Ok(Some(idx.try_into().map_err(|_| {
SphinxError::BadChildIndex {
r: "infallible".to_string(),
}
})?))
}

fn make_keys_manager(
seed: &str,
idx: Option<isize>,
time: &str,
network: &str,
) -> Result<MyKeysManager> {
let seed = parse_seed(seed)?;
let ts = parse_u64(time)?;
let time = std::time::Duration::from_millis(ts);
Expand All @@ -97,7 +134,11 @@ fn make_keys_manager(seed: &str, time: &str, network: &str) -> Result<MyKeysMana
r: "invalid network".to_string(),
})?;
let style = KeyDerivationStyle::Native;
Ok(MyKeysManager::new(style, &seed, net, &nstf))
let mut mkm = MyKeysManager::new(style, &seed, net, &nstf);
if let Some(cidx) = idx {
mkm.set_current_signing_child_index(cidx);
}
Ok(mkm)
}

fn parse_u64(time: &str) -> Result<u64> {
Expand Down
67 changes: 55 additions & 12 deletions sphinx-ffi/src/sphinxrs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ public enum SphinxError {
case InitFailed(`r`: String)
case LssFailed(`r`: String)
case VlsFailed(`r`: String)
case BadChildIndex(`r`: String)

fileprivate static func uniffiErrorHandler(_ error: RustBuffer) throws -> Error {
return try FfiConverterTypeSphinxError.lift(error)
Expand Down Expand Up @@ -612,6 +613,9 @@ public struct FfiConverterTypeSphinxError: FfiConverterRustBuffer {
case 18: return .VlsFailed(
`r`: try FfiConverterString.read(from: &buf)
)
case 19: return .BadChildIndex(
`r`: try FfiConverterString.read(from: &buf)
)

default: throw UniffiInternalError.unexpectedEnumCase
}
Expand Down Expand Up @@ -713,6 +717,11 @@ public struct FfiConverterTypeSphinxError: FfiConverterRustBuffer {
writeInt(&buf, Int32(18))
FfiConverterString.write(`r`, into: &buf)


case let .BadChildIndex(`r`):
writeInt(&buf, Int32(19))
FfiConverterString.write(`r`, into: &buf)

}
}
}
Expand Down Expand Up @@ -881,11 +890,12 @@ public func `sha256`(`msg`: Data) -> String {
)
}

public func `createOnion`(`seed`: String, `time`: String, `network`: String, `hops`: String, `payload`: Data) throws -> Data {
public func `createOnion`(`seed`: String, `idx`: UInt32, `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`),
FfiConverterUInt32.lower(`idx`),
FfiConverterString.lower(`time`),
FfiConverterString.lower(`network`),
FfiConverterString.lower(`hops`),
Expand All @@ -894,11 +904,12 @@ public func `createOnion`(`seed`: String, `time`: String, `network`: String, `ho
)
}

public func `createKeysend`(`seed`: String, `time`: String, `network`: String, `hops`: String, `msat`: UInt64, `rhash`: String, `payload`: Data, `currHeight`: UInt32, `preimage`: String) throws -> Data {
public func `createKeysend`(`seed`: String, `idx`: UInt32, `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`),
FfiConverterUInt32.lower(`idx`),
FfiConverterString.lower(`time`),
FfiConverterString.lower(`network`),
FfiConverterString.lower(`hops`),
Expand All @@ -911,23 +922,25 @@ public func `createKeysend`(`seed`: String, `time`: String, `network`: String, `
)
}

public func `peelOnion`(`seed`: String, `time`: String, `network`: String, `payload`: Data) throws -> Data {
public func `peelOnion`(`seed`: String, `idx`: UInt32, `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`),
FfiConverterUInt32.lower(`idx`),
FfiConverterString.lower(`time`),
FfiConverterString.lower(`network`),
FfiConverterData.lower(`payload`),$0)
}
)
}

public func `peelPayment`(`seed`: String, `time`: String, `network`: String, `payload`: Data, `preimage`: String) throws -> Data {
public func `peelPayment`(`seed`: String, `idx`: UInt32, `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`),
FfiConverterUInt32.lower(`idx`),
FfiConverterString.lower(`time`),
FfiConverterString.lower(`network`),
FfiConverterData.lower(`payload`),
Expand All @@ -936,21 +949,45 @@ public func `peelPayment`(`seed`: String, `time`: String, `network`: String, `pa
)
}

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

public func `pubkeyFromSeed`(`seed`: String, `time`: String, `network`: String) throws -> String {
public func `pubkeyFromSeed`(`seed`: String, `idx`: UInt32, `time`: String, `network`: String) throws -> String {
return try FfiConverterString.lift(
try rustCallWithError(FfiConverterTypeSphinxError.lift) {
uniffi_sphinxrs_fn_func_pubkey_from_seed(
FfiConverterString.lower(`seed`),
FfiConverterUInt32.lower(`idx`),
FfiConverterString.lower(`time`),
FfiConverterString.lower(`network`),$0)
}
)
}

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

public func `xpubFromSeed`(`seed`: String, `time`: String, `network`: String) throws -> String {
return try FfiConverterString.lift(
try rustCallWithError(FfiConverterTypeSphinxError.lift) {
uniffi_sphinxrs_fn_func_xpub_from_seed(
FfiConverterString.lower(`seed`),
FfiConverterString.lower(`time`),
FfiConverterString.lower(`network`),$0)
Expand Down Expand Up @@ -1015,22 +1052,28 @@ private var initializationResult: InitializationResult {
if (uniffi_sphinxrs_checksum_func_sha_256() != 54805) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_sphinxrs_checksum_func_create_onion() != 27807) {
if (uniffi_sphinxrs_checksum_func_create_onion() != 65256) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_sphinxrs_checksum_func_create_keysend() != 11970) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_sphinxrs_checksum_func_peel_onion() != 38236) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_sphinxrs_checksum_func_create_keysend() != 38040) {
if (uniffi_sphinxrs_checksum_func_peel_payment() != 55979) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_sphinxrs_checksum_func_peel_onion() != 6042) {
if (uniffi_sphinxrs_checksum_func_sign_ms() != 54469) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_sphinxrs_checksum_func_peel_payment() != 37944) {
if (uniffi_sphinxrs_checksum_func_pubkey_from_seed() != 3734) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_sphinxrs_checksum_func_sign_ms() != 34795) {
if (uniffi_sphinxrs_checksum_func_root_sign_ms() != 52594) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_sphinxrs_checksum_func_pubkey_from_seed() != 33074) {
if (uniffi_sphinxrs_checksum_func_xpub_from_seed() != 14922) {
return InitializationResult.apiChecksumMismatch
}

Expand Down
17 changes: 11 additions & 6 deletions sphinx-ffi/src/sphinxrs.udl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface SphinxError {
InitFailed(string r);
LssFailed(string r);
VlsFailed(string r);
BadChildIndex(string r);
};

dictionary Keys {
Expand Down Expand Up @@ -62,15 +63,19 @@ 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 network, string hops, bytes payload);
bytes create_onion(string seed, u32 idx, string time, string network, string hops, bytes payload);
[Throws=SphinxError]
bytes create_keysend(string seed, string time, string network, string hops, u64 msat, string rhash, bytes payload, u32 curr_height, string preimage);
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 peel_onion(string seed, string time, string network, bytes payload);
bytes peel_onion(string seed, u32 idx, string time, string network, bytes payload);
[Throws=SphinxError]
bytes peel_payment(string seed, string time, string network, bytes payload, string preimage);
bytes peel_payment(string seed, u32 idx, string time, string network, bytes payload, string preimage);
[Throws=SphinxError]
string sign_ms(string seed, string time, string network);
string sign_ms(string seed, u32 idx, string time, string network);
[Throws=SphinxError]
string pubkey_from_seed(string seed, string time, string network);
string pubkey_from_seed(string seed, u32 idx, string time, string network);
[Throws=SphinxError]
string root_sign_ms(string seed, string time, string network);
[Throws=SphinxError]
string xpub_from_seed(string seed, string time, string network);
};
Loading

0 comments on commit 4bd68a9

Please sign in to comment.