Skip to content

Commit

Permalink
V3.3.0
Browse files Browse the repository at this point in the history
Fix substrate key generation from seed (ECDSA, EDDSA).
  • Loading branch information
mrtnetwork committed Jul 14, 2024
1 parent ff1842e commit 54b38b8
Show file tree
Hide file tree
Showing 52 changed files with 1,174 additions and 627 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.3.0

- Fix substrate key generation from seed (ECDSA, EDDSA).

## 3.2.0

- Add Pepecoin configiration to Bip-44 and Bip-49 coins.
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ packages:
path: ".."
relative: true
source: path
version: "3.0.0"
version: "3.3.0"
boolean_selector:
dependency: transitive
description:
Expand Down
4 changes: 4 additions & 0 deletions lib/bip/bip/bip32/bip32.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
/// cryptocurrencies to manage and derive keys.
library bip32;

export 'base/bip32_base.dart';
export 'base/ibip32_key_derivator.dart';
export 'base/ibip32_mst_key_generator.dart';

/// Export statements for Khalow-based BIP-32 components:
/// Export the Khalow BIP-32 implementation for Ed25519.
export 'khalow/bip32_kholaw_ed25519.dart';
Expand Down
32 changes: 28 additions & 4 deletions lib/bip/bip/bip39/bip39_seed_generator.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import 'package:blockchain_utils/bip/bip/bip39/bip39_mnemonic_validator.dart';
import 'package:blockchain_utils/crypto/quick_crypto.dart';
import 'package:blockchain_utils/bip/mnemonic/mnemonic.dart';
import 'package:blockchain_utils/utils/utils.dart';

import 'bip39_mnemonic_decoder.dart';

