-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulator.mjs
322 lines (271 loc) · 11.8 KB
/
simulator.mjs
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import FlowService from './flowoffchain.mjs'
import fcl from '@onflow/fcl';
import * as types from "@onflow/types";
import { SHA3 } from 'sha3';
import fs from 'fs';
import path from 'path';
import * as mtonflow from './messageTypesOnFlow.js';
import oc from './omnichainCrypto.js'
const args = process.argv;
const sha3_256FromString = (msg) => {
const sha = new SHA3(256);
sha.update(Buffer.from(msg, 'hex'));
return sha.digest();
};
const flowService = new FlowService('0xf8d6e0586b0a20c7',
'69e7e51ead557351ade7a575e947c4d4bd19dd8a6cdf00c51f9c7f6f721b72dc',
0,
sha3_256FromString,
'p256');
async function getNextSubmittionID(router, recver, fromChain) {
const scriptID = fs.readFileSync(
path.join(
process.cwd(),
'./scripts/send-recv-message/queryNextSubmitMessage.cdc'
),
'utf8'
);
let rstData = await flowService.executeScripts({
script: scriptID,
args: [
fcl.arg(router, types.Address)
]
});
var msgID = 1;
for (var eleIdx in rstData[fromChain]) {
if (rstData[fromChain][eleIdx].recver == recver) {
msgID = Number(rstData[fromChain][eleIdx].id);
break;
}
}
return msgID;
}
async function simuRegister() {
const script = fs.readFileSync(
path.join(
process.cwd(),
'./scripts/send-recv-message/queryRegisterRouter.cdc'
),
'utf8'
);
let rstData = await flowService.executeScripts({
script: script,
args: [
fcl.arg('0xf8d6e0586b0a20c7', types.Address)
]
});
console.log(rstData);
// this can be verified by Flow CLI
const signature = flowService.sign2string(rstData);
console.log(signature);
const tras = fs.readFileSync(
path.join(
process.cwd(),
'./transactions/registerRouter.cdc'
),
'utf8'
);
let response = await flowService.sendTx({
transaction: tras,
args: [
fcl.arg(signature, types.String)
]
});
console.log(response);
}
async function submitSimuCompute(fromChain, contractName, actionName, session, msgPayload) {
// console.log(fromChain);
// console.log(contractName);
// console.log(actionName);
// console.log(session);
// console.log(msgPayload);
const recver = '0x01cf0e2f2f715450';
const msgID = await getNextSubmittionID("0xf8d6e0586b0a20c7", recver, fromChain);
// console.log(msgID);
// return;
const sqosItem = new mtonflow.SQoSItem(mtonflow.SQoSType.SelectionDelay, [0x12, 0x34, 0x56, 0x78], await fcl.config.get('Profile'));
const InputSQoSArray = new mtonflow.SQoSItemArray([sqosItem], await fcl.config.get('Profile'));
const script = fs.readFileSync(
path.join(
process.cwd(),
'./scripts/crypto-dev/GenerateSubmittion.cdc'
),
'utf8'
);
try {
let rstData = await flowService.executeScripts({
script: script,
args: [
fcl.arg(msgID.toString(10), types.UInt128),
fcl.arg(fromChain, types.String),
fcl.arg(Array.from(Buffer.from('0101010101010101010101010101010101010101010101010101010101010101', 'hex')).map(num => {return String(num);}), types.Array(types.UInt8)),
fcl.arg(Array.from(Buffer.from('0101010101010101010101010101010101010101010101010101010101010101', 'hex')).map(num => {return String(num);}), types.Array(types.UInt8)),
InputSQoSArray.get_fcl_arg(),
fcl.arg(contractName, types.Address),
// normally, use `Buffer.from('interface on Flow', 'utf8')` when messages sent to Flow
fcl.arg(actionName, types.String),
msgPayload.get_fcl_arg(),
session.get_fcl_arg(),
fcl.arg('0xf8d6e0586b0a20c7', types.Address)
]
});
console.log(rstData.toBeSign);
const toBeSign = rstData.toBeSign;
// this can be verified by Flow CLI
const signature = flowService.sign2string(toBeSign);
console.log(signature);
const tras = fs.readFileSync(
path.join(
process.cwd(),
'./transactions/send-recv-message/submitRecvedMessage.cdc'
),
'utf8'
);
let response = await flowService.sendTx({
transaction: tras,
args: [
fcl.arg(msgID.toString(10), types.UInt128),
fcl.arg(fromChain, types.String),
fcl.arg(Array.from(Buffer.from('0101010101010101010101010101010101010101010101010101010101010101', 'hex')).map(num => {return String(num);}), types.Array(types.UInt8)),
fcl.arg(Array.from(Buffer.from('0101010101010101010101010101010101010101010101010101010101010101', 'hex')).map(num => {return String(num);}), types.Array(types.UInt8)),
InputSQoSArray.get_fcl_arg(),
fcl.arg(contractName, types.Address),
// normally, use `Buffer.from('interface on Flow', 'utf8')` when messages sent to Flow
fcl.arg(actionName, types.String),
msgPayload.get_fcl_arg(),
session.get_fcl_arg(),
fcl.arg('0xf8d6e0586b0a20c7', types.Address),
fcl.arg(signature, types.String),
// In official version, the address below shall be the same as `resourceAccount`
fcl.arg(recver, types.Address),
// and the link shall be got from `CrossChain.registeredRecvAccounts`
fcl.arg('receivedMessageVault', types.String)
]
});
console.log(response);
} catch (error) {
console.error(error);
}
}
async function simulateServer(chain, msgID) {
const script = fs.readFileSync(
path.join(
process.cwd(),
'./scripts/send-recv-message/querySendMessageByID.cdc'
),
'utf8'
);
let rstData = await flowService.executeScripts({
script: script,
args: [
fcl.arg(chain, types.String),
fcl.arg(msgID, types.UInt128)
]
});
// console.log(rstData);
// console.log(Buffer.from(rstData.session.callback, 'utf-8').toString('utf-8'));
var numbers;
for (var eleIdx in rstData.data.items) {
if (rstData.data.items[eleIdx].name == 'nums') {
numbers = rstData.data.items[eleIdx].value;
break;
}
}
var sum = 0;
if (numbers != undefined) {
const calcArray = new Uint32Array(numbers);
sum = calcArray.reduce((a, b) => {return a + b}), 0;
}
// console.log(sum);
const fromChain = rstData.toChain;
const contractName = '0x' + Buffer.from(rstData.sender, 'utf-8').toString('hex');
const actionName = Buffer.from(rstData.session.callback, 'utf-8').toString('utf-8');
const session = new mtonflow.Session(Number(rstData.session.id),
Number(rstData.session.type),
await fcl.config.get('Profile'),
new Uint8Array(rstData.session.callback));
const msgItem = new mtonflow.MessageItem("result", mtonflow.MsgType.cdcU32, sum,
await fcl.config.get('Profile'));
const msgPayload = new mtonflow.MessagePayload([msgItem], await fcl.config.get('Profile'));
await submitSimuCompute(fromChain, contractName, actionName, session, msgPayload);
}
async function simuRequest() {
const recver = '0x01cf0e2f2f715450';
const fromChain = 'POLKADOT';
const contractName = recver;
const actionName = 'computationServer';
const msgID = await getNextSubmittionID("0xf8d6e0586b0a20c7", recver, fromChain);
// console.log(msgID);
// return;
const sqosItem = new mtonflow.SQoSItem(mtonflow.SQoSType.SelectionDelay, [0x12, 0x34, 0x56, 0x78], await fcl.config.get('Profile'));
const InputSQoSArray = new mtonflow.SQoSItemArray([sqosItem], await fcl.config.get('Profile'));
const msgItem = new mtonflow.MessageItem("nums", mtonflow.MsgType.cdcVecU32, [11, 12, 13, 14, 15],
await fcl.config.get('Profile'));
const msgPayload = new mtonflow.MessagePayload([msgItem], await fcl.config.get('Profile'));
const session = new mtonflow.Session(99, 2, await fcl.config.get('Profile'), new Uint8Array([0x11, 0x11, 0x11, 0x11]), new Uint8Array([0x22]), new Uint8Array([0x33]));
const script = fs.readFileSync(
path.join(
process.cwd(),
'./scripts/crypto-dev/GenerateSubmittion.cdc'
),
'utf8'
);
try {
let rstData = await flowService.executeScripts({
script: script,
args: [
fcl.arg(msgID.toString(10), types.UInt128),
fcl.arg(fromChain, types.String),
fcl.arg(Array.from(Buffer.from('0101010101010101010101010101010101010101010101010101010101010101', 'hex')).map(num => {return String(num);}), types.Array(types.UInt8)),
fcl.arg(Array.from(Buffer.from('0101010101010101010101010101010101010101010101010101010101010101', 'hex')).map(num => {return String(num);}), types.Array(types.UInt8)),
InputSQoSArray.get_fcl_arg(),
fcl.arg(contractName, types.Address),
// normally, use `Buffer.from('interface on Flow', 'utf8')` when messages sent to Flow
fcl.arg(actionName, types.String),
msgPayload.get_fcl_arg(),
session.get_fcl_arg(),
fcl.arg('0xf8d6e0586b0a20c7', types.Address)
]
});
console.log(rstData.toBeSign);
const toBeSign = rstData.toBeSign;
// this can be verified by Flow CLI
const signature = flowService.sign2string(toBeSign);
console.log(signature);
const tras = fs.readFileSync(
path.join(
process.cwd(),
'./transactions/send-recv-message/submitRecvedMessage.cdc'
),
'utf8'
);
let response = await flowService.sendTx({
transaction: tras,
args: [
fcl.arg(msgID.toString(10), types.UInt128),
fcl.arg(fromChain, types.String),
fcl.arg(Array.from(Buffer.from('0101010101010101010101010101010101010101010101010101010101010101', 'hex')).map(num => {return String(num);}), types.Array(types.UInt8)),
fcl.arg(Array.from(Buffer.from('0101010101010101010101010101010101010101010101010101010101010101', 'hex')).map(num => {return String(num);}), types.Array(types.UInt8)),
InputSQoSArray.get_fcl_arg(),
fcl.arg(contractName, types.Address),
// normally, use `Buffer.from('interface on Flow', 'utf8')` when messages sent to Flow
fcl.arg(actionName, types.String),
msgPayload.get_fcl_arg(),
session.get_fcl_arg(),
fcl.arg('0xf8d6e0586b0a20c7', types.Address),
fcl.arg(signature, types.String),
// In official version, the address below shall be the same as `resourceAccount`
fcl.arg(recver, types.Address),
// and the link shall be got from `CrossChain.registeredRecvAccounts`
fcl.arg('receivedMessageVault', types.String)
]
});
console.log(response);
} catch (error) {
console.error(error);
}
}
// await simuRegister();
// await simuRequest();
await simulateServer(args[2], args[3]);
// await simuRequest();