Skip to content
This repository has been archived by the owner on Jun 17, 2020. It is now read-only.

Redesign asset listing and validation infrastructure #32

Merged
merged 106 commits into from
Apr 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
106 commits
Select commit Hold shift + click to select a range
ba4ac0f
Move altcoin address validators to separate classes.
Mar 23, 2018
856669a
AssetProvider
Mar 23, 2018
cb6c579
AssetProviderRegistry
Mar 23, 2018
c949a24
Adjust Bch and Ella asset providers names.
Mar 23, 2018
695053d
Organize imports
cbeams Mar 26, 2018
cab46e2
Redesign asset listing and validation infrastructure
cbeams Mar 26, 2018
9026200
Extract BSQ
cbeams Mar 27, 2018
9fbed62
Extract Litecoin
cbeams Mar 27, 2018
c2c7e40
Extract Dogecoin
cbeams Mar 27, 2018
bd37996
Extract Dash
cbeams Mar 27, 2018
f07e13e
Remove unused imports
cbeams Mar 27, 2018
a08ac11
Extract PIVX
cbeams Mar 27, 2018
22cd1b4
Extract Internet of People
cbeams Mar 27, 2018
df0931e
Add missing license headers
cbeams Mar 27, 2018
981ca38
Extract Octocoin
cbeams Mar 27, 2018
49b119d
Extract Zcash
Mar 28, 2018
db51bf6
Extract Byteball
Mar 28, 2018
b516ce3
Extract Nxt
Mar 28, 2018
055acf2
Extract Decent
Mar 28, 2018
cfb2484
Extract Pranacoin
Mar 28, 2018
44fa4b2
Extract Wacoin
Mar 28, 2018
267261b
Extract ZenCash
Mar 28, 2018
956a5c6
Extract Cryptonite
Mar 28, 2018
f43d9fa
Extract Terracoin
Mar 28, 2018
0f5c3aa
Extract Internext
Mar 28, 2018
da62f3a
Extract Particl
Mar 28, 2018
8e67b8b
Extract Madcoin
Mar 28, 2018
64c947a
Extract Bitcoin Clashic
Mar 28, 2018
15d9376
Extract Bitcoin Gold
Mar 28, 2018
0f3bfbb
Extract Cagecoin
Mar 28, 2018
7d23f15
Extract Verify
Mar 28, 2018
56ec783
Extract Spectrecoin
Mar 28, 2018
c263cf5
Extract Wild Token
Mar 28, 2018
a33fe9d
Extract DeepOnion
Mar 28, 2018
2ee9f13
Extract Creativecoin
Mar 28, 2018
c11df7b
Extract Infinity Economics
Mar 28, 2018
159e975
Extract Better Betting
Mar 28, 2018
648c2e4
Extract Movement
Mar 28, 2018
5647b74
Extract RefToken
Mar 28, 2018
f79e055
Extract Stellite
Mar 28, 2018
f1d84c0
Extract Dai Stablecoin
Mar 28, 2018
e750c3f
Extract Yenten
Mar 28, 2018
ddc77e7
Extract BitDaric
Mar 28, 2018
4f3f151
Extract Obsidian
Mar 28, 2018
a0f5b0a
Extract Cassubian Detk
Mar 28, 2018
034d62a
Extract DigiMoney
Mar 28, 2018
0ad058c
Extract SpeedCash
Mar 28, 2018
335496b
Extract SOS Coin
Mar 28, 2018
c89fe8a
Extract AchieveCoin
Mar 28, 2018
934dbf6
Extract vDinar
Mar 29, 2018
731845e
Extract Anglecoin
Mar 29, 2018
aa22f58
Extract Dinero
Mar 29, 2018
1295c8d
Extract Strayacoin
Mar 29, 2018
560c588
Extract ROIcoin
Mar 29, 2018
98893f7
Extract WorldMobileCoin
Mar 29, 2018
80375e0
Extract Arto
Mar 29, 2018
0fc1a38
Extract Koto
Mar 29, 2018
97584a7
Extract Ubiq
Mar 29, 2018
686d134
Extract Qwark
Mar 29, 2018
3c1032a
Extract GeoCoin
Mar 29, 2018
d2ee3ec
Extract 10grans
Mar 29, 2018
03ed04e
Extract Phore
Mar 29, 2018
81f403d
Extract Octocoin
Mar 29, 2018
f0d0ab5
Extract Burstcoin
Mar 29, 2018
ecd6632
Extract Counterparty
Mar 29, 2018
af23a18
Extract DarkNet
Mar 29, 2018
ee2b77b
Extract Decred
Mar 29, 2018
53931a7
Extract DynamicCoin
Mar 29, 2018
1433c45
Extract Espers
Mar 29, 2018
b100c10
Extract EtherClassic
Mar 29, 2018
a7e2fdc
Extract Gridcoin
Mar 29, 2018
04c2e35
Extract LBRY
Mar 29, 2018
d84bf70
Extract Lisk
Mar 29, 2018
eba4552
Extract MaidSafeCoin
Mar 29, 2018
d114c58
Extract Monero
Mar 29, 2018
90e8043
Extract MyceliumToken
Mar 29, 2018
c22021e
Extract NavCoin
Mar 29, 2018
ca8d348
Extract Namecoin
Mar 29, 2018
0cecac6
Extract NuBits
Mar 29, 2018
9b70137
Extract PascalCoin
Mar 29, 2018
0749774
Extract PepeCash
Mar 29, 2018
7998747
Extract PostCoin
Mar 29, 2018
375a2f0
Extract ReddCoin
Mar 29, 2018
fa1e3b3
Extract SafeFileSystemCoin
Mar 29, 2018
3bae6ec
Extract Siacoin
Mar 29, 2018
239ce5a
Extract Siafund
Mar 29, 2018
66f7b51
Extract Sibcoin
Mar 29, 2018
e887223
Extract STEEM
Mar 29, 2018
1ca2535
Extract Unobtanium
Mar 29, 2018
5b061e2
Extract Zcoin
Mar 29, 2018
393e090
Fuse sorting into stream for CurrencyUtil.
Mar 29, 2018
f49dc8d
Extract Ellaism
Mar 29, 2018
0eae155
Add missing license headers
cbeams Apr 3, 2018
f8796f7
Remove blank lines between closing braces
cbeams Apr 3, 2018
0551d22
Add blank lines where appropriate
cbeams Apr 3, 2018
c1d5aac
Wrap code at 120 characters
cbeams Apr 3, 2018
b73fa60
Remove unnecessary else(if) clauses after a return
cbeams Apr 3, 2018
7ad70c3
Simplify BitcoinClashic
cbeams Apr 3, 2018
cc5edbe
Improve AbstractAssetTest#testPresenceInAssetRegistry
cbeams Apr 3, 2018
9c7d19e
Sort META-INF/services/bisq.asset.Asset
cbeams Apr 3, 2018
e47d59f
Polish Byteball
cbeams Apr 3, 2018
1d040a2
Polish CurrencyUtil
cbeams Apr 3, 2018
9dc5b8d
Polish AltCoinAddressValidator
cbeams Apr 3, 2018
d7537ae
Polish AddressValidationResult
cbeams Apr 3, 2018
990070f
Add Javadoc for bisq.asset types
cbeams Apr 3, 2018
2f231d3
Document META-INF/services/bisq.asset.Asset
cbeams Apr 3, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions src/main/java/bisq/asset/AbstractAsset.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.asset;

