alan-sdk / alan.dart

Compare 68d9285 ... +91 ... 4c4dde7

Showing 1 of 300 files from the diff.
Other files ignored by Codecov
doc/README.md has changed.
.gitignore has changed.
CHANGELOG.md has changed.
Makefile is new.
CNAME was deleted.
doc/x/auth.md was deleted.
doc/x/bank.md was deleted.
lib/alan.dart has changed.
doc/x/staking.md was deleted.
README.md has changed.
doc/x/gov.md was deleted.
build.yaml was deleted.

@@ -1,78 +1,95 @@
Loading
1 1
import 'package:alan/alan.dart';
2 -
import 'package:alan/x/gov/types/messages/export.dart';
3 -
import 'package:reflectable/mirrors.dart';
2 +
import 'package:protobuf/protobuf.dart';
4 3
5 -
/// Represents the codec that is used to serialize [StdMsg] instances
6 -
/// properly during transactions signature processes.
7 -
class Codec {
8 -
  static const _TYPE_KEY = 'type';
9 -
  static const _VALUE_KEY = 'value';
10 -
11 -
  static bool _defaultInitialized = false;
12 -
  static final Map<Type, String> _msgTypes = {};
4 +
/// Represents a function that can read an [Any] as a particular
5 +
/// [AccountI] implementation.
6 +
typedef AccountDeserializer = AccountI Function(Any any);
13 7
14 -
  /// Registers the defaults message types.
15 -
  static void _init() {
16 -
    // x/bank
17 -
    registerType('cosmos-sdk/MsgSend', MsgSend);
8 +
/// Contains the data to properly register an [AccountI] implementation
9 +
/// so that it can be later deserialized starting from an [Any] by the [Codec].
10 +
class AccountImpl {
11 +
  String typeUrl;
12 +
  AccountDeserializer deserializer;
18 13
19 -
    // x/staking
20 -
    registerType('cosmos-sdk/MsgCreateValidator', MsgCreateValidator);
21 -
    registerType('cosmos-sdk/MsgEditValidator', MsgEditValidator);
22 -
    registerType('cosmos-sdk/MsgDelegate', MsgDelegate);
23 -
    registerType('cosmos-sdk/MsgUndelegate', MsgUndelegate);
24 -
    registerType('cosmos-sdk/MsgBeginRedelegate', MsgBeginRedelegate);
25 -
26 -
    // x/gov
27 -
    registerType('cosmos-sdk/MsgSubmitProposal', MsgSubmitProposal);
28 -
    registerType('cosmos-sdk/MsgDeposit', MsgDeposit);
29 -
    registerType('cosmos-sdk/MsgVote', MsgVote);
30 -
    registerType('cosmos-sdk/TextProposal', TextProposal);
14 +
  AccountImpl(String typeUrl, AccountDeserializer deserializer)
15 +
      : typeUrl = typeUrl,
16 +
        deserializer = deserializer;
17 +
}
31 18
32 -
    _defaultInitialized = true;
19 +
/// Represents the codec that is used to serialize [StdMsg] instances
20 +
/// properly during transactions signature processes.
21 +
class Codec {
22 +
  /// Serializes the given [value] into a [Map].
23 +
  static Any serialize(GeneratedMessage value) {
24 +
    return Any.pack(value, typeUrlPrefix: '');
33 25
  }
34 26
35 -
  /// Checks that this codec is properly initialized.
36 -
  static void _checkInit() {
37 -
    if (!_defaultInitialized) {
38 -
      _init();
39 -
      _defaultInitialized = true;
27 +
  static bool _initialized = false;
28 +
  static final List<AccountImpl> _accountImpls = [];
29 +
30 +
  static void _ensureInit() {
31 +
    if (_initialized) {
32 +
      return;
40 33
    }
41 -
  }
42 34
43 -
  /// Registers the given [msgType] associating it to the specified
44 -
  /// [typeString].
45 -
  static void registerType(String typeString, Type msgType) {
46 -
    _msgTypes[msgType] = typeString;
35 +
    _accountImpls.add(AccountImpl(
36 +
      'BaseAccount',
37 +
      BaseAccount.fromAny,
38 +
    ));
39 +
    _accountImpls.add(AccountImpl(
40 +
      'ModuleAccount',
41 +
      ModuleAccount.fromAny,
42 +
    ));
43 +
    _accountImpls.add(AccountImpl(
44 +
      'BaseVestingAccount',
45 +
      BaseVestingAccount.fromAny,
46 +
    ));
47 +
    _accountImpls.add(AccountImpl(
48 +
      'DelayedVestingAccount',
49 +
      DelayedVestingAccount.fromAny,
50 +
    ));
51 +
    _accountImpls.add(AccountImpl(
52 +
      'ContinuousVestingAccount',
53 +
      ContinuousVestingAccount.fromAny,
54 +
    ));
55 +
    _accountImpls.add(AccountImpl(
56 +
      'PeriodicVestingAccount',
57 +
      PeriodicVestingAccount.fromAny,
58 +
    ));
59 +
60 +
    _initialized = true;
47 61
  }
48 62
49 -
  /// Serializes the given [value] into a [Map].
50 -
  static Map<String, dynamic> serialize(Serializable value) {
51 -
    _checkInit();
63 +
  /// Registers the given [impl] as an [AccountI] implementation.
64 +
  static void registerAccountImpl(AccountImpl impl) {
65 +
    _ensureInit();
52 66
53 -
    final type = _msgTypes[value.runtimeType];
54 -
    if (type == null) {
55 -
      throw Exception('${value.runtimeType} is not registered');
67 +
    if (_accountImpls.any((element) => element.typeUrl == impl.typeUrl)) {
68 +
      throw Exception(
69 +
        '${impl.typeUrl} has an implementation already registered',
70 +
      );
56 71
    }
57 72
58 -
    return {_TYPE_KEY: type, _VALUE_KEY: value.asJson()};
73 +
    _accountImpls.add(impl);
59 74
  }
60 75
61 -
  static Serializable deserialize(Map<String, dynamic> json) {
62 -
    _checkInit();
76 +
  /// Deserializes the provided [value] into an [AccountI] instance.
77 +
  static AccountI deserializeAccount(Any value) {
78 +
    _ensureInit();
63 79
64 -
    final typeKey = json[_TYPE_KEY];
65 -
    final type = _msgTypes.keys.firstWhere(
66 -
      (k) => _msgTypes[k] == typeKey,
67 -
      orElse: () => null,
68 -
    );
80 +
    AccountImpl? impl;
69 81
70 -
    if (type == null) {
71 -
      throw Exception('$typeKey is not registered');
82 +
    try {
83 +
      impl = _accountImpls.singleWhere(
84 +
        (element) => value.typeUrl.contains(element.typeUrl),
85 +
      );
86 +
    } catch (e) {
87 +
      throw Exception(
88 +
        'Account of type ${value.typeUrl} cannot be deserialized properly. '
89 +
        'Please register this type using Codec.registerAccountImpl',
90 +
      );
72 91
    }
73 92
74 -
    final classMirror = reflector.reflectType(type) as ClassMirror;
75 -
    return classMirror.newInstance('fromJson', [json[_VALUE_KEY]])
76 -
        as Serializable;
93 +
    return impl.deserializer(value);
77 94
  }
78 95
}

Learn more Showing 74 files with coverage changes found.

Changes in lib/wallet/network_info.dart
+25
+27
Loading file...
Changes in lib/x/node/querier.dart
+2
Loading file...
Changes in lib/queries/query_helper.dart
-1
+1
Loading file...
Changes in lib/wallet/wallet.dart
-10
+5
Loading file...
Changes in lib/x/auth/querier.dart
-4
Loading file...
lib/transactions/sign/map_sorter.dart
Loading file...
lib/x/staking/types/validator_description.dart
Loading file...
lib/x/gov/querier.dart
Loading file...
lib/x/gov/types/messages/msg_vote.dart
Loading file...
lib/x/staking/types/delegation.dart
Loading file...
lib/x/staking/types/messages/msg_create_validator.dart
Loading file...
lib/types/rest/block_response.dart
Loading file...
lib/transactions/sign/tx_signer.dart
Loading file...
lib/x/gov/types/messages/msg_submit_proposal.dart
Loading file...
lib/x/auth/types/account_response.dart
Loading file...
lib/x/gov/types/text_proposal.dart
Loading file...
lib/x/staking/types/redelegation.dart
Loading file...
lib/types/tx/signing/std_signature_message.dart
Loading file...
lib/wallet/msg_signer.dart
Loading file...
lib/types/transaction_result.dart
Loading file...
lib/x/bank/types/messages/msg_send.dart
Loading file...
lib/x/staking/types/messages/msg_undelegate.dart
Loading file...
lib/x/gov/types/proposal_content.dart
Loading file...
lib/types/rest/node_info_response.dart
Loading file...
lib/x/gov/types/proposer.dart
Loading file...
lib/x/staking/querier.dart
Loading file...
lib/transactions/send/send_mode.dart
Loading file...
lib/types/rest/lcd_response.dart
Loading file...
lib/x/staking/types/messages/msg_delegate.dart
Loading file...
lib/x/txs/querier.dart
Loading file...
lib/x/gov/types/params.dart
Loading file...
lib/types/std_fee.dart
Loading file...
lib/x/gov/types/deposit.dart
Loading file...
lib/types/std_coin.dart
Loading file...
lib/types/tx/std_tx.dart
Loading file...
lib/x/staking/types/commission.dart
Loading file...
lib/x/gov/types/messages/msg_deposit.dart
Loading file...
lib/types/tx/signing/std_signature.dart
Loading file...
lib/x/staking/types/messages/msg_edit_validator.dart
Loading file...
lib/types/rest/transactions_response.dart
Loading file...
lib/x/staking/types/params.dart
Loading file...
lib/types/serializable.dart
Loading file...
lib/x/auth/types/account.dart
Loading file...
lib/x/staking/types/messages/msg_begin_redelegate.dart
Loading file...
lib/types/rest/genesis_response.dart
Loading file...
lib/x/gov/types/vote.dart
Loading file...
lib/x/staking/types/validator.dart
Loading file...
lib/x/staking/types/unbonding_delegation.dart
Loading file...
lib/types/tx/std_public_key.dart
Loading file...
lib/x/staking/types/pool.dart
Loading file...
lib/x/gov/types/proposal.dart
Loading file...
lib/types/rest/tx_response.dart
Loading file...
lib/transactions/create/tx_builder.dart
Loading file...
lib/transactions/send/tx_sender.dart
Loading file...
lib/x/gov/types/tally_result.dart
Loading file...
New file lib/utils/bip_39.dart
New
Loading file...
New file lib/transactions/sender/tx_sender.dart
New
Loading file...
New file lib/types/utils_bytearray.dart
New
Loading file...
New file lib/types/ext_txresponse.dart
New
Loading file...
New file lib/transactions/sign_mode_handler/direct.dart
New
Loading file...
New file lib/types/ext_coins.dart
New
Loading file...
New file lib/x/auth/account.dart
New
Loading file...
New file lib/transactions/signer/tx_signer.dart
New
Loading file...
New file lib/types/ext_uint8list.dart
New
Loading file...
New file lib/transactions/config/default.dart
New
Loading file...
New file lib/transactions/encoder/default.dart
New
Loading file...
New file lib/types/ext_bigint.dart
New
Loading file...
New file lib/x/vesting/vesting_account.dart
New
Loading file...
New file lib/transactions/builder/tx_builder.dart
New
Loading file...
New file lib/queries/headers.dart
New
Loading file...
New file lib/utils/big_int_big_endian.dart
New
Loading file...
New file lib/types/ext_int.dart
New
Loading file...
New file lib/utils/bip_32.dart
New
Loading file...
New file lib/types/cosmos_signature.dart
New
Loading file...

93 Commits

Hiding 5 contexual commits
-2
+2
Hiding 3 contexual commits
+2
-2
Hiding 15 contexual commits
-1 Files
-16
-26
+10
-2
+2
Hiding 2 contexual commits
+16
+6
+10
+2
-2
Hiding 34 contexual commits
-2
+2
+2
-2
-2
+2
+2
-2
Hiding 1 contexual commits
-2
+2
+44
+27
+17
+2
-2
Hiding 1 contexual commits
+4 Files
+166
+108
+58
Hiding 1 contexual commits
-34 Files
-741
-687
-54
Files Coverage
lib -13.91% 76.39%
Project Totals (26 files) 76.39%
Loading