com.jingtum.model.FinGate Maven / Gradle / Ivy
/*
* Copyright www.jingtum.com Inc.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.jingtum.model;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.text.DecimalFormat;
import java.util.Properties;
import com.jingtum.Jingtum;
import com.jingtum.JingtumMessage;
import com.jingtum.core.crypto.ecdsa.Seed;
import com.jingtum.exception.APIConnectionException;
import com.jingtum.exception.APIException;
import com.jingtum.exception.AuthenticationException;
import com.jingtum.exception.ChannelException;
import com.jingtum.exception.FailedException;
import com.jingtum.exception.InvalidParameterException;
import com.jingtum.exception.InvalidRequestException;
import com.jingtum.exception.JingtumException;
import com.jingtum.net.JingtumWebSocket;
import com.jingtum.net.SubscribeEventHandler;
import com.jingtum.util.Utility;
/**
* @author jzhao
* @version 1.0 FinGate class
*/
public class FinGate extends BaseWallet {
private static final String PROPERTY_FILE = "com/jingtum/conf/config.properties"; // property file
private static final String DEV_PROPERTY_FILE = "com/jingtum/conf/dev.properties"; // property file for testing
private static final String ISSUE_CURRENCY = "/currency/issue";
private static final String QUERY_ISSUE = "/currency/queryIssue";
private static final String CURRENCY_STATUS = "/currency/status";
private String webSocketServer; // web socket server
private String apiBase; // api server
private String prefix; // prefix
private String apiVersion; // api version
private boolean isTest;
private double activateAmount; // default amout of SWT to activate wallet
private double trustLimit;
private double pathRate;
private Wallet wallet;
private SubscribeEventHandler handler;
private JingtumWebSocket jtWebSocket;
private String tt_server;
private String custom;
private String customSecret;
private static class FinGateHolder {
private static final FinGate INSTANCE = new FinGate();
}
/**
* Server utility class
* @author jzhao
*
*/
private class ServerUtility {
private boolean connected;
private String uuid;
public boolean getConnected() {
return connected;
}
public String getUuid() {
return uuid;
}
}
/**
* Read .properties file
*/
private FinGate() {
init();
}
/**
* Singleton mode
* @return FinGat instance
*/
public static final FinGate getInstance() {
return FinGateHolder.INSTANCE;
}
/**
* Set api server
* @param server
*/
public void setServer(String server) {
this.apiBase = server;
}
/**
* @return api server
*/
public String getServer() {
return apiBase;
}
/**
* Initialize FinGate
* @param address
* @param secret
* @throws InvalidParameterException
*/
public void setFinGate(String address, String secret) throws InvalidParameterException {
if (!Utility.validateKeyPair(address, secret)) {
throw new InvalidParameterException(JingtumMessage.INVALID_JINGTUM_ADDRESS_OR_SECRET, address + secret,
null);
}
this.address = address;
this.secret = secret;
}
/**
* @return FinGate address
*/
public String getFinGate() {
return address;
}
/**
* Set to test mode
* @param isTest
*/
public void setTest(boolean isTest) {
if(this.isTest != isTest){
this.isTest = isTest;
init();
}
}
/**
* @return activate amount
*/
public double getActivateAmount() {
return activateAmount;
}
/**
* Set default activate amount
* @param activateAmount
*/
public void setActivateAmount(double activateAmount) {
this.activateAmount = activateAmount;
}
/**
* @return prefix
*/
public String getPrefix() {
return prefix;
}
/**
* Set prefix
* @param prefix
*/
public void setPrefix(String prefix) {
this.prefix = prefix;
}
/**
* @return web socket server
*/
public String getWebSocketServer() {
return webSocketServer;
}
/**
* Set web socket server
* @param webSocketServer
*/
public void setWebSocketServer(String webSocketServer) {
this.webSocketServer = webSocketServer;
}
/**
* @return default trust limit
*/
public double getTrustLimit() {
return trustLimit;
}
/**
* Set default trust limit
* @param trustLimit
*/
public void setTrustLimit(double trustLimit) {
this.trustLimit = trustLimit;
}
/**
* @return path rate
*/
public double getPathRate() {
return pathRate;
}
/**
* Set path rate
* @param pathRate
*/
public void setPathRate(double pathRate) {
this.pathRate = pathRate;
}
/**
* @return api version
*/
public String getApiVersion() {
return apiVersion;
}
/**
* Set api version
* @param apiVersion
*/
public void setApiVersion(String apiVersion) {
this.apiVersion = apiVersion;
}
/**
* Get custom number
* @return custom
*/
public String getCustom() {
return custom;
}
/**
* Set custom number
* @param custom
*/
public void setCustom(String custom) {
this.custom = custom;
}
/**
* Get custom secret
* @return custom secret
*/
public String getCustomSecret() {
return customSecret;
}
/**
* Set custom secret
* @param customSecret
*/
public void setCustomSecret(String customSecret) {
this.customSecret = customSecret;
}
/**
* @return server status
* @throws AuthenticationException
* @throws InvalidRequestException
* @throws APIConnectionException
* @throws ChannelException
* @throws APIException
* @throws FailedException
*/
public boolean getStatus() throws AuthenticationException, InvalidRequestException, APIConnectionException,
ChannelException, APIException, FailedException {
return request(RequestMethod.GET, formatURL("server/connected"), null, ServerUtility.class).getConnected();
}
/**
* @return next uuid
* @throws AuthenticationException
* @throws InvalidRequestException
* @throws APIConnectionException
* @throws ChannelException
* @throws APIException
* @throws FailedException
*/
public String getNextUUID() throws AuthenticationException, InvalidRequestException, APIConnectionException,
ChannelException, APIException, FailedException {
return request(RequestMethod.GET, formatURL("uuid"), null, ServerUtility.class).getUuid();
}
/**
* @return wallet
* @throws InvalidParameterException
*/
private Wallet getMyWallet() throws InvalidParameterException {
if (wallet == null) {
wallet = new Wallet(this.address, this.secret);
}
return wallet;
}
/**
* Set handler in subscription
* @param handler
*/
public void setTxHandler(SubscribeEventHandler handler) {
this.handler = handler;
}
/**
* @return websocket
*/
public JingtumWebSocket getJtWebSocket() {
return jtWebSocket;
}
/**
* Creat wallet
* @return wallet
*/
public Wallet createWallet() {
String secret = Seed.generateSecret();
String address = null;
try {
address = Seed.computeAddress(secret);
} catch (InvalidParameterException e) {
e.printStackTrace();
}
try {
return new Wallet(address, secret);
} catch (InvalidParameterException e) {
e.printStackTrace();
}
return null;
}
/**
* Activate newly created wallet
*
* @param address
* @return true if successfully activated wallet
* @throws APIException
* @throws InvalidParameterException
*/
public boolean activateWallet(String address) throws APIException, InvalidParameterException {
if (this.address == null) { // Must initialized the gateway first
throw new APIException(JingtumMessage.GATEWAY_NOT_INITIALIZED, null);
}
if (!Utility.isValidAddress(address)) {
throw new InvalidParameterException(JingtumMessage.INVALID_JINGTUM_ADDRESS, address, null);
}
RequestResult payment = null;
Amount jtc = new Amount(); // build jingtum amount
try {
jtc.setCounterparty("");
jtc.setCurrency(Jingtum.getCurrencySWT());
jtc.setValue(this.activateAmount); // value
// send default amount of SWT to each wallet
payment = getMyWallet().submitPayment(address, jtc, true, getNextUUID());
} catch (FailedException e) {
e.printStackTrace();
} catch (AuthenticationException e) {
e.printStackTrace();
} catch (InvalidRequestException e) {
e.printStackTrace();
} catch (APIConnectionException e) {
e.printStackTrace();
} catch (ChannelException e) {
e.printStackTrace();
} catch (InvalidParameterException e) {
e.printStackTrace();
}
if (payment != null) {
return payment.getSuccess();
}
return false;
}
/**
* Open connection to Web Socket server
*
* @return true if opened connection successfully
* @throws APIException
* @throws InvalidParameterException
*/
public boolean connet() throws APIException, InvalidParameterException {
if (null == handler) {
throw new InvalidParameterException(JingtumMessage.NOT_NULL_MESSAGE_HANDLER, null, null);
}
try {
jtWebSocket = new JingtumWebSocket();
} catch (URISyntaxException e) {
e.printStackTrace();
return false;
}
return jtWebSocket.openWebSocket(handler);
}
/**
* Close Web Socket connection
*
* @return true if successfully closed connection
* @throws APIException
*/
public boolean disconnect() throws APIException {
if (null == jtWebSocket || jtWebSocket.isConnected() == false) {
throw new APIException(JingtumMessage.NO_CONNECTION_AVAIABLE, null);
}
boolean isClosed = jtWebSocket.closeWebSocket();
if (isClosed) {
jtWebSocket = null;
}
return isClosed;
}
/**
* Get order book
*
* @param base
* @param counter
* @return OrderBookResult
* @throws AuthenticationException
* @throws InvalidRequestException
* @throws APIConnectionException
* @throws APIException
* @throws ChannelException
* @throws InvalidParameterException
* @throws FailedException
*/
public OrderBookResult getOrderBook(Amount base, Amount counter)
throws AuthenticationException, InvalidRequestException, APIConnectionException, APIException,
ChannelException, InvalidParameterException, FailedException {
if (!Utility.isValidAmount(base) || !Utility.isValidAmount(counter)) {
throw new InvalidParameterException(JingtumMessage.INVALID_JINGTUM_AMOUNT, null, null);
}
StringBuffer sb = new StringBuffer();
sb.append("/");
sb.append(base.getCurrency());
sb.append("+");
sb.append(base.getCounterparty());
sb.append("/");
sb.append(counter.getCurrency());
sb.append("+");
sb.append(counter.getCounterparty());
if (this.secret == null) {
this.secret = Seed.generateSecret();
this.address = Seed.computeAddress(secret);
}
return request(RequestMethod.GET,
formatURL(OrderBook.class, this.address,
sb.toString() + Utility.buildSignString(this.address, this.secret)),
null, OrderBookResult.class);
}
/**
* @param order
* @param currency
* @param amount
* @param account
* @return true if issue successfully
* @throws InvalidParameterException
*/
public boolean issueCustomTum(String order,String currency,double amount,String account) throws InvalidParameterException{
if(Utility.isEmpty(this.custom)){
throw new InvalidParameterException(JingtumMessage.EMPTY_CUSTOM,this.custom,null);
}
if(Utility.isEmpty(this.customSecret)){
throw new InvalidParameterException(JingtumMessage.EMPTY_SECRET,this.customSecret,null);
}
if(Utility.isEmpty(currency)||Utility.isEmpty(account)||amount<=0){
throw new InvalidParameterException(JingtumMessage.ERROR_INPUT,null,null);
}
if(!Utility.isValidAddress(account)){
throw new InvalidParameterException(JingtumMessage.INVALID_JINGTUM_ADDRESS,account,null);
}
TongTong tt;
DecimalFormat myFormatter = new DecimalFormat("0.00");
String amountString = myFormatter.format(amount);
String orderNumber = order;
if(Utility.isEmpty(orderNumber)){
try {
orderNumber = getNextUUID();
} catch (JingtumException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
StringBuffer sb = new StringBuffer();
sb.append(TongTong.CmdType.IssueCurrency);
sb.append(this.custom);
sb.append(orderNumber);
sb.append(currency);
sb.append(amountString);
sb.append(account);
String hmac = Utility.buildHmac(sb.toString(),this.customSecret);
StringBuffer param = new StringBuffer();
param.append("p0_cmd=");
param.append(TongTong.CmdType.IssueCurrency);
param.append("&");
param.append("p1_custom=");
param.append(this.custom);
param.append("&");
param.append("p2_order=");
param.append(orderNumber);
param.append("&");
param.append("p3_currency=");
param.append(currency);
param.append("&");
param.append("p4_amount=");
param.append(amountString);
param.append("&");
param.append("p5_account=");
param.append(account);
param.append("&");
param.append("hmac=");
param.append(hmac);
try {
tt = request(RequestMethod.POST_FORM, tt_server+ ISSUE_CURRENCY, param.toString(), TongTong.class);
} catch (JingtumException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
if (tt != null && "1".equals(tt.getSystemCode())){
return true;
}
else{
return false;
}
}
/**
* @param order
* @return IssueRecord
* @throws InvalidParameterException
*/
public IssueRecord queryIssue(String order) throws InvalidParameterException{
if(Utility.isEmpty(this.custom)){
throw new InvalidParameterException(JingtumMessage.EMPTY_CUSTOM,this.custom,null);
}
if(Utility.isEmpty(this.customSecret)){
throw new InvalidParameterException(JingtumMessage.EMPTY_SECRET,this.customSecret,null);
}
if(Utility.isEmpty(order)){
throw new InvalidParameterException(JingtumMessage.INVALID_ORDER_NUMBER,order,null);
}
StringBuffer sb = new StringBuffer();
sb.append(TongTong.CmdType.QueryIssue);
sb.append(this.custom);
sb.append(order);
String hmac = Utility.buildHmac(sb.toString(),this.customSecret);
StringBuffer param = new StringBuffer();
param.append("p0_cmd=");
param.append(TongTong.CmdType.QueryIssue);
param.append("&");
param.append("p1_custom=");
param.append(this.custom);
param.append("&");
param.append("p2_order=");
param.append(order);
param.append("&");
param.append("hmac=");
param.append(hmac);
try {
return request(RequestMethod.GET, tt_server+ QUERY_ISSUE, param.toString(), IssueRecord.class);
} catch (AuthenticationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidRequestException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (APIConnectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ChannelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (APIException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FailedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* @param currency
* @return TumInfo
* @throws InvalidParameterException
*/
public TumInfo queryCustomTum(String currency) throws InvalidParameterException{
if(Utility.isEmpty(this.custom)){
throw new InvalidParameterException(JingtumMessage.EMPTY_CUSTOM,this.custom,null);
}
if(Utility.isEmpty(this.customSecret)){
throw new InvalidParameterException(JingtumMessage.EMPTY_SECRET,this.customSecret,null);
}
if(Utility.isEmpty(currency)){
throw new InvalidParameterException(JingtumMessage.ERROR_INPUT,currency,null);
}
long unix = System.currentTimeMillis()/1000L;
StringBuffer sb = new StringBuffer();
sb.append(TongTong.CmdType.CurrencyStatus);
sb.append(this.custom);
sb.append(currency);
sb.append(unix);
String hmac = Utility.buildHmac(sb.toString(),this.customSecret);
StringBuffer param = new StringBuffer();
param.append("p0_cmd=");
param.append(TongTong.CmdType.CurrencyStatus);
param.append("&");
param.append("p1_custom=");
param.append(this.custom);
param.append("&");
param.append("p2_currency=");
param.append(currency);
param.append("&");
param.append("p3_date=");
param.append(unix);
param.append("&");
param.append("hmac=");
param.append(hmac);
try {
return request(RequestMethod.GET, tt_server+ CURRENCY_STATUS, param.toString(), TumInfo.class);
} catch (AuthenticationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidRequestException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (APIConnectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ChannelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (APIException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FailedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* Read .properties file
*/
private void init() {
String configFile;
configFile = isTest ? DEV_PROPERTY_FILE : PROPERTY_FILE;
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(configFile);
Properties p = new Properties();
try {
p.load(inputStream);
this.webSocketServer = p.getProperty("web_socket_server");
this.apiBase = p.getProperty("api_server");
apiVersion = p.getProperty("api_version");
this.activateAmount = Double.parseDouble(p.getProperty("activate_amount"));
this.prefix = p.getProperty("prefix");
this.tt_server = p.getProperty("tt_server");
this.trustLimit = Double.parseDouble(p.getProperty("default_trust_limit"));
this.pathRate = Double.parseDouble(p.getProperty("payment_path_rate"));
} catch (IOException e1) {
e1.printStackTrace();
}
}
} © 2015 - 2025 Weber Informatics LLC | Privacy Policy