diff --git a/example/lib/services/ffi_test_service.dart b/example/lib/services/ffi_test_service.dart index d851b15..83abb11 100644 --- a/example/lib/services/ffi_test_service.dart +++ b/example/lib/services/ffi_test_service.dart @@ -502,25 +502,43 @@ class FFITestService { 'Encode compact slate JSON to slatepack and decode back via FFI', () async { try { - // Construct a minimal compact slate JSON. - // Use version 3 + compact_slate flag to satisfy current lib expectations. + // A compact slate from the MWC integration docs. Encoding requires + // both the transaction body and the participant data. const compactSlateJson = '{\n' - ' "version_info": {\n' - ' "orig_version": 3,\n' - ' "version": 3,\n' - ' "block_header_version": 1\n' - ' },\n' - ' "id": "0436430c-2b02-624c-2032-570501212b00",\n' - ' "sta": "S1",\n' - ' "num_participants": 2,\n' - ' "amount": "1000000000",\n' - ' "fee": "1000000",\n' - ' "height": "0",\n' + ' "amount": "200000000",\n' + ' "coin_type": "mwc",\n' + ' "compact_slate": true,\n' + ' "fee": "7000000",\n' + ' "height": "2793169",\n' + ' "id": "e2757000-4a03-47f9-b6fe-ed91dc2ceb9f",\n' ' "lock_height": "0",\n' - ' "ttl_cutoff_height": "1440",\n' + ' "network_type": "mainnet",\n' + ' "num_participants": 2,\n' + ' "participant_data": [\n' + ' {\n' + ' "id": "0",\n' + ' "message": null,\n' + ' "message_sig": null,\n' + ' "part_sig": null,\n' + ' "public_blind_excess": "028d440ffa605a04cc92ca9e77b6be3295042fdc088b12565c73ae3bb4c7fbabae",\n' + ' "public_nonce": "0309338aec074b65e82e6077aab0e5a9b052df9948207e047bbedca1477f17a6cd"\n' + ' }\n' + ' ],\n' ' "payment_proof": null,\n' - ' "compact_slate": true,\n' - ' "participant_data": []\n' + ' "ttl_cutoff_height": "2794609",\n' + ' "tx": {\n' + ' "body": {\n' + ' "inputs": [],\n' + ' "kernels": [],\n' + ' "outputs": []\n' + ' },\n' + ' "offset": "0000000000000000000000000000000000000000000000000000000000000000"\n' + ' },\n' + ' "version_info": {\n' + ' "block_header_version": 2,\n' + ' "orig_version": 3,\n' + ' "version": 3\n' + ' }\n' '}'; // Encode to slatepack (unencrypted) — recipientAddress null. @@ -531,8 +549,8 @@ class FFITestService { ); // Basic format assertions. - if (!enc.slatepack.contains('BEGINSLATEPACK') || - !enc.slatepack.contains('ENDSLATEPACK')) { + if (!enc.slatepack.contains('BEGINSLATE') || + !enc.slatepack.contains('ENDSLATE')) { throw TestException('Encoded slatepack missing BEGIN/END markers'); } if (enc.wasEncrypted) { @@ -544,7 +562,7 @@ class FFITestService { final decoded = jsonDecode(dec.slateJson) as Map; final decodedId = decoded['id'] as String?; final versionInfo = decoded['version_info'] as Map?; - if (decodedId != '0436430c-2b02-624c-2032-570501212b00') { + if (decodedId != 'e2757000-4a03-47f9-b6fe-ed91dc2ceb9f') { throw TestException('Decoded slate id mismatch or missing: $decodedId'); } if (versionInfo == null || versionInfo['version'] != 3) { @@ -573,7 +591,7 @@ class FFITestService { bool threw = false; try { await Libmwc.encodeSlatepack( - slateJson: '{"id":"abc","version_info":{"version":3,"block_header_version":1}}', + slateJson: '{"amount":"200000000","coin_type":"mwc","compact_slate":true,"fee":"7000000","height":"2793169","id":"e2757000-4a03-47f9-b6fe-ed91dc2ceb9f","lock_height":"0","network_type":"mainnet","num_participants":2,"participant_data":[{"id":"0","message":null,"message_sig":null,"part_sig":null,"public_blind_excess":"028d440ffa605a04cc92ca9e77b6be3295042fdc088b12565c73ae3bb4c7fbabae","public_nonce":"0309338aec074b65e82e6077aab0e5a9b052df9948207e047bbedca1477f17a6cd"}],"payment_proof":null,"ttl_cutoff_height":"2794609","tx":{"body":{"inputs":[],"kernels":[],"outputs":[]},"offset":"0000000000000000000000000000000000000000000000000000000000000000"},"version_info":{"block_header_version":2,"orig_version":3,"version":3}}', recipientAddress: 'dummy@mwcmqs.mwc.mw', encrypt: true, wallet: null, // No wallet context in test environment @@ -854,10 +872,13 @@ class FFITestService { // We can't check if methods are null, so we'll try to call them. try { // Try to access the methods - this will throw if they don't exist. - final startMethod = Libmwc.startMwcMqsListener; - final stopMethod = Libmwc.stopMwcMqsListener; - - // If we get here, methods exist. + final methods = [ + Libmwc.startMwcMqsListener, + Libmwc.stopMwcMqsListener, + ]; + if (methods.length != 2) { + throw TestException('Libmwc MWCMQS methods not available'); + } } catch (methodError) { throw TestException('Libmwc MWCMQS methods not available: ${methodError.toString()}'); } diff --git a/test/ffi_test.dart b/test/ffi_test.dart new file mode 100644 index 0000000..4a47837 --- /dev/null +++ b/test/ffi_test.dart @@ -0,0 +1,132 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:flutter_libmwc/lib.dart'; +import 'package:flutter_test/flutter_test.dart'; + +// A compact slate from the MWC integration docs. Encoding requires both the +// transaction body and the participant data, even when the lists are empty. +const _compactSlateJson = + '{\n' + ' "amount": "200000000",\n' + ' "coin_type": "mwc",\n' + ' "compact_slate": true,\n' + ' "fee": "7000000",\n' + ' "height": "2793169",\n' + ' "id": "e2757000-4a03-47f9-b6fe-ed91dc2ceb9f",\n' + ' "lock_height": "0",\n' + ' "network_type": "mainnet",\n' + ' "num_participants": 2,\n' + ' "participant_data": [\n' + ' {\n' + ' "id": "0",\n' + ' "message": null,\n' + ' "message_sig": null,\n' + ' "part_sig": null,\n' + ' "public_blind_excess": "028d440ffa605a04cc92ca9e77b6be3295042fdc088b12565c73ae3bb4c7fbabae",\n' + ' "public_nonce": "0309338aec074b65e82e6077aab0e5a9b052df9948207e047bbedca1477f17a6cd"\n' + ' }\n' + ' ],\n' + ' "payment_proof": null,\n' + ' "ttl_cutoff_height": "2794609",\n' + ' "tx": {\n' + ' "body": {\n' + ' "inputs": [],\n' + ' "kernels": [],\n' + ' "outputs": []\n' + ' },\n' + ' "offset": "0000000000000000000000000000000000000000000000000000000000000000"\n' + ' },\n' + ' "version_info": {\n' + ' "block_header_version": 2,\n' + ' "orig_version": 3,\n' + ' "version": 3\n' + ' }\n' + '}'; + +String _walletConfig(String walletDir) => jsonEncode({ + 'wallet_dir': walletDir, + 'check_node_api_http_addr': 'https://mwc713.mwc.mw:443', + 'chain': 'mainnet', + 'account': 'default', +}); + +void main() { + test('generates a 24-word mnemonic', () { + final words = Libmwc.getMnemonic().trim().split(RegExp(r'\s+')); + expect(words, hasLength(24)); + }); + + test('rejects an invalid send address', () { + expect(Libmwc.validateSendAddress(address: 'invalid_address'), isFalse); + }); + + test('creates a wallet and opens it', () async { + final walletDir = await Directory.systemTemp.createTemp( + 'flutter_libmwc_wallet_', + ); + addTearDown(() async { + try { + await walletDir.delete(recursive: true); + } on FileSystemException { + // The wallet library may still hold files open on Windows. + } + }); + + final config = _walletConfig(walletDir.path); + final name = 'ffi_test_${DateTime.now().microsecondsSinceEpoch}'; + final handle = await Libmwc.initializeNewWallet( + config: config, + mnemonic: Libmwc.getMnemonic(), + password: 'test_password_123', + name: name, + ); + expect(handle.toUpperCase(), isNot(contains('ERROR'))); + + final opened = await Libmwc.openWallet( + config: config, + password: 'test_password_123', + ); + expect(opened.toUpperCase(), isNot(contains('ERROR'))); + }); + + test('round-trips an unencrypted slatepack', () async { + final encoded = await Libmwc.encodeSlatepack( + slateJson: _compactSlateJson, + encrypt: false, + ); + + expect(encoded.slatepack, contains('BEGINSLATE')); + expect(encoded.slatepack, contains('ENDSLATE')); + expect(encoded.wasEncrypted, isFalse); + + final decoded = await Libmwc.decodeSlatepack(slatepack: encoded.slatepack); + final slate = jsonDecode(decoded.slateJson) as Map; + expect(slate['id'], 'e2757000-4a03-47f9-b6fe-ed91dc2ceb9f'); + expect(await Libmwc.isSlatepackEncrypted(encoded.slatepack), isFalse); + }); + + test('requires wallet context for encrypted slatepacks', () async { + await expectLater( + Libmwc.encodeSlatepack( + slateJson: _compactSlateJson, + recipientAddress: 'dummy@mwcmqs.mwc.mw', + encrypt: true, + wallet: null, + ), + throwsA( + predicate((error) { + final message = error.toString().toLowerCase(); + return message.contains('wallet') && message.contains('required'); + }), + ), + ); + }); + + test('rejects a malformed slatepack', () async { + await expectLater( + Libmwc.decodeSlatepack(slatepack: 'NOT_A_SLATEPACK'), + throwsA(anything), + ); + }); +}