* API Breaking Change: CryptoSystem.verify() should return bool, and reserve errors for error cases, not validation failures.

* API Breaking Change: VeilidAPI.verify_signatures() returns Option<TypedKeySet> now
Fixes #313
This commit is contained in:
Christien Rioux 2024-05-31 16:20:58 -04:00
parent 8e8ee06fe9
commit 05180252e4
36 changed files with 445 additions and 174 deletions

View file

@ -115,6 +115,13 @@ extension DHTRecordDescriptorExt on DHTRecordDescriptor {
return KeyPair(key: owner, secret: ownerSecret!);
}
TypedKey? ownerTypedSecret() {
if (ownerSecret == null) {
return null;
}
return TypedKey(kind: key.kind, value: ownerSecret!);
}
TypedKeyPair? ownerTypedKeyPair() {
if (ownerSecret == null) {
return null;

View file

@ -44,6 +44,10 @@ Object? veilidApiToEncodable(Object? value) {
List<T> Function(dynamic) jsonListConstructor<T>(
T Function(dynamic) jsonConstructor) =>
(dynamic j) => (j as List<dynamic>).map(jsonConstructor).toList();
List<T>? Function(dynamic) optJsonListConstructor<T>(
T Function(dynamic) jsonConstructor) =>
(dynamic j) =>
j == null ? null : (j as List<dynamic>).map(jsonConstructor).toList();
//////////////////////////////////////
/// VeilidVersion
@ -152,8 +156,8 @@ abstract class Veilid {
List<CryptoKind> validCryptoKinds();
Future<VeilidCryptoSystem> getCryptoSystem(CryptoKind kind);
Future<VeilidCryptoSystem> bestCryptoSystem();
Future<List<TypedKey>> verifySignatures(
List<TypedKey> nodeIds, Uint8List data, List<TypedSignature> signatures);
Future<List<TypedKey>?> verifySignatures(List<TypedKey> publicKeys,
Uint8List data, List<TypedSignature> signatures);
Future<List<TypedSignature>> generateSignatures(
Uint8List data, List<TypedKeyPair> keyPairs);
Future<TypedKeyPair> generateKeyPair(CryptoKind kind);

View file

@ -214,7 +214,7 @@ abstract class VeilidCryptoSystem {
Future<Signature> signWithKeyPair(KeyPair keyPair, Uint8List data) =>
sign(keyPair.key, keyPair.secret, data);
Future<void> verify(PublicKey key, Uint8List data, Signature signature);
Future<bool> verify(PublicKey key, Uint8List data, Signature signature);
Future<int> aeadOverhead();
Future<Uint8List> decryptAead(Uint8List body, Nonce nonce,
SharedSecret sharedSecret, Uint8List? associatedData);

View file

@ -1154,7 +1154,7 @@ class VeilidCryptoSystemFFI extends VeilidCryptoSystem {
}
@override
Future<void> verify(
Future<bool> verify(
PublicKey key, Uint8List data, Signature signature) async {
final nativeKey = jsonEncode(key).toNativeUtf8();
final nativeEncodedData = base64UrlNoPadEncode(data).toNativeUtf8();
@ -1164,7 +1164,7 @@ class VeilidCryptoSystemFFI extends VeilidCryptoSystem {
final sendPort = recvPort.sendPort;
_ffi._cryptoVerify(sendPort.nativePort, _kind, nativeKey, nativeEncodedData,
nativeSignature);
return processFutureVoid(recvPort.first);
return processFuturePlain(recvPort.first);
}
@override
@ -1742,7 +1742,7 @@ class VeilidFFI extends Veilid {
VeilidCryptoSystemFFI._(this, _bestCryptoKind());
@override
Future<List<TypedKey>> verifySignatures(List<TypedKey> nodeIds,
Future<List<TypedKey>?> verifySignatures(List<TypedKey> nodeIds,
Uint8List data, List<TypedSignature> signatures) async {
final nativeNodeIds = jsonEncode(nodeIds).toNativeUtf8();
final nativeData = base64UrlNoPadEncode(data).toNativeUtf8();
@ -1752,7 +1752,7 @@ class VeilidFFI extends Veilid {
final sendPort = recvPort.sendPort;
_verifySignatures(
sendPort.nativePort, nativeNodeIds, nativeData, nativeSignatures);
return processFutureJson(
return processFutureOptJson(
jsonListConstructor<TypedKey>(TypedKey.fromJson), recvPort.first);
}

View file

@ -359,7 +359,7 @@ class VeilidCryptoSystemJS extends VeilidCryptoSystem {
]))));
@override
Future<void> verify(PublicKey key, Uint8List data, Signature signature) =>
Future<bool> verify(PublicKey key, Uint8List data, Signature signature) =>
_wrapApiPromise(js_util.callMethod(wasm, 'crypto_verify', [
_kind,
jsonEncode(key),
@ -655,10 +655,10 @@ class VeilidJS extends Veilid {
this, js_util.callMethod(wasm, 'best_crypto_kind', []));
@override
Future<List<TypedKey>> verifySignatures(List<TypedKey> nodeIds,
Future<List<TypedKey>?> verifySignatures(List<TypedKey> nodeIds,
Uint8List data, List<TypedSignature> signatures) async =>
jsonListConstructor(TypedKey.fromJson)(jsonDecode(await _wrapApiPromise(
js_util.callMethod(wasm, 'verify_signatures', [
optJsonListConstructor(TypedKey.fromJson)(jsonDecode(
await _wrapApiPromise(js_util.callMethod(wasm, 'verify_signatures', [
jsonEncode(nodeIds),
base64UrlNoPadEncode(data),
jsonEncode(signatures)