import static org.apache.commons.lang3.Validate.notBlank;
import static org.apache.commons.lang3.Validate.notNull;

/**
* Abstract base class for {@link Asset} implementations. Most implementations should not
* extend this class directly, but should rather extend {@link Coin}, {@link Token} or one
* of their subtypes.
*
* @author Chris Beams
* @since 0.7.0
*/
public abstract class AbstractAsset implements Asset {

private final String name;
private final String tickerSymbol;
private final AddressValidator addressValidator;

public AbstractAsset(String name, String tickerSymbol, AddressValidator addressValidator) {
this.name = notBlank(name);
this.tickerSymbol = notBlank(tickerSymbol);
this.addressValidator = notNull(addressValidator);
}

@Override
public final String getName() {
return name;
}

@Override
public final String getTickerSymbol() {
return tickerSymbol;
}

@Override
public final AddressValidationResult validateAddress(String address) {
return addressValidator.validate(address);
}

@Override
public String toString() {
return getClass().getName();
}
}
73 changes: 73 additions & 0 deletions src/main/java/bisq/asset/AddressValidationResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.asset;

/**
* Value object representing the result of validating an {@link Asset} address. Various
* factory methods are provided for typical use cases.
*
* @author Chris Beams
* @since 0.7.0
* @see Asset#validateAddress(String)
*/
public class AddressValidationResult {

private static AddressValidationResult VALID_ADDRESS = new AddressValidationResult(true, "", "");

private final boolean isValid;
private final String message;
private final String i18nKey;

private AddressValidationResult(boolean isValid, String message, String i18nKey) {
this.isValid = isValid;
this.message = message;
this.i18nKey = i18nKey;
}

public boolean isValid() {
return isValid;
}

public String getI18nKey() {
return i18nKey;
}

public String getMessage() {
return message;
}

public static AddressValidationResult validAddress() {
return VALID_ADDRESS;
}

public static AddressValidationResult invalidAddress(Throwable cause) {
return invalidAddress(cause.getMessage());
}

public static AddressValidationResult invalidAddress(String cause) {
return invalidAddress(cause, "validation.altcoin.invalidAddress");
}

public static AddressValidationResult invalidAddress(String cause, String i18nKey) {
return new AddressValidationResult(false, cause, i18nKey);
}

public static AddressValidationResult invalidStructure() {
return invalidAddress("", "validation.altcoin.wrongStructure");
}
}
29 changes: 29 additions & 0 deletions src/main/java/bisq/asset/AddressValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.asset;

