Skip to content

Commit

Permalink
Merge branch 'main' into feat/create_keychain_dialog_structure
Browse files Browse the repository at this point in the history
  • Loading branch information
damian-molinski authored Sep 26, 2024
2 parents d3590ba + 67fa4b4 commit d743232
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import 'dart:typed_data';
import 'package:bip39/bip39.dart' as bip39;
import 'package:bip39/src/wordlists/english.dart';
import 'package:catalyst_cardano_serialization/catalyst_cardano_serialization.dart';
import 'package:convert/convert.dart';
import 'package:ed25519_hd_key/ed25519_hd_key.dart';

/// Represents a seed phrase consisting of a mnemonic and provides methods for
/// generating and deriving cryptographic data from the mnemonic.
Expand Down Expand Up @@ -65,6 +67,27 @@ class SeedPhrase {
/// The mnemonic phrase as a list of individual words.
List<String> get mnemonicWords => mnemonic.split(' ');

/// Derives an Ed25519 key pair from a seed.
///
/// Throws a [RangeError] If the provided [offset] is negative or exceeds
/// the length of the seed (64).
///
/// [offset]: The offset is applied
/// to the seed to adjust where key derivation starts. It defaults to 0.
Future<Ed25519KeyPair> deriveKeyPair([int offset = 0]) async {
final modifiedSeed = uint8ListSeed.sublist(offset);

final masterKey = await ED25519_HD_KEY.getMasterKeyFromSeed(modifiedSeed);
final privateKey = masterKey.key;

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

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

/// The full list of BIP-39 mnemonic words in English.
static List<String> get wordList => WORDLIST;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dependencies:
bip39: ^1.0.6
catalyst_cardano_serialization: ^0.4.0
convert: ^3.1.1
ed25519_hd_key: ^2.3.0
equatable: ^2.0.5
flutter_quill: ^10.5.13
meta: ^1.10.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,23 @@ void main() {
final expectedWords = mnemonic.split(' ');
expect(seedPhrase.mnemonicWords, expectedWords);
});

test('should generate key pair with different valid offsets', () async {
for (final offset in [0, 4, 28, 32, 64]) {
final keyPair = await SeedPhrase().deriveKeyPair(offset);

expect(keyPair, isNotNull);
}
});

test('should throw an error for key pair with out of range offset',
() async {
for (final offset in [-1, 65]) {
expect(
() async => SeedPhrase().deriveKeyPair(offset),
throwsA(isA<RangeError>()),
);
}
});
});
}
1 change: 1 addition & 0 deletions melos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ command:
bloc_concurrency: ^0.2.2
collection: ^1.18.0
cryptography: ^2.7.0
ed25519_hd_key: ^2.3.0
equatable: ^2.0.5
flutter_bloc: ^8.1.5
flutter_localized_locales: ^2.0.5
Expand Down

0 comments on commit d743232

Please sign in to comment.