/
ckb.ts
381 lines (364 loc) · 14.3 KB
/
ckb.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import { CkbDb } from '../db';
import { CkbMint, ICkbBurn } from '../db/model';
import { logger } from '../utils/logger';
import { asyncSleep, fromHexString, toHexString, uint8ArrayToString, bigintToSudtAmount } from '../utils';
import { Asset, BtcAsset, ChainType, EosAsset, EthAsset, TronAsset } from '../ckb/model/asset';
import { Address, Amount, AddressType, Script, HashType } from '@lay2/pw-core';
import { Account } from '@force-bridge/ckb/model/accounts';
import { CkbTxGenerator } from '@force-bridge/ckb/tx-helper/generator';
import { IndexerCollector } from '@force-bridge/ckb/tx-helper/collector';
import { ScriptType } from '@force-bridge/ckb/tx-helper/indexer';
import { ForceBridgeCore } from '@force-bridge/core';
import Transaction = CKBComponents.Transaction;
import { Script as LumosScript } from '@ckb-lumos/base';
import { BigNumber } from 'ethers';
import { RecipientCellData } from '@force-bridge/ckb/tx-helper/generated/eth_recipient_cell';
import { getAssetTypeByAsset } from '@force-bridge/xchain/tron/utils';
// CKB handler
// 1. Listen CKB chain to get new burn events.
// 2. Listen database to get new mint events, send tx.
export class CkbHandler {
private ckb = ForceBridgeCore.ckb;
private indexer = ForceBridgeCore.indexer;
private PRI_KEY = ForceBridgeCore.config.ckb.privateKey;
constructor(private db: CkbDb) {}
// save unlock event first and then
async saveBurnEvent(burns: ICkbBurn[]): Promise<void> {
logger.debug('save burn event:', burns);
for (const burn of burns) {
switch (burn.chain) {
case ChainType.BTC:
await this.db.createBtcUnlock([
{
ckbTxHash: burn.ckbTxHash,
asset: burn.asset,
amount: burn.amount,
chain: burn.chain,
recipientAddress: burn.recipientAddress,
},
]);
break;
case ChainType.ETH:
await this.db.createEthUnlock([
{
ckbTxHash: burn.ckbTxHash,
asset: burn.asset,
amount: burn.amount,
recipientAddress: burn.recipientAddress,
},
]);
break;
case ChainType.TRON:
await this.db.createTronUnlock([
{
ckbTxHash: burn.ckbTxHash,
asset: burn.asset,
assetType: getAssetTypeByAsset(burn.asset),
amount: burn.amount,
recipientAddress: burn.recipientAddress,
},
]);
break;
case ChainType.EOS:
await this.db.createEosUnlock([
{
ckbTxHash: burn.ckbTxHash,
asset: burn.asset,
amount: burn.amount,
recipientAddress: burn.recipientAddress,
},
]);
break;
default:
throw new Error(`wrong burn chain type: ${burn.chain}`);
}
await this.db.createCkbBurn([burn]);
}
}
async watchBurnEvents(): Promise<never> {
// get cursor from db, usually the block height, to start the poll or subscribe
// invoke saveBurnEvent when get new one
let latestHeight = await this.db.getCkbLatestHeight();
while (true) {
logger.debug('watch burn event height: ', latestHeight);
const block = await this.ckb.rpc.getBlockByNumber(BigInt(latestHeight));
if (block == null) {
logger.debug('waitting for new ckb block');
await asyncSleep(5000);
continue;
}
const burnTxs = new Map();
for (const tx of block.transactions) {
const recipientData = tx.outputsData[0];
logger.debug('recipientData:', recipientData);
let cellData;
try {
cellData = new RecipientCellData(fromHexString(recipientData).buffer);
} catch (e) {
continue;
}
if (await this.isBurnTx(tx, cellData)) {
burnTxs.set(tx.hash, cellData);
}
}
logger.debug('get new burn events and save to db', burnTxs);
if (burnTxs.size > 0) {
const ckbBurns = [];
burnTxs.forEach((v: RecipientCellData, k: string) => {
const chain = v.getChain();
let burn;
switch (chain) {
case ChainType.BTC:
burn = {
ckbTxHash: k,
asset: uint8ArrayToString(new Uint8Array(v.getAsset().raw())),
chain,
amount: Amount.fromUInt128LE(`0x${toHexString(new Uint8Array(v.getAmount().raw()))}`).toString(),
recipientAddress: uint8ArrayToString(new Uint8Array(v.getRecipientAddress().raw())),
blockNumber: latestHeight,
};
break;
case ChainType.ETH:
burn = {
ckbTxHash: k,
asset: `0x${toHexString(new Uint8Array(v.getAsset().raw()))}`,
chain,
amount: Amount.fromUInt128LE(`0x${toHexString(new Uint8Array(v.getAmount().raw()))}`).toString(),
recipientAddress: `0x${toHexString(new Uint8Array(v.getRecipientAddress().raw()))}`,
blockNumber: latestHeight,
};
break;
case ChainType.TRON:
burn = {
ckbTxHash: k,
asset: uint8ArrayToString(new Uint8Array(v.getAsset().raw())),
chain,
amount: Amount.fromUInt128LE(`0x${toHexString(new Uint8Array(v.getAmount().raw()))}`).toString(),
recipientAddress: uint8ArrayToString(new Uint8Array(v.getRecipientAddress().raw())),
blockNumber: latestHeight,
};
break;
case ChainType.EOS:
burn = {
ckbTxHash: k,
asset: uint8ArrayToString(new Uint8Array(v.getAsset().raw())),
chain,
amount: Amount.fromUInt128LE(`0x${toHexString(new Uint8Array(v.getAmount().raw()))}`).toString(),
recipientAddress: uint8ArrayToString(new Uint8Array(v.getRecipientAddress().raw())),
blockNumber: latestHeight,
};
break;
}
ckbBurns.push(burn);
});
await this.saveBurnEvent(ckbBurns);
}
latestHeight++;
await asyncSleep(1000);
}
}
async isBurnTx(tx: Transaction, cellData: RecipientCellData): Promise<boolean> {
if (tx.outputs.length < 1) {
return false;
}
const ownLockHash = await this.getOwnLockHash();
logger.debug('amount: ', toHexString(new Uint8Array(cellData.getAmount().raw())));
logger.debug('recipient address: ', toHexString(new Uint8Array(cellData.getRecipientAddress().raw())));
logger.debug('asset: ', toHexString(new Uint8Array(cellData.getAsset().raw())));
logger.debug('chain: ', cellData.getChain());
let asset;
const assetAddress = toHexString(new Uint8Array(cellData.getAsset().raw()));
switch (cellData.getChain()) {
case ChainType.BTC:
asset = new BtcAsset(uint8ArrayToString(fromHexString(assetAddress)), ownLockHash);
break;
case ChainType.ETH:
asset = new EthAsset(`0x${assetAddress}`, ownLockHash);
break;
case ChainType.TRON:
asset = new TronAsset(uint8ArrayToString(fromHexString(assetAddress)), ownLockHash);
break;
case ChainType.EOS:
asset = new EosAsset(uint8ArrayToString(fromHexString(assetAddress)), ownLockHash);
break;
default:
return false;
}
// verify tx input: sudt cell.
const preHash = tx.inputs[0].previousOutput.txHash;
const txPrevious = await this.ckb.rpc.getTransaction(preHash);
if (txPrevious == null) {
return false;
}
const sudtType = txPrevious.transaction.outputs[Number(tx.inputs[0].previousOutput.index)].type;
const expectType = {
codeHash: ForceBridgeCore.config.ckb.deps.sudtType.script.codeHash,
hashType: ForceBridgeCore.config.ckb.deps.sudtType.script.hashType,
args: this.getBridgeLockHash(asset),
};
logger.debug('expectType:', expectType);
logger.debug('sudtType:', sudtType);
if (sudtType == null || expectType.codeHash != sudtType.codeHash || expectType.args != sudtType.args) {
return false;
}
// verify tx output recipientLockscript: recipient cell.
const recipientScript = tx.outputs[0].type;
const expect = ForceBridgeCore.config.ckb.deps.recipientType.script;
logger.debug('recipientScript:', recipientScript);
logger.debug('expect:', expect);
return recipientScript.codeHash == expect.codeHash;
}
async handleMintRecords(): Promise<never> {
const account = new Account(this.PRI_KEY);
const ownLockHash = await this.getOwnLockHash();
const generator = new CkbTxGenerator(this.ckb, new IndexerCollector(this.indexer));
while (true) {
const mintRecords = await this.db.getCkbMintRecordsToMint();
logger.debug('new mintRecords: ', mintRecords);
if (mintRecords.length == 0) {
logger.debug('wait for new mint records');
await asyncSleep(3000);
continue;
}
const records = mintRecords.map((r) => this.filterMintRecords(r, ownLockHash));
const newTokens = await this.filterNewTokens(records);
if (newTokens.length > 0) {
logger.debug('bridge cell is not exist. do create bridge cell.');
await this.createBridgeCell(newTokens, generator);
}
try {
mintRecords.map((r) => {
r.status = 'pending';
});
await this.db.updateCkbMint(mintRecords);
const rawTx = await generator.mint(await account.getLockscript(), records);
const signedTx = this.ckb.signTransaction(this.PRI_KEY)(rawTx);
const mintTxHash = await this.ckb.rpc.sendTransaction(signedTx);
console.log(`Mint Transaction has been sent with tx hash ${mintTxHash}`);
const txStatus = await this.waitUntilCommitted(mintTxHash, 60);
if (txStatus.txStatus.status === 'committed') {
mintRecords.map((r) => {
r.status = 'success';
});
} else {
mintRecords.map((r) => {
r.status = 'error';
});
logger.error('mint execute failed: ', mintRecords);
}
await this.db.updateCkbMint(mintRecords);
} catch (e) {
logger.debug('mint execute failed:', e.toString());
mintRecords.map((r) => {
r.status = 'error';
});
await this.db.updateCkbMint(mintRecords);
}
await this.indexer.waitUntilSync();
}
}
filterMintRecords(r: CkbMint, ownLockHash: string): any {
switch (r.chain) {
case ChainType.BTC:
return {
asset: new BtcAsset(r.asset, ownLockHash),
recipient: new Address(r.recipientLockscript, AddressType.ckb),
amount: new Amount(r.amount),
};
case ChainType.ETH:
return {
asset: new EthAsset(r.asset, ownLockHash),
recipient: new Address(uint8ArrayToString(fromHexString(r.recipientLockscript)), AddressType.ckb),
amount: new Amount(BigNumber.from(r.amount).toString()),
};
case ChainType.TRON:
return {
asset: new TronAsset(r.asset, ownLockHash),
amount: new Amount(r.amount),
recipient: new Address(r.recipientLockscript, AddressType.ckb),
};
case ChainType.EOS:
return {
asset: new EosAsset(r.asset, ownLockHash),
amount: new Amount(r.amount),
recipient: new Address(r.recipientLockscript, AddressType.ckb),
};
default:
throw new Error('asset not supported!');
}
}
async filterNewTokens(records: any[]): Promise<any[]> {
const newTokens = [];
for (const record of records) {
logger.debug('record:', record);
const bridgeCellLockscript = {
codeHash: ForceBridgeCore.config.ckb.deps.bridgeLock.script.codeHash,
hashType: ForceBridgeCore.config.ckb.deps.bridgeLock.script.hashType,
args: record.asset.toBridgeLockscriptArgs(),
};
logger.debug('record: bridgeCellLockscript ', bridgeCellLockscript);
const searchKey = {
script: new Script(
bridgeCellLockscript.codeHash,
bridgeCellLockscript.args,
<HashType>bridgeCellLockscript.hashType,
).serializeJson() as LumosScript,
script_type: ScriptType.lock,
};
const bridgeCells = await this.indexer.getCells(searchKey);
if (bridgeCells.length == 0) {
newTokens.push(record);
}
}
return newTokens;
}
async createBridgeCell(newTokens: any[], generator: CkbTxGenerator) {
const account = new Account(this.PRI_KEY);
const scripts = newTokens.map((r) => {
return {
codeHash: ForceBridgeCore.config.ckb.deps.bridgeLock.script.codeHash,
hashType: HashType.data,
args: r.asset.toBridgeLockscriptArgs(),
};
});
const rawTx = await generator.createBridgeCell(await account.getLockscript(), scripts);
const signedTx = this.ckb.signTransaction(this.PRI_KEY)(rawTx);
const tx_hash = await this.ckb.rpc.sendTransaction(signedTx);
await this.waitUntilCommitted(tx_hash, 60);
await this.indexer.waitUntilSync();
}
async getOwnLockHash(): Promise<string> {
const account = new Account(this.PRI_KEY);
const ownLockHash = this.ckb.utils.scriptToHash(<CKBComponents.Script>await account.getLockscript());
return ownLockHash;
}
getBridgeLockHash(asset: Asset): string {
const bridgeCellLockscript = {
codeHash: ForceBridgeCore.config.ckb.deps.bridgeLock.script.codeHash,
hashType: ForceBridgeCore.config.ckb.deps.bridgeLock.script.hashType,
args: asset.toBridgeLockscriptArgs(),
};
const bridgeLockHash = this.ckb.utils.scriptToHash(<CKBComponents.Script>bridgeCellLockscript);
return bridgeLockHash;
}
async waitUntilCommitted(txHash: string, timeout: number) {
let waitTime = 0;
while (true) {
const txStatus = await this.ckb.rpc.getTransaction(txHash);
logger.debug(`tx ${txHash} status: ${txStatus.txStatus.status}, index: ${waitTime}`);
if (txStatus.txStatus.status === 'committed') {
return txStatus;
}
await asyncSleep(1000);
waitTime += 1;
if (waitTime >= timeout) {
return txStatus;
}
}
}
start(): void {
this.watchBurnEvents();
this.handleMintRecords();
logger.info('ckb handler started 🚀');
}
}