/**
* An {@link Asset} address validation function.
*
* @author Chris Beams
* @since 0.7.0
*/
public interface AddressValidator {

AddressValidationResult validate(String address);
}
46 changes: 46 additions & 0 deletions src/main/java/bisq/asset/Asset.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.asset;

/**
* Interface representing a given ("crypto") asset in its most abstract form, having a
* {@link #getName() name}, e.g. "Bitcoin", a {@link #getTickerSymbol() ticker symbol},
* e.g. "BTC", and an address validation function. Together, these properties represent
* the minimum information and functionality required to register and trade an asset on
* the Bisq network.
* <p>
* Implementations typically extend either the {@link Coin} or {@link Token} base
* classes, and must be registered in the {@code META-INF/services/bisq.asset.Asset} file
* in order to be available in the {@link AssetRegistry} at runtime.
*
* @author Chris Beams
* @since 0.7.0
* @see AbstractAsset
* @see Coin
* @see Token
* @see Erc20Token
* @see AssetRegistry
*/
public interface Asset {

String getName();

String getTickerSymbol();

AddressValidationResult validateAddress(String address);
}
46 changes: 46 additions & 0 deletions src/main/java/bisq/asset/AssetRegistry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.asset;

import java.util.ArrayList;
import java.util.List;
import java.util.ServiceLoader;
import java.util.stream.Stream;

/**
* Provides {@link Stream}-based access to {@link Asset} implementations registered in
* the {@code META-INF/services/bisq.asset.Asset} provider-configuration file.
*
* @author Chris Beams
* @since 0.7.0
* @see ServiceLoader
*/
public class AssetRegistry {

private static final List<Asset> registeredAssets = new ArrayList<>();

static {
for (Asset asset : ServiceLoader.load(Asset.class)) {
registeredAssets.add(asset);
}
}

public Stream<Asset> stream() {
return registeredAssets.stream();
}
}
54 changes: 54 additions & 0 deletions src/main/java/bisq/asset/Base58BitcoinAddressValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.asset;

import org.bitcoinj.core.Address;
import org.bitcoinj.core.AddressFormatException;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.params.MainNetParams;

/**
* {@link AddressValidator} for Base58-encoded Bitcoin addresses.
*
* @author Chris Beams
* @since 0.7.0
* @see org.bitcoinj.core.Address#fromBase58(NetworkParameters, String)
*/
public class Base58BitcoinAddressValidator implements AddressValidator {

private final NetworkParameters networkParameters;

public Base58BitcoinAddressValidator() {
this(MainNetParams.get());
}

public Base58BitcoinAddressValidator(NetworkParameters networkParameters) {
this.networkParameters = networkParameters;
}

@Override
public AddressValidationResult validate(String address) {
try {
Address.fromBase58(networkParameters, address);
} catch (AddressFormatException ex) {
return AddressValidationResult.invalidAddress(ex);
}

return AddressValidationResult.validAddress();
}
}
55 changes: 55 additions & 0 deletions src/main/java/bisq/asset/Coin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.asset;

/**
* Abstract base class for {@link Asset}s with their own dedicated blockchain, such as
* {@link bisq.asset.coins.Bitcoin} itself or one of its many derivatives, competitors and
* alternatives, often called "altcoins", such as {@link bisq.asset.coins.Litecoin},
* {@link bisq.asset.coins.Ether}, {@link bisq.asset.coins.Monero} and
* {@link bisq.asset.coins.Zcash}.
* <p>
* In addition to the usual {@code Asset} properties, a {@code Coin} maintains information
* about which {@link Network} it may be used on. By default, coins are constructed with
* the assumption they are for use on that coin's "main network", or "main blockchain",
* i.e. that they are "real" coins for use in a production environment. In testing
* scenarios, however, a coin may be constructed for use only on "testnet" or "regtest"
* networks.
*
* @author Chris Beams
* @since 0.7.0
*/
public abstract class Coin extends AbstractAsset {

public enum Network { MAINNET, TESTNET, REGTEST }

private final Network network;

public Coin(String name, String tickerSymbol, AddressValidator addressValidator) {
this(name, tickerSymbol, addressValidator, Network.MAINNET);
}

public Coin(String name, String tickerSymbol, AddressValidator addressValidator, Network network) {
super(name, tickerSymbol, addressValidator);
this.network = network;
}

public Network getNetwork() {
return network;
}
}
Loading