Skip to content

Commit

Permalink
Refactor Sp Wallet (#65)
Browse files Browse the repository at this point in the history
* Merge outpoints list with Sp Wallet

* Move scanning logic to wallet

* Use bitcoin structs where possible

* Provide start and end as arguments for scan_blocks

* Add SpScanner with Updater that processes results

* Add owned_outpoints list to scanner

* Split get block data stream into separate function

* Refactor SpScanner, add process_blocks function

* Add Updater trait that is implemented by WalletUpdater

* Remove unused update_wallet_with_transaction

* Move mnemonic and label to SpWallet from SpClient

* Add ChainBackend trait implemented by BlindbitBackend

* Move SpScanner, ChainBackend, Updater to sp_client lib

* Move blindbit backend to sp-client behind feature
  • Loading branch information
cygnet3 authored Sep 30, 2024
1 parent f9f432b commit fcc8e58
Show file tree
Hide file tree
Showing 26 changed files with 555 additions and 1,321 deletions.
1 change: 1 addition & 0 deletions lib/generated/rust/api/psbt.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// ignore_for_file: invalid_use_of_internal_member, unused_import, unnecessary_import

import '../frb_generated.dart';
import '../lib.dart';
import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart';
import 'structs.dart';

Expand Down
3 changes: 2 additions & 1 deletion lib/generated/rust/api/structs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// ignore_for_file: invalid_use_of_internal_member, unused_import, unnecessary_import

import '../frb_generated.dart';
import '../lib.dart';
import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart';
import 'package:freezed_annotation/freezed_annotation.dart' hide protected;
part 'structs.freezed.dart';
Expand Down Expand Up @@ -47,7 +48,7 @@ sealed class OutputSpendStatus with _$OutputSpendStatus {

class OwnedOutput {
final int blockheight;
final String tweak;
final U8Array32 tweak;
final Amount amount;
final String script;
final String? label;
Expand Down
14 changes: 2 additions & 12 deletions lib/generated/rust/api/wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// ignore_for_file: invalid_use_of_internal_member, unused_import, unnecessary_import

import '../frb_generated.dart';
import '../lib.dart';
import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart';
import 'structs.dart';

Expand Down Expand Up @@ -34,7 +35,7 @@ String resetWallet({required String encodedWallet}) => RustLib.instance.api

Future<void> scanToTip(
{required String blindbitUrl,
required int dustLimit,
required BigInt dustLimit,
required String encodedWallet}) =>
RustLib.instance.api.crateApiWalletScanToTip(
blindbitUrl: blindbitUrl,
Expand Down Expand Up @@ -65,16 +66,5 @@ String addOutgoingTxToHistory(
recipients: recipients,
change: change);

String addIncomingTxToHistory(
{required String encodedWallet,
required String txid,
required Amount amount,
required int height}) =>
RustLib.instance.api.crateApiWalletAddIncomingTxToHistory(
encodedWallet: encodedWallet,
txid: txid,
amount: amount,
height: height);

String? showMnemonic({required String encodedWallet}) => RustLib.instance.api
.crateApiWalletShowMnemonic(encodedWallet: encodedWallet);
87 changes: 35 additions & 52 deletions lib/generated/rust/frb_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'dart:convert';
import 'frb_generated.dart';
import 'frb_generated.io.dart'
if (dart.library.js_interop) 'frb_generated.web.dart';
import 'lib.dart';
import 'logger.dart';
import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart';
import 'stream.dart';
Expand Down Expand Up @@ -74,7 +75,7 @@ class RustLib extends BaseEntrypoint<RustLibApi, RustLibApiImpl, RustLibWire> {
String get codegenVersion => '2.3.0';

@override
int get rustContentHash => -154598911;
int get rustContentHash => 141683958;

static const kDefaultExternalLibraryLoaderConfig =
ExternalLibraryLoaderConfig(
Expand Down Expand Up @@ -128,12 +129,6 @@ abstract class RustLibApi extends BaseApi {
String crateApiStructsRecordedTransactionOutgoingToString(
{required RecordedTransactionOutgoing that});

String crateApiWalletAddIncomingTxToHistory(
{required String encodedWallet,
required String txid,
required Amount amount,
required int height});

String crateApiWalletAddOutgoingTxToHistory(
{required String encodedWallet,
required String txid,
Expand All @@ -155,7 +150,7 @@ abstract class RustLibApi extends BaseApi {

Future<void> crateApiWalletScanToTip(
{required String blindbitUrl,
required int dustLimit,
required BigInt dustLimit,
required String encodedWallet});

Future<String> crateApiWalletSetup(
Expand Down Expand Up @@ -565,37 +560,6 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
argNames: ["that"],
);

@override
String crateApiWalletAddIncomingTxToHistory(
{required String encodedWallet,
required String txid,
required Amount amount,
required int height}) {
return handler.executeSync(SyncTask(
callFfi: () {
final serializer = SseSerializer(generalizedFrbRustBinding);
sse_encode_String(encodedWallet, serializer);
sse_encode_String(txid, serializer);
sse_encode_box_autoadd_amount(amount, serializer);
sse_encode_u_32(height, serializer);
return pdeCallFfi(generalizedFrbRustBinding, serializer, funcId: 16)!;
},
codec: SseCodec(
decodeSuccessData: sse_decode_String,
decodeErrorData: sse_decode_AnyhowException,
),
constMeta: kCrateApiWalletAddIncomingTxToHistoryConstMeta,
argValues: [encodedWallet, txid, amount, height],
apiImpl: this,
));
}

TaskConstMeta get kCrateApiWalletAddIncomingTxToHistoryConstMeta =>
const TaskConstMeta(
debugName: "add_incoming_tx_to_history",
argNames: ["encodedWallet", "txid", "amount", "height"],
);

@override
String crateApiWalletAddOutgoingTxToHistory(
{required String encodedWallet,
Expand All @@ -611,7 +575,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
sse_encode_list_String(spentOutpoints, serializer);
sse_encode_list_recipient(recipients, serializer);
sse_encode_box_autoadd_amount(change, serializer);
return pdeCallFfi(generalizedFrbRustBinding, serializer, funcId: 17)!;
return pdeCallFfi(generalizedFrbRustBinding, serializer, funcId: 16)!;
},
codec: SseCodec(
decodeSuccessData: sse_decode_String,
Expand Down Expand Up @@ -643,7 +607,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
final serializer = SseSerializer(generalizedFrbRustBinding);
sse_encode_String(encodedWallet, serializer);
sse_encode_u_32(birthday, serializer);
return pdeCallFfi(generalizedFrbRustBinding, serializer, funcId: 18)!;
return pdeCallFfi(generalizedFrbRustBinding, serializer, funcId: 17)!;
},
codec: SseCodec(
decodeSuccessData: sse_decode_String,
Expand All @@ -667,7 +631,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
callFfi: () {
final serializer = SseSerializer(generalizedFrbRustBinding);
sse_encode_String(encodedWallet, serializer);
return pdeCallFfi(generalizedFrbRustBinding, serializer, funcId: 19)!;
return pdeCallFfi(generalizedFrbRustBinding, serializer, funcId: 18)!;
},
codec: SseCodec(
decodeSuccessData: sse_decode_wallet_status,
Expand Down Expand Up @@ -696,7 +660,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
sse_encode_String(encodedWallet, serializer);
sse_encode_String(spentBy, serializer);
sse_encode_list_String(spent, serializer);
return pdeCallFfi(generalizedFrbRustBinding, serializer, funcId: 20)!;
return pdeCallFfi(generalizedFrbRustBinding, serializer, funcId: 19)!;
},
codec: SseCodec(
decodeSuccessData: sse_decode_String,
Expand All @@ -720,7 +684,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
callFfi: () {
final serializer = SseSerializer(generalizedFrbRustBinding);
sse_encode_String(encodedWallet, serializer);
return pdeCallFfi(generalizedFrbRustBinding, serializer, funcId: 21)!;
return pdeCallFfi(generalizedFrbRustBinding, serializer, funcId: 20)!;
},
codec: SseCodec(
decodeSuccessData: sse_decode_String,
Expand All @@ -740,16 +704,16 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
@override
Future<void> crateApiWalletScanToTip(
{required String blindbitUrl,
required int dustLimit,
required BigInt dustLimit,
required String encodedWallet}) {
return handler.executeNormal(NormalTask(
callFfi: (port_) {
final serializer = SseSerializer(generalizedFrbRustBinding);
sse_encode_String(blindbitUrl, serializer);
sse_encode_u_32(dustLimit, serializer);
sse_encode_u_64(dustLimit, serializer);
sse_encode_String(encodedWallet, serializer);
pdeCallFfi(generalizedFrbRustBinding, serializer,
funcId: 22, port: port_);
funcId: 21, port: port_);
},
codec: SseCodec(
decodeSuccessData: sse_decode_unit,
Expand Down Expand Up @@ -784,7 +748,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
sse_encode_u_32(birthday, serializer);
sse_encode_String(network, serializer);
pdeCallFfi(generalizedFrbRustBinding, serializer,
funcId: 23, port: port_);
funcId: 22, port: port_);
},
codec: SseCodec(
decodeSuccessData: sse_decode_String,
Expand Down Expand Up @@ -814,7 +778,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
callFfi: () {
final serializer = SseSerializer(generalizedFrbRustBinding);
sse_encode_String(encodedWallet, serializer);
return pdeCallFfi(generalizedFrbRustBinding, serializer, funcId: 24)!;
return pdeCallFfi(generalizedFrbRustBinding, serializer, funcId: 23)!;
},
codec: SseCodec(
decodeSuccessData: sse_decode_opt_String,
Expand Down Expand Up @@ -1029,7 +993,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
throw Exception('unexpected arr length: expect 6 but see ${arr.length}');
return OwnedOutput(
blockheight: dco_decode_u_32(arr[0]),
tweak: dco_decode_String(arr[1]),
tweak: dco_decode_u_8_array_32(arr[1]),
amount: dco_decode_amount(arr[2]),
script: dco_decode_String(arr[3]),
label: dco_decode_opt_String(arr[4]),
Expand Down Expand Up @@ -1166,6 +1130,12 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
return raw as int;
}

@protected
U8Array32 dco_decode_u_8_array_32(dynamic raw) {
// Codec=Dco (DartCObject based), see doc to use other codecs
return U8Array32(dco_decode_list_prim_u_8_strict(raw));
}

@protected
void dco_decode_unit(dynamic raw) {
// Codec=Dco (DartCObject based), see doc to use other codecs
Expand Down Expand Up @@ -1432,7 +1402,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
OwnedOutput sse_decode_owned_output(SseDeserializer deserializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
var var_blockheight = sse_decode_u_32(deserializer);
var var_tweak = sse_decode_String(deserializer);
var var_tweak = sse_decode_u_8_array_32(deserializer);
var var_amount = sse_decode_amount(deserializer);
var var_script = sse_decode_String(deserializer);
var var_label = sse_decode_opt_String(deserializer);
Expand Down Expand Up @@ -1556,6 +1526,13 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
return deserializer.buffer.getUint8();
}

@protected
U8Array32 sse_decode_u_8_array_32(SseDeserializer deserializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
var inner = sse_decode_list_prim_u_8_strict(deserializer);
return U8Array32(inner);
}

@protected
void sse_decode_unit(SseDeserializer deserializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
Expand Down Expand Up @@ -1818,7 +1795,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
void sse_encode_owned_output(OwnedOutput self, SseSerializer serializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
sse_encode_u_32(self.blockheight, serializer);
sse_encode_String(self.tweak, serializer);
sse_encode_u_8_array_32(self.tweak, serializer);
sse_encode_amount(self.amount, serializer);
sse_encode_String(self.script, serializer);
sse_encode_opt_String(self.label, serializer);
Expand Down Expand Up @@ -1919,6 +1896,12 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
serializer.buffer.putUint8(self);
}

@protected
void sse_encode_u_8_array_32(U8Array32 self, SseSerializer serializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
sse_encode_list_prim_u_8_strict(self.inner, serializer);
}

@protected
void sse_encode_unit(void self, SseSerializer serializer) {
// Codec=Sse (Serialization based), see doc to use other codecs
Expand Down
10 changes: 10 additions & 0 deletions lib/generated/rust/frb_generated.io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'dart:async';
import 'dart:convert';
import 'dart:ffi' as ffi;
import 'frb_generated.dart';
import 'lib.dart';
import 'logger.dart';
import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated_io.dart';
import 'stream.dart';
Expand Down Expand Up @@ -144,6 +145,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected
int dco_decode_u_8(dynamic raw);

@protected
U8Array32 dco_decode_u_8_array_32(dynamic raw);

@protected
void dco_decode_unit(dynamic raw);

Expand Down Expand Up @@ -283,6 +287,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected
int sse_decode_u_8(SseDeserializer deserializer);

@protected
U8Array32 sse_decode_u_8_array_32(SseDeserializer deserializer);

@protected
void sse_decode_unit(SseDeserializer deserializer);

Expand Down Expand Up @@ -423,6 +430,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected
void sse_encode_u_8(int self, SseSerializer serializer);

@protected
void sse_encode_u_8_array_32(U8Array32 self, SseSerializer serializer);

@protected
void sse_encode_unit(void self, SseSerializer serializer);

Expand Down
10 changes: 10 additions & 0 deletions lib/generated/rust/frb_generated.web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'api/wallet.dart';
import 'dart:async';
import 'dart:convert';
import 'frb_generated.dart';
import 'lib.dart';
import 'logger.dart';
import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated_web.dart';
import 'stream.dart';
Expand Down Expand Up @@ -146,6 +147,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected
int dco_decode_u_8(dynamic raw);

@protected
U8Array32 dco_decode_u_8_array_32(dynamic raw);

@protected
void dco_decode_unit(dynamic raw);

Expand Down Expand Up @@ -285,6 +289,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected
int sse_decode_u_8(SseDeserializer deserializer);

@protected
U8Array32 sse_decode_u_8_array_32(SseDeserializer deserializer);

@protected
void sse_decode_unit(SseDeserializer deserializer);

Expand Down Expand Up @@ -425,6 +432,9 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl<RustLibWire> {
@protected
void sse_encode_u_8(int self, SseSerializer serializer);

@protected
void sse_encode_u_8_array_32(U8Array32 self, SseSerializer serializer);

@protected
void sse_encode_unit(void self, SseSerializer serializer);

Expand Down
22 changes: 22 additions & 0 deletions lib/generated/rust/lib.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// This file is automatically generated, so please do not edit it.
// Generated by `flutter_rust_bridge`@ 2.3.0.

// ignore_for_file: invalid_use_of_internal_member, unused_import, unnecessary_import

import 'frb_generated.dart';
import 'package:collection/collection.dart';
import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart';

class U8Array32 extends NonGrowableListView<int> {
static const arraySize = 32;

@internal
Uint8List get inner => _inner;
final Uint8List _inner;

U8Array32(this._inner)
: assert(_inner.length == arraySize),
super(_inner);

U8Array32.init() : this(Uint8List(arraySize));
}
2 changes: 1 addition & 1 deletion lib/states/wallet_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class WalletState extends ChangeNotifier {

await scanToTip(
blindbitUrl: blindbitUrl!,
dustLimit: dustLimit!,
dustLimit: BigInt.from(dustLimit!),
encodedWallet: wallet);
} catch (e) {
scanning = false;
Expand Down
Loading

0 comments on commit fcc8e58

Please sign in to comment.