Skip to content

Commit

Permalink
Merge pull request #26 from kumulynja/danger-local-https-fix
Browse files Browse the repository at this point in the history
Danger local https fix
  • Loading branch information
BitcoinZavior authored Jul 20, 2024
2 parents be86545 + e3324ad commit c7c18e7
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 2,163 deletions.
4 changes: 2 additions & 2 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ packages:
dependency: "direct main"
description:
path: "."
ref: "0.31.2"
resolved-ref: "1944db1e3cf8c8e909fb8d22079a1caaec512ab2"
ref: main
resolved-ref: "8a9bd981078eaa8692fc2bed869810acc9918a2c"
url: "https://github.com/LtbLightning/bdk-flutter"
source: git
version: "0.31.2"
Expand Down
9 changes: 4 additions & 5 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: payjoin_flutter_example
description: "Demonstrates how to use the payjoin plugin."
# The following line prevents the package from being accidentally published to
# pub.dev using `flutter pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
publish_to: "none" # Remove this line if you wish to publish to pub.dev

# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
Expand All @@ -19,7 +19,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
version: 1.0.0+1

environment:
sdk: '>=3.2.3 <4.0.0'
sdk: ">=3.2.3 <4.0.0"

# Dependencies specify other packages that your package needs in order to work.
# To automatically upgrade your package dependencies to the latest versions
Expand All @@ -45,8 +45,7 @@ dependencies:
bdk_flutter:
git:
url: https://github.com/LtbLightning/bdk-flutter
ref: 0.31.2

ref: main

google_fonts: ^6.2.1

Expand All @@ -60,4 +59,4 @@ dev_dependencies:
flutter_lints: ^2.0.0

flutter:
uses-material-design: true
uses-material-design: true
11 changes: 5 additions & 6 deletions ios/Classes/frb_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ typedef struct wire_cst_ffi_url {
uintptr_t field0;
} wire_cst_ffi_url;

typedef struct wire_cst_ffi_active_session {
uintptr_t field0;
} wire_cst_ffi_active_session;

typedef struct wire_cst_list_prim_u_8_loose {
uint8_t *ptr;
int32_t len;
} wire_cst_list_prim_u_8_loose;

typedef struct wire_cst_ffi_active_session {
uintptr_t field0;
} wire_cst_ffi_active_session;

typedef struct wire_cst_ffi_client_response {
uintptr_t field0;
} wire_cst_ffi_client_response;
Expand Down Expand Up @@ -292,8 +292,7 @@ typedef struct wire_cst_request_context_v_2 {

void frbgen_payjoin_flutter_wire__crate__api__io__fetch_ohttp_keys(int64_t port_,
struct wire_cst_ffi_url *ohttp_relay,
struct wire_cst_ffi_url *payjoin_directory,
struct wire_cst_list_prim_u_8_loose *cert_der);
struct wire_cst_ffi_url *payjoin_directory);

