Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(flutter/catalyst_key_derivation): Key derivation integration #1149

Merged
merged 158 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
158 commits
Select commit Hold shift + click to select a range
d872480
feat(cat-voice-package): add rust key derivation implementation
bkioshn Oct 29, 2024
939d4a2
refactor: move uikit_example into utilities dir
damian-molinski Oct 29, 2024
e387a27
fix: exclude example packages from melos bootstrap
damian-molinski Oct 29, 2024
8847b78
refactor: move poc_local_storage into catalyst_voices/utilities
damian-molinski Oct 29, 2024
c90d392
fix: key derivation implementation
bkioshn Oct 29, 2024
9be664a
fix: auto gen file
bkioshn Oct 29, 2024
7960858
refactor: move catalyst_voices_remote_widgets package to catalyst_voi…
damian-molinski Oct 29, 2024
376cfb3
refactor: move scripts into catalyst_voices dir
damian-molinski Oct 29, 2024
93c9d24
refactor: move catalyst_voices/packages into catalyst_voices/packages…
damian-molinski Oct 29, 2024
cdd42d2
refactor: move catalyst_voices_packages into catalyst_voices/packages…
damian-molinski Oct 29, 2024
9571bc5
refactor: move melos.yaml into catalyst_voices
damian-molinski Oct 29, 2024
021c13d
fix: add word to dict
bkioshn Oct 30, 2024
78eccc9
fix: key derivation implementation
bkioshn Oct 30, 2024
b971eb8
Merge branch 'main' into feat/rust-key-derivation
bkioshn Oct 30, 2024
6d58c65
fix: remove simple rs
bkioshn Oct 30, 2024
c7a3ab3
Merge branch 'main' into feat/rust-key-derivation
bkioshn Oct 30, 2024
31f1237
fix: flutter format
bkioshn Oct 30, 2024
557748b
refactor: move app into apps/voices dir
damian-molinski Oct 30, 2024
166cf2d
refactor: rename packages/external to packages/libs
damian-molinski Oct 30, 2024
f0d301f
docs: update README
damian-molinski Oct 30, 2024
f4dd7b2
style: dart lint issues
dtscalac Oct 30, 2024
e0cde31
fix: builder target
damian-molinski Oct 30, 2024
665a38c
fix: update paths in Earthfile
damian-molinski Oct 30, 2024
9057dab
fix: uikit_example pubspec + libs examples deps override
damian-molinski Oct 30, 2024
75266d2
chore: test
damian-molinski Oct 30, 2024
13aef88
Merge main into refactor/self-contained-frontend_899
damian-molinski Oct 30, 2024
5bcc56d
fix: update cspell and check markdown rules
damian-molinski Oct 30, 2024
b02550d
fix: README formatting
damian-molinski Oct 30, 2024
e45f1a5
fix: add description to module
bkioshn Oct 31, 2024
63ef11f
fix: add cspell dict
bkioshn Oct 31, 2024
c056496
feat: add sign data, verify sig, get pubkey
bkioshn Oct 31, 2024
b137655
fix: auto gen file
bkioshn Oct 31, 2024
d1f523a
feat: add rust project setup files
bkioshn Oct 31, 2024
2cd1435
fix: flutter example
bkioshn Oct 31, 2024
ce24ad6
fix: build-web target path
damian-molinski Oct 31, 2024
7bcf37e
fix: remove build-web target suffix separator
damian-molinski Oct 31, 2024
431fef3
fix: wallet-automation tests target path
damian-molinski Oct 31, 2024
e581615
fix: wallet-automation builder target
damian-molinski Oct 31, 2024
5948d84
fix: voices automation tests target
damian-molinski Oct 31, 2024
79ae37c
fix: code-generator target path
damian-molinski Oct 31, 2024
1e0be6a
fix: typo and comment
bkioshn Oct 31, 2024
b6f981f
fix: vscode launch.recommended paths
damian-molinski Oct 31, 2024
9009c0a
feat: catalyst_voices justfile
damian-molinski Oct 31, 2024
9497665
refactor: rename catalyst_voices_remote_widgets to remote_widgets
damian-molinski Oct 31, 2024
0e3d066
fix: remote_widget spelling and licence checks
damian-molinski Oct 31, 2024
7243017
fix: remote_widget analyzer
damian-molinski Oct 31, 2024
dfcb159
Merge branch 'main' into refactor/self-contained-frontend_899
damian-molinski Oct 31, 2024
6162c45
Merge branch 'main' into feat/rust-key-derivation
bkioshn Oct 31, 2024
13467fc
refactor: move repo-catalyst-voices from root Earthfile to catalyst_v…
damian-molinski Oct 31, 2024
1d0b9bb
fix: restructure rust key derivation and solve dispose value
bkioshn Nov 1, 2024
7c6224b
fix: auto gen file
bkioshn Nov 1, 2024
5eabfc5
fix: ignore auto gen file
bkioshn Nov 1, 2024
b830da7
Merge branch 'main' into feat/rust-key-derivation
bkioshn Nov 1, 2024
2d4c082
fix: remove auto gen file
bkioshn Nov 1, 2024
947d63c
fix: add justfile, fix readme, and remove run.sh
bkioshn Nov 1, 2024
d5f7706
fix: markdown
bkioshn Nov 1, 2024
15770e2
fix: update git ignore
bkioshn Nov 1, 2024
8d10bb9
fix: ignore spelling check for dart auto gen file
bkioshn Nov 1, 2024
f026073
chore: add flutter_rust_bridge headers to launch.recommended.json for…
dtscalac Nov 3, 2024
21ca66c
feat: add loader to transaction panel
dtscalac Nov 3, 2024
d6a637a
chore: move ed25519 code to catalyst_key_derivation package
dtscalac Nov 3, 2024
0b7bfa8
fix: add ci check and build
bkioshn Nov 4, 2024
d397252
fix: add earthfile and fix justfile
bkioshn Nov 4, 2024
2c3a619
fix: deprecated api
dtscalac Nov 4, 2024
dcd9f6c
Merge branch 'main' into refactor/self-contained-frontend_899
damian-molinski Nov 4, 2024
e7e426c
fix: file drop
damian-molinski Nov 4, 2024
5590248
fix: force downgraded version of flutter_dropzone
damian-molinski Nov 4, 2024
3c8d52c
chore: update repository links + codeowners paths
damian-molinski Nov 4, 2024
16853de
fix: deploy uikit workflow
damian-molinski Nov 4, 2024
c28f127
fix: uikit_example earthly build target
damian-molinski Nov 4, 2024
84779f6
fix: uikit deploy workflow path
damian-molinski Nov 4, 2024
b0a790c
chore: integration tests docs readme paths
damian-molinski Nov 4, 2024
f8e3f68
chore: remote widget readme path
damian-molinski Nov 4, 2024
6572b54
chore: markdown ignore macos pods path fix
damian-molinski Nov 4, 2024
171e4f9
chore: integration test scripts
damian-molinski Nov 4, 2024
6041b2f
chore: build_runner assets
damian-molinski Nov 4, 2024
b9b8543
chore: document flutter_dropzone issue
dtscalac Nov 4, 2024
c9f7186
Merge branch 'refactor/self-contained-frontend_899' into feat/rust-ke…
dtscalac Nov 4, 2024
9fcde96
chore: revert dropzone fixes
dtscalac Nov 4, 2024
015d0e9
chore: regenerate code
dtscalac Nov 4, 2024
63c259c
Merge branch 'feat/rust-key-derivation' into feat/dart-key-derivation
dtscalac Nov 4, 2024
0bfb73d
feat: key derivation
dtscalac Nov 4, 2024
a20e18d
fix: remove frb generated rust file
bkioshn Nov 4, 2024
f627f07
fix: rename
bkioshn Nov 4, 2024
d62c7e7
fix: add word to dict
bkioshn Nov 4, 2024
f9a6de5
fix: cspell ignore auto gen flutter file
bkioshn Nov 4, 2024
fe549cc
fix: auto gen file
bkioshn Nov 4, 2024
1b2dfa6
fix: file path cspell
bkioshn Nov 4, 2024
75d5141
Merge branch 'main' into feat/rust-key-derivation
bkioshn Nov 4, 2024
abee76b
Merge branch 'main' into feat/rust-key-derivation
bkioshn Nov 4, 2024
b4fdb3b
test: check target
bkioshn Nov 4, 2024
6d22b27
fix: rust earthfile
bkioshn Nov 4, 2024
365528c
chore: add blueprint.cue along with new Earthfile
bkioshn Nov 4, 2024
2598895
chore: fix blueprint.cue
bkioshn Nov 4, 2024
757e30a
fix: linter and format
bkioshn Nov 5, 2024
664be71
fix: remove simple.rs
bkioshn Nov 5, 2024
0ebcea6
Merge branch 'main' into feat/rust-key-derivation
dtscalac Nov 5, 2024
6be7f7a
Merge branch 'main' into feat/rust-key-derivation
dtscalac Nov 5, 2024
be33804
fix: ci check create dummy
bkioshn Nov 5, 2024
fc9a703
Merge branch 'feat/rust-key-derivation' into feat/dart-key-derivation
dtscalac Nov 5, 2024
c905da2
Merge branch 'feat/rust-key-derivation' into feat/dart-key-derivation
dtscalac Nov 5, 2024
13cd50d
chore: adjust breaking changes
dtscalac Nov 5, 2024
1b21484
fix: ci check create dummy
bkioshn Nov 5, 2024
d84d86a
Merge branch 'main' into feat/rust-key-derivation
stevenj Nov 5, 2024
3aea8be
fix: rust ci
bkioshn Nov 5, 2024
d1a67e7
fix: rust format
bkioshn Nov 5, 2024
cbe61e4
feat: add to hex functions
bkioshn Nov 5, 2024
f22ddcf
fix: auto gen file
bkioshn Nov 5, 2024
348bd63
fix: add toHex example to dart
bkioshn Nov 5, 2024
cc8fc40
Merge branch 'main' into feat/rust-key-derivation
bkioshn Nov 6, 2024
637a2cf
fix: tests
dtscalac Nov 6, 2024
332b081
Merge branch 'feat/rust-key-derivation' into feat/dart-key-derivation
dtscalac Nov 6, 2024
dbebb68
docs: update example
dtscalac Nov 6, 2024
61c5ea2
feat: update code generation to include web/pkg, it's needed for pack…
dtscalac Nov 6, 2024
658b2db
chore: code generation
dtscalac Nov 6, 2024
cc4dcfe
fix: initializer
dtscalac Nov 6, 2024
dc984ae
chore: cleanup
dtscalac Nov 6, 2024
131cd96
Merge branch 'main' into feat/rust-key-derivation
bkioshn Nov 6, 2024
df6b7e0
fix: code-generator
bkioshn Nov 6, 2024
04a3bac
Merge branch 'feat/rust-key-derivation' into feat/dart-key-derivation
dtscalac Nov 6, 2024
990d029
Merge branch 'main' into feat/rust-key-derivation
dtscalac Nov 7, 2024
4c6d8b8
Merge branch 'feat/rust-key-derivation' into feat/dart-key-derivation
dtscalac Nov 7, 2024
c477d8c
docs: document how to contribute changes to the package
dtscalac Nov 7, 2024
02045eb
feat: add earthlyignore
dtscalac Nov 8, 2024
8d49f1f
chore: regenerate files
dtscalac Nov 8, 2024
5dce720
fix: bump ci version to 3.2.24
bkioshn Nov 11, 2024
f6c7826
fix: update rustfmt.toml
bkioshn Nov 11, 2024
2da3023
fix: point flutter_rust_bridge ci to 3.2.23
bkioshn Nov 11, 2024
fdeb56c
fix: ci bump version and deny.toml
bkioshn Nov 11, 2024
33a57c2
fix: typo
bkioshn Nov 11, 2024
ef7093f
Merge branch 'main' into feat/rust-key-derivation
bkioshn Nov 11, 2024
84765aa
Merge branch 'main' into feat/dart-key-derivation
dtscalac Nov 11, 2024
5935876
Merge branch 'feat/rust-key-derivation' into feat/dart-key-derivation
dtscalac Nov 11, 2024
b678fbc
chore: add integration tests for catalyst_key_derivation
dtscalac Nov 11, 2024
7948ccf
docs: document that a private key should be dropped
dtscalac Nov 11, 2024
8b7805a
docs: add flutter_rust_bridge issue link
dtscalac Nov 11, 2024
e427de4
style: typo
dtscalac Nov 11, 2024
1d246f2
style: reformat
dtscalac Nov 11, 2024
6963b64
docs: update readme
dtscalac Nov 11, 2024
d745794
docs: update readme
dtscalac Nov 11, 2024
483277e
Merge branch 'main' into feat/rust-key-derivation
dtscalac Nov 12, 2024
580fe8d
Merge branch 'feat/rust-key-derivation' into feat/dart-key-derivation
dtscalac Nov 12, 2024
8f9b019
chore: disable cache
dtscalac Nov 12, 2024
74c007f
chore: workaround integration tests
dtscalac Nov 12, 2024
2b291df
chore: revert workaround
dtscalac Nov 12, 2024
cfbda02
chore: configure nginx headers for flutter_rust_bridge
dtscalac Nov 12, 2024
55e73c4
chore: drop enable-threads.js to test if it fixes ci
dtscalac Nov 12, 2024
98d1295
chore: make global nginx headers
dtscalac Nov 12, 2024
ab218e8
chore: add again the workaround
dtscalac Nov 12, 2024
c1c4b33
chore: don't compile the app for integration tests, they do it themse…
dtscalac Nov 12, 2024
e5d8611
chore: run tests on firefox only
dtscalac Nov 12, 2024
9270a5f
chore: run only on chrome without workaround
dtscalac Nov 12, 2024
3d8d6d8
chore: script load order
dtscalac Nov 12, 2024
7d1097c
chore: reenable firefox tests
dtscalac Nov 13, 2024
9852d7e
docs: clarify todo
dtscalac Nov 13, 2024
3860cc3
feat: skip catalyst key derivation tests if init didn't work
dtscalac Nov 13, 2024
575ad71
chore: update flutter ci
dtscalac Nov 13, 2024
937dc1a
Merge branch 'main' into feat/dart-key-derivation
dtscalac Nov 13, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .earthlyignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
**/*.iml
**/coverage/
**/test_reports/
**/*.log

# node related

Expand Down
7 changes: 6 additions & 1 deletion .vscode/launch.recommended.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
"program": "lib/configs/main_web.dart",
"args": [
"--dart-define",
"SENTRY_DSN=REPLACE_WITH_SENTRY_DSN_URL"
"SENTRY_DSN=REPLACE_WITH_SENTRY_DSN_URL",
// flutter_rust_bridge: https://cjycode.com/flutter_rust_bridge/manual/miscellaneous/web-cross-origin#when-flutter-run
"--web-header",
"Cross-Origin-Opener-Policy=same-origin",
"--web-header",
"Cross-Origin-Embedder-Policy=require-corp"
]
},
{
Expand Down
1 change: 1 addition & 0 deletions catalyst_voices/.earthlyignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
**/*.iml
**/coverage/
**/test_reports/
**/*.log

# node related

Expand Down
6 changes: 5 additions & 1 deletion catalyst_voices/apps/voices/integration_test/Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ VERSION 0.8
IMPORT ../../.. AS catalyst-voices

integration-test-web:
FROM catalyst-voices+build-web
FROM catalyst-voices+builder
dtscalac marked this conversation as resolved.
Show resolved Hide resolved
ARG TARGETARCH
ARG browser
LET driver_port = 4444
Expand All @@ -21,6 +21,9 @@ integration-test-web:
# IF [ $browser = "edge" && $TARGETARCH = "amd64" ]]
# LET driver = "msedgedriver"
# END

WORKDIR /frontend/apps/voices

RUN ($driver --port=$driver_port > $driver.log &) && \
sleep 5 && \
flutter drive --driver=test_driver/integration_tests.dart \
Expand All @@ -35,6 +38,7 @@ integration-test-web:
WAIT
SAVE ARTIFACT $driver.log AS LOCAL $driver.log
END

IF [ -f fail ]
RUN --no-cache echo ""$browser" integration test failed" && \
echo "Printing "$driver" logs..." && \
Expand Down
4 changes: 4 additions & 0 deletions catalyst_voices/apps/voices/lib/configs/bootstrap.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';

import 'package:catalyst_key_derivation/catalyst_key_derivation.dart';
import 'package:catalyst_voices/app/app.dart';
import 'package:catalyst_voices/configs/app_bloc_observer.dart';
import 'package:catalyst_voices/configs/sentry_service.dart';
Expand Down Expand Up @@ -90,6 +91,9 @@ Future<BootstrapArgs> bootstrap() async {

await Dependencies.instance.init();

// Key derivation needs to be initialized before it can be used
await CatalystKeyDerivation.init();

final router = AppRouter.init(
guards: const [
MilestoneGuard(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';

import 'package:catalyst_cardano/catalyst_cardano.dart';
import 'package:catalyst_key_derivation/catalyst_key_derivation.dart';
import 'package:catalyst_voices_blocs/catalyst_voices_blocs.dart';
import 'package:catalyst_voices_repositories/catalyst_voices_repositories.dart';
import 'package:catalyst_voices_services/catalyst_voices_services.dart';
Expand Down Expand Up @@ -66,7 +67,8 @@ final class Dependencies extends DependencyProvider {

void _registerServices() {
registerLazySingleton<Storage>(() => const SecureStorage());
registerLazySingleton<KeyDerivation>(KeyDerivation.new);
registerLazySingleton<CatalystKeyDerivation>(CatalystKeyDerivation.new);
registerLazySingleton<KeyDerivation>(() => KeyDerivation(get()));
registerLazySingleton<KeychainProvider>(VaultKeychainProvider.new);
registerLazySingleton<DummyAuthStorage>(SecureDummyAuthStorage.new);
registerLazySingleton<Downloader>(Downloader.new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class _BlocSummary extends StatelessWidget {
},
builder: (context, state) {
if (state == null) {
return const Offstage();
return const _SummaryPlaceholder();
}

return _Summary(
Expand All @@ -131,6 +131,20 @@ class _BlocSummary extends StatelessWidget {
}
}

class _SummaryPlaceholder extends StatelessWidget {
const _SummaryPlaceholder();

@override
Widget build(BuildContext context) {
return const Center(
child: Padding(
padding: EdgeInsets.all(32),
child: CircularProgressIndicator(),
),
);
}
}

class _Summary extends StatelessWidget {
final Set<AccountRole> roles;
final WalletInfo walletInfo;
Expand Down
1 change: 1 addition & 0 deletions catalyst_voices/apps/voices/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies:
catalyst_cardano: ^0.3.0
catalyst_cardano_serialization: ^0.4.0
catalyst_cardano_web: ^0.3.0
catalyst_key_derivation: ^0.1.0
catalyst_voices_assets:
path: ../../packages/internal/catalyst_voices_assets
catalyst_voices_blocs:
Expand Down
80 changes: 80 additions & 0 deletions catalyst_voices/apps/voices/web/enable-threads.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// TODO(dtscalac): remove workaround when flutter_rust_bridge supports crossOriginIsolated for flutter drive:
// https://github.com/fzyzcjy/flutter_rust_bridge/issues/2407

// https://github.com/orgs/community/discussions/13309#discussioncomment-3844940
// NOTE: This file creates a service worker that cross-origin-isolates the page (read more here: https://web.dev/coop-coep/) which allows us to use wasm threads.
// Normally you would set the COOP and COEP headers on the server to do this, but Github Pages doesn't allow this, so this is a hack to do that.

/* Edited version of: coi-serviceworker v0.1.6 - Guido Zuidhof, licensed under MIT */
// From here: https://github.com/gzuidhof/coi-serviceworker
if (typeof window === 'undefined') {
self.addEventListener("install", () => self.skipWaiting());
self.addEventListener("activate", e => e.waitUntil(self.clients.claim()));

async function handleFetch(request) {
if (request.cache === "only-if-cached" && request.mode !== "same-origin") {
return;
}

if (request.mode === "no-cors") { // We need to set `credentials` to "omit" for no-cors requests, per this comment: https://bugs.chromium.org/p/chromium/issues/detail?id=1309901#c7
request = new Request(request.url, {
cache: request.cache,
credentials: "omit",
headers: request.headers,
integrity: request.integrity,
destination: request.destination,
keepalive: request.keepalive,
method: request.method,
mode: request.mode,
redirect: request.redirect,
referrer: request.referrer,
referrerPolicy: request.referrerPolicy,
signal: request.signal,
});
}

let r = await fetch(request).catch(e => console.error(e));

if (r.status === 0) {
return r;
}

const headers = new Headers(r.headers);
// NOTE https://github.com/fzyzcjy/flutter_rust_bridge/issues/1618 changes to require-corp
headers.set("Cross-Origin-Embedder-Policy", "require-corp"); // credentialless or require-corp
headers.set("Cross-Origin-Opener-Policy", "same-origin");

return new Response(r.body, { status: r.status, statusText: r.statusText, headers });
}

self.addEventListener("fetch", function (e) {
e.respondWith(handleFetch(e.request)); // respondWith must be executed synchonously (but can be passed a Promise)
});

} else {
(async function () {
if (window.crossOriginIsolated !== false) return;

let registration = await navigator.serviceWorker.register(window.document.currentScript.src).catch(e => console.error("COOP/COEP Service Worker failed to register:", e));
if (registration) {
console.log("COOP/COEP Service Worker registered", registration.scope);

registration.addEventListener("updatefound", () => {
console.log("Reloading page to make use of updated COOP/COEP Service Worker.");
window.location.reload();
});

// If the registration is active, but it's not controlling the page
if (registration.active && !navigator.serviceWorker.controller) {
console.log("Reloading page to make use of COOP/COEP Service Worker.");
window.location.reload();
}
}
})();
}

