Skip to content

Commit

Permalink
Merge pull request #30 from kumulynja/override-ffi-functions
Browse files Browse the repository at this point in the history
Override ffi functions
  • Loading branch information
BitcoinZavior authored Aug 15, 2024
2 parents 0d89b4d + e9fe95c commit 6bce23c
Show file tree
Hide file tree
Showing 23 changed files with 1,261 additions and 1,269 deletions.
4 changes: 2 additions & 2 deletions example/integration_test/bdk_full_cycle_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void main() {
await sender.syncWallet();
// Sender create a funded PSBT (not broadcast) to address with amount given in the pjUri
debugPrint("Sender Balance: ${sender.getBalance().toString()}");
final uri = await pay_join_uri.Uri.fromString(
final uri = await pay_join_uri.Uri.fromStr(
"${pjReceiverAddress.toQrUri()}?amount=${0.0083285}&pj=https://example.com");
final address = uri.address();
int amount = (((uri.amount()) ?? 0) * 100000000).toInt();
Expand All @@ -55,7 +55,7 @@ void main() {
maxFeeContribution: BigInt.from(10000),
minFeeRate: BigInt.zero,
clampFeeContribution: false))
.extractContextV1();
.extractV1();
final headers = common.Headers(map: {
'content-type': 'text/plain',
'content-length': req.body.length.toString(),
Expand Down
2 changes: 1 addition & 1 deletion example/integration_test/bitcoin_core_full_cycle_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void main() {
final pjUri = await payJoinLib.buildPjUri(0.0083285, pjReceiverAddress);
// Sender create a funded PSBT (not broadcast) to address with amount given in the pjUri
debugPrint("Sender Balance: ${(await sender.getBalance()).toString()}");
final uri = await pay_join_uri.Uri.fromString(pjUri);
final uri = await pay_join_uri.Uri.fromStr(pjUri);
final address = await uri.address();
final amount = await uri.amount();
final senderPsbt =
Expand Down
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class _PayJoinState extends State<PayJoin> {
onPressed: () async {
final balance = await sender.getBalance();
debugPrint("Sender Balance: ${balance.toString()}");
final uri = await pay_join_uri.Uri.fromString(pjUri);
final uri = await pay_join_uri.Uri.fromStr(pjUri);
final address = await uri.address();
int amount =
(((await uri.amount()) ?? 0) * 100000000).toInt();
Expand Down
6 changes: 3 additions & 3 deletions example/lib/payjoin_library.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PayJoinLibrary {
Future<String> buildPjUri(double amount, String address, {String? pj}) async {
try {
final pjUri = "$address?amount=$amount&pj=${pj ?? pjUrl}";
await pj_uri.Uri.fromString(pjUri);
await pj_uri.Uri.fromStr(pjUri);
return pjUri;
} catch (e) {
debugPrint(e.toString());
Expand All @@ -26,14 +26,14 @@ class PayJoinLibrary {
String psbtBase64,
String uriStr,
Future<bool> Function(Uint8List) isOwned) async {
final uri = await pj_uri.Uri.fromString(uriStr);
final uri = await pj_uri.Uri.fromStr(uriStr);
final (req, cxt) = await (await (await send.RequestBuilder.fromPsbtAndUri(
psbtBase64: psbtBase64, pjUri: uri.checkPjSupported()))
.buildWithAdditionalFee(
maxFeeContribution: BigInt.from(10000),
minFeeRate: BigInt.zero,
clampFeeContribution: false))
.extractContextV1();
.extractV1();
final headers = common.Headers(map: {
'content-type': 'text/plain',
'content-length': req.body.length.toString(),
Expand Down
99 changes: 49 additions & 50 deletions ios/Classes/frb_generated.h

Large diffs are not rendered by default.

9 changes: 0 additions & 9 deletions lib/common.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,2 @@
import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart';
import 'package:payjoin_flutter/uri.dart';

export 'src/exceptions.dart' hide mapPayjoinError, ExceptionBase;
export 'src/generated/utils/types.dart';

class Request {
final Url url;
final Uint8List body;
Request(this.url, this.body);
}
44 changes: 24 additions & 20 deletions lib/receive/v1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ class UncheckedProposal extends FfiUncheckedProposal {
/// Receiver MUST check that the Original PSBT from the sender can be broadcast, i.e. testmempoolaccept bitcoind rpc returns { “allowed”: true,.. } for gettransactiontocheckbroadcast() before calling this method.
/// Do this check if you generate bitcoin uri to receive Payjoin on sender request without manual human approval, like a payment processor. Such so called “non-interactive” receivers are otherwise vulnerable to probing attacks. If a sender can make requests at will, they can learn which bitcoin the receiver owns at no cost. Broadcasting the Original PSBT after some time in the failure case makes incurs sender cost and prevents probing.
/// Call this after checking downstream.
@override
Future<MaybeInputsOwned> checkBroadcastSuitability(
{BigInt? minFeeRate,
required FutureOr<bool> Function(Uint8List) canBroadcast,
hint}) async {
try {
final res = await FfiUncheckedProposal.checkBroadcastSuitability(
minFeeRate: minFeeRate, canBroadcast: canBroadcast, ptr: this);
final res = await super.checkBroadcastSuitability(
minFeeRate: minFeeRate, canBroadcast: canBroadcast);
return MaybeInputsOwned._(field0: res.field0);
} on error.PayjoinError catch (e) {
throw mapPayjoinError(e);
Expand All @@ -56,10 +57,10 @@ class UncheckedProposal extends FfiUncheckedProposal {
///Call this method if the only way to initiate a `Payjoin` with this receiver requires manual intervention, as in most consumer wallets.
/// So-called “non-interactive” receivers, like payment processors,
/// that allow arbitrary requests are otherwise vulnerable to probing attacks.
@override
Future<MaybeInputsOwned> assumeInteractiveReceiver() async {
try {
final res =
await FfiUncheckedProposal.assumeInteractiveReceiver(ptr: this);
final res = await super.assumeInteractiveReceiver();
return MaybeInputsOwned._(field0: res.field0);
} on error.PayjoinError catch (e) {
throw mapPayjoinError(e);
Expand All @@ -69,11 +70,11 @@ class UncheckedProposal extends FfiUncheckedProposal {

class MaybeInputsOwned extends FfiMaybeInputsOwned {
MaybeInputsOwned._({required super.field0});
@override
Future<MaybeMixedInputScripts> checkInputsNotOwned(
{required FutureOr<bool> Function(Uint8List) isOwned}) async {
try {
final res = await FfiMaybeInputsOwned.checkInputsNotOwned(
ptr: this, isOwned: isOwned);
final res = await super.checkInputsNotOwned(isOwned: isOwned);
return MaybeMixedInputScripts._(field0: res.field0);
} on error.PayjoinError catch (e) {
throw mapPayjoinError(e);
Expand All @@ -87,10 +88,10 @@ class MaybeMixedInputScripts extends FfiMaybeMixedInputScripts {
/// Verify the original transaction did not have mixed input types Call this after checking downstream.
///
/// Note: mixed spends do not necessarily indicate distinct wallet fingerprints. This check is intended to prevent some types of wallet fingerprinting.
@override
Future<MaybeInputsSeen> checkNoMixedInputScripts() async {
try {
final res =
await FfiMaybeMixedInputScripts.checkNoMixedInputScripts(ptr: this);
final res = await super.checkNoMixedInputScripts();
return MaybeInputsSeen._(field0: res.field0);
} on error.PayjoinError catch (e) {
throw mapPayjoinError(e);
Expand All @@ -100,11 +101,11 @@ class MaybeMixedInputScripts extends FfiMaybeMixedInputScripts {

class MaybeInputsSeen extends FfiMaybeInputsSeen {
MaybeInputsSeen._({required super.field0});
@override
Future<OutputsUnknown> checkNoInputsSeenBefore(
{required FutureOr<bool> Function(OutPoint) isKnown}) async {
try {
final res = await FfiMaybeInputsSeen.checkNoInputsSeenBefore(
ptr: this, isKnown: isKnown);
final res = await super.checkNoInputsSeenBefore(isKnown: isKnown);
return OutputsUnknown._(field0: res.field0);
} on error.PayjoinError catch (e) {
throw mapPayjoinError(e);
Expand All @@ -114,11 +115,12 @@ class MaybeInputsSeen extends FfiMaybeInputsSeen {

class OutputsUnknown extends FfiOutputsUnknown {
OutputsUnknown._({required super.field0});
@override
Future<ProvisionalProposal> identifyReceiverOutputs(
{required Future<bool> Function(Uint8List) isReceiverOutput}) async {
{required FutureOr<bool> Function(Uint8List) isReceiverOutput}) async {
try {
final res = await FfiOutputsUnknown.identifyReceiverOutputs(
ptr: this, isReceiverOutput: isReceiverOutput);
final res = await super
.identifyReceiverOutputs(isReceiverOutput: isReceiverOutput);
return ProvisionalProposal._(field0: res.field0);
} on error.PayjoinError catch (e) {
throw mapPayjoinError(e);
Expand Down Expand Up @@ -174,14 +176,16 @@ class ProvisionalProposal extends FfiProvisionalProposal {
}
}

Future<PayjoinProposal> finalizeProposal(
{required FutureOr<String> Function(String) processPsbt,
BigInt? minFeeRateSatPerVb}) async {
@override
Future<PayjoinProposal> finalizeProposal({
required FutureOr<String> Function(String) processPsbt,
BigInt? minFeeRateSatPerVb,
}) async {
try {
final res = await FfiProvisionalProposal.finalizeProposal(
processPsbt: processPsbt,
minFeerateSatPerVb: minFeeRateSatPerVb,
ptr: this);
final res = await super.finalizeProposal(
processPsbt: processPsbt,
minFeeRateSatPerVb: minFeeRateSatPerVb,
);
return PayjoinProposal._(field0: res.field0);
} on error.PayjoinError catch (e) {
throw mapPayjoinError(e);
Expand Down
75 changes: 43 additions & 32 deletions lib/receive/v2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,26 @@ class SessionInitializer extends FfiSessionInitializer {
}
}

Future<ActiveSession> processResponse(
{required List<int> body, required ClientResponse clientResponse}) async {
@override
Future<ActiveSession> processRes(
{required List<int> body, required ClientResponse ctx}) async {
try {
final res = await super.processRes(body: body, ctx: clientResponse);
final res = await super.processRes(body: body, ctx: ctx);
return ActiveSession._(field0: res.field0);
} on error.PayjoinError catch (e) {
throw mapPayjoinError(e);
}
}

Future<(Request, ClientResponse)> extractRequest({hint}) async {
@override
Future<(Request, ClientResponse)> extractReq() async {
try {
final res = await FfiSessionInitializer.extractReq(ptr: this);
final request =
Request(await Url.fromString(res.$1.$1.asString()), res.$1.$2);
return (request, ClientResponse._(field0: res.$2.field0));
final res = await super.extractReq();
final request = Request(
url: await Url.fromStr(res.$1.url.asString()),
body: res.$1.body,
);
return (request, res.$2);
} on error.PayjoinError catch (e) {
throw mapPayjoinError(e);
}
Expand All @@ -56,21 +60,26 @@ class SessionInitializer extends FfiSessionInitializer {

class ActiveSession extends FfiActiveSession {
ActiveSession._({required super.field0});
Future<(Request, ClientResponse)> extractRequest({hint}) async {

@override
Future<(Request, ClientResponse)> extractReq() async {
try {
final res = await FfiActiveSession.extractReq(ptr: this);
final request =
Request(await Url.fromString(res.$1.$1.asString()), res.$1.$2);
return (request, ClientResponse._(field0: res.$2.field0));
final res = await super.extractReq();
final request = Request(
url: await Url.fromStr(res.$1.url.asString()),
body: res.$1.body,
);
return (request, res.$2);
} on error.PayjoinError catch (e) {
throw mapPayjoinError(e);
}
}

Future<UncheckedProposal?> processResponse(
{required List<int> body, required ClientResponse clientResponse}) async {
@override
Future<UncheckedProposal?> processRes(
{required List<int> body, required ClientResponse ctx}) async {
try {
final res = await super.processRes(body: body, ctx: clientResponse);
final res = await super.processRes(body: body, ctx: ctx);
if (res != null) {
return UncheckedProposal._(field0: res.field0);
} else {
Expand All @@ -83,13 +92,15 @@ class ActiveSession extends FfiActiveSession {

/// The contents of the `&pj=` query parameter including the base64url-encoded public key receiver subdirectory.
/// This identifies a session at the payjoin directory server.
Future<Url> pjUrl() {
final res = FfiActiveSession.pjUrl(ptr: this);
return Url.fromString(res.asString());
@override
Future<Url> pjUrl() async {
final res = await super.pjUrl();
return Url.fromStr(res.asString());
}

@override
PjUriBuilder pjUriBuilder() {
final res = FfiActiveSession.pjUriBuilder(ptr: this);
final res = super.pjUriBuilder();
return PjUriBuilder(internal: res.internal);
}
}
Expand Down Expand Up @@ -260,11 +271,11 @@ class ProvisionalProposal extends FfiV2ProvisionalProposal {
@override
Future<PayjoinProposal> finalizeProposal(
{required FutureOr<String> Function(String p1) processPsbt,
BigInt? minFeerateSatPerVb,
BigInt? minFeeRateSatPerVb,
hint}) async {
try {
final res = await super.finalizeProposal(
processPsbt: processPsbt, minFeerateSatPerVb: minFeerateSatPerVb);
processPsbt: processPsbt, minFeeRateSatPerVb: minFeeRateSatPerVb);
return PayjoinProposal._(field0: res.field0);
} on error.PayjoinError catch (e) {
throw mapPayjoinError(e);
Expand All @@ -275,7 +286,8 @@ class ProvisionalProposal extends FfiV2ProvisionalProposal {
class PayjoinProposal extends FfiV2PayjoinProposal {
PayjoinProposal._({required super.field0});

Future<void> processResponse(
@override
Future<void> processRes(
{required List<int> res, required ClientResponse ohttpContext}) {
try {
return super.processRes(ohttpContext: ohttpContext, res: res);
Expand All @@ -293,12 +305,15 @@ class PayjoinProposal extends FfiV2PayjoinProposal {
}
}

Future<(Request, ClientResponse)> extractV2Request({hint}) async {
@override
Future<(Request, ClientResponse)> extractV2Req() async {
try {
final res = await FfiV2PayjoinProposal.extractV2Req(ptr: this);
final request =
Request(await Url.fromString(res.$1.$1.asString()), res.$1.$2);
return (request, ClientResponse._(field0: res.$2.field0));
final res = await super.extractV2Req();
final request = Request(
url: await Url.fromStr(res.$1.url.asString()),
body: res.$1.body,
);
return (request, res.$2);
} on error.PayjoinError catch (e) {
throw mapPayjoinError(e);
}
Expand Down Expand Up @@ -340,7 +355,3 @@ class PayjoinProposal extends FfiV2PayjoinProposal {
}
}
}

class ClientResponse extends FfiClientResponse {
ClientResponse._({required super.field0});
}
29 changes: 19 additions & 10 deletions lib/send.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:payjoin_flutter/uri.dart';

import 'common.dart' as common;
import 'src/generated/api/send.dart';
import 'src/generated/api/uri.dart';
import 'src/generated/utils/error.dart' as error;

class RequestBuilder extends FfiRequestBuilder {
Expand Down Expand Up @@ -75,25 +76,33 @@ class RequestBuilder extends FfiRequestBuilder {
class RequestContext extends FfiRequestContext {
RequestContext._({required super.field0});

Future<(common.Request, ContextV1)> extractContextV1() async {
@override
Future<(common.Request, ContextV1)> extractV1() async {
try {
final res = await FfiRequestContext.extractV1(ptr: this);
final res = await super.extractV1();
final request = common.Request(
await Url.fromString((res.request.$1.asString())), res.request.$2);
return (request, ContextV1._(field0: res.contextV1.field0));
url: await Url.fromStr((res.$1.url.asString())),
body: res.$1.body,
);
return (request, ContextV1._(field0: res.$2.field0));
} on error.PayjoinError catch (e) {
throw mapPayjoinError(e);
}
}

Future<(common.Request, ContextV2)> extractContextV2(
Url ohttpProxyUrl) async {
@override
Future<(common.Request, ContextV2)> extractV2({
required FfiUrl ohttpProxyUrl,
}) async {
try {
final res = await FfiRequestContext.extractV2(
ohttpProxyUrl: ohttpProxyUrl, ptr: this);
final res = await super.extractV2(
ohttpProxyUrl: ohttpProxyUrl,
);
final request = common.Request(
await Url.fromString((res.request.$1.asString())), res.request.$2);
return (request, ContextV2._(field0: res.contextV2.field0));
url: await Url.fromStr((res.$1.url.asString())),
body: res.$1.body,
);
return (request, ContextV2._(field0: res.$2.field0));
} on error.PayjoinError catch (e) {
throw mapPayjoinError(e);
}
Expand Down
Loading

0 comments on commit 6bce23c

Please sign in to comment.