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: rust key derivation #1063

Open
wants to merge 105 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
105 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
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
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
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
131cd96
Merge branch 'main' into feat/rust-key-derivation
bkioshn Nov 6, 2024
df6b7e0
fix: code-generator
bkioshn Nov 6, 2024
990d029
Merge branch 'main' into feat/rust-key-derivation
dtscalac Nov 7, 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
3 changes: 3 additions & 0 deletions .config/dictionaries/project.dic
Original file line number Diff line number Diff line change
Expand Up @@ -319,5 +319,8 @@ xcodeproj
xctest
xctestrun
xcworkspace
xprv
xpub
xpublic
xvfb
yoroi
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ migrate_working_dir/
**/doc/api/
.dart_tool/
build/
/web/pkg
/web/pkg

# Ignore all auto generated file for rust
rust/src/frb_generated.rs
23 changes: 23 additions & 0 deletions catalyst_voices/packages/libs/catalyst_key_derivation/Earthfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
VERSION 0.8

IMPORT github.com/input-output-hk/catalyst-ci/earthly/flutter_rust_bridge:feat/add-flutter-rust-bridge AS flutter_rust_bridge

builder:
FROM flutter_rust_bridge+builder
COPY . .

# Generated necessary files for running Flutter web locally and save it locally.
code-generator:
ARG local = false
FROM +builder
DO flutter_rust_bridge+CODE_GENERATOR_WEB

IF [ $local = true ]
SAVE ARTIFACT ./assets/js AS LOCAL ./assets/js
SAVE ARTIFACT ./rust/src/frb_generated.rs AS LOCAL ./rust/src/frb_generated.rs
SAVE ARTIFACT ./lib/src AS LOCAL ./lib/src
ELSE
SAVE ARTIFACT ./assets/js assets_js
SAVE ARTIFACT ./rust/src/frb_generated.rs frb_generated.rs
SAVE ARTIFACT ./lib/src src
END
12 changes: 12 additions & 0 deletions catalyst_voices/packages/libs/catalyst_key_derivation/Justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# use with https://github.com/casey/just
#
# Catalyst Key Derivation developers' convenience functions.

# Generated necessary files using Earthly for running Flutter web locally.
code-gen-web:
earthly +code-generator

