区块链相关命令

for Mac

1
2
3
4
5
6
7
8
9
10
11
12
13
geth init "/Users/sentient3326/Desktop/geth/genesis.json" --datadir  "/Users/sentient3326/Desktop/geth/chaindata"

geth attach rpc:http://localhost:8545

geth --datadir /Users/sentient3326/Desktop/geth/chaindata --networkid 15 --http --http.api "eth,web3,miner,admin,personal,net" --http.corsdomain "*" --allow-insecure-unlock --nodiscover --dev --dev.period 1

personal.unlockAccount(eth.accounts[0],"123456")

eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:3000000000000000000})

eth.getBalance(eth.accounts[1])

--dev Ephemeral proof-of-authority network with a pre-funded developer account, mining enabled --dev.period value Block periodtouseindeveloper mode (0= mine onlyiftransaction pending) (default:0)

for Windows

1
2
3
geth init "C:\geth\genesis.json" --datadir  "C:\geth\chaindata "

geth --datadir "C:\geth\chaindata " --rpc --rpcapi "eth,web3,miner,admin,personal,net" --rpccorsdomain "*" --nodiscover --networkid 15 --allow-insecure-unlock

pojo

Order

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package edu.wtbu.pojo;

public class Order {
private int id;
private String imgUrl;
private String goodsName;
private float orderPrice;
private String buyTime;
private String payTime;
private int status;
private String transactionHash;
private String orderAddress;
private String userEmail;

public Order() {
super();

}

public Order(int id, String imgUrl, String goodsName, float orderPrice, String buyTime, String payTime, int status,
String transactionHash, String orderAddress, String userEmail) {
super();
this.id = id;
this.imgUrl = imgUrl;
this.goodsName = goodsName;
this.orderPrice = orderPrice;
this.buyTime = buyTime;
this.payTime = payTime;
this.status = status;
this.transactionHash = transactionHash;
this.orderAddress = orderAddress;
this.userEmail = userEmail;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getImgUrl() {
return imgUrl;
}

public void setImgUrl(String imgUrl) {
this.imgUrl = imgUrl;
}

public String getGoodsName() {
return goodsName;
}

public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}

public float getOrderPrice() {
return orderPrice;
}

public void setOrderPrice(float orderPrice) {
this.orderPrice = orderPrice;
}

public String getBuyTime() {
return buyTime;
}

public void setBuyTime(String buyTime) {
this.buyTime = buyTime;
}

public String getPayTime() {
return payTime;
}

public void setPayTime(String payTime) {
this.payTime = payTime;
}

public int getStatus() {
return status;
}

public void setStatus(int status) {
this.status = status;
}

public String getTransactionHash() {
return transactionHash;
}

public void setTransactionHash(String transactionHash) {
this.transactionHash = transactionHash;
}

public String getOrderAddress() {
return orderAddress;
}

public void setOrderAddress(String orderAddress) {
this.orderAddress = orderAddress;
}

public String getUserEmail() {
return userEmail;
}

public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}




}

OrderDetail

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package edu.wtbu.pojo;

public class OrderDetail {
private String orderAddress;
private String goodsPrice;
private String goodsNum;
private String orderPrice;
private String buyerAddress;
private String sellerAddress;
private String buyTime;
private String payTime;
private String status;

public String getOrderAddress() {
return orderAddress;
}
public void setOrderAddress(String orderAddress) {
this.orderAddress = orderAddress;
}
public String getGoodsPrice() {
return goodsPrice;
}
public void setGoodsPrice(String goodsPrice) {
this.goodsPrice = goodsPrice;
}
public String getGoodsNum() {
return goodsNum;
}
public void setGoodsNum(String goodsNum) {
this.goodsNum = goodsNum;
}
public String getOrderPrice() {
return orderPrice;
}
public void setOrderPrice(String orderPrice) {
this.orderPrice = orderPrice;
}
public String getBuyerAddress() {
return buyerAddress;
}
public void setBuyerAddress(String buyerAddress) {
this.buyerAddress = buyerAddress;
}
public String getSellerAddress() {
return sellerAddress;
}
public void setSellerAddress(String sellerAddress) {
this.sellerAddress = sellerAddress;
}
public String getBuyTime() {
return buyTime;
}
public void setBuyTime(String buyTime) {
this.buyTime = buyTime;
}
public String getPayTime() {
return payTime;
}
public void setPayTime(String payTime) {
this.payTime = payTime;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}

public OrderDetail() {
super();

}

public OrderDetail(String orderAddress, String goodsPrice, String goodsNum, String orderPrice, String buyerAddress,
String sellerAddress, String buyTime, String payTime, String status) {
super();
this.orderAddress = orderAddress;
this.goodsPrice = goodsPrice;
this.goodsNum = goodsNum;
this.orderPrice = orderPrice;
this.buyerAddress = buyerAddress;
this.sellerAddress = sellerAddress;
this.buyTime = buyTime;
this.payTime = payTime;
this.status = status;
}




}

Helper

MySqlHelper

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package edu.wtbu.helper;

import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class MySqlHelper {

private static String driver="com.mysql.cj.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/geth?serverTimezone=GMT%2B8&userUnicode=true&characterEncoding=UTF-8";
private static String user = "root";
private static String password = "123456";

private static Connection conn = null;
private static PreparedStatement prep = null;
private static ResultSet rs = null;

static {
try {
Class.forName(driver);
} catch (Exception e) {}
}

public static int executeUpdate(String sql,Object [] param) {
int result = 0;
try {
conn = DriverManager.getConnection(url,user,password);
prep = conn.prepareStatement(sql);

if(param != null) {

for(int i = 0;i < param.length;i++) {
String className = param[i].getClass().getName();
if(className.contains("String")) {
prep.setString(i+1, param[i].toString());
}else if(className.contains("Integer")) {
prep.setInt(i+1, Integer.parseInt(param[i].toString()));
}else if(className.contains("Float")) {
prep.setFloat(i+1, Float.parseFloat(param[i].toString()));
}
}
}
result = prep.executeUpdate();
}catch(SQLException e) {}

CloseAll();
return result;
}

public static ResultSet executeQuery(String sql,Object [] param) {

try {
conn = DriverManager.getConnection(url,user,password);
prep = conn.prepareStatement(sql);

if(param != null) {

for(int i = 0;i < param.length;i++) {
String className = param[i].getClass().getName();
if(className.contains("String")) {
prep.setString(i+1, param[i].toString());
}else if(className.contains("Integer")) {
prep.setInt(i+1, Integer.parseInt(param[i].toString()));
}else if(className.contains("Float")) {
prep.setFloat(i+1, Float.parseFloat(param[i].toString()));
}
}
}
rs = prep.executeQuery();
}catch(SQLException e) {}

return rs;
}

public static void CloseAll() {
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

if(prep != null) {
try {
prep.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}


}


#Eth

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package edu.wtbu.eth;

import java.io.IOException;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;

import org.web3j.abi.FunctionEncoder;
import org.web3j.abi.FunctionReturnDecoder;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.Utf8String;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.admin.Admin;
import org.web3j.protocol.admin.methods.response.NewAccountIdentifier;
import org.web3j.protocol.admin.methods.response.PersonalListAccounts;
import org.web3j.protocol.admin.methods.response.PersonalUnlockAccount;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.Request;
import org.web3j.protocol.core.methods.request.Transaction;
import org.web3j.protocol.core.methods.response.EthCall;
import org.web3j.protocol.core.methods.response.EthGetBalance;
import org.web3j.protocol.core.methods.response.EthGetTransactionReceipt;
import org.web3j.protocol.core.methods.response.EthSendTransaction;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.Contract;
import org.web3j.utils.Convert;

import edu.wtbu.pojo.OrderDetail;

public class EthService {
private static Admin admin = null;
private static Web3j web3j = null;
private static String SystemPassword = "123456";
private static String BINCODE = "0x6060604052341561000f57600080fd5b6040516109fb3803806109fb83398101604052808051820191906020018051906020019091908051820191906020018051820191906020018051820191906020018051820191905050856000908051906020019061006e9291906100e5565b5084600181905550836002908051906020019061008c9291906100e5565b5082600390805190602001906100a39291906100e5565b5081600490805190602001906100ba9291906100e5565b5080600590805190602001906100d19291906100e5565b50600060078190555050505050505061018a565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061012657805160ff1916838001178555610154565b82800160010185558215610154579182015b82811115610153578251825591602001919060010190610138565b5b5090506101619190610165565b5090565b61018791905b8082111561018357600081600090555060010161016b565b5090565b90565b610862806101996000396000f30060606040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806301efb6c814610051578063966a7dca14610309575b600080fd5b341561005c57600080fd5b610064610366565b6040518080602001898152602001806020018060200180602001806020018060200188815260200187810387528f818151815260200191508051906020019080838360005b838110156100c45780820151818401526020810190506100a9565b50505050905090810190601f1680156100f15780820380516001836020036101000a031916815260200191505b5087810386528d818151815260200191508051906020019080838360005b8381101561012a57808201518184015260208101905061010f565b50505050905090810190601f1680156101575780820380516001836020036101000a031916815260200191505b5087810385528c818151815260200191508051906020019080838360005b83811015610190578082015181840152602081019050610175565b50505050905090810190601f1680156101bd5780820380516001836020036101000a031916815260200191505b5087810384528b818151815260200191508051906020019080838360005b838110156101f65780820151818401526020810190506101db565b50505050905090810190601f1680156102235780820380516001836020036101000a031916815260200191505b5087810383528a818151815260200191508051906020019080838360005b8381101561025c578082015181840152602081019050610241565b50505050905090810190601f1680156102895780820380516001836020036101000a031916815260200191505b50878103825289818151815260200191508051906020019080838360005b838110156102c25780820151818401526020810190506102a7565b50505050905090810190601f1680156102ef5780820380516001836020036101000a031916815260200191505b509e50505050505050505050505050505060405180910390f35b341561031457600080fd5b610364600480803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509190505061075b565b005b61036e61077d565b600061037861077d565b61038061077d565b61038861077d565b61039061077d565b61039861077d565b6000808054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561042f5780601f106104045761010080835404028352916020019161042f565b820191906000526020600020905b81548152906001019060200180831161041257829003601f168201915b50505050509750600154965060028054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156104d15780601f106104a6576101008083540402835291602001916104d1565b820191906000526020600020905b8154815290600101906020018083116104b457829003601f168201915b5050505050955060038054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561056e5780601f106105435761010080835404028352916020019161056e565b820191906000526020600020905b81548152906001019060200180831161055157829003601f168201915b5050505050945060048054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561060b5780601f106105e05761010080835404028352916020019161060b565b820191906000526020600020905b8154815290600101906020018083116105ee57829003601f168201915b5050505050935060058054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156106a85780601f1061067d576101008083540402835291602001916106a8565b820191906000526020600020905b81548152906001019060200180831161068b57829003601f168201915b5050505050925060068054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156107455780601f1061071a57610100808354040283529160200191610745565b820191906000526020600020905b81548152906001019060200180831161072857829003601f168201915b5050505050915060075490509091929394959697565b60016007819055508060069080519060200190610779929190610791565b5050565b602060405190810160405280600081525090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106107d257805160ff1916838001178555610800565b82800160010185558215610800579182015b828111156107ff5782518255916020019190600101906107e4565b5b50905061080d9190610811565b5090565b61083391905b8082111561082f576000816000905550600101610817565b5090565b905600a165627a7a72305820194d22581d81d97445f3febf2511fde4ee6b074083b70b4ebe2bb0d70ec237790029";
static {
HttpService service = new HttpService("http://127.0.0.1:8545");
admin = Admin.build(service);
web3j = Web3j.build(service);
}

public static String createAccount(String password) {
String address = null;
try {
Request<?, NewAccountIdentifier> request = admin.personalNewAccount(password);
NewAccountIdentifier response = request.send();
address = response.getAccountId();
} catch (IOException e) {
e.printStackTrace();
}
return address;
}

public static String getBalance(String address) {
String balance = "0";
Request<?, EthGetBalance> request = web3j.ethGetBalance(address, DefaultBlockParameterName.LATEST);
try {
EthGetBalance response = request.send();
balance = Convert.fromWei(response.getBalance().toString(), Convert.Unit.ETHER).toString();
} catch (IOException e) {
e.printStackTrace();
}
return balance;
}

public static Boolean unlockAccount(String address, String password) {
Boolean result = false;
try {
Request<?, PersonalUnlockAccount> request = admin.personalUnlockAccount(address, password);
PersonalUnlockAccount response = request.send();
Boolean unlockResult = response.getResult();
if (unlockResult != null && unlockResult) {
result = true;
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}

public static Boolean transferEther(String from, String to, String amount, String password) {
Boolean result = false;
try {
Boolean unlockResult = unlockAccount(from, password);

if (unlockResult) {

BigInteger value = Convert.toWei(amount, Convert.Unit.ETHER).toBigInteger();

Transaction transaction = Transaction.createEtherTransaction(from, null, null, Contract.GAS_LIMIT, to,value);

Request<?, EthSendTransaction> request = web3j.ethSendTransaction(transaction);

EthSendTransaction response = request.sendAsync().get();

String transactionHash = response.getResult();
if (transactionHash != null) {
result = true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}

public static String getSysAddress() {
String sysAddress = null;
try {

Request<?, PersonalListAccounts> request = admin.personalListAccounts();

PersonalListAccounts response = request.send();

List<String> accountList = response.getAccountIds();

sysAddress = accountList.get(0);
} catch (Exception e) {

e.printStackTrace();
}
return sysAddress;
}


public static Boolean recharge(String userAddress, String amount) {
Boolean result = false;

String sysAddress = getSysAddress();

result = transferEther(sysAddress, userAddress, amount, SystemPassword);
return result;
}

//
// start from here
//

public static String deployOrderContract(String goodsPrice,int goodsNum,String orderPrice, String buyerAddress,String sellerAddress,String buyTime) {
String transactionHash = null;
Boolean unlockResult = unlockAccount(sellerAddress,"123456");
if(unlockResult) {
try {
String encode = FunctionEncoder.encodeConstructor(Arrays.asList(new Utf8String(goodsPrice),new Uint256(goodsNum),new Utf8String(orderPrice),new Utf8String(buyerAddress),new Utf8String(sellerAddress),new Utf8String(buyTime)));
Transaction transaction = Transaction.createContractTransaction(sellerAddress, null, null, BINCODE+encode);
Request<?, EthSendTransaction> request = web3j.ethSendTransaction(transaction);
EthSendTransaction response = request.sendAsync().get();
transactionHash = response.getResult();
} catch (Exception e) { }
}

return transactionHash;


}

public static String getOrderAddressByHash(String transactionHash) {
String orderAddress = null;
try {
Request<?, EthGetTransactionReceipt> request = web3j.ethGetTransactionReceipt(transactionHash);
EthGetTransactionReceipt response = request.send();
orderAddress = response.getResult().getContractAddress();
} catch (IOException e) {
e.printStackTrace();
}
return orderAddress;
}

public static Boolean payOrder(String userAddress, String contractAddress, String payTime, String password) {
Boolean result = false;
try {
Boolean unlockResult = unlockAccount(userAddress, password);
if (unlockResult) {
Function function = new Function("payOrder", Arrays.asList(new Utf8String(payTime)), Arrays.asList());
String encode = FunctionEncoder.encode(function);
Transaction transaction = Transaction.createFunctionCallTransaction(userAddress, null, null,
Contract.GAS_LIMIT, contractAddress, encode);
Request<?, EthSendTransaction> request = web3j.ethSendTransaction(transaction);
EthSendTransaction response = request.sendAsync().get();
String transactionHash = response.getResult();
if (transactionHash != null) {
result = true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}

public static OrderDetail getOrderInfo(String userAddress, String orderAddress) {
OrderDetail detail = null;
try {
Function function = new Function("getOrderInfo", Arrays.asList(),
Arrays.asList(
new TypeReference<Utf8String>() {},
new TypeReference<Uint256>() {},
new TypeReference<Utf8String>() {},
new TypeReference<Utf8String>() {},
new TypeReference<Utf8String>() {},
new TypeReference<Utf8String>() {},
new TypeReference<Utf8String>() {},
new TypeReference<Uint256>() {}
)
);
String encode = FunctionEncoder.encode(function);
Transaction transaction = Transaction.createEthCallTransaction(userAddress, orderAddress, encode);
Request<?, EthCall> request = web3j.ethCall(transaction, DefaultBlockParameterName.LATEST);
EthCall response = request.send();
String value = response.getResult();
List<Type> list = FunctionReturnDecoder.decode(value, function.getOutputParameters());
detail = new OrderDetail();
detail.setGoodsPrice(list.get(0).getValue().toString());
detail.setGoodsNum(list.get(1).getValue().toString());
detail.setOrderPrice(list.get(2).getValue().toString());
detail.setBuyerAddress(list.get(3).getValue().toString());
detail.setSellerAddress(list.get(4).getValue().toString());
detail.setBuyTime(list.get(5).getValue().toString());
detail.setPayTime(list.get(6).getValue().toString());
detail.setStatus(list.get(7).getValue().toString());
} catch (IOException e) {
e.printStackTrace();
}
return detail;
}

}

Servlet

BuyServlet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package edu.wtbu.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import edu.wtbu.dao.GoodsDao;
import edu.wtbu.dao.OrderDao;
import edu.wtbu.dao.UserDao;
import edu.wtbu.eth.EthService;
import edu.wtbu.pojo.Goods;
import edu.wtbu.pojo.Result;
import edu.wtbu.pojo.User;

/**
* Servlet implementation class BuyServlet
*/
@WebServlet("/buy")
public class BuyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public BuyServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf8");
String email = request.getParameter("email");
int goodsId = 0;
try {
goodsId = Integer.parseInt(request.getParameter("goodsId"));
} catch (Exception e) {
goodsId = 0;
}
int goodsNum = 0;
try {
goodsNum = Integer.parseInt(request.getParameter("goodsNum"));
} catch (Exception e) {
goodsNum = 0;
}
Result result = new Result("fail", null);
if (email == null || email.equals("")) {
result.setData("登录信息失效,请重新登录!");
response.getWriter().append(JSON.toJSONString(result));
return;
}
Goods goods = GoodsDao.findGoodsById(goodsId);
User user = UserDao.findByEmail(email);
float goodsPrice = goods.getPrice();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String buyTime = sdf.format(new Date());
String transactionHash = EthService.deployOrderContract(String.valueOf(goodsPrice), goodsNum,
String.valueOf(goodsPrice * goodsNum), user.getAddress(), goods.getSellerAddress(), buyTime);
if (transactionHash == null) {
result.setData("购买失败!");
response.getWriter().append(JSON.toJSONString(result));
return;
}
int addResult = OrderDao.addOrder(goods.getImgUrl(), goods.getName(), goodsPrice * goodsNum, buyTime, 0,
transactionHash, email);
if (addResult > 0) {
result.setFlag("success");
}
response.getWriter().append(JSON.toJSONString(result));
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}

}

PayServlet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package edu.wtbu.servlet;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import edu.wtbu.dao.GoodsDao;
import edu.wtbu.dao.OrderDao;
import edu.wtbu.dao.UserDao;
import edu.wtbu.eth.EthService;
import edu.wtbu.pojo.Goods;
import edu.wtbu.pojo.Order;
import edu.wtbu.pojo.Result;
import edu.wtbu.pojo.User;

/**
* Servlet implementation class PayServlet
*/
@WebServlet("/pay")
public class PayServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public PayServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf8");
String email = request.getParameter("email");
int orderId = 0;
try {
orderId = Integer.parseInt(request.getParameter("orderId"));
} catch (Exception e) {
orderId = 0;
}
Result result = new Result("fail", null);
if (email == null || email.equals("")) {
result.setData("登录信息失效,请重新登录!");
response.getWriter().append(JSON.toJSONString(result));
return;
}
User user = UserDao.findByEmail(email);
String balance = EthService.getBalance(user.getAddress());
Order order = OrderDao.findOrderById(orderId);
float orderPrice = order.getOrderPrice();
if (new BigDecimal(balance).compareTo(new BigDecimal(orderPrice)) < 0) {
result.setData("余额不足,请充值!");
response.getWriter().append(JSON.toJSONString(result));
return;
}
Goods goods = GoodsDao.findGoodsByName(order.getGoodsName());
Boolean payResult = EthService.transferEther(user.getAddress(), goods.getSellerAddress(),
String.valueOf(order.getOrderPrice()), user.getPassword());
if (!payResult) {
result.setData("转账失败!");
response.getWriter().append(JSON.toJSONString(result));
return;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String payTime = sdf.format(new Date());
String orderAddress = EthService.getOrderAddressByHash(order.getTransactionHash());
payResult = EthService.payOrder(user.getAddress(), orderAddress, payTime, user.getPassword());
if (!payResult) {
result.setData("更新合约失败!");
response.getWriter().append(JSON.toJSONString(result));
return;
}
int updateResult = OrderDao.updateOrder(orderId, payTime, 1, orderAddress);
if (updateResult > 0) {
result.setFlag("success");
}
response.getWriter().append(JSON.toJSONString(result));
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}

OrderListServlet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package edu.wtbu.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import edu.wtbu.dao.OrderDao;
import edu.wtbu.pojo.Order;
import edu.wtbu.pojo.Result;

/**
* Servlet implementation class OrderLisyServlet
*/
@WebServlet("/orderList")
public class OrderListServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public OrderListServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
response.setContentType("text/html;charset=utf8");
String email = request.getParameter("email");
Result result = new Result("fail", null);
if (email == null || email.equals("")) {
result.setData("登录信息失效,请登录后重试!");
response.getWriter().append(JSON.toJSONString(result));
return;
}
ArrayList<Order> list = OrderDao.findOrderListByEmail(email);
result.setFlag("success");
result.setData(list);
response.getWriter().append(JSON.toJSONString(result));
}


/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}

}

DetailServlet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package edu.wtbu.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import edu.wtbu.dao.OrderDao;
import edu.wtbu.dao.UserDao;
import edu.wtbu.eth.EthService;
import edu.wtbu.pojo.Order;
import edu.wtbu.pojo.OrderDetail;
import edu.wtbu.pojo.Result;
import edu.wtbu.pojo.User;

/**
* Servlet implementation class DetailServlet
*/
@WebServlet("/orderDetail")
public class DetailServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public DetailServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf8");
String email = request.getParameter("email");
int orderId = 0;
try {
orderId = Integer.parseInt(request.getParameter("orderId"));
} catch (Exception e) {
orderId = 0;
}
Result result = new Result("fail", null);
if (email == null || email.equals("")) {
result.setData("登录信息失效,请重新登录!");
response.getWriter().append(JSON.toJSONString(result));
return;
}
User user = UserDao.findByEmail(email);
Order order = OrderDao.findOrderById(orderId);
OrderDetail detail = EthService.getOrderInfo(user.getAddress(), order.getOrderAddress());
detail.setOrderAddress(order.getOrderAddress());
result.setFlag("success");
result.setData(detail);
response.getWriter().append(JSON.toJSONString(result));
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}

ByteCode

1
2
0x6060604052341561000f57600080fd5b6040516109fb3803806109fb83398101604052808051820191906020018051906020019091908051820191906020018051820191906020018051820191906020018051820191905050856000908051906020019061006e9291906100e5565b5084600181905550836002908051906020019061008c9291906100e5565b5082600390805190602001906100a39291906100e5565b5081600490805190602001906100ba9291906100e5565b5080600590805190602001906100d19291906100e5565b50600060078190555050505050505061018a565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061012657805160ff1916838001178555610154565b82800160010185558215610154579182015b82811115610153578251825591602001919060010190610138565b5b5090506101619190610165565b5090565b61018791905b8082111561018357600081600090555060010161016b565b5090565b90565b610862806101996000396000f30060606040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806301efb6c814610051578063966a7dca14610309575b600080fd5b341561005c57600080fd5b610064610366565b6040518080602001898152602001806020018060200180602001806020018060200188815260200187810387528f818151815260200191508051906020019080838360005b838110156100c45780820151818401526020810190506100a9565b50505050905090810190601f1680156100f15780820380516001836020036101000a031916815260200191505b5087810386528d818151815260200191508051906020019080838360005b8381101561012a57808201518184015260208101905061010f565b50505050905090810190601f1680156101575780820380516001836020036101000a031916815260200191505b5087810385528c818151815260200191508051906020019080838360005b83811015610190578082015181840152602081019050610175565b50505050905090810190601f1680156101bd5780820380516001836020036101000a031916815260200191505b5087810384528b818151815260200191508051906020019080838360005b838110156101f65780820151818401526020810190506101db565b50505050905090810190601f1680156102235780820380516001836020036101000a031916815260200191505b5087810383528a818151815260200191508051906020019080838360005b8381101561025c578082015181840152602081019050610241565b50505050905090810190601f1680156102895780820380516001836020036101000a031916815260200191505b50878103825289818151815260200191508051906020019080838360005b838110156102c25780820151818401526020810190506102a7565b50505050905090810190601f1680156102ef5780820380516001836020036101000a031916815260200191505b509e50505050505050505050505050505060405180910390f35b341561031457600080fd5b610364600480803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509190505061075b565b005b61036e61077d565b600061037861077d565b61038061077d565b61038861077d565b61039061077d565b61039861077d565b6000808054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561042f5780601f106104045761010080835404028352916020019161042f565b820191906000526020600020905b81548152906001019060200180831161041257829003601f168201915b50505050509750600154965060028054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156104d15780601f106104a6576101008083540402835291602001916104d1565b820191906000526020600020905b8154815290600101906020018083116104b457829003601f168201915b5050505050955060038054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561056e5780601f106105435761010080835404028352916020019161056e565b820191906000526020600020905b81548152906001019060200180831161055157829003601f168201915b5050505050945060048054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561060b5780601f106105e05761010080835404028352916020019161060b565b820191906000526020600020905b8154815290600101906020018083116105ee57829003601f168201915b5050505050935060058054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156106a85780601f1061067d576101008083540402835291602001916106a8565b820191906000526020600020905b81548152906001019060200180831161068b57829003601f168201915b5050505050925060068054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156107455780601f1061071a57610100808354040283529160200191610745565b820191906000526020600020905b81548152906001019060200180831161072857829003601f168201915b5050505050915060075490509091929394959697565b60016007819055508060069080519060200190610779929190610791565b5050565b602060405190810160405280600081525090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106107d257805160ff1916838001178555610800565b82800160010185558215610800579182015b828111156107ff5782518255916020019190600101906107e4565b5b50905061080d9190610811565b5090565b61083391905b8082111561082f576000816000905550600101610817565b5090565b905600a165627a7a72305820194d22581d81d97445f3febf2511fde4ee6b074083b70b4ebe2bb0d70ec237790029