Skip to content

Commit 1ce2e12

Browse files
committed
Implement the registerWithDCAP api call to be used by Crust Sworker.
Note: When the Crust-API talks to Crust Mainnet for the entrynetwork registration, we re-use the sworker.registerWithDeauthChain extrinsic in the Crust Mainnet, so that we don't need to change the mainnet code.
1 parent 64f0bba commit 1ce2e12

File tree

7 files changed

+73
-13
lines changed

7 files changed

+73
-13
lines changed

docs/api.md

+28
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,34 @@ Response:
166166
}
167167
```
168168

169+
#### Register with decentralized auth chain as sWorker
170+
171+
Request:
172+
173+
```shell
174+
curl --request POST 'http://localhost:56666/api/v1/swork/registerWithDCAP' \
175+
--header 'Content-Type: application/json' \
176+
--header 'password: 123456' \
177+
--data-raw '{
178+
"who" : "0xa6efa374700f8640b777bc92c77d34447c5588d7eb7c4ec984323c7db0983009",
179+
"code" : "0x34633130383435666561376230353433646264333965656236626434376434623735383131663565356435313666353164623263373564343439353961633761",
180+
"pubkey" : "0x3061376637646531626635303538636161366239363661346435303738326139663261656137393536313735303234373734373136343264636433623132313363386562643663303162633737333062613432353563373938316463656437626432343464633735323063353936303534663031323361383833346333313139",
181+
"pubkeys" : ["0x328846691dd2401b2a62b123daea0e6f626cb4919dc560797645d26e3273a57a", "0x2ec91af63632573a5b051376cdeb79730261e696117e68c67aa298d519f0c77c"],
182+
"sig" : "1f9ddc0ec3cb13c6b779924e78ebf4036c6c063a691649a18955f871feeac36a3204ee0c88a26249cfb9702ccb69e4e3d8de7720f1cdf185fbc24351a8c4e548",
183+
"sigs" : ["0x08c8b9c428add1c886d3c7485b36c2b3eea61c74a651e162ec2c53af7f97b67422a0b70d4b4e2eba35a6c01ccdd95a40527f9e9670ef8f1a109a43648479218a",
184+
"0x1831dd101ed60ce5193a5b9ad95cc4792b735490f36398c4e2536ca7c9d88945bb40636f2313a0906b24f9e46dd2ba4c18c8a0fd772caa1b878af9398deb0a89"]
185+
"backup" : "{\"address\":\"5FqazaU79hjpEMiWTWZx81VjsYFst15eBuSBKdQLgQibD7CX\",\"encoded\":\"0xc81537c9442bd1d3f4985531293d88f6d2a960969a88b1cf8413e7c9ec1d5f4955adf91d2d687d8493b70ef457532d505b9cee7a3d2b726a554242b75fb9bec7d4beab74da4bf65260e1d6f7a6b44af4505bf35aaae4cf95b1059ba0f03f1d63c5b7c3ccbacd6bd80577de71f35d0c4976b6e43fe0e1583530e773dfab3ab46c92ce3fa2168673ba52678407a3ef619b5e14155706d43bd329a5e72d36\",\"encoding\":{\"content\":[\"pkcs8\",\"sr25519\"],\"type\":\"xsalsa20-poly1305\",\"version\":\"2\"},\"meta\":{\"name\":\"Yang1\",\"tags\":[],\"whenCreated\":1580628430860}}"
186+
}'
187+
```
188+
189+
Response:
190+
191+
```json
192+
{
193+
"status": "success",
194+
}
195+
```
196+
169197
#### Send sWorker work report
170198

171199
Request:

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
"fix": "gts fix"
1414
},
1515
"dependencies": {
16-
"@crustio/type-definitions": "1.1.0",
17-
"@polkadot/api": "4.2.2-4",
16+
"@crustio/type-definitions": "1.3.0",
17+
"@polkadot/api": "6.0.5",
1818
"body-parser": "^1.19.0",
1919
"connect-timeout": "^1.9.0",
2020
"express": "^4.17.1",

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ app.get('/api/v1/market/file', services.market.file);
5959

6060
// Post routes
6161
app.post('/api/v1/swork/identity', services.swork.register);
62+
app.post('/api/v1/swork/registerWithDCAP', services.swork.registerWithDCAP);
6263
app.post('/api/v1/swork/workreport', services.swork.reportWorks);
6364

6465
// Error handler

src/services/chain.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export async function blockHash(api: ApiPromise, bn: number) {
2020

2121
export async function health(api: ApiPromise) {
2222
logger.info('⛓ [chain]: Query system health');
23-
const h = await api.rpc.system.health();
23+
const h = (await api.rpc.system.health()) as any;
2424
const ch: CrustHealth = {
2525
isSyncing: h.isSyncing.isTrue,
2626
peers: h.peers.toNumber(),
@@ -29,9 +29,9 @@ export async function health(api: ApiPromise) {
2929

3030
// HEALTH PATCH: This is for the poor syncing process
3131
if (!ch.isSyncing) {
32-
const h_before = await header(api);
32+
const h_before = (await header(api)) as any;
3333
await sleep(3000);
34-
const h_after = await header(api);
34+
const h_after = (await header(api)) as any;
3535
if (h_before.number.toNumber() + 1 < h_after.number.toNumber()) {
3636
ch.isSyncing = true;
3737
}

src/services/index.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {Request, Response, NextFunction} from 'express';
22
import {ApiPromise, WsProvider} from '@polkadot/api';
33
import {typesBundleForPolkadot} from '@crustio/type-definitions';
44
import {blockHash, header, health} from './chain';
5-
import {register, reportWorks, workReport, code, identity} from './swork';
5+
import {register, reportWorks, workReport, code, identity, registerWithDCAP } from './swork';
66
import {file} from './market';
77
import {loadKeyringPair, resHandler, withApiReady} from './util';
88
import {logger} from '../log';
@@ -39,7 +39,7 @@ export const getApi = (): ApiPromise => {
3939
export const chain = {
4040
header: (_: Request, res: Response, next: NextFunction) => {
4141
withApiReady(async (api: ApiPromise) => {
42-
const h = await header(api);
42+
const h = (await header(api)) as any;
4343
res.json({
4444
number: h.number,
4545
hash: h.hash,
@@ -65,6 +65,12 @@ export const swork = {
6565
await resHandler(register(api, krp, req), res);
6666
}, next);
6767
},
68+
registerWithDCAP: (req: Request, res: Response, next: NextFunction) => {
69+
withApiReady(async (api: ApiPromise) => {
70+
const krp = loadKeyringPair(req);
71+
await resHandler(registerWithDCAP(api, krp, req), res);
72+
}, next);
73+
},
6874
reportWorks: (req: Request, res: Response, next: NextFunction) => {
6975
withApiReady(async (api: ApiPromise) => {
7076
const krp = loadKeyringPair(req);

src/services/swork.ts

+22-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,27 @@ export async function register(
2323
'0x' + req.body['sig']
2424
);
2525

26-
return handleSworkTxWithLock(async () => sendTx(tx, krp));
26+
return handleSworkTxWithLock(async () => sendTx(api, tx, krp));
27+
}
28+
29+
export async function registerWithDCAP(
30+
api: ApiPromise,
31+
krp: KeyringPair,
32+
req: Request
33+
) {
34+
logger.info(`⚙️ [swork]: Call register With DCAP with ${JSON.stringify(req.body)}`);
35+
// Re-use the registerWithDeauthChain extrinsic in the Crust Mainnet, so that we don't need to change
36+
// the mainnet code.
37+
const tx = api.tx.swork.registerWithDeauthChain(
38+
req.body['who'],
39+
req.body['code'],
40+
req.body['pubkeys'],
41+
req.body['sigs'],
42+
req.body['pubkey'],
43+
req.body['sig']
44+
);
45+
46+
return handleSworkTxWithLock(async () => sendTx(api, tx, krp));
2747
}
2848

2949
export async function reportWorks(
@@ -57,7 +77,7 @@ export async function reportWorks(
5777
);
5878

5979
let txRes = queryToObj(
60-
await handleSworkTxWithLock(async () => sendTx(tx, krp))
80+
await handleSworkTxWithLock(async () => sendTx(api, tx, krp))
6181
);
6282

6383
// Double confirm of tx status

src/services/util.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {SubmittableExtrinsic} from '@polkadot/api/promise/types';
88
import {timeout} from 'promise-timeout';
99
import {TxRes, getApi} from './index';
1010
import {logger} from '../log';
11+
import {ApiPromise, WsProvider} from '@polkadot/api';
1112

1213
const txLocker = {swork: false};
1314
/**
@@ -24,7 +25,11 @@ export function loadKeyringPair(req: Request): KeyringPair {
2425
return krp;
2526
}
2627

27-
export async function sendTx(tx: SubmittableExtrinsic, krp: KeyringPair) {
28+
export async function sendTx(
29+
api: ApiPromise,
30+
tx: SubmittableExtrinsic,
31+
krp: KeyringPair
32+
) {
2833
return new Promise((resolve, reject) => {
2934
tx.signAndSend(krp, ({events = [], status}) => {
3035
logger.info(
@@ -40,19 +45,19 @@ export async function sendTx(tx: SubmittableExtrinsic, krp: KeyringPair) {
4045
if (status.isInBlock) {
4146
events.forEach(({event: {data, method, section}}) => {
4247
if (section === 'system' && method === 'ExtrinsicFailed') {
43-
const [dispatchError] = data as unknown as ITuple<[DispatchError]>;
48+
const [dispatchError] = (data as unknown) as ITuple<[DispatchError]>;
4449
const result: TxRes = {
4550
status: 'failed',
4651
message: dispatchError.type,
4752
};
4853
// Can get detail error info
4954
if (dispatchError.isModule) {
5055
const mod = dispatchError.asModule;
51-
const error = getApi().registry.findMetaError(
56+
const error = api.registry.findMetaError(
5257
new Uint8Array([mod.index.toNumber(), mod.error.toNumber()])
5358
);
5459
result.message = `${error.section}.${error.name}`;
55-
result.details = error.documentation.join('');
60+
result.details = error.docs.join('');
5661
}
5762

5863
logger.info(

0 commit comments

Comments
 (0)