void frbgen_payjoin_flutter_wire__crate__api__receive__ffi_active_session_extract_req(int64_t port_,
struct wire_cst_ffi_active_session *ptr);
Expand Down
4 changes: 2 additions & 2 deletions lib/receive/v2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class SessionInitializer extends FfiSessionInitializer {
try {
final res = await FfiSessionInitializer.extractReq(ptr: this);
final request =
Request(await Url.fromString(res.$1.$1.query()!), res.$1.$2);
Request(await Url.fromString(res.$1.$1.asString()), res.$1.$2);
return (request, ClientResponse._(field0: res.$2.field0));
} on error.PayjoinError catch (e) {
throw mapPayjoinError(e);
Expand All @@ -60,7 +60,7 @@ class ActiveSession extends FfiActiveSession {
try {
final res = await FfiActiveSession.extractReq(ptr: this);
final request =
Request(await Url.fromString(res.$1.$1.query()!), res.$1.$2);
Request(await Url.fromString(res.$1.$1.asString()), res.$1.$2);
return (request, ClientResponse._(field0: res.$2.field0));
} on error.PayjoinError catch (e) {
throw mapPayjoinError(e);
Expand Down
8 changes: 2 additions & 6 deletions lib/src/generated/api/io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ import 'uri.dart';
/// * `cert_der` (optional): The DER-encoded certificate to use for local HTTPS connections. This
/// parameter is only available when the "danger-local-https" feature is enabled.
Future<FfiOhttpKeys> fetchOhttpKeys(
{required FfiUrl ohttpRelay,
required FfiUrl payjoinDirectory,
required List<int> certDer}) =>
{required FfiUrl ohttpRelay, required FfiUrl payjoinDirectory}) =>
core.instance.api.crateApiIoFetchOhttpKeys(
ohttpRelay: ohttpRelay,
payjoinDirectory: payjoinDirectory,
certDer: certDer);
ohttpRelay: ohttpRelay, payjoinDirectory: payjoinDirectory);
16 changes: 5 additions & 11 deletions lib/src/generated/frb_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ class core extends BaseEntrypoint<coreApi, coreApiImpl, coreWire> {

abstract class coreApi extends BaseApi {
Future<FfiOhttpKeys> crateApiIoFetchOhttpKeys(
{required FfiUrl ohttpRelay,
required FfiUrl payjoinDirectory,
required List<int> certDer});
{required FfiUrl ohttpRelay, required FfiUrl payjoinDirectory});

Future<((FfiUrl, Uint8List), FfiClientResponse)>
crateApiReceiveFfiActiveSessionExtractReq(
Expand Down Expand Up @@ -580,30 +578,26 @@ class coreApiImpl extends coreApiImplPlatform implements coreApi {

@override
Future<FfiOhttpKeys> crateApiIoFetchOhttpKeys(
{required FfiUrl ohttpRelay,
required FfiUrl payjoinDirectory,
required List<int> certDer}) {
{required FfiUrl ohttpRelay, required FfiUrl payjoinDirectory}) {
return handler.executeNormal(NormalTask(
callFfi: (port_) {
var arg0 = cst_encode_box_autoadd_ffi_url(ohttpRelay);
var arg1 = cst_encode_box_autoadd_ffi_url(payjoinDirectory);
var arg2 = cst_encode_list_prim_u_8_loose(certDer);
return wire.wire__crate__api__io__fetch_ohttp_keys(
port_, arg0, arg1, arg2);
return wire.wire__crate__api__io__fetch_ohttp_keys(port_, arg0, arg1);
},
codec: DcoCodec(
decodeSuccessData: dco_decode_ffi_ohttp_keys,
decodeErrorData: dco_decode_payjoin_error,
),
constMeta: kCrateApiIoFetchOhttpKeysConstMeta,
argValues: [ohttpRelay, payjoinDirectory, certDer],
argValues: [ohttpRelay, payjoinDirectory],
apiImpl: this,
));
}

TaskConstMeta get kCrateApiIoFetchOhttpKeysConstMeta => const TaskConstMeta(
debugName: "fetch_ohttp_keys",
argNames: ["ohttpRelay", "payjoinDirectory", "certDer"],
argNames: ["ohttpRelay", "payjoinDirectory"],
);

@override
Expand Down
26 changes: 9 additions & 17 deletions lib/src/generated/frb_generated.io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2638,31 +2638,23 @@ class coreWire implements BaseWire {
int port_,
ffi.Pointer<wire_cst_ffi_url> ohttp_relay,
ffi.Pointer<wire_cst_ffi_url> payjoin_directory,
ffi.Pointer<wire_cst_list_prim_u_8_loose> cert_der,
) {
return _wire__crate__api__io__fetch_ohttp_keys(
port_,
ohttp_relay,
payjoin_directory,
cert_der,
);
}

late final _wire__crate__api__io__fetch_ohttp_keysPtr = _lookup<
ffi.NativeFunction<
ffi.Void Function(
ffi.Int64,
ffi.Pointer<wire_cst_ffi_url>,
ffi.Pointer<wire_cst_ffi_url>,
ffi.Pointer<wire_cst_list_prim_u_8_loose>)>>(
ffi.Void Function(ffi.Int64, ffi.Pointer<wire_cst_ffi_url>,
ffi.Pointer<wire_cst_ffi_url>)>>(
'frbgen_payjoin_flutter_wire__crate__api__io__fetch_ohttp_keys');
late final _wire__crate__api__io__fetch_ohttp_keys =
_wire__crate__api__io__fetch_ohttp_keysPtr.asFunction<
void Function(
int,
ffi.Pointer<wire_cst_ffi_url>,
ffi.Pointer<wire_cst_ffi_url>,
ffi.Pointer<wire_cst_list_prim_u_8_loose>)>();
void Function(int, ffi.Pointer<wire_cst_ffi_url>,
ffi.Pointer<wire_cst_ffi_url>)>();

void wire__crate__api__receive__ffi_active_session_extract_req(
int port_,
Expand Down Expand Up @@ -5735,18 +5727,18 @@ final class wire_cst_ffi_url extends ffi.Struct {
external int field0;
}

final class wire_cst_ffi_active_session extends ffi.Struct {
@ffi.UintPtr()
external int field0;
}

final class wire_cst_list_prim_u_8_loose extends ffi.Struct {
external ffi.Pointer<ffi.Uint8> ptr;

@ffi.Int32()
external int len;
}

final class wire_cst_ffi_active_session extends ffi.Struct {
@ffi.UintPtr()
external int field0;
}

final class wire_cst_ffi_client_response extends ffi.Struct {
@ffi.UintPtr()
external int field0;
Expand Down
14 changes: 7 additions & 7 deletions lib/uri.dart
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,15 @@ class OhttpKeys extends FfiOhttpKeys {
///
/// * `certDer` (optional): The DER-encoded certificate to use for local HTTPS connections. This
/// parameter is only available when the "danger-local-https" feature is enabled.
Future<OhttpKeys> fetchOhttpKeys(
{required Url ohttpRelay,
required Url payjoinDirectory,
required List<int> certDer}) async {
Future<OhttpKeys> fetchOhttpKeys({
required Url ohttpRelay,
required Url payjoinDirectory,
}) async {
try {
final res = await io.fetchOhttpKeys(
ohttpRelay: ohttpRelay,
payjoinDirectory: payjoinDirectory,
certDer: certDer);
ohttpRelay: ohttpRelay,
payjoinDirectory: payjoinDirectory,
);
return OhttpKeys._(field0: res.field0);
} on error.PayjoinError catch (e) {
throw mapPayjoinError(e);
Expand Down
6 changes: 3 additions & 3 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ edition = "2021"
crate-type = ["cdylib", "staticlib"]

[dev-dependencies]
bdk = { version = "0.29.0", features = ["all-keys", "use-esplora-ureq", "keys-bip39"] }
bdk = { version = "0.29.0", features = [
"all-keys",
"use-esplora-ureq",
"keys-bip39",
] }
bitcoincore-rpc = "0.19.0"
anyhow = "1.0.68"
[dependencies]
ohttp = { version = "0.5.1" }
payjoin_ffi = {git = "https://github.com/LtbLightning/payjoin-ffi", branch="v0.18.0"}
payjoin_ffi = { git = "https://github.com/LtbLightning/payjoin-ffi", branch = "main" }
flutter_rust_bridge = "=2.0.0"
anyhow = "1.0.68"
tokio = "1.36.0"
Expand All @@ -24,3 +28,6 @@ opt-level = "z"
lto = true
codegen-units = 1
panic = "abort"

[features]
danger-local-https = ["payjoin_ffi/enable-danger-local-https"]
14 changes: 8 additions & 6 deletions rust/src/api/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ use crate::utils::error::PayjoinError;
pub async fn fetch_ohttp_keys(
ohttp_relay: FfiUrl,
payjoin_directory: FfiUrl,
cert_der: Vec<u8>,
#[cfg(feature = "danger-local-https")] cert_der: Vec<u8>,
) -> Result<FfiOhttpKeys, PayjoinError> {
payjoin_ffi::io::fetch_ohttp_keys(
#[cfg(not(feature = "danger-local-https"))]
let res =
payjoin_ffi::io::fetch_ohttp_keys((*ohttp_relay.0).clone(), (*payjoin_directory.0).clone());
#[cfg(feature = "danger-local-https")]
let res = payjoin_ffi::io::fetch_ohttp_keys(
(*ohttp_relay.0).clone(),
(*payjoin_directory.0).clone(),
cert_der,
)
.await
.map(|e| e.into())
.map_err(|e| e.into())
);
res.await.map(|e| e.into()).map_err(|e| e.into())
}
19 changes: 16 additions & 3 deletions rust/src/api/uri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,13 @@ mod tests {
let bech32_upper = "TB1Q6D3A2W975YNY0ASUVD9A67NER4NKS58FF0Q8G4";
let bech32_lower = "tb1q6d3a2w975yny0asuvd9a67ner4nks58ff0q8g4";

for address in vec![base58, bech32_upper, bech32_lower] {
for pj in vec![https, onion] {
let builder = FfiPjUriBuilder::new(
for address in [base58, bech32_upper, bech32_lower] {
for pj in [https, onion] {
let builder = FfiPjUriBuilder::create(
address.to_string(),
FfiUrl::from_str(pj.to_string()).unwrap(),
None,
None,
)
.unwrap();
let uri = builder
Expand All @@ -193,6 +194,18 @@ mod tests {
.build();
// assert_eq!(uri.amount(), Some(bitcoin::Amount::ONE_BTC.to_btc()));
print!("\n {}", uri.as_string());
let expected_address = if address == base58 {
base58
} else {
bech32_lower
};
assert_eq!(
uri.as_string(),
format!(
"bitcoin:{}?amount=0.00000001&label=label&message=message&pj={}&pjos=1",
expected_address, pj
),
);
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions rust/src/frb_generated.io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1321,9 +1321,8 @@ pub extern "C" fn frbgen_payjoin_flutter_wire__crate__api__io__fetch_ohttp_keys(
port_: i64,
ohttp_relay: *mut wire_cst_ffi_url,
payjoin_directory: *mut wire_cst_ffi_url,
cert_der: *mut wire_cst_list_prim_u_8_loose,
) {
wire__crate__api__io__fetch_ohttp_keys_impl(port_, ohttp_relay, payjoin_directory, cert_der)
wire__crate__api__io__fetch_ohttp_keys_impl(port_, ohttp_relay, payjoin_directory)
}

#[no_mangle]
Expand Down
3 changes: 0 additions & 3 deletions rust/src/frb_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ fn wire__crate__api__io__fetch_ohttp_keys_impl(
port_: flutter_rust_bridge::for_generated::MessagePort,
ohttp_relay: impl CstDecode<crate::api::uri::FfiUrl>,
payjoin_directory: impl CstDecode<crate::api::uri::FfiUrl>,
cert_der: impl CstDecode<Vec<u8>>,
) {
FLUTTER_RUST_BRIDGE_HANDLER.wrap_async::<flutter_rust_bridge::for_generated::DcoCodec, _, _, _>(
flutter_rust_bridge::for_generated::TaskInfo {
Expand All @@ -61,14 +60,12 @@ fn wire__crate__api__io__fetch_ohttp_keys_impl(
move || {
let api_ohttp_relay = ohttp_relay.cst_decode();
let api_payjoin_directory = payjoin_directory.cst_decode();
let api_cert_der = cert_der.cst_decode();
move |context| async move {
transform_result_dco::<_, _, crate::utils::error::PayjoinError>(
(move || async move {
let output_ok = crate::api::io::fetch_ohttp_keys(
api_ohttp_relay,
api_payjoin_directory,
api_cert_der,
)
.await?;
Ok(output_ok)
Expand Down
Loading

0 comments on commit c7c18e7

Please sign in to comment.