mirror of
https://github.com/MAGICGrants/flutter_libsparkmobile.git
synced 2026-01-09 21:17:56 -05:00
WIP id and recover coin setup from the dart side
This commit is contained in:
@@ -4,6 +4,7 @@ import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:ffi/ffi.dart';
|
||||
import 'package:flutter_libsparkmobile/src/models/spark_coin.dart';
|
||||
|
||||
import 'src/extensions.dart';
|
||||
import 'src/flutter_libsparkmobile_bindings_generated.dart';
|
||||
@@ -76,4 +77,36 @@ abstract final class LibSpark {
|
||||
|
||||
return addressString;
|
||||
}
|
||||
|
||||
///
|
||||
/// Check whether the spark coin is ours and if so recover required data
|
||||
/// and encapsulate into a single object ([SparkCoin]).
|
||||
///
|
||||
/// Returns a [SparkCoin] if the coin belongs to us, or null otherwise.
|
||||
///
|
||||
static SparkCoin? identifyAndRecoverCoin(
|
||||
String serializedCoin, {
|
||||
required String privateKeyHex,
|
||||
required int index,
|
||||
}) {
|
||||
final result = _bindings.identifyCoin(
|
||||
serializedCoin.toNativeUtf8().cast<Char>(),
|
||||
serializedCoin.length,
|
||||
privateKeyHex.toNativeUtf8().cast<Char>(),
|
||||
index,
|
||||
);
|
||||
|
||||
throw UnimplementedError(
|
||||
"Have ffi return all the data we need for SparkCoin."
|
||||
" This may or may not encompass all fields currently in SparkCoin.",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
extension on Uint8List {
|
||||
Pointer<UnsignedChar> unsignedCharPointer() {
|
||||
final pointer = malloc.allocate<Uint8>(sizeOf<Uint8>() * lengthInBytes);
|
||||
pointer.asTypedList(lengthInBytes).setAll(0, this);
|
||||
return pointer.cast<UnsignedChar>();
|
||||
}
|
||||
}
|
||||
|
||||
89
lib/src/models/spark_coin.dart
Normal file
89
lib/src/models/spark_coin.dart
Normal file
@@ -0,0 +1,89 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
enum SparkCoinType {
|
||||
mint(0),
|
||||
spend(1);
|
||||
|
||||
const SparkCoinType(this.value);
|
||||
final int value;
|
||||
}
|
||||
|
||||
class SparkCoin {
|
||||
final SparkCoinType type;
|
||||
|
||||
final Uint8List? k; // TODO: proper name (not single char!!) is this nonce???
|
||||
|
||||
final String? address;
|
||||
|
||||
final BigInt? value;
|
||||
|
||||
final String? memo;
|
||||
final Uint8List? serialContext;
|
||||
|
||||
final BigInt? diversifier;
|
||||
final Uint8List? encryptedDiversifier;
|
||||
|
||||
final Uint8List? serial;
|
||||
final Uint8List? tag;
|
||||
|
||||
final Uint8List? lTagHash;
|
||||
|
||||
SparkCoin({
|
||||
required this.type,
|
||||
this.k,
|
||||
this.address,
|
||||
this.value,
|
||||
this.memo,
|
||||
this.serialContext,
|
||||
this.diversifier,
|
||||
this.encryptedDiversifier,
|
||||
this.serial,
|
||||
this.tag,
|
||||
this.lTagHash,
|
||||
});
|
||||
|
||||
SparkCoin copyWith({
|
||||
SparkCoinType? type,
|
||||
Uint8List? k,
|
||||
String? address,
|
||||
BigInt? value,
|
||||
String? memo,
|
||||
Uint8List? serialContext,
|
||||
BigInt? diversifier,
|
||||
Uint8List? encryptedDiversifier,
|
||||
Uint8List? serial,
|
||||
Uint8List? tag,
|
||||
Uint8List? lTagHash,
|
||||
}) {
|
||||
return SparkCoin(
|
||||
type: type ?? this.type,
|
||||
k: k ?? this.k,
|
||||
address: address ?? this.address,
|
||||
value: value ?? this.value,
|
||||
memo: memo ?? this.memo,
|
||||
serialContext: serialContext ?? this.serialContext,
|
||||
diversifier: diversifier ?? this.diversifier,
|
||||
encryptedDiversifier: encryptedDiversifier ?? this.encryptedDiversifier,
|
||||
serial: serial ?? this.serial,
|
||||
tag: tag ?? this.tag,
|
||||
lTagHash: lTagHash ?? this.lTagHash,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'SparkCoin('
|
||||
', type: $type'
|
||||
', k: $k'
|
||||
', address: $address'
|
||||
', value: $value'
|
||||
', memo: $memo'
|
||||
', serialContext: $serialContext'
|
||||
', diversifier: $diversifier'
|
||||
', encryptedDiversifier: $encryptedDiversifier'
|
||||
', serial: $serial'
|
||||
', tag: $tag'
|
||||
', lTagHash: $lTagHash'
|
||||
')';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user