-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlatform.js
More file actions
244 lines (234 loc) · 14.6 KB
/
Platform.js
File metadata and controls
244 lines (234 loc) · 14.6 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
const Web3 = require('web3');
var web3 = new Web3('http://127.0.0.1:8545');
// var my_addr = "0x86c923e0e2fad5862be4a552be46693ff84aa7cd"
// var pwd = "cmq19950520"
function getContract() {
var abi = [{ "inputs": [{ "internalType": "address", "name": "Addr", "type": "address" }], "stateMutability": "nonpayable", "type": "constructor" }, { "anonymous": false, "inputs": [{ "indexed": true, "internalType": "bytes32", "name": "targetCustom", "type": "bytes32" }, { "indexed": false, "internalType": "address", "name": "owner", "type": "address" }, { "indexed": false, "internalType": "string", "name": "responseAddr", "type": "string" }], "name": "customResponse", "type": "event" }, { "anonymous": false, "inputs": [{ "indexed": true, "internalType": "bytes32", "name": "targetData", "type": "bytes32" }, { "indexed": false, "internalType": "address", "name": "buyer", "type": "address" }, { "indexed": false, "internalType": "address", "name": "owner", "type": "address" }, { "indexed": false, "internalType": "bool", "name": "listing", "type": "bool" }, { "indexed": false, "internalType": "string", "name": "pubKey", "type": "string" }], "name": "txRequest", "type": "event" }, { "anonymous": false, "inputs": [{ "indexed": true, "internalType": "bytes32", "name": "txHash", "type": "bytes32" }, { "indexed": false, "internalType": "string", "name": "dataAddr", "type": "string" }], "name": "txResponse", "type": "event" }, { "inputs": [{ "internalType": "bytes32", "name": "h", "type": "bytes32" }, { "internalType": "uint8", "name": "v", "type": "uint8" }, { "internalType": "bytes32", "name": "r", "type": "bytes32" }, { "internalType": "bytes32", "name": "s", "type": "bytes32" }, { "internalType": "address", "name": "addr", "type": "address" }], "name": "checkSign", "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "internalType": "bytes32", "name": "", "type": "bytes32" }], "name": "customDict", "outputs": [{ "internalType": "bool", "name": "stillNeed", "type": "bool" }, { "internalType": "address", "name": "buyer", "type": "address" }, { "internalType": "uint256", "name": "price", "type": "uint256" }, { "internalType": "string", "name": "modelAddr", "type": "string" }, { "internalType": "string", "name": "dataName", "type": "string" }, { "internalType": "string", "name": "dataInfo", "type": "string" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "customLength", "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], "stateMutability": "view", "type": "function" }, { "inputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], "name": "customList", "outputs": [{ "internalType": "bytes32", "name": "", "type": "bytes32" }], "stateMutability": "view", "type": "function" }, { "inputs": [{ "internalType": "uint256", "name": "price", "type": "uint256" }, { "internalType": "string", "name": "modelAddr", "type": "string" }, { "internalType": "string", "name": "dataName", "type": "string" }, { "internalType": "string", "name": "dataInfo", "type": "string" }], "name": "customizeDataset", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "internalType": "bytes32", "name": "customHash", "type": "bytes32" }, { "internalType": "string", "name": "responseAddr", "type": "string" }, { "internalType": "uint8", "name": "v", "type": "uint8" }, { "internalType": "bytes32", "name": "r", "type": "bytes32" }, { "internalType": "bytes32", "name": "s", "type": "bytes32" }], "name": "customizedResponse", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "internalType": "bytes32", "name": "txHash", "type": "bytes32" }], "name": "isFinished", "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }], "stateMutability": "view", "type": "function" }, { "inputs": [{ "internalType": "bool", "name": "listing", "type": "bool" }, { "internalType": "bytes32", "name": "targetData", "type": "bytes32" }, { "internalType": "address", "name": "owner", "type": "address" }, { "internalType": "string", "name": "pubKey", "type": "string" }], "name": "phurchaseRequest", "outputs": [], "stateMutability": "payable", "type": "function" }, { "inputs": [{ "internalType": "bytes32", "name": "txHash", "type": "bytes32" }, { "internalType": "string", "name": "dataAddr", "type": "string" }, { "internalType": "uint8", "name": "v", "type": "uint8" }, { "internalType": "bytes32", "name": "r", "type": "bytes32" }, { "internalType": "bytes32", "name": "s", "type": "bytes32" }], "name": "phurchaseResponse", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "internalType": "bytes32", "name": "customHash", "type": "bytes32" }], "name": "retrieveCustom", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "internalType": "bytes32", "name": "txHash", "type": "bytes32" }], "name": "retrievePhurchase", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "internalType": "bytes32", "name": "sellHash", "type": "bytes32" }], "name": "retrieveSale", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "internalType": "uint256", "name": "price", "type": "uint256" }, { "internalType": "string", "name": "modelAddr", "type": "string" }, { "internalType": "string", "name": "dataName", "type": "string" }, { "internalType": "string", "name": "dataInfo", "type": "string" }, { "internalType": "uint8", "name": "v", "type": "uint8" }, { "internalType": "bytes32", "name": "r", "type": "bytes32" }, { "internalType": "bytes32", "name": "s", "type": "bytes32" }], "name": "sellDataset", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "internalType": "bytes32", "name": "", "type": "bytes32" }], "name": "sellDict", "outputs": [{ "internalType": "bool", "name": "onSale", "type": "bool" }, { "internalType": "address", "name": "owner", "type": "address" }, { "internalType": "uint256", "name": "price", "type": "uint256" }, { "internalType": "string", "name": "modelAddr", "type": "string" }, { "internalType": "string", "name": "dataName", "type": "string" }, { "internalType": "string", "name": "dataInfo", "type": "string" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "sellLength", "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], "stateMutability": "view", "type": "function" }, { "inputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], "name": "sellList", "outputs": [{ "internalType": "bytes32", "name": "", "type": "bytes32" }], "stateMutability": "view", "type": "function" }, { "inputs": [{ "internalType": "bytes32", "name": "", "type": "bytes32" }], "name": "txDict", "outputs": [{ "internalType": "bool", "name": "finished", "type": "bool" }, { "internalType": "bool", "name": "listing", "type": "bool" }, { "internalType": "uint256", "name": "price", "type": "uint256" }, { "internalType": "bytes32", "name": "targetData", "type": "bytes32" }, { "internalType": "address", "name": "buyer", "type": "address" }, { "internalType": "address", "name": "owner", "type": "address" }], "stateMutability": "view", "type": "function" }];
var address = '0x3D6D8C59F6ea394EF6a3EEe62A2C154B5B9737e9';
var myContract = new web3.eth.Contract(abi, {
gasPrice: '20000'
});
myContract.options.address = address;
return myContract;
}
//var myContract = getContract();
function sellData(myContract, my_addr, pwd, price, modelAddr, dataName, dataInfo) {
var sha = web3.utils.soliditySha3(my_addr, price, modelAddr, dataInfo);
var pri_key = '0x2533d8712b12382a629105fcea607c64ec97bc246413059c5bfbb3742540a42a';
//var pub_addr = '0xac76a0786e57A4396c18a951a95e603c4300b236';
var sig = web3.eth.accounts.sign(sha, pri_key);
var v = sig.v;
var r = sig.r;
var s = sig.s;
return new Promise(function (resolve, reject) {
web3.eth.personal.unlockAccount(my_addr, pwd).then(function () {
myContract.methods.sellDataset(price, modelAddr, dataName, dataInfo, v, r, s).send({
from: my_addr, gas: 2000000
}).then(function () {
console.log('sellData suc');
resolve(sha);
});
}).catch((err) => { reject(err) });
});
}
function customizeDataset(myContract, my_addr, pwd, price, modelAddr, dataName, dataInfo) {
var sha = web3.utils.soliditySha3(my_addr, price, modelAddr, dataInfo);
return new Promise(function (resolve, reject) {
web3.eth.personal.unlockAccount(my_addr, pwd).then(function () {
myContract.methods.customizeDataset(price, modelAddr, dataName, dataInfo).send({
from: my_addr, gas: 2000000
}).then(function () {
console.log('customizeDataset suc');
resolve(sha);
});
}).catch((err) => { reject(err) });
});
}
function customizedResponse(myContract, my_addr, pwd, phurchaseHash, responseAddr) {
var sha = web3.utils.soliditySha3(phurchaseHash, responseAddr, my_addr);
var pri_key = '0x2533d8712b12382a629105fcea607c64ec97bc246413059c5bfbb3742540a42a';
var sig = web3.eth.accounts.sign(sha, pri_key);
var v = sig.v;
var r = sig.r;
var s = sig.s;
return new Promise(function (resolve, reject) {
web3.eth.personal.unlockAccount(my_addr, pwd).then(function () {
myContract.methods.customizedResponse(phurchaseHash, responseAddr, v, r, s).send({
from: my_addr, gas: 2000000
}).then(function () {
console.log('customizedResponse suc');
resolve();
});
}).catch((err) => { reject(err) });
});
}
function getAllDataset(myContract) {
return new Promise(function (resolve, reject) {
var datasetList = [];
myContract.methods.sellLength().call().then(async function (length) {
if (length > datasetList.length) {
for (var i = 0; i < length; i++) {
var dataHash = await myContract.methods.sellList(i).call();
var datasetObj = await myContract.methods.sellDict(dataHash).call()
datasetObj['dataHash'] = dataHash;
datasetList.push(datasetObj);
}
}
console.log('getAllDataset suc');
resolve(datasetList);
}).catch(function (err) {
reject(err);
});
});
}
function getAllCustom(myContract) {
return new Promise(function (resolve, reject) {
let customList = [];
myContract.methods.customLength().call().then(async function (length) {
if (length > customList.length) {
for (var i = 0; i < length; i++) {
let customHash = await myContract.methods.customList(i).call();
let customObj = await myContract.methods.customDict(customHash).call()
customObj['customHash'] = customHash;
customList.push(customObj);
}
}
console.log('getAllCustom suc');
resolve(customList);
}).catch(function (err) {
reject(err);
});
});
}
function getTxHash(listing, targetData, buyer, owner) {
return web3.utils.soliditySha3(listing, targetData, buyer, owner);
}
function getSellInfo(myContract, dataHash) {
return myContract.methods.sellDict(dataHash).call();
}
function getCustomInfo(myContract, customHash) {
return myContract.methods.customDict(customHash).call();
}
function getTxInfo(myContract, txHash) {
return myContract.methods.txDict(txHash).call();
}
function phurchaseRequest(myContract, my_addr, pwd, price, listing, targetData, owner, pubKey) {
return new Promise(function (resolve, reject) {
web3.eth.personal.unlockAccount(my_addr, pwd).then(function () {
myContract.methods.phurchaseRequest(listing, targetData, owner, pubKey).send({
from: my_addr, gas: 1000000, value: price
}).then(function () {
var txHash = web3.utils.soliditySha3(listing, targetData, my_addr, owner);
console.log('phurchaseRequest suc');
resolve(txHash);
});
}).catch(function (err) { reject(err); });
});
}
function phurchaseResponse(myContract, my_addr, pwd, txHash, dataAddr) {
return new Promise(function (resolve, reject) {
var sha = web3.utils.soliditySha3(txHash, dataAddr);
var pri_key = '0x2533d8712b12382a629105fcea607c64ec97bc246413059c5bfbb3742540a42a';
var sig = web3.eth.accounts.sign(sha, pri_key);
var v = sig.v;
var r = sig.r;
var s = sig.s;
web3.eth.personal.unlockAccount(my_addr, pwd).then(function () {
myContract.methods.phurchaseResponse(txHash, dataAddr, v, r, s).send({
from: my_addr, gas: 2000000
}).then(function () {
console.log('phurchaseResponse suc');
resolve();
});
}).catch(function (err) {
reject(err);
});
});
}
function checkCustomResponse(myContract, targetCustom){
return new Promise(function (resolve, reject) {
var responseList = [];
myContract.getPastEvents('customResponse', {
filter: { targetCustom: targetCustom },
fromBlock: 0,
toBlock: 'latest'
}).then(async function (events) {
for (let event of events) {
//检测交易是否已经结束
let owner = event.returnValues.owner;
let responseAddr = event.returnValues.responseAddr;
//responseAddr = responseAddr.replace(/\n/gm,"<br>");
responseList.push({
targetCustom:targetCustom,
owner:owner,
responseAddr:responseAddr
});
}
resolve(responseList);
}).catch(function (err) { reject(err); });
});
}
function checkTxRequest(myContract, targetData) {
return new Promise(function (resolve, reject) {
var requestList = [];
myContract.getPastEvents('txRequest', {
filter: { targetData: targetData },
fromBlock: 0,
toBlock: 'latest'
}).then(async function (events) {
for (var event of events) {
//检测交易是否已经结束
var buyer = event.returnValues.buyer;
var owner = event.returnValues.owner;
var listing = event.returnValues.listing;
var pubKey = event.returnValues.pubKey;
var txHash = getTxHash(listing, targetData, buyer, owner);
var txInfo = await getTxInfo(myContract, txHash);
if (!txInfo.finished) {
requestList.push({
targetData: targetData,
buyer: buyer,
listing: listing,
pubKey: pubKey
});
}
}
console.log('checkTxRequest suc');
resolve(requestList);
}).catch(function (err) { reject(err); });
});
}
function checkTxResponse(myContract, txHash) {
return new Promise(function (resolve, reject) {
var responseList = [];
myContract.getPastEvents('txResponse', {
filter: { txHash: txHash },
fromBlock: 0,
toBlock: 'latest'
}).then(function (events) {
for (var event of events) {
responseList.push(event.returnValues);
}
//这里可能会有无数个响应,我们先把所有的都保存,默认只使用第一个
console.log('checkTxResponse suc')
resolve(responseList);
}).catch(function (err) { reject(err); });
});
}
module.exports = {
getContract,
sellData,
customizeDataset,
customizedResponse,
getAllDataset,
getAllCustom,
getTxHash,
getSellInfo,
getCustomInfo,
getTxInfo,
phurchaseRequest,
phurchaseResponse,
checkCustomResponse,
checkTxRequest,
checkTxResponse,
}