Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stellar Coin Extension #899

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* CryptoCurrency.valueOf("$PAC") returns IllegalArgumentException.
*/
public enum CryptoCurrency {

XLM("Stellar"),
ADA("Cardano Ada"),
ANON("ANON"),
AQUA("Aquachain"),
Expand Down Expand Up @@ -124,15 +124,14 @@ public enum CryptoCurrency {
NULS("NULS"),
MUE("MonetaryUnit"),
XPM("Primecoin"),
ZPAE("ZelaaPayAE"),

ZPAE("ZelaaPayAE --test"),
TBCH("test BCH"),
TBTC("test BTC"),
TETH("test ETH"),
TLTC("test LTC"),
TRMG("test RMG"),
TXRP("test XRP"),
;
TXRP("test XRP");

private static Map<String, CryptoCurrency> cryptoCurrenciesUpperCase = new HashMap<>();
private static Map<String, CryptoCurrency> cryptoCurrencies = new LinkedHashMap<>();
Expand Down
6 changes: 6 additions & 0 deletions server_extensions_extra/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ dependencies {
implementation("com.vdurmont:emoji-java:3.1.3") //for chat emojis
implementation("com.nexmo:client:5.5.0") // sms provider
implementation("org.bitcoinj:bitcoinj-core:0.16.1")

// Note when updating bitcoinj: block.io depends on libdohj for doge and ltc
// which depends on bitcoinj and extends some classes, it must be compatible version
// and it will break at runtime if the version is substituted for an incompatible one
Expand Down Expand Up @@ -95,6 +96,11 @@ dependencies {
implementation("com.google.protobuf:protobuf-java:3.13.0")
implementation("com.onfido:onfido-api-java:2.3.1")

// https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0'
// https://mvnrepository.com/artifact/com.google.code.gson/gson
implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.6'

testImplementation("junit:junit:4.13.1")
testImplementation("org.assertj:assertj-core:3.19.0")
testImplementation("ch.qos.logback:logback-classic:1.2.9")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class BinanceComExchange extends BinanceExchange {
SUPPORTED_FIATS.add(CryptoCurrency.DAI.getCode());
SUPPORTED_FIATS.add(CryptoCurrency.USDC.getCode());
SUPPORTED_FIATS.add(CryptoCurrency.USDT.getCode());

SUPPORTED_FIATS.add(CryptoCurrency.XLM.getCode());
SUPPORTED_CRYPTOS.add(new SupportedCryptoCurrency(CryptoCurrency.BAT.getCode()));
SUPPORTED_CRYPTOS.add(new SupportedCryptoCurrency(CryptoCurrency.BCH.getCode()));
SUPPORTED_CRYPTOS.add(new SupportedCryptoCurrency(CryptoCurrency.BNB.getCode()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class BinanceUsExchange extends BinanceExchange {
SUPPORTED_FIATS.add(CryptoCurrency.BUSD.getCode());
SUPPORTED_FIATS.add(CryptoCurrency.USDC.getCode());
SUPPORTED_FIATS.add(CryptoCurrency.USDT.getCode());

SUPPORTED_FIATS.add(CryptoCurrency.XLM.getCode());
SUPPORTED_CRYPTOS.add(new SupportedCryptoCurrency(CryptoCurrency.BAT.getCode()));
SUPPORTED_CRYPTOS.add(new SupportedCryptoCurrency(CryptoCurrency.BCH.getCode()));
SUPPORTED_CRYPTOS.add(new SupportedCryptoCurrency(CryptoCurrency.BNB.getCode()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public class BitfinexExchange implements IExchangeAdvanced, IRateSourceAdvanced
static {
FIAT_CURRENCIES.add(FiatCurrency.USD.getCode());
FIAT_CURRENCIES.add(CryptoCurrency.USDT.getCode());
FIAT_CURRENCIES.add(CryptoCurrency.XLM.getCode());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ enum TradableLimitFor {BUY, SELL}

static {
CRYPTO_CURRENCIES.add(CryptoCurrency.BTC.getCode());
CRYPTO_CURRENCIES.add(CryptoCurrency.XLM.getCode());
}

private void initConstants(String baseUrl) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private static void initConstants() {
FIAT_CURRENCIES.add(FiatCurrency.GBP.getCode());
FIAT_CURRENCIES.add(FiatCurrency.SGD.getCode());
FIAT_CURRENCIES.add(FiatCurrency.USD.getCode());

CRYPTO_CURRENCIES.add(CryptoCurrency.XLM.getCode());
CRYPTO_CURRENCIES.add(CryptoCurrency.ADA.getCode());
CRYPTO_CURRENCIES.add(CryptoCurrency.BAT.getCode());
CRYPTO_CURRENCIES.add(CryptoCurrency.BCH.getCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class CoinZixExchange implements IRateSourceAdvanced, IExchangeAdvanced {

private static void initConstants() {
FIAT_CURRENCIES.add(FiatCurrency.EUR.getCode());

CRYPTO_CURRENCIES.add(CryptoCurrency.XLM.getCode());
CRYPTO_CURRENCIES.add(CryptoCurrency.BTC.getCode());
CRYPTO_CURRENCIES.add(CryptoCurrency.ETH.getCode());
CRYPTO_CURRENCIES.add(CryptoCurrency.EGLD.getCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class CoinGeckoRateSource implements IRateSource {

static {
// from https://api.coingecko.com/api/v3/coins/list
CRYPTOCURRENCIES.put(CryptoCurrency.XLM.getCode(), "XLM");
CRYPTOCURRENCIES.put(CryptoCurrency.ANON.getCode(), "anon");
CRYPTOCURRENCIES.put(CryptoCurrency.ANT.getCode(), "aragon");
CRYPTOCURRENCIES.put(CryptoCurrency.BAT.getCode(), "basic-attention-token");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package com.generalbytes.batm.server.extensions.extra.stellar;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.generalbytes.batm.common.currencies.CryptoCurrency;
import com.generalbytes.batm.server.extensions.AbstractExtension;
import com.generalbytes.batm.server.extensions.ExtensionsUtil;
import com.generalbytes.batm.server.extensions.ICryptoAddressValidator;
import com.generalbytes.batm.server.extensions.IWallet;
import com.generalbytes.batm.server.extensions.extra.stellar.wallets.stellar.StellarCoinWallet;
import com.generalbytes.batm.server.extensions.extra.stellar.wallets.stellar.consts.Const;
import com.generalbytes.batm.server.extensions.extra.stellar.wallets.stellar.dto.Wallet;
import com.google.gson.Gson;

public class StellarCoinExtension extends AbstractExtension {
private static final Logger log = LoggerFactory.getLogger(StellarCoinExtension.class);
String requestBody;

@Override
public String getName() {
return "BATM Stellarcoin extension";
}

@Override
public IWallet createWallet(String walletLogin, String tunnelPassword) {

log.debug("Stellar:CreateWallet Started");
if (walletLogin != null && !walletLogin.trim().isEmpty()) {
try {
log.debug("Stellar: Step 1");
StringTokenizer st = new StringTokenizer(walletLogin, ":");
String walletType = st.nextToken();

if ("bpventures.us".equalsIgnoreCase(walletType)) {
log.debug("Stellar: Step 2");
String secret = st.nextToken();
String apikey = st.nextToken();
String hostname = st.nextToken();
String testnet = st.nextToken();
if (testnet.equals("true")) {
log.debug("Stellar: Step 3");
requestBody = "{\"testnet\":"+ testnet+" , \"secret\":"+secret+"}";
}
try {
log.debug("Stellar: Step 4");
URL url = new URL(hostname + Const.ADDWALLET);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Authorization", "Token " + apikey);
connection.setDoOutput(true);
try (OutputStream os = connection.getOutputStream()) {
byte[] input = requestBody.getBytes("utf-8");
os.write(input, 0, input.length);
}
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
Gson gson = new Gson();
Wallet wallet = gson.fromJson(reader.readLine(), Wallet.class);
wallet.setApiKey(apikey);
wallet.setSecret(secret);
log.debug("Stellar: Step 5");
return new StellarCoinWallet(wallet);
}
} catch (IOException e) {
e.printStackTrace();
}
connection.disconnect();

} catch (IOException e) {
e.printStackTrace();
}

}
} catch (Exception e) {
ExtensionsUtil.logExtensionParamsException("createWallet", getClass().getSimpleName(), walletLogin, e);
}
}
return null;

}

@Override
public ICryptoAddressValidator createAddressValidator(String cryptoCurrency) {
log.debug("Stellar: Step 6");
if (CryptoCurrency.XLM.getCode().equalsIgnoreCase(cryptoCurrency)) {
return new StellarcoinAddressValidator();
}
return null;
}

@Override
public Set<String> getSupportedCryptoCurrencies() {
log.debug("Stellar: Step 7");
Set<String> result = new HashSet<String>();
result.add(CryptoCurrency.XLM.getCode());
return result;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.generalbytes.batm.server.extensions.extra.stellar;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.generalbytes.batm.server.coinutil.AddressFormatException;
import com.generalbytes.batm.server.coinutil.Base58;
import com.generalbytes.batm.server.extensions.ExtensionsUtil;
import com.generalbytes.batm.server.extensions.ICryptoAddressValidator;

public class StellarcoinAddressValidator implements ICryptoAddressValidator {
private static final Logger log = LoggerFactory.getLogger("batm.master.extensions.StellarcoinAddressValidator");

@Override
public boolean isAddressValid(String address) {
if (address.length() < 56) {
System.out.println("Error: Stellar public keys should be 56 characters long");
return false;
} else if (address.equals("0")) {
System.out.println(
"Error: Although technically the correct length, a key of all zeros is not a valid Stellar public key.\n"
+ "");
return false;
}

else if (!address.startsWith("G")) {
System.out.println("Error:Stellar public keys should start with the letter G");
return false;
} else

if (!StringUtils.isAlphanumeric(address)) {
System.out.println("Error: Stellar public keys should only contain alphanumeric characters.");
return false;
}
return true;
}

@Override
public boolean mustBeBase58Address() {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean isPaperWalletSupported() {
// TODO Auto-generated method stub
return false;
}

}
Loading