Skip to content

Commit

Permalink
Refactor Coins.cpp, coin data as a const map (#900)
Browse files Browse the repository at this point in the history
* Refactor Coins.cpp, coin data as a const map.
* DerivationPath: add range check.
* Fill complete coin maps.
* All accessors work from the data map.
* Move fix parts from generated Coins.cpp to non-genersted Coin.cpp.
* Rename Coins.cpp.
* Codegen fix.
Co-authored-by: Catenocrypt <[email protected]>
  • Loading branch information
optout21 authored Mar 24, 2020
1 parent dba30d4 commit fcd1f04
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 236 deletions.
2 changes: 1 addition & 1 deletion codegen/bin/coins
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ coins = JSON.parse(json_string).sort_by { |x| x['name'] }
coins_md = coins.sort_by { |x| Integer(coin_type(x['derivationPath'])) }

erbs = [
{'template' => 'coins.cpp.erb', 'folder' => 'src/Generated', 'file' => 'Coins.cpp'},
{'template' => 'CoinInfoData.cpp.erb', 'folder' => 'src/Generated', 'file' => 'CoinInfoData.cpp'},
{'template' => 'coins.md.erb', 'folder' => 'docs', 'file' => 'coins.md'},
{'template' => 'hrp.cpp.erb', 'folder' => 'src/Generated', 'file' => 'TWHRP.cpp'},
{'template' => 'hrp.h.erb', 'folder' => 'include/TrustWalletCore', 'file' => 'TWHRP.h'}
Expand Down
80 changes: 80 additions & 0 deletions codegen/lib/templates/CoinInfoData.cpp.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright © 2017-2020 Trust Wallet.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.
//
// This is a GENERATED FILE, changes made here WILL BE LOST.
//

#include "Coin.h"
#include <TrustWalletCore/TWCoinTypeConfiguration.h>

#include <map>

using namespace TW;

/// Map with info about known coins
static std::map<TWCoinType, CoinInfo> coins;

static const CoinInfo defaultsForMissing = {
"?",
"?",
TWBlockchainBitcoin,
TWPurposeBIP44,
TWCurveED25519,
TWHDVersionNone,
TWHDVersionNone,
DerivationPath(),
TWPublicKeyTypeSECP256k1,
0,
0,
0,
TWHRPUnknown,
Hash::sha256ripemd,
Hash::sha256d,
"?",
2,
"",
"",
};

void fillMap(); // forward

/// Get coin from map, if missing returns defaults (not to have contains-check in each accessor method)
const CoinInfo& getCoinInfo(TWCoinType coin) {
if (coins.size() == 0) { fillMap(); }
if (coins.find(coin) == coins.end()) { return defaultsForMissing; }
return coins.at(coin);
}

/// Fill the map. Simple static initializer did not work out due to nondeterministic static initialization order
void fillMap() {
coins = std::map<TWCoinType, CoinInfo> {
<% coins.each do |coin| -%>
{
TWCoinType<%= format_name(coin['name']) %>, {
"<%= coin['id'] %>",
<% if coin['displayName'].nil? -%>"<%= coin['name'] %>"<% else -%>"<%= coin['displayName'] %>"<% end -%>,
TWBlockchain<%= format_name(coin['blockchain']) %>,
TWPurposeBIP<%= /^m\/(\d+)'?(\/\d+'?)+$/.match(coin['derivationPath'])[1] %>,
TWCurve<%= format_name(coin['curve']) %>,
TWHDVersion<% if coin['xpub'].nil? -%>None<% else -%><%= format_name(coin['xpub']) %><% end -%>,
TWHDVersion<% if coin['xprv'].nil? -%>None<% else -%><%= format_name(coin['xprv']) %><% end -%>,
DerivationPath("<%= coin['derivationPath'] %>"),
TWPublicKeyType<%= format_name(coin['publicKeyType']) %>,
<% if coin['staticPrefix'].nil? -%>0<% else -%><%= coin['staticPrefix'] %><% end -%>,
<% if coin['p2pkhPrefix'].nil? -%>0<% else -%><%= coin['p2pkhPrefix'] %><% end -%>,
<% if coin['p2shPrefix'].nil? -%>0<% else -%><%= coin['p2shPrefix'] %><% end -%>,
TWHRP<% if coin['hrp'].nil? -%>Unknown<% else -%><%= format_name(coin['name']) %><% end -%>,
Hash::<% if coin['publicKeyHasher'].nil? -%>sha256ripemd<% else -%><%= coin['publicKeyHasher'] %><% end -%>,
Hash::<% if coin['base58Hasher'].nil? -%>sha256d<% else -%><%= coin['base58Hasher'] %><% end -%>,
"<%= coin['symbol'] %>",
<%= coin['decimals'] %>,
"<%= explorer_tx_url(coin) %>",
"<%= explorer_account_url(coin) %>",
}
},
<% end -%>
};
}
225 changes: 0 additions & 225 deletions codegen/lib/templates/coins.cpp.erb

This file was deleted.

Loading

0 comments on commit fcd1f04

Please sign in to comment.