From 712659e86f7edf982a3189e49f5266c0332e62d0 Mon Sep 17 00:00:00 2001 From: John Smith Date: Wed, 4 Jan 2023 17:02:45 -0500 Subject: [PATCH] bug fixes --- veilid-flutter/lib/base64url_no_pad.dart | 15 ++++++++ veilid-flutter/lib/veilid.dart | 10 +++--- veilid-flutter/lib/veilid_ffi.dart | 29 +++++++-------- veilid-flutter/lib/veilid_js.dart | 46 +++++++++++++----------- 4 files changed, 61 insertions(+), 39 deletions(-) create mode 100644 veilid-flutter/lib/base64url_no_pad.dart diff --git a/veilid-flutter/lib/base64url_no_pad.dart b/veilid-flutter/lib/base64url_no_pad.dart new file mode 100644 index 00000000..81f97a2d --- /dev/null +++ b/veilid-flutter/lib/base64url_no_pad.dart @@ -0,0 +1,15 @@ +import 'dart:convert'; +import 'dart:typed_data'; + +String base64UrlNoPadEncode(List bytes) { + var x = base64Url.encode(bytes); + while (x.endsWith('=')) { + x = x.substring(0, x.length - 1); + } + return x; +} + +Uint8List base64UrlNoPadDecode(String source) { + source = base64.normalize(source); + return base64.decode(source); +} diff --git a/veilid-flutter/lib/veilid.dart b/veilid-flutter/lib/veilid.dart index 12c6740a..3a8baea4 100644 --- a/veilid-flutter/lib/veilid.dart +++ b/veilid-flutter/lib/veilid.dart @@ -8,6 +8,8 @@ import 'veilid_stub.dart' if (dart.library.io) 'veilid_ffi.dart' if (dart.library.js) 'veilid_js.dart'; +import 'base64url_no_pad.dart'; + ////////////////////////////////////////////////////////// export 'default_config.dart'; @@ -1315,7 +1317,7 @@ class VeilidAppMessage implements VeilidUpdate { return { 'kind': "AppMessage", 'sender': sender, - 'message': base64UrlEncode(message) + 'message': base64UrlNoPadEncode(message) }; } } @@ -1337,7 +1339,7 @@ class VeilidAppCall implements VeilidUpdate { return { 'kind': "AppMessage", 'sender': sender, - 'message': base64UrlEncode(message), + 'message': base64UrlNoPadEncode(message), 'id': id, }; } @@ -1829,10 +1831,10 @@ class KeyBlob { KeyBlob.fromJson(dynamic json) : key = json['key'], - blob = base64Decode(json['blob']); + blob = base64UrlNoPadDecode(json['blob']); Map get json { - return {'key': key, 'blob': base64UrlEncode(blob)}; + return {'key': key, 'blob': base64UrlNoPadEncode(blob)}; } } diff --git a/veilid-flutter/lib/veilid_ffi.dart b/veilid-flutter/lib/veilid_ffi.dart index dc1bf13b..4af42a26 100644 --- a/veilid-flutter/lib/veilid_ffi.dart +++ b/veilid-flutter/lib/veilid_ffi.dart @@ -8,6 +8,7 @@ import 'dart:typed_data'; import 'package:ffi/ffi.dart'; import 'veilid.dart'; +import 'base64url_no_pad.dart'; ////////////////////////////////////////////////////////// @@ -425,20 +426,20 @@ class VeilidRoutingContextFFI implements VeilidRoutingContext { @override Future appCall(String target, Uint8List request) async { var nativeEncodedTarget = target.toNativeUtf8(); - var nativeEncodedRequest = base64UrlEncode(request).toNativeUtf8(); + var nativeEncodedRequest = base64UrlNoPadEncode(request).toNativeUtf8(); final recvPort = ReceivePort("routing_context_app_call"); final sendPort = recvPort.sendPort; _ctx.ffi._routingContextAppCall(sendPort.nativePort, _ctx.id, nativeEncodedTarget, nativeEncodedRequest); final out = await processFuturePlain(recvPort.first); - return base64Decode(out); + return base64UrlNoPadDecode(out); } @override Future appMessage(String target, Uint8List message) async { final nativeEncodedTarget = target.toNativeUtf8(); - final nativeEncodedMessage = base64UrlEncode(message).toNativeUtf8(); + final nativeEncodedMessage = base64UrlNoPadEncode(message).toNativeUtf8(); final recvPort = ReceivePort("routing_context_app_message"); final sendPort = recvPort.sendPort; @@ -490,8 +491,8 @@ class VeilidTableDBTransactionFFI extends VeilidTableDBTransaction { @override Future store(int col, Uint8List key, Uint8List value) { - final nativeEncodedKey = base64UrlEncode(key).toNativeUtf8(); - final nativeEncodedValue = base64UrlEncode(value).toNativeUtf8(); + final nativeEncodedKey = base64UrlNoPadEncode(key).toNativeUtf8(); + final nativeEncodedValue = base64UrlNoPadEncode(value).toNativeUtf8(); final recvPort = ReceivePort("veilid_table_db_transaction_store"); final sendPort = recvPort.sendPort; @@ -507,7 +508,7 @@ class VeilidTableDBTransactionFFI extends VeilidTableDBTransaction { @override Future delete(int col, Uint8List key) { - final nativeEncodedKey = base64UrlEncode(key).toNativeUtf8(); + final nativeEncodedKey = base64UrlNoPadEncode(key).toNativeUtf8(); final recvPort = ReceivePort("veilid_table_db_transaction_delete"); final sendPort = recvPort.sendPort; @@ -551,7 +552,7 @@ class VeilidTableDBFFI extends VeilidTableDB { String ja = s.toDartString(); _tdb.ffi._freeString(s); List jarr = jsonDecode(ja); - return jarr.map((e) => base64Decode(e)).toList(); + return jarr.map((e) => base64UrlNoPadDecode(e)).toList(); } @override @@ -562,8 +563,8 @@ class VeilidTableDBFFI extends VeilidTableDB { @override Future store(int col, Uint8List key, Uint8List value) { - final nativeEncodedKey = base64UrlEncode(key).toNativeUtf8(); - final nativeEncodedValue = base64UrlEncode(value).toNativeUtf8(); + final nativeEncodedKey = base64UrlNoPadEncode(key).toNativeUtf8(); + final nativeEncodedValue = base64UrlNoPadEncode(value).toNativeUtf8(); final recvPort = ReceivePort("veilid_table_db_store"); final sendPort = recvPort.sendPort; @@ -579,7 +580,7 @@ class VeilidTableDBFFI extends VeilidTableDB { @override Future load(int col, Uint8List key) async { - final nativeEncodedKey = base64UrlEncode(key).toNativeUtf8(); + final nativeEncodedKey = base64UrlNoPadEncode(key).toNativeUtf8(); final recvPort = ReceivePort("veilid_table_db_load"); final sendPort = recvPort.sendPort; @@ -593,12 +594,12 @@ class VeilidTableDBFFI extends VeilidTableDB { if (out == null) { return null; } - return base64Decode(out); + return base64UrlNoPadDecode(out); } @override Future delete(int col, Uint8List key) { - final nativeEncodedKey = base64UrlEncode(key).toNativeUtf8(); + final nativeEncodedKey = base64UrlNoPadEncode(key).toNativeUtf8(); final recvPort = ReceivePort("veilid_table_db_delete"); final sendPort = recvPort.sendPort; @@ -866,7 +867,7 @@ class VeilidFFI implements Veilid { @override Future importRemotePrivateRoute(Uint8List blob) { - final nativeEncodedBlob = base64UrlEncode(blob).toNativeUtf8(); + final nativeEncodedBlob = base64UrlNoPadEncode(blob).toNativeUtf8(); final recvPort = ReceivePort("import_remote_private_route"); final sendPort = recvPort.sendPort; @@ -887,7 +888,7 @@ class VeilidFFI implements Veilid { @override Future appCallReply(String id, Uint8List message) { final nativeId = id.toNativeUtf8(); - final nativeEncodedMessage = base64UrlEncode(message).toNativeUtf8(); + final nativeEncodedMessage = base64UrlNoPadEncode(message).toNativeUtf8(); final recvPort = ReceivePort("app_call_reply"); final sendPort = recvPort.sendPort; _appCallReply(sendPort.nativePort, nativeId, nativeEncodedMessage); diff --git a/veilid-flutter/lib/veilid_js.dart b/veilid-flutter/lib/veilid_js.dart index 87966390..fbfcecba 100644 --- a/veilid-flutter/lib/veilid_js.dart +++ b/veilid-flutter/lib/veilid_js.dart @@ -7,6 +7,8 @@ import 'dart:async'; import 'dart:convert'; import 'dart:typed_data'; +import 'base64url_no_pad.dart'; + ////////////////////////////////////////////////////////// Veilid getVeilid() => VeilidJS(); @@ -60,15 +62,15 @@ class VeilidRoutingContextJS implements VeilidRoutingContext { @override Future appCall(String target, Uint8List request) async { - var encodedRequest = base64UrlEncode(request); + var encodedRequest = base64UrlNoPadEncode(request); - return base64Decode(await _wrapApiPromise(js_util.callMethod( + return base64UrlNoPadDecode(await _wrapApiPromise(js_util.callMethod( wasm, "routing_context_app_call", [_ctx.id, encodedRequest]))); } @override Future appMessage(String target, Uint8List message) { - var encodedMessage = base64UrlEncode(message); + var encodedMessage = base64UrlNoPadEncode(message); return _wrapApiPromise(js_util.callMethod( wasm, "routing_context_app_message", [_ctx.id, encodedMessage])); @@ -108,19 +110,21 @@ class VeilidTableDBTransactionJS extends VeilidTableDBTransaction { @override Future store(int col, Uint8List key, Uint8List value) { - final encodedKey = base64UrlEncode(key); - final encodedValue = base64UrlEncode(value); + final encodedKey = base64UrlNoPadEncode(key); + final encodedValue = base64UrlNoPadEncode(value); - return _wrapApiPromise(js_util.callMethod(wasm, - "table_db_transaction_store", [_tdbt.id, encodedKey, encodedValue])); + return _wrapApiPromise(js_util.callMethod( + wasm, + "table_db_transaction_store", + [_tdbt.id, col, encodedKey, encodedValue])); } @override Future delete(int col, Uint8List key) { - final encodedKey = base64UrlEncode(key); + final encodedKey = base64UrlNoPadEncode(key); return _wrapApiPromise(js_util.callMethod( - wasm, "table_db_transaction_delete", [_tdbt.id, encodedKey])); + wasm, "table_db_transaction_delete", [_tdbt.id, col, encodedKey])); } } @@ -154,7 +158,7 @@ class VeilidTableDBJS extends VeilidTableDB { throw VeilidAPIExceptionInternal("No db for id"); } List jarr = jsonDecode(s); - return jarr.map((e) => base64Decode(e)).toList(); + return jarr.map((e) => base64UrlNoPadDecode(e)).toList(); } @override @@ -166,31 +170,31 @@ class VeilidTableDBJS extends VeilidTableDB { @override Future store(int col, Uint8List key, Uint8List value) { - final encodedKey = base64UrlEncode(key); - final encodedValue = base64UrlEncode(value); + final encodedKey = base64UrlNoPadEncode(key); + final encodedValue = base64UrlNoPadEncode(value); return _wrapApiPromise(js_util.callMethod( - wasm, "table_db_store", [_tdb.id, encodedKey, encodedValue])); + wasm, "table_db_store", [_tdb.id, col, encodedKey, encodedValue])); } @override Future load(int col, Uint8List key) async { - final encodedKey = base64UrlEncode(key); + final encodedKey = base64UrlNoPadEncode(key); String? out = await _wrapApiPromise( - js_util.callMethod(wasm, "table_db_load", [_tdb.id, encodedKey])); + js_util.callMethod(wasm, "table_db_load", [_tdb.id, col, encodedKey])); if (out == null) { return null; } - return base64Decode(out); + return base64UrlNoPadDecode(out); } @override Future delete(int col, Uint8List key) { - final encodedKey = base64UrlEncode(key); + final encodedKey = base64UrlNoPadEncode(key); - return _wrapApiPromise( - js_util.callMethod(wasm, "table_db_delete", [_tdb.id, encodedKey])); + return _wrapApiPromise(js_util + .callMethod(wasm, "table_db_delete", [_tdb.id, col, encodedKey])); } } @@ -285,7 +289,7 @@ class VeilidJS implements Veilid { @override Future importRemotePrivateRoute(Uint8List blob) { - var encodedBlob = base64UrlEncode(blob); + var encodedBlob = base64UrlNoPadEncode(blob); return _wrapApiPromise( js_util.callMethod(wasm, "import_remote_private_route", [encodedBlob])); } @@ -298,7 +302,7 @@ class VeilidJS implements Veilid { @override Future appCallReply(String id, Uint8List message) { - var encodedMessage = base64UrlEncode(message); + var encodedMessage = base64UrlNoPadEncode(message); return _wrapApiPromise( js_util.callMethod(wasm, "app_call_reply", [id, encodedMessage])); }