Skip to content

Commit

Permalink
internalize some boilerplate code revolving around creating strong ty…
Browse files Browse the repository at this point in the history
…pes (#96)

* [worker] setShieldingKey internally when it is fetched

* [worker] internally create strong types for trusted calls
  • Loading branch information
clangenb authored Apr 11, 2024
1 parent 0eb6c7b commit 3e21bfa
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
20 changes: 16 additions & 4 deletions packages/worker-api/src/integriteeWorker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand All @@ -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();
});
Expand Down
28 changes: 22 additions & 6 deletions packages/worker-api/src/integriteeWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -33,23 +32,40 @@ export class IntegriteeWorker extends Worker {
}, options)
}

public async trustedBalanceTransfer(accountOrPubKey: KeyringPair | PubKeyPinPair, shard: ShardIdentifier, mrenclave: string, params: BalanceTransferArgs, options: CallOptions = {} as CallOptions): Promise<Hash> {
public async trustedBalanceTransfer(
accountOrPubKey: KeyringPair | PubKeyPinPair,
shard: ShardIdentifier,
mrenclave: string,
from: String,
to: String,
amount: number,
options: CallOptions = {} as CallOptions
): Promise<Hash> {
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<Hash> {
public async balanceUnshieldFunds(
accountOrPubKey: KeyringPair | PubKeyPinPair,
shard: ShardIdentifier,
mrenclave: string,
fromIncognitoAddress: string,
toPublicAddress: string,
amount: number,
options: CallOptions = {} as CallOptions
): Promise<Hash> {
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);
}

async sendTrustedCall(call: IntegriteeTrustedCallSigned, shard: ShardIdentifier, options: CallOptions = {} as CallOptions): Promise<Hash> {
async sendTrustedCall(call: IntegriteeTrustedCallSigned, shard: ShardIdentifier, options: CallOptions = {} as CallOptions): Promise<Hash> {
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<Hash>(this, call, shard, true, 'TrustedOperationResult', options);
Expand Down
4 changes: 3 additions & 1 deletion packages/worker-api/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ export class Worker extends WebSocketAsPromised implements IWorker {
}

public async getShieldingKey(options: CallOptions = {} as CallOptions): Promise<NodeRSA> {
return await callGetter<NodeRSA>(this, [Request.Worker, 'author_getShieldingKey', 'NodeRSA'], {}, options)
const key = await callGetter<NodeRSA>(this, [Request.Worker, 'author_getShieldingKey', 'NodeRSA'], {}, options)
this.setShieldingKey(key);
return key;
}

public async getShardVault(options: CallOptions = {} as CallOptions): Promise<Vault> {
Expand Down

0 comments on commit 3e21bfa

Please sign in to comment.