Skip to content

Commit

Permalink
Add account info getter and enclave finger print getter (#112)
Browse files Browse the repository at this point in the history
* update integritee trusted getter

* add `getAccountInfo` getter

* fix readme command

* fix log in tests

* add `author_getFingerprint`

* debug author_getFingerprint

* fix author_getFingerprint

* fix author_getFingerprint for good

* skip integration tests

* v0.15.1-alpha.0

* skip tests
  • Loading branch information
clangenb authored Oct 9, 2024
1 parent aa12422 commit f32b2e8
Show file tree
Hide file tree
Showing 17 changed files with 77 additions and 83 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"publishConfig": {
"directory": "build"
},
"version": "0.15.0"
"version": "0.15.1-alpha.0"
}
4 changes: 2 additions & 2 deletions packages/node-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
},
"sideEffects": false,
"type": "module",
"version": "0.15.0",
"version": "0.15.1-alpha.0",
"main": "index.js",
"dependencies": {
"@encointer/types": "^0.15.0",
"@encointer/types": "^0.15.1-alpha.0",
"@polkadot/api": "^11.2.1",
"tslib": "^2.6.2"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/types/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export default {
};
```

A subsequent `yarn generate:defs` updates the types.
A subsequent `yarn generate:types` updates the types.

To update the polkadot types, upgrade the `@polkadot/types` package and generate the types again.

The type generation process is elaborated on in the polkadot.{js} [docs](https://polkadot.js.org/docs/api/examples/promise/typegen#types-setup).
The type generation process is elaborated on in the polkadot.{js} [docs](https://polkadot.js.org/docs/api/examples/promise/typegen#types-setup).
2 changes: 1 addition & 1 deletion packages/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"sideEffects": false,
"type": "module",
"version": "0.15.0",
"version": "0.15.1-alpha.0",
"main": "index.js",
"scripts": {
"generate:defs": "node --experimental-specifier-resolution=node --loader ts-node/esm ../../node_modules/.bin/polkadot-types-from-defs --package @encointer/types/interfaces --input ./src/interfaces",
Expand Down
3 changes: 2 additions & 1 deletion packages/types/src/interfaces/augment-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type { AnnouncementSigner, Bip340, CidDigest, CidName, CommunityCeremony,
import type { EncointerBalanceTransferArgs, EncointerGetter, EncointerGetterArgs, EncointerPublicGetter, EncointerTrustedCall, EncointerTrustedCallSigned, EncointerTrustedGetter, EncointerTrustedGetterSigned, GrantReputationArgs, RegisterAttestationsArgs, RegisterParticipantArgs } from '@encointer/types/interfaces/encointerWorker';
import type { BalanceSetBalanceArgs, BalanceShieldArgs, BalanceTransferArgs, BalanceUnshieldArgs, IntegriteeGetter, IntegriteePublicGetter, IntegriteeTrustedCall, IntegriteeTrustedCallSigned, IntegriteeTrustedGetter, IntegriteeTrustedGetterSigned, IntegriteeTrustedOperation, TimestampSetArgs } from '@encointer/types/interfaces/integriteeWorker';
import type { SchedulerState, SystemNumber } from '@encointer/types/interfaces/scheduler';
import type { DirectRequestStatus, Enclave, GetterArgs, ParentchainId, Request, RpcReturnValue, ShardIdentifier, TrustedOperationStatus, Vault, WorkerEncoded } from '@encointer/types/interfaces/worker';
import type { DirectRequestStatus, Enclave, EnclaveFingerprint, GetterArgs, ParentchainId, Request, RpcReturnValue, ShardIdentifier, TrustedOperationStatus, Vault, WorkerEncoded } from '@encointer/types/interfaces/worker';
import type { Data, StorageKey } from '@polkadot/types';
import type { BitVec, Bool, Bytes, F32, F64, I128, I16, I256, I32, I64, I8, ISize, Json, Null, OptionBool, Raw, Text, Type, U128, U16, U256, U32, U64, U8, USize, bool, f32, f64, i128, i16, i256, i32, i64, i8, isize, u128, u16, u256, u32, u64, u8, usize } from '@polkadot/types-codec';
import type { TAssetConversion } from '@polkadot/types/interfaces/assetConversion';
Expand Down Expand Up @@ -426,6 +426,7 @@ declare module '@polkadot/types/types/registry' {
ElectionSize: ElectionSize;
ElectionStatus: ElectionStatus;
Enclave: Enclave;
EnclaveFingerprint: EnclaveFingerprint;
EncodedFinalityProofs: EncodedFinalityProofs;
EncodedJustification: EncodedJustification;
EncointerBalanceTransferArgs: EncointerBalanceTransferArgs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ export default {
},
IntegriteeTrustedGetter: {
_enum: {
free_balance: 'AccountId',
reserved_balance: 'AccountId',
nonce: 'AccountId',
account_info: 'AccountId',
}
},
IntegriteeTrustedGetterSigned: {
Expand Down
10 changes: 3 additions & 7 deletions packages/types/src/interfaces/integriteeWorker/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,9 @@ export interface IntegriteeTrustedCallSigned extends Struct {

/** @name IntegriteeTrustedGetter */
export interface IntegriteeTrustedGetter extends Enum {
readonly isFreeBalance: boolean;
readonly asFreeBalance: AccountId;
readonly isReservedBalance: boolean;
readonly asReservedBalance: AccountId;
readonly isNonce: boolean;
readonly asNonce: AccountId;
readonly type: 'FreeBalance' | 'ReservedBalance' | 'Nonce';
readonly isAccountInfo: boolean;
readonly asAccountInfo: AccountId;
readonly type: 'AccountInfo';
}

/** @name IntegriteeTrustedGetterSigned */
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/interfaces/worker/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export default {
rpc: {},
types: {
ShardIdentifier: 'Hash',
EnclaveFingerprint: 'H256',
GetterArgs: '(AccountId, CommunityIdentifier)',
Enclave: {
pubkey: 'AccountId',
Expand Down
5 changes: 4 additions & 1 deletion packages/types/src/interfaces/worker/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import type { CommunityIdentifier } from '@encointer/types/interfaces/community';
import type { Bytes, Enum, Struct, Text, bool, u64 } from '@polkadot/types-codec';
import type { ITuple } from '@polkadot/types-codec/types';
import type { AccountId, Hash } from '@polkadot/types/interfaces/runtime';
import type { AccountId, H256, Hash } from '@polkadot/types/interfaces/runtime';

/** @name DirectRequestStatus */
export interface DirectRequestStatus extends Enum {
Expand All @@ -23,6 +23,9 @@ export interface Enclave extends Struct {
readonly url: Text;
}

/** @name EnclaveFingerprint */
export interface EnclaveFingerprint extends H256 {}

/** @name GetterArgs */
export interface GetterArgs extends ITuple<[AccountId, CommunityIdentifier]> {}

Expand Down
2 changes: 1 addition & 1 deletion packages/util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"sideEffects": false,
"type": "module",
"types": "./index.d.ts",
"version": "0.15.0",
"version": "0.15.1-alpha.0",
"main": "index.js",
"dependencies": {
"@babel/runtime": "^7.18.9",
Expand Down
8 changes: 4 additions & 4 deletions packages/worker-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
"sideEffects": false,
"type": "module",
"types": "./index.d.ts",
"version": "0.15.0",
"version": "0.15.1-alpha.0",
"main": "index.js",
"dependencies": {
"@encointer/node-api": "^0.15.0",
"@encointer/types": "^0.15.0",
"@encointer/util": "^0.15.0",
"@encointer/node-api": "^0.15.1-alpha.0",
"@encointer/types": "^0.15.1-alpha.0",
"@encointer/util": "^0.15.1-alpha.0",
"@peculiar/webcrypto": "^1.4.6",
"@polkadot/api": "^11.2.1",
"@polkadot/keyring": "^12.6.2",
Expand Down
33 changes: 12 additions & 21 deletions packages/worker-api/src/integriteeWorker.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Keyring } from '@polkadot/api';
import { cryptoWaitReady } from '@polkadot/util-crypto';
import { paseoNetwork} from './testUtils/networks.js';
import {localDockerNetwork} from './testUtils/networks.js';
import { IntegriteeWorker } from './integriteeWorker.js';
import WS from 'websocket';
import {type KeyringPair} from "@polkadot/keyring/types";

const {w3cwebsocket: WebSocket} = WS;

describe('worker', () => {
const network = paseoNetwork();
const network = localDockerNetwork();
let keyring: Keyring;
let worker: IntegriteeWorker;
let alice: KeyringPair;
Expand Down Expand Up @@ -56,38 +56,29 @@ describe('worker', () => {
});
});

describe('getBalance', () => {
describe('getNonce', () => {
it('should return value', async () => {
const result = await worker.getBalance(charlie, network.mrenclave);
console.log('getBalance toNumber:', result.toString(10));
const result = await worker.getNonce(alice, network.mrenclave);
console.log('Nonce', result);
expect(result).toBeDefined();
});
});

describe('getBalanceGetter', () => {
it('should return value', async () => {
const getter = await worker.getBalanceGetter(charlie, network.mrenclave);
console.log(`BalanceGetter: ${JSON.stringify(getter)}`);
const result = await getter.send();
console.log('getBalance toNumber:', result.toString(10));
expect(result).toBeDefined();
});
});

describe('getNonce', () => {
describe('getAccountInfo', () => {
it('should return value', async () => {
const result = await worker.getNonce(alice, network.mrenclave);
console.log('getNonce', result);
const result = await worker.getAccountInfo(alice, network.mrenclave);
console.log('getAccountInfo', result);
expect(result).toBeDefined();
});
});

describe('getNonceGetter', () => {
describe('getAccountInfoGetter', () => {
it('should return value', async () => {
const getter = await worker.getNonceGetter(charlie, network.mrenclave);
console.log(`NonceGetter: ${JSON.stringify(getter)}`);
const getter = await worker.getAccountInfoGetter(charlie, network.mrenclave);
console.log(`AccountInfoGetter: ${JSON.stringify(getter)}`);
const result = await getter.send();
console.log('getNonce', result);
console.log('getAccountInfo', result);
expect(result).toBeDefined();
});
});
Expand Down
39 changes: 14 additions & 25 deletions packages/worker-api/src/integriteeWorker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import type {u32} from '@polkadot/types';

import type {Balance, Hash} from '@polkadot/types/interfaces/runtime';
import type {Hash} from '@polkadot/types/interfaces/runtime';
import type {
ShardIdentifier, IntegriteeTrustedCallSigned, IntegriteeGetter,
} from '@encointer/types';
Expand All @@ -20,41 +18,31 @@ import {
} from "./requests.js";
import bs58 from "bs58";
import type {AddressOrPair} from "@polkadot/api-base/types/submittable";
import type { AccountInfo } from "@polkadot/types/interfaces/system";
import type {u32} from "@polkadot/types-codec";

export class IntegriteeWorker extends Worker {

public async getNonce(accountOrPubKey: AddressOrPair, shard: string, singerOptions?: TrustedSignerOptions, requestOptions?: RequestOptions): Promise<u32> {
return await callGetter<u32>(this, [Request.TrustedGetter, 'nonce', 'u32'], {
shard: shard,
account: accountOrPubKey,
signer: singerOptions?.signer
}, requestOptions)
const info = await this.getAccountInfo(accountOrPubKey, shard, singerOptions, requestOptions);
return info.nonce;
}

public async getNonceGetter(accountOrPubKey: AddressOrPair, shard: string, signerOptions?: TrustedSignerOptions): Promise<SubmittableGetter<IntegriteeWorker, Balance>> {
const trustedGetterArgs = {
public async getAccountInfo(accountOrPubKey: AddressOrPair, shard: string, singerOptions?: TrustedSignerOptions, requestOptions?: RequestOptions): Promise<AccountInfo> {
return await callGetter<AccountInfo>(this, [Request.TrustedGetter, 'account_info', 'AccountInfo'], {
shard: shard,
account: accountOrPubKey,
signer: signerOptions?.signer,
}
return await submittableGetter<IntegriteeWorker, Balance>(this, 'nonce', trustedGetterArgs,'u32');
}

public async getBalance(accountOrPubKey: AddressOrPair, shard: string, signerOptions?: TrustedSignerOptions, requestOptions?: RequestOptions): Promise<Balance> {
return await callGetter<Balance>(this, [Request.TrustedGetter, 'free_balance', 'Balance'], {
shard: shard,
account: accountOrPubKey,
signer: signerOptions?.signer
signer: singerOptions?.signer
}, requestOptions)
}

public async getBalanceGetter(accountOrPubKey: AddressOrPair, shard: string, signerOptions?: TrustedSignerOptions): Promise<SubmittableGetter<IntegriteeWorker, Balance>> {
public async getAccountInfoGetter(accountOrPubKey: AddressOrPair, shard: string, signerOptions?: TrustedSignerOptions): Promise<SubmittableGetter<IntegriteeWorker, AccountInfo>> {
const trustedGetterArgs = {
shard: shard,
account: accountOrPubKey,
signer: signerOptions?.signer
signer: signerOptions?.signer,
}
return await submittableGetter<IntegriteeWorker, Balance>(this, 'free_balance', trustedGetterArgs,'Balance');
return await submittableGetter<IntegriteeWorker, AccountInfo>(this, 'account_info', trustedGetterArgs,'AccountInfo');
}

public async trustedBalanceTransfer(
Expand All @@ -67,7 +55,7 @@ export class IntegriteeWorker extends Worker {
signerOptions?: TrustedSignerOptions,
requestOptions?: RequestOptions,
): Promise<Hash> {
const nonce = signerOptions?.nonce ?? await this.getNonce(account, shard, signerOptions, requestOptions);
const nonce = signerOptions?.nonce ?? await this.getNonce(account, shard, signerOptions, requestOptions)
const shardT = this.createType('ShardIdentifier', bs58.decode(shard));
const params = this.createType('BalanceTransferArgs', [from, to, amount])
const call = createTrustedCall(this, ['balance_transfer', 'BalanceTransferArgs'], params);
Expand All @@ -85,7 +73,8 @@ export class IntegriteeWorker extends Worker {
signerOptions?: TrustedSignerOptions,
requestOptions?: RequestOptions,
): Promise<Hash> {
const nonce = signerOptions?.nonce ?? await this.getNonce(account, shard, signerOptions, requestOptions);
const nonce = signerOptions?.nonce ?? await this.getNonce(account, shard, signerOptions, requestOptions)

const shardT = this.createType('ShardIdentifier', bs58.decode(shard));
const params = this.createType('BalanceUnshieldArgs', [fromIncognitoAddress, toPublicAddress, amount, shardT])
const call = createTrustedCall(this, ['balance_unshield', 'BalanceUnshieldArgs'], params);
Expand Down
4 changes: 2 additions & 2 deletions packages/worker-api/src/testUtils/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export const localDockerNetwork = () => {
chain: 'ws://127.0.0.1:9944',
worker: 'wss://127.0.0.1:2000',
genesisHash: '0x388c446a804e24e77ae89f5bb099edb60cacc2ac7c898ce175bdaa08629c1439',
mrenclave: '9jm9Wm4DwGxsUUPA1cvcWWxyTuynpJ2YeEcNGnm8nztk',
chosenCid: '9jm9Wm4DwGxsUUPA1cvcWWxyTuynpJ2YeEcNGnm8nztk',
mrenclave: 'FtNhy2ZvHHDPZxHZY5atVav2VVkoipR3BE6zAbcveUcT',
chosenCid: 'FtNhy2ZvHHDPZxHZY5atVav2VVkoipR3BE6zAbcveUcT',
customTypes: {},
palletOverrides: {}
};
Expand Down
16 changes: 12 additions & 4 deletions packages/worker-api/src/worker.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Keyring } from '@polkadot/api';
import { cryptoWaitReady } from '@polkadot/util-crypto';
import { paseoNetwork } from './testUtils/networks.js';
import {localDockerNetwork} from './testUtils/networks.js';
import { Worker } from './worker.js';
import WS from 'websocket';

const {w3cwebsocket: WebSocket} = WS;

describe('worker', () => {
const network = paseoNetwork();
describe.skip('worker', () => {
const network = localDockerNetwork();
let keyring: Keyring;
let worker: Worker;
beforeAll(async () => {
Expand All @@ -26,7 +26,7 @@ describe('worker', () => {
undefined,
// Allow the worker's self-signed certificate, needed in non-reverse proxy setups
// where we talk to the worker directly.
// { rejectUnauthorized: false }
{ rejectUnauthorized: false }
),
api: null,
});
Expand All @@ -51,5 +51,13 @@ describe('worker', () => {
expect(result).toBeDefined();
});
});

describe('getFingerprint', () => {
it('should return value', async () => {
const result = await worker.getFingerprint();
console.log('Fingerprint', result.toString());
expect(result).toBeDefined();
});
});
});
});
9 changes: 8 additions & 1 deletion packages/worker-api/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import WebSocketAsPromised from 'websocket-as-promised';
import {options as encointerOptions} from '@encointer/node-api';
import {parseI64F64} from '@encointer/util';

import type {Vault} from '@encointer/types';
import type {EnclaveFingerprint, Vault} from '@encointer/types';

import {type RequestOptions, type IWorker, Request, type WorkerOptions} from './interface.js';
import {parseBalance} from './parsers.js';
Expand Down Expand Up @@ -66,6 +66,9 @@ const parseGetterResponse = (self: IWorker, responseType: string, data: string)
case 'Vault':
parsedData = self.createType(responseType, returnValue.value);
break
case 'EnclaveFingerprint':
parsedData = self.createType(responseType, returnValue.value);
break
case 'TrustedOperationResult':
console.log(`Got TrustedOperationResult`)
parsedData = self.createType('Hash', returnValue.value);
Expand Down Expand Up @@ -162,4 +165,8 @@ export class Worker extends WebSocketAsPromised implements IWorker {
public async getShardVault(options: RequestOptions = {} as RequestOptions): Promise<Vault> {
return await callGetter<Vault>(this, [Request.Worker, 'author_getShardVault', 'Vault'], {}, options)
}

public async getFingerprint(options: RequestOptions = {} as RequestOptions): Promise<EnclaveFingerprint> {
return await callGetter<EnclaveFingerprint>(this, [Request.Worker, 'author_getFingerprint', 'EnclaveFingerprint'], {}, options)
}
}
Loading

0 comments on commit f32b2e8

Please sign in to comment.