-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJTInterfacetest.cpp
More file actions
271 lines (239 loc) · 7.17 KB
/
JTInterfacetest.cpp
File metadata and controls
271 lines (239 loc) · 7.17 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include <iostream>
#include <random>
#include "/usr/include/curl/curl.h"
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::random_device;
typedef random_device Random;
#define CREATURLGET "https://tapi.jingtum.com/v1/wallet/new"
#define ACTIVITURL "https://tapi.jingtum.com/v1/accounts/jsqRs9BDCjyTuRWEPZk3yHa4MFmRi9D834/payments?validated=true"
#define URL_DEFAULT "https://tapi.jingtum.com/"
//#define TRUSTURL "https://tapi.jingtum.com/v1/accounts/jsqRs9BDCjyTuRWEPZk3yHa4MFmRi9D834/trustlines?validated=true"
void getprocess(const string strURL)
{
CURL* curl;
CURLcode res;
curl = curl_easy_init();
if(NULL != curl)
{
curl_easy_setopt(curl, CURLOPT_URL, strURL.c_str());
struct curl_slist *plist = curl_slist_append(NULL,
"Content-Type:application/json;charset=utf-8");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, plist);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
}
void postprocess(const string strURL, const string strJson)
{
CURL* curl;
CURLcode res;
curl = curl_easy_init();
if(NULL != curl)
{
curl_easy_setopt(curl, CURLOPT_URL, strURL.c_str());
struct curl_slist *plist = curl_slist_append(NULL,
"Content-Type:application/json;charset=utf-8");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, plist);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strJson.c_str());
curl_easy_setopt(curl,CURLOPT_POST,1);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
}
//获取信任信息
void gettrustinfo()
{
string strURL;
string strPerfix("https://tapi.jingtum.com/v1/accounts/");
string strSuffix("/trustlines");
string strAddress;
cout << "输入地址:";
cin >> strAddress;
strURL = strPerfix+strAddress+strSuffix;
//Get
getprocess( strURL );
}
//余额获取
void getbalances()
{
string strURL;
string strPerfix("https://tapi.jingtum.com/v1/accounts/");
string strSuffix("/balances");
string strAddress;
cout << "输入地址:";
cin >> strAddress;
strURL = strPerfix+strAddress+strSuffix;
//Get
getprocess( strURL );
}
//信任
void posttrust( const string trustingAdd,
const string trustedAdd,
string strSecret = "sn7wfavva6mV1wbjtHR1mwXjEkb8Y",
string strLimit = "888",
string strCurrency = "8000000001201504081454405641668733216531")
{
cout<< endl << "trust" << endl;
//设置URL
string strURL;
string strPerfix_trusting("https://tapi.jingtum.com/v1/accounts/");
string strSuffix_trusting("/trustlines?validated=true");
strURL = strPerfix_trusting+trustingAdd+strSuffix_trusting;
//设置Json串
//string strPerfix_trustedAdd("{\"secret\": \"sn7wfavva6mV1wbjtHR1mwXjEkb8Y\",\"trustline\": {\"limit\": \"888\",\"currency\": \"8000000001201504081454405641668733216531\",\"counterparty\": \"");
string strPresec("{\"secret\": \"");
string strPrelimit("\",\"trustline\": {\"limit\": \"");
string strPrecurrency("\",\"currency\": \"");
string strPrecounterparty("\",\"counterparty\": \"");
string strSuffix_json("\"}}");
string strJson;
strJson = strPresec+strSecret+strPrelimit+strLimit+strPrecurrency+strCurrency+strPrecounterparty+trustedAdd+strSuffix_json;
//Post
postprocess(strURL, strJson);
}
//随机创建client_resource_id 需要支持c++11编译(-std=c++11)
//有很小概率获得重复的client_resource_id,导致激活失败
//可以修改数组中的值,改变随机内容
void creatorederno(string& orederno)
{
char str[] = "123456789ABCabcDTYdty123456789";
int len = sizeof(str);
Random rd;
int i = 0;
for(i=0; i<len-1; i++)
{
int index = rd()%(len-1);
orederno += str[index];
}
}
//激活
void activiting( const string str )
{
cout << endl << "activiting" << endl;
CURL* curl;
CURLcode res;
string strPrefix("{\"secret\": \"sn7wfavva6mV1wbjtHR1mwXjEkb8Y\",\"client_resource_id\": \"");
string orederNo;
//创建client_resource_id
creatorederno(orederNo);
string strMidfix("\",\"payment\": {\"source_account\": \"jsqRs9BDCjyTuRWEPZk3yHa4MFmRi9D834\",\"destination_account\": \"");
string strSuffix("\",\"destination_amount\": {\"value\": \"200.1\",\"currency\": \"SWT\",\"issuer\": \"\"}}}");
string strJson;
strJson = strPrefix+orederNo+strMidfix+str+strSuffix;
curl = curl_easy_init();
if(NULL != curl)
{
curl_easy_setopt(curl, CURLOPT_URL, ACTIVITURL);
struct curl_slist *plist = curl_slist_append(NULL,
"Content-Type:application/json;charset=utf-8");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, plist);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strJson.c_str());
curl_easy_setopt(curl,CURLOPT_POST,1);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
}
//curl回调函数
size_t write_data(void* buffer,size_t size,size_t nmemb,void *stream)
{
FILE *fptr = (FILE*)stream;
fwrite(buffer,size,nmemb,fptr);
string str = (char*)buffer;
cout << str.c_str() << endl;
int lenth_add = sizeof("\"address\": \"");
int lenth = str.find("\"address\": \"");
string strAccount = str.substr(lenth+lenth_add-1, 34);
int lenth_sec = sizeof("\"secret\": \"");
lenth = str.find("\"secret\": \"");
string strSecret = str.substr(lenth+lenth_sec-1, 29);
string trustingAdd("jsqRs9BDCjyTuRWEPZk3yHa4MFmRi9D834");
//激活刚创建的地址,所有刚创建的地址均被jsqRs9BDCjyTuRWEPZk3yHa4MFmRi9D834激活
activiting(strAccount);
//信任刚创建的地址,所有刚创建的地址均被jsqRs9BDCjyTuRWEPZk3yHa4MFmRi9D834信任
posttrust(trustingAdd, strAccount);
return size*nmemb;
}
//创建地址
void creataccount()
{
int count = 0;
cout << "请输入创建地址的数量:";
cin >> count;
int i = 0;
FILE *fptr = NULL;
for (i=0; i<count; i++)
{
CURL* curl;
CURLcode res;
if ( NULL == (fptr = fopen("accountid.txt","a+")) )
{
cout << "fopen file error:%s\n";
return;
}
cout << endl << "creat address" <<endl;
curl = curl_easy_init();
if(NULL != curl)
{
curl_easy_setopt(curl, CURLOPT_URL, CREATURLGET);
struct curl_slist *plist = curl_slist_append(NULL,
"Content-Type:application/json;charset=utf-8");
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,write_data);
curl_easy_setopt(curl,CURLOPT_WRITEDATA,fptr);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, plist);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
fclose(fptr);
}
return;
}
int main()
{
creataccount();
while(true)
{
cout << endl;
cout << "获取余额信息:1" << endl;
cout << "获取信任信息:2" << endl;
cout << "设置信任 :3" << endl;
cout << "退出 :0" <<endl;
int a;
cin >> a;
if(0 == a)break;
switch(a)
{
case 1 :
getbalances();
break;
case 2 :
gettrustinfo();
break;
case 3 :
{
cout << "输入信任方地址:" << endl;
string trustingAdd;
cin >> trustingAdd;
cout << "输入信任方密码:" << endl;
string strSecret;
cin >> strSecret;
cout << "输入被信任地址:" << endl;
string trustedAdd;
cin >> trustedAdd;
cout << "输入信任金额:" <<endl;
string strMonety;
cin >> strMonety;
cout << "输入货币种类:" << endl;
string strCurrency;
cin >> strCurrency;
posttrust(trustingAdd, trustedAdd, strSecret, strMonety, strCurrency);
break;
}
default:
break;
}
}
return 0;
}