From 54b38b868aa9c801f381e7a0283173a6f78818b5 Mon Sep 17 00:00:00 2001 From: Mohsen <56779182+mrtnetwork@users.noreply.github.com> Date: Sun, 14 Jul 2024 19:55:42 +0330 Subject: [PATCH] V3.3.0 Fix substrate key generation from seed (ECDSA, EDDSA). --- CHANGELOG.md | 4 + example/pubspec.lock | 2 +- lib/bip/bip/bip32/bip32.dart | 4 + lib/bip/bip/bip39/bip39_seed_generator.dart | 32 +- lib/bip/bip/bip44/base/bip44_base.dart | 18 +- lib/bip/bip/bip44/base/bip44_keys.dart | 14 +- lib/bip/bip/bip44/bip44.dart | 2 + lib/bip/bip/bip44/bip44_base.dart | 5 +- lib/bip/bip/bip49/bip49_base.dart | 5 +- lib/bip/bip/bip84/bip84_base.dart | 5 +- lib/bip/bip/bip86/bip86_base.dart | 5 +- lib/bip/bip/conf/{ => bip}/bip_coins.dart | 76 ++--- lib/bip/bip/conf/bip44/bip44_coins.dart | 22 +- lib/bip/bip/conf/bip44/bip44_conf.dart | 206 +++++++----- lib/bip/bip/conf/bip49/bip49_coins.dart | 12 +- lib/bip/bip/conf/bip49/bip49_conf.dart | 35 +- lib/bip/bip/conf/bip84/bip84_coins.dart | 12 +- lib/bip/bip/conf/bip84/bip84_conf.dart | 14 +- lib/bip/bip/conf/bip86/bip86_coins.dart | 12 +- lib/bip/bip/conf/bip86/bip86_conf.dart | 10 +- lib/bip/bip/conf/bip_config.dart | 26 +- .../{ => config}/bip_bitcoin_cash_conf.dart | 5 +- .../bip/conf/{ => config}/bip_coin_conf.dart | 29 +- .../conf/{ => config}/bip_litecoin_conf.dart | 5 +- .../bip/conf/{ => const}/bip_conf_const.dart | 0 lib/bip/bip/conf/core/coin_conf.dart | 21 ++ lib/bip/bip/conf/core/coins.dart | 71 ++++ lib/bip/cardano/cip1852/cip1852.dart | 6 +- .../cardano/cip1852/conf/cip1852_coins.dart | 34 +- .../cardano/cip1852/conf/cip1852_conf.dart | 12 +- .../cardano/shelley/cardano_shelley_keys.dart | 6 +- lib/bip/ecc/curve/elliptic_curve_getter.dart | 4 +- lib/bip/ecc/keys/i_keys.dart | 8 + lib/bip/monero/conf/monero_coin_conf.dart | 44 ++- lib/bip/monero/conf/monero_coins.dart | 43 ++- lib/bip/monero/conf/monero_conf.dart | 23 +- lib/bip/monero/monero_base.dart | 5 +- .../substrate/conf/substrate_coin_conf.dart | 48 ++- lib/bip/substrate/conf/substrate_coins.dart | 246 ++++++++++++-- lib/bip/substrate/conf/substrate_conf.dart | 304 +++++++++++++----- .../substrate/{ => core}/substrate_base.dart | 82 ++--- .../{ => exception}/substrate_ex.dart | 0 lib/bip/substrate/keys/substrate_keys.dart | 79 +++++ .../substrate/{ => path}/substrate_path.dart | 18 +- lib/bip/substrate/substrate.dart | 6 +- lib/bip/substrate/substrate_keys.dart | 106 ------ lib/signer/substrate/core/signer.dart | 19 +- lib/signer/substrate/core/verifier.dart | 28 +- lib/signer/substrate/substrate.dart | 8 +- lib/utils/string/string.dart | 10 +- pubspec.yaml | 2 +- test/substrate/substrate_test.dart | 8 +- 52 files changed, 1174 insertions(+), 627 deletions(-) rename lib/bip/bip/conf/{ => bip}/bip_coins.dart (50%) rename lib/bip/bip/conf/{ => config}/bip_bitcoin_cash_conf.dart (93%) rename lib/bip/bip/conf/{ => config}/bip_coin_conf.dart (91%) rename lib/bip/bip/conf/{ => config}/bip_litecoin_conf.dart (93%) rename lib/bip/bip/conf/{ => const}/bip_conf_const.dart (100%) create mode 100644 lib/bip/bip/conf/core/coin_conf.dart create mode 100644 lib/bip/bip/conf/core/coins.dart rename lib/bip/substrate/{ => core}/substrate_base.dart (71%) rename lib/bip/substrate/{ => exception}/substrate_ex.dart (100%) create mode 100644 lib/bip/substrate/keys/substrate_keys.dart rename lib/bip/substrate/{ => path}/substrate_path.dart (93%) delete mode 100644 lib/bip/substrate/substrate_keys.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index aa43c83..c7d6ca3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/example/pubspec.lock b/example/pubspec.lock index a09f357..226ad0d 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -39,7 +39,7 @@ packages: path: ".." relative: true source: path - version: "3.0.0" + version: "3.3.0" boolean_selector: dependency: transitive description: diff --git a/lib/bip/bip/bip32/bip32.dart b/lib/bip/bip/bip32/bip32.dart index dbffea7..a734ec1 100644 --- a/lib/bip/bip/bip32/bip32.dart +++ b/lib/bip/bip/bip32/bip32.dart @@ -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'; diff --git a/lib/bip/bip/bip39/bip39_seed_generator.dart b/lib/bip/bip/bip39/bip39_seed_generator.dart index 25cc498..c47031c 100644 --- a/lib/bip/bip/bip39/bip39_seed_generator.dart +++ b/lib/bip/bip/bip39/bip39_seed_generator.dart @@ -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. @@ -18,14 +19,19 @@ class Bip39SeedGeneratorConst { /// optional passphrase into account. It validates the mnemonic before /// generating the seed. class Bip39SeedGenerator { + final List _entropy; + final Mnemonic mnemonic; + Bip39SeedGenerator._(this.mnemonic, List 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. /// @@ -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 generateFromEntropy([String passphrase = ""]) { + final salt = Bip39SeedGeneratorConst.seedSaltMod + passphrase; + return QuickCrypto.pbkdf2DeriveKey( + password: _entropy, + salt: StringUtils.encode(salt), + iterations: Bip39SeedGeneratorConst.seedPbkdf2Rounds, + ); + } } diff --git a/lib/bip/bip/bip44/base/bip44_base.dart b/lib/bip/bip/bip44/base/bip44_base.dart index 209cf09..8f00969 100644 --- a/lib/bip/bip/bip44/base/bip44_base.dart +++ b/lib/bip/bip/bip44/base/bip44_base.dart @@ -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'; @@ -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 seedBytes, CoinConfig coin) { + Bip44Base.fromSeed(List seedBytes, BipCoinConfig coin) { Bip32Base bip; switch (coin.type) { case EllipticCurveTypes.secp256k1: @@ -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) { @@ -154,7 +154,7 @@ abstract class Bip44Base { } /// Constructor for creating a [Bip44Base] object from a private key and coin. - Bip44Base.fromPrivateKey(List privateKeyBytes, CoinConfig coin, + Bip44Base.fromPrivateKey(List privateKeyBytes, BipCoinConfig coin, {Bip32KeyData? keyData}) { Bip32Base bip; switch (coin.type) { @@ -192,7 +192,7 @@ abstract class Bip44Base { } /// Constructor for creating a [Bip44Base] object from a public key and coin. - Bip44Base.fromPublicKey(List pubkeyBytes, CoinConfig coin, + Bip44Base.fromPublicKey(List pubkeyBytes, BipCoinConfig coin, {Bip32KeyData? keyData}) { Bip32Base bip; switch (coin.type) { @@ -230,8 +230,8 @@ abstract class Bip44Base { } /// Internal validation method for checking the depth of the BIP object. - static Tuple _validate( - Bip32Base bip32Obj, CoinConfig coinConf) { + static Tuple _validate( + Bip32Base bip32Obj, BipCoinConfig coinConf) { int depth = bip32Obj.depth.depth; if (bip32Obj.isPublicOnly) { @@ -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); } diff --git a/lib/bip/bip/bip44/base/bip44_keys.dart b/lib/bip/bip/bip44/base/bip44_keys.dart index a125843..cfc4363 100644 --- a/lib/bip/bip/bip44/base/bip44_keys.dart +++ b/lib/bip/bip/bip44/base/bip44_keys.dart @@ -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'; @@ -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 ' @@ -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})', diff --git a/lib/bip/bip/bip44/bip44.dart b/lib/bip/bip/bip44/bip44.dart index 16fda1c..50d902f 100644 --- a/lib/bip/bip/bip44/bip44.dart +++ b/lib/bip/bip/bip44/bip44.dart @@ -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'; diff --git a/lib/bip/bip/bip44/bip44_base.dart b/lib/bip/bip/bip44/bip44_base.dart index 50d12a9..a158b45 100644 --- a/lib/bip/bip/bip44/bip44_base.dart +++ b/lib/bip/bip/bip44/bip44_base.dart @@ -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). @@ -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 seedBytes, Bip44Coins coinType) diff --git a/lib/bip/bip/bip49/bip49_base.dart b/lib/bip/bip/bip49/bip49_base.dart index c72b8c8..d8bea2f 100644 --- a/lib/bip/bip/bip49/bip49_base.dart +++ b/lib/bip/bip/bip49/bip49_base.dart @@ -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 { @@ -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 seedBytes, Bip49Coins coinType) diff --git a/lib/bip/bip/bip84/bip84_base.dart b/lib/bip/bip/bip84/bip84_base.dart index 8ab6ec0..806587f 100644 --- a/lib/bip/bip/bip84/bip84_base.dart +++ b/lib/bip/bip/bip84/bip84_base.dart @@ -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 { @@ -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 seedBytes, Bip84Coins coinType) diff --git a/lib/bip/bip/bip86/bip86_base.dart b/lib/bip/bip/bip86/bip86_base.dart index fcf3d78..00f49e3 100644 --- a/lib/bip/bip/bip86/bip86_base.dart +++ b/lib/bip/bip/bip86/bip86_base.dart @@ -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 { @@ -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 seedBytes, Bip86Coins coinType) diff --git a/lib/bip/bip/conf/bip_coins.dart b/lib/bip/bip/conf/bip/bip_coins.dart similarity index 50% rename from lib/bip/bip/conf/bip_coins.dart rename to lib/bip/bip/conf/bip/bip_coins.dart index 5f542bd..0c5eafa 100644 --- a/lib/bip/bip/conf/bip_coins.dart +++ b/lib/bip/bip/conf/bip/bip_coins.dart @@ -1,32 +1,32 @@ -import 'package:blockchain_utils/bip/bip/bip32/bip32.dart'; +import 'package:blockchain_utils/bip/bip/bip32/bip32_key_data.dart'; import 'package:blockchain_utils/bip/bip/bip44/bip44_base.dart'; import 'package:blockchain_utils/bip/bip/bip49/bip49_base.dart'; import 'package:blockchain_utils/bip/bip/bip84/bip84_base.dart'; import 'package:blockchain_utils/bip/bip/bip86/bip86_base.dart'; +import 'package:blockchain_utils/bip/bip/conf/config/bip_coin_conf.dart'; +import 'package:blockchain_utils/bip/bip/conf/core/coins.dart'; import 'package:blockchain_utils/bip/bip/conf/bip44/bip44_coins.dart'; import 'package:blockchain_utils/bip/bip/conf/bip49/bip49_coins.dart'; import 'package:blockchain_utils/bip/bip/conf/bip84/bip84_coins.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/cardano/cip1852/cip1852.dart'; -import 'package:blockchain_utils/bip/cardano/cip1852/conf/cip1852_coins.dart'; -import 'package:blockchain_utils/exception/exception.dart'; -/// An abstract class representing a collection of cryptocurrency coins. -/// -/// This abstract class defines a contract for classes that provide a collection -/// of cryptocurrency coins. Subclasses should implement the 'value' getter to -/// return an instance of themselves or a specific type that represents the -/// collection of coins. -abstract class CryptoCoins { +abstract class BipCoins implements CryptoCoins { + const BipCoins(); + /// Gets the collection of cryptocurrency coins. + @override CryptoCoins get value; + @override String get coinName; - CoinConfig get conf; + @override + BipCoinConfig get conf; + + @override + bool get isBipCoin => true; - static CryptoCoins? getCoin(String name, CryptoProposal proposal) { + static CryptoCoins? fromName(String name, BipProposal proposal) { switch (proposal) { case BipProposal.bip44: return Bip44Coins.fromName(name); @@ -36,14 +36,13 @@ abstract class CryptoCoins { return Bip84Coins.fromName(name); case BipProposal.bip86: return Bip86Coins.fromName(name); - case CipProposal.cip1852: - return Cip1852Coins.fromName(name); default: return null; } } - CryptoProposal get proposal; + @override + BipProposal get proposal; @override String toString() { @@ -51,44 +50,12 @@ abstract class CryptoCoins { } } -abstract class CryptoProposal { - String get specName; - CryptoProposal get value; - Bip32KeyIndex get purpose; - - static CryptoProposal fromName(String name) { - try { - return BipProposal.values.firstWhere((element) => element.name == name); - } on StateError { - return CipProposal.values.firstWhere( - (element) => element.name == name, - orElse: () => throw MessageException( - "Unable to locate a proposal with the given name.", - details: {"Name": name}), - ); - } - } -} - -class CipProposal implements CryptoProposal { - static const CipProposal cip1852 = CipProposal._('cip1852'); - - const CipProposal._(this.name); - final String name; - - @override - String get specName => name; - @override - CipProposal get value => this; - - static const List values = [cip1852]; - - @override - Bip32KeyIndex get purpose => Cip1852Const.purpose; -} +// abstract class BipProposals implements CoinProposal { +// abstract final Bip32KeyIndex purpose; +// } /// Enum representing different BIP proposals. -class BipProposal implements CryptoProposal { +class BipProposal implements CoinProposal { static const BipProposal bip44 = BipProposal._('bip44'); static const BipProposal bip49 = BipProposal._('bip49'); static const BipProposal bip84 = BipProposal._('bip84'); @@ -105,8 +72,7 @@ class BipProposal implements CryptoProposal { @override BipProposal get value => this; - /// Extension method to get the corresponding [Bip32KeyIndex.purpose] for each [BipProposal]. - @override + /// Extension method to get the corresponding [purpose] key index for each [BipProposal]. Bip32KeyIndex get purpose { switch (this) { case BipProposal.bip44: diff --git a/lib/bip/bip/conf/bip44/bip44_coins.dart b/lib/bip/bip/conf/bip44/bip44_coins.dart index 6bff0f2..ce3638f 100644 --- a/lib/bip/bip/conf/bip44/bip44_coins.dart +++ b/lib/bip/bip/conf/bip44/bip44_coins.dart @@ -52,13 +52,13 @@ OF THE POSSIBILITY OF SUCH DAMAGE. */ +import 'package:blockchain_utils/bip/bip/conf/bip/bip_coins.dart'; import 'package:blockchain_utils/bip/bip/conf/bip44/bip44_conf.dart'; -import 'package:blockchain_utils/bip/bip/conf/bip_coin_conf.dart'; -import 'package:blockchain_utils/bip/bip/conf/bip_coins.dart'; +import 'package:blockchain_utils/bip/bip/conf/config/bip_coin_conf.dart'; /// An enumeration of supported cryptocurrencies for BIP44. It includes both main /// networks and test networks of various cryptocurrencies. -class Bip44Coins implements CryptoCoins { +class Bip44Coins extends BipCoins { // Named constants representing each coin const Bip44Coins._(this.name); @@ -204,6 +204,10 @@ class Bip44Coins implements CryptoCoins { /// Kusama Ed25519 Slip static const kusamaEd25519Slip = Bip44Coins._('kusamaEd25519Slip'); + /// Kusama testnet Ed25519 Slip + static const kusamaTestnetEd25519Slip = + Bip44Coins._('kusamaTestnetEd25519Slip'); + /// Litecoin static const litecoin = Bip44Coins._('litecoin'); @@ -246,6 +250,10 @@ class Bip44Coins implements CryptoCoins { /// Polkadot Ed25519 Slip static const polkadotEd25519Slip = Bip44Coins._('polkadotEd25519Slip'); + /// Polkadot testnet Ed25519 Slip + static const polkadotTestnetEd25519Slip = + Bip44Coins._('polkadotTestnetEd25519Slip'); + /// Polygon static const polygon = Bip44Coins._('polygon'); @@ -357,7 +365,7 @@ class Bip44Coins implements CryptoCoins { } @override - CoinConfig get conf => _coinToConf[this]!; + BipCoinConfig get conf => _coinToConf[this]!; static Bip44Coins? fromName(String name) { try { @@ -370,8 +378,8 @@ class Bip44Coins implements CryptoCoins { static List get values => _coinToConf.keys.toList(); /// A mapping that associates each BIP44Coin (enum) with its corresponding - /// CoinConfig configuration. - static final Map _coinToConf = { + /// BipCoinConfig configuration. + static final Map _coinToConf = { Bip44Coins.akashNetwork: Bip44Conf.akashNetwork, Bip44Coins.algorand: Bip44Conf.algorand, Bip44Coins.aptos: Bip44Conf.aptos, @@ -427,6 +435,7 @@ class Bip44Coins implements CryptoCoins { Bip44Coins.irisNet: Bip44Conf.irisNet, Bip44Coins.kava: Bip44Conf.kava, Bip44Coins.kusamaEd25519Slip: Bip44Conf.kusamaEd25519Slip, + Bip44Coins.kusamaTestnetEd25519Slip: Bip44Conf.kusamaTestnetEd25519Slip, Bip44Coins.litecoin: Bip44Conf.litecoinMainNet, Bip44Coins.litecoinTestnet: Bip44Conf.litecoinTestNet, Bip44Coins.moneroEd25519Slip: Bip44Conf.moneroEd25519Slip, @@ -442,6 +451,7 @@ class Bip44Coins implements CryptoCoins { Bip44Coins.osmosis: Bip44Conf.osmosis, Bip44Coins.piNetwork: Bip44Conf.piNetwork, Bip44Coins.polkadotEd25519Slip: Bip44Conf.polkadotEd25519Slip, + Bip44Coins.polkadotTestnetEd25519Slip: Bip44Conf.polkadotTestnetEd25519Slip, Bip44Coins.polygon: Bip44Conf.polygon, Bip44Coins.ripple: Bip44Conf.ripple, Bip44Coins.rippleTestnet: Bip44Conf.rippleTestnet, diff --git a/lib/bip/bip/conf/bip44/bip44_conf.dart b/lib/bip/bip/conf/bip44/bip44_conf.dart index ef5c6bb..40907d7 100644 --- a/lib/bip/bip/conf/bip44/bip44_conf.dart +++ b/lib/bip/bip/conf/bip44/bip44_conf.dart @@ -1,16 +1,16 @@ import 'package:blockchain_utils/bip/address/encoders.dart'; import 'package:blockchain_utils/bip/bip/bip32/bip32_const.dart'; import 'package:blockchain_utils/bip/bip/bip32/bip32_key_net_ver.dart'; -import 'package:blockchain_utils/bip/bip/conf/bip_bitcoin_cash_conf.dart'; -import 'package:blockchain_utils/bip/bip/conf/bip_coin_conf.dart'; -import 'package:blockchain_utils/bip/bip/conf/bip_conf_const.dart'; -import 'package:blockchain_utils/bip/bip/conf/bip_litecoin_conf.dart'; +import 'package:blockchain_utils/bip/bip/conf/config/bip_bitcoin_cash_conf.dart'; +import 'package:blockchain_utils/bip/bip/conf/config/bip_coin_conf.dart'; +import 'package:blockchain_utils/bip/bip/conf/const/bip_conf_const.dart'; +import 'package:blockchain_utils/bip/bip/conf/config/bip_litecoin_conf.dart'; import 'package:blockchain_utils/bip/coin_conf/coins_conf.dart'; import 'package:blockchain_utils/bip/ecc/curve/elliptic_curve_types.dart'; import 'package:blockchain_utils/bip/slip/slip44/slip44.dart'; /// A configuration class for BIP44 coins that defines the key network versions and -/// maps each supported BIP44Coin to its corresponding CoinConfig. +/// maps each supported BIP44Coin to its corresponding BipCoinConfig. class Bip44Conf { /// The key network version for the mainnet of Bitcoin. static final Bip32KeyNetVersions bip44BtcKeyNetVerMain = @@ -21,7 +21,7 @@ class Bip44Conf { Bip32Const.testNetKeyNetVersions; /// Configuration for Akash Network - static final CoinConfig akashNetwork = CoinConfig( + static final BipCoinConfig akashNetwork = BipCoinConfig( coinNames: CoinsConf.akashNetwork.coinName, coinIdx: Slip44.atom, isTestnet: false, @@ -36,7 +36,7 @@ class Bip44Conf { ); /// Configuration for Algorand - static final CoinConfig algorand = CoinConfig( + static final BipCoinConfig algorand = BipCoinConfig( coinNames: CoinsConf.algorand.coinName, addressEncoder: ([dynamic kwargs]) => AlgoAddrEncoder(), coinIdx: Slip44.algorand, @@ -49,7 +49,7 @@ class Bip44Conf { ); /// Configuration for Aptos - static final CoinConfig aptos = CoinConfig( + static final BipCoinConfig aptos = BipCoinConfig( coinNames: CoinsConf.aptos.coinName, coinIdx: Slip44.aptos, isTestnet: false, @@ -62,7 +62,7 @@ class Bip44Conf { ); /// Configuration for Avax C-Chain - static final CoinConfig avaxCChain = CoinConfig( + static final BipCoinConfig avaxCChain = BipCoinConfig( coinNames: CoinsConf.avaxCChain.coinName, coinIdx: Slip44.ethereum, isTestnet: false, @@ -75,7 +75,7 @@ class Bip44Conf { ); /// Configuration for Avax P-Chain - static final CoinConfig avaxPChain = CoinConfig( + static final BipCoinConfig avaxPChain = BipCoinConfig( coinNames: CoinsConf.avaxPChain.coinName, coinIdx: Slip44.avalanche, isTestnet: false, @@ -88,7 +88,7 @@ class Bip44Conf { ); /// Configuration for Avax X-Chain - static final CoinConfig avaxXChain = CoinConfig( + static final BipCoinConfig avaxXChain = BipCoinConfig( coinNames: CoinsConf.avaxXChain.coinName, coinIdx: Slip44.avalanche, isTestnet: false, @@ -101,7 +101,7 @@ class Bip44Conf { ); /// Configuration for Axelar - static final CoinConfig axelar = CoinConfig( + static final BipCoinConfig axelar = BipCoinConfig( coinNames: CoinsConf.axelar.coinName, coinIdx: Slip44.atom, isTestnet: false, @@ -116,7 +116,7 @@ class Bip44Conf { ); /// Configuration for Band Protocol - static final CoinConfig bandProtocol = CoinConfig( + static final BipCoinConfig bandProtocol = BipCoinConfig( coinNames: CoinsConf.bandProtocol.coinName, coinIdx: Slip44.bandProtocol, isTestnet: false, @@ -131,7 +131,7 @@ class Bip44Conf { ); /// Configuration for Binance Chain - static final CoinConfig binanceChain = CoinConfig( + static final BipCoinConfig binanceChain = BipCoinConfig( coinNames: CoinsConf.binanceChain.coinName, coinIdx: Slip44.binanceChain, isTestnet: false, @@ -146,7 +146,7 @@ class Bip44Conf { ); /// Configuration for Binance Smart Chain - static final CoinConfig binanceSmartChain = CoinConfig( + static final BipCoinConfig binanceSmartChain = BipCoinConfig( coinNames: CoinsConf.binanceSmartChain.coinName, coinIdx: Slip44.ethereum, isTestnet: false, @@ -159,7 +159,7 @@ class Bip44Conf { ); /// Configuration for Bitcoin main net - static final CoinConfig bitcoinMainNet = CoinConfig( + static final BipCoinConfig bitcoinMainNet = BipCoinConfig( coinNames: CoinsConf.bitcoinMainNet.coinName, coinIdx: Slip44.bitcoin, isTestnet: false, @@ -174,7 +174,7 @@ class Bip44Conf { ); /// Configuration for Bitcoin test net - static final CoinConfig bitcoinTestNet = CoinConfig( + static final BipCoinConfig bitcoinTestNet = BipCoinConfig( coinNames: CoinsConf.bitcoinTestNet.coinName, coinIdx: Slip44.testnet, isTestnet: true, @@ -293,7 +293,7 @@ class Bip44Conf { ); /// Configuration for BitcoinSV main net - static final CoinConfig bitcoinSvMainNet = CoinConfig( + static final BipCoinConfig bitcoinSvMainNet = BipCoinConfig( coinNames: CoinsConf.bitcoinSvMainNet.coinName, coinIdx: Slip44.bitcoinSv, isTestnet: false, @@ -310,7 +310,7 @@ class Bip44Conf { ); /// Configuration for BitcoinSV test net - static final CoinConfig bitcoinSvTestNet = CoinConfig( + static final BipCoinConfig bitcoinSvTestNet = BipCoinConfig( coinNames: CoinsConf.bitcoinSvTestNet.coinName, coinIdx: Slip44.testnet, isTestnet: true, @@ -324,7 +324,7 @@ class Bip44Conf { }, ); - static final CoinConfig cardanoByronIcarus = CoinConfig( + static final BipCoinConfig cardanoByronIcarus = BipCoinConfig( coinNames: CoinsConf.cardanoMainNet.coinName, coinIdx: Slip44.cardano, isTestnet: false, @@ -337,7 +337,7 @@ class Bip44Conf { ); /// Configuration for Cardano Byron (Ledger) - static final CoinConfig cardanoByronLedger = CoinConfig( + static final BipCoinConfig cardanoByronLedger = BipCoinConfig( coinNames: CoinsConf.cardanoMainNet.coinName, coinIdx: Slip44.cardano, isTestnet: false, @@ -348,7 +348,7 @@ class Bip44Conf { addressEncoder: ([dynamic kwargs]) => AdaByronIcarusAddrEncoder(), addrParams: {"chain_code": true}, ); - static final CoinConfig cardanoByronIcarusTestnet = CoinConfig( + static final BipCoinConfig cardanoByronIcarusTestnet = BipCoinConfig( coinNames: CoinsConf.cardanoMainNet.coinName, coinIdx: Slip44.testnet, isTestnet: true, @@ -361,7 +361,7 @@ class Bip44Conf { ); /// Configuration for Cardano Byron (Ledger) - static final CoinConfig cardanoByronLedgerTestnet = CoinConfig( + static final BipCoinConfig cardanoByronLedgerTestnet = BipCoinConfig( coinNames: CoinsConf.cardanoMainNet.coinName, coinIdx: Slip44.testnet, isTestnet: true, @@ -374,7 +374,7 @@ class Bip44Conf { ); /// Configuration for Celo - static final CoinConfig celo = CoinConfig( + static final BipCoinConfig celo = BipCoinConfig( coinNames: CoinsConf.celo.coinName, coinIdx: Slip44.celo, isTestnet: false, @@ -387,7 +387,7 @@ class Bip44Conf { ); /// Configuration for Certik - static final CoinConfig certik = CoinConfig( + static final BipCoinConfig certik = BipCoinConfig( coinNames: CoinsConf.certik.coinName, coinIdx: Slip44.atom, isTestnet: false, @@ -402,7 +402,7 @@ class Bip44Conf { ); /// Configuration for Chihuahua - static final CoinConfig chihuahua = CoinConfig( + static final BipCoinConfig chihuahua = BipCoinConfig( coinNames: CoinsConf.chihuahua.coinName, coinIdx: Slip44.atom, isTestnet: false, @@ -417,7 +417,7 @@ class Bip44Conf { ); /// Configuration for Cosmos - static final CoinConfig cosmos = CoinConfig( + static final BipCoinConfig cosmos = BipCoinConfig( coinNames: CoinsConf.cosmos.coinName, coinIdx: Slip44.atom, isTestnet: false, @@ -430,7 +430,7 @@ class Bip44Conf { "hrp": CoinsConf.cosmos.params.addrHrp!, }, ); - static final CoinConfig cosmosTestnet = CoinConfig( + static final BipCoinConfig cosmosTestnet = BipCoinConfig( coinNames: CoinsConf.cosmos.coinName, coinIdx: Slip44.testnet, isTestnet: false, @@ -445,7 +445,7 @@ class Bip44Conf { ); /// Configuration for Cosmos - static final CoinConfig cosmosNist256p1 = CoinConfig( + static final BipCoinConfig cosmosNist256p1 = BipCoinConfig( coinNames: CoinsConf.cosmos.coinName, coinIdx: Slip44.atom, isTestnet: false, @@ -458,7 +458,7 @@ class Bip44Conf { "hrp": CoinsConf.cosmos.params.addrHrp!, }, ); - static final CoinConfig cosmosTestnetNist256p1 = CoinConfig( + static final BipCoinConfig cosmosTestnetNist256p1 = BipCoinConfig( coinNames: CoinsConf.cosmos.coinName, coinIdx: Slip44.testnet, isTestnet: false, @@ -473,7 +473,7 @@ class Bip44Conf { ); /// Configuration for Dash main net - static final CoinConfig dashMainNet = CoinConfig( + static final BipCoinConfig dashMainNet = BipCoinConfig( coinNames: CoinsConf.dashMainNet.coinName, coinIdx: Slip44.dash, isTestnet: false, @@ -488,7 +488,7 @@ class Bip44Conf { ); /// Configuration for Dash test net - static final CoinConfig dashTestNet = CoinConfig( + static final BipCoinConfig dashTestNet = BipCoinConfig( coinNames: CoinsConf.dashTestNet.coinName, coinIdx: Slip44.testnet, isTestnet: true, @@ -503,7 +503,7 @@ class Bip44Conf { ); /// Configuration for Dogecoin main net - static final CoinConfig dogecoinMainNet = CoinConfig( + static final BipCoinConfig dogecoinMainNet = BipCoinConfig( coinNames: CoinsConf.dogecoinMainNet.coinName, coinIdx: Slip44.dogecoin, isTestnet: false, @@ -519,7 +519,7 @@ class Bip44Conf { ); /// Configuration for Dogecoin test net - static final CoinConfig dogecoinTestNet = CoinConfig( + static final BipCoinConfig dogecoinTestNet = BipCoinConfig( coinNames: CoinsConf.dogecoinTestNet.coinName, coinIdx: Slip44.testnet, isTestnet: true, @@ -535,7 +535,7 @@ class Bip44Conf { ); /// Configuration for Pepecoin main net - static final CoinConfig pepeMainnet = CoinConfig( + static final BipCoinConfig pepeMainnet = BipCoinConfig( coinNames: CoinsConf.pepeMainnet.coinName, coinIdx: Slip44.pepecoin, isTestnet: false, @@ -549,7 +549,7 @@ class Bip44Conf { ); /// Configuration for Pepecoin test net - static final CoinConfig pepeTestnet = CoinConfig( + static final BipCoinConfig pepeTestnet = BipCoinConfig( coinNames: CoinsConf.pepeTestnet.coinName, coinIdx: Slip44.testnet, isTestnet: true, @@ -617,7 +617,7 @@ class Bip44Conf { ); /// Configuration for Elrond - static final CoinConfig elrond = CoinConfig( + static final BipCoinConfig elrond = BipCoinConfig( coinNames: CoinsConf.elrond.coinName, coinIdx: Slip44.elrond, isTestnet: false, @@ -630,7 +630,7 @@ class Bip44Conf { ); /// Configuration for Eos - static final CoinConfig eos = CoinConfig( + static final BipCoinConfig eos = BipCoinConfig( coinNames: CoinsConf.eos.coinName, coinIdx: Slip44.eos, isTestnet: false, @@ -643,7 +643,7 @@ class Bip44Conf { ); /// Configuration for Ergo main net - static final CoinConfig ergoMainNet = CoinConfig( + static final BipCoinConfig ergoMainNet = BipCoinConfig( coinNames: CoinsConf.ergoMainNet.coinName, coinIdx: Slip44.ergo, isTestnet: false, @@ -658,7 +658,7 @@ class Bip44Conf { ); /// Configuration for Ergo test net - static final CoinConfig ergoTestNet = CoinConfig( + static final BipCoinConfig ergoTestNet = BipCoinConfig( coinNames: CoinsConf.ergoTestNet.coinName, coinIdx: Slip44.ergo, isTestnet: true, @@ -673,7 +673,7 @@ class Bip44Conf { ); /// Configuration for Ethereum - static final CoinConfig ethereum = CoinConfig( + static final BipCoinConfig ethereum = BipCoinConfig( coinNames: CoinsConf.ethereum.coinName, coinIdx: Slip44.ethereum, isTestnet: false, @@ -686,7 +686,7 @@ class Bip44Conf { ); /// Configuration for EthereumTestnet - static final CoinConfig ethereumTestnet = CoinConfig( + static final BipCoinConfig ethereumTestnet = BipCoinConfig( coinNames: CoinsConf.ethereum.coinName, coinIdx: Slip44.testnet, isTestnet: true, @@ -699,7 +699,7 @@ class Bip44Conf { ); /// Configuration for Ethereum Classic - static final CoinConfig ethereumClassic = CoinConfig( + static final BipCoinConfig ethereumClassic = BipCoinConfig( coinNames: CoinsConf.ethereumClassic.coinName, coinIdx: Slip44.ethereumClassic, isTestnet: false, @@ -712,7 +712,7 @@ class Bip44Conf { ); /// Configuration for Fantom Opera - static final CoinConfig fantomOpera = CoinConfig( + static final BipCoinConfig fantomOpera = BipCoinConfig( coinNames: CoinsConf.fantomOpera.coinName, coinIdx: Slip44.ethereum, isTestnet: false, @@ -725,7 +725,7 @@ class Bip44Conf { ); /// Configuration for Filecoin - static final CoinConfig filecoin = CoinConfig( + static final BipCoinConfig filecoin = BipCoinConfig( coinNames: CoinsConf.filecoin.coinName, coinIdx: Slip44.filecoin, isTestnet: false, @@ -738,7 +738,7 @@ class Bip44Conf { ); /// Configuration for Harmony One (Metamask address) - static final CoinConfig harmonyOneMetamask = CoinConfig( + static final BipCoinConfig harmonyOneMetamask = BipCoinConfig( coinNames: CoinsConf.harmonyOne.coinName, coinIdx: Slip44.ethereum, isTestnet: false, @@ -751,7 +751,7 @@ class Bip44Conf { ); /// Configuration for Harmony One (Ethereum address) - static final CoinConfig harmonyOneEth = CoinConfig( + static final BipCoinConfig harmonyOneEth = BipCoinConfig( coinNames: CoinsConf.harmonyOne.coinName, coinIdx: Slip44.harmonyOne, isTestnet: false, @@ -764,7 +764,7 @@ class Bip44Conf { ); /// Configuration for Harmony One (Atom address) - static final CoinConfig harmonyOneAtom = CoinConfig( + static final BipCoinConfig harmonyOneAtom = BipCoinConfig( coinNames: CoinsConf.harmonyOne.coinName, coinIdx: Slip44.harmonyOne, isTestnet: false, @@ -777,7 +777,7 @@ class Bip44Conf { ); /// Configuration for Huobi Chain - static final CoinConfig huobiChain = CoinConfig( + static final BipCoinConfig huobiChain = BipCoinConfig( coinNames: CoinsConf.huobiChain.coinName, coinIdx: Slip44.ethereum, isTestnet: false, @@ -790,7 +790,7 @@ class Bip44Conf { ); /// Configuration for Icon - static final CoinConfig icon = CoinConfig( + static final BipCoinConfig icon = BipCoinConfig( coinNames: CoinsConf.icon.coinName, coinIdx: Slip44.icon, isTestnet: false, @@ -803,7 +803,7 @@ class Bip44Conf { ); /// Configuration for Injective - static final CoinConfig injective = CoinConfig( + static final BipCoinConfig injective = BipCoinConfig( coinNames: CoinsConf.injective.coinName, coinIdx: Slip44.ethereum, isTestnet: false, @@ -816,7 +816,7 @@ class Bip44Conf { ); /// Configuration for IRISnet - static final CoinConfig irisNet = CoinConfig( + static final BipCoinConfig irisNet = BipCoinConfig( coinNames: CoinsConf.irisNet.coinName, coinIdx: Slip44.atom, isTestnet: false, @@ -831,7 +831,7 @@ class Bip44Conf { ); /// Configuration for Kava - static final CoinConfig kava = CoinConfig( + static final BipCoinConfig kava = BipCoinConfig( coinNames: CoinsConf.kava.coinName, coinIdx: Slip44.kava, isTestnet: false, @@ -846,7 +846,7 @@ class Bip44Conf { ); /// Configuration for Kusama (ed25519 SLIP-0010) - static final CoinConfig kusamaEd25519Slip = CoinConfig( + static final BipCoinConfig kusamaEd25519Slip = BipCoinConfig( coinNames: CoinsConf.kusama.coinName, coinIdx: Slip44.kusama, isTestnet: false, @@ -860,6 +860,21 @@ class Bip44Conf { }, ); + /// Configuration for KusamaTestnet (ed25519 SLIP-0010) + static final BipCoinConfig kusamaTestnetEd25519Slip = BipCoinConfig( + coinNames: CoinsConf.kusama.coinName, + coinIdx: Slip44.testnet, + isTestnet: false, + defPath: derPathHardenedFull, + keyNetVer: bip44BtcKeyNetVerMain, + wifNetVer: null, + type: EllipticCurveTypes.ed25519, + addressEncoder: ([dynamic kwargs]) => SubstrateEd25519AddrEncoder(), + addrParams: { + "ss58_format": CoinsConf.kusama.params.addrSs58Format, + }, + ); + /// Configuration for Litecoin main net static final BipLitecoinConf litecoinMainNet = BipLitecoinConf( coinNames: CoinsConf.litecoinMainNet.coinName, @@ -904,7 +919,7 @@ class Bip44Conf { ); /// Configuration for Monero (ed25519 SLIP-0010) - static final CoinConfig moneroEd25519Slip = CoinConfig( + static final BipCoinConfig moneroEd25519Slip = BipCoinConfig( coinNames: CoinsConf.moneroMainNet.coinName, coinIdx: Slip44.monero, isTestnet: false, @@ -917,7 +932,7 @@ class Bip44Conf { ); /// Configuration for Monero (secp256k1) - static final CoinConfig moneroSecp256k1 = CoinConfig( + static final BipCoinConfig moneroSecp256k1 = BipCoinConfig( coinNames: CoinsConf.moneroMainNet.coinName, coinIdx: Slip44.monero, isTestnet: false, @@ -930,7 +945,7 @@ class Bip44Conf { ); /// Configuration for Nano - static final CoinConfig nano = CoinConfig( + static final BipCoinConfig nano = BipCoinConfig( coinNames: CoinsConf.nano.coinName, coinIdx: Slip44.nano, isTestnet: false, @@ -943,7 +958,7 @@ class Bip44Conf { ); /// Configuration for Near Protocol - static final CoinConfig nearProtocol = CoinConfig( + static final BipCoinConfig nearProtocol = BipCoinConfig( coinNames: CoinsConf.nearProtocol.coinName, coinIdx: Slip44.nearProtocol, isTestnet: false, @@ -956,7 +971,7 @@ class Bip44Conf { ); /// Configuration for Neo - static final CoinConfig neo = CoinConfig( + static final BipCoinConfig neo = BipCoinConfig( coinNames: CoinsConf.neo.coinName, coinIdx: Slip44.neo, isTestnet: false, @@ -971,7 +986,7 @@ class Bip44Conf { ); /// Configuration for Nine Chronicles Gold - static final CoinConfig nineChroniclesGold = CoinConfig( + static final BipCoinConfig nineChroniclesGold = BipCoinConfig( coinNames: CoinsConf.nineChroniclesGold.coinName, coinIdx: Slip44.nineChronicles, isTestnet: false, @@ -984,7 +999,7 @@ class Bip44Conf { ); /// Configuration for OKEx Chain (Ethereum address) - static final CoinConfig okexChainEth = CoinConfig( + static final BipCoinConfig okexChainEth = BipCoinConfig( coinNames: CoinsConf.okexChain.coinName, coinIdx: Slip44.ethereum, isTestnet: false, @@ -997,7 +1012,7 @@ class Bip44Conf { ); /// Configuration for OKEx Chain (Atom address) - static final CoinConfig okexChainAtom = CoinConfig( + static final BipCoinConfig okexChainAtom = BipCoinConfig( coinNames: CoinsConf.okexChain.coinName, coinIdx: Slip44.ethereum, isTestnet: false, @@ -1010,7 +1025,7 @@ class Bip44Conf { ); /// Configuration for OKEx Chain (old Atom address) - static final CoinConfig okexChainAtomOld = CoinConfig( + static final BipCoinConfig okexChainAtomOld = BipCoinConfig( coinNames: CoinsConf.okexChain.coinName, coinIdx: Slip44.okexChain, isTestnet: false, @@ -1023,7 +1038,7 @@ class Bip44Conf { ); /// Configuration for Ontology - static final CoinConfig ontology = CoinConfig( + static final BipCoinConfig ontology = BipCoinConfig( coinNames: CoinsConf.ontology.coinName, coinIdx: Slip44.ontology, isTestnet: false, @@ -1038,7 +1053,7 @@ class Bip44Conf { ); /// Configuration for Osmosis - static final CoinConfig osmosis = CoinConfig( + static final BipCoinConfig osmosis = BipCoinConfig( coinNames: CoinsConf.osmosis.coinName, coinIdx: Slip44.atom, isTestnet: false, @@ -1053,7 +1068,7 @@ class Bip44Conf { ); /// Configuration for Pi Network - static final CoinConfig piNetwork = CoinConfig( + static final BipCoinConfig piNetwork = BipCoinConfig( coinNames: CoinsConf.piNetwork.coinName, coinIdx: Slip44.piNetwork, isTestnet: false, @@ -1066,7 +1081,7 @@ class Bip44Conf { ); /// Configuration for Polkadot (ed25519 SLIP-0010) - static final CoinConfig polkadotEd25519Slip = CoinConfig( + static final BipCoinConfig polkadotEd25519Slip = BipCoinConfig( coinNames: CoinsConf.polkadot.coinName, coinIdx: Slip44.polkadot, isTestnet: false, @@ -1079,9 +1094,22 @@ class Bip44Conf { "ss58_format": CoinsConf.polkadot.params.addrSs58Format!, }, ); + static final BipCoinConfig polkadotTestnetEd25519Slip = BipCoinConfig( + coinNames: CoinsConf.polkadot.coinName, + coinIdx: Slip44.testnet, + isTestnet: true, + defPath: derPathHardenedFull, + keyNetVer: bip44BtcKeyNetVerMain, + wifNetVer: null, + type: EllipticCurveTypes.ed25519, + addressEncoder: ([dynamic kwargs]) => SubstrateEd25519AddrEncoder(), + addrParams: { + "ss58_format": CoinsConf.genericSubstrate.params.addrSs58Format! + }, + ); /// Configuration for Polygon - static final CoinConfig polygon = CoinConfig( + static final BipCoinConfig polygon = BipCoinConfig( coinNames: CoinsConf.polygon.coinName, coinIdx: Slip44.ethereum, isTestnet: false, @@ -1094,7 +1122,7 @@ class Bip44Conf { ); /// Configuration for Ripple - static final CoinConfig ripple = CoinConfig( + static final BipCoinConfig ripple = BipCoinConfig( coinNames: CoinsConf.ripple.coinName, coinIdx: Slip44.ripple, isTestnet: false, @@ -1107,7 +1135,7 @@ class Bip44Conf { ); /// Configuration for Ripple testnet - static final CoinConfig rippleTestnet = CoinConfig( + static final BipCoinConfig rippleTestnet = BipCoinConfig( coinNames: CoinsConf.ripple.coinName, coinIdx: Slip44.testnet, isTestnet: true, @@ -1120,7 +1148,7 @@ class Bip44Conf { ); /// Configuration for Ripple - static final CoinConfig rippleEd25519 = CoinConfig( + static final BipCoinConfig rippleEd25519 = BipCoinConfig( coinNames: CoinsConf.ripple.coinName, coinIdx: Slip44.ripple, isTestnet: false, @@ -1136,7 +1164,7 @@ class Bip44Conf { ); /// Configuration for Ripple testnet - static final CoinConfig rippleTestnetEd25519 = CoinConfig( + static final BipCoinConfig rippleTestnetEd25519 = BipCoinConfig( coinNames: CoinsConf.ripple.coinName, coinIdx: Slip44.testnet, isTestnet: true, @@ -1150,7 +1178,7 @@ class Bip44Conf { "curve_type": EllipticCurveTypes.ed25519 }, ); - static final CoinConfig secretNetworkOld = CoinConfig( + static final BipCoinConfig secretNetworkOld = BipCoinConfig( coinNames: CoinsConf.secretNetwork.coinName, coinIdx: Slip44.atom, isTestnet: false, @@ -1165,7 +1193,7 @@ class Bip44Conf { ); /// Configuration for Secret Network (new path) - static final CoinConfig secretNetworkNew = CoinConfig( + static final BipCoinConfig secretNetworkNew = BipCoinConfig( coinNames: CoinsConf.secretNetwork.coinName, coinIdx: Slip44.secretNetwork, isTestnet: false, @@ -1180,7 +1208,7 @@ class Bip44Conf { ); /// Configuration for Solana - static final CoinConfig solana = CoinConfig( + static final BipCoinConfig solana = BipCoinConfig( coinNames: CoinsConf.solana.coinName, coinIdx: Slip44.solana, isTestnet: false, @@ -1193,7 +1221,7 @@ class Bip44Conf { ); /// Configuration for Solana - static final CoinConfig solanaTestnet = CoinConfig( + static final BipCoinConfig solanaTestnet = BipCoinConfig( coinNames: CoinsConf.solana.coinName, coinIdx: Slip44.testnet, isTestnet: true, @@ -1206,7 +1234,7 @@ class Bip44Conf { ); /// Configuration for Stellar - static final CoinConfig stellar = CoinConfig( + static final BipCoinConfig stellar = BipCoinConfig( coinNames: CoinsConf.stellar.coinName, coinIdx: Slip44.stellar, isTestnet: false, @@ -1219,7 +1247,7 @@ class Bip44Conf { ); /// Configuration for Terra - static final CoinConfig terra = CoinConfig( + static final BipCoinConfig terra = BipCoinConfig( coinNames: CoinsConf.terra.coinName, coinIdx: Slip44.terra, isTestnet: false, @@ -1234,7 +1262,7 @@ class Bip44Conf { ); /// Configuration for Tezos - static final CoinConfig tezos = CoinConfig( + static final BipCoinConfig tezos = BipCoinConfig( coinNames: CoinsConf.tezos.coinName, coinIdx: Slip44.tezos, isTestnet: false, @@ -1247,7 +1275,7 @@ class Bip44Conf { ); /// Configuration for Theta - static final CoinConfig theta = CoinConfig( + static final BipCoinConfig theta = BipCoinConfig( coinNames: CoinsConf.theta.coinName, coinIdx: Slip44.theta, isTestnet: false, @@ -1260,7 +1288,7 @@ class Bip44Conf { ); /// Configuration for Tron - static final CoinConfig tron = CoinConfig( + static final BipCoinConfig tron = BipCoinConfig( coinNames: CoinsConf.tron.coinName, coinIdx: Slip44.tron, isTestnet: false, @@ -1273,7 +1301,7 @@ class Bip44Conf { ); /// Configuration for Tron testnet - static final CoinConfig tronTestnet = CoinConfig( + static final BipCoinConfig tronTestnet = BipCoinConfig( coinNames: CoinsConf.tron.coinName, coinIdx: Slip44.testnet, isTestnet: true, @@ -1286,7 +1314,7 @@ class Bip44Conf { ); /// Configuration for VeChain - static final CoinConfig vechain = CoinConfig( + static final BipCoinConfig vechain = BipCoinConfig( coinNames: CoinsConf.veChain.coinName, coinIdx: Slip44.vechain, isTestnet: false, @@ -1299,7 +1327,7 @@ class Bip44Conf { ); /// Configuration for Verge - static final CoinConfig verge = CoinConfig( + static final BipCoinConfig verge = BipCoinConfig( coinNames: CoinsConf.verge.coinName, coinIdx: Slip44.verge, isTestnet: false, @@ -1314,7 +1342,7 @@ class Bip44Conf { ); /// Configuration for Zcash main net - static final CoinConfig zcashMainNet = CoinConfig( + static final BipCoinConfig zcashMainNet = BipCoinConfig( coinNames: CoinsConf.zcashMainNet.coinName, coinIdx: Slip44.zcash, isTestnet: false, @@ -1329,7 +1357,7 @@ class Bip44Conf { ); /// Configuration for Zcash test net - static final CoinConfig zcashTestNet = CoinConfig( + static final BipCoinConfig zcashTestNet = BipCoinConfig( coinNames: CoinsConf.zcashTestNet.coinName, coinIdx: Slip44.testnet, isTestnet: true, @@ -1344,7 +1372,7 @@ class Bip44Conf { ); /// Configuration for Zilliqa - static final CoinConfig zilliqa = CoinConfig( + static final BipCoinConfig zilliqa = BipCoinConfig( coinNames: CoinsConf.zilliqa.coinName, coinIdx: Slip44.zilliqa, isTestnet: false, @@ -1356,7 +1384,7 @@ class Bip44Conf { addrParams: {}, ); - static final CoinConfig tonMainnet = CoinConfig( + static final BipCoinConfig tonMainnet = BipCoinConfig( coinNames: CoinsConf.tonMainnet.coinName, coinIdx: Slip44.ton, isTestnet: false, @@ -1367,7 +1395,7 @@ class Bip44Conf { addressEncoder: ([dynamic kwargs]) => TonAddrEncoder(), addrParams: {"workchain": CoinsConf.tonMainnet.params.workchain}, ); - static final CoinConfig tonTestnet = CoinConfig( + static final BipCoinConfig tonTestnet = BipCoinConfig( coinNames: CoinsConf.tonTestnet.coinName, coinIdx: Slip44.testnet, isTestnet: true, diff --git a/lib/bip/bip/conf/bip49/bip49_coins.dart b/lib/bip/bip/conf/bip49/bip49_coins.dart index 5688b22..01b1222 100644 --- a/lib/bip/bip/conf/bip49/bip49_coins.dart +++ b/lib/bip/bip/conf/bip49/bip49_coins.dart @@ -1,10 +1,10 @@ +import 'package:blockchain_utils/bip/bip/conf/bip/bip_coins.dart'; import 'package:blockchain_utils/bip/bip/conf/bip49/bip49_conf.dart'; -import 'package:blockchain_utils/bip/bip/conf/bip_coin_conf.dart'; -import 'package:blockchain_utils/bip/bip/conf/bip_coins.dart'; +import 'package:blockchain_utils/bip/bip/conf/config/bip_coin_conf.dart'; /// An enumeration of supported cryptocurrencies for BIP49. It includes both main /// networks and test networks of various cryptocurrencies. -class Bip49Coins implements CryptoCoins { +class Bip49Coins extends BipCoins { static const Bip49Coins bitcoin = Bip49Coins._('bitcoin'); static const Bip49Coins bitcoinCash = Bip49Coins._('bitcoinCash'); static const Bip49Coins bitcoinCashSlp = Bip49Coins._('bitcoinCashSlp'); @@ -41,7 +41,7 @@ class Bip49Coins implements CryptoCoins { String get coinName => name; @override - CoinConfig get conf => _coinToConf[this]!; + BipCoinConfig get conf => _coinToConf[this]!; static Bip49Coins? fromName(String name) { try { @@ -54,8 +54,8 @@ class Bip49Coins implements CryptoCoins { static List get values => _coinToConf.keys.toList(); /// A mapping that associates each BIP49Coin (enum) with its corresponding - /// CoinConfig configuration. - static final Map _coinToConf = { + /// BipCoinConfig configuration. + static final Map _coinToConf = { Bip49Coins.bitcoin: Bip49Conf.bitcoinMainNet, Bip49Coins.bitcoinTestnet: Bip49Conf.bitcoinTestNet, Bip49Coins.bitcoinCash: Bip49Conf.bitcoinCashMainNet, diff --git a/lib/bip/bip/conf/bip49/bip49_conf.dart b/lib/bip/bip/conf/bip49/bip49_conf.dart index 7776099..173e092 100644 --- a/lib/bip/bip/conf/bip49/bip49_conf.dart +++ b/lib/bip/bip/conf/bip49/bip49_conf.dart @@ -1,16 +1,15 @@ import 'package:blockchain_utils/bip/address/p2sh_addr.dart'; import 'package:blockchain_utils/bip/bip/bip32/bip32_key_net_ver.dart'; -import 'package:blockchain_utils/bip/bip/conf/bip_bitcoin_cash_conf.dart'; -import 'package:blockchain_utils/bip/bip/conf/bip_coin_conf.dart'; -import 'package:blockchain_utils/bip/bip/conf/bip_litecoin_conf.dart'; +import 'package:blockchain_utils/bip/bip/conf/const/bip_conf_const.dart'; +import 'package:blockchain_utils/bip/bip/conf/config/bip_bitcoin_cash_conf.dart'; +import 'package:blockchain_utils/bip/bip/conf/config/bip_coin_conf.dart'; +import 'package:blockchain_utils/bip/bip/conf/config/bip_litecoin_conf.dart'; import 'package:blockchain_utils/bip/coin_conf/coins_conf.dart'; import 'package:blockchain_utils/bip/ecc/curve/elliptic_curve_types.dart'; import 'package:blockchain_utils/bip/slip/slip44/slip44.dart'; -import '../bip_conf_const.dart'; - /// A configuration class for BIP49 that defines the key network versions and -/// maps each supported BIP49Coin to its corresponding CoinConfig. +/// maps each supported BIP49Coin to its corresponding BipCoinConfig. class Bip49Conf { /// The key network version for the mainnet of Bitcoin. static final bip49BtcKeyNetVerMain = Bip32KeyNetVersions( @@ -25,7 +24,7 @@ class Bip49Conf { ); /// Configuration for Dash main net - static final CoinConfig dashMainNet = CoinConfig( + static final BipCoinConfig dashMainNet = BipCoinConfig( coinNames: CoinsConf.dashMainNet.coinName, coinIdx: Slip44.dash, isTestnet: false, @@ -39,7 +38,7 @@ class Bip49Conf { }); /// Configuration for Dash test net - static final CoinConfig dashTestNet = CoinConfig( + static final BipCoinConfig dashTestNet = BipCoinConfig( coinNames: CoinsConf.dashTestNet.coinName, coinIdx: Slip44.testnet, isTestnet: true, @@ -53,7 +52,7 @@ class Bip49Conf { }); /// Configuration for Dogecoin main net - static final CoinConfig dogecoinMainNet = CoinConfig( + static final BipCoinConfig dogecoinMainNet = BipCoinConfig( coinNames: CoinsConf.dogecoinMainNet.coinName, coinIdx: Slip44.dogecoin, isTestnet: false, @@ -70,7 +69,7 @@ class Bip49Conf { }); /// Configuration for Dogecoin test net - static final CoinConfig dogecoinTestNet = CoinConfig( + static final BipCoinConfig dogecoinTestNet = BipCoinConfig( coinNames: CoinsConf.dogecoinTestNet.coinName, coinIdx: Slip44.testnet, isTestnet: true, @@ -134,7 +133,7 @@ class Bip49Conf { }); /// Configuration for Zcash main net - static final CoinConfig zcashMainNet = CoinConfig( + static final BipCoinConfig zcashMainNet = BipCoinConfig( coinNames: CoinsConf.zcashMainNet.coinName, coinIdx: Slip44.zcash, isTestnet: false, @@ -148,7 +147,7 @@ class Bip49Conf { }); /// Configuration for Zcash test net - static final CoinConfig zcashTestNet = CoinConfig( + static final BipCoinConfig zcashTestNet = BipCoinConfig( coinNames: CoinsConf.zcashTestNet.coinName, coinIdx: Slip44.testnet, isTestnet: true, @@ -164,7 +163,7 @@ class Bip49Conf { }); /// Configuration for Bitcoin main net - static final CoinConfig bitcoinMainNet = CoinConfig( + static final BipCoinConfig bitcoinMainNet = BipCoinConfig( coinNames: CoinsConf.bitcoinMainNet.coinName, coinIdx: Slip44.bitcoin, isTestnet: false, @@ -178,7 +177,7 @@ class Bip49Conf { }); /// Configuration for Bitcoin test net - static final CoinConfig bitcoinTestNet = CoinConfig( + static final BipCoinConfig bitcoinTestNet = BipCoinConfig( coinNames: CoinsConf.bitcoinTestNet.coinName, coinIdx: Slip44.testnet, isTestnet: true, @@ -192,7 +191,7 @@ class Bip49Conf { }); /// Configuration for BitcoinSV main net - static final CoinConfig bitcoinSvMainNet = CoinConfig( + static final BipCoinConfig bitcoinSvMainNet = BipCoinConfig( coinNames: CoinsConf.bitcoinSvMainNet.coinName, coinIdx: Slip44.bitcoinSv, isTestnet: false, @@ -206,7 +205,7 @@ class Bip49Conf { }); /// Configuration for BitcoinSV test net - static final CoinConfig bitcoinSvTestNet = CoinConfig( + static final BipCoinConfig bitcoinSvTestNet = BipCoinConfig( coinNames: CoinsConf.bitcoinSvTestNet.coinName, coinIdx: Slip44.testnet, isTestnet: true, @@ -379,7 +378,7 @@ class Bip49Conf { ); /// Configuration for pepecoin main net - static final CoinConfig pepeMainnet = CoinConfig( + static final BipCoinConfig pepeMainnet = BipCoinConfig( coinNames: CoinsConf.pepeMainnet.coinName, coinIdx: Slip44.pepecoin, isTestnet: false, @@ -396,7 +395,7 @@ class Bip49Conf { }); /// Configuration for pepecoin test net - static final CoinConfig pepeTestnet = CoinConfig( + static final BipCoinConfig pepeTestnet = BipCoinConfig( coinNames: CoinsConf.pepeTestnet.coinName, coinIdx: Slip44.testnet, isTestnet: true, diff --git a/lib/bip/bip/conf/bip84/bip84_coins.dart b/lib/bip/bip/conf/bip84/bip84_coins.dart index 7286fde..8425fa4 100644 --- a/lib/bip/bip/conf/bip84/bip84_coins.dart +++ b/lib/bip/bip/conf/bip84/bip84_coins.dart @@ -52,13 +52,13 @@ OF THE POSSIBILITY OF SUCH DAMAGE. */ +import 'package:blockchain_utils/bip/bip/conf/bip/bip_coins.dart'; import 'package:blockchain_utils/bip/bip/conf/bip84/bip84_conf.dart'; -import 'package:blockchain_utils/bip/bip/conf/bip_coin_conf.dart'; -import 'package:blockchain_utils/bip/bip/conf/bip_coins.dart'; +import 'package:blockchain_utils/bip/bip/conf/config/bip_coin_conf.dart'; /// An enumeration of supported cryptocurrencies for BIP84. It includes both main /// networks and test networks of various cryptocurrencies. -class Bip84Coins implements CryptoCoins { +class Bip84Coins extends BipCoins { // Main nets static const Bip84Coins bitcoin = Bip84Coins._('bitcoin'); static const Bip84Coins litecoin = Bip84Coins._('litecoin'); @@ -83,7 +83,7 @@ class Bip84Coins implements CryptoCoins { String get coinName => name; @override - CoinConfig get conf => _coinToConf[this]!; + BipCoinConfig get conf => _coinToConf[this]!; static Bip84Coins? fromName(String name) { try { @@ -94,8 +94,8 @@ class Bip84Coins implements CryptoCoins { } /// A mapping that associates each BIP84Coin (enum) with its corresponding - /// CoinConfig configuration. - static final Map _coinToConf = { + /// BipCoinConfig configuration. + static final Map _coinToConf = { Bip84Coins.bitcoin: Bip84Conf.bitcoinMainNet, Bip84Coins.bitcoinTestnet: Bip84Conf.bitcoinTestNet, Bip84Coins.litecoin: Bip84Conf.litecoinMainNet, diff --git a/lib/bip/bip/conf/bip84/bip84_conf.dart b/lib/bip/bip/conf/bip84/bip84_conf.dart index e1cbce2..e78755c 100644 --- a/lib/bip/bip/conf/bip84/bip84_conf.dart +++ b/lib/bip/bip/conf/bip84/bip84_conf.dart @@ -1,13 +1,13 @@ import 'package:blockchain_utils/bip/address/p2wpkh_addr.dart'; import 'package:blockchain_utils/bip/bip/bip32/bip32_key_net_ver.dart'; -import 'package:blockchain_utils/bip/bip/conf/bip_coin_conf.dart'; -import 'package:blockchain_utils/bip/bip/conf/bip_conf_const.dart'; +import 'package:blockchain_utils/bip/bip/conf/config/bip_coin_conf.dart'; +import 'package:blockchain_utils/bip/bip/conf/const/bip_conf_const.dart'; import 'package:blockchain_utils/bip/coin_conf/coins_conf.dart'; import 'package:blockchain_utils/bip/ecc/curve/elliptic_curve_types.dart'; import 'package:blockchain_utils/bip/slip/slip44/slip44.dart'; /// A configuration class for BIP84 that defines the key network versions and -/// maps each supported BIP84Coin to its corresponding CoinConfig. +/// maps each supported BIP84Coin to its corresponding BipCoinConfig. class Bip84Conf { /// The key network version for Bitcoin. static final Bip32KeyNetVersions bip84BtcKeyNetVer = Bip32KeyNetVersions( @@ -16,7 +16,7 @@ class Bip84Conf { ); /// Configuration for Bitcoin main net - static final CoinConfig bitcoinMainNet = CoinConfig( + static final BipCoinConfig bitcoinMainNet = BipCoinConfig( coinNames: CoinsConf.bitcoinMainNet.coinName, coinIdx: Slip44.bitcoin, isTestnet: false, @@ -29,7 +29,7 @@ class Bip84Conf { ); /// Configuration for Bitcoin test net - static final CoinConfig bitcoinTestNet = CoinConfig( + static final BipCoinConfig bitcoinTestNet = BipCoinConfig( coinNames: CoinsConf.bitcoinTestNet.coinName, coinIdx: Slip44.testnet, isTestnet: true, @@ -45,7 +45,7 @@ class Bip84Conf { ); /// Configuration for Litecoin main net - static final CoinConfig litecoinMainNet = CoinConfig( + static final BipCoinConfig litecoinMainNet = BipCoinConfig( coinNames: CoinsConf.litecoinMainNet.coinName, coinIdx: Slip44.litecoin, isTestnet: false, @@ -58,7 +58,7 @@ class Bip84Conf { ); /// Configuration for Litecoin test net - static final CoinConfig litecoinTestNet = CoinConfig( + static final BipCoinConfig litecoinTestNet = BipCoinConfig( coinNames: CoinsConf.litecoinTestNet.coinName, coinIdx: Slip44.testnet, isTestnet: true, diff --git a/lib/bip/bip/conf/bip86/bip86_coins.dart b/lib/bip/bip/conf/bip86/bip86_coins.dart index cec1cdc..624a73a 100644 --- a/lib/bip/bip/conf/bip86/bip86_coins.dart +++ b/lib/bip/bip/conf/bip86/bip86_coins.dart @@ -1,10 +1,10 @@ +import 'package:blockchain_utils/bip/bip/conf/bip/bip_coins.dart'; import 'package:blockchain_utils/bip/bip/conf/bip86/bip86_conf.dart'; -import 'package:blockchain_utils/bip/bip/conf/bip_coin_conf.dart'; -import 'package:blockchain_utils/bip/bip/conf/bip_coins.dart'; +import 'package:blockchain_utils/bip/bip/conf/config/bip_coin_conf.dart'; /// An enumeration of supported cryptocurrencies for BIP86. It includes both main /// networks and test networks of various cryptocurrencies. -class Bip86Coins implements CryptoCoins { +class Bip86Coins extends BipCoins { /// mainnets static const Bip86Coins bitcoin = Bip86Coins._('bitcoin'); @@ -28,7 +28,7 @@ class Bip86Coins implements CryptoCoins { } @override - CoinConfig get conf => _coinToConf[this]!; + BipCoinConfig get conf => _coinToConf[this]!; static Bip86Coins? fromName(String name) { try { @@ -39,8 +39,8 @@ class Bip86Coins implements CryptoCoins { } /// A mapping that associates each BIP86Coin (enum) with its corresponding - /// CoinConfig configuration. - static final Map _coinToConf = { + /// BipCoinConfig configuration. + static final Map _coinToConf = { Bip86Coins.bitcoin: Bip86Conf.bitcoinMainNet, Bip86Coins.bitcoinTestnet: Bip86Conf.bitcoinTestNet, }; diff --git a/lib/bip/bip/conf/bip86/bip86_conf.dart b/lib/bip/bip/conf/bip86/bip86_conf.dart index 5ff92fc..7fa07f4 100644 --- a/lib/bip/bip/conf/bip86/bip86_conf.dart +++ b/lib/bip/bip/conf/bip86/bip86_conf.dart @@ -54,14 +54,14 @@ import 'package:blockchain_utils/bip/address/p2tr_addr.dart'; import 'package:blockchain_utils/bip/bip/bip32/bip32_const.dart'; -import 'package:blockchain_utils/bip/bip/conf/bip_coin_conf.dart'; -import 'package:blockchain_utils/bip/bip/conf/bip_conf_const.dart'; +import 'package:blockchain_utils/bip/bip/conf/config/bip_coin_conf.dart'; +import 'package:blockchain_utils/bip/bip/conf/const/bip_conf_const.dart'; import 'package:blockchain_utils/bip/coin_conf/coins_conf.dart'; import 'package:blockchain_utils/bip/ecc/curve/elliptic_curve_types.dart'; import 'package:blockchain_utils/bip/slip/slip44/slip44.dart'; /// A configuration class for BIP86 that defines the key network versions and -/// maps each supported BIP86Coin to its corresponding CoinConfig. +/// maps each supported BIP86Coin to its corresponding BipCoinConfig. class Bip86Conf { /// The key network version for the mainnet of Bitcoin. static final bip86BtcKeyNetVer = Bip32Const.mainNetKeyNetVersions; @@ -70,7 +70,7 @@ class Bip86Conf { static final bip86BtcKeyNetVerTest = Bip32Const.testNetKeyNetVersions; /// Configuration for Bitcoin main net - static CoinConfig bitcoinMainNet = CoinConfig( + static BipCoinConfig bitcoinMainNet = BipCoinConfig( coinNames: CoinsConf.bitcoinMainNet.coinName, coinIdx: Slip44.bitcoin, isTestnet: false, @@ -85,7 +85,7 @@ class Bip86Conf { ); /// Configuration for Bitcoin test net - static CoinConfig bitcoinTestNet = CoinConfig( + static BipCoinConfig bitcoinTestNet = BipCoinConfig( coinNames: CoinsConf.bitcoinTestNet.coinName, coinIdx: Slip44.testnet, isTestnet: true, diff --git a/lib/bip/bip/conf/bip_config.dart b/lib/bip/bip/conf/bip_config.dart index 9996ba8..c02caaa 100644 --- a/lib/bip/bip/conf/bip_config.dart +++ b/lib/bip/bip/conf/bip_config.dart @@ -5,22 +5,32 @@ /// and key derivation for a variety of blockchain networks. library bip_config; -/// Export statement for BIP-44 coin definitions and parameters, including -/// coin names and configuration information for hierarchical deterministic wallets. +export 'bip/bip_coins.dart'; + +/// bip44 export 'bip44/bip44_coins.dart'; export 'bip44/bip44_conf.dart'; -/// Export statement for BIP-49 coin definitions and parameters, including -/// coin names and configuration information for hierarchical deterministic wallets. +/// bip49 export 'bip49/bip49_coins.dart'; export 'bip49/bip49_conf.dart'; -/// Export statement for BIP-84 coin definitions and parameters, including -/// coin names and configuration information for hierarchical deterministic wallets. +/// bip84 export 'bip84/bip84_coins.dart'; export 'bip84/bip84_conf.dart'; -/// Export statement for BIP-86 coin definitions and parameters, including -/// coin names and configuration information for hierarchical deterministic wallets. +/// bip86 export 'bip86/bip86_coins.dart'; export 'bip86/bip86_conf.dart'; + +/// coin configs +export 'config/bip_bitcoin_cash_conf.dart'; +export 'config/bip_coin_conf.dart'; +export 'config/bip_litecoin_conf.dart'; + +/// coin constants +export 'const/bip_conf_const.dart'; + +/// crypto coin base class +export 'core/coins.dart'; +export 'core/coin_conf.dart'; diff --git a/lib/bip/bip/conf/bip_bitcoin_cash_conf.dart b/lib/bip/bip/conf/config/bip_bitcoin_cash_conf.dart similarity index 93% rename from lib/bip/bip/conf/bip_bitcoin_cash_conf.dart rename to lib/bip/bip/conf/config/bip_bitcoin_cash_conf.dart index 3be3e01..3906a02 100644 --- a/lib/bip/bip/conf/bip_bitcoin_cash_conf.dart +++ b/lib/bip/bip/conf/config/bip_bitcoin_cash_conf.dart @@ -1,11 +1,12 @@ import 'package:blockchain_utils/bip/address/encoder.dart'; import 'package:blockchain_utils/bip/bip/bip32/bip32_key_net_ver.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/bip/conf/core/coin_conf.dart'; import 'package:blockchain_utils/bip/ecc/curve/elliptic_curve_types.dart'; import 'package:blockchain_utils/bip/coin_conf/coins_name.dart'; /// A class representing the configuration for Bitcoin Cash (BCH) based on the BIP framework. -class BipBitcoinCashConf extends CoinConfig { +class BipBitcoinCashConf extends BipCoinConfig { /// Flag to indicate whether legacy address format should be used. final bool useLagacyAdder; diff --git a/lib/bip/bip/conf/bip_coin_conf.dart b/lib/bip/bip/conf/config/bip_coin_conf.dart similarity index 91% rename from lib/bip/bip/conf/bip_coin_conf.dart rename to lib/bip/bip/conf/config/bip_coin_conf.dart index 27ec07c..224bb4f 100644 --- a/lib/bip/bip/conf/bip_coin_conf.dart +++ b/lib/bip/bip/conf/config/bip_coin_conf.dart @@ -55,32 +55,37 @@ import 'package:blockchain_utils/bip/address/encoder.dart'; import 'package:blockchain_utils/bip/bip/bip32/bip32_key_net_ver.dart'; import 'package:blockchain_utils/bip/bip/bip32/bip32_keys.dart'; +import 'package:blockchain_utils/bip/bip/conf/core/coin_conf.dart'; import 'package:blockchain_utils/bip/ecc/curve/elliptic_curve_types.dart'; import 'package:blockchain_utils/bip/coin_conf/coins_name.dart'; -/// A typedef for the address encoder function that takes optional dynamic parameters. -typedef AddrEncoder = BlockchainAddressEncoder Function([dynamic kwargs]); - /// A base class representing configuration parameters for a cryptocurrency coin. -class CoinConfig { +class BipCoinConfig implements CoinConfig { /// Returns the address encoder for this coin configuration. BlockchainAddressEncoder encoder() { return addressEncoder(); } /// Configuration properties. + @override final CoinNames coinNames; final int coinIdx; + @override final bool isTestnet; final String defPath; + @override final Bip32KeyNetVersions keyNetVer; + @override final List? wifNetVer; + @override final AddrEncoder addressEncoder; + @override final Map addrParams; + @override final EllipticCurveTypes type; - /// Creates a copy of the CoinConfig object with optional properties updated. - CoinConfig copy({ + /// Creates a copy of the BipCoinConfig object with optional properties updated. + BipCoinConfig copy({ CoinNames? coinNames, int? coinIdx, bool? isTestnet, @@ -91,7 +96,7 @@ class CoinConfig { EllipticCurveTypes? type, AddrEncoder? addressEncoder, }) { - return CoinConfig( + return BipCoinConfig( coinNames: coinNames ?? this.coinNames, coinIdx: coinIdx ?? this.coinIdx, isTestnet: isTestnet ?? this.isTestnet, @@ -103,8 +108,8 @@ class CoinConfig { addressEncoder: addressEncoder ?? this.addressEncoder); } - /// Constructor for CoinConfig. - const CoinConfig({ + /// Constructor for BipCoinConfig. + const BipCoinConfig({ required this.coinNames, required this.coinIdx, required this.isTestnet, @@ -127,4 +132,10 @@ class CoinConfig { } return addrParams; } + + @override + bool get hasExtendedKeys => true; + + @override + bool get hasWif => wifNetVer != null; } diff --git a/lib/bip/bip/conf/bip_litecoin_conf.dart b/lib/bip/bip/conf/config/bip_litecoin_conf.dart similarity index 93% rename from lib/bip/bip/conf/bip_litecoin_conf.dart rename to lib/bip/bip/conf/config/bip_litecoin_conf.dart index 3ad4134..acaa738 100644 --- a/lib/bip/bip/conf/bip_litecoin_conf.dart +++ b/lib/bip/bip/conf/config/bip_litecoin_conf.dart @@ -1,10 +1,11 @@ import 'package:blockchain_utils/bip/bip/bip32/bip32_key_net_ver.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/bip/conf/core/coin_conf.dart'; import 'package:blockchain_utils/bip/ecc/curve/elliptic_curve_types.dart'; import 'package:blockchain_utils/bip/coin_conf/coins_name.dart'; /// A class representing the configuration for Litecoin (LTC) based on the BIP framework. -class BipLitecoinConf extends CoinConfig { +class BipLitecoinConf extends BipCoinConfig { /// Configuration properties specific to Litecoin. final Bip32KeyNetVersions altKeyNetVer; final bool useDeprAddress; diff --git a/lib/bip/bip/conf/bip_conf_const.dart b/lib/bip/bip/conf/const/bip_conf_const.dart similarity index 100% rename from lib/bip/bip/conf/bip_conf_const.dart rename to lib/bip/bip/conf/const/bip_conf_const.dart diff --git a/lib/bip/bip/conf/core/coin_conf.dart b/lib/bip/bip/conf/core/coin_conf.dart new file mode 100644 index 0000000..bff5190 --- /dev/null +++ b/lib/bip/bip/conf/core/coin_conf.dart @@ -0,0 +1,21 @@ +import 'package:blockchain_utils/bip/address/encoder.dart'; +import 'package:blockchain_utils/bip/bip/bip32/bip32_key_net_ver.dart'; +import 'package:blockchain_utils/bip/ecc/curve/elliptic_curve_types.dart'; +import 'package:blockchain_utils/bip/coin_conf/coins_name.dart'; + +/// A typedef for the address encoder function that takes optional dynamic parameters. +typedef AddrEncoder = BlockchainAddressEncoder Function([dynamic kwargs]); + +/// A base class representing configuration parameters for a cryptocurrency coin. +abstract class CoinConfig { + /// Base Configuration properties. + abstract final CoinNames coinNames; + abstract final AddrEncoder addressEncoder; + abstract final EllipticCurveTypes type; + abstract final Map addrParams; + abstract final bool? isTestnet; + bool get hasWif; + bool get hasExtendedKeys; + abstract final Bip32KeyNetVersions? keyNetVer; + abstract final List? wifNetVer; +} diff --git a/lib/bip/bip/conf/core/coins.dart b/lib/bip/bip/conf/core/coins.dart new file mode 100644 index 0000000..8199c5c --- /dev/null +++ b/lib/bip/bip/conf/core/coins.dart @@ -0,0 +1,71 @@ +import 'package:blockchain_utils/bip/bip/conf/bip/bip_coins.dart'; +import 'package:blockchain_utils/bip/cardano/cip1852/conf/cip1852_coins.dart'; +import 'package:blockchain_utils/bip/monero/conf/monero_coins.dart'; +import 'package:blockchain_utils/bip/substrate/conf/substrate_coins.dart'; +import 'package:blockchain_utils/exception/exception.dart'; +import 'coin_conf.dart'; + +/// An abstract class representing a collection of cryptocurrency coins. +/// +/// This abstract class defines a contract for classes that provide a collection +/// of cryptocurrency coins. Subclasses should implement the 'value' getter to +/// return an instance of themselves or a specific type that represents the +/// collection of coins. +abstract class CryptoCoins { + /// Gets the collection of cryptocurrency coins. + CryptoCoins get value; + + String get coinName; + + T get conf; + + static CryptoCoins? getCoin(String name, CoinProposal proposal) { + switch (proposal) { + case BipProposal.bip44: + case BipProposal.bip49: + case BipProposal.bip84: + case BipProposal.bip86: + return BipCoins.fromName(name, proposal as BipProposal); + case CipProposal.cip1852: + return Cip1852Coins.fromName(name); + case SubstratePropoosal.substrate: + return SubstrateCoins.fromName(name); + case MoneroProposal.monero: + return MoneroCoins.fromName(name); + default: + return null; + } + } + + CoinProposal get proposal; + + bool get isBipCoin; + + @override + String toString() { + return "$runtimeType.$coinName"; + } +} + +abstract class CoinProposal { + String get specName; + CoinProposal get value; + + static CoinProposal fromName(String name) { + switch (name) { + case "cip1852": + return CipProposal.cip1852; + case "substrate": + return SubstratePropoosal.substrate; + case "monero": + return MoneroProposal.monero; + default: + return BipProposal.values.firstWhere( + (element) => element.name == name, + orElse: () => throw MessageException( + "Unable to locate a proposal with the given name.", + details: {"Name": name}), + ); + } + } +} diff --git a/lib/bip/cardano/cip1852/cip1852.dart b/lib/bip/cardano/cip1852/cip1852.dart index 8db098c..4788e94 100644 --- a/lib/bip/cardano/cip1852/cip1852.dart +++ b/lib/bip/cardano/cip1852/cip1852.dart @@ -56,7 +56,7 @@ import 'package:blockchain_utils/bip/bip/bip32/base/bip32_base.dart'; 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/bip_coin_conf.dart'; +import 'package:blockchain_utils/bip/bip/conf/config/bip_coin_conf.dart'; import 'package:blockchain_utils/bip/cardano/cip1852/conf/cip1852_coins.dart'; /// Constants related to CIP-1852 (Cardano Improvement Proposal 1852). @@ -70,11 +70,11 @@ class Cip1852Const { class Cip1852 extends Bip44Base { /// private constractor - Cip1852._(Bip32Base bip32Obj, CoinConfig coinConf) + Cip1852._(Bip32Base bip32Obj, BipCoinConfig coinConf) : super(bip32Obj, coinConf); /// Constructor for creating a [Cip1852] object from a bip32 object and coin. - Cip1852.fromBip32(Bip32Base bip, CoinConfig coin) : super(bip, coin); + Cip1852.fromBip32(Bip32Base bip, BipCoinConfig coin) : super(bip, coin); /// Constructor for creating a [Cip1852] object from a seed and coin. Cip1852.fromSeed(List seedBytes, Cip1852Coins coinType) diff --git a/lib/bip/cardano/cip1852/conf/cip1852_coins.dart b/lib/bip/cardano/cip1852/conf/cip1852_coins.dart index 923b432..f2b38fc 100644 --- a/lib/bip/cardano/cip1852/conf/cip1852_coins.dart +++ b/lib/bip/cardano/cip1852/conf/cip1852_coins.dart @@ -1,10 +1,12 @@ -import 'package:blockchain_utils/bip/bip/conf/bip_coin_conf.dart'; -import 'package:blockchain_utils/bip/bip/conf/bip_coins.dart'; +import 'package:blockchain_utils/bip/bip/bip32/bip32_key_data.dart'; +import 'package:blockchain_utils/bip/bip/conf/bip/bip_coins.dart'; +import 'package:blockchain_utils/bip/bip/conf/config/bip_coin_conf.dart'; +import 'package:blockchain_utils/bip/cardano/cip1852/cip1852.dart'; import 'package:blockchain_utils/bip/cardano/cip1852/conf/cip1852_conf.dart'; /// An enumeration of supported cryptocurrencies for CIP1852. It includes both main /// networks and test networks of various cryptocurrencies. -class Cip1852Coins implements CryptoCoins { +class Cip1852Coins extends BipCoins { /// mainnets static const Cip1852Coins cardanoIcarus = Cip1852Coins._('cardanoIcarus'); static const Cip1852Coins cardanoLedger = Cip1852Coins._('cardanoLedger'); @@ -36,11 +38,11 @@ class Cip1852Coins implements CryptoCoins { } @override - CoinConfig get conf => _coinToConf[this]!; + BipCoinConfig get conf => _coinToConf[this]!; /// A mapping that associates each Cip1852Coins (enum) with its corresponding - /// CoinConfig configuration. - static final Map _coinToConf = { + /// BipCoinConfig configuration. + static final Map _coinToConf = { Cip1852Coins.cardanoIcarus: Cip1852Conf.cardanoIcarusMainNet, Cip1852Coins.cardanoLedger: Cip1852Conf.cardanoLedgerMainNet, Cip1852Coins.cardanoIcarusTestnet: Cip1852Conf.cardanoIcarusTestNet, @@ -48,5 +50,23 @@ class Cip1852Coins implements CryptoCoins { }; @override - CryptoProposal get proposal => CipProposal.cip1852; + BipProposal get proposal => CipProposal.cip1852; +} + +class CipProposal implements BipProposal { + static const CipProposal cip1852 = CipProposal._('cip1852'); + + const CipProposal._(this.name); + @override + final String name; + + @override + String get specName => name; + @override + CipProposal get value => this; + + static const List values = [cip1852]; + + @override + Bip32KeyIndex get purpose => Cip1852Const.purpose; } diff --git a/lib/bip/cardano/cip1852/conf/cip1852_conf.dart b/lib/bip/cardano/cip1852/conf/cip1852_conf.dart index 6606792..d843d2b 100644 --- a/lib/bip/cardano/cip1852/conf/cip1852_conf.dart +++ b/lib/bip/cardano/cip1852/conf/cip1852_conf.dart @@ -1,8 +1,8 @@ import 'package:blockchain_utils/bip/address/ada/ada_shelley_addr.dart'; import 'package:blockchain_utils/bip/address/ada/network.dart'; import 'package:blockchain_utils/bip/bip/bip32/bip32_const.dart'; -import 'package:blockchain_utils/bip/bip/conf/bip_coin_conf.dart'; -import 'package:blockchain_utils/bip/bip/conf/bip_conf_const.dart'; +import 'package:blockchain_utils/bip/bip/conf/config/bip_coin_conf.dart'; +import 'package:blockchain_utils/bip/bip/conf/const/bip_conf_const.dart'; import 'package:blockchain_utils/bip/coin_conf/coins_conf.dart'; import 'package:blockchain_utils/bip/ecc/curve/elliptic_curve_types.dart'; import 'package:blockchain_utils/bip/slip/slip44/slip44.dart'; @@ -10,7 +10,7 @@ import 'package:blockchain_utils/bip/slip/slip44/slip44.dart'; /// A configuration class for CIP1852 coins that defines the class Cip1852Conf { // Configuration for Cardano main net (Icarus) - static final CoinConfig cardanoIcarusMainNet = CoinConfig( + static final BipCoinConfig cardanoIcarusMainNet = BipCoinConfig( coinNames: CoinsConf.cardanoMainNet.coinName, coinIdx: Slip44.cardano, isTestnet: false, @@ -26,7 +26,7 @@ class Cip1852Conf { ); // Configuration for Cardano test net (Icarus) - static final CoinConfig cardanoIcarusTestNet = CoinConfig( + static final BipCoinConfig cardanoIcarusTestNet = BipCoinConfig( coinNames: CoinsConf.cardanoTestNet.coinName, coinIdx: Slip44.testnet, isTestnet: true, @@ -42,7 +42,7 @@ class Cip1852Conf { ); // Configuration for Cardano main net (Ledger) - static final CoinConfig cardanoLedgerMainNet = CoinConfig( + static final BipCoinConfig cardanoLedgerMainNet = BipCoinConfig( coinNames: CoinsConf.cardanoMainNet.coinName, coinIdx: Slip44.cardano, isTestnet: false, @@ -55,7 +55,7 @@ class Cip1852Conf { ); // Configuration for Cardano test net (Ledger) - static final CoinConfig cardanoLedgerTestNet = CoinConfig( + static final BipCoinConfig cardanoLedgerTestNet = BipCoinConfig( coinNames: CoinsConf.cardanoTestNet.coinName, coinIdx: Slip44.testnet, isTestnet: true, diff --git a/lib/bip/cardano/shelley/cardano_shelley_keys.dart b/lib/bip/cardano/shelley/cardano_shelley_keys.dart index 7fc5b75..bcfe36b 100644 --- a/lib/bip/cardano/shelley/cardano_shelley_keys.dart +++ b/lib/bip/cardano/shelley/cardano_shelley_keys.dart @@ -1,12 +1,12 @@ import 'package:blockchain_utils/bip/address/ada/ada_shelley_addr.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'; /// A class that represents public keys associated with a Cardano Shelley wallet. class CardanoShelleyPublicKeys { final Bip32PublicKey pubAddrKey; final Bip32PublicKey pubSkKey; - final CoinConfig coinConf; + final BipCoinConfig coinConf; /// Constructor to create a Cardano Shelley Public Keys instance. /// @@ -56,7 +56,7 @@ class CardanoShelleyPublicKeys { class CardanoShelleyPrivateKeys { final Bip32PrivateKey privAddrKey; final Bip32PrivateKey privSkKey; - final CoinConfig coinConf; + final BipCoinConfig coinConf; /// Constructor to create a Cardano Shelley Private Keys instance. /// diff --git a/lib/bip/ecc/curve/elliptic_curve_getter.dart b/lib/bip/ecc/curve/elliptic_curve_getter.dart index 596ec75..ff0ffec 100644 --- a/lib/bip/ecc/curve/elliptic_curve_getter.dart +++ b/lib/bip/ecc/curve/elliptic_curve_getter.dart @@ -12,13 +12,15 @@ class EllipticCurveGetter { /// Returns an abstract elliptic curve point generator. static AbstractPoint generatorFromType(EllipticCurveTypes type) { switch (type) { + case EllipticCurveTypes.secp256k1: + return Curves.generatorSecp256k1; case EllipticCurveTypes.nist256p1: return Curves.generator256; case EllipticCurveTypes.ed25519: case EllipticCurveTypes.ed25519Kholaw: return Curves.generatorED25519; default: - return Curves.generatorSecp256k1; + throw UnimplementedError("generatorFromType does not provide curve."); } } } diff --git a/lib/bip/ecc/keys/i_keys.dart b/lib/bip/ecc/keys/i_keys.dart index 1276a40..1cf29ec 100644 --- a/lib/bip/ecc/keys/i_keys.dart +++ b/lib/bip/ecc/keys/i_keys.dart @@ -7,6 +7,7 @@ import 'package:blockchain_utils/bip/ecc/keys/sr25519_keys.dart'; import 'package:blockchain_utils/bip/monero/monero_keys.dart'; import 'package:blockchain_utils/bip/ecc/curve/elliptic_curve_types.dart'; import 'package:blockchain_utils/crypto/crypto/cdsa/point/base.dart'; +import 'package:blockchain_utils/utils/binary/utils.dart'; /// An abstract class representing a generic public key interface for different elliptic curve types. abstract class IPublicKey { @@ -29,6 +30,9 @@ abstract class IPublicKey { return Secp256k1PublicKeyEcdsa.fromBytes(keybytes); } } + factory IPublicKey.fromHex(String keyHex, EllipticCurveTypes type) { + return IPublicKey.fromBytes(BytesUtils.fromHexString(keyHex), type); + } /// Static method to check the validity of a byte array as a public key for a specific elliptic curve type. static bool isValidBytes(List keyBytes, EllipticCurveTypes type) { @@ -95,6 +99,10 @@ abstract class IPrivateKey { return Secp256k1PrivateKeyEcdsa.fromBytes(keyBytes); } + factory IPrivateKey.fromHex(String keyHex, EllipticCurveTypes type) { + return IPrivateKey.fromBytes(BytesUtils.fromHexString(keyHex), type); + } + /// Get the length of the private key in bytes. int get length; diff --git a/lib/bip/monero/conf/monero_coin_conf.dart b/lib/bip/monero/conf/monero_coin_conf.dart index 3713789..8416fb3 100644 --- a/lib/bip/monero/conf/monero_coin_conf.dart +++ b/lib/bip/monero/conf/monero_coin_conf.dart @@ -1,28 +1,54 @@ +import 'package:blockchain_utils/bip/bip/bip32/bip32_key_net_ver.dart'; +import 'package:blockchain_utils/bip/bip/conf/core/coin_conf.dart'; import 'package:blockchain_utils/bip/coin_conf/coin_conf.dart'; import 'package:blockchain_utils/bip/coin_conf/coins_name.dart'; +import 'package:blockchain_utils/bip/ecc/curve/elliptic_curve_types.dart'; /// Configuration class for Monero-based cryptocurrencies, specifying various parameters /// such as network versions, address types, and coin names. -class MoneroCoinConf { +class MoneroCoinConf implements CoinConfig { + @override final CoinNames coinNames; final List addrNetVer; final List intAddrNetVer; final List subaddrNetVer; /// address parameters + @override final Map> addrParams = {}; /// private constructor - MoneroCoinConf._( - this.coinNames, this.addrNetVer, this.intAddrNetVer, this.subaddrNetVer); + MoneroCoinConf._(this.coinNames, this.addrNetVer, this.intAddrNetVer, + this.subaddrNetVer, this.isTestnet); /// MoneroCoinConf from coinConf - factory MoneroCoinConf.fromCoinConf(CoinConf coinConf) { + factory MoneroCoinConf.fromCoinConf(CoinConf coinConf, {bool? isTestnet}) { return MoneroCoinConf._( - coinConf.coinName, - coinConf.params.addrNetVer!, - coinConf.params.addrIntNetVer!, - coinConf.params.subaddrNetVer!, - ); + coinConf.coinName, + coinConf.params.addrNetVer!, + coinConf.params.addrIntNetVer!, + coinConf.params.subaddrNetVer!, + isTestnet); } + + @override + AddrEncoder get addressEncoder => throw UnimplementedError(); + + @override + EllipticCurveTypes get type => EllipticCurveTypes.ed25519Monero; + + @override + final bool? isTestnet; + + @override + bool get hasExtendedKeys => false; + + @override + bool get hasWif => false; + + @override + final Bip32KeyNetVersions? keyNetVer = null; + + @override + final List? wifNetVer = null; } diff --git a/lib/bip/monero/conf/monero_coins.dart b/lib/bip/monero/conf/monero_coins.dart index 883646d..82b50a2 100644 --- a/lib/bip/monero/conf/monero_coins.dart +++ b/lib/bip/monero/conf/monero_coins.dart @@ -1,9 +1,10 @@ -import 'package:blockchain_utils/bip/bip/conf/bip_coin_conf.dart'; -import 'package:blockchain_utils/bip/bip/conf/bip_coins.dart'; +import 'package:blockchain_utils/bip/bip/conf/core/coins.dart'; +import 'monero_coin_conf.dart'; +import 'monero_conf.dart'; /// An enumeration of supported cryptocurrencies for Monero. It includes both main /// networks and test networks of various cryptocurrencies. -class MoneroCoins implements CryptoCoins { +class MoneroCoins implements CryptoCoins { /// mainnets static const MoneroCoins moneroMainnet = MoneroCoins._('moneroMainnet'); @@ -16,6 +17,13 @@ class MoneroCoins implements CryptoCoins { moneroStagenet, moneroTestnet ]; + static MoneroCoins? fromName(String name) { + try { + return _coinToConf.keys.firstWhere((element) => element.name == name); + } on StateError { + return null; + } + } final String name; @@ -32,7 +40,32 @@ class MoneroCoins implements CryptoCoins { } @override - CoinConfig get conf => throw UnimplementedError(); + MoneroCoinConf get conf => _coinToConf[this]!; + @override + CoinProposal get proposal => MoneroProposal.monero; + + /// A mapping that associates each MoneroCoins (enum) with its corresponding + /// MoneroCoinConf configuration. + static final Map _coinToConf = { + MoneroCoins.moneroMainnet: MoneroConf.mainNet, + MoneroCoins.moneroStagenet: MoneroConf.stageNet, + MoneroCoins.moneroTestnet: MoneroConf.testNet + }; + + @override + bool get isBipCoin => false; +} + +class MoneroProposal implements CoinProposal { + static const MoneroProposal monero = MoneroProposal._('monero'); + + const MoneroProposal._(this.name); + final String name; + + @override + String get specName => name; @override - BipProposal get proposal => throw UnimplementedError(); + MoneroProposal get value => this; + + static const List values = [monero]; } diff --git a/lib/bip/monero/conf/monero_conf.dart b/lib/bip/monero/conf/monero_conf.dart index a0b3352..c5702e3 100644 --- a/lib/bip/monero/conf/monero_conf.dart +++ b/lib/bip/monero/conf/monero_conf.dart @@ -1,30 +1,9 @@ -import 'package:blockchain_utils/bip/bip/conf/bip_coins.dart'; import 'package:blockchain_utils/bip/coin_conf/coins_conf.dart'; import 'package:blockchain_utils/bip/monero/conf/monero_coin_conf.dart'; -import 'package:blockchain_utils/bip/monero/conf/monero_coins.dart'; -import 'package:blockchain_utils/exception/exception.dart'; /// A configuration class for Monero that defines the key network versions and -/// maps each supported MoneroCoins to its corresponding CoinConfig. +/// maps each supported MoneroCoins to its corresponding BipCoinConfig. class MoneroConf { - /// Retrieves the MoneroCoinConf for the given MoneroCoins. If the provided coin - /// is not an instance of MoneroCoins, an error is thrown. - static MoneroCoinConf getCoin(CryptoCoins coin) { - if (coin is! MoneroCoins) { - throw const ArgumentException( - "Coin type is not an enumerative of MoneroCoins"); - } - return coinToConf[coin.value]!; - } - - /// A mapping that associates each MoneroCoins (enum) with its corresponding - /// MoneroCoinConf configuration. - static final Map coinToConf = { - MoneroCoins.moneroMainnet: MoneroConf.mainNet, - MoneroCoins.moneroStagenet: MoneroConf.stageNet, - MoneroCoins.moneroTestnet: MoneroConf.testNet, - }; - // Configuration for Monero main net static final MoneroCoinConf mainNet = MoneroCoinConf.fromCoinConf(CoinsConf.moneroMainNet); diff --git a/lib/bip/monero/monero_base.dart b/lib/bip/monero/monero_base.dart index 974f6f8..0a088b8 100644 --- a/lib/bip/monero/monero_base.dart +++ b/lib/bip/monero/monero_base.dart @@ -3,7 +3,6 @@ import 'package:blockchain_utils/bip/ecc/keys/ed25519_keys.dart'; import 'package:blockchain_utils/crypto/crypto/cdsa/utils/ed25519_utils.dart'; import 'package:blockchain_utils/bip/monero/conf/monero_coin_conf.dart'; import 'package:blockchain_utils/bip/monero/conf/monero_coins.dart'; -import 'package:blockchain_utils/bip/monero/conf/monero_conf.dart'; import 'package:blockchain_utils/bip/monero/monero_exc.dart'; import 'package:blockchain_utils/bip/monero/monero_keys.dart'; import 'package:blockchain_utils/bip/monero/monero_subaddr.dart'; @@ -61,7 +60,7 @@ class Monero { final mPubSkey = mPrivSkey.publicKey; final mPubVkey = mPrivVkey.publicKey; return Monero.__( - coinConf: MoneroConf.getCoin(coinType), + coinConf: coinType.conf, privSkey: mPrivSkey, privVkey: mPrivVkey, pubSkey: mPubSkey, @@ -72,7 +71,7 @@ class Monero { final mPubSkey = MoneroPublicKey.fromBytes(pubKey); final mPubVkey = mPrivVkey.publicKey; return Monero.__( - coinConf: MoneroConf.getCoin(coinType), + coinConf: coinType.conf, privSkey: null, privVkey: mPrivVkey, pubSkey: mPubSkey, diff --git a/lib/bip/substrate/conf/substrate_coin_conf.dart b/lib/bip/substrate/conf/substrate_coin_conf.dart index 8dd7d38..c502c42 100644 --- a/lib/bip/substrate/conf/substrate_coin_conf.dart +++ b/lib/bip/substrate/conf/substrate_coin_conf.dart @@ -1,9 +1,13 @@ +import 'package:blockchain_utils/bip/bip/bip32/bip32_key_net_ver.dart'; +import 'package:blockchain_utils/bip/bip/conf/core/coin_conf.dart'; import 'package:blockchain_utils/bip/coin_conf/coin_conf.dart'; import 'package:blockchain_utils/bip/coin_conf/coins_name.dart'; +import 'package:blockchain_utils/bip/ecc/curve/elliptic_curve_types.dart'; /// A class representing the configuration for a Substrate-based cryptocurrency. -class SubstrateCoinConf { +class SubstrateCoinConf implements CoinConfig { /// Coin names and symbols + @override final CoinNames coinNames; /// Address format identifier @@ -13,18 +17,50 @@ class SubstrateCoinConf { /// /// It initializes the Substrate cryptocurrency's coin names and symbols [coinNames] /// and the SS58 address format identifier [ss58Format]. - SubstrateCoinConf({required this.coinNames, required this.ss58Format}); + const SubstrateCoinConf( + {required this.coinNames, + required this.ss58Format, + required this.addressEncoder, + required this.type, + this.addrParams = const {}, + this.isTestnet}); /// Factory method to create a SubstrateCoinConf from a generic CoinConf. /// /// This method takes a generic `CoinConf` instance and extracts the coin names /// and SS58 address format information to create a `SubstrateCoinConf`. - factory SubstrateCoinConf.fromCoinConf(CoinConf coinConf) { + factory SubstrateCoinConf.fromCoinConf({ + required CoinConf coinConf, + required AddrEncoder addressEncode, + required EllipticCurveTypes type, + }) { return SubstrateCoinConf( coinNames: coinConf.coinName, - ss58Format: coinConf.params.addrSs58Format!); + ss58Format: coinConf.params.addrSs58Format!, + addressEncoder: addressEncode, + type: type); } - /// Get the Substrate-specific address parameters as a map. - Map get addrParams => {"ss58_format": ss58Format}; + @override + final AddrEncoder addressEncoder; + + @override + final EllipticCurveTypes type; + + @override + final Map addrParams; + + @override + final bool? isTestnet; + + @override + bool get hasExtendedKeys => false; + + @override + bool get hasWif => false; + + @override + final Bip32KeyNetVersions? keyNetVer = null; + @override + final List? wifNetVer = null; } diff --git a/lib/bip/substrate/conf/substrate_coins.dart b/lib/bip/substrate/conf/substrate_coins.dart index 6476de5..6c51eae 100644 --- a/lib/bip/substrate/conf/substrate_coins.dart +++ b/lib/bip/substrate/conf/substrate_coins.dart @@ -1,22 +1,103 @@ -import 'package:blockchain_utils/bip/bip/conf/bip_coin_conf.dart'; -import 'package:blockchain_utils/bip/bip/conf/bip_coins.dart'; +import 'package:blockchain_utils/bip/bip/conf/core/coins.dart'; +import 'package:blockchain_utils/bip/substrate/conf/substrate_coin_conf.dart'; + +import 'substrate_conf.dart'; /// An enumeration of supported cryptocurrencies for SubstrateCoins. -class SubstrateCoins implements CryptoCoins { - static const SubstrateCoins acala = SubstrateCoins._('acala'); - static const SubstrateCoins bifrost = SubstrateCoins._('bifrost'); - static const SubstrateCoins chainx = SubstrateCoins._('chainx'); - static const SubstrateCoins edgeware = SubstrateCoins._('edgeware'); - static const SubstrateCoins generic = SubstrateCoins._('generic'); - static const SubstrateCoins karura = SubstrateCoins._('karura'); - static const SubstrateCoins kusama = SubstrateCoins._('kusama'); - static const SubstrateCoins moonbeam = SubstrateCoins._('moonbeam'); - static const SubstrateCoins moonriver = SubstrateCoins._('moonriver'); - static const SubstrateCoins phala = SubstrateCoins._('phala'); - static const SubstrateCoins plasm = SubstrateCoins._('plasm'); - static const SubstrateCoins polkadot = SubstrateCoins._('polkadot'); - static const SubstrateCoins sora = SubstrateCoins._('sora'); - static const SubstrateCoins stafi = SubstrateCoins._('stafi'); +class SubstrateCoins implements CryptoCoins { + static const SubstrateCoins acalaEd25519 = SubstrateCoins._('acalaEd25519'); + static const SubstrateCoins acalaSecp256k1 = + SubstrateCoins._('acalaSecp256k1'); + static const SubstrateCoins acalaSr25519 = SubstrateCoins._('acalaSr25519'); + + // Configuration for Bifrost + static const SubstrateCoins bifrostEd25519 = + SubstrateCoins._('bifrostEd25519'); + static const SubstrateCoins bifrostSecp256k1 = + SubstrateCoins._('bifrostSecp256k1'); + static const SubstrateCoins bifrostSr25519 = + SubstrateCoins._('bifrostSr25519'); + +// Configuration for ChainX + static const SubstrateCoins chainxEd25519 = SubstrateCoins._('chainxEd25519'); + static const SubstrateCoins chainxSecp256k1 = + SubstrateCoins._('chainxSecp256k1'); + static const SubstrateCoins chainxSr25519 = SubstrateCoins._('chainxSr25519'); + +// Configuration for Edgeware + static const SubstrateCoins edgewareEd25519 = + SubstrateCoins._('edgewareEd25519'); + static const SubstrateCoins edgewareSecp256k1 = + SubstrateCoins._('edgewareSecp256k1'); + static const SubstrateCoins edgewareSr25519 = + SubstrateCoins._('edgewareSr25519'); + +// Configuration for generic Substrate coin + static const SubstrateCoins genericEd25519 = + SubstrateCoins._('genericEd25519'); + static const SubstrateCoins genericSecp256k1 = + SubstrateCoins._('genericSecp256k1'); + static const SubstrateCoins genericSr25519 = + SubstrateCoins._('genericSr25519'); + +// Configuration for Karura + static const SubstrateCoins karuraEd25519 = SubstrateCoins._('karuraEd25519'); + static const SubstrateCoins karuraSecp256k1 = + SubstrateCoins._('karuraSecp256k1'); + static const SubstrateCoins karuraSr25519 = SubstrateCoins._('karuraSr25519'); + +// Configuration for Kusama + static const SubstrateCoins kusamaEd25519 = SubstrateCoins._('kusamaEd25519'); + static const SubstrateCoins kusamaSecp256k1 = + SubstrateCoins._('kusamaSecp256k1'); + static const SubstrateCoins kusamaSr25519 = SubstrateCoins._('kusamaSr25519'); + +// Configuration for Moonbeam + static const SubstrateCoins moonbeamEd25519 = + SubstrateCoins._('moonbeamEd25519'); + static const SubstrateCoins moonbeamSecp256k1 = + SubstrateCoins._('moonbeamSecp256k1'); + static const SubstrateCoins moonbeamSr25519 = + SubstrateCoins._('moonbeamSr25519'); + +// Configuration for Moonriver + static const SubstrateCoins moonriverEd25519 = + SubstrateCoins._('moonriverEd25519'); + static const SubstrateCoins moonriverSecp256k1 = + SubstrateCoins._('moonriverSecp256k1'); + static const SubstrateCoins moonriverSr25519 = + SubstrateCoins._('moonriverSr25519'); + +// Configuration for Phala + static const SubstrateCoins phalaEd25519 = SubstrateCoins._('phalaEd25519'); + static const SubstrateCoins phalaSecp256k1 = + SubstrateCoins._('phalaSecp256k1'); + static const SubstrateCoins phalaSr25519 = SubstrateCoins._('phalaSr25519'); + +// Configuration for Plasm + static const SubstrateCoins plasmEd25519 = SubstrateCoins._('plasmEd25519'); + static const SubstrateCoins plasmSecp256k1 = + SubstrateCoins._('plasmSecp256k1'); + static const SubstrateCoins plasmSr25519 = SubstrateCoins._('plasmSr25519'); + +// Configuration for Polkadot + static const SubstrateCoins polkadotEd25519 = + SubstrateCoins._('polkadotEd25519'); + static const SubstrateCoins polkadotSecp256k1 = + SubstrateCoins._('polkadotSecp256k1'); + static const SubstrateCoins polkadotSr25519 = + SubstrateCoins._('polkadotSr25519'); + +// Configuration for Sora + static const SubstrateCoins soraEd25519 = SubstrateCoins._('soraEd25519'); + static const SubstrateCoins soraSecp256k1 = SubstrateCoins._('soraSecp256k1'); + static const SubstrateCoins soraSr25519 = SubstrateCoins._('soraSr25519'); + +// Configuration for Stafi + static const SubstrateCoins stafiEd25519 = SubstrateCoins._('stafiEd25519'); + static const SubstrateCoins stafiSecp256k1 = + SubstrateCoins._('stafiSecp256k1'); + static const SubstrateCoins stafiSr25519 = SubstrateCoins._('stafiSr25519'); final String name; @@ -31,24 +112,123 @@ class SubstrateCoins implements CryptoCoins { } @override - CoinConfig get conf => throw UnimplementedError(); + SubstrateCoinConf get conf => _coinToConf[this]!; @override - BipProposal get proposal => throw UnimplementedError(); + CoinProposal get proposal => SubstratePropoosal.substrate; + + static SubstrateCoins? fromName(String name) { + try { + return values.firstWhere((element) => element.name == name); + } on StateError { + return null; + } + } static const List values = [ - acala, - bifrost, - chainx, - edgeware, - generic, - karura, - kusama, - moonbeam, - moonriver, - phala, - plasm, - polkadot, - sora, - stafi, + SubstrateCoins.acalaEd25519, + SubstrateCoins.acalaSecp256k1, + SubstrateCoins.acalaSr25519, + SubstrateCoins.bifrostEd25519, + SubstrateCoins.bifrostSecp256k1, + SubstrateCoins.bifrostSr25519, + SubstrateCoins.chainxEd25519, + SubstrateCoins.chainxSecp256k1, + SubstrateCoins.chainxSr25519, + SubstrateCoins.edgewareEd25519, + SubstrateCoins.edgewareSecp256k1, + SubstrateCoins.edgewareSr25519, + SubstrateCoins.genericEd25519, + SubstrateCoins.genericSecp256k1, + SubstrateCoins.genericSr25519, + SubstrateCoins.karuraEd25519, + SubstrateCoins.karuraSecp256k1, + SubstrateCoins.karuraSr25519, + SubstrateCoins.kusamaEd25519, + SubstrateCoins.kusamaSecp256k1, + SubstrateCoins.kusamaSr25519, + SubstrateCoins.moonbeamEd25519, + SubstrateCoins.moonbeamSecp256k1, + SubstrateCoins.moonbeamSr25519, + SubstrateCoins.moonriverEd25519, + SubstrateCoins.moonriverSecp256k1, + SubstrateCoins.moonriverSr25519, + SubstrateCoins.phalaEd25519, + SubstrateCoins.phalaSecp256k1, + SubstrateCoins.phalaSr25519, + SubstrateCoins.plasmEd25519, + SubstrateCoins.plasmSecp256k1, + SubstrateCoins.plasmSr25519, + SubstrateCoins.polkadotEd25519, + SubstrateCoins.polkadotSecp256k1, + SubstrateCoins.polkadotSr25519, + SubstrateCoins.soraEd25519, + SubstrateCoins.soraSecp256k1, + SubstrateCoins.soraSr25519, + SubstrateCoins.stafiEd25519, + SubstrateCoins.stafiSecp256k1, + SubstrateCoins.stafiSr25519 ]; + + /// A mapping that associates each SubstrateCoins (enum) with its corresponding + /// SubstrateCoinConf configuration. + static final Map _coinToConf = { + SubstrateCoins.acalaEd25519: SubstrateConf.acalaEd25519, + SubstrateCoins.acalaSecp256k1: SubstrateConf.acalaSecp256k1, + SubstrateCoins.acalaSr25519: SubstrateConf.acalaSr25519, + SubstrateCoins.bifrostEd25519: SubstrateConf.bifrostEd25519, + SubstrateCoins.bifrostSecp256k1: SubstrateConf.bifrostSecp256k1, + SubstrateCoins.bifrostSr25519: SubstrateConf.bifrostSr25519, + SubstrateCoins.chainxEd25519: SubstrateConf.chainXEd25519, + SubstrateCoins.chainxSecp256k1: SubstrateConf.chainXSecp256k1, + SubstrateCoins.chainxSr25519: SubstrateConf.chainXSr25519, + SubstrateCoins.edgewareEd25519: SubstrateConf.edgewareEd25519, + SubstrateCoins.edgewareSecp256k1: SubstrateConf.edgewareSecp256k1, + SubstrateCoins.edgewareSr25519: SubstrateConf.edgewareSr25519, + SubstrateCoins.genericEd25519: SubstrateConf.genericEd25519, + SubstrateCoins.genericSecp256k1: SubstrateConf.genericSecp256k1, + SubstrateCoins.genericSr25519: SubstrateConf.genericSr25519, + SubstrateCoins.karuraEd25519: SubstrateConf.karuraEd25519, + SubstrateCoins.karuraSecp256k1: SubstrateConf.karuraSecp256k1, + SubstrateCoins.karuraSr25519: SubstrateConf.karuraSr25519, + SubstrateCoins.kusamaEd25519: SubstrateConf.kusamaEd25519, + SubstrateCoins.kusamaSecp256k1: SubstrateConf.kusamaSecp256k1, + SubstrateCoins.kusamaSr25519: SubstrateConf.kusamaSr25519, + SubstrateCoins.moonbeamEd25519: SubstrateConf.moonbeamEd25519, + SubstrateCoins.moonbeamSecp256k1: SubstrateConf.moonbeamSecp256k1, + SubstrateCoins.moonbeamSr25519: SubstrateConf.moonbeamSr25519, + SubstrateCoins.moonriverEd25519: SubstrateConf.moonriverEd25519, + SubstrateCoins.moonriverSecp256k1: SubstrateConf.moonriverSecp256k1, + SubstrateCoins.moonriverSr25519: SubstrateConf.moonriverSr25519, + SubstrateCoins.phalaEd25519: SubstrateConf.phalaEd25519, + SubstrateCoins.phalaSecp256k1: SubstrateConf.phalaSecp256k1, + SubstrateCoins.phalaSr25519: SubstrateConf.phalaSr25519, + SubstrateCoins.plasmEd25519: SubstrateConf.plasmEd25519, + SubstrateCoins.plasmSecp256k1: SubstrateConf.plasmSecp256k1, + SubstrateCoins.plasmSr25519: SubstrateConf.plasmSr25519, + SubstrateCoins.polkadotEd25519: SubstrateConf.polkadotEd25519, + SubstrateCoins.polkadotSecp256k1: SubstrateConf.polkadotSecp256k1, + SubstrateCoins.polkadotSr25519: SubstrateConf.polkadotSr25519, + SubstrateCoins.soraEd25519: SubstrateConf.soraEd25519, + SubstrateCoins.soraSecp256k1: SubstrateConf.soraSecp256k1, + SubstrateCoins.soraSr25519: SubstrateConf.soraSr25519, + SubstrateCoins.stafiEd25519: SubstrateConf.stafiEd25519, + SubstrateCoins.stafiSecp256k1: SubstrateConf.stafiSecp256k1, + SubstrateCoins.stafiSr25519: SubstrateConf.stafiSr25519 + }; + @override + bool get isBipCoin => false; +} + +class SubstratePropoosal implements CoinProposal { + static const SubstratePropoosal substrate = SubstratePropoosal._('substrate'); + + const SubstratePropoosal._(this.name); + final String name; + + @override + String get specName => name; + @override + CoinProposal get value => this; + + static const List values = [substrate]; } diff --git a/lib/bip/substrate/conf/substrate_conf.dart b/lib/bip/substrate/conf/substrate_conf.dart index 9451a95..01b627d 100644 --- a/lib/bip/substrate/conf/substrate_conf.dart +++ b/lib/bip/substrate/conf/substrate_conf.dart @@ -1,94 +1,230 @@ -import 'package:blockchain_utils/bip/bip/conf/bip_coins.dart'; +import 'package:blockchain_utils/bip/address/substrate_addr.dart'; import 'package:blockchain_utils/bip/coin_conf/coins_conf.dart'; -import 'package:blockchain_utils/bip/substrate/conf/substrate_coins.dart'; -import 'package:blockchain_utils/exception/exception.dart'; - +import 'package:blockchain_utils/bip/ecc/curve/elliptic_curve_types.dart'; import 'substrate_coin_conf.dart'; /// A configuration class for Substrate coins that defines the key network versions and /// maps each supported SubstrateCoins to its corresponding SubstrateConf. class SubstrateConf { - /// Retrieves the SubstrateCoinConf for the given SubstrateCoin. If the provided coin - /// is not an instance of SubstrateCoins, an error is thrown. - static SubstrateCoinConf getCoin(CryptoCoins coin) { - if (coin is! SubstrateCoins) { - throw const ArgumentException( - "Coin type is not an enumerative of SubstrateCoins"); - } - return coinToConf[coin.value]!; - } - - /// A mapping that associates each SubstrateCoins (enum) with its corresponding - /// SubstrateCoinConf configuration. - static final Map coinToConf = { - SubstrateCoins.acala: SubstrateConf.acala, - SubstrateCoins.bifrost: SubstrateConf.bifrost, - SubstrateCoins.chainx: SubstrateConf.chainX, - SubstrateCoins.edgeware: SubstrateConf.edgeware, - SubstrateCoins.generic: SubstrateConf.generic, - SubstrateCoins.karura: SubstrateConf.karura, - SubstrateCoins.kusama: SubstrateConf.kusama, - SubstrateCoins.moonbeam: SubstrateConf.moonbeam, - SubstrateCoins.moonriver: SubstrateConf.moonriver, - SubstrateCoins.phala: SubstrateConf.phala, - SubstrateCoins.plasm: SubstrateConf.plasm, - SubstrateCoins.polkadot: SubstrateConf.polkadot, - SubstrateCoins.sora: SubstrateConf.sora, - SubstrateCoins.stafi: SubstrateConf.stafi, - }; + static final SubstrateCoinConf acalaEd25519 = SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.acala, + addressEncode: ([kwargs]) => SubstrateEd25519AddrEncoder(), + type: EllipticCurveTypes.ed25519); // Configuration for Acala - static final SubstrateCoinConf acala = - SubstrateCoinConf.fromCoinConf(CoinsConf.acala); - - // Configuration for Bifrost - static final SubstrateCoinConf bifrost = - SubstrateCoinConf.fromCoinConf(CoinsConf.bifrost); - - // Configuration for ChainX - static final SubstrateCoinConf chainX = - SubstrateCoinConf.fromCoinConf(CoinsConf.chainX); - - // Configuration for Edgeware - static final SubstrateCoinConf edgeware = - SubstrateCoinConf.fromCoinConf(CoinsConf.edgeware); - - // Configuration for generic Substrate coin - static final SubstrateCoinConf generic = - SubstrateCoinConf.fromCoinConf(CoinsConf.genericSubstrate); - - // Configuration for Karura - static final SubstrateCoinConf karura = - SubstrateCoinConf.fromCoinConf(CoinsConf.karura); + static final SubstrateCoinConf acalaSecp256k1 = + SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.acala, + addressEncode: ([kwargs]) => SubstrateSecp256k1AddrEncoder(), + type: EllipticCurveTypes.secp256k1); - // Configuration for Kusama - static final SubstrateCoinConf kusama = - SubstrateCoinConf.fromCoinConf(CoinsConf.kusama); + static final SubstrateCoinConf acalaSr25519 = SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.acala, + addressEncode: ([kwargs]) => SubstrateSr25519AddrEncoder(), + type: EllipticCurveTypes.sr25519); - // Configuration for Moonbeam - static final SubstrateCoinConf moonbeam = - SubstrateCoinConf.fromCoinConf(CoinsConf.moonbeam); - - // Configuration for Moonriver - static final SubstrateCoinConf moonriver = - SubstrateCoinConf.fromCoinConf(CoinsConf.moonriver); - - // Configuration for Phala - static final SubstrateCoinConf phala = - SubstrateCoinConf.fromCoinConf(CoinsConf.phala); - - // Configuration for Plasm - static final SubstrateCoinConf plasm = - SubstrateCoinConf.fromCoinConf(CoinsConf.plasm); - - // Configuration for Polkadot - static final SubstrateCoinConf polkadot = - SubstrateCoinConf.fromCoinConf(CoinsConf.polkadot); - - // Configuration for Sora - static final SubstrateCoinConf sora = - SubstrateCoinConf.fromCoinConf(CoinsConf.sora); - - // Configuration for Stafi - static final SubstrateCoinConf stafi = - SubstrateCoinConf.fromCoinConf(CoinsConf.stafi); + // Configuration for Bifrost + static final SubstrateCoinConf bifrostEd25519 = + SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.bifrost, + addressEncode: ([kwargs]) => SubstrateEd25519AddrEncoder(), + type: EllipticCurveTypes.ed25519); + static final SubstrateCoinConf bifrostSecp256k1 = + SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.bifrost, + addressEncode: ([kwargs]) => SubstrateSecp256k1AddrEncoder(), + type: EllipticCurveTypes.secp256k1); + static final SubstrateCoinConf bifrostSr25519 = + SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.bifrost, + addressEncode: ([kwargs]) => SubstrateSr25519AddrEncoder(), + type: EllipticCurveTypes.sr25519); + +// Configuration for ChainX + static final SubstrateCoinConf chainXEd25519 = SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.chainX, + addressEncode: ([kwargs]) => SubstrateEd25519AddrEncoder(), + type: EllipticCurveTypes.ed25519); + static final SubstrateCoinConf chainXSecp256k1 = + SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.chainX, + addressEncode: ([kwargs]) => SubstrateSecp256k1AddrEncoder(), + type: EllipticCurveTypes.secp256k1); + static final SubstrateCoinConf chainXSr25519 = SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.chainX, + addressEncode: ([kwargs]) => SubstrateSr25519AddrEncoder(), + type: EllipticCurveTypes.sr25519); + +// Configuration for Edgeware + static final SubstrateCoinConf edgewareEd25519 = + SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.edgeware, + addressEncode: ([kwargs]) => SubstrateEd25519AddrEncoder(), + type: EllipticCurveTypes.ed25519); + static final SubstrateCoinConf edgewareSecp256k1 = + SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.edgeware, + addressEncode: ([kwargs]) => SubstrateSecp256k1AddrEncoder(), + type: EllipticCurveTypes.secp256k1); + static final SubstrateCoinConf edgewareSr25519 = + SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.edgeware, + addressEncode: ([kwargs]) => SubstrateSr25519AddrEncoder(), + type: EllipticCurveTypes.sr25519); + +// Configuration for generic Substrate coin + static final SubstrateCoinConf genericEd25519 = + SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.genericSubstrate, + addressEncode: ([kwargs]) => SubstrateEd25519AddrEncoder(), + type: EllipticCurveTypes.ed25519); + static final SubstrateCoinConf genericSecp256k1 = + SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.genericSubstrate, + addressEncode: ([kwargs]) => SubstrateSecp256k1AddrEncoder(), + type: EllipticCurveTypes.secp256k1); + static final SubstrateCoinConf genericSr25519 = + SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.genericSubstrate, + addressEncode: ([kwargs]) => SubstrateSr25519AddrEncoder(), + type: EllipticCurveTypes.sr25519); + +// Configuration for Karura + static final SubstrateCoinConf karuraEd25519 = SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.karura, + addressEncode: ([kwargs]) => SubstrateEd25519AddrEncoder(), + type: EllipticCurveTypes.ed25519); + static final SubstrateCoinConf karuraSecp256k1 = + SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.karura, + addressEncode: ([kwargs]) => SubstrateSecp256k1AddrEncoder(), + type: EllipticCurveTypes.secp256k1); + static final SubstrateCoinConf karuraSr25519 = SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.karura, + addressEncode: ([kwargs]) => SubstrateSr25519AddrEncoder(), + type: EllipticCurveTypes.sr25519); + +// Configuration for Kusama + static final SubstrateCoinConf kusamaEd25519 = SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.kusama, + addressEncode: ([kwargs]) => SubstrateEd25519AddrEncoder(), + type: EllipticCurveTypes.ed25519); + static final SubstrateCoinConf kusamaSecp256k1 = + SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.kusama, + addressEncode: ([kwargs]) => SubstrateSecp256k1AddrEncoder(), + type: EllipticCurveTypes.secp256k1); + static final SubstrateCoinConf kusamaSr25519 = SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.kusama, + addressEncode: ([kwargs]) => SubstrateSr25519AddrEncoder(), + type: EllipticCurveTypes.sr25519); + +// Configuration for Moonbeam + static final SubstrateCoinConf moonbeamEd25519 = + SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.moonbeam, + addressEncode: ([kwargs]) => SubstrateEd25519AddrEncoder(), + type: EllipticCurveTypes.ed25519); + static final SubstrateCoinConf moonbeamSecp256k1 = + SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.moonbeam, + addressEncode: ([kwargs]) => SubstrateSecp256k1AddrEncoder(), + type: EllipticCurveTypes.secp256k1); + static final SubstrateCoinConf moonbeamSr25519 = + SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.moonbeam, + addressEncode: ([kwargs]) => SubstrateSr25519AddrEncoder(), + type: EllipticCurveTypes.sr25519); + +// Configuration for Moonriver + static final SubstrateCoinConf moonriverEd25519 = + SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.moonriver, + addressEncode: ([kwargs]) => SubstrateEd25519AddrEncoder(), + type: EllipticCurveTypes.ed25519); + static final SubstrateCoinConf moonriverSecp256k1 = + SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.moonriver, + addressEncode: ([kwargs]) => SubstrateSecp256k1AddrEncoder(), + type: EllipticCurveTypes.secp256k1); + static final SubstrateCoinConf moonriverSr25519 = + SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.moonriver, + addressEncode: ([kwargs]) => SubstrateSr25519AddrEncoder(), + type: EllipticCurveTypes.sr25519); + +// Configuration for Phala + static final SubstrateCoinConf phalaEd25519 = SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.phala, + addressEncode: ([kwargs]) => SubstrateEd25519AddrEncoder(), + type: EllipticCurveTypes.ed25519); + static final SubstrateCoinConf phalaSecp256k1 = + SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.phala, + addressEncode: ([kwargs]) => SubstrateSecp256k1AddrEncoder(), + type: EllipticCurveTypes.secp256k1); + static final SubstrateCoinConf phalaSr25519 = SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.phala, + addressEncode: ([kwargs]) => SubstrateSr25519AddrEncoder(), + type: EllipticCurveTypes.sr25519); + +// Configuration for Plasm + static final SubstrateCoinConf plasmEd25519 = SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.plasm, + addressEncode: ([kwargs]) => SubstrateEd25519AddrEncoder(), + type: EllipticCurveTypes.ed25519); + static final SubstrateCoinConf plasmSecp256k1 = + SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.plasm, + addressEncode: ([kwargs]) => SubstrateSecp256k1AddrEncoder(), + type: EllipticCurveTypes.secp256k1); + static final SubstrateCoinConf plasmSr25519 = SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.plasm, + addressEncode: ([kwargs]) => SubstrateSr25519AddrEncoder(), + type: EllipticCurveTypes.sr25519); + +// Configuration for Polkadot + static final SubstrateCoinConf polkadotEd25519 = + SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.polkadot, + addressEncode: ([kwargs]) => SubstrateEd25519AddrEncoder(), + type: EllipticCurveTypes.ed25519); + static final SubstrateCoinConf polkadotSecp256k1 = + SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.polkadot, + addressEncode: ([kwargs]) => SubstrateSecp256k1AddrEncoder(), + type: EllipticCurveTypes.secp256k1); + static final SubstrateCoinConf polkadotSr25519 = + SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.polkadot, + addressEncode: ([kwargs]) => SubstrateSr25519AddrEncoder(), + type: EllipticCurveTypes.sr25519); + +// Configuration for Sora + static final SubstrateCoinConf soraEd25519 = SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.sora, + addressEncode: ([kwargs]) => SubstrateEd25519AddrEncoder(), + type: EllipticCurveTypes.ed25519); + static final SubstrateCoinConf soraSecp256k1 = SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.sora, + addressEncode: ([kwargs]) => SubstrateSecp256k1AddrEncoder(), + type: EllipticCurveTypes.secp256k1); + static final SubstrateCoinConf soraSr25519 = SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.sora, + addressEncode: ([kwargs]) => SubstrateSr25519AddrEncoder(), + type: EllipticCurveTypes.sr25519); + +// Configuration for Stafi + static final SubstrateCoinConf stafiEd25519 = SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.stafi, + addressEncode: ([kwargs]) => SubstrateEd25519AddrEncoder(), + type: EllipticCurveTypes.ed25519); + static final SubstrateCoinConf stafiSecp256k1 = + SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.stafi, + addressEncode: ([kwargs]) => SubstrateSecp256k1AddrEncoder(), + type: EllipticCurveTypes.secp256k1); + static final SubstrateCoinConf stafiSr25519 = SubstrateCoinConf.fromCoinConf( + coinConf: CoinsConf.stafi, + addressEncode: ([kwargs]) => SubstrateSr25519AddrEncoder(), + type: EllipticCurveTypes.sr25519); } diff --git a/lib/bip/substrate/substrate_base.dart b/lib/bip/substrate/core/substrate_base.dart similarity index 71% rename from lib/bip/substrate/substrate_base.dart rename to lib/bip/substrate/core/substrate_base.dart index d17d332..88e7506 100644 --- a/lib/bip/substrate/substrate_base.dart +++ b/lib/bip/substrate/core/substrate_base.dart @@ -1,15 +1,13 @@ import 'package:blockchain_utils/bip/ecc/bip_ecc.dart'; import 'package:blockchain_utils/bip/substrate/conf/substrate_coin_conf.dart'; -import 'package:blockchain_utils/bip/substrate/conf/substrate_conf.dart'; -import 'package:blockchain_utils/bip/substrate/substrate_ex.dart'; -import 'package:blockchain_utils/bip/substrate/substrate_keys.dart'; +import 'package:blockchain_utils/bip/substrate/conf/substrate_coins.dart'; +import 'package:blockchain_utils/bip/substrate/exception/substrate_ex.dart'; +import 'package:blockchain_utils/bip/substrate/keys/substrate_keys.dart'; +import 'package:blockchain_utils/bip/substrate/path/substrate_path.dart'; import 'package:blockchain_utils/crypto/crypto/schnorrkel/keys/keys.dart'; import 'package:blockchain_utils/crypto/quick_crypto.dart'; import 'package:blockchain_utils/exception/exception.dart'; -import 'conf/substrate_coins.dart'; -import 'substrate_path.dart'; - /// Constants used in Substrate-related operations. class SubstrateConst { /// The minimum byte length for a seed in Substrate. @@ -47,22 +45,20 @@ class SubstrateConst { } class _SubstrateUtils { - static List getSecretKey( - List seedBytes, SubstrateKeyAlgorithm curve) { + static List getSecretKey(List seedBytes, EllipticCurveTypes curve) { if (seedBytes.length < SubstrateConst.seedMinByteLen) { throw const ArgumentException( 'Seed length is too small, it shall be at least ${SubstrateConst.seedMinByteLen} bytes', ); } - - if (curve == SubstrateKeyAlgorithm.sr25519) { - final seed = seedBytes.sublist(0, SubstrateConst.seedMinByteLen); + final seed = seedBytes.sublist(0, SubstrateConst.seedMinByteLen); + if (curve == EllipticCurveTypes.sr25519) { final SchnorrkelMiniSecretKey miniSecretKey = SchnorrkelMiniSecretKey.fromBytes(seed); final secretKey = miniSecretKey.toSecretKey(); return secretKey.toBytes(); } - return seedBytes; + return seed; } } @@ -85,49 +81,43 @@ class Substrate { Substrate._(this._priveKey, this.publicKey, this.path, this.coinConf); /// Create a Substrate context from a seed and coin type. - factory Substrate.fromSeed(List seedBytes, SubstrateCoins coinType, - {SubstrateKeyAlgorithm curve = SubstrateKeyAlgorithm.sr25519}) { - final secretKey = _SubstrateUtils.getSecretKey(seedBytes, curve); - final privateKey = SubstratePrvKey.fromBytes( - secretKey, SubstrateConf.getCoin(coinType), curve); - return Substrate._(privateKey, privateKey.publicKey, SubstratePath(), - SubstrateConf.getCoin(coinType)); + factory Substrate.fromSeed(List seedBytes, SubstrateCoins coinType) { + final secretKey = + _SubstrateUtils.getSecretKey(seedBytes, coinType.conf.type); + final privateKey = SubstratePrvKey.fromBytes(secretKey, coinType.conf); + return Substrate._( + privateKey, privateKey.publicKey, SubstratePath(), coinType.conf); } /// Create a Substrate context from a seed, path, and coin type. factory Substrate.fromSeedAndPath( - List seedBytes, dynamic path, SubstrateCoins coinType, - {SubstrateKeyAlgorithm curve = SubstrateKeyAlgorithm.sr25519}) { - final substrateCtx = Substrate.fromSeed(seedBytes, coinType, curve: curve); + List seedBytes, dynamic path, SubstrateCoins coinType) { + final substrateCtx = Substrate.fromSeed(seedBytes, coinType); return substrateCtx.derivePath(path); } /// Create a Substrate context from a private key and coin type. factory Substrate.fromPrivateKey( - List privateKeyBytes, SubstrateCoins coinType, - {SubstrateKeyAlgorithm curve = SubstrateKeyAlgorithm.sr25519}) { - final privateKey = SubstratePrvKey.fromBytes( - privateKeyBytes, SubstrateConf.getCoin(coinType), curve); + List privateKeyBytes, SubstrateCoins coinType) { + final privateKey = + SubstratePrvKey.fromBytes(privateKeyBytes, coinType.conf); return Substrate._( privateKey, privateKey.publicKey, SubstratePath(), - SubstrateConf.getCoin(coinType), + coinType.conf, ); } /// Create a Substrate context from a public key and coin type. - factory Substrate.fromPublicKey(List publicKey, SubstrateCoins coinType, - {SubstrateKeyAlgorithm curve = SubstrateKeyAlgorithm.sr25519}) { + factory Substrate.fromPublicKey( + List publicKey, SubstrateCoins coinType) { return Substrate._( null, - SubstratePubKey.fromBytes( - keyBytes: publicKey, - coinConf: SubstrateConf.getCoin(coinType), - curve: curve), + SubstratePubKey.fromBytes(keyBytes: publicKey, coinConf: coinType.conf), SubstratePath(), - SubstrateConf.getCoin(coinType), + coinType.conf, ); } @@ -188,8 +178,7 @@ class Substrate { } else { result = secret.softDerive(pathElem.chainCode).item1; } - final privateKey = SubstratePrvKey.fromBytes( - result.toBytes(), coinConf, SubstrateKeyAlgorithm.sr25519); + final privateKey = SubstratePrvKey.fromBytes(result.toBytes(), coinConf); return Substrate._( privateKey, privateKey.publicKey, @@ -200,14 +189,14 @@ class Substrate { /// Perform private child key derivation for the current context. Substrate _ckdPriv(SubstratePathElem pathElem) { - if (publicKey.algorithm == SubstrateKeyAlgorithm.sr25519) { + if (publicKey.coinConf.type == EllipticCurveTypes.sr25519) { return _ckdPrivSr25519(pathElem); } - if (!pathElem.isHard) { - throw const SubstrateKeyError( - 'Public child derivation cannot be used to create a hardened child key'); - } - List hdkd = publicKey.algorithm == SubstrateKeyAlgorithm.ed25519 + // if (!pathElem.isHard) { + // throw const SubstrateKeyError( + // 'Public child derivation cannot be used to create a hardened child key'); + // } + List hdkd = publicKey.coinConf.type == EllipticCurveTypes.ed25519 ? SubstrateConst.hdkd : SubstrateConst.secp256k1HDKD; final key = QuickCrypto.blake2b256Hash([ @@ -215,8 +204,7 @@ class Substrate { ..._priveKey!.raw, ...pathElem.chainCode, ]); - final privateKey = - SubstratePrvKey.fromBytes(key, coinConf, publicKey.algorithm); + final privateKey = SubstratePrvKey.fromBytes(key, coinConf); return Substrate._( privateKey, privateKey.publicKey, @@ -232,9 +220,7 @@ class Substrate { return Substrate._( null, SubstratePubKey.fromBytes( - keyBytes: pubKeyBytes.toBytes(), - coinConf: coinConf, - curve: SubstrateKeyAlgorithm.sr25519), + keyBytes: pubKeyBytes.toBytes(), coinConf: coinConf), path.addElem(pathElem), coinConf, ); @@ -246,7 +232,7 @@ class Substrate { throw const SubstrateKeyError( 'Public child derivation cannot be used to create a hardened child key'); } - if (publicKey.algorithm == SubstrateKeyAlgorithm.sr25519) { + if (publicKey.coinConf.type == EllipticCurveTypes.sr25519) { return _ckdPubSr25519(pathElem); } throw SubstrateKeyError( diff --git a/lib/bip/substrate/substrate_ex.dart b/lib/bip/substrate/exception/substrate_ex.dart similarity index 100% rename from lib/bip/substrate/substrate_ex.dart rename to lib/bip/substrate/exception/substrate_ex.dart diff --git a/lib/bip/substrate/keys/substrate_keys.dart b/lib/bip/substrate/keys/substrate_keys.dart new file mode 100644 index 0000000..a650088 --- /dev/null +++ b/lib/bip/substrate/keys/substrate_keys.dart @@ -0,0 +1,79 @@ +import 'package:blockchain_utils/bip/ecc/bip_ecc.dart'; +import 'package:blockchain_utils/bip/substrate/conf/substrate_coin_conf.dart'; +import 'package:blockchain_utils/bip/substrate/exception/substrate_ex.dart'; + +class SubstratePrvKey { + final IPrivateKey privKey; + final SubstrateCoinConf coinConf; + + const SubstratePrvKey._(this.privKey, this.coinConf); + + /// Creates a [Sr25519PrivateKey] from the provided [keyBytes] and [coinConf]. + factory SubstratePrvKey.fromBytes( + List keyBytes, SubstrateCoinConf coinConf) { + return SubstratePrvKey._(_keyFromBytes(keyBytes, coinConf.type), coinConf); + } + + /// Gets the raw representation of the private key as a List. + List get raw { + return privKey.raw; + } + + /// Derives the corresponding Substrate public key from this private key. + SubstratePubKey get publicKey { + return SubstratePubKey._(privKey.publicKey, coinConf); + } + + /// Internal method to create an Sr25519 private key from raw bytes. + static IPrivateKey _keyFromBytes( + List keyBytes, EllipticCurveTypes curve) { + try { + return IPrivateKey.fromBytes(keyBytes, curve); + } catch (e) { + throw const SubstrateKeyError('Invalid private key'); + } + } +} + +class SubstratePubKey { + final IPublicKey pubKey; + + final SubstrateCoinConf coinConf; + + const SubstratePubKey._(this.pubKey, this.coinConf); + + factory SubstratePubKey.fromBytes( + {required List keyBytes, required SubstrateCoinConf coinConf}) { + return SubstratePubKey._(_keyFromBytes(keyBytes, coinConf.type), coinConf); + } + + EllipticCurveTypes get curve => pubKey.curve; + + List get compressed { + return pubKey.compressed; + } + + List get uncompressed { + return pubKey.uncompressed; + } + + String toSS58Address({int? ss58Format}) { + final Map addrParams = { + "ss58_format": ss58Format ?? coinConf.ss58Format + }; + return coinConf.addressEncoder().encodeKey(pubKey.compressed, addrParams); + } + + String get toAddress { + return toSS58Address(); + } + + static IPublicKey _keyFromBytes( + List keyBytes, EllipticCurveTypes curve) { + try { + return IPublicKey.fromBytes(keyBytes, curve); + } catch (e) { + throw const SubstrateKeyError('Invalid public key'); + } + } +} diff --git a/lib/bip/substrate/substrate_path.dart b/lib/bip/substrate/path/substrate_path.dart similarity index 93% rename from lib/bip/substrate/substrate_path.dart rename to lib/bip/substrate/path/substrate_path.dart index 954a194..5720abc 100644 --- a/lib/bip/substrate/substrate_path.dart +++ b/lib/bip/substrate/path/substrate_path.dart @@ -1,7 +1,7 @@ import 'package:blockchain_utils/bip/substrate/scale/substrate_scale_enc_base.dart'; import 'package:blockchain_utils/bip/substrate/scale/substrate_scale_enc_bytes.dart'; import 'package:blockchain_utils/bip/substrate/scale/substrate_scale_enc_uint.dart'; -import 'package:blockchain_utils/bip/substrate/substrate_ex.dart'; +import 'package:blockchain_utils/bip/substrate/exception/substrate_ex.dart'; import 'package:blockchain_utils/crypto/quick_crypto.dart'; /// A constants class that provides predefined values and regular expressions related to Substrate paths @@ -20,14 +20,14 @@ class SubstratePathConst { static const String hardPathPrefix = "//"; /// SCALE encoders for different integer sizes - static Map get scaleIntEncoders => const { - 8: SubstrateScaleU8Encoder(), - 16: SubstrateScaleU16Encoder(), - 32: SubstrateScaleU32Encoder(), - 64: SubstrateScaleU64Encoder(), - 128: SubstrateScaleU128Encoder(), - 256: SubstrateScaleU256Encoder(), - }; + static const Map scaleIntEncoders = { + 8: SubstrateScaleU8Encoder(), + 16: SubstrateScaleU16Encoder(), + 32: SubstrateScaleU32Encoder(), + 64: SubstrateScaleU64Encoder(), + 128: SubstrateScaleU128Encoder(), + 256: SubstrateScaleU256Encoder(), + }; } /// Represents a Substrate path element, which can be either soft or hard, and provides methods for diff --git a/lib/bip/substrate/substrate.dart b/lib/bip/substrate/substrate.dart index 9b4379d..a014dfe 100644 --- a/lib/bip/substrate/substrate.dart +++ b/lib/bip/substrate/substrate.dart @@ -13,10 +13,10 @@ export 'scale/substrate_scale_enc_cuint.dart'; export 'scale/substrate_scale_enc_uint.dart'; /// Export statement for Substrate base components, including key, derivation, address. -export 'substrate_base.dart'; +export 'core/substrate_base.dart'; /// Export statement for Substrate key management components. -export 'substrate_keys.dart'; +export 'keys/substrate_keys.dart'; /// Export statement for Substrate path components. -export 'substrate_path.dart'; +export 'path/substrate_path.dart'; diff --git a/lib/bip/substrate/substrate_keys.dart b/lib/bip/substrate/substrate_keys.dart deleted file mode 100644 index 193e754..0000000 --- a/lib/bip/substrate/substrate_keys.dart +++ /dev/null @@ -1,106 +0,0 @@ -import 'package:blockchain_utils/bip/address/substrate_addr.dart'; -import 'package:blockchain_utils/bip/ecc/bip_ecc.dart'; - -import 'package:blockchain_utils/bip/substrate/conf/substrate_coin_conf.dart'; -import 'package:blockchain_utils/bip/substrate/substrate_ex.dart'; - -class SubstrateKeyAlgorithm { - final EllipticCurveTypes cuve; - const SubstrateKeyAlgorithm._(this.cuve); - static const SubstrateKeyAlgorithm ed25519 = - SubstrateKeyAlgorithm._(EllipticCurveTypes.ed25519); - static const SubstrateKeyAlgorithm sr25519 = - SubstrateKeyAlgorithm._(EllipticCurveTypes.sr25519); - static const SubstrateKeyAlgorithm secp256k1 = - SubstrateKeyAlgorithm._(EllipticCurveTypes.secp256k1); -} - -class SubstratePrvKey { - final IPrivateKey privKey; - final SubstrateKeyAlgorithm algorithm; - final SubstrateCoinConf coinConf; - - const SubstratePrvKey._(this.privKey, this.coinConf, this.algorithm); - - /// Creates a [Sr25519PrivateKey] from the provided [keyBytes] and [coinConf]. - factory SubstratePrvKey.fromBytes(List keyBytes, - SubstrateCoinConf coinConf, SubstrateKeyAlgorithm curve) { - return SubstratePrvKey._(_keyFromBytes(keyBytes, curve), coinConf, curve); - } - - /// Gets the raw representation of the private key as a List. - List get raw { - return privKey.raw; - } - - /// Derives the corresponding Substrate public key from this private key. - SubstratePubKey get publicKey { - return SubstratePubKey._(privKey.publicKey, coinConf, algorithm); - } - - /// Internal method to create an Sr25519 private key from raw bytes. - static IPrivateKey _keyFromBytes( - List keyBytes, SubstrateKeyAlgorithm curve) { - try { - return IPrivateKey.fromBytes(keyBytes, curve.cuve); - } catch (e) { - throw const SubstrateKeyError('Invalid private key'); - } - } -} - -class SubstratePubKey { - final IPublicKey pubKey; - final SubstrateKeyAlgorithm algorithm; - - final SubstrateCoinConf coinConf; - - const SubstratePubKey._(this.pubKey, this.coinConf, this.algorithm); - - factory SubstratePubKey.fromBytes( - {required List keyBytes, - required SubstrateCoinConf coinConf, - required SubstrateKeyAlgorithm curve}) { - return SubstratePubKey._(_keyFromBytes(keyBytes, curve), coinConf, curve); - } - - EllipticCurveTypes get curve => pubKey.curve; - - List get compressed { - return pubKey.compressed; - } - - List get uncompressed { - return pubKey.uncompressed; - } - - String toSS58Address({int? ss58Format}) { - final Map addrParams = { - "ss58_format": ss58Format ?? coinConf.addrParams["ss58_format"]! - }; - switch (pubKey.curve) { - case EllipticCurveTypes.sr25519: - return SubstrateSr25519AddrEncoder() - .encodeKey(pubKey.compressed, addrParams); - case EllipticCurveTypes.ed25519: - return SubstrateEd25519AddrEncoder() - .encodeKey(pubKey.compressed, addrParams); - default: - return SubstrateSecp256k1AddrEncoder() - .encodeKey(pubKey.compressed, addrParams); - } - } - - String get toAddress { - return toSS58Address(); - } - - static IPublicKey _keyFromBytes( - List keyBytes, SubstrateKeyAlgorithm curve) { - try { - return IPublicKey.fromBytes(keyBytes, curve.cuve); - } catch (e) { - throw const SubstrateKeyError('Invalid public key'); - } - } -} diff --git a/lib/signer/substrate/core/signer.dart b/lib/signer/substrate/core/signer.dart index 2d61f04..9d2eba2 100644 --- a/lib/signer/substrate/core/signer.dart +++ b/lib/signer/substrate/core/signer.dart @@ -1,4 +1,6 @@ +import 'package:blockchain_utils/bip/ecc/curve/elliptic_curve_types.dart'; import 'package:blockchain_utils/bip/substrate/substrate.dart'; +import 'package:blockchain_utils/exception/exception.dart'; import 'package:blockchain_utils/signer/substrate/signers/substrate_ecdsa.dart'; import 'package:blockchain_utils/signer/substrate/signers/substrate_eddsa.dart'; import 'package:blockchain_utils/signer/substrate/signers/substrate_sr25519.dart'; @@ -8,21 +10,24 @@ abstract class BaseSubstrateSigner { List vrfSign(List message, {List? context, List? extra}); factory BaseSubstrateSigner.fromBytes( - List keyBytes, SubstrateKeyAlgorithm algorithm) { + List keyBytes, EllipticCurveTypes algorithm) { switch (algorithm) { - case SubstrateKeyAlgorithm.ed25519: + case EllipticCurveTypes.ed25519: return SubstrateED25519Signer.fromKeyBytes(keyBytes); - case SubstrateKeyAlgorithm.secp256k1: + case EllipticCurveTypes.secp256k1: return SubstrateEcdsaSigner.fromKeyBytes(keyBytes); - default: + case EllipticCurveTypes.sr25519: return SubstrateSr25519Signer.fromKeyBytes(keyBytes); + default: + throw ArgumentException( + "Invalid substrate signing key algorithm. Excepted: ed25519, secp256k1, or sr25519. Got: ${algorithm.name}"); } } factory BaseSubstrateSigner.fromSubstrate(Substrate substrate) { - switch (substrate.publicKey.algorithm) { - case SubstrateKeyAlgorithm.ed25519: + switch (substrate.coinConf.type) { + case EllipticCurveTypes.ed25519: return SubstrateED25519Signer.fromKeyBytes(substrate.priveKey.raw); - case SubstrateKeyAlgorithm.secp256k1: + case EllipticCurveTypes.secp256k1: return SubstrateEcdsaSigner.fromKeyBytes(substrate.priveKey.raw); default: return SubstrateSr25519Signer.fromKeyBytes(substrate.priveKey.raw); diff --git a/lib/signer/substrate/core/verifier.dart b/lib/signer/substrate/core/verifier.dart index b1f5306..5bd17f0 100644 --- a/lib/signer/substrate/core/verifier.dart +++ b/lib/signer/substrate/core/verifier.dart @@ -1,4 +1,6 @@ +import 'package:blockchain_utils/bip/ecc/curve/elliptic_curve_types.dart'; import 'package:blockchain_utils/bip/substrate/substrate.dart'; +import 'package:blockchain_utils/exception/exception.dart'; import 'package:blockchain_utils/signer/substrate/signers/substrate_ecdsa.dart'; import 'package:blockchain_utils/signer/substrate/signers/substrate_eddsa.dart'; import 'package:blockchain_utils/signer/substrate/signers/substrate_sr25519.dart'; @@ -9,27 +11,27 @@ abstract class BaseSubstrateVerifier { {List? context, List? extra}); factory BaseSubstrateVerifier.fromBytes( - List keyBytes, SubstrateKeyAlgorithm algorithm) { + List keyBytes, EllipticCurveTypes algorithm) { switch (algorithm) { - case SubstrateKeyAlgorithm.ed25519: + case EllipticCurveTypes.ed25519: return SubstrateED25519Verifier.fromKeyBytes(keyBytes); - case SubstrateKeyAlgorithm.secp256k1: + case EllipticCurveTypes.secp256k1: return SubstrateEcdsaVerifier.fromKeyBytes(keyBytes); - default: + case EllipticCurveTypes.sr25519: return SubstrateSr25519Verifier.fromKeyBytes(keyBytes); + default: + throw ArgumentException( + "Invalid substrate signing key algorithm. Excepted: ed25519, secp256k1, or sr25519. Got: ${algorithm.name}"); } } factory BaseSubstrateVerifier.fromSubstrate(Substrate substrate) { - switch (substrate.publicKey.algorithm) { - case SubstrateKeyAlgorithm.ed25519: - return SubstrateED25519Verifier.fromKeyBytes( - substrate.publicKey.compressed); - case SubstrateKeyAlgorithm.secp256k1: - return SubstrateEcdsaVerifier.fromKeyBytes( - substrate.publicKey.compressed); + switch (substrate.coinConf.type) { + case EllipticCurveTypes.ed25519: + return SubstrateED25519Verifier.fromKeyBytes(substrate.priveKey.raw); + case EllipticCurveTypes.secp256k1: + return SubstrateEcdsaVerifier.fromKeyBytes(substrate.priveKey.raw); default: - return SubstrateSr25519Verifier.fromKeyBytes( - substrate.publicKey.compressed); + return SubstrateSr25519Verifier.fromKeyBytes(substrate.priveKey.raw); } } } diff --git a/lib/signer/substrate/substrate.dart b/lib/signer/substrate/substrate.dart index beab244..2d925a8 100644 --- a/lib/signer/substrate/substrate.dart +++ b/lib/signer/substrate/substrate.dart @@ -1,5 +1,5 @@ -import 'package:blockchain_utils/bip/substrate/substrate_base.dart'; -import 'package:blockchain_utils/bip/substrate/substrate_keys.dart'; +import 'package:blockchain_utils/bip/ecc/curve/elliptic_curve_types.dart'; +import 'package:blockchain_utils/bip/substrate/core/substrate_base.dart'; import 'package:blockchain_utils/signer/substrate/core/signer.dart'; import 'package:blockchain_utils/signer/substrate/core/verifier.dart'; import 'package:blockchain_utils/utils/utils.dart'; @@ -9,7 +9,7 @@ class SubstrateSigner { const SubstrateSigner._(this._signer); factory SubstrateSigner.fromBytes( - List keyBytes, SubstrateKeyAlgorithm algorithm) { + List keyBytes, EllipticCurveTypes algorithm) { return SubstrateSigner._( BaseSubstrateSigner.fromBytes(keyBytes, algorithm)); } @@ -35,7 +35,7 @@ class SubstrateVerifier { const SubstrateVerifier._(this._verifier); factory SubstrateVerifier.fromBytes( - List keyBytes, SubstrateKeyAlgorithm algorithm) { + List keyBytes, EllipticCurveTypes algorithm) { return SubstrateVerifier._( BaseSubstrateVerifier.fromBytes(keyBytes, algorithm)); } diff --git a/lib/utils/string/string.dart b/lib/utils/string/string.dart index b637d6e..ae7ac91 100644 --- a/lib/utils/string/string.dart +++ b/lib/utils/string/string.dart @@ -1,4 +1,5 @@ import 'dart:convert'; +import 'package:blockchain_utils/exception/exceptions.dart'; import 'package:blockchain_utils/utils/binary/utils.dart'; /// An enumeration representing different string encoding options. @@ -141,7 +142,12 @@ class StringUtils { /// /// The input [data] is a JSON-encoded string. /// Returns a Map representing the Dart object. - static dynamic toJson(String data) { + static T toJson(String data) { + final decode = jsonDecode(data); + if (decode is! T) { + throw ArgumentException( + "Invalid json casting. excepted: $T got: ${decode.runtimeType}"); + } return jsonDecode(data); } @@ -160,7 +166,7 @@ class StringUtils { /// /// The input [data] is a JSON-encoded string. /// Returns a Map representing the Dart object. - static Object? tryToJson(String data) { + static T? tryToJson(String data) { try { return toJson(data); } catch (e) { diff --git a/pubspec.yaml b/pubspec.yaml index 7a1d4ef..8542f2f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: blockchain_utils description: Comprehensive Crypto & Blockchain Toolkit, Pure Dart, Cross-Platform, Encoding, Cryptography, Addresses, Mnemonics, & More. -version: 3.2.0 +version: 3.3.0 homepage: "https://github.com/mrtnetwork/blockchain_utils" repository: "https://github.com/mrtnetwork/blockchain_utils" Author: mrhaydari.t@gmail.com diff --git a/test/substrate/substrate_test.dart b/test/substrate/substrate_test.dart index f759249..528153b 100644 --- a/test/substrate/substrate_test.dart +++ b/test/substrate/substrate_test.dart @@ -1,7 +1,7 @@ import 'package:blockchain_utils/bip/ecc/keys/sr25519_keys.dart'; import 'package:blockchain_utils/bip/substrate/conf/substrate_coins.dart'; -import 'package:blockchain_utils/bip/substrate/substrate_base.dart'; -import 'package:blockchain_utils/bip/substrate/substrate_path.dart'; +import 'package:blockchain_utils/bip/substrate/core/substrate_base.dart'; +import 'package:blockchain_utils/bip/substrate/path/substrate_path.dart'; import 'package:blockchain_utils/utils/utils.dart'; import 'package:test/test.dart'; @@ -15,7 +15,7 @@ void main() { final seed = BytesUtils.fromHexString(i["private_key"]); final coin = SubstrateCoins.values.firstWhere((element) => element.name.toLowerCase() == - (i["coin"] as String).toLowerCase().replaceAll("_", "")); + "${(i["coin"] as String).toLowerCase().replaceAll("_", "")}sr25519"); Substrate w = Substrate.fromPrivateKey(seed, coin); expect(w.publicKey.compressed.toHex(), i["public_key"]); expect(w.priveKey.raw.toHex(), i["private_key"]); @@ -48,7 +48,7 @@ void main() { final seed = BytesUtils.fromHexString(i["seed"]); final coin = SubstrateCoins.values.firstWhere((element) => element.name.toLowerCase() == - (i["coin"] as String).toLowerCase().replaceAll("_", "")); + "${(i["coin"] as String).toLowerCase().replaceAll("_", "")}sr25519"); Substrate w = Substrate.fromSeed(seed, coin); expect(w.publicKey.compressed.toHex(), i["public_key"]); expect(w.priveKey.raw.toHex(), i["private_key"]);