Added support for Maxcoin

This commit is contained in:
b00lean 2014-07-15 13:39:17 +02:00
parent 2f36249d13
commit b0c74b3094
9 changed files with 545 additions and 0 deletions

View File

@ -22,6 +22,7 @@ public interface ICurrencies {
public static final String BTC = "BTC";
public static final String LTC = "LTC";
public static final String DOGE = "DOGE";
public static final String MAX = "MAX";
public static final String CZK = "CZK";

View File

@ -51,4 +51,20 @@
</ratesource>
<cryptologo cryptocurrency="LTC" file="ltc.png"/>
</extension>
<extension class="com.generalbytes.batm.server.extensions.extra.maxcoin.MaxcoinExtension" >
<wallet prefix="maxcoind" name="Maxcoind">
<param name="protocol" />
<param name="user" />
<param name="password" />
<param name="host" />
<param name="port" />
<param name="accountname" />
<cryptocurrency>MAX</cryptocurrency>
</wallet>
<ratesource prefix="maxcointicker" name="Maxcointicker.com" >
<cryptocurrency>MAX</cryptocurrency>
</ratesource>
<cryptologo cryptocurrency="MAX" file="max.png"/>
</extension>
</extensions>

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.9 KiB

View File

@ -0,0 +1,60 @@
/*************************************************************************************
* 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.maxcoin;
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 MaxcoinAddressValidator implements ICryptoAddressValidator {
@Override
public boolean isAddressValid(String address) {
boolean result = isMaxcoinAddressValid(address);
if (!result) {
result = isPaperWalletSupported() && ExtensionsUtil.isValidEmailAddress(address);
}
return result;
}
private boolean isMaxcoinAddressValid(String address) {
if (address.startsWith("m")) {
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,100 @@
/*************************************************************************************
* 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.maxcoin;
import com.generalbytes.batm.server.extensions.*;
import com.generalbytes.batm.server.extensions.extra.maxcoin.sources.MaxcoinTickerRateSource;
import com.generalbytes.batm.server.extensions.extra.maxcoin.wallets.maxcoind.MaxcoindRPCWallet;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;
public class MaxcoinExtension implements IExtension{
@Override
public String getName() {
return "BATM Maxcoin 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 ("maxcoind".equalsIgnoreCase(walletType)) {
//"maxcoind: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 MaxcoindRPCWallet(rpcURL,accountName);
}
}
}
return null;
}
@Override
public ICryptoAddressValidator createAddressValidator(String cryptoCurrency) {
if (ICurrencies.MAX.equalsIgnoreCase(cryptoCurrency)) {
return new MaxcoinAddressValidator();
}
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 exchangeType = st.nextToken();
if ("maxcointicker".equalsIgnoreCase(exchangeType)) {
return new MaxcoinTickerRateSource();
}
}
return null;
}
@Override
public Set<String> getSupportedCryptoCurrencies() {
Set<String> result = new HashSet<String>();
result.add(ICurrencies.MAX);
return result;
}
}

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.maxcoin.sources;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public interface IMaxcoinTickerRateAPI {
@GET
@Path("stats.php")
MaxcoinTickerResponse getTicker();
}

View File

@ -0,0 +1,113 @@
/*************************************************************************************
* 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.maxcoin.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 MaxcoinTickerRateSource implements IRateSource{
private static final Logger log = LoggerFactory.getLogger(MaxcoinTickerRateSource.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 IMaxcoinTickerRateAPI api;
public MaxcoinTickerRateSource() {
api = RestProxyFactory.createProxy(IMaxcoinTickerRateAPI.class, "http://www.maxcointicker.com");
}
@Override
public BigDecimal getExchangeRateLast(String cryptoCurrency, String fiatCurrency) {
if (!(ICurrencies.MAX.equalsIgnoreCase(cryptoCurrency))) {
return null;
}
if (!(ICurrencies.USD.equalsIgnoreCase(fiatCurrency) || 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 MaxTicker 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 MaxTicker 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.MAX.equalsIgnoreCase(cryptoCurrency))) {
return null;
}
if (!(ICurrencies.USD.equalsIgnoreCase(fiatCurrency) || ICurrencies.EUR.equalsIgnoreCase(fiatCurrency))) {
return null;
}
MaxcoinTickerResponse ticker = api.getTicker();
if (ticker != null) {
if (ICurrencies.USD.equalsIgnoreCase(fiatCurrency)){
return ticker.getMpusd();
}else if (ICurrencies.USD.equalsIgnoreCase(fiatCurrency)){
return ticker.getBtceuro();
}
return null;
}
return null;
}
@Override
public Set<String> getCryptoCurrencies() {
Set<String> result = new HashSet<String>();
result.add(ICurrencies.MAX);
return result;
}
@Override
public Set<String> getFiatCurrencies() {
Set<String> result = new HashSet<String>();
result.add(ICurrencies.USD);
result.add(ICurrencies.EUR);
return result;
}
}

View File

@ -0,0 +1,104 @@
/*************************************************************************************
* 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.maxcoin.sources;
import java.math.BigDecimal;
public class MaxcoinTickerResponse {
private BigDecimal btcusd;
private BigDecimal btceuro;
private BigDecimal btcgbp;
private BigDecimal btccny;
private BigDecimal mpbtc;
private BigDecimal mpusd;
private BigDecimal mpeuro;
private BigDecimal mpgbp;
private BigDecimal mpcny;
public BigDecimal getBtcusd() {
return btcusd;
}
public void setBtcusd(BigDecimal btcusd) {
this.btcusd = btcusd;
}
public BigDecimal getBtceuro() {
return btceuro;
}
public void setBtceuro(BigDecimal btceuro) {
this.btceuro = btceuro;
}
public BigDecimal getBtcgbp() {
return btcgbp;
}
public void setBtcgbp(BigDecimal btcgbp) {
this.btcgbp = btcgbp;
}
public BigDecimal getBtccny() {
return btccny;
}
public void setBtccny(BigDecimal btccny) {
this.btccny = btccny;
}
public BigDecimal getMpbtc() {
return mpbtc;
}
public void setMpbtc(BigDecimal mpbtc) {
this.mpbtc = mpbtc;
}
public BigDecimal getMpusd() {
return mpusd;
}
public void setMpusd(BigDecimal mpusd) {
this.mpusd = mpusd;
}
public BigDecimal getMpeuro() {
return mpeuro;
}
public void setMpeuro(BigDecimal mpeuro) {
this.mpeuro = mpeuro;
}
public BigDecimal getMpgbp() {
return mpgbp;
}
public void setMpgbp(BigDecimal mpgbp) {
this.mpgbp = mpgbp;
}
public BigDecimal getMpcny() {
return mpcny;
}
public void setMpcny(BigDecimal mpcny) {
this.mpcny = mpcny;
}
}

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.maxcoin.wallets.maxcoind;
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 MaxcoindRPCWallet implements IWallet{
private static final Logger log = LoggerFactory.getLogger(MaxcoindRPCWallet.class);
private static final String CRYPTO_CURRENCY = ICurrencies.MAX;
public MaxcoindRPCWallet(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) {
if (!CRYPTO_CURRENCY.equalsIgnoreCase(cryptoCurrency)) {
log.error("Maxcoind wallet error: unknown cryptocurrency.");
return null;
}
log.info("Maxcoind 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("Maxcoind 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("Maxcoind 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;
}
}