2022-02-06 21:18:42 -05:00
|
|
|
import 'veilid.dart';
|
|
|
|
|
2022-03-17 10:31:10 -04:00
|
|
|
import 'dart:html' as html;
|
|
|
|
import 'dart:js' as js;
|
|
|
|
import 'dart:js_util' as js_util;
|
2022-02-06 21:18:42 -05:00
|
|
|
import 'dart:async';
|
2022-03-15 23:02:24 -04:00
|
|
|
import 'dart:convert';
|
2022-09-30 22:37:55 -04:00
|
|
|
import 'dart:typed_data';
|
2022-02-06 21:18:42 -05:00
|
|
|
|
2023-05-29 15:24:57 -04:00
|
|
|
import 'veilid_encoding.dart';
|
2023-01-04 17:02:45 -05:00
|
|
|
|
2022-02-06 21:18:42 -05:00
|
|
|
//////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
Veilid getVeilid() => VeilidJS();
|
|
|
|
|
2022-03-17 10:31:10 -04:00
|
|
|
Object wasm = js_util.getProperty(html.window, "veilid_wasm");
|
2022-03-15 23:02:24 -04:00
|
|
|
|
2022-03-17 10:31:10 -04:00
|
|
|
Future<T> _wrapApiPromise<T>(Object p) {
|
|
|
|
return js_util.promiseToFuture(p).then((value) => value as T).catchError(
|
|
|
|
(error) => Future<T>.error(
|
|
|
|
VeilidAPIException.fromJson(jsonDecode(error as String))));
|
2022-03-15 23:02:24 -04:00
|
|
|
}
|
|
|
|
|
2022-12-02 22:52:03 -05:00
|
|
|
class _Ctx {
|
|
|
|
final int id;
|
|
|
|
final VeilidJS js;
|
|
|
|
_Ctx(this.id, this.js);
|
|
|
|
}
|
|
|
|
|
2022-11-26 14:16:02 -05:00
|
|
|
// JS implementation of VeilidRoutingContext
|
|
|
|
class VeilidRoutingContextJS implements VeilidRoutingContext {
|
2022-12-02 22:52:03 -05:00
|
|
|
final _Ctx _ctx;
|
2023-05-29 15:24:57 -04:00
|
|
|
static final Finalizer<_Ctx> _finalizer = Finalizer(
|
|
|
|
(ctx) => js_util.callMethod(wasm, "release_routing_context", [ctx.id]));
|
2022-12-02 22:52:03 -05:00
|
|
|
|
|
|
|
VeilidRoutingContextJS._(this._ctx) {
|
|
|
|
_finalizer.attach(this, _ctx, detach: this);
|
|
|
|
}
|
2022-11-26 14:16:02 -05:00
|
|
|
|
|
|
|
@override
|
2022-12-02 22:52:03 -05:00
|
|
|
VeilidRoutingContextJS withPrivacy() {
|
|
|
|
int newId =
|
|
|
|
js_util.callMethod(wasm, "routing_context_with_privacy", [_ctx.id]);
|
|
|
|
return VeilidRoutingContextJS._(_Ctx(newId, _ctx.js));
|
2022-11-26 14:16:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-06-28 11:40:02 -04:00
|
|
|
VeilidRoutingContextJS withCustomPrivacy(SafetySelection safetySelection) {
|
2022-12-02 22:52:03 -05:00
|
|
|
final newId = js_util.callMethod(
|
2023-05-29 15:24:57 -04:00
|
|
|
wasm,
|
|
|
|
"routing_context_with_custom_privacy",
|
2023-06-28 11:40:02 -04:00
|
|
|
[_ctx.id, jsonEncode(safetySelection)]);
|
2022-12-02 22:52:03 -05:00
|
|
|
|
|
|
|
return VeilidRoutingContextJS._(_Ctx(newId, _ctx.js));
|
2022-11-26 14:16:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2022-12-02 22:52:03 -05:00
|
|
|
VeilidRoutingContextJS withSequencing(Sequencing sequencing) {
|
2023-05-29 15:24:57 -04:00
|
|
|
final newId = js_util.callMethod(wasm, "routing_context_with_sequencing",
|
|
|
|
[_ctx.id, jsonEncode(sequencing)]);
|
2022-12-02 22:52:03 -05:00
|
|
|
return VeilidRoutingContextJS._(_Ctx(newId, _ctx.js));
|
2022-11-26 14:16:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<Uint8List> appCall(String target, Uint8List request) async {
|
2023-01-04 17:02:45 -05:00
|
|
|
var encodedRequest = base64UrlNoPadEncode(request);
|
2022-11-26 14:16:02 -05:00
|
|
|
|
2023-01-04 17:02:45 -05:00
|
|
|
return base64UrlNoPadDecode(await _wrapApiPromise(js_util.callMethod(
|
2023-03-01 15:50:30 -05:00
|
|
|
wasm, "routing_context_app_call", [_ctx.id, target, encodedRequest])));
|
2022-11-26 14:16:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2022-12-28 22:53:58 -05:00
|
|
|
Future<void> appMessage(String target, Uint8List message) {
|
2023-01-04 17:02:45 -05:00
|
|
|
var encodedMessage = base64UrlNoPadEncode(message);
|
2022-11-26 14:16:02 -05:00
|
|
|
|
2023-03-01 15:50:30 -05:00
|
|
|
return _wrapApiPromise(js_util.callMethod(wasm,
|
|
|
|
"routing_context_app_message", [_ctx.id, target, encodedMessage]));
|
2022-11-26 14:16:02 -05:00
|
|
|
}
|
2023-05-29 15:24:57 -04:00
|
|
|
|
|
|
|
@override
|
2023-07-08 22:50:44 -04:00
|
|
|
Future<DHTRecordDescriptor> createDHTRecord(DHTSchema schema,
|
|
|
|
{CryptoKind kind = 0}) async {
|
2023-05-29 15:24:57 -04:00
|
|
|
return DHTRecordDescriptor.fromJson(jsonDecode(await _wrapApiPromise(js_util
|
|
|
|
.callMethod(wasm, "routing_context_create_dht_record",
|
2023-07-08 22:50:44 -04:00
|
|
|
[_ctx.id, jsonEncode(schema), kind]))));
|
2023-05-29 15:24:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<DHTRecordDescriptor> openDHTRecord(
|
|
|
|
TypedKey key, KeyPair? writer) async {
|
|
|
|
return DHTRecordDescriptor.fromJson(jsonDecode(await _wrapApiPromise(js_util
|
|
|
|
.callMethod(wasm, "routing_context_open_dht_record", [
|
|
|
|
_ctx.id,
|
|
|
|
jsonEncode(key),
|
|
|
|
writer != null ? jsonEncode(writer) : null
|
|
|
|
]))));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> closeDHTRecord(TypedKey key) {
|
|
|
|
return _wrapApiPromise(js_util.callMethod(
|
|
|
|
wasm, "routing_context_close_dht_record", [_ctx.id, jsonEncode(key)]));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> deleteDHTRecord(TypedKey key) {
|
|
|
|
return _wrapApiPromise(js_util.callMethod(
|
|
|
|
wasm, "routing_context_delete_dht_record", [_ctx.id, jsonEncode(key)]));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<ValueData?> getDHTValue(
|
|
|
|
TypedKey key, int subkey, bool forceRefresh) async {
|
|
|
|
final opt = await _wrapApiPromise(js_util.callMethod(
|
|
|
|
wasm,
|
|
|
|
"routing_context_get_dht_value",
|
|
|
|
[_ctx.id, jsonEncode(key), subkey, forceRefresh]));
|
|
|
|
return opt == null ? null : ValueData.fromJson(jsonDecode(opt));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<ValueData?> setDHTValue(
|
|
|
|
TypedKey key, int subkey, Uint8List data) async {
|
|
|
|
final opt = await _wrapApiPromise(js_util.callMethod(
|
|
|
|
wasm,
|
|
|
|
"routing_context_set_dht_value",
|
|
|
|
[_ctx.id, jsonEncode(key), subkey, base64UrlNoPadEncode(data)]));
|
|
|
|
return opt == null ? null : ValueData.fromJson(jsonDecode(opt));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-06-27 18:26:53 -04:00
|
|
|
Future<Timestamp> watchDHTValues(TypedKey key, List<ValueSubkeyRange> subkeys,
|
2023-05-29 15:24:57 -04:00
|
|
|
Timestamp expiration, int count) async {
|
|
|
|
final ts = await _wrapApiPromise(js_util.callMethod(
|
|
|
|
wasm, "routing_context_watch_dht_values", [
|
|
|
|
_ctx.id,
|
|
|
|
jsonEncode(key),
|
|
|
|
jsonEncode(subkeys),
|
|
|
|
expiration.toString(),
|
|
|
|
count
|
|
|
|
]));
|
|
|
|
return Timestamp.fromString(ts);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-06-27 18:26:53 -04:00
|
|
|
Future<bool> cancelDHTWatch(TypedKey key, List<ValueSubkeyRange> subkeys) {
|
2023-05-29 15:24:57 -04:00
|
|
|
return _wrapApiPromise(js_util.callMethod(
|
|
|
|
wasm,
|
|
|
|
"routing_context_cancel_dht_watch",
|
|
|
|
[_ctx.id, jsonEncode(key), jsonEncode(subkeys)]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// JS implementation of VeilidCryptoSystem
|
|
|
|
class VeilidCryptoSystemJS implements VeilidCryptoSystem {
|
|
|
|
final CryptoKind _kind;
|
|
|
|
final VeilidJS _js;
|
|
|
|
|
|
|
|
VeilidCryptoSystemJS._(this._js, this._kind) {
|
|
|
|
// Keep the reference
|
|
|
|
_js;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
CryptoKind kind() {
|
|
|
|
return _kind;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<SharedSecret> cachedDH(PublicKey key, SecretKey secret) async {
|
|
|
|
return SharedSecret.fromJson(jsonDecode(await _wrapApiPromise(js_util
|
|
|
|
.callMethod(wasm, "crypto_cached_dh",
|
|
|
|
[_kind, jsonEncode(key), jsonEncode(secret)]))));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<SharedSecret> computeDH(PublicKey key, SecretKey secret) async {
|
|
|
|
return SharedSecret.fromJson(jsonDecode(await _wrapApiPromise(js_util
|
|
|
|
.callMethod(wasm, "crypto_compute_dh",
|
|
|
|
[_kind, jsonEncode(key), jsonEncode(secret)]))));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<Uint8List> randomBytes(int len) async {
|
|
|
|
return base64UrlNoPadDecode(await _wrapApiPromise(
|
|
|
|
js_util.callMethod(wasm, "crypto_random_bytes", [_kind, len])));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<int> defaultSaltLength() {
|
|
|
|
return _wrapApiPromise(
|
|
|
|
js_util.callMethod(wasm, "crypto_default_salt_length", [_kind]));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<String> hashPassword(Uint8List password, Uint8List salt) {
|
|
|
|
return _wrapApiPromise(js_util.callMethod(wasm, "crypto_hash_password",
|
|
|
|
[_kind, base64UrlNoPadEncode(password), base64UrlNoPadEncode(salt)]));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<bool> verifyPassword(Uint8List password, String passwordHash) {
|
|
|
|
return _wrapApiPromise(js_util.callMethod(wasm, "crypto_verify_password",
|
|
|
|
[_kind, base64UrlNoPadEncode(password), passwordHash]));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<SharedSecret> deriveSharedSecret(
|
|
|
|
Uint8List password, Uint8List salt) async {
|
|
|
|
return SharedSecret.fromJson(jsonDecode(await _wrapApiPromise(js_util
|
|
|
|
.callMethod(wasm, "crypto_derive_shared_secret", [
|
|
|
|
_kind,
|
|
|
|
base64UrlNoPadEncode(password),
|
|
|
|
base64UrlNoPadEncode(salt)
|
|
|
|
]))));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<Nonce> randomNonce() async {
|
|
|
|
return Nonce.fromJson(jsonDecode(await _wrapApiPromise(
|
|
|
|
js_util.callMethod(wasm, "crypto_random_nonce", [_kind]))));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<SharedSecret> randomSharedSecret() async {
|
|
|
|
return SharedSecret.fromJson(jsonDecode(await _wrapApiPromise(
|
|
|
|
js_util.callMethod(wasm, "crypto_random_shared_secret", [_kind]))));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<KeyPair> generateKeyPair() async {
|
|
|
|
return KeyPair.fromJson(jsonDecode(await _wrapApiPromise(
|
|
|
|
js_util.callMethod(wasm, "crypto_generate_key_pair", [_kind]))));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<HashDigest> generateHash(Uint8List data) async {
|
|
|
|
return HashDigest.fromJson(jsonDecode(await _wrapApiPromise(js_util
|
|
|
|
.callMethod(wasm, "crypto_generate_hash",
|
|
|
|
[_kind, base64UrlNoPadEncode(data)]))));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<bool> validateKeyPair(PublicKey key, SecretKey secret) {
|
|
|
|
return _wrapApiPromise(js_util.callMethod(wasm, "crypto_validate_key_pair",
|
|
|
|
[_kind, jsonEncode(key), jsonEncode(secret)]));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<bool> validateHash(Uint8List data, HashDigest hash) {
|
|
|
|
return _wrapApiPromise(js_util.callMethod(wasm, "crypto_validate_hash",
|
|
|
|
[_kind, base64UrlNoPadEncode(data), jsonEncode(hash)]));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<CryptoKeyDistance> distance(CryptoKey key1, CryptoKey key2) async {
|
|
|
|
return CryptoKeyDistance.fromJson(jsonDecode(await _wrapApiPromise(js_util
|
|
|
|
.callMethod(wasm, "crypto_distance",
|
|
|
|
[_kind, jsonEncode(key1), jsonEncode(key2)]))));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<Signature> sign(
|
|
|
|
PublicKey key, SecretKey secret, Uint8List data) async {
|
|
|
|
return Signature.fromJson(jsonDecode(await _wrapApiPromise(js_util
|
|
|
|
.callMethod(wasm, "crypto_sign", [
|
|
|
|
_kind,
|
|
|
|
jsonEncode(key),
|
|
|
|
jsonEncode(secret),
|
|
|
|
base64UrlNoPadEncode(data)
|
|
|
|
]))));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> verify(PublicKey key, Uint8List data, Signature signature) {
|
|
|
|
return _wrapApiPromise(js_util.callMethod(wasm, "crypto_verify", [
|
|
|
|
_kind,
|
|
|
|
jsonEncode(key),
|
|
|
|
base64UrlNoPadEncode(data),
|
|
|
|
jsonEncode(signature),
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<int> aeadOverhead() {
|
|
|
|
return _wrapApiPromise(
|
|
|
|
js_util.callMethod(wasm, "crypto_aead_overhead", [_kind]));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<Uint8List> decryptAead(Uint8List body, Nonce nonce,
|
|
|
|
SharedSecret sharedSecret, Uint8List? associatedData) async {
|
|
|
|
return base64UrlNoPadDecode(
|
|
|
|
await _wrapApiPromise(js_util.callMethod(wasm, "crypto_decrypt_aead", [
|
|
|
|
_kind,
|
|
|
|
base64UrlNoPadEncode(body),
|
|
|
|
jsonEncode(nonce),
|
|
|
|
jsonEncode(sharedSecret),
|
|
|
|
associatedData != null ? base64UrlNoPadEncode(associatedData) : null
|
|
|
|
])));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<Uint8List> encryptAead(Uint8List body, Nonce nonce,
|
|
|
|
SharedSecret sharedSecret, Uint8List? associatedData) async {
|
|
|
|
return base64UrlNoPadDecode(
|
|
|
|
await _wrapApiPromise(js_util.callMethod(wasm, "crypto_encrypt_aead", [
|
|
|
|
_kind,
|
|
|
|
base64UrlNoPadEncode(body),
|
|
|
|
jsonEncode(nonce),
|
|
|
|
jsonEncode(sharedSecret),
|
|
|
|
associatedData != null ? base64UrlNoPadEncode(associatedData) : null
|
|
|
|
])));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<Uint8List> cryptNoAuth(
|
|
|
|
Uint8List body, Nonce nonce, SharedSecret sharedSecret) async {
|
|
|
|
return base64UrlNoPadDecode(await _wrapApiPromise(js_util.callMethod(
|
|
|
|
wasm, "crypto_crypt_no_auth", [
|
|
|
|
_kind,
|
|
|
|
base64UrlNoPadEncode(body),
|
|
|
|
jsonEncode(nonce),
|
|
|
|
jsonEncode(sharedSecret)
|
|
|
|
])));
|
|
|
|
}
|
2022-11-26 14:16:02 -05:00
|
|
|
}
|
|
|
|
|
2022-12-28 22:53:58 -05:00
|
|
|
class _TDBT {
|
|
|
|
final int id;
|
|
|
|
VeilidTableDBJS tdbjs;
|
|
|
|
VeilidJS js;
|
|
|
|
|
|
|
|
_TDBT(this.id, this.tdbjs, this.js);
|
|
|
|
}
|
|
|
|
|
|
|
|
// JS implementation of VeilidTableDBTransaction
|
|
|
|
class VeilidTableDBTransactionJS extends VeilidTableDBTransaction {
|
|
|
|
final _TDBT _tdbt;
|
2023-05-29 15:24:57 -04:00
|
|
|
static final Finalizer<_TDBT> _finalizer = Finalizer((tdbt) =>
|
|
|
|
js_util.callMethod(wasm, "release_table_db_transaction", [tdbt.id]));
|
2022-12-28 22:53:58 -05:00
|
|
|
|
|
|
|
VeilidTableDBTransactionJS._(this._tdbt) {
|
|
|
|
_finalizer.attach(this, _tdbt, detach: this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> commit() {
|
2023-01-04 14:51:13 -05:00
|
|
|
return _wrapApiPromise(
|
|
|
|
js_util.callMethod(wasm, "table_db_transaction_commit", [_tdbt.id]));
|
2022-12-28 22:53:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> rollback() {
|
2023-01-04 14:51:13 -05:00
|
|
|
return _wrapApiPromise(
|
|
|
|
js_util.callMethod(wasm, "table_db_transaction_rollback", [_tdbt.id]));
|
2022-12-28 22:53:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> store(int col, Uint8List key, Uint8List value) {
|
2023-01-04 17:02:45 -05:00
|
|
|
final encodedKey = base64UrlNoPadEncode(key);
|
|
|
|
final encodedValue = base64UrlNoPadEncode(value);
|
2022-12-28 22:53:58 -05:00
|
|
|
|
2023-01-04 17:02:45 -05:00
|
|
|
return _wrapApiPromise(js_util.callMethod(
|
|
|
|
wasm,
|
|
|
|
"table_db_transaction_store",
|
|
|
|
[_tdbt.id, col, encodedKey, encodedValue]));
|
2022-12-28 22:53:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-06-04 22:08:46 -04:00
|
|
|
Future<void> delete(int col, Uint8List key) {
|
2023-01-04 17:02:45 -05:00
|
|
|
final encodedKey = base64UrlNoPadEncode(key);
|
2022-12-28 22:53:58 -05:00
|
|
|
|
|
|
|
return _wrapApiPromise(js_util.callMethod(
|
2023-01-04 17:02:45 -05:00
|
|
|
wasm, "table_db_transaction_delete", [_tdbt.id, col, encodedKey]));
|
2022-12-28 22:53:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class _TDB {
|
|
|
|
final int id;
|
|
|
|
VeilidJS js;
|
|
|
|
|
|
|
|
_TDB(this.id, this.js);
|
|
|
|
}
|
|
|
|
|
|
|
|
// JS implementation of VeilidTableDB
|
|
|
|
class VeilidTableDBJS extends VeilidTableDB {
|
|
|
|
final _TDB _tdb;
|
2023-05-29 15:24:57 -04:00
|
|
|
static final Finalizer<_TDB> _finalizer = Finalizer(
|
|
|
|
(tdb) => js_util.callMethod(wasm, "release_table_db", [tdb.id]));
|
2022-12-28 22:53:58 -05:00
|
|
|
|
|
|
|
VeilidTableDBJS._(this._tdb) {
|
|
|
|
_finalizer.attach(this, _tdb, detach: this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
int getColumnCount() {
|
|
|
|
return js_util.callMethod(wasm, "table_db_get_column_count", [_tdb.id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-05-29 15:24:57 -04:00
|
|
|
Future<List<Uint8List>> getKeys(int col) async {
|
|
|
|
return jsonListConstructor(base64UrlNoPadDecodeDynamic)(jsonDecode(
|
|
|
|
await js_util.callMethod(wasm, "table_db_get_keys", [_tdb.id, col])));
|
2022-12-28 22:53:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
VeilidTableDBTransaction transact() {
|
|
|
|
final id = js_util.callMethod(wasm, "table_db_transact", [_tdb.id]);
|
|
|
|
|
|
|
|
return VeilidTableDBTransactionJS._(_TDBT(id, this, _tdb.js));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> store(int col, Uint8List key, Uint8List value) {
|
2023-01-04 17:02:45 -05:00
|
|
|
final encodedKey = base64UrlNoPadEncode(key);
|
|
|
|
final encodedValue = base64UrlNoPadEncode(value);
|
2022-12-28 22:53:58 -05:00
|
|
|
|
|
|
|
return _wrapApiPromise(js_util.callMethod(
|
2023-01-04 17:02:45 -05:00
|
|
|
wasm, "table_db_store", [_tdb.id, col, encodedKey, encodedValue]));
|
2022-12-28 22:53:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<Uint8List?> load(int col, Uint8List key) async {
|
2023-01-04 17:02:45 -05:00
|
|
|
final encodedKey = base64UrlNoPadEncode(key);
|
2022-12-28 22:53:58 -05:00
|
|
|
|
2023-01-04 14:51:13 -05:00
|
|
|
String? out = await _wrapApiPromise(
|
2023-01-04 17:02:45 -05:00
|
|
|
js_util.callMethod(wasm, "table_db_load", [_tdb.id, col, encodedKey]));
|
2022-12-28 22:53:58 -05:00
|
|
|
if (out == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-01-04 17:02:45 -05:00
|
|
|
return base64UrlNoPadDecode(out);
|
2022-12-28 22:53:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-05-29 15:24:57 -04:00
|
|
|
Future<Uint8List?> delete(int col, Uint8List key) {
|
2023-01-04 17:02:45 -05:00
|
|
|
final encodedKey = base64UrlNoPadEncode(key);
|
2022-12-28 22:53:58 -05:00
|
|
|
|
2023-01-04 17:02:45 -05:00
|
|
|
return _wrapApiPromise(js_util
|
|
|
|
.callMethod(wasm, "table_db_delete", [_tdb.id, col, encodedKey]));
|
2022-12-28 22:53:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-26 14:16:02 -05:00
|
|
|
// JS implementation of high level Veilid API
|
|
|
|
|
2022-02-13 21:09:43 -05:00
|
|
|
class VeilidJS implements Veilid {
|
2022-06-15 21:51:38 -04:00
|
|
|
@override
|
2022-07-01 16:20:43 -04:00
|
|
|
void initializeVeilidCore(Map<String, dynamic> platformConfigJson) {
|
2023-05-29 15:24:57 -04:00
|
|
|
var platformConfigJsonString = jsonEncode(platformConfigJson);
|
2022-07-01 16:20:43 -04:00
|
|
|
js_util
|
|
|
|
.callMethod(wasm, "initialize_veilid_core", [platformConfigJsonString]);
|
2022-06-15 21:51:38 -04:00
|
|
|
}
|
|
|
|
|
2022-07-01 12:13:52 -04:00
|
|
|
@override
|
|
|
|
void changeLogLevel(String layer, VeilidConfigLogLevel logLevel) {
|
2023-05-29 15:24:57 -04:00
|
|
|
var logLevelJsonString = jsonEncode(logLevel);
|
2022-07-01 12:13:52 -04:00
|
|
|
js_util.callMethod(wasm, "change_log_level", [layer, logLevelJsonString]);
|
|
|
|
}
|
|
|
|
|
2022-03-03 20:45:39 -05:00
|
|
|
@override
|
2022-09-09 16:27:13 -04:00
|
|
|
Future<Stream<VeilidUpdate>> startupVeilidCore(VeilidConfig config) async {
|
2022-03-15 23:02:24 -04:00
|
|
|
var streamController = StreamController<VeilidUpdate>();
|
2022-03-17 10:31:10 -04:00
|
|
|
updateCallback(String update) {
|
|
|
|
var updateJson = jsonDecode(update);
|
|
|
|
if (updateJson["kind"] == "Shutdown") {
|
|
|
|
streamController.close();
|
|
|
|
} else {
|
|
|
|
var update = VeilidUpdate.fromJson(updateJson);
|
|
|
|
streamController.add(update);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-29 15:24:57 -04:00
|
|
|
await _wrapApiPromise(js_util.callMethod(wasm, "startup_veilid_core",
|
|
|
|
[js.allowInterop(updateCallback), jsonEncode(config)]));
|
2022-09-09 16:27:13 -04:00
|
|
|
|
|
|
|
return streamController.stream;
|
2022-02-06 21:18:42 -05:00
|
|
|
}
|
|
|
|
|
2022-03-03 20:45:39 -05:00
|
|
|
@override
|
2022-03-15 23:02:24 -04:00
|
|
|
Future<VeilidState> getVeilidState() async {
|
2022-03-17 10:31:10 -04:00
|
|
|
return VeilidState.fromJson(jsonDecode(await _wrapApiPromise(
|
|
|
|
js_util.callMethod(wasm, "get_veilid_state", []))));
|
2022-02-06 21:18:42 -05:00
|
|
|
}
|
|
|
|
|
2022-09-06 18:59:41 -04:00
|
|
|
@override
|
2022-12-28 22:53:58 -05:00
|
|
|
Future<void> attach() {
|
2022-09-06 18:59:41 -04:00
|
|
|
return _wrapApiPromise(js_util.callMethod(wasm, "attach", []));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2022-12-28 22:53:58 -05:00
|
|
|
Future<void> detach() {
|
2022-09-06 18:59:41 -04:00
|
|
|
return _wrapApiPromise(js_util.callMethod(wasm, "detach", []));
|
|
|
|
}
|
|
|
|
|
2022-03-03 20:45:39 -05:00
|
|
|
@override
|
2022-03-17 10:31:10 -04:00
|
|
|
Future<void> shutdownVeilidCore() {
|
|
|
|
return _wrapApiPromise(
|
|
|
|
js_util.callMethod(wasm, "shutdown_veilid_core", []));
|
2022-02-06 21:18:42 -05:00
|
|
|
}
|
|
|
|
|
2023-05-29 15:24:57 -04:00
|
|
|
@override
|
|
|
|
List<CryptoKind> validCryptoKinds() {
|
|
|
|
return jsonDecode(js_util.callMethod(wasm, "valid_crypto_kinds", []));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<VeilidCryptoSystem> getCryptoSystem(CryptoKind kind) async {
|
|
|
|
if (!validCryptoKinds().contains(kind)) {
|
|
|
|
throw VeilidAPIExceptionGeneric("unsupported cryptosystem");
|
|
|
|
}
|
|
|
|
return VeilidCryptoSystemJS._(this, kind);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<VeilidCryptoSystem> bestCryptoSystem() async {
|
|
|
|
return VeilidCryptoSystemJS._(
|
|
|
|
this, js_util.callMethod(wasm, "best_crypto_kind", []));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<List<TypedKey>> verifySignatures(List<TypedKey> nodeIds,
|
|
|
|
Uint8List data, List<TypedSignature> signatures) async {
|
|
|
|
return jsonListConstructor(TypedKey.fromJson)(jsonDecode(
|
|
|
|
await _wrapApiPromise(js_util.callMethod(wasm, "verify_signatures", [
|
|
|
|
jsonEncode(nodeIds),
|
|
|
|
base64UrlNoPadEncode(data),
|
|
|
|
jsonEncode(signatures)
|
|
|
|
]))));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<List<TypedSignature>> generateSignatures(
|
|
|
|
Uint8List data, List<TypedKeyPair> keyPairs) async {
|
|
|
|
return jsonListConstructor(TypedSignature.fromJson)(jsonDecode(
|
|
|
|
await _wrapApiPromise(js_util.callMethod(wasm, "generate_signatures",
|
|
|
|
[base64UrlNoPadEncode(data), jsonEncode(keyPairs)]))));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<TypedKeyPair> generateKeyPair(CryptoKind kind) async {
|
|
|
|
return TypedKeyPair.fromJson(jsonDecode(await _wrapApiPromise(
|
|
|
|
js_util.callMethod(wasm, "generate_key_pair", [kind]))));
|
|
|
|
}
|
|
|
|
|
2022-03-03 20:45:39 -05:00
|
|
|
@override
|
2022-11-26 14:16:02 -05:00
|
|
|
Future<VeilidRoutingContext> routingContext() async {
|
2023-01-04 14:20:41 -05:00
|
|
|
int id =
|
|
|
|
await _wrapApiPromise(js_util.callMethod(wasm, "routing_context", []));
|
2022-12-02 22:52:03 -05:00
|
|
|
return VeilidRoutingContextJS._(_Ctx(id, this));
|
2022-11-26 14:16:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-03-01 15:50:30 -05:00
|
|
|
Future<RouteBlob> newPrivateRoute() async {
|
2023-05-29 15:24:57 -04:00
|
|
|
return RouteBlob.fromJson(jsonDecode(await _wrapApiPromise(
|
|
|
|
js_util.callMethod(wasm, "new_private_route", []))));
|
2022-11-26 14:16:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-03-01 15:50:30 -05:00
|
|
|
Future<RouteBlob> newCustomPrivateRoute(
|
2022-11-26 14:16:02 -05:00
|
|
|
Stability stability, Sequencing sequencing) async {
|
2023-05-29 15:24:57 -04:00
|
|
|
var stabilityString = jsonEncode(stability);
|
|
|
|
var sequencingString = jsonEncode(sequencing);
|
2022-11-26 14:16:02 -05:00
|
|
|
|
2023-05-29 15:24:57 -04:00
|
|
|
return RouteBlob.fromJson(jsonDecode(await _wrapApiPromise(js_util
|
2022-12-02 22:52:03 -05:00
|
|
|
.callMethod(
|
2023-05-29 15:24:57 -04:00
|
|
|
wasm, "new_private_route", [stabilityString, sequencingString]))));
|
2022-11-26 14:16:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2022-12-28 22:53:58 -05:00
|
|
|
Future<String> importRemotePrivateRoute(Uint8List blob) {
|
2023-01-04 17:02:45 -05:00
|
|
|
var encodedBlob = base64UrlNoPadEncode(blob);
|
2022-11-26 14:16:02 -05:00
|
|
|
return _wrapApiPromise(
|
|
|
|
js_util.callMethod(wasm, "import_remote_private_route", [encodedBlob]));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2022-12-28 22:53:58 -05:00
|
|
|
Future<void> releasePrivateRoute(String key) {
|
2022-11-26 14:16:02 -05:00
|
|
|
return _wrapApiPromise(
|
|
|
|
js_util.callMethod(wasm, "release_private_route", [key]));
|
2022-03-03 20:45:39 -05:00
|
|
|
}
|
|
|
|
|
2022-09-30 22:37:55 -04:00
|
|
|
@override
|
2023-06-16 11:57:55 -04:00
|
|
|
Future<void> appCallReply(String callId, Uint8List message) {
|
2023-01-04 17:02:45 -05:00
|
|
|
var encodedMessage = base64UrlNoPadEncode(message);
|
2022-09-30 22:37:55 -04:00
|
|
|
return _wrapApiPromise(
|
2023-06-16 11:57:55 -04:00
|
|
|
js_util.callMethod(wasm, "app_call_reply", [callId, encodedMessage]));
|
2022-09-30 22:37:55 -04:00
|
|
|
}
|
2022-12-02 22:52:03 -05:00
|
|
|
|
2022-12-28 22:53:58 -05:00
|
|
|
@override
|
|
|
|
Future<VeilidTableDB> openTableDB(String name, int columnCount) async {
|
|
|
|
int id = await _wrapApiPromise(
|
|
|
|
js_util.callMethod(wasm, "open_table_db", [name, columnCount]));
|
|
|
|
return VeilidTableDBJS._(_TDB(id, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<bool> deleteTableDB(String name) {
|
|
|
|
return _wrapApiPromise(js_util.callMethod(wasm, "delete_table_db", [name]));
|
|
|
|
}
|
|
|
|
|
2023-05-29 15:24:57 -04:00
|
|
|
@override
|
|
|
|
Timestamp now() {
|
|
|
|
return Timestamp.fromString(js_util.callMethod(wasm, "now", []));
|
|
|
|
}
|
|
|
|
|
2022-11-26 14:16:02 -05:00
|
|
|
@override
|
2022-12-10 19:11:58 -05:00
|
|
|
Future<String> debug(String command) async {
|
2023-01-04 14:20:41 -05:00
|
|
|
return await _wrapApiPromise(js_util.callMethod(wasm, "debug", [command]));
|
2022-11-26 14:16:02 -05:00
|
|
|
}
|
2022-09-30 22:37:55 -04:00
|
|
|
|
2022-03-03 20:45:39 -05:00
|
|
|
@override
|
2022-03-17 10:31:10 -04:00
|
|
|
String veilidVersionString() {
|
|
|
|
return js_util.callMethod(wasm, "veilid_version_string", []);
|
2022-02-06 21:18:42 -05:00
|
|
|
}
|
|
|
|
|
2022-03-03 20:45:39 -05:00
|
|
|
@override
|
2022-03-17 10:31:10 -04:00
|
|
|
VeilidVersion veilidVersion() {
|
2022-12-02 22:52:03 -05:00
|
|
|
Map<String, dynamic> jsonVersion =
|
2022-03-17 10:31:10 -04:00
|
|
|
jsonDecode(js_util.callMethod(wasm, "veilid_version", []));
|
2022-03-15 23:02:24 -04:00
|
|
|
return VeilidVersion(
|
|
|
|
jsonVersion["major"], jsonVersion["minor"], jsonVersion["patch"]);
|
2022-02-06 21:18:42 -05:00
|
|
|
}
|
|
|
|
}
|