// Code to deregister:
// let registrations = await navigator.serviceWorker.getRegistrations();
// for(let registration of registrations) {
// await registration.unregister();
// }
4 changes: 3 additions & 1 deletion catalyst_voices/apps/voices/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
</head>

<body>
<script src="flutter_bootstrap.js" async></script>
<!-- TODO(dtscalac): make flutter_bootstrap.js just async when enable-threads.js is no longer needed -->
<script src="flutter_bootstrap.js" async="false"></script>
<script src="enable-threads.js" async="false"></script>
</body>

</html>
5 changes: 5 additions & 0 deletions catalyst_voices/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ http {
server {
listen 80;
server_name localhost;

# https://cjycode.com/flutter_rust_bridge/manual/miscellaneous/web-cross-origin#background
add_header Cross-Origin-Opener-Policy "same-origin";
add_header Cross-Origin-Embedder-Policy "require-corp";

location / {
root /app;
index index.html;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';

import 'package:catalyst_cardano_serialization/catalyst_cardano_serialization.dart';
import 'package:catalyst_key_derivation/catalyst_key_derivation.dart';
import 'package:catalyst_voices_blocs/catalyst_voices_blocs.dart';
import 'package:catalyst_voices_blocs/src/registration/cubits/keychain_creation_cubit.dart';
import 'package:catalyst_voices_blocs/src/registration/cubits/recover_cubit.dart';
Expand All @@ -27,7 +28,7 @@ final class RegistrationCubit extends Cubit<RegistrationState>
final RegistrationService _registrationService;
final RegistrationProgressNotifier _progressNotifier;

Ed25519KeyPair? _keyPair;
Bip32Ed25519XPrivateKey? _masterKey;
Transaction? _transaction;

/// Returns [RegistrationCubit] if found in widget tree. Does not add
Expand Down Expand Up @@ -168,20 +169,18 @@ final class RegistrationCubit extends Cubit<RegistrationState>
final wallet = _walletLinkCubit.selectedWallet!;
final roles = _walletLinkCubit.roles;

final keyPair = await _registrationService.deriveAccountRoleKeyPair(
seedPhrase: seedPhrase,
roles: roles,
);
final masterKey =
await _registrationService.deriveMasterKey(seedPhrase: seedPhrase);

final transaction = await _registrationService.prepareRegistration(
wallet: wallet,
// TODO(dtscalac): inject the networkId
networkId: NetworkId.testnet,
keyPair: keyPair,
masterKey: masterKey,
roles: roles,
);

_keyPair = keyPair;
_masterKey = masterKey;
_transaction = transaction;

final fee = transaction.body.fee;
Expand All @@ -197,7 +196,8 @@ final class RegistrationCubit extends Cubit<RegistrationState>
} on RegistrationException catch (error, stackTrace) {
_logger.severe('Prepare registration', error, stackTrace);

_keyPair = null;
_masterKey?.drop();
_masterKey = null;
_transaction = null;

final exception = LocalizedRegistrationException.from(error);
Expand All @@ -220,7 +220,7 @@ final class RegistrationCubit extends Cubit<RegistrationState>
),
);

final keyPair = _keyPair!;
final masterKey = _masterKey!;
final transaction = _transaction!;

final password = _keychainCreationCubit.password;
Expand All @@ -234,8 +234,7 @@ final class RegistrationCubit extends Cubit<RegistrationState>
unsignedTx: transaction,
roles: roles,
lockFactor: lockFactor,
// TODO(dtscalac): Update key value when derivation is final.
keyPair: keyPair,
masterKey: masterKey,
);

await _userService.useAccount(account);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies:
catalyst_cardano: ^0.3.0
catalyst_cardano_serialization: ^0.4.0
catalyst_cardano_web: ^0.3.0
catalyst_key_derivation: ^0.1.0
catalyst_voices_brands:
path: ../catalyst_voices_brands
catalyst_voices_models:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
import 'package:catalyst_cardano_serialization/catalyst_cardano_serialization.dart';
import 'package:catalyst_key_derivation/catalyst_key_derivation.dart';
import 'package:catalyst_voices_models/catalyst_voices_models.dart';
import 'package:ed25519_hd_key/ed25519_hd_key.dart';

/// Derives key pairs from a seed phrase.
final class KeyDerivation {
const KeyDerivation();
final CatalystKeyDerivation _keyDerivation;

/// Derives an [Ed25519KeyPair] from a [seedPhrase] and [path].
const KeyDerivation(this._keyDerivation);

Future<Bip32Ed25519XPrivateKey> deriveMasterKey({
required SeedPhrase seedPhrase,
}) {
return _keyDerivation.deriveMasterKey(
mnemonic: seedPhrase.mnemonic,
);
}

/// Derives an [Ed25519KeyPair] from a [masterKey] and [path].
///
/// Example [path]: m/0'/2147483647'
///
// TODO(dtscalac): this takes around 2.5s to execute, optimize it
// or move to a JS web worker.
Future<Ed25519KeyPair> deriveKeyPair({
required SeedPhrase seedPhrase,
Future<Bip32Ed25519XKeyPair> deriveKeyPair({
required Bip32Ed25519XPrivateKey masterKey,
required String path,
}) async {
final masterKey = await ED25519_HD_KEY.derivePath(
path,
seedPhrase.uint8ListSeed,
);
final privateKey = await masterKey.derivePrivateKey(path: path);
final publicKey = await privateKey.derivePublicKey();

final privateKey = masterKey.key;
final publicKey = await ED25519_HD_KEY.getPublicKey(privateKey, false);

return Ed25519KeyPair(
publicKey: Ed25519PublicKey.fromBytes(publicKey),
privateKey: Ed25519PrivateKey.fromBytes(privateKey),
return Bip32Ed25519XKeyPair(
publicKey: publicKey,
privateKey: privateKey,
);
}

/// Derives the [Ed25519KeyPair] for the [role] from a [seedPhrase].
Future<Ed25519KeyPair> deriveAccountRoleKeyPair({
required SeedPhrase seedPhrase,
/// Derives the [Ed25519KeyPair] for the [role] from a [masterKey].
Future<Bip32Ed25519XKeyPair> deriveAccountRoleKeyPair({
required Bip32Ed25519XPrivateKey masterKey,
required AccountRole role,
}) async {
return deriveKeyPair(
seedPhrase: seedPhrase,
masterKey: masterKey,
path: _roleKeyDerivationPath(role),
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:catalyst_cardano_serialization/catalyst_cardano_serialization.dart';
import 'package:catalyst_key_derivation/catalyst_key_derivation.dart';
import 'package:catalyst_voices_models/catalyst_voices_models.dart';
import 'package:catalyst_voices_services/src/lockable.dart';

Expand All @@ -9,9 +9,9 @@ abstract interface class Keychain implements Lockable {

Future<KeychainMetadata> get metadata;

Future<Ed25519PrivateKey?> getMasterKey();
Future<Bip32Ed25519XPrivateKey?> getMasterKey();

Future<void> setMasterKey(Ed25519PrivateKey key);
Future<void> setMasterKey(Bip32Ed25519XPrivateKey key);

Future<void> clear();
}
Loading
Loading