From 90b7b25d7bd555350c8002fae4424cafccdff06f Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Thu, 11 Apr 2024 15:11:02 +0800 Subject: [PATCH 1/2] [worker] setShieldingKey internally when it is fetched --- packages/worker-api/src/integriteeWorker.ts | 5 ++--- packages/worker-api/src/worker.ts | 4 +++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/worker-api/src/integriteeWorker.ts b/packages/worker-api/src/integriteeWorker.ts index db37118d..eaf1cab4 100644 --- a/packages/worker-api/src/integriteeWorker.ts +++ b/packages/worker-api/src/integriteeWorker.ts @@ -45,11 +45,10 @@ export class IntegriteeWorker extends Worker { return this.sendTrustedCall(call, shard, options); } - async sendTrustedCall(call: IntegriteeTrustedCallSigned, shard: ShardIdentifier, options: CallOptions = {} as CallOptions): Promise { + async sendTrustedCall(call: IntegriteeTrustedCallSigned, shard: ShardIdentifier, options: CallOptions = {} as CallOptions): Promise { if (this.shieldingKey() == undefined) { - const key = await this.getShieldingKey(options); console.log(`Setting the shielding pubKey of the worker.`) - this.setShieldingKey(key); + await this.getShieldingKey(options); } return sendTrustedCall(this, call, shard, true, 'TrustedOperationResult', options); diff --git a/packages/worker-api/src/worker.ts b/packages/worker-api/src/worker.ts index 575d62f6..ecc3891b 100644 --- a/packages/worker-api/src/worker.ts +++ b/packages/worker-api/src/worker.ts @@ -148,7 +148,9 @@ export class Worker extends WebSocketAsPromised implements IWorker { } public async getShieldingKey(options: CallOptions = {} as CallOptions): Promise { - return await callGetter(this, [Request.Worker, 'author_getShieldingKey', 'NodeRSA'], {}, options) + const key = await callGetter(this, [Request.Worker, 'author_getShieldingKey', 'NodeRSA'], {}, options) + this.setShieldingKey(key); + return key; } public async getShardVault(options: CallOptions = {} as CallOptions): Promise { From dbe36189c27976725be5d6ed22e99fdea172a5d2 Mon Sep 17 00:00:00 2001 From: Christian Langenbacher Date: Thu, 11 Apr 2024 15:14:11 +0800 Subject: [PATCH 2/2] [worker] internally create strong types for trusted calls --- .../worker-api/src/integriteeWorker.spec.ts | 20 ++++++++++++---- packages/worker-api/src/integriteeWorker.ts | 23 ++++++++++++++++--- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/packages/worker-api/src/integriteeWorker.spec.ts b/packages/worker-api/src/integriteeWorker.spec.ts index a01435ff..e3bb7957 100644 --- a/packages/worker-api/src/integriteeWorker.spec.ts +++ b/packages/worker-api/src/integriteeWorker.spec.ts @@ -76,8 +76,14 @@ describe('worker', () => { describe('balance transfer should work', () => { it('should return value', async () => { const shard = worker.createType('ShardIdentifier', bs58.decode(network.mrenclave)); - const params = worker.createType('BalanceTransferArgs', [alice.address, charlie.address, 1100000000000]) - const result = await worker.trustedBalanceTransfer(alice, shard, network.mrenclave, params); + const result = await worker.trustedBalanceTransfer( + alice, + shard, + network.mrenclave, + alice.address, + charlie.address, + 1100000000000 + ); console.log('balance transfer result', result.toHuman()); expect(result).toBeDefined(); }); @@ -86,8 +92,14 @@ describe('worker', () => { describe('balance unshield should work', () => { it('should return value', async () => { const shard = worker.createType('ShardIdentifier', bs58.decode(network.mrenclave)); - const params = worker.createType('BalanceUnshieldArgs', [alice.address, charlie.address, 1100000000000, shard]) - const result = await worker.balanceUnshieldFunds(alice, shard, network.mrenclave, params); + const result = await worker.balanceUnshieldFunds( + alice, + shard, + network.mrenclave, + alice.address, + charlie.address, + 1100000000000 + ); console.log('balance unshield result', result.toHuman()); expect(result).toBeDefined(); }); diff --git a/packages/worker-api/src/integriteeWorker.ts b/packages/worker-api/src/integriteeWorker.ts index eaf1cab4..7efb9781 100644 --- a/packages/worker-api/src/integriteeWorker.ts +++ b/packages/worker-api/src/integriteeWorker.ts @@ -7,7 +7,6 @@ import NodeRSA from 'node-rsa'; import type {KeyringPair} from '@polkadot/keyring/types'; import type {Balance, Hash} from '@polkadot/types/interfaces/runtime'; import type { - BalanceTransferArgs, BalanceUnshieldArgs, ShardIdentifier, IntegriteeTrustedCallSigned, } from '@encointer/types'; @@ -33,14 +32,32 @@ export class IntegriteeWorker extends Worker { }, options) } - public async trustedBalanceTransfer(accountOrPubKey: KeyringPair | PubKeyPinPair, shard: ShardIdentifier, mrenclave: string, params: BalanceTransferArgs, options: CallOptions = {} as CallOptions): Promise { + public async trustedBalanceTransfer( + accountOrPubKey: KeyringPair | PubKeyPinPair, + shard: ShardIdentifier, + mrenclave: string, + from: String, + to: String, + amount: number, + options: CallOptions = {} as CallOptions + ): Promise { const nonce = await this.getNonce(accountOrPubKey, mrenclave, options); + const params = this.createType('BalanceTransferArgs', [from, to, amount]) const call = createTrustedCall(this, ['balance_transfer', 'BalanceTransferArgs'], accountOrPubKey, shard, mrenclave, nonce, params); return this.sendTrustedCall(call, shard, options); } - public async balanceUnshieldFunds(accountOrPubKey: KeyringPair | PubKeyPinPair, shard: ShardIdentifier, mrenclave: string, params: BalanceUnshieldArgs, options: CallOptions = {} as CallOptions): Promise { + public async balanceUnshieldFunds( + accountOrPubKey: KeyringPair | PubKeyPinPair, + shard: ShardIdentifier, + mrenclave: string, + fromIncognitoAddress: string, + toPublicAddress: string, + amount: number, + options: CallOptions = {} as CallOptions + ): Promise { const nonce = await this.getNonce(accountOrPubKey, mrenclave, options); + const params = this.createType('BalanceUnshieldArgs', [fromIncognitoAddress, toPublicAddress, amount, shard]) const call = createTrustedCall(this, ['balance_unshield', 'BalanceUnshieldArgs'], accountOrPubKey, shard, mrenclave, nonce, params); return this.sendTrustedCall(call, shard, options); }