Skip to content

Commit

Permalink
client-sdk: decode the result of native trusted operations (#3169)
Browse files Browse the repository at this point in the history
Signed-off-by: Jonathan Alvarez <[email protected]>
  • Loading branch information
jonalvarezz authored Nov 7, 2024
1 parent f71cf97 commit 0fd2f74
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 89 deletions.
2 changes: 1 addition & 1 deletion tee-worker/identity/client-api/parachain-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "dist/src/index.js",
"module": "dist/src/index.js",
"sideEffects": false,
"version": "0.9.20-next.5",
"version": "0.9.20-next.7",
"scripts": {
"clean": "rm -rf dist build node_modules",
"update-metadata": "curl -s -H \"Content-Type: application/json\" -d '{\"id\":\"1\", \"jsonrpc\":\"2.0\", \"method\": \"state_getMetadata\", \"params\":[]}' http://localhost:9944 > prepare-build/litentry-parachain-metadata.json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,27 +142,29 @@ export default {
},
},
NativeTaskError: {
UnexpectedCall: "String",
ShieldingKeyRetrievalFailed: "String", // Stringified itp_sgx_crypto::Error
RequestPayloadDecodingFailed: "Null",
ParentchainDataRetrievalFailed: "String", // Stringified itp_stf_state_handler::Error
InvalidSignerAccount: "Null",
UnauthorizedSigner: "Null",
InvalidMemberIdentity: "Null",
MissingAesKey: "Null",
MrEnclaveRetrievalFailed: "Null",
EnclaveSignerRetrievalFailed: "Null",
AuthenticationVerificationFailed: "Null",
ValidationDataVerificationFailed: "Null",
ConnectionHashNotFound: "String",
MetadataRetrievalFailed: "String", // Stringified itp_node_api_metadata_provider::Error
InvalidMetadata: "String", // Stringified itp_node_api_metadata::Error
TrustedCallSendingFailed: "String", // Stringified mpsc::SendError<(H256, TrustedCall)>
CallSendingFailed: "String",
ExtrinsicConstructionFailed: "String", // Stringified itp_extrinsics_factory::Error
ExtrinsicSendingFailed: "String", // Stringified sgx_status_t
InvalidRequest: "Null",
NativeRequestSendFailed: "Null",
_enum: {
UnexpectedCall: "Text",
ShieldingKeyRetrievalFailed: "Text", // Stringified itp_sgx_crypto::Error
RequestPayloadDecodingFailed: "Null",
ParentchainDataRetrievalFailed: "Text", // Stringified itp_stf_state_handler::Error
InvalidSignerAccount: "Null",
UnauthorizedSigner: "Null",
InvalidMemberIdentity: "Null",
MissingAesKey: "Null",
MrEnclaveRetrievalFailed: "Null",
EnclaveSignerRetrievalFailed: "Null",
AuthenticationVerificationFailed: "Null",
ValidationDataVerificationFailed: "Null",
ConnectionHashNotFound: "Text",
MetadataRetrievalFailed: "Text", // Stringified itp_node_api_metadata_provider::Error
InvalidMetadata: "Text", // Stringified itp_node_api_metadata::Error
TrustedCallSendingFailed: "Text", // Stringified mpsc::SendError<(H256, TrustedCall)>
CallSendingFailed: "Text",
ExtrinsicConstructionFailed: "Text", // Stringified itp_extrinsics_factory::Error
ExtrinsicSendingFailed: "Text", // Stringified sgx_status_t
InvalidRequest: "Null",
NativeRequestSendFailed: "Null",
},
},
},
};
2 changes: 1 addition & 1 deletion tee-worker/identity/client-api/sidechain-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "dist/src/index.js",
"module": "dist/src/index.js",
"sideEffects": false,
"version": "0.9.20-next.5",
"version": "0.9.20-next.7",
"scripts": {
"clean": "rm -rf dist build node_modules",
"update-metadata": "../../bin/litentry-cli print-sgx-metadata-raw > prepare-build/litentry-sidechain-metadata.json",
Expand Down
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-next.0",
"version": "1.0.0-next.1",
"license": "GPL-3.0-or-later",
"dependencies": {},
"devDependencies": {
"@polkadot/rpc-provider": "^10.9.1"
},
"peerDependencies": {
"@litentry/parachain-api": "0.9.20-next.5",
"@litentry/sidechain-api": "0.9.20-next.5",
"@litentry/parachain-api": "0.9.20-next.7",
"@litentry/sidechain-api": "0.9.20-next.7",
"@litentry/chaindata": "*",
"@polkadot/api": "^10.9.1",
"@polkadot/types": "^10.9.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { assert, hexToU8a } from '@polkadot/util';
import { randomAsHex } from '@polkadot/util-crypto';

import type { ApiPromise } from '@polkadot/api';
import type {
LitentryIdentity,
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';
Expand All @@ -33,10 +34,10 @@ export async function callEthereum(
}
): Promise<{
payloadToSign: string;
txHash: string;
send: (args: { signedPayload: string }) => Promise<{
response: Array<WorkerRpcReturnValue>;
txHash: string;
response: WorkerRpcReturnValue;
blockHash: string;
extrinsicHash: string;
}>;
}> {
const { who, omniAccount } = data;
Expand All @@ -45,7 +46,6 @@ export async function callEthereum(

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

const { call } = await createTrustedCallType(api.registry, {
method: 'request_intent',
Expand All @@ -72,8 +72,9 @@ export async function callEthereum(
const send = async (args: {
signedPayload: string;
}): Promise<{
response: Array<WorkerRpcReturnValue>;
txHash: string;
response: WorkerRpcReturnValue;
blockHash: string;
extrinsicHash: string;
}> => {
// prepare and encrypt request

Expand All @@ -92,16 +93,27 @@ export async function callEthereum(
params: [request.toHex()],
};

const enclaveResult = await enclave.send(api, rpcRequest);
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));
}

const { extrinsic_hash, block_hash } = result.asOk;

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

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

import type { ApiPromise } from '@polkadot/api';
import type {
LitentryIdentity,
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';
Expand All @@ -28,17 +29,16 @@ export async function createAccountStore(
}
): Promise<{
payloadToSign: string;
txHash: string;
send: (args: { signedPayload: string }) => Promise<{
response: Array<WorkerRpcReturnValue>;
txHash: string;
response: WorkerRpcReturnValue;
blockHash: string;
extrinsicHash: 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',
Expand All @@ -61,8 +61,9 @@ export async function createAccountStore(
const send = async (args: {
signedPayload: string;
}): Promise<{
response: Array<WorkerRpcReturnValue>;
txHash: string;
response: WorkerRpcReturnValue;
blockHash: string;
extrinsicHash: string;
}> => {
// prepare and encrypt request

Expand All @@ -81,16 +82,27 @@ export async function createAccountStore(
params: [request.toHex()],
};

const enclaveResult = await enclave.send(api, rpcRequest);
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));
}

const { extrinsic_hash, block_hash } = result.asOk;

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

return {
txHash,
payloadToSign,
send,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { assert, hexToU8a, stringToHex } from '@polkadot/util';
import { randomAsHex } from '@polkadot/util-crypto';

import type { ApiPromise } from '@polkadot/api';
import type {
LitentryIdentity,
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';
Expand All @@ -30,17 +31,16 @@ export async function remark(
}
): Promise<{
payloadToSign: string;
txHash: string;
send: (args: { signedPayload: string }) => Promise<{
response: Array<WorkerRpcReturnValue>;
txHash: string;
response: WorkerRpcReturnValue;
blockHash: string;
extrinsicHash: string;
}>;
}> {
const { who, message, omniAccount } = data;

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

const { call } = await createTrustedCallType(api.registry, {
method: 'request_intent',
Expand Down Expand Up @@ -68,8 +68,9 @@ export async function remark(
const send = async (args: {
signedPayload: string;
}): Promise<{
response: Array<WorkerRpcReturnValue>;
txHash: string;
response: WorkerRpcReturnValue;
blockHash: string;
extrinsicHash: string;
}> => {
// prepare and encrypt request

Expand All @@ -88,16 +89,27 @@ export async function remark(
params: [request.toHex()],
};

const enclaveResult = await enclave.send(api, rpcRequest);
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));
}

const { extrinsic_hash, block_hash } = result.asOk;

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

return {
txHash,
payloadToSign,
send,
};
Expand Down
Loading

0 comments on commit 0fd2f74

Please sign in to comment.