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

client-sdk: add OmniAccount helpers #3161

Merged
merged 16 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 15 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
12 changes: 9 additions & 3 deletions tee-worker/identity/client-api/parachain-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ These types were auto generated using [Polkadot.js Type Generation](https://polk
2. Extend and decorate the API's types with:

```typescript
import { identity, vc, trusted_operations, sidechain } from "parachain-api";

const types = { ...identity.types, ...vc.types, ...trusted_operations.types, ...sidechain.types };
import { identity, vc, trusted_operations, sidechain, omniAccount } from "parachain-api";

const types = {
...identity.types,
...vc.types,
...omniAccount.types,
...trusted_operations.types,
...sidechain.types,
};

const api = await ApiPromise.create({
provider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Add OmniAccount requestors for `createAccountStore`, `remark`, `transferNative`, `transferEthereum`, and `callEthereum`.

## 2024-10-14

Initial version. Merge [@litentry/enclave](https://www.npmjs.com/package/@litentry/enclave) and [@litentry/vc-sdk](https://www.npmjs.com/package/@litentry/vc-sdk) into this one.
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name": "@litentry/client-sdk",
"description": "This package provides helpers for dApps to interact with the Litentry Protocol.",
"version": "1.0.0",
"version": "1.0.0-next.0",
"license": "GPL-3.0-or-later",
"dependencies": {},
"devDependencies": {
"@polkadot/rpc-provider": "^10.9.1"
},
"peerDependencies": {
"@litentry/parachain-api": "0.9.20-4.1",
"@litentry/sidechain-api": "0.9.20-4",
"@litentry/parachain-api": "0.9.20-next.4",
"@litentry/sidechain-api": "0.9.20-next.4",
"@litentry/chaindata": "*",
"@polkadot/api": "^10.9.1",
"@polkadot/types": "^10.9.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export { ID_GRAPH_STRUCT } from './lib/type-creators/id-graph';
// vc
export {
validateVc,
VerifiableCredentialLike,
type VerifiableCredentialLike,
} from './lib/vc-validator/validator';
export {
export type {
ValidationResultDetail,
ValidationResult,
} from './lib/vc-validator/validator.types';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { assert, hexToU8a } from '@polkadot/util';
import { randomAsHex } from '@polkadot/util-crypto';

import type { ApiPromise } from '@polkadot/api';
import type {
LitentryIdentity,
WorkerRpcReturnValue,
} from '@litentry/parachain-api';

import { enclave } from '../enclave';
import { createPayloadToSign } from '../util/create-payload-to-sign';
import { createTrustedCallType } from '../type-creators/trusted-call';
import { createRequestType } from '../type-creators/request';

import type { JsonRpcRequest } from '../util/types';
import type { U8aLike } from '@polkadot/util/types';

/**
* OmniAccount: Call an Ethereum contract.
*/
export async function callEthereum(
/** Litentry Parachain API instance from Polkadot.js */
api: ApiPromise,
data: {
/** The user's omniAccount. Use `createLitentryIdentityType` helper to create this struct */
omniAccount: LitentryIdentity;
/** The user's account. Use `createLitentryIdentityType` helper to create this struct */
who: LitentryIdentity;
/** Ethereum contract address */
address: string;
/** Contract input data */
input: U8aLike;
}
): Promise<{
payloadToSign: string;
txHash: string;
send: (args: { signedPayload: string }) => Promise<{
response: Array<WorkerRpcReturnValue>;
txHash: string;
}>;
}> {
const { who, omniAccount } = data;

assert(omniAccount.isSubstrate, 'OmniAccount must be a Substrate identity');

const shard = await enclave.getShard(api);
const shardU8 = hexToU8a(shard);
const txHash = randomAsHex();

const { call } = await createTrustedCallType(api.registry, {
method: 'request_intent',
params: {
who,
intent: api.createType('Intent', {
CallEthereum: api.createType('IntentCallEthereum', {
address: data.address,
input: data.input,
}),
}),
},
});

const nonce = await api.rpc.system.accountNextIndex(omniAccount.asSubstrate);

const payloadToSign = createPayloadToSign({
who,
call,
nonce,
shard: shardU8,
});

const send = async (args: {
signedPayload: string;
}): Promise<{
response: Array<WorkerRpcReturnValue>;
txHash: string;
}> => {
// prepare and encrypt request

const request = await createRequestType(api, {
signer: who,
signature: args.signedPayload,
call,
nonce,
shard: shardU8,
});

// send the request to the Enclave
const rpcRequest: JsonRpcRequest = {
jsonrpc: '2.0',
method: 'author_submitNativeRequest',
params: [request.toHex()],
};

const enclaveResult = await enclave.send(api, rpcRequest);

return {
txHash,
response: enclaveResult,
};
};

return {
txHash,
payloadToSign,
send,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { hexToU8a } from '@polkadot/util';
import { randomAsHex } from '@polkadot/util-crypto';

import type { ApiPromise } from '@polkadot/api';
import type {
LitentryIdentity,
WorkerRpcReturnValue,
} from '@litentry/parachain-api';

import { enclave } from '../enclave';
import { createPayloadToSign } from '../util/create-payload-to-sign';
import { createTrustedCallType } from '../type-creators/trusted-call';
import { createRequestType } from '../type-creators/request';

import type { JsonRpcRequest } from '../util/types';

/**
* OmniAccount: Create the OmniAccount for the given Identity
*/
export async function createAccountStore(
/** Litentry Parachain API instance from Polkadot.js */
api: ApiPromise,
data: {
/** The user's OmniAccount. Use `createLitentryIdentityType` helper to create this struct */
omniAccount: LitentryIdentity;
/** The user's account. Use `createLitentryIdentityType` helper to create this struct */
who: LitentryIdentity;
}
): Promise<{
payloadToSign: string;
txHash: string;
send: (args: { signedPayload: string }) => Promise<{
response: Array<WorkerRpcReturnValue>;
txHash: string;
}>;
}> {
const { who, omniAccount } = data;

const shard = await enclave.getShard(api);
const shardU8 = hexToU8a(shard);
const txHash = randomAsHex();

const { call } = await createTrustedCallType(api.registry, {
method: 'create_account_store',
params: {
who,
},
});

const nonce = await api.rpc.system.accountNextIndex(
omniAccount.asSubstrate.toHex()
);

const payloadToSign = createPayloadToSign({
who,
call,
nonce,
shard: shardU8,
});

const send = async (args: {
signedPayload: string;
}): Promise<{
response: Array<WorkerRpcReturnValue>;
txHash: string;
}> => {
// prepare and encrypt request

const request = await createRequestType(api, {
signer: who,
signature: args.signedPayload,
call,
nonce,
shard: shardU8,
});

// send the request to the Enclave
const rpcRequest: JsonRpcRequest = {
jsonrpc: '2.0',
method: 'author_submitNativeRequest',
params: [request.toHex()],
};

const enclaveResult = await enclave.send(api, rpcRequest);

return {
txHash,
response: enclaveResult,
};
};

return {
txHash,
payloadToSign,
send,
};
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import type { ApiPromise } from '@polkadot/api';
import { AccountId32 } from '@polkadot/types/interfaces';
import type {
PalletTeebagEnclave,
PalletTeebagWorkerType,
CorePrimitivesTeebagTypesEnclave,
CorePrimitivesTeebagTypesWorkerType,
} from '@polkadot/types/lookup';

/**
* Return the Enclave registry information of the latest registered TEE worker.
*/
export async function getLastRegisteredEnclave(
api: ApiPromise,
workerType: PalletTeebagWorkerType['type'] = 'Identity'
): Promise<{ account: AccountId32; enclave: PalletTeebagEnclave }> {
workerType: CorePrimitivesTeebagTypesWorkerType['type'] = 'Identity'
): Promise<{
account: AccountId32;
enclave: CorePrimitivesTeebagTypesEnclave;
}> {
const identifiers = await api.query.teebag.enclaveIdentifier(workerType);
const latestEnclaveId = identifiers[identifiers.length - 1];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ export { createChallengeCode, linkIdentity } from './link-identity.request';
export { linkIdentityCallback } from './link-identity-callback.request';
export { setIdentityNetworks } from './set-identity-networks.request';
export { requestBatchVC } from './request-batch-vc.request';
export { remark } from './remark.request';
export { transferNative } from './transfer-native.request';
export { transferEthereum } from './transfer-ethereum.request';
export { callEthereum } from './call-ethereum.request';

export { createAccountStore } from './create-account-store.request';
export { getIdGraph } from './get-id-graph.request';
export { getIdGraphHash } from './get-id-graph-hash';
export { getLastRegisteredEnclave } from './get-last-registered-enclave';
Loading
Loading