Skip to content

Commit

Permalink
extract generic top to worker base implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
clangenb committed Oct 25, 2024
1 parent 6012548 commit 7c19e64
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 42 deletions.
33 changes: 4 additions & 29 deletions packages/worker-api/src/integriteeWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Hash> {
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);
}
}

Expand Down
19 changes: 10 additions & 9 deletions packages/worker-api/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<u8>>
registry: () => TypeRegistry
}

export interface GenericGetter {
toU8a(): Uint8Array,
toHex(): string
}

export interface GenericTop {
toU8a(): Uint8Array,
toHex(): string
}

export interface ISubmittableGetter<W extends IWorkerBase, Type> {

worker: W;
Expand Down Expand Up @@ -65,8 +71,3 @@ export interface PublicGetterArgs {
}

export type PublicGetterParams = GuessTheNumberPublicGetter | null

export interface RequestOptions {
timeout?: number;
debug?: boolean;
}
6 changes: 3 additions & 3 deletions packages/worker-api/src/testUtils/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}
};
Expand Down
22 changes: 21 additions & 1 deletion packages/worker-api/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -143,6 +144,25 @@ export class Worker implements IWorkerBase {
return this.createType(returnType, value);
}

async submitAndWatchTop<Top extends GenericTop>(top: Top, shard: ShardIdentifier): Promise<Hash> {

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<RpcReturnValue> {
await this.isReady();
Expand Down

0 comments on commit 7c19e64

Please sign in to comment.