-
Notifications
You must be signed in to change notification settings - Fork 2
/
7.3.getComputeResult.js
118 lines (107 loc) · 3.24 KB
/
7.3.getComputeResult.js
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
const { ProviderInstance, Aquarius } = require('@oceanprotocol/lib');
const Web3 = require('web3');
const { web3Provider, oceanConfig, web3Instance } = require('./config');
// const HDWalletProvider = require('@truffle/hdwallet-provider');
const web3 = new Web3(web3Provider);
const aquarius = new Aquarius(oceanConfig.metadataCacheUri);
const providerUrl = oceanConfig.providerUri;
const getData = async (url) => {
return fetch(url, {
method: 'GET',
headers: {
'Content-type': 'application/json'
}
});
};
const signProviderRequest = async (web3, accountId, message, password) => {
const consumerMessage = web3.utils.soliditySha3({
t: 'bytes',
v: web3.utils.utf8ToHex(message)
});
const isMetaMask =
web3 && web3.currentProvider && web3.currentProvider.isMetaMask;
if (isMetaMask)
return await web3.eth.personal.sign(consumerMessage, accountId, password);
else
return await web3.eth.accounts.sign(
consumerMessage,
process.env.PRIVATE_KEY
);
};
const getEResult = async () => {
const res = await getComputeResultUrl(
providerUrl,
web3Instance,
'0xdF1dEc52e602020E27B0644Ea0F584b6Eb5CE4eA',
'd1345d72477649f8a2bda2062c4c17f9',
0
);
console.log('res', res);
};
const getEndpointURL = (servicesEndpoints, serviceName) => {
if (!servicesEndpoints) return null;
return servicesEndpoints.find((s) => s.serviceName === serviceName);
};
const getEndpoints = async (providerUri) => {
try {
const endpoints = await getData(providerUri);
return await endpoints.json();
} catch (e) {
// LoggerInstance.error('Finding the service endpoints failed:', e);
console.error(e);
throw new Error('HTTP request failed calling Provider');
}
};
const getServiceEndpoints = async (providerEndpoint, endpoints) => {
const serviceEndpoints = [];
for (const i in endpoints.serviceEndpoints) {
const endpoint = {
serviceName: i,
method: endpoints.serviceEndpoints[i][0],
urlPath: providerEndpoint + endpoints.serviceEndpoints[i][1]
};
serviceEndpoints.push(endpoint);
}
return serviceEndpoints;
};
const getComputeResultUrl = async (
providerUri,
web3,
consumerAddress,
jobId,
index
) => {
const providerEndpoints = await getEndpoints(providerUri);
const serviceEndpoints = await getServiceEndpoints(
providerUri,
providerEndpoints
);
const computeResultUrl = getEndpointURL(serviceEndpoints, 'computeResult')
? getEndpointURL(serviceEndpoints, 'computeResult').urlPath
: null;
const nonce = Date.now();
let signatureMessage = consumerAddress;
signatureMessage += jobId;
signatureMessage += index.toString();
signatureMessage += nonce;
const signature = (
await signProviderRequest(web3, consumerAddress, signatureMessage)
)['signature'];
console.log(signature);
if (!computeResultUrl) return null;
let resultUrl = computeResultUrl;
resultUrl += `?consumerAddress=${consumerAddress}`;
resultUrl += `&jobId=${jobId}`;
resultUrl += `&index=${index.toString()}`;
resultUrl += `&nonce=${nonce}`;
resultUrl += (signature && `&signature=${signature}`) || '';
return resultUrl;
};
getEResult()
.then(() => {
process.exit();
})
.catch((err) => {
console.error(err);
process.exit(-1);
});