From 7c19e6420ede22fae01081bb6bdbf865d960908f Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Fri, 25 Oct 2024 20:18:30 +0200 Subject: [PATCH] extract generic top to worker base implementation --- packages/worker-api/src/integriteeWorker.ts | 33 +++---------------- packages/worker-api/src/interface.ts | 19 ++++++----- packages/worker-api/src/testUtils/networks.ts | 6 ++-- packages/worker-api/src/worker.ts | 22 ++++++++++++- 4 files changed, 38 insertions(+), 42 deletions(-) diff --git a/packages/worker-api/src/integriteeWorker.ts b/packages/worker-api/src/integriteeWorker.ts index 79ab4019..b14c42da 100644 --- a/packages/worker-api/src/integriteeWorker.ts +++ b/packages/worker-api/src/integriteeWorker.ts @@ -133,36 +133,11 @@ export class IntegriteeWorker extends Worker { await this.getShieldingKey(); } - return this.submitAndWatch(call, shard, true); - } - - async submitAndWatch(call: IntegriteeTrustedCallSigned, shard: ShardIdentifier, direct: boolean): Promise { - let top; - if (direct) { - top = this.createType('IntegriteeTrustedOperation', { - direct_call: call - }) - } else { - top = this.createType('IntegriteeTrustedOperation', { - indirect_call: call - }) - } - - console.debug(`Sending TrustedOperation: ${JSON.stringify(top)}`); - - const cyphertext = await this.encrypt(top.toU8a()); - - const r = this.createType( - 'Request', { shard, cyphertext: cyphertext } - ); - - const returnValue = await this.subscribe('author_submitAndWatchExtrinsic', [r.toHex()]) - - // const returnValue = await this.send('author_submitExtrinsic', [r.toHex()]) - - console.debug(`[sendTrustedCall] result: ${JSON.stringify(returnValue)}`); + const top = this.createType('IntegriteeTrustedOperation', { + direct_call: call + }) - return this.createType('Hash', returnValue.value); + return this.submitAndWatchTop(top, shard); } } diff --git a/packages/worker-api/src/interface.ts b/packages/worker-api/src/interface.ts index ea0e5868..fbcaa5f6 100644 --- a/packages/worker-api/src/interface.ts +++ b/packages/worker-api/src/interface.ts @@ -10,16 +10,22 @@ import type { ShardIdentifier } from "@encointer/types"; -export interface GenericGetter { - toHex(): string -} - export interface IWorkerBase { createType: (apiType: string, obj?: any) => any; encrypt: (data: Uint8Array) => Promise> registry: () => TypeRegistry } +export interface GenericGetter { + toU8a(): Uint8Array, + toHex(): string +} + +export interface GenericTop { + toU8a(): Uint8Array, + toHex(): string +} + export interface ISubmittableGetter { worker: W; @@ -65,8 +71,3 @@ export interface PublicGetterArgs { } export type PublicGetterParams = GuessTheNumberPublicGetter | null - -export interface RequestOptions { - timeout?: number; - debug?: boolean; -} diff --git a/packages/worker-api/src/testUtils/networks.ts b/packages/worker-api/src/testUtils/networks.ts index efe7fe45..786e3269 100644 --- a/packages/worker-api/src/testUtils/networks.ts +++ b/packages/worker-api/src/testUtils/networks.ts @@ -39,9 +39,9 @@ export const localDockerNetwork = () => { chain: 'ws://127.0.0.1:9944', worker: 'wss://127.0.0.1:2000', genesisHash: '0x388c446a804e24e77ae89f5bb099edb60cacc2ac7c898ce175bdaa08629c1439', - mrenclave: 'GopQCtWihHQ8Xw1sZP6DTWMDQuaijSBE2NM8GU2U4Erc', - shard: 'GopQCtWihHQ8Xw1sZP6DTWMDQuaijSBE2NM8GU2U4Erc', - chosenCid: 'GopQCtWihHQ8Xw1sZP6DTWMDQuaijSBE2NM8GU2U4Erc', + mrenclave: 'BrFk2gARyQxD56NLpDKVbZinDmD1Twt1GwMsEyQuWshx', + shard: 'BrFk2gARyQxD56NLpDKVbZinDmD1Twt1GwMsEyQuWshx', + chosenCid: 'BrFk2gARyQxD56NLpDKVbZinDmD1Twt1GwMsEyQuWshx', customTypes: {}, palletOverrides: {} }; diff --git a/packages/worker-api/src/worker.ts b/packages/worker-api/src/worker.ts index bc060bbc..e805d8e3 100644 --- a/packages/worker-api/src/worker.ts +++ b/packages/worker-api/src/worker.ts @@ -13,12 +13,13 @@ import type { Vault } from '@encointer/types'; -import {type GenericGetter, type IWorkerBase, type WorkerOptions} from './interface.js'; +import {type GenericGetter, type GenericTop, type IWorkerBase, type WorkerOptions} from './interface.js'; import {encryptWithPublicKey, parseWebCryptoRSA} from "./webCryptoRSA.js"; import type {Bytes, u8} from "@polkadot/types-codec"; import BN from "bn.js"; import {WsProvider} from "./rpc-provider/src/index.js"; import {Keyring} from "@polkadot/keyring"; +import type {Hash} from "@polkadot/types/interfaces/runtime"; export class Worker implements IWorkerBase { @@ -143,6 +144,25 @@ export class Worker implements IWorkerBase { return this.createType(returnType, value); } + async submitAndWatchTop(top: Top, shard: ShardIdentifier): Promise { + + console.debug(`Sending TrustedOperation: ${JSON.stringify(top)}`); + + const cyphertext = await this.encrypt(top.toU8a()); + + const r = this.createType( + 'Request', { shard, cyphertext: cyphertext } + ); + + const returnValue = await this.subscribe('author_submitAndWatchExtrinsic', [r.toHex()]) + + // const returnValue = await this.send('author_submitExtrinsic', [r.toHex()]) + + console.debug(`[sendTrustedCall] result: ${JSON.stringify(returnValue)}`); + + return this.createType('Hash', returnValue.value); + } + public async send(method: string, params: unknown[]): Promise { await this.isReady();