Skip to content

Commit

Permalink
Updating client sdk to better support email authentication (#3177)
Browse files Browse the repository at this point in the history
  • Loading branch information
silva-fj authored Nov 15, 2024
1 parent 76ed060 commit aba3b1a
Show file tree
Hide file tree
Showing 12 changed files with 227 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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`.
- Add `requestVerificationCode` requestor.

## 2024-10-14

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@litentry/client-sdk",
"description": "This package provides helpers for dApps to interact with the Litentry Protocol.",
"version": "1.0.0-next.3",
"version": "1.0.0-next.4",
"license": "GPL-3.0-or-later",
"dependencies": {},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export * from './lib/type-creators/litentry-identity';
export * from './lib/type-creators/request';
export * from './lib/type-creators/trusted-call';
export * from './lib/type-creators/validation-data';
export * from './lib/type-creators/tc-authentication';

export type { IdGraph } from './lib/type-creators/id-graph';
export { ID_GRAPH_STRUCT } from './lib/type-creators/id-graph';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ import type { U8aLike } from '@polkadot/util/types';

/**
* OmniAccount: Call an Ethereum contract.
*
* @returns {Promise<Object>} - A promise that resolves to an object containing the payload to signature
* (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.authorization - The authentication string. If who is an
* email identity, this is the email verification code. If the who is not an email identity, this
* is the signed payload.
*/
export async function callEthereum(
/** Litentry Parachain API instance from Polkadot.js */
Expand All @@ -33,8 +42,8 @@ export async function callEthereum(
input: U8aLike;
}
): Promise<{
payloadToSign: string;
send: (args: { signedPayload: string }) => Promise<{
payloadToSign?: string;
send: (args: { authorization: string }) => Promise<{
response: WorkerRpcReturnValue;
blockHash: string;
extrinsicHash: string;
Expand Down Expand Up @@ -62,15 +71,8 @@ export async function callEthereum(

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

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

const send = async (args: {
signedPayload: string;
authorization: string;
}): Promise<{
response: WorkerRpcReturnValue;
blockHash: string;
Expand All @@ -80,7 +82,7 @@ export async function callEthereum(

const request = await createRequestType(api, {
signer: who,
signature: args.signedPayload,
signature: args.authorization,
call,
nonce,
shard: shardU8,
Expand Down Expand Up @@ -113,6 +115,19 @@ export async function callEthereum(
};
};

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

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

return {
payloadToSign,
send,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ import { createRequestType } from '../type-creators/request';
import type { JsonRpcRequest } from '../util/types';

/**
* OmniAccount: Create the OmniAccount for the given Identity
* Creates an account store 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 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 the who is not an email identity, this is the
* signed payload.
*/
export async function createAccountStore(
/** Litentry Parachain API instance from Polkadot.js */
Expand All @@ -28,8 +36,8 @@ export async function createAccountStore(
who: LitentryIdentity;
}
): Promise<{
payloadToSign: string;
send: (args: { signedPayload: string }) => Promise<{
payloadToSign?: string;
send: (args: { authentication: string }) => Promise<{
response: WorkerRpcReturnValue;
blockHash: string;
extrinsicHash: string;
Expand All @@ -51,15 +59,8 @@ export async function createAccountStore(
omniAccount.asSubstrate.toHex()
);

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

const send = async (args: {
signedPayload: string;
authentication: string;
}): Promise<{
response: WorkerRpcReturnValue;
blockHash: string;
Expand All @@ -69,7 +70,7 @@ export async function createAccountStore(

const request = await createRequestType(api, {
signer: who,
signature: args.signedPayload,
signature: args.authentication,
call,
nonce,
shard: shardU8,
Expand Down Expand Up @@ -102,6 +103,17 @@ export async function createAccountStore(
};
};

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

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

return {
payloadToSign,
send,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ 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';
export { requestVerificationCode } from './request-verification-code.request';
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ import { createRequestType } from '../type-creators/request';
import type { JsonRpcRequest } from '../util/types';

/**
* OmniAccount: Make a remark
* Sends a remark 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 the who is not an email identity,
* this is the signed payload.
*/
export async function remark(
/** Litentry Parachain API instance from Polkadot.js */
Expand All @@ -30,8 +39,8 @@ export async function remark(
message: string;
}
): Promise<{
payloadToSign: string;
send: (args: { signedPayload: string }) => Promise<{
payloadToSign?: string;
send: (args: { authentication: string }) => Promise<{
response: WorkerRpcReturnValue;
blockHash: string;
extrinsicHash: string;
Expand All @@ -58,15 +67,8 @@ export async function remark(
omniAccount.asSubstrate.toHex()
);

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

const send = async (args: {
signedPayload: string;
authentication: string;
}): Promise<{
response: WorkerRpcReturnValue;
blockHash: string;
Expand All @@ -76,7 +78,7 @@ export async function remark(

const request = await createRequestType(api, {
signer: who,
signature: args.signedPayload,
signature: args.authentication,
call,
nonce,
shard: shardU8,
Expand Down Expand Up @@ -109,6 +111,17 @@ export async function remark(
};
};

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

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

return {
payloadToSign,
send,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { enclave } from '../enclave';

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

/**
* Request email verification code.
*/
export async function requestVerificationCode(
api: ApiPromise,
{ omniAccount, email }: { omniAccount: `0x${string}`; email: string }
): Promise<{ success: boolean }> {
const payload: JsonRpcRequest = {
jsonrpc: '2.0',
method: 'omni_requestEmailVerificationCode',
params: [omniAccount, email],
};

const [workerResponse] = await enclave.send(api, payload);

return { success: workerResponse.status.isOk };
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ import { createRequestType } from '../type-creators/request';
import type { JsonRpcRequest } from '../util/types';

/**
* OmniAccount: Transfer funds to Ethereum Network.
* Transfers Ethereum to another account on the Litentry Parachain.
*
* @returns {Promise<Object>} - A promise that resolves to an object containing the payload to signature
* (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 the who is not an email identity,
* this is the signed payload.
*/
export async function transferEthereum(
/** Litentry Parachain API instance from Polkadot.js */
Expand All @@ -32,8 +41,8 @@ export async function transferEthereum(
amount: bigint;
}
): Promise<{
payloadToSign: string;
send: (args: { signedPayload: string }) => Promise<{
payloadToSign?: string;
send: (args: { authentication: string }) => Promise<{
response: WorkerRpcReturnValue;
blockHash: string;
extrinsicHash: string;
Expand Down Expand Up @@ -61,15 +70,8 @@ export async function transferEthereum(

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

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

const send = async (args: {
signedPayload: string;
authentication: string;
}): Promise<{
response: WorkerRpcReturnValue;
blockHash: string;
Expand All @@ -79,7 +81,7 @@ export async function transferEthereum(

const request = await createRequestType(api, {
signer: who,
signature: args.signedPayload,
signature: args.authentication,
call,
nonce,
shard: shardU8,
Expand Down Expand Up @@ -112,6 +114,17 @@ export async function transferEthereum(
};
};

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

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

return {
payloadToSign,
send,
Expand Down
Loading

0 comments on commit aba3b1a

Please sign in to comment.