mirror of
https://github.com/MAGICGrants/flutter_libsparkmobile.git
synced 2026-01-09 21:17:56 -05:00
pass a hex string
This commit is contained in:
@@ -17,32 +17,34 @@ class FlutterLibsparkmobile {
|
||||
|
||||
// SparkMobileBindings methods:
|
||||
|
||||
/// Derive an address from the keyData (mnemonic).
|
||||
Future<String> getAddress(
|
||||
List<int> keyData, int index, int diversifier) async {
|
||||
// Allocate space for the key data on the native heap.
|
||||
final keyDataPointer = malloc.allocate<Int>(keyData.length);
|
||||
final keyDataHex = _toHexString(keyData);
|
||||
|
||||
// Copy the key data into the allocated space.
|
||||
for (int i = 0; i < keyData.length; i++) {
|
||||
keyDataPointer[i] = keyData[i];
|
||||
}
|
||||
// Allocate memory for the hex string on the native heap.
|
||||
final keyDataPointer = keyDataHex.toNativeUtf8().cast<Char>();
|
||||
|
||||
// Call the native method with the pointer.
|
||||
final addressPointer = _bindings
|
||||
.getAddress(keyDataPointer, keyData.length, index, diversifier)
|
||||
.cast<Utf8>();
|
||||
final addressPointer =
|
||||
_bindings.getAddress(keyDataPointer, index, diversifier);
|
||||
|
||||
// Convert the Pointer<Utf8> to a Dart String.
|
||||
final addressString = addressPointer.toDartString();
|
||||
// Convert the Pointer<Char> to a Dart String.
|
||||
final addressString = addressPointer.cast<Utf8>().toDartString();
|
||||
|
||||
// Free the native heap allocated memory for the key data.
|
||||
// Free the native heap allocated memory.
|
||||
malloc.free(keyDataPointer);
|
||||
|
||||
// The native side allocated memory for the returned address,
|
||||
// it needs to be freed after copying it to Dart-controlled memory.
|
||||
final String result = addressString;
|
||||
malloc.free(addressPointer);
|
||||
|
||||
return result;
|
||||
return addressString;
|
||||
}
|
||||
|
||||
// Utility methods:
|
||||
|
||||
/// Convert List<int> keyData to a hex string.
|
||||
///
|
||||
/// TODO make extension.
|
||||
String _toHexString(List<int> keyData) {
|
||||
return keyData.map((byte) => byte.toRadixString(16).padLeft(2, '0')).join();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,14 +23,12 @@ class SparkMobileBindings {
|
||||
: _lookup = lookup;
|
||||
|
||||
ffi.Pointer<ffi.Char> getAddress(
|
||||
ffi.Pointer<ffi.Int> keyData,
|
||||
int keyDataLength,
|
||||
ffi.Pointer<ffi.Char> keyDataHex,
|
||||
int index,
|
||||
int diversifier,
|
||||
) {
|
||||
return _getAddress(
|
||||
keyData,
|
||||
keyDataLength,
|
||||
keyDataHex,
|
||||
index,
|
||||
diversifier,
|
||||
);
|
||||
@@ -39,7 +37,7 @@ class SparkMobileBindings {
|
||||
late final _getAddressPtr = _lookup<
|
||||
ffi.NativeFunction<
|
||||
ffi.Pointer<ffi.Char> Function(
|
||||
ffi.Pointer<ffi.Int>, ffi.Int, ffi.Int, ffi.Int)>>('getAddress');
|
||||
ffi.Pointer<ffi.Char>, ffi.Int, ffi.Int)>>('getAddress');
|
||||
late final _getAddress = _getAddressPtr.asFunction<
|
||||
ffi.Pointer<ffi.Char> Function(ffi.Pointer<ffi.Int>, int, int, int)>();
|
||||
ffi.Pointer<ffi.Char> Function(ffi.Pointer<ffi.Char>, int, int)>();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user