Added Groestlcoin Support

This commit is contained in:
aetherum 2015-01-01 20:14:20 +11:00
parent 67a0f62cee
commit eed78217b4
10 changed files with 674 additions and 7 deletions

View File

@ -25,6 +25,7 @@ public interface ICurrencies {
public static final String MAX = "MAX";
public static final String LEO = "LEO";
public static final String NLG = "NLG";
public static final String GRS = "GRS";
public static final String ICG = "ICG";
public static final String NBT = "NBT";

View File

@ -120,24 +120,24 @@
</ratesource>
<cryptologo cryptocurrency="LEO" file="leo.png"/>
</extension>
<extension class="com.generalbytes.batm.server.extensions.extra.guldencoin.GuldencoinExtension" >
<wallet prefix="guldencoind" name="guldencoind">
<extension class="com.generalbytes.batm.server.extensions.extra.groestlcoin.GroestlcoinExtension" >
<wallet prefix="groestlcoind" name="groestlcoind">
<param name="protocol" />
<param name="user" />
<param name="password" />
<param name="host" />
<param name="port" />
<param name="accountname" />
<cryptocurrency>NLG</cryptocurrency>
<cryptocurrency>GRS</cryptocurrency>
</wallet>
<ratesource prefix="nlgfix" name ="Fix Price" >
<param name="price" />
<cryptocurrency>NLG</cryptocurrency>
<cryptocurrency>GRS</cryptocurrency>
</ratesource>
<ratesource prefix="guldencoincom" name="Guldencoin.com" >
<cryptocurrency>NLG</cryptocurrency>
<ratesource prefix="groestlcoincom" name="Groestlcoin.org" >
<cryptocurrency>GRS</cryptocurrency>
</ratesource>
<cryptologo cryptocurrency="NLG" file="nlg.png"/>
<cryptologo cryptocurrency="GRS" file="groestl.png"/>
</extension>
<extension class="com.generalbytes.batm.server.extensions.extra.incognitocoin.IncognitocoinExtension" >
<wallet prefix="incognitocoind" name="Incognitocoind">

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

View File

@ -0,0 +1,61 @@
/*************************************************************************************
* Copyright (C) 2014 GENERAL BYTES s.r.o. All rights reserved.
*
* This software may be distributed and modified under the terms of the GNU
* General Public License version 2 (GPL2) as published by the Free Software
* Foundation and appearing in the file GPL2.TXT included in the packaging of
* this file. Please note that GPL2 Section 2[b] requires that all works based
* on this software must also be made publicly available under the terms of
* the GPL2 ("Copyleft").
*
* Contact information
* -------------------
*
* GENERAL BYTES s.r.o.
* Web : http://www.generalbytes.com
*
************************************************************************************/
package com.generalbytes.batm.server.extensions.extra.groestlcoin;
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 GroestlcoinAddressValidator implements ICryptoAddressValidator {
@Override
public boolean isAddressValid(String address) {
boolean result = isGroestlcoinAddressValid(address);
if (!result) {
result = isPaperWalletSupported() && ExtensionsUtil.isValidEmailAddress(address);
}
return result;
}
private static boolean isGroestlcoinAddressValid(String address) {
if (address.startsWith("G")) {
try {
Base58.decodeToBigInteger(address);
Base58.decodeChecked(address);
} catch (AddressFormatException e) {
e.printStackTrace();
return false;
}
return true;
}else{
return false;
}
}
@Override
public boolean isPaperWalletSupported() {
return false;
}
@Override
public boolean mustBeBase58Address() {
return true;
}
}

View File

@ -0,0 +1,125 @@
/*************************************************************************************
* Copyright (C) 2014 GENERAL BYTES s.r.o. All rights reserved.
*
* This software may be distributed and modified under the terms of the GNU
* General Public License version 2 (GPL2) as published by the Free Software
* Foundation and appearing in the file GPL2.TXT included in the packaging of
* this file. Please note that GPL2 Section 2[b] requires that all works based
* on this software must also be made publicly available under the terms of
* the GPL2 ("Copyleft").
*
* Contact information
* -------------------
*
* GENERAL BYTES s.r.o.
* Web : http://www.generalbytes.com
*
************************************************************************************/
package com.generalbytes.batm.server.extensions.extra.groestlcoin;
import com.generalbytes.batm.server.extensions.*;
import com.generalbytes.batm.server.extensions.extra.groestlcoin.sources.FixPriceRateSource;
import com.generalbytes.batm.server.extensions.extra.groestlcoin.sources.GroestlcoinTickerRateSource;
import com.generalbytes.batm.server.extensions.extra.groestlcoin.wallets.groestlcoind.GroestlcoindRPCWallet;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;
public class GroestlcoinExtension implements IExtension{
@Override
public String getName() {
return "BATM Groestlcoin extension";
}
@Override
public IExchange createExchange(String exchangeLogin) {
return null;
}
@Override
public IWallet createWallet(String walletLogin) {
if (walletLogin !=null && !walletLogin.trim().isEmpty()) {
StringTokenizer st = new StringTokenizer(walletLogin,":");
String walletType = st.nextToken();
if ("groestlcoind".equalsIgnoreCase(walletType)) {
//"groestlcoind:protocol:user:password:ip:port:accountname"
String protocol = st.nextToken();
String username = st.nextToken();
String password = st.nextToken();
String hostname = st.nextToken();
String port = st.nextToken();
String accountName ="";
if (st.hasMoreTokens()) {
accountName = st.nextToken();
}
if (protocol != null && username != null && password != null && hostname !=null && port != null && accountName != null) {
String rpcURL = protocol +"://" + username +":" + password + "@" + hostname +":" + port;
return new GroestlcoindRPCWallet(rpcURL,accountName);
}
}
}
return null;
}
@Override
public ICryptoAddressValidator createAddressValidator(String cryptoCurrency) {
if (ICurrencies.GRS.equalsIgnoreCase(cryptoCurrency)) {
return new GroestlcoinAddressValidator();
}
return null;
}
@Override
public IPaperWalletGenerator createPaperWalletGenerator(String cryptoCurrency) {
return null;
}
@Override
public IRateSource createRateSource(String sourceLogin) {
if (sourceLogin != null && !sourceLogin.trim().isEmpty()) {
StringTokenizer st = new StringTokenizer(sourceLogin,":");
String prefix = st.nextToken();
if ("nlgfix".equalsIgnoreCase(prefix)) {
BigDecimal rate = BigDecimal.ZERO;
if (st.hasMoreTokens()) {
try {
rate = new BigDecimal(st.nextToken());
} catch (Throwable e) {
}
}
String preferedFiatCurrency = ICurrencies.USD;
if (st.hasMoreTokens()) {
preferedFiatCurrency = st.nextToken();
}
return new FixPriceRateSource(rate,preferedFiatCurrency);
}else if ("groestlcoincom".equalsIgnoreCase(prefix)) {
return new GroestlcoinTickerRateSource();
}
}
return null;
}
@Override
public IPaymentProcessor createPaymentProcessor(String paymentProcessorLogin) {
return null; //no payment processors available
}
@Override
public Set<String> getSupportedCryptoCurrencies() {
Set<String> result = new HashSet<String>();
result.add(ICurrencies.GRS);
return result;
}
}

View File

@ -0,0 +1,54 @@
package com.generalbytes.batm.server.extensions.extra.groestlcoin.sources;
import com.generalbytes.batm.server.extensions.ICurrencies;
import com.generalbytes.batm.server.extensions.IRateSource;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;
/**
* Created by b00lean on 7/31/14.
*/
public class FixPriceRateSource implements IRateSource {
private BigDecimal rate = BigDecimal.ZERO;
private String preferedFiatCurrency = ICurrencies.USD;
public FixPriceRateSource(BigDecimal rate,String preferedFiatCurrency) {
this.rate = rate;
if (ICurrencies.EUR.equalsIgnoreCase(preferedFiatCurrency)) {
this.preferedFiatCurrency = ICurrencies.EUR;
}
if (ICurrencies.USD.equalsIgnoreCase(preferedFiatCurrency)) {
this.preferedFiatCurrency = ICurrencies.USD;
}
}
@Override
public Set<String> getCryptoCurrencies() {
Set<String> result = new HashSet<String>();
result.add(ICurrencies.GRS);
return result;
}
@Override
public BigDecimal getExchangeRateLast(String cryptoCurrency, String fiatCurrency) {
if (ICurrencies.GRS.equalsIgnoreCase(cryptoCurrency)) {
return rate;
}
return null;
}
@Override
public Set<String> getFiatCurrencies() {
Set<String> result = new HashSet<String>();
result.add(ICurrencies.EUR);
return result;
}
@Override
public String getPreferredFiatCurrency() {
return preferedFiatCurrency;
}
}

View File

@ -0,0 +1,122 @@
/*************************************************************************************
* Copyright (C) 2014 GENERAL BYTES s.r.o. All rights reserved.
*
* This software may be distributed and modified under the terms of the GNU
* General Public License version 2 (GPL2) as published by the Free Software
* Foundation and appearing in the file GPL2.TXT included in the packaging of
* this file. Please note that GPL2 Section 2[b] requires that all works based
* on this software must also be made publicly available under the terms of
* the GPL2 ("Copyleft").
*
* Contact information
* -------------------
*
* GENERAL BYTES s.r.o.
* Web : http://www.generalbytes.com
*
************************************************************************************/
package com.generalbytes.batm.server.extensions.extra.groestlcoin.sources;
import com.generalbytes.batm.server.extensions.ICurrencies;
import com.generalbytes.batm.server.extensions.IRateSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import si.mazi.rescu.RestProxyFactory;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
public class GroestlcoinTickerRateSource implements IRateSource{
private static final Logger log = LoggerFactory.getLogger(GroestlcoinTickerRateSource.class);
private static HashMap<String,BigDecimal> rateAmounts = new HashMap<String, BigDecimal>();
private static HashMap<String,Long> rateTimes = new HashMap<String, Long>();
private static final long MAXIMUM_ALLOWED_TIME_OFFSET = 30 * 1000; //30sec
private IGroestlcoinTickerRateAPI api;
public GroestlcoinTickerRateSource() {
api = RestProxyFactory.createProxy(IGroestlcoinTickerRateAPI.class, "https://markets.groestlcoin.com");
}
@Override
public BigDecimal getExchangeRateLast(String cryptoCurrency, String fiatCurrency) {
if (!(ICurrencies.GRS.equalsIgnoreCase(cryptoCurrency))) {
return null;
}
if (!(ICurrencies.EUR.equalsIgnoreCase(fiatCurrency))) {
return null;
}
String key = cryptoCurrency +"_" + fiatCurrency;
synchronized (rateAmounts) {
long now = System.currentTimeMillis();
BigDecimal amount = rateAmounts.get(key);
if (amount == null) {
BigDecimal result = getExchangeRateLastSync(cryptoCurrency, fiatCurrency);
log.debug("Called GroestlcoinTicker exchange for rate: " + key + " = " + result);
rateAmounts.put(key,result);
rateTimes.put(key,now+MAXIMUM_ALLOWED_TIME_OFFSET);
return result;
}else {
Long expirationTime = rateTimes.get(key);
if (expirationTime > now) {
return rateAmounts.get(key);
}else{
//do the job;
BigDecimal result = getExchangeRateLastSync(cryptoCurrency, fiatCurrency);
log.debug("Called GroestlcoinTicker exchange for rate: " + key + " = " + result);
rateAmounts.put(key,result);
rateTimes.put(key,now+MAXIMUM_ALLOWED_TIME_OFFSET);
return result;
}
}
}
}
private BigDecimal getExchangeRateLastSync(String cryptoCurrency, String fiatCurrency) {
if (!(ICurrencies.GRS.equalsIgnoreCase(cryptoCurrency))) {
return null;
}
if (!(ICurrencies.EUR.equalsIgnoreCase(fiatCurrency))) {
return null;
}
GroestlcoinTickerResponse ticker = api.getTicker();
if (ticker != null && ticker.getEUR() != null) {
if (ICurrencies.EUR.equalsIgnoreCase(fiatCurrency)){
return ticker.getEUR().getSell15m();
}
return null;
}
return null;
}
@Override
public Set<String> getCryptoCurrencies() {
Set<String> result = new HashSet<String>();
result.add(ICurrencies.GRS);
return result;
}
@Override
public Set<String> getFiatCurrencies() {
Set<String> result = new HashSet<String>();
result.add(ICurrencies.EUR);
return result;
}
@Override
public String getPreferredFiatCurrency() {
return ICurrencies.EUR;
}
public static void main(String[] args) {
BigDecimal exchangeRateLast = (new GroestlcoinTickerRateSource()).getExchangeRateLast("GRS", "EUR");
System.out.println("exchangeRateLast = " + exchangeRateLast);
}
}

View File

@ -0,0 +1,153 @@
/*************************************************************************************
* Copyright (C) 2014 GENERAL BYTES s.r.o. All rights reserved.
*
* This software may be distributed and modified under the terms of the GNU
* General Public License version 2 (GPL2) as published by the Free Software
* Foundation and appearing in the file GPL2.TXT included in the packaging of
* this file. Please note that GPL2 Section 2[b] requires that all works based
* on this software must also be made publicly available under the terms of
* the GPL2 ("Copyleft").
*
* Contact information
* -------------------
*
* GENERAL BYTES s.r.o.
* Web : http://www.generalbytes.com
*
************************************************************************************/
package com.generalbytes.batm.server.extensions.extra.Groestlcoin.sources;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.math.BigDecimal;
public class GroestlcoinTickerResponse {
@JsonProperty("EUR")
private EUR euro;
@JsonProperty("BTC")
private BTC btc;
public class BTC {
private String code;
private String symbol;
private BigDecimal buy;
private BigDecimal sell;
private BigDecimal buy15m;
private BigDecimal sell15m;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public BigDecimal getBuy() {
return buy;
}
public void setBuy(BigDecimal buy) {
this.buy = buy;
}
public BigDecimal getSell() {
return sell;
}
public void setSell(BigDecimal sell) {
this.sell = sell;
}
public BigDecimal getBuy15m() {
return buy15m;
}
public void setBuy15m(BigDecimal buy15m) {
this.buy15m = buy15m;
}
public BigDecimal getSell15m() {
return sell15m;
}
public void setSell15m(BigDecimal sell15m) {
this.sell15m = sell15m;
}
}
public class EUR {
private String code;
private String symbol;
private BigDecimal buy;
private BigDecimal sell;
private BigDecimal buy15m;
private BigDecimal sell15m;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public BigDecimal getBuy() {
return buy;
}
public void setBuy(BigDecimal buy) {
this.buy = buy;
}
public BigDecimal getSell() {
return sell;
}
public void setSell(BigDecimal sell) {
this.sell = sell;
}
public BigDecimal getBuy15m() {
return buy15m;
}
public void setBuy15m(BigDecimal buy15m) {
this.buy15m = buy15m;
}
public BigDecimal getSell15m() {
return sell15m;
}
public void setSell15m(BigDecimal sell15m) {
this.sell15m = sell15m;
}
}
public EUR getEUR() {
return euro;
}
public void setEUR(EUR euro) {
this.euro = euro;
}
public BTC getBTC() {
return btc;
}
public void setBTC(BTC btc) {
this.btc = btc;
}
}

View File

@ -0,0 +1,31 @@
/*************************************************************************************
* Copyright (C) 2014 GENERAL BYTES s.r.o. All rights reserved.
*
* This software may be distributed and modified under the terms of the GNU
* General Public License version 2 (GPL2) as published by the Free Software
* Foundation and appearing in the file GPL2.TXT included in the packaging of
* this file. Please note that GPL2 Section 2[b] requires that all works based
* on this software must also be made publicly available under the terms of
* the GPL2 ("Copyleft").
*
* Contact information
* -------------------
*
* GENERAL BYTES s.r.o.
* Web : http://www.generalbytes.com
*
************************************************************************************/
package com.generalbytes.batm.server.extensions.extra.groestlcoin.sources;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/api/v1/")
@Produces(MediaType.APPLICATION_JSON)
public interface IGroestlcoinTickerRateAPI {
@GET
@Path("ticker")
GroestlcoinTickerResponse getTicker();
}

View File

@ -0,0 +1,120 @@
/*************************************************************************************
* Copyright (C) 2014 GENERAL BYTES s.r.o. All rights reserved.
*
* This software may be distributed and modified under the terms of the GNU
* General Public License version 2 (GPL2) as published by the Free Software
* Foundation and appearing in the file GPL2.TXT included in the packaging of
* this file. Please note that GPL2 Section 2[b] requires that all works based
* on this software must also be made publicly available under the terms of
* the GPL2 ("Copyleft").
*
* Contact information
* -------------------
*
* GENERAL BYTES s.r.o.
* Web : http://www.generalbytes.com
*
************************************************************************************/
package com.generalbytes.batm.server.extensions.extra.groestlcoin.wallets.groestlcoind;
import com.azazar.bitcoin.jsonrpcclient.BitcoinException;
import com.azazar.bitcoin.jsonrpcclient.BitcoinJSONRPCClient;
import com.generalbytes.batm.server.extensions.ICurrencies;
import com.generalbytes.batm.server.extensions.IWallet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class GroestlcoindRPCWallet implements IWallet{
private static final Logger log = LoggerFactory.getLogger(GroestlcoindRPCWallet.class);
private static final String CRYPTO_CURRENCY = ICurrencies.GRS;
public GroestlcoindRPCWallet(String rpcURL, String accountName) {
this.rpcURL = rpcURL;
this.accountName = accountName;
}
private String rpcURL;
private String accountName;
@Override
public Set<String> getCryptoCurrencies() {
Set<String> result = new HashSet<String>();
result.add(CRYPTO_CURRENCY);
return result;
}
@Override
public String getPreferredCryptoCurrency() {
return CRYPTO_CURRENCY;
}
@Override
public String sendCoins(String destinationAddress, BigDecimal amount, String cryptoCurrency, String description) {
if (!CRYPTO_CURRENCY.equalsIgnoreCase(cryptoCurrency)) {
log.error("Groestlcoind wallet error: unknown cryptocurrency.");
return null;
}
log.info("Groestlcoind sending coins from " + accountName + " to: " + destinationAddress + " " + amount);
try {
String result = getClient(rpcURL).sendFrom(accountName, destinationAddress,amount.doubleValue());
log.debug("result = " + result);
return result;
} catch (BitcoinException e) {
e.printStackTrace();
return null;
}
}
@Override
public String getCryptoAddress(String cryptoCurrency) {
if (!CRYPTO_CURRENCY.equalsIgnoreCase(cryptoCurrency)) {
log.error("Groestlcoind wallet error: unknown cryptocurrency.");
return null;
}
try {
List<String> addressesByAccount = getClient(rpcURL).getAddressesByAccount(accountName);
if (addressesByAccount == null || addressesByAccount.size() == 0) {
return null;
}else{
return addressesByAccount.get(0);
}
} catch (BitcoinException e) {
e.printStackTrace();
return null;
}
}
@Override
public BigDecimal getCryptoBalance(String cryptoCurrency) {
if (!CRYPTO_CURRENCY.equalsIgnoreCase(cryptoCurrency)) {
log.error("Groestlcoind wallet error: unknown cryptocurrency: " + cryptoCurrency);
return null;
}
try {
double balance = getClient(rpcURL).getBalance(accountName);
return new BigDecimal(balance);
} catch (BitcoinException e) {
e.printStackTrace();
return null;
}
}
private BitcoinJSONRPCClient getClient(String rpcURL) {
try {
return new BitcoinJSONRPCClient(rpcURL);
} catch (MalformedURLException e) {
e.printStackTrace();
}
return null;
}
}