mirror of
https://github.com/MAGICGrants/flutter_libsparkmobile.git
synced 2026-01-09 21:17:56 -05:00
add validate spark address call with basic tests
This commit is contained in:
@@ -2,9 +2,9 @@ import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:ffi';
|
||||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:ffi/ffi.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_libsparkmobile/src/extensions.dart';
|
||||
import 'package:flutter_libsparkmobile/src/models/spark_coin.dart';
|
||||
|
||||
@@ -640,6 +640,46 @@ abstract final class LibSpark {
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static bool validateAddress({
|
||||
required String address,
|
||||
required bool isTestNet,
|
||||
}) {
|
||||
final addressPtr = address.toNativeUtf8().cast<Char>();
|
||||
|
||||
final result = _bindings.isValidSparkAddress(
|
||||
addressPtr,
|
||||
isTestNet ? 1 : 0,
|
||||
);
|
||||
|
||||
malloc.free(addressPtr);
|
||||
|
||||
if (result.address != nullptr.address) {
|
||||
final isValid = result.ref.isValid > 0;
|
||||
final String message;
|
||||
|
||||
if (result.ref.errorMessage.address == nullptr.address) {
|
||||
message = "";
|
||||
} else {
|
||||
message = result.ref.errorMessage.cast<Utf8>().toDartString();
|
||||
malloc.free(result.ref.errorMessage);
|
||||
}
|
||||
malloc.free(result);
|
||||
|
||||
if (kDebugMode && message.isNotEmpty) {
|
||||
debugPrint("validateAddress error message: $message");
|
||||
}
|
||||
|
||||
return isValid;
|
||||
} else {
|
||||
// some error occurred result in null being returned which should happen
|
||||
// but is checked anyways
|
||||
if (kDebugMode) {
|
||||
debugPrint("validateAddress ffi called returned nullptr");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension on Pointer<UnsignedChar> {
|
||||
|
||||
@@ -251,6 +251,24 @@ class FlutterLibsparkmobileBindings {
|
||||
late final _serializeMintContext = _serializeMintContextPtr.asFunction<
|
||||
ffi.Pointer<SerializedMintContextResult> Function(
|
||||
ffi.Pointer<DartInputData>, int)>();
|
||||
|
||||
ffi.Pointer<ValidateAddressResult> isValidSparkAddress(
|
||||
ffi.Pointer<ffi.Char> addressCStr,
|
||||
int isTestNet,
|
||||
) {
|
||||
return _isValidSparkAddress(
|
||||
addressCStr,
|
||||
isTestNet,
|
||||
);
|
||||
}
|
||||
|
||||
late final _isValidSparkAddressPtr = _lookup<
|
||||
ffi.NativeFunction<
|
||||
ffi.Pointer<ValidateAddressResult> Function(
|
||||
ffi.Pointer<ffi.Char>, ffi.Int)>>('isValidSparkAddress');
|
||||
late final _isValidSparkAddress = _isValidSparkAddressPtr.asFunction<
|
||||
ffi.Pointer<ValidateAddressResult> Function(
|
||||
ffi.Pointer<ffi.Char>, int)>();
|
||||
}
|
||||
|
||||
/// FFI-friendly wrapper for a spark::Coin.
|
||||
@@ -578,3 +596,10 @@ final class SerializedMintContextResult extends ffi.Struct {
|
||||
@ffi.Int()
|
||||
external int contextLength;
|
||||
}
|
||||
|
||||
final class ValidateAddressResult extends ffi.Struct {
|
||||
@ffi.Int()
|
||||
external int isValid;
|
||||
|
||||
external ffi.Pointer<ffi.Char> errorMessage;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user