Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internalize some boilerplate code revolving around creating strong types #96

Merged
merged 2 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading