From f011d73ec2718666f3c60c68d973fbbb6d9e363c Mon Sep 17 00:00:00 2001 From: Skycoder42 Date: Thu, 27 May 2021 08:29:00 +0200 Subject: [PATCH] use typedefs for JS states --- .../secret_stream_pull_transformer_js.dart | 20 ++++--- .../secret_stream_push_transformer_js.dart | 19 +++--- .../helpers/sign/sign_consumer_js_mixin.dart | 4 +- .../helpers/sign/signature_consumer_js.dart | 2 +- .../sign/verification_consumer_js.dart | 2 +- .../sodium/lib/src/js/bindings/sodium.js.dart | 58 +++++++------------ packages/sodium/pubspec.yaml | 8 +-- .../generators/library_generator.dart | 1 - .../libsodium_js_generator/type_mappings.dart | 50 ++++++++-------- .../types/opaque_type.dart.template | 6 -- 10 files changed, 75 insertions(+), 95 deletions(-) delete mode 100644 packages/sodium/tool/libsodium_js_generator/types/opaque_type.dart.template diff --git a/packages/sodium/lib/src/js/api/helpers/secret_stream/secret_stream_pull_transformer_js.dart b/packages/sodium/lib/src/js/api/helpers/secret_stream/secret_stream_pull_transformer_js.dart index a300322a..dcd15ee2 100644 --- a/packages/sodium/lib/src/js/api/helpers/secret_stream/secret_stream_pull_transformer_js.dart +++ b/packages/sodium/lib/src/js/api/helpers/secret_stream/secret_stream_pull_transformer_js.dart @@ -11,8 +11,8 @@ import '../../../bindings/sodium.js.dart'; import '../../../bindings/to_safe_int.dart'; import 'secret_stream_message_tag_jsx.dart'; -class SecretStreamPullTransformerSinkJS - extends SecretStreamPullTransformerSink { +class SecretStreamPullTransformerSinkJS extends SecretStreamPullTransformerSink< + SecretstreamXchacha20poly1305State> { final LibSodiumJS sodium; SecretStreamPullTransformerSinkJS( @@ -26,7 +26,7 @@ class SecretStreamPullTransformerSinkJS sodium.crypto_secretstream_xchacha20poly1305_HEADERBYTES.toSafeUInt32(); @override - num initialize( + SecretstreamXchacha20poly1305State initialize( SecureKey key, Uint8List header, ) => @@ -40,14 +40,14 @@ class SecretStreamPullTransformerSinkJS ); @override - void rekey(num cryptoState) => JsError.wrap( + void rekey(SecretstreamXchacha20poly1305State cryptoState) => JsError.wrap( // ignore result, as it is always true () => sodium.crypto_secretstream_xchacha20poly1305_rekey(cryptoState), ); @override SecretStreamPlainMessage decryptMessage( - num cryptoState, + SecretstreamXchacha20poly1305State cryptoState, SecretStreamCipherMessage event, ) { final dynamic pullResult = JsError.wrap( @@ -76,11 +76,12 @@ class SecretStreamPullTransformerSinkJS } @override - void disposeState(num cryptoState) {} + void disposeState(SecretstreamXchacha20poly1305State cryptoState) {} } @internal -class SecretStreamPullTransformerJS extends SecretStreamPullTransformer { +class SecretStreamPullTransformerJS + extends SecretStreamPullTransformer { final LibSodiumJS sodium; const SecretStreamPullTransformerJS( @@ -91,6 +92,7 @@ class SecretStreamPullTransformerJS extends SecretStreamPullTransformer { ) : super(key, requireFinalized); @override - SecretStreamPullTransformerSink createSink(bool requireFinalized) => - SecretStreamPullTransformerSinkJS(sodium, requireFinalized); + SecretStreamPullTransformerSink + createSink(bool requireFinalized) => + SecretStreamPullTransformerSinkJS(sodium, requireFinalized); } diff --git a/packages/sodium/lib/src/js/api/helpers/secret_stream/secret_stream_push_transformer_js.dart b/packages/sodium/lib/src/js/api/helpers/secret_stream/secret_stream_push_transformer_js.dart index 890680fd..9a83ff8c 100644 --- a/packages/sodium/lib/src/js/api/helpers/secret_stream/secret_stream_push_transformer_js.dart +++ b/packages/sodium/lib/src/js/api/helpers/secret_stream/secret_stream_push_transformer_js.dart @@ -9,14 +9,14 @@ import '../../../bindings/sodium.js.dart'; import 'secret_stream_message_tag_jsx.dart'; @internal -class SecretStreamPushTransformerSinkJS - extends SecretStreamPushTransformerSink { +class SecretStreamPushTransformerSinkJS extends SecretStreamPushTransformerSink< + SecretstreamXchacha20poly1305State> { final LibSodiumJS sodium; SecretStreamPushTransformerSinkJS(this.sodium); @override - InitPushResult initialize(SecureKey key) { + InitPushResult initialize(SecureKey key) { final initResult = JsError.wrap( () => key.runUnlockedSync( (keyData) => sodium.crypto_secretstream_xchacha20poly1305_init_push( @@ -32,14 +32,14 @@ class SecretStreamPushTransformerSinkJS } @override - void rekey(num cryptoState) => JsError.wrap( + void rekey(SecretstreamXchacha20poly1305State cryptoState) => JsError.wrap( // always returns true, ignore result () => sodium.crypto_secretstream_xchacha20poly1305_rekey(cryptoState), ); @override SecretStreamCipherMessage encryptMessage( - num cryptoState, + SecretstreamXchacha20poly1305State cryptoState, SecretStreamPlainMessage event, ) { final cipherText = JsError.wrap( @@ -58,16 +58,17 @@ class SecretStreamPushTransformerSinkJS } @override - void disposeState(num cryptoState) {} + void disposeState(SecretstreamXchacha20poly1305State cryptoState) {} } @internal -class SecretStreamPushTransformerJS extends SecretStreamPushTransformer { +class SecretStreamPushTransformerJS + extends SecretStreamPushTransformer { final LibSodiumJS sodium; const SecretStreamPushTransformerJS(this.sodium, SecureKey key) : super(key); @override - SecretStreamPushTransformerSink createSink() => - SecretStreamPushTransformerSinkJS(sodium); + SecretStreamPushTransformerSink + createSink() => SecretStreamPushTransformerSinkJS(sodium); } diff --git a/packages/sodium/lib/src/js/api/helpers/sign/sign_consumer_js_mixin.dart b/packages/sodium/lib/src/js/api/helpers/sign/sign_consumer_js_mixin.dart index 0f3c5ffa..4a0a76be 100644 --- a/packages/sodium/lib/src/js/api/helpers/sign/sign_consumer_js_mixin.dart +++ b/packages/sodium/lib/src/js/api/helpers/sign/sign_consumer_js_mixin.dart @@ -11,13 +11,13 @@ mixin SignConsumerJSMixin LibSodiumJS get sodium; final _signatureCompleter = Completer(); - late final num _state; + late final SignState _state; @protected Future get result => _signatureCompleter.future; @protected - T finalize(num state); + T finalize(SignState state); @protected void initState() { diff --git a/packages/sodium/lib/src/js/api/helpers/sign/signature_consumer_js.dart b/packages/sodium/lib/src/js/api/helpers/sign/signature_consumer_js.dart index e33faeb1..ecba19b0 100644 --- a/packages/sodium/lib/src/js/api/helpers/sign/signature_consumer_js.dart +++ b/packages/sodium/lib/src/js/api/helpers/sign/signature_consumer_js.dart @@ -32,7 +32,7 @@ class SignatureConsumerJS Future get signature => result; @override - Uint8List finalize(num state) => JsError.wrap( + Uint8List finalize(SignState state) => JsError.wrap( () => secretKey.runUnlockedSync( (secretKeyData) => sodium.crypto_sign_final_create( state, diff --git a/packages/sodium/lib/src/js/api/helpers/sign/verification_consumer_js.dart b/packages/sodium/lib/src/js/api/helpers/sign/verification_consumer_js.dart index 4b64aeb8..e58cb563 100644 --- a/packages/sodium/lib/src/js/api/helpers/sign/verification_consumer_js.dart +++ b/packages/sodium/lib/src/js/api/helpers/sign/verification_consumer_js.dart @@ -26,7 +26,7 @@ class VerificationConsumerJS Future get signatureValid => result; @override - bool finalize(num state) => JsError.wrap( + bool finalize(SignState state) => JsError.wrap( () => sodium.crypto_sign_final_verify( state, signature, diff --git a/packages/sodium/lib/src/js/bindings/sodium.js.dart b/packages/sodium/lib/src/js/bindings/sodium.js.dart index 2c45a828..cd7dd55a 100644 --- a/packages/sodium/lib/src/js/bindings/sodium.js.dart +++ b/packages/sodium/lib/src/js/bindings/sodium.js.dart @@ -6,7 +6,18 @@ library sodium.js; import 'dart:typed_data'; import 'package:js/js.dart'; -import 'package:meta/meta.dart'; + +typedef SecretstreamXchacha20poly1305State = num; + +typedef SignState = num; + +typedef GenerichashState = num; + +typedef HashSha256State = num; + +typedef HashSha512State = num; + +typedef OnetimeauthState = num; @JS() @anonymous @@ -82,34 +93,6 @@ class SecretStreamPull { }); } -@JS() -@anonymous -class GenerichashState { - @visibleForTesting - external factory GenerichashState(); -} - -@JS() -@anonymous -class HashSha256State { - @visibleForTesting - external factory HashSha256State(); -} - -@JS() -@anonymous -class HashSha512State { - @visibleForTesting - external factory HashSha512State(); -} - -@JS() -@anonymous -class OnetimeauthState { - @visibleForTesting - external factory OnetimeauthState(); -} - @JS() @anonymous class LibSodiumJS { @@ -1141,7 +1124,8 @@ class LibSodiumJS { Uint8List key, ); - external num crypto_secretstream_xchacha20poly1305_init_pull( + external SecretstreamXchacha20poly1305State + crypto_secretstream_xchacha20poly1305_init_pull( Uint8List header, Uint8List key, ); @@ -1153,20 +1137,20 @@ class LibSodiumJS { external Uint8List crypto_secretstream_xchacha20poly1305_keygen(); external dynamic crypto_secretstream_xchacha20poly1305_pull( - num state_address, + SecretstreamXchacha20poly1305State state_address, Uint8List cipher, Uint8List? ad, ); external Uint8List crypto_secretstream_xchacha20poly1305_push( - num state_address, + SecretstreamXchacha20poly1305State state_address, Uint8List message_chunk, Uint8List? ad, num tag, ); external bool crypto_secretstream_xchacha20poly1305_rekey( - num state_address, + SecretstreamXchacha20poly1305State state_address, ); external Uint8List crypto_shorthash( @@ -1208,17 +1192,17 @@ class LibSodiumJS { ); external Uint8List crypto_sign_final_create( - num state_address, + SignState state_address, Uint8List privateKey, ); external bool crypto_sign_final_verify( - num state_address, + SignState state_address, Uint8List signature, Uint8List publicKey, ); - external num crypto_sign_init(); + external SignState crypto_sign_init(); external KeyPair crypto_sign_keypair(); @@ -1232,7 +1216,7 @@ class LibSodiumJS { ); external void crypto_sign_update( - num state_address, + SignState state_address, Uint8List message_chunk, ); diff --git a/packages/sodium/pubspec.yaml b/packages/sodium/pubspec.yaml index 818dbda5..4bfa82c7 100644 --- a/packages/sodium/pubspec.yaml +++ b/packages/sodium/pubspec.yaml @@ -7,21 +7,21 @@ environment: sdk: '>=2.13.0 <3.0.0' dependencies: - ffi: ^1.0.0 + ffi: ^1.1.0 freezed_annotation: ^0.14.2 js: ^0.6.3 - meta: ^1.3.0 + meta: ^1.4.0 dev_dependencies: build_runner: ^2.0.4 - coverage: ^1.0.2 + coverage: ^1.0.3 dart_pre_commit: ^2.3.1 ffigen: ^3.0.0 freezed: ^0.14.2 lint: ^1.5.3 mocktail: ^0.1.3 path: ^1.8.0 - test: ^1.17.4 + test: ^1.17.5 tuple: ^2.0.0 ffigen: diff --git a/packages/sodium/tool/libsodium_js_generator/generators/library_generator.dart b/packages/sodium/tool/libsodium_js_generator/generators/library_generator.dart index 5f633c1f..afbd8b70 100644 --- a/packages/sodium/tool/libsodium_js_generator/generators/library_generator.dart +++ b/packages/sodium/tool/libsodium_js_generator/generators/library_generator.dart @@ -13,7 +13,6 @@ class LibraryGenerator { ..writeln("import 'dart:typed_data';") ..writeln() ..writeln("import 'package:js/js.dart';") - ..writeln("import 'package:meta/meta.dart';") ..writeln(); } diff --git a/packages/sodium/tool/libsodium_js_generator/type_mappings.dart b/packages/sodium/tool/libsodium_js_generator/type_mappings.dart index af46b988..3a41116c 100644 --- a/packages/sodium/tool/libsodium_js_generator/type_mappings.dart +++ b/packages/sodium/tool/libsodium_js_generator/type_mappings.dart @@ -6,15 +6,17 @@ import 'file_loader.dart'; class TypeInfo { final String dartType; - final String? template; + final String? typeDef; final bool force; - const TypeInfo(this.dartType, {this.template, this.force = false}); + const TypeInfo( + this.dartType, { + this.typeDef, + this.force = false, + }); } class TypeMappings { - static const _classNameParam = '%{className}'; - static const _mappings = { // simple types 'void': TypeInfo('void'), @@ -59,20 +61,23 @@ class TypeMappings { ), 'crypto_sign_keypair_result': TypeInfo('KeyPair'), 'crypto_sign_seed_keypair_result': TypeInfo('KeyPair'), - // opaque types (states) - 'generichash_state': TypeInfo('GenerichashState', template: 'opaque_type'), + // state typedefs + 'secretstream_xchacha20poly1305_state': TypeInfo( + 'SecretstreamXchacha20poly1305State', + typeDef: 'num', + ), + 'secretstream_xchacha20poly1305_state_address': + TypeInfo('SecretstreamXchacha20poly1305State'), + 'sign_state': TypeInfo('SignState', typeDef: 'num'), + 'sign_state_address': TypeInfo('SignState'), + 'generichash_state': TypeInfo('GenerichashState', typeDef: 'num'), 'generichash_state_address': TypeInfo('GenerichashState'), - 'hash_sha256_state': TypeInfo('HashSha256State', template: 'opaque_type'), + 'hash_sha256_state': TypeInfo('HashSha256State', typeDef: 'num'), 'hash_sha256_state_address': TypeInfo('HashSha256State'), - 'hash_sha512_state': TypeInfo('HashSha512State', template: 'opaque_type'), + 'hash_sha512_state': TypeInfo('HashSha512State', typeDef: 'num'), 'hash_sha512_state_address': TypeInfo('HashSha512State'), - 'onetimeauth_state': TypeInfo('OnetimeauthState', template: 'opaque_type'), + 'onetimeauth_state': TypeInfo('OnetimeauthState', typeDef: 'num'), 'onetimeauth_state_address': TypeInfo('OnetimeauthState'), - // num states - 'secretstream_xchacha20poly1305_state': TypeInfo('num'), - 'secretstream_xchacha20poly1305_state_address': TypeInfo('num'), - 'sign_state': TypeInfo('num'), - 'sign_state_address': TypeInfo('num'), // hidden types 'randombytes_implementation': TypeInfo('Never'), 'randombytes_set_implementation_result': TypeInfo('Never'), @@ -102,23 +107,18 @@ class TypeMappings { bool isForced(String type) => _mappings[type]?.force ?? false; Future writeTypeDefinitions(StringSink sink) async { + for (final info in _mappings.values) { + if (info.typeDef != null) { + sink.writeln('typedef ${info.dartType} = ${info.typeDef};\n'); + } + } + final typeFiles = await fileLoader.listFilesSorted( '.', (file) => file.path.endsWith('.dart.type'), ); - for (final typeFile in typeFiles) { sink.writeln(await typeFile.readAsString()); } - - for (final info in _mappings.values) { - if (info.template != null) { - final templateStr = await fileLoader.loadFile( - '${info.template}.dart.template', - ); - final outStr = templateStr.replaceAll(_classNameParam, info.dartType); - sink.writeln(outStr); - } - } } } diff --git a/packages/sodium/tool/libsodium_js_generator/types/opaque_type.dart.template b/packages/sodium/tool/libsodium_js_generator/types/opaque_type.dart.template deleted file mode 100644 index 2695f032..00000000 --- a/packages/sodium/tool/libsodium_js_generator/types/opaque_type.dart.template +++ /dev/null @@ -1,6 +0,0 @@ -@JS() -@anonymous -class %{className} { - @visibleForTesting - external factory %{className}(); -}