-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: P-1221 supported new omni account api in client-sdk: add_accoun…
…t, remove_accounts, publicize_account
- Loading branch information
higherordertech
committed
Dec 17, 2024
1 parent
993f883
commit 8908cfc
Showing
7 changed files
with
631 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
tee-worker/identity/client-sdk/packages/client-sdk/src/lib/requests/add-account.request.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
122 changes: 122 additions & 0 deletions
122
...ker/identity/client-sdk/packages/client-sdk/src/lib/requests/publicize-account.request.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
Oops, something went wrong.