-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
client-sdk: add OmniAccount helpers (#3161)
* update to [email protected] Signed-off-by: Jonathan Alvarez <[email protected]> * add requestor for intent Signed-off-by: Jonathan Alvarez <[email protected]> * update PalletTeebagEnclave to CorePrimitivesTeebagTypesEnclave Signed-off-by: Jonathan Alvarez <[email protected]> * expose remark Signed-off-by: Jonathan Alvarez <[email protected]> * client-sdk: add request.createAccountStore Signed-off-by: Jonathan Alvarez <[email protected]> * remark: remove unused signer Signed-off-by: Jonathan Alvarez <[email protected]> * remark: use omniAccount Signed-off-by: Jonathan Alvarez <[email protected]> * add request.transferNative Signed-off-by: Jonathan Alvarez <[email protected]> * transfer: rename value to amount Signed-off-by: Jonathan Alvarez <[email protected]> * add request.transferEthereum Signed-off-by: Jonathan Alvarez <[email protected]> * add request.callEthereum Signed-off-by: Jonathan Alvarez <[email protected]> * update changelog Signed-off-by: Jonathan Alvarez <[email protected]> * bump version to preview .0 Signed-off-by: Jonathan Alvarez <[email protected]> * client-api: update readme Signed-off-by: Jonathan Alvarez <[email protected]> --------- Signed-off-by: Jonathan Alvarez <[email protected]>
- Loading branch information
1 parent
88b3a90
commit dc1ef96
Showing
15 changed files
with
661 additions
and
53 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
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
6 changes: 3 additions & 3 deletions
6
tee-worker/identity/client-sdk/packages/client-sdk/package.json
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
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
108 changes: 108 additions & 0 deletions
108
tee-worker/identity/client-sdk/packages/client-sdk/src/lib/requests/call-ethereum.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,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, | ||
}; | ||
} |
97 changes: 97 additions & 0 deletions
97
.../identity/client-sdk/packages/client-sdk/src/lib/requests/create-account-store.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,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, | ||
}; | ||
} |
11 changes: 7 additions & 4 deletions
11
...r/identity/client-sdk/packages/client-sdk/src/lib/requests/get-last-registered-enclave.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
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
Oops, something went wrong.