/// Constants related to Bip39 seed generation.
class Bip39SeedGeneratorConst {
/// The modification string used as part of the seed salt.
Expand All @@ -18,14 +19,19 @@ class Bip39SeedGeneratorConst {
/// optional passphrase into account. It validates the mnemonic before
/// generating the seed.
class Bip39SeedGenerator {
final List<int> _entropy;
final Mnemonic mnemonic;
Bip39SeedGenerator._(this.mnemonic, List<int> entropy)
: _entropy = BytesUtils.toBytes(entropy, unmodifiable: true);

/// Initializes a new instance of the Bip39SeedGenerator.
///
/// The [mnemonic] parameter represents the Bip39 mnemonic to be used for seed generation.
Bip39SeedGenerator(this.mnemonic) {
factory Bip39SeedGenerator(Mnemonic mnemonic) {
/// Validate the provided Bip39 mnemonic.
Bip39MnemonicValidator().validate(mnemonic.toStr());
final entropy = Bip39MnemonicDecoder().decode(mnemonic.toStr());
return Bip39SeedGenerator._(mnemonic, entropy);
}
final Mnemonic mnemonic;

/// Generates a seed from the Bip39 mnemonic.
///
Expand All @@ -44,4 +50,22 @@ class Bip39SeedGenerator {
iterations: Bip39SeedGeneratorConst.seedPbkdf2Rounds,
);
}

/// Generates a seed from the Bip39 mnemonic entropy.
///
/// Optionally, a [passphrase] can be provided to further secure the seed generation.
///
/// Example usage:
/// ```dart
/// final seedGenerator = Bip39SeedGenerator(mnemonic);
/// final seed = seedGenerator.generateFromEntropy("my_passphrase");
/// ```
List<int> generateFromEntropy([String passphrase = ""]) {
final salt = Bip39SeedGeneratorConst.seedSaltMod + passphrase;
return QuickCrypto.pbkdf2DeriveKey(
password: _entropy,
salt: StringUtils.encode(salt),
iterations: Bip39SeedGeneratorConst.seedPbkdf2Rounds,
);
}
}
18 changes: 9 additions & 9 deletions lib/bip/bip/bip44/base/bip44_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:blockchain_utils/bip/bip/bip32/slip10/bip32_slip10_ed25519_blake
import 'package:blockchain_utils/bip/bip/bip32/slip10/bip32_slip10_nist256p1.dart';
import 'package:blockchain_utils/bip/bip/bip32/slip10/bip32_slip10_secp256k1.dart';
import 'package:blockchain_utils/bip/bip/bip44/base/bip44_base_ex.dart';
import 'package:blockchain_utils/bip/bip/conf/bip_coin_conf.dart';
import 'package:blockchain_utils/bip/bip/conf/config/bip_coin_conf.dart';
import 'package:blockchain_utils/bip/cardano/bip32/cardano_icarus_bip32.dart';
import 'package:blockchain_utils/bip/ecc/curve/elliptic_curve_types.dart';
import 'package:blockchain_utils/exception/exception.dart';
Expand Down Expand Up @@ -86,10 +86,10 @@ class Bip44Levels {
/// Abstract base class for BIP-44 hierarchical deterministic wallets.
abstract class Bip44Base {
late final Bip32Base bip32;
late final CoinConfig coinConf;
late final BipCoinConfig coinConf;

/// Constructor for creating a [Bip44Base] object from a seed and coin.
Bip44Base.fromSeed(List<int> seedBytes, CoinConfig coin) {
Bip44Base.fromSeed(List<int> seedBytes, BipCoinConfig coin) {
Bip32Base bip;
switch (coin.type) {
case EllipticCurveTypes.secp256k1:
Expand Down Expand Up @@ -120,7 +120,7 @@ abstract class Bip44Base {
}

/// Constructor for creating a [Bip44Base] object from a extended key and coin.
Bip44Base.fromExtendedKey(String extendedKey, CoinConfig coin) {
Bip44Base.fromExtendedKey(String extendedKey, BipCoinConfig coin) {
Bip32Base bip;

switch (coin.type) {
Expand Down Expand Up @@ -154,7 +154,7 @@ abstract class Bip44Base {
}

/// Constructor for creating a [Bip44Base] object from a private key and coin.
Bip44Base.fromPrivateKey(List<int> privateKeyBytes, CoinConfig coin,
Bip44Base.fromPrivateKey(List<int> privateKeyBytes, BipCoinConfig coin,
{Bip32KeyData? keyData}) {
Bip32Base bip;
switch (coin.type) {
Expand Down Expand Up @@ -192,7 +192,7 @@ abstract class Bip44Base {
}

/// Constructor for creating a [Bip44Base] object from a public key and coin.
Bip44Base.fromPublicKey(List<int> pubkeyBytes, CoinConfig coin,
Bip44Base.fromPublicKey(List<int> pubkeyBytes, BipCoinConfig coin,
{Bip32KeyData? keyData}) {
Bip32Base bip;
switch (coin.type) {
Expand Down Expand Up @@ -230,8 +230,8 @@ abstract class Bip44Base {
}

/// Internal validation method for checking the depth of the BIP object.
static Tuple<Bip32Base, CoinConfig> _validate(
Bip32Base bip32Obj, CoinConfig coinConf) {
static Tuple<Bip32Base, BipCoinConfig> _validate(
Bip32Base bip32Obj, BipCoinConfig coinConf) {
int depth = bip32Obj.depth.depth;

if (bip32Obj.isPublicOnly) {
Expand All @@ -250,7 +250,7 @@ abstract class Bip44Base {
return Tuple(bip32Obj, coinConf);
}

/// Constructor for creating a [Bip44Base] object from a bip32 [Bip32Base] and coin [CoinConfig].
/// Constructor for creating a [Bip44Base] object from a bip32 [Bip32Base] and coin [BipCoinConfig].
Bip44Base(this.bip32, this.coinConf) {
_validate(bip32, coinConf);
}
Expand Down
14 changes: 7 additions & 7 deletions lib/bip/bip/bip44/base/bip44_keys.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:blockchain_utils/bip/address/encoders.dart';
import 'package:blockchain_utils/bip/address/p2pkh_addr.dart';
import 'package:blockchain_utils/bip/bip/bip32/bip32_key_data.dart';
import 'package:blockchain_utils/bip/bip/bip32/bip32_keys.dart';
import 'package:blockchain_utils/bip/bip/conf/bip_coin_conf.dart';
import 'package:blockchain_utils/bip/bip/conf/config/bip_coin_conf.dart';
import 'package:blockchain_utils/bip/wif/wif.dart';
import 'package:blockchain_utils/exception/exception.dart';

Expand All @@ -19,12 +19,12 @@ class Bip44PublicKey {
final Bip32PublicKey pubKey;

/// The coin configuration associated with this public key.
final CoinConfig coinConf;
final BipCoinConfig coinConf;

/// Factory constructor to create a [Bip44PublicKey] from a [Bip32PublicKey]
/// and a [CoinConfig]. It verifies that the elliptic curve type of the public
/// and a [BipCoinConfig]. It verifies that the elliptic curve type of the public
/// key matches the coin's configuration.
factory Bip44PublicKey(Bip32PublicKey pubKey, CoinConfig coinConf) {
factory Bip44PublicKey(Bip32PublicKey pubKey, BipCoinConfig coinConf) {
if (pubKey.curveType != coinConf.type) {
throw ArgumentException(
'The public key elliptic curve (${pubKey.curveType}) shall match '
Expand Down Expand Up @@ -96,12 +96,12 @@ class Bip44PrivateKey {
final Bip32PrivateKey privKey;

/// The coin configuration associated with this private key.
final CoinConfig coinConf;
final BipCoinConfig coinConf;

/// Factory constructor to create a [Bip44PrivateKey] from a [Bip32PrivateKey]
/// and a [CoinConfig]. It verifies that the elliptic curve type of the private
/// and a [BipCoinConfig]. It verifies that the elliptic curve type of the private
/// key matches the coin's configuration.
factory Bip44PrivateKey(Bip32PrivateKey privKey, CoinConfig coinConf) {
factory Bip44PrivateKey(Bip32PrivateKey privKey, BipCoinConfig coinConf) {
if (privKey.curveType != coinConf.type) {
throw ArgumentException(
'The private key elliptic curve (${privKey.curveType}) shall match the coin configuration one (${coinConf.type})',
Expand Down
2 changes: 2 additions & 0 deletions lib/bip/bip/bip44/bip44.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export 'base/bip44_base.dart';
/// Export statement for BIP-44 key-related functions and structures.
export 'base/bip44_keys.dart';

export 'base/bip44_base_ex.dart';

/// Export statement for the BIP-44 base implementation, which includes key
/// derivation and wallet management functions.
export 'bip44_base.dart';
5 changes: 3 additions & 2 deletions lib/bip/bip/bip44/bip44_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:blockchain_utils/bip/bip/bip32/base/bip32_base.dart';
import 'package:blockchain_utils/bip/bip/bip44/base/bip44_base.dart';
import 'package:blockchain_utils/bip/bip/bip44/base/bip44_base_ex.dart';
import 'package:blockchain_utils/bip/bip/conf/bip44/bip44_coins.dart';
import 'package:blockchain_utils/bip/bip/conf/bip_coin_conf.dart';
import 'package:blockchain_utils/bip/bip/conf/config/bip_coin_conf.dart';
import '../bip32/bip32_key_data.dart';

/// Constants related to BIP-44 (Bitcoin Improvement Proposal 44).
Expand All @@ -16,7 +16,8 @@ class Bip44Const {

class Bip44 extends Bip44Base {
// private constractor
Bip44._(Bip32Base bip32Obj, CoinConfig coinConf) : super(bip32Obj, coinConf);
Bip44._(Bip32Base bip32Obj, BipCoinConfig coinConf)
: super(bip32Obj, coinConf);

/// Constructor for creating a [Bip44] object from a seed and coin.
Bip44.fromSeed(List<int> seedBytes, Bip44Coins coinType)
Expand Down
5 changes: 3 additions & 2 deletions lib/bip/bip/bip49/bip49_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import 'package:blockchain_utils/bip/bip/bip32/bip32_key_data.dart';
import 'package:blockchain_utils/bip/bip/bip44/base/bip44_base.dart';
import 'package:blockchain_utils/bip/bip/bip44/base/bip44_base_ex.dart';
import 'package:blockchain_utils/bip/bip/conf/bip49/bip49_coins.dart';
import 'package:blockchain_utils/bip/bip/conf/bip_coin_conf.dart';
import 'package:blockchain_utils/bip/bip/conf/config/bip_coin_conf.dart';

/// Constants related to BIP-49 (Bitcoin Improvement Proposal 44).
class Bip49Const {
Expand All @@ -70,7 +70,8 @@ class Bip49Const {

class Bip49 extends Bip44Base {
/// private constractor
Bip49._(Bip32Base bip32Obj, CoinConfig coinConf) : super(bip32Obj, coinConf);
Bip49._(Bip32Base bip32Obj, BipCoinConfig coinConf)
: super(bip32Obj, coinConf);

/// Constructor for creating a [Bip49] object from a seed and coin.
Bip49.fromSeed(List<int> seedBytes, Bip49Coins coinType)
Expand Down
5 changes: 3 additions & 2 deletions lib/bip/bip/bip84/bip84_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:blockchain_utils/bip/bip/bip32/bip32_key_data.dart';
import 'package:blockchain_utils/bip/bip/bip44/base/bip44_base.dart';
import 'package:blockchain_utils/bip/bip/bip44/base/bip44_base_ex.dart';
import 'package:blockchain_utils/bip/bip/conf/bip84/bip84_coins.dart';
import 'package:blockchain_utils/bip/bip/conf/bip_coin_conf.dart';
import 'package:blockchain_utils/bip/bip/conf/config/bip_coin_conf.dart';

/// Constants related to BIP-84 (Bitcoin Improvement Proposal 84).
class Bip84Const {
Expand All @@ -16,7 +16,8 @@ class Bip84Const {

class Bip84 extends Bip44Base {
/// private constractor
Bip84._(Bip32Base bip32Obj, CoinConfig coinConf) : super(bip32Obj, coinConf);
Bip84._(Bip32Base bip32Obj, BipCoinConfig coinConf)
: super(bip32Obj, coinConf);

/// Constructor for creating a [Bip84] object from a seed and coin.
Bip84.fromSeed(List<int> seedBytes, Bip84Coins coinType)
Expand Down
5 changes: 3 additions & 2 deletions lib/bip/bip/bip86/bip86_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:blockchain_utils/bip/bip/bip32/bip32_key_data.dart';
import 'package:blockchain_utils/bip/bip/bip44/base/bip44_base.dart';
import 'package:blockchain_utils/bip/bip/bip44/base/bip44_base_ex.dart';
import 'package:blockchain_utils/bip/bip/conf/bip86/bip86_coins.dart';
import 'package:blockchain_utils/bip/bip/conf/bip_coin_conf.dart';
import 'package:blockchain_utils/bip/bip/conf/config/bip_coin_conf.dart';

/// Constants related to BIP-86 (Bitcoin Improvement Proposal 86).
class Bip86Const {
Expand All @@ -16,7 +16,8 @@ class Bip86Const {

class Bip86 extends Bip44Base {
/// private constructor
Bip86._(Bip32Base bip32Obj, CoinConfig coinConf) : super(bip32Obj, coinConf);
Bip86._(Bip32Base bip32Obj, BipCoinConfig coinConf)
: super(bip32Obj, coinConf);

/// Constructor for creating a [Bip86] object from a seed and coin.
Bip86.fromSeed(List<int> seedBytes, Bip86Coins coinType)
Expand Down
Loading

0 comments on commit 54b38b8

Please sign in to comment.