-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouterExample.mjs
209 lines (171 loc) · 7.25 KB
/
routerExample.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
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 testRegister() {
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 testSubmit(msgID) {
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("greeting", mtonflow.MsgType.cdcVecString, ['hello nika', 'I\'m moon'],
await fcl.config.get('Profile'));
const msgPayload = new mtonflow.MessagePayload([msgItem], await fcl.config.get('Profile'));
const session = new mtonflow.Session(0, 0, 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 {
const addr = Buffer.alloc(8, 0);
addr[addr.length - 1] = 0x34;
addr[addr.length - 2] = 0x12;
let rstData = await flowService.executeScripts({
script: script,
args: [
fcl.arg(msgID, types.UInt128),
fcl.arg('POLKADOT', 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('0x01cf0e2f2f715450', types.Address),
// normally, use `Buffer.from('interface on Flow', 'utf8')` when messages sent to Flow
fcl.arg('GreetingRecver', 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, types.UInt128),
fcl.arg('POLKADOT', 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('0x01cf0e2f2f715450', types.Address),
// normally, use `Buffer.from('interface on Flow', 'utf8')` when messages sent to Flow
fcl.arg('GreetingRecver', 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('0x01cf0e2f2f715450', 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 testSignatureToNormalString() {
const message = 'hello nika';
const msgBuf = Buffer.from(message, 'utf-8');
// The two input `msg`s below are the same
const signature = flowService.sign2string(msgBuf.toString('hex'));
console.log(signature);
console.log(flowService.sign2string(msgBuf));
const sha = new SHA3(256);
sha.update(Buffer.from(message, 'hex'));
console.log(sha.digest());
sha.update(Buffer.from(msgBuf, 'hex'));
console.log(sha.digest());
// Check `omnichainCrypto`
const keyPair = ['bb499b3527649d37f86d4a16e83aae2f9bd64de510077cf8c5fcb12eafc96c93a0425ac965ce4eb2cc2dd5a350569f10035b4308aadfc544415ddc812f919025',
'69e7e51ead557351ade7a575e947c4d4bd19dd8a6cdf00c51f9c7f6f721b72dc'];
const testOC = new oc.OmnichainCrypto(sha3_256FromString, 'p256', keyPair);
const signatureOC = testOC.sign2hexstring(msgBuf)
console.log(signatureOC);
console.log(testOC.verify(msgBuf, testOC.sign(msgBuf)));
}
async function signData() {
const message = 'f8d6e0586b0a20c70165267ecaa60bd88374b73bca0c8fce7da9d8ebd81744bb891859a6db8d30d4db';
const signature = flowService.sign2string(message);
console.log(signature);
}
// query sent message count
async function getSentMessageCount(toChain) {
const scriptID = fs.readFileSync(
path.join(
process.cwd(),
'./scripts/send-recv-message/currentSentMessageID.cdc'
),
'utf8'
);
let rstData = await flowService.executeScripts({
script: scriptID,
args: [
fcl.arg(toChain, types.String)
]
});
return rstData;
}
// await testRegister();
// await testSubmit(args[2]);
// await testSignatureToNormalString();
// await signData();
console.log(await getSentMessageCount('nika'));