Viacoin: RPCWallet and AddressValidator

This commit is contained in:
romanornr 2017-11-07 16:23:17 +01:00
parent 522ee4e632
commit b01a7a05b4
No known key found for this signature in database
GPG Key ID: 3F92368F0D21A206
3 changed files with 167 additions and 0 deletions

View File

@ -0,0 +1,53 @@
/*************************************************************************************
* Copyright (C) 2014-2017 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.viacoin;
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 ViacoinAddressValidator implements ICryptoAddressValidator {
@Override
public boolean isAddressValid(String address) {
if (address.startsWith("V")) {
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,114 @@
/*************************************************************************************
* Copyright (C) 2014-2017 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.viacoin.wallets.viacoind;
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 ViacoindRPCWallet implements IWallet{
private static final Logger log = LoggerFactory.getLogger(ViacoindRPCWallet.class);
private static final CRYPTO_CURRENCY = ICurrencies.VIA;
public ViacoindRPCWallet(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("Viacoind wallet error: unknown cryptocurrency");
return null;
}
log.info("Viacoind 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("Viacoind wallet error: unknown cryptocurrency.");
return null;
}
try{
double balance = getClient(rpcURL).getBalance(accountName);
return new BigDecimal(balance);
}catch(BitcoinException e){
e.printStackTrace();
return null
}
}
@Override
public BigDecimal getCryptoBalance(string cryptocurrency){
if(!CRYPTO_CURRENCY.equalsIgnoreCase(cryptocurrency)){
log.error("Viacoind wallet error: unknown cryptocurrency" + cryptoCurrency);
return null;
}
try{
double balance = getClient(rpcUrl).getBalance(accountName);
return BigDecimal.ValueOf(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;
}
}