# Run local Flutter web.
run-web: code-gen-web
cd example
flutter run --web-header=Cross-Origin-Opener-Policy=same-origin --web-header=Cross-Origin-Embedder-Policy=require-corp -d chrome
13 changes: 10 additions & 3 deletions catalyst_voices/packages/libs/catalyst_key_derivation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* [Content](#content)
* [Features](#features)
* [References](#references)
* [Requirements](#requirements)
* [Install](#install)
* [How to run](#how-to-run)
Expand All @@ -11,7 +12,14 @@

## Features

This package exposes a CIP-1852 Cardano HD Key Derivation.
This package expose BIP32-Ed25519 and CIP-1852 Cardano HD Key Derivation.

## References

* [BIP32-Ed25519](https://input-output-hk.github.io/adrestia/static/Ed25519_BIP.pdf)
* [SLIP-0023](https://github.com/satoshilabs/slips/blob/master/slip-0023.md)
* [CIP-1852](https://cips.cardano.org/cip/CIP-1852)
* [BIP-0032](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki)

## Requirements

Expand All @@ -27,8 +35,7 @@ dependencies:

## How to run

1. cd catalyst_key_derivation
2. ./run.sh
1. just run-web

## Example

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
version: "1.0.0"
project: name: "catalyst-key-derivation"
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore_for_file: avoid_print

import 'package:catalyst_key_derivation/catalyst_key_derivation.dart';
import 'package:flutter/material.dart';

Expand Down Expand Up @@ -34,7 +36,32 @@ class MyApp extends StatelessWidget {
}

Future<void> _doMagic() async {
// ignore: avoid_print
print(await greet(name: 'Tom'));
final xprv = await mnemonicToXprv(
mnemonic: 'prevent company field green slot measure chief'
' hero apple task eagle sunset endorse dress seed',
);
print('Master xprv ${xprv.inner}');

final xpub = await xprv.xpublicKey();
print('Master xpub ${xpub.inner}');

final data = [1, 2, 3, 4];
final sig = await xprv.signData(data: data);

final checkXprvSig = await xprv.verifySignature(data: data, signature: sig);
print('Check signature by using xprv $checkXprvSig');

final checkXpubSig = await xpub.verifySignature(data: data, signature: sig);
print('Check signature by using xpub $checkXpubSig');

const path = "m/1852'/1815'/0'/2/0";
final childXprv = await xprv.deriveXprv(path: path);
print('Derive xprv with $path: ${childXprv.inner}');

final childXprvHex = childXprv.toHex();
print('Master xprv hex $childXprvHex');

xprv.drop();
print('Master xprv dropped ${xprv.inner}');
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library catalyst_key_derivation;

export 'src/rust/api/simple.dart';
export 'src/rust/api/key_derivation.dart';
export 'src/rust/frb_generated.dart' show RustLib;
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
// This file is automatically generated, so please do not edit it.
// @generated by `flutter_rust_bridge`@ 2.5.1.

// ignore_for_file: invalid_use_of_internal_member, unused_import, unnecessary_import

import '../frb_generated.dart';
import 'package:collection/collection.dart';
import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart';

// These functions are ignored because they are not marked as `pub`: `derive_xprv_helper`, `mnemonic_to_xprv_helper`, `sign_data_helper`, `verify_signature_xprv_helper`, `verify_signature_xpub_helper`, `xpublic_key_helper`
// These function are ignored because they are on traits that is not defined in current crate (put an empty `#[frb]` on it to unignore): `assert_receiver_is_total_eq`, `assert_receiver_is_total_eq`, `assert_receiver_is_total_eq`, `clone`, `clone`, `clone`, `eq`, `eq`, `eq`, `fmt`, `fmt`, `fmt`, `from`, `from`

/// Generate a new extended private key (`XPrv`) from a mnemonic and passphrase.
/// Note that this function only works with BIP-0039 mnemonics.
/// For more information: Cardano Icarus master node derivation
/// <https://github.com/satoshilabs/slips/blob/master/slip-0023.md>
///
/// # Arguments
///
/// - `mnemonic`: A string representing the mnemonic.
/// - `passphrase`: An optional string representing the passphrase (aka. password).
///
/// # Returns
///
/// Returns a bytes of extended private key as a `Result`.
///
/// # Errors
///
/// Returns an error if the mnemonic is invalid.
Future<Bip32Ed25519XPrivateKey> mnemonicToXprv(
{required String mnemonic, String? passphrase}) =>
RustLib.instance.api.crateApiKeyDerivationMnemonicToXprv(
mnemonic: mnemonic, passphrase: passphrase);

// Rust type: RustOpaqueMoi<flutter_rust_bridge::for_generated::RustAutoOpaqueInner<Bip32Ed25519Signature>>
abstract class Bip32Ed25519Signature implements RustOpaqueInterface {
/// Get the inner bytes.
U8Array64 get inner;

/// Create a new `Bip32Ed25519Signature` from the given bytes.
factory Bip32Ed25519Signature({required U8Array64 sigBytes}) =>
RustLib.instance.api
.crateApiKeyDerivationBip32Ed25519SignatureNew(sigBytes: sigBytes);

/// Convert to a hex string.
String toHex();
}

// Rust type: RustOpaqueMoi<flutter_rust_bridge::for_generated::RustAutoOpaqueInner<Bip32Ed25519XPrivateKey>>
abstract class Bip32Ed25519XPrivateKey implements RustOpaqueInterface {
/// Derive a new extended private key from the given extended private key.
/// - V2 derivation scheme is used as it is mention in [SLIP-0023](https://github.com/satoshilabs/slips/blob/master/slip-0023.md).
/// - More information about child key derivation can be found in [BIP32-Ed25519](https://input-output-hk.github.io/adrestia/static/Ed25519_BIP.pdf).
///
/// # Arguments
///
/// - `xprv_bytes`: An extended private key bytes of type `Bip32Ed25519XPrivateKey`.
/// - `path`: Derivation path. eg. m/0/2'/3 where ' represents hardened derivation.
///
/// # Returns
///
/// Returns a bytes of extended private key as a `Result`.
///
/// # Errors
///
/// Returns an error if the derivation path is invalid.
Future<Bip32Ed25519XPrivateKey> deriveXprv({required String path});

/// Drop the extended private key.
void drop();

/// Extract the chain code from the extended private key.
/// The chain code is the last 32 bytes of the extended private key.
///
/// # Returns
///
/// Returns a 32 length bytes representing the chain code.
U8Array32 get chainCode;

/// Extract the extended secret key from the extended private key.
/// The extended secret key is the first 64 bytes of the extended private key.
///
/// # Returns
///
/// Returns a 64 length bytes representing the extended secret key.
U8Array64 get extendedSecretKey;

/// Get the inner bytes.
U8Array96 get inner;

/// Create a new `Bip32Ed25519XPrivateKey` from the given bytes.
factory Bip32Ed25519XPrivateKey({required U8Array96 xprvBytes}) => RustLib
.instance.api
.crateApiKeyDerivationBip32Ed25519XPrivateKeyNew(xprvBytes: xprvBytes);

/// Sign the given data with the given extended private key.
///
/// # Arguments
///
/// - `data`: The data to sign.
///
/// # Returns
/// Returns a 64 length bytes `Bip32Ed25519Signature` representing the signature.
///
/// # Errors
///
/// Returns an error if the extended private key is invalid.
Future<Bip32Ed25519Signature> signData({required List<int> data});

/// Convert to a hex string.
String toHex();

/// Verify the signature on the given data using extended private key.
///
/// # Arguments
///
/// - `data`: The data to sign.
/// - `signature`: The signature to check.
///
/// # Returns
/// Returns a boolean value indicating if the signature match the sign data
/// True if the signature is valid and match the sign data, false otherwise.
///
/// # Errors
///
/// Returns an error if the extended private key or signature is invalid.
Future<bool> verifySignature(
{required List<int> data, required Bip32Ed25519Signature signature});

/// Get extended public key from the given extended private key.
///
/// # Returns
///
/// Returns a 64 length bytes `Bip32Ed25519XPublicKey` representing the extended
/// public key.
///
/// # Errors
///
/// Returns an error if the extended private key is invalid.
Future<Bip32Ed25519XPublicKey> xpublicKey();
}

// Rust type: RustOpaqueMoi<flutter_rust_bridge::for_generated::RustAutoOpaqueInner<Bip32Ed25519XPublicKey>>
abstract class Bip32Ed25519XPublicKey implements RustOpaqueInterface {
/// Extract the chain code from the extended public key.
/// The chain code is the last 32 bytes of the extended public key.
///
/// # Returns
///
/// Returns a 32 length bytes representing the chain code.
U8Array32 get chainCode;

/// Get the inner bytes.
U8Array64 get inner;

/// Extract the public key from the extended public key.
/// The public key is the first 32 bytes of the extended public key.
///
/// # Returns
///
/// Returns a 32 length bytes representing the public key.
U8Array32 get publicKey;

/// Create a new `Bip32Ed25519XPublicKey` from the given bytes.
factory Bip32Ed25519XPublicKey({required U8Array64 xpubBytes}) =>
RustLib.instance.api
.crateApiKeyDerivationBip32Ed25519XPublicKeyNew(xpubBytes: xpubBytes);

/// Convert to a hex string.
String toHex();

/// Verify the signature on the given data using extended public key.
///
/// # Arguments
///
/// - `data`: The data to sign.
/// - `signature`: The signature to check.
///
/// # Returns
/// Returns a boolean value indicating if the signature match the sign data
/// True if the signature is valid and match the sign data, false otherwise.
///
/// # Errors
///
/// Returns an error if the extended public key or signature is invalid.
Future<bool> verifySignature(
{required List<int> data, required Bip32Ed25519Signature signature});
}

class U8Array32 extends NonGrowableListView<int> {
static const arraySize = 32;

@internal
Uint8List get inner => _inner;
final Uint8List _inner;

U8Array32(this._inner)
: assert(_inner.length == arraySize),
super(_inner);

U8Array32.init() : this(Uint8List(arraySize));
}

class U8Array64 extends NonGrowableListView<int> {
static const arraySize = 64;

@internal
Uint8List get inner => _inner;
final Uint8List _inner;

U8Array64(this._inner)
: assert(_inner.length == arraySize),
super(_inner);

U8Array64.init() : this(Uint8List(arraySize));
}

class U8Array96 extends NonGrowableListView<int> {
static const arraySize = 96;

@internal
Uint8List get inner => _inner;
final Uint8List _inner;

U8Array96(this._inner)
: assert(_inner.length == arraySize),
super(_inner);

U8Array96.init() : this(Uint8List(arraySize));
}
Loading
Loading