Skip to content

Commit

Permalink
feat: P-1221 supported new omni account api in client-sdk: add_accoun…
Browse files Browse the repository at this point in the history
…t, remove_accounts, publicize_account
  • Loading branch information
higherordertech committed Dec 17, 2024
1 parent 993f883 commit 8908cfc
Show file tree
Hide file tree
Showing 7 changed files with 631 additions and 7 deletions.
71 changes: 64 additions & 7 deletions tee-worker/identity/client-sdk/packages/client-sdk/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ Similarly, our types definitions must match also.

#### Defined in

[lib/type-creators/trusted-call.ts:90](https://github.com/litentry/client-sdk/blob/develop/lib/type-creators/trusted-call.ts#L90)
[lib/type-creators/trusted-call.ts:109](https://github.com/litentry/client-sdk/blob/develop/lib/type-creators/trusted-call.ts#L109)

**createTrustedCallType**(`registry`, `data`): `Promise`\<\{ `call`: `TrustedCall` ; `key`: `CryptoKey` }\>

Expand All @@ -597,7 +597,7 @@ Similarly, our types definitions must match also.

#### Defined in

[lib/type-creators/trusted-call.ts:97](https://github.com/litentry/client-sdk/blob/develop/lib/type-creators/trusted-call.ts#L97)
[lib/type-creators/trusted-call.ts:116](https://github.com/litentry/client-sdk/blob/develop/lib/type-creators/trusted-call.ts#L116)

**createTrustedCallType**(`registry`, `data`): `Promise`\<\{ `call`: `TrustedCall` ; `key`: `CryptoKey` }\>

Expand All @@ -616,7 +616,7 @@ Similarly, our types definitions must match also.

#### Defined in

[lib/type-creators/trusted-call.ts:104](https://github.com/litentry/client-sdk/blob/develop/lib/type-creators/trusted-call.ts#L104)
[lib/type-creators/trusted-call.ts:123](https://github.com/litentry/client-sdk/blob/develop/lib/type-creators/trusted-call.ts#L123)

**createTrustedCallType**(`registry`, `data`): `Promise`\<\{ `call`: `TrustedCall` ; `key`: `CryptoKey` }\>

Expand All @@ -635,7 +635,7 @@ Similarly, our types definitions must match also.

#### Defined in

[lib/type-creators/trusted-call.ts:111](https://github.com/litentry/client-sdk/blob/develop/lib/type-creators/trusted-call.ts#L111)
[lib/type-creators/trusted-call.ts:130](https://github.com/litentry/client-sdk/blob/develop/lib/type-creators/trusted-call.ts#L130)

**createTrustedCallType**(`registry`, `data`): `Promise`\<\{ `call`: `TrustedCall` ; `key`: `CryptoKey` }\>

Expand All @@ -654,7 +654,7 @@ Similarly, our types definitions must match also.

#### Defined in

[lib/type-creators/trusted-call.ts:118](https://github.com/litentry/client-sdk/blob/develop/lib/type-creators/trusted-call.ts#L118)
[lib/type-creators/trusted-call.ts:137](https://github.com/litentry/client-sdk/blob/develop/lib/type-creators/trusted-call.ts#L137)

**createTrustedCallType**(`registry`, `data`): `Promise`\<\{ `call`: `TrustedCall` ; `key`: `CryptoKey` }\>

Expand All @@ -673,7 +673,64 @@ Similarly, our types definitions must match also.

#### Defined in

[lib/type-creators/trusted-call.ts:125](https://github.com/litentry/client-sdk/blob/develop/lib/type-creators/trusted-call.ts#L125)
[lib/type-creators/trusted-call.ts:144](https://github.com/litentry/client-sdk/blob/develop/lib/type-creators/trusted-call.ts#L144)

**createTrustedCallType**(`registry`, `data`): `Promise`\<\{ `call`: `TrustedCall` ; `key`: `CryptoKey` }\>

#### Parameters

| Name | Type |
| :------ | :------ |
| `registry` | `Registry` |
| `data` | `Object` |
| `data.method` | ``"add_account"`` |
| `data.params` | `RequestAddAccountParams` |

#### Returns

`Promise`\<\{ `call`: `TrustedCall` ; `key`: `CryptoKey` }\>

#### Defined in

[lib/type-creators/trusted-call.ts:151](https://github.com/litentry/client-sdk/blob/develop/lib/type-creators/trusted-call.ts#L151)

**createTrustedCallType**(`registry`, `data`): `Promise`\<\{ `call`: `TrustedCall` ; `key`: `CryptoKey` }\>

#### Parameters

| Name | Type |
| :------ | :------ |
| `registry` | `Registry` |
| `data` | `Object` |
| `data.method` | ``"remove_accounts"`` |
| `data.params` | `RequestRemoveAccountsParams` |

#### Returns

`Promise`\<\{ `call`: `TrustedCall` ; `key`: `CryptoKey` }\>

#### Defined in

[lib/type-creators/trusted-call.ts:158](https://github.com/litentry/client-sdk/blob/develop/lib/type-creators/trusted-call.ts#L158)

**createTrustedCallType**(`registry`, `data`): `Promise`\<\{ `call`: `TrustedCall` ; `key`: `CryptoKey` }\>

#### Parameters

| Name | Type |
| :------ | :------ |
| `registry` | `Registry` |
| `data` | `Object` |
| `data.method` | ``"publicize_account"`` |
| `data.params` | `RequestPublicizeAccountParams` |

#### Returns

`Promise`\<\{ `call`: `TrustedCall` ; `key`: `CryptoKey` }\>

#### Defined in

[lib/type-creators/trusted-call.ts:165](https://github.com/litentry/client-sdk/blob/develop/lib/type-creators/trusted-call.ts#L165)

**createTrustedCallType**(`registry`, `data`): `Promise`\<\{ `call`: `TrustedCall` ; `key`: `CryptoKey` }\>

Expand All @@ -692,7 +749,7 @@ Similarly, our types definitions must match also.

#### Defined in

[lib/type-creators/trusted-call.ts:132](https://github.com/litentry/client-sdk/blob/develop/lib/type-creators/trusted-call.ts#L132)
[lib/type-creators/trusted-call.ts:172](https://github.com/litentry/client-sdk/blob/develop/lib/type-creators/trusted-call.ts#L172)

___

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { hexToU8a } from '@polkadot/util';

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

import { enclave } from '../enclave';
import { codecToString } from '../util/codec-to-string';
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 { getEnclaveNonce } from './get-enclave-nonce';

/**
* Adds an account to the Litentry Parachain.
*
* @returns {Promise<Object>} - A promise that resolves to an object containing the payload to sign (if applicable) and a send function.
* @returns {string} [payloadToSign] - The payload to sign if who is not an email identity.
* @returns {Function} send - A function to send the request to the Enclave.
* @returns {Promise<Object>} send.args - The arguments required to send the request.
* @returns {string} send.args.authentication - The authentication string. If who is
* an email identity, this is the email verification code. If who is not an email identity, this is the
* signed payload.
*/
export async function addAccount(
/** Litentry Parachain API instance from Polkadot.js */
api: ApiPromise,
data: {
/** The member account for adding. Use `createMemberAccountType` helper to create this struct */
memberAccount: MemberAccount;
/** The user's account. Use `createLitentryIdentityType` helper to create this struct */
who: LitentryIdentity;
}
): Promise<{
payloadToSign?: string;
send: (args: { authentication: string }) => Promise<{
response: WorkerRpcReturnValue;
blockHash: string;
extrinsicHash: string;
}>;
}> {
const { who, memberAccount } = data;

const shard = await enclave.getShard(api);
const shardU8 = hexToU8a(shard);
const nonce = await getEnclaveNonce(api, { who });

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

const send = async (args: {
authentication: string;
}): Promise<{
response: WorkerRpcReturnValue;
blockHash: string;
extrinsicHash: string;
}> => {
// prepare and encrypt request
const request = await createRequestType(api, {
sender: who,
authentication: args.authentication,
call,
nonce,
shard: shardU8,
});

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

const [response] = await enclave.send(api, rpcRequest); // we expect 1 response only

const result: TrustedCallResult = api.createType(
'TrustedCallResult',
response.value
);

if (result.isErr) {
throw new Error(codecToString(result.asErr));
}

if (!result.asOk.isExtrinsicReport) {
throw new Error('Unexpected response type');
}

const { extrinsic_hash, block_hash } = result.asOk.asExtrinsicReport;

return {
response,
extrinsicHash: extrinsic_hash.toString(),
blockHash: block_hash.toString(),
};
};

if (who.isEmail) {
return { send };
}

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

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

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

import { enclave } from '../enclave';
import { codecToString } from '../util/codec-to-string';
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 { getEnclaveNonce } from './get-enclave-nonce';

/**
* Publicizes a member account in the AccountStore on the Litentry Parachain.
*
* @returns {Promise<Object>} A promise that resolves to an object containing the payload to sign (if applicable) and a send function.
* @returns {string} [payloadToSign] The payload to sign if the identity is not an email.
* @returns {Function} send A function to send the request to the Enclave.
* @returns {Promise<Object>} send.args The arguments required to send the request.
* @returns {string} send.args.authentication The authentication string. For email identities, this is the verification code. For non-email identities, this is the signed payload.
*/
export async function publicizeAccount(
/** Litentry Parachain API instance from Polkadot.js */
api: ApiPromise,
data: {
/** The member account for publicizing. Use `createLitentryIdentityType` helper to create this struct */
memberAccount: LitentryIdentity;
/** The user's account. Use `createLitentryIdentityType` helper to create this struct */
who: LitentryIdentity;
}
): Promise<{
payloadToSign?: string;
send: (args: { authentication: string }) => Promise<{
response: WorkerRpcReturnValue;
blockHash: string;
extrinsicHash: string;
}>;
}> {
const { who, memberAccount } = data;

const shard = await enclave.getShard(api);
const shardU8 = hexToU8a(shard);
const nonce = await getEnclaveNonce(api, { who });

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

const send = async (args: {
authentication: string;
}): Promise<{
response: WorkerRpcReturnValue;
blockHash: string;
extrinsicHash: string;
}> => {
// prepare and encrypt request
const request = await createRequestType(api, {
sender: who,
authentication: args.authentication,
call,
nonce,
shard: shardU8,
});

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

const [response] = await enclave.send(api, rpcRequest); // we expect 1 response only

const result: TrustedCallResult = api.createType(
'TrustedCallResult',
response.value
);

if (result.isErr) {
throw new Error(codecToString(result.asErr));
}

if (!result.asOk.isExtrinsicReport) {
throw new Error('Unexpected response type');
}

const { extrinsic_hash, block_hash } = result.asOk.asExtrinsicReport;

return {
response,
extrinsicHash: extrinsic_hash.toString(),
blockHash: block_hash.toString(),
};
};

if (who.isEmail) {
return { send };
}

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

return {
payloadToSign,
send,
};
}
Loading

0 comments on commit 8908cfc

Please sign in to comment.