From 627bde5ed3fee8697d32248753310b755d993a3e Mon Sep 17 00:00:00 2001 From: Evan Feenstra Date: Tue, 5 Mar 2024 10:52:32 -0800 Subject: [PATCH] invoices stuff --- sphinx-ffi/Cargo.toml | 2 +- sphinx-ffi/src/auto.rs | 34 +++++++++++------ sphinx-ffi/src/onion.rs | 2 +- sphinx-ffi/src/sphinxrs.swift | 43 +++++++++++++++++++--- sphinx-ffi/src/sphinxrs.udl | 6 ++- sphinx-ffi/src/sphinxrsFFI.h | 7 +++- sphinx-ffi/src/uniffi/sphinxrs/sphinxrs.kt | 38 +++++++++++++++---- 7 files changed, 104 insertions(+), 28 deletions(-) diff --git a/sphinx-ffi/Cargo.toml b/sphinx-ffi/Cargo.toml index 1fe5538..b76509e 100644 --- a/sphinx-ffi/Cargo.toml +++ b/sphinx-ffi/Cargo.toml @@ -25,7 +25,7 @@ sphinx-signer = { path = "../signer", default-features = false, features = [ "no-native", ] } sphinx-glyph = { path = "../glyph", default-features = false } -sphinx = { git = "https://github.com/stakwork/sphinx", rev = "44c04d19c83b966ea619df326d14047f19d882fe", features = [ +sphinx = { git = "https://github.com/stakwork/sphinx", rev = "cc96d3467ed5418f441b7e28d50fd65d4ab34c8d", features = [ "msg", "bindings", ] } diff --git a/sphinx-ffi/src/auto.rs b/sphinx-ffi/src/auto.rs index ba78e40..bb8802b 100644 --- a/sphinx-ffi/src/auto.rs +++ b/sphinx-ffi/src/auto.rs @@ -18,6 +18,7 @@ pub struct RunReturn { pub topics: Vec, pub payloads: Vec>, pub state_mp: Option>, + pub state_to_delete: Vec, pub new_balance: Option, pub my_contact_info: Option, pub sent_status: Option, @@ -30,6 +31,7 @@ pub struct RunReturn { pub inviter_alias: Option, pub initial_tribe: Option, pub lsp_host: Option, + pub invoice: Option, } pub fn set_network(net: String) -> Result { @@ -260,19 +262,27 @@ pub fn make_invoice( unique_time: String, full_state: Vec, amt_msat: u64, - preimage: String, description: String, -) -> Result { - Ok(bindings::make_invoice( - &seed, - &unique_time, - &full_state, - amt_msat, - &preimage, - &description, +) -> Result { + Ok( + bindings::make_invoice(&seed, &unique_time, &full_state, amt_msat, &description) + .map_err(|e| SphinxError::SendFailed { r: e.to_string() })? + .into(), + ) +} + +pub fn pay_invoice( + seed: String, + unique_time: String, + full_state: Vec, + bolt11: String, + overpay_msat: Option, +) -> Result { + Ok( + bindings::pay_invoice(&seed, &unique_time, &full_state, &bolt11, overpay_msat) + .map_err(|e| SphinxError::SendFailed { r: e.to_string() })? + .into(), ) - .map_err(|e| SphinxError::SendFailed { r: e.to_string() })? - .into()) } pub fn create_tribe( @@ -399,6 +409,7 @@ impl From for RunReturn { topics: rr.topics, payloads: rr.payloads, state_mp: rr.state_mp, + state_to_delete: rr.state_to_delete, new_balance: rr.new_balance, my_contact_info: rr.my_contact_info, sent_status: rr.sent_status, @@ -411,6 +422,7 @@ impl From for RunReturn { inviter_alias: rr.inviter_alias, initial_tribe: rr.initial_tribe, lsp_host: rr.lsp_host, + invoice: rr.invoice, } } } diff --git a/sphinx-ffi/src/onion.rs b/sphinx-ffi/src/onion.rs index 219ba0a..e4a5741 100644 --- a/sphinx-ffi/src/onion.rs +++ b/sphinx-ffi/src/onion.rs @@ -352,7 +352,7 @@ fn run_peel_received_payment_to_bytes( rhash: [u8; 32], cur_height: u32, cltv_expiry: u32, -) -> Result<(u64, String, Vec)> { +) -> Result<(u64, Option, Vec)> { Ok( sphinx::peel_received_payment(km, pld, rhash, cur_height, cltv_expiry).map_err(|e| { SphinxError::Decrypt { diff --git a/sphinx-ffi/src/sphinxrs.swift b/sphinx-ffi/src/sphinxrs.swift index 2bfe7b2..4e27675 100644 --- a/sphinx-ffi/src/sphinxrs.swift +++ b/sphinx-ffi/src/sphinxrs.swift @@ -583,6 +583,7 @@ public struct RunReturn { public var `topics`: [String] public var `payloads`: [Data] public var `stateMp`: Data? + public var `stateToDelete`: [String] public var `newBalance`: UInt64? public var `myContactInfo`: String? public var `sentStatus`: String? @@ -595,14 +596,16 @@ public struct RunReturn { public var `inviterAlias`: String? public var `initialTribe`: String? public var `lspHost`: String? + public var `invoice`: String? // Default memberwise initializers are never public by default, so we // declare one manually. - public init(`msgs`: [Msg], `topics`: [String], `payloads`: [Data], `stateMp`: Data?, `newBalance`: UInt64?, `myContactInfo`: String?, `sentStatus`: String?, `settledStatus`: String?, `error`: String?, `newTribe`: String?, `tribeMembers`: String?, `newInvite`: String?, `inviterContactInfo`: String?, `inviterAlias`: String?, `initialTribe`: String?, `lspHost`: String?) { + public init(`msgs`: [Msg], `topics`: [String], `payloads`: [Data], `stateMp`: Data?, `stateToDelete`: [String], `newBalance`: UInt64?, `myContactInfo`: String?, `sentStatus`: String?, `settledStatus`: String?, `error`: String?, `newTribe`: String?, `tribeMembers`: String?, `newInvite`: String?, `inviterContactInfo`: String?, `inviterAlias`: String?, `initialTribe`: String?, `lspHost`: String?, `invoice`: String?) { self.`msgs` = `msgs` self.`topics` = `topics` self.`payloads` = `payloads` self.`stateMp` = `stateMp` + self.`stateToDelete` = `stateToDelete` self.`newBalance` = `newBalance` self.`myContactInfo` = `myContactInfo` self.`sentStatus` = `sentStatus` @@ -615,6 +618,7 @@ public struct RunReturn { self.`inviterAlias` = `inviterAlias` self.`initialTribe` = `initialTribe` self.`lspHost` = `lspHost` + self.`invoice` = `invoice` } } @@ -633,6 +637,9 @@ extension RunReturn: Equatable, Hashable { if lhs.`stateMp` != rhs.`stateMp` { return false } + if lhs.`stateToDelete` != rhs.`stateToDelete` { + return false + } if lhs.`newBalance` != rhs.`newBalance` { return false } @@ -669,6 +676,9 @@ extension RunReturn: Equatable, Hashable { if lhs.`lspHost` != rhs.`lspHost` { return false } + if lhs.`invoice` != rhs.`invoice` { + return false + } return true } @@ -677,6 +687,7 @@ extension RunReturn: Equatable, Hashable { hasher.combine(`topics`) hasher.combine(`payloads`) hasher.combine(`stateMp`) + hasher.combine(`stateToDelete`) hasher.combine(`newBalance`) hasher.combine(`myContactInfo`) hasher.combine(`sentStatus`) @@ -689,6 +700,7 @@ extension RunReturn: Equatable, Hashable { hasher.combine(`inviterAlias`) hasher.combine(`initialTribe`) hasher.combine(`lspHost`) + hasher.combine(`invoice`) } } @@ -700,6 +712,7 @@ public struct FfiConverterTypeRunReturn: FfiConverterRustBuffer { `topics`: FfiConverterSequenceString.read(from: &buf), `payloads`: FfiConverterSequenceData.read(from: &buf), `stateMp`: FfiConverterOptionData.read(from: &buf), + `stateToDelete`: FfiConverterSequenceString.read(from: &buf), `newBalance`: FfiConverterOptionUInt64.read(from: &buf), `myContactInfo`: FfiConverterOptionString.read(from: &buf), `sentStatus`: FfiConverterOptionString.read(from: &buf), @@ -711,7 +724,8 @@ public struct FfiConverterTypeRunReturn: FfiConverterRustBuffer { `inviterContactInfo`: FfiConverterOptionString.read(from: &buf), `inviterAlias`: FfiConverterOptionString.read(from: &buf), `initialTribe`: FfiConverterOptionString.read(from: &buf), - `lspHost`: FfiConverterOptionString.read(from: &buf) + `lspHost`: FfiConverterOptionString.read(from: &buf), + `invoice`: FfiConverterOptionString.read(from: &buf) ) } @@ -720,6 +734,7 @@ public struct FfiConverterTypeRunReturn: FfiConverterRustBuffer { FfiConverterSequenceString.write(value.`topics`, into: &buf) FfiConverterSequenceData.write(value.`payloads`, into: &buf) FfiConverterOptionData.write(value.`stateMp`, into: &buf) + FfiConverterSequenceString.write(value.`stateToDelete`, into: &buf) FfiConverterOptionUInt64.write(value.`newBalance`, into: &buf) FfiConverterOptionString.write(value.`myContactInfo`, into: &buf) FfiConverterOptionString.write(value.`sentStatus`, into: &buf) @@ -732,6 +747,7 @@ public struct FfiConverterTypeRunReturn: FfiConverterRustBuffer { FfiConverterOptionString.write(value.`inviterAlias`, into: &buf) FfiConverterOptionString.write(value.`initialTribe`, into: &buf) FfiConverterOptionString.write(value.`lspHost`, into: &buf) + FfiConverterOptionString.write(value.`invoice`, into: &buf) } } @@ -1798,20 +1814,32 @@ public func `makeMediaTokenWithPrice`(`seed`: String, `uniqueTime`: String, `sta ) } -public func `makeInvoice`(`seed`: String, `uniqueTime`: String, `state`: Data, `amtMsat`: UInt64, `preimage`: String, `description`: String) throws -> String { - return try FfiConverterString.lift( +public func `makeInvoice`(`seed`: String, `uniqueTime`: String, `state`: Data, `amtMsat`: UInt64, `description`: String) throws -> RunReturn { + return try FfiConverterTypeRunReturn.lift( try rustCallWithError(FfiConverterTypeSphinxError.lift) { uniffi_sphinxrs_fn_func_make_invoice( FfiConverterString.lower(`seed`), FfiConverterString.lower(`uniqueTime`), FfiConverterData.lower(`state`), FfiConverterUInt64.lower(`amtMsat`), - FfiConverterString.lower(`preimage`), FfiConverterString.lower(`description`),$0) } ) } +public func `payInvoice`(`seed`: String, `uniqueTime`: String, `state`: Data, `bolt11`: String, `overpayMsat`: UInt64?) throws -> RunReturn { + return try FfiConverterTypeRunReturn.lift( + try rustCallWithError(FfiConverterTypeSphinxError.lift) { + uniffi_sphinxrs_fn_func_pay_invoice( + FfiConverterString.lower(`seed`), + FfiConverterString.lower(`uniqueTime`), + FfiConverterData.lower(`state`), + FfiConverterString.lower(`bolt11`), + FfiConverterOptionUInt64.lower(`overpayMsat`),$0) +} + ) +} + public func `createTribe`(`seed`: String, `uniqueTime`: String, `state`: Data, `tribeServerPubkey`: String, `tribeJson`: String) throws -> RunReturn { return try FfiConverterTypeRunReturn.lift( try rustCallWithError(FfiConverterTypeSphinxError.lift) { @@ -2029,7 +2057,10 @@ private var initializationResult: InitializationResult { if (uniffi_sphinxrs_checksum_func_make_media_token_with_price() != 53555) { return InitializationResult.apiChecksumMismatch } - if (uniffi_sphinxrs_checksum_func_make_invoice() != 41170) { + if (uniffi_sphinxrs_checksum_func_make_invoice() != 12949) { + return InitializationResult.apiChecksumMismatch + } + if (uniffi_sphinxrs_checksum_func_pay_invoice() != 40951) { return InitializationResult.apiChecksumMismatch } if (uniffi_sphinxrs_checksum_func_create_tribe() != 28873) { diff --git a/sphinx-ffi/src/sphinxrs.udl b/sphinx-ffi/src/sphinxrs.udl index 7d54e2f..9dfb4cd 100644 --- a/sphinx-ffi/src/sphinxrs.udl +++ b/sphinx-ffi/src/sphinxrs.udl @@ -58,6 +58,7 @@ dictionary RunReturn { sequence topics; sequence payloads; bytes? state_mp; + sequence state_to_delete; u64? new_balance; string? my_contact_info; string? sent_status; @@ -70,6 +71,7 @@ dictionary RunReturn { string? inviter_alias; string? initial_tribe; string? lsp_host; + string? invoice; }; namespace sphinxrs { @@ -155,7 +157,9 @@ namespace sphinxrs { [Throws=SphinxError] string make_media_token_with_price(string seed, string unique_time, bytes state, string host, string muid, string to, u32 expiry, u64 price); [Throws=SphinxError] - string make_invoice(string seed, string unique_time, bytes state, u64 amt_msat, string preimage, string description); + RunReturn make_invoice(string seed, string unique_time, bytes state, u64 amt_msat, string description); + [Throws=SphinxError] + RunReturn pay_invoice(string seed, string unique_time, bytes state, string bolt11, u64? overpay_msat); [Throws=SphinxError] RunReturn create_tribe(string seed, string unique_time, bytes state, string tribe_server_pubkey, string tribe_json); [Throws=SphinxError] diff --git a/sphinx-ffi/src/sphinxrsFFI.h b/sphinx-ffi/src/sphinxrsFFI.h index d2468e4..95b2287 100644 --- a/sphinx-ffi/src/sphinxrsFFI.h +++ b/sphinx-ffi/src/sphinxrsFFI.h @@ -145,7 +145,9 @@ RustBuffer uniffi_sphinxrs_fn_func_make_media_token_with_meta(RustBuffer seed, R ); RustBuffer uniffi_sphinxrs_fn_func_make_media_token_with_price(RustBuffer seed, RustBuffer unique_time, RustBuffer state, RustBuffer host, RustBuffer muid, RustBuffer to, uint32_t expiry, uint64_t price, RustCallStatus *_Nonnull out_status ); -RustBuffer uniffi_sphinxrs_fn_func_make_invoice(RustBuffer seed, RustBuffer unique_time, RustBuffer state, uint64_t amt_msat, RustBuffer preimage, RustBuffer description, RustCallStatus *_Nonnull out_status +RustBuffer uniffi_sphinxrs_fn_func_make_invoice(RustBuffer seed, RustBuffer unique_time, RustBuffer state, uint64_t amt_msat, RustBuffer description, RustCallStatus *_Nonnull out_status +); +RustBuffer uniffi_sphinxrs_fn_func_pay_invoice(RustBuffer seed, RustBuffer unique_time, RustBuffer state, RustBuffer bolt11, RustBuffer overpay_msat, RustCallStatus *_Nonnull out_status ); RustBuffer uniffi_sphinxrs_fn_func_create_tribe(RustBuffer seed, RustBuffer unique_time, RustBuffer state, RustBuffer tribe_server_pubkey, RustBuffer tribe_json, RustCallStatus *_Nonnull out_status ); @@ -292,6 +294,9 @@ uint16_t uniffi_sphinxrs_checksum_func_make_media_token_with_price(void ); uint16_t uniffi_sphinxrs_checksum_func_make_invoice(void +); +uint16_t uniffi_sphinxrs_checksum_func_pay_invoice(void + ); uint16_t uniffi_sphinxrs_checksum_func_create_tribe(void diff --git a/sphinx-ffi/src/uniffi/sphinxrs/sphinxrs.kt b/sphinx-ffi/src/uniffi/sphinxrs/sphinxrs.kt index d06869f..8f13c77 100644 --- a/sphinx-ffi/src/uniffi/sphinxrs/sphinxrs.kt +++ b/sphinx-ffi/src/uniffi/sphinxrs/sphinxrs.kt @@ -446,7 +446,9 @@ internal interface _UniFFILib : Library { ): RustBuffer.ByValue fun uniffi_sphinxrs_fn_func_make_media_token_with_price(`seed`: RustBuffer.ByValue,`uniqueTime`: RustBuffer.ByValue,`state`: RustBuffer.ByValue,`host`: RustBuffer.ByValue,`muid`: RustBuffer.ByValue,`to`: RustBuffer.ByValue,`expiry`: Int,`price`: Long,_uniffi_out_err: RustCallStatus, ): RustBuffer.ByValue - fun uniffi_sphinxrs_fn_func_make_invoice(`seed`: RustBuffer.ByValue,`uniqueTime`: RustBuffer.ByValue,`state`: RustBuffer.ByValue,`amtMsat`: Long,`preimage`: RustBuffer.ByValue,`description`: RustBuffer.ByValue,_uniffi_out_err: RustCallStatus, + fun uniffi_sphinxrs_fn_func_make_invoice(`seed`: RustBuffer.ByValue,`uniqueTime`: RustBuffer.ByValue,`state`: RustBuffer.ByValue,`amtMsat`: Long,`description`: RustBuffer.ByValue,_uniffi_out_err: RustCallStatus, + ): RustBuffer.ByValue + fun uniffi_sphinxrs_fn_func_pay_invoice(`seed`: RustBuffer.ByValue,`uniqueTime`: RustBuffer.ByValue,`state`: RustBuffer.ByValue,`bolt11`: RustBuffer.ByValue,`overpayMsat`: RustBuffer.ByValue,_uniffi_out_err: RustCallStatus, ): RustBuffer.ByValue fun uniffi_sphinxrs_fn_func_create_tribe(`seed`: RustBuffer.ByValue,`uniqueTime`: RustBuffer.ByValue,`state`: RustBuffer.ByValue,`tribeServerPubkey`: RustBuffer.ByValue,`tribeJson`: RustBuffer.ByValue,_uniffi_out_err: RustCallStatus, ): RustBuffer.ByValue @@ -552,6 +554,8 @@ internal interface _UniFFILib : Library { ): Short fun uniffi_sphinxrs_checksum_func_make_invoice( ): Short + fun uniffi_sphinxrs_checksum_func_pay_invoice( + ): Short fun uniffi_sphinxrs_checksum_func_create_tribe( ): Short fun uniffi_sphinxrs_checksum_func_join_tribe( @@ -704,7 +708,10 @@ private fun uniffiCheckApiChecksums(lib: _UniFFILib) { if (lib.uniffi_sphinxrs_checksum_func_make_media_token_with_price() != 53555.toShort()) { throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project") } - if (lib.uniffi_sphinxrs_checksum_func_make_invoice() != 41170.toShort()) { + if (lib.uniffi_sphinxrs_checksum_func_make_invoice() != 12949.toShort()) { + throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project") + } + if (lib.uniffi_sphinxrs_checksum_func_pay_invoice() != 40951.toShort()) { throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project") } if (lib.uniffi_sphinxrs_checksum_func_create_tribe() != 28873.toShort()) { @@ -982,6 +989,7 @@ data class RunReturn ( var `topics`: List, var `payloads`: List, var `stateMp`: ByteArray?, + var `stateToDelete`: List, var `newBalance`: ULong?, var `myContactInfo`: String?, var `sentStatus`: String?, @@ -993,7 +1001,8 @@ data class RunReturn ( var `inviterContactInfo`: String?, var `inviterAlias`: String?, var `initialTribe`: String?, - var `lspHost`: String? + var `lspHost`: String?, + var `invoice`: String? ) { } @@ -1005,6 +1014,7 @@ public object FfiConverterTypeRunReturn: FfiConverterRustBuffer { FfiConverterSequenceString.read(buf), FfiConverterSequenceByteArray.read(buf), FfiConverterOptionalByteArray.read(buf), + FfiConverterSequenceString.read(buf), FfiConverterOptionalULong.read(buf), FfiConverterOptionalString.read(buf), FfiConverterOptionalString.read(buf), @@ -1017,6 +1027,7 @@ public object FfiConverterTypeRunReturn: FfiConverterRustBuffer { FfiConverterOptionalString.read(buf), FfiConverterOptionalString.read(buf), FfiConverterOptionalString.read(buf), + FfiConverterOptionalString.read(buf), ) } @@ -1025,6 +1036,7 @@ public object FfiConverterTypeRunReturn: FfiConverterRustBuffer { FfiConverterSequenceString.allocationSize(value.`topics`) + FfiConverterSequenceByteArray.allocationSize(value.`payloads`) + FfiConverterOptionalByteArray.allocationSize(value.`stateMp`) + + FfiConverterSequenceString.allocationSize(value.`stateToDelete`) + FfiConverterOptionalULong.allocationSize(value.`newBalance`) + FfiConverterOptionalString.allocationSize(value.`myContactInfo`) + FfiConverterOptionalString.allocationSize(value.`sentStatus`) + @@ -1036,7 +1048,8 @@ public object FfiConverterTypeRunReturn: FfiConverterRustBuffer { FfiConverterOptionalString.allocationSize(value.`inviterContactInfo`) + FfiConverterOptionalString.allocationSize(value.`inviterAlias`) + FfiConverterOptionalString.allocationSize(value.`initialTribe`) + - FfiConverterOptionalString.allocationSize(value.`lspHost`) + FfiConverterOptionalString.allocationSize(value.`lspHost`) + + FfiConverterOptionalString.allocationSize(value.`invoice`) ) override fun write(value: RunReturn, buf: ByteBuffer) { @@ -1044,6 +1057,7 @@ public object FfiConverterTypeRunReturn: FfiConverterRustBuffer { FfiConverterSequenceString.write(value.`topics`, buf) FfiConverterSequenceByteArray.write(value.`payloads`, buf) FfiConverterOptionalByteArray.write(value.`stateMp`, buf) + FfiConverterSequenceString.write(value.`stateToDelete`, buf) FfiConverterOptionalULong.write(value.`newBalance`, buf) FfiConverterOptionalString.write(value.`myContactInfo`, buf) FfiConverterOptionalString.write(value.`sentStatus`, buf) @@ -1056,6 +1070,7 @@ public object FfiConverterTypeRunReturn: FfiConverterRustBuffer { FfiConverterOptionalString.write(value.`inviterAlias`, buf) FfiConverterOptionalString.write(value.`initialTribe`, buf) FfiConverterOptionalString.write(value.`lspHost`, buf) + FfiConverterOptionalString.write(value.`invoice`, buf) } } @@ -2294,10 +2309,19 @@ fun `makeMediaTokenWithPrice`(`seed`: String, `uniqueTime`: String, `state`: Byt @Throws(SphinxException::class) -fun `makeInvoice`(`seed`: String, `uniqueTime`: String, `state`: ByteArray, `amtMsat`: ULong, `preimage`: String, `description`: String): String { - return FfiConverterString.lift( +fun `makeInvoice`(`seed`: String, `uniqueTime`: String, `state`: ByteArray, `amtMsat`: ULong, `description`: String): RunReturn { + return FfiConverterTypeRunReturn.lift( + rustCallWithError(SphinxException) { _status -> + _UniFFILib.INSTANCE.uniffi_sphinxrs_fn_func_make_invoice(FfiConverterString.lower(`seed`),FfiConverterString.lower(`uniqueTime`),FfiConverterByteArray.lower(`state`),FfiConverterULong.lower(`amtMsat`),FfiConverterString.lower(`description`),_status) +}) +} + +@Throws(SphinxException::class) + +fun `payInvoice`(`seed`: String, `uniqueTime`: String, `state`: ByteArray, `bolt11`: String, `overpayMsat`: ULong?): RunReturn { + return FfiConverterTypeRunReturn.lift( rustCallWithError(SphinxException) { _status -> - _UniFFILib.INSTANCE.uniffi_sphinxrs_fn_func_make_invoice(FfiConverterString.lower(`seed`),FfiConverterString.lower(`uniqueTime`),FfiConverterByteArray.lower(`state`),FfiConverterULong.lower(`amtMsat`),FfiConverterString.lower(`preimage`),FfiConverterString.lower(`description`),_status) + _UniFFILib.INSTANCE.uniffi_sphinxrs_fn_func_pay_invoice(FfiConverterString.lower(`seed`),FfiConverterString.lower(`uniqueTime`),FfiConverterByteArray.lower(`state`),FfiConverterString.lower(`bolt11`),FfiConverterOptionalULong.lower(`overpayMsat`),_status) }) }