forked from Dearkano/TrueResume
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtility.ts
160 lines (145 loc) · 5.96 KB
/
Utility.ts
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
/**
* {
"success": true,
"secret": "RaxhMgevgJcm",
"message": "Jim enrolled Successfully",
"token": "<put JSON Web Token here>"
}
* @param name
* @param org
*/
export async function login(name,org) {
const url = `http://47.100.192.19:4010/users`;
const headers = {
"Content-Type": "application/x-www-form-urlencoded"
};
const body = 'username='+name+'&orgName='+org;
const response = await fetch(url, { method: "POST", headers, body });
const data = await response.json();
return data.token;
}
export function trim(str) {
return str.replace(/\s|\xA0|\x00/g, "");
}
export function outASCii(str) {
var len = 0;
for (var i = 0; i < str.length; i++) {
var c = str.charCodeAt(i);
console.log(parseInt(c));
}
}
export function strlen(str) {
var len = 0;
for (var i = 0; i < str.length; i++) {
var c = str.charCodeAt(i);
//单字节加1
if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
len++;
}
else {
len += 2;
}
}
return len;
}
export async function createChannel(token) {
const url = 'http://47.100.192.19:4010/channels';
const headers = { "Authorization": "Bearer " + token, "Content-Type": "application/json" };
const body = {
"channelName": "mychannel",
"channelConfigPath": "../artifacts/channel/mychannel.tx"
};
const response = await fetch(url, { method: "POST", headers, body: JSON.stringify(body) });
return await response.json();
}
export async function joinChannel(token) {
const url = 'http://47.100.192.19:4010/channels/mychannel/peers';
const headers = { "Authorization": "Bearer " + token, "Content-Type": "application/json" };
const body = {
"peers": [
"peer0.org1.example.com",
"peer1.org1.example.com",
]
};
const response = await fetch(url, { method: "POST", headers, body: JSON.stringify(body) });
return await response.json();
}
export async function installChaincode(token) {
const url = 'http://47.100.192.19:4010/chaincodes';
const headers = { "Authorization": "Bearer " + token, "Content-Type": "application/json" };
const body = {
"peers": ["peer0.org1.example.com", "peer1.org1.example.com" ,],
"chaincodeName": "mycc",
"chaincodePath": "github.com/example_cc/go",
"chaincodeType": "golang",
"chaincodeVersion": "v0"
};
const response = await fetch(url, { method: "POST", headers, body:JSON.stringify(body) });
return await response.json();
}
export async function instantiateChaincode(token) {
const url = 'http://47.100.192.19:4010/channels/mychannel/chaincodes';
const headers = { "Authorization": "Bearer " + token, "Content-Type": "application/json" };
const body = {
"peers": ["peer0.org1.example.com", "peer1.org1.example.com" ],
"chaincodeName": "mycc",
"chaincodeVersion": "v0",
"chaincodeType": "golang",
"args": ["a", "100", "b", "200"]
};
const response = await fetch(url, { method: "POST", headers, body: JSON.stringify(body) });
return await response.json();
}
export async function invoke(token) {
const url = 'http://47.100.192.19:4010/channels/mychannel/chaincodes/mycc';
const headers = { "Authorization": "Bearer " + token, "Content-Type": "application/json" };
const body = {
"peers": ["peer0.org1.example.com", "peer1.org1.example.com" ],
"fcn": "move",
"args": ["a", "b", "10"]
};
const response = await fetch(url, { method: "POST", headers, body: JSON.stringify(body) });
return await response.json();
}
export async function query(token) {
const url = 'http://47.100.192.19:4010/channels/mychannel/chaincodes/mycc?peer=peer0.org1.example.com&fcn=query&args=%5B%22a%22%5D';
const headers = { "Authorization": "Bearer " + token, "Content-Type": "application/json" };
const response = await fetch(url, {headers});
return await response.json();
}
export async function queryBlock(token,id) {
const url = 'http://47.100.192.19:4010/channels/mychannel/blocks/'+id+'?peer=peer0.org1.example.com';
const headers = { "Authorization": "Bearer " + token, "Content-Type": "application/json" };
const response = await fetch(url, { headers });
return await response.json();
}
export async function queryTraction(token, id) {
const url = 'http://47.100.192.19:4010/channels/mychannel/transactions/' + id + '?peer=peer0.org1.example.com';
const headers = { "Authorization": "Bearer " + token, "Content-Type": "application/json" };
const response = await fetch(url, { headers });
return await response.json();
}
export async function queryChainInfo(token) {
const url = 'http://47.100.192.19:4010/channels/mychannel?peer=peer0.org1.example.com';
const headers = { "Authorization": "Bearer " + token, "Content-Type": "application/json" };
const response = await fetch(url, { headers });
return await response.json();
}
export async function queryInstalledChaincode(token) {
const url = "http://47.100.192.19:4010/chaincodes?peer=peer0.org1.example.com&type=installed";
const headers = { "Authorization": "Bearer " + token, "Content-Type": "application/json" };
const response = await fetch(url, { headers });
return await response.json();
}
export async function queryInstantiatedChaincode(token) {
const url = "http://47.100.192.19:4010/chaincodes?peer=peer0.org1.example.com&type=instantiated";
const headers = { "Authorization": "Bearer " + token, "Content-Type": "application/json" };
const response = await fetch(url, { headers });
return await response.json();
}
export async function queryChannels(token) {
const url = "http://47.100.192.19:4010/channels?peer=peer0.org1.example.com";
const headers = { "Authorization": "Bearer " + token, "Content-Type": "application/json" };
const response = await fetch(url, { headers });
return await response.json();
}