-
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.
Adding support for solana intent (transfer) (#3198)
- Loading branch information
Showing
25 changed files
with
8,717 additions
and
4,090 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
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
345 changes: 193 additions & 152 deletions
345
tee-worker/identity/client-sdk/packages/client-sdk/docs/modules/request.md
Large diffs are not rendered by default.
Oops, something went wrong.
4 changes: 2 additions & 2 deletions
4
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
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
136 changes: 136 additions & 0 deletions
136
...orker/identity/client-sdk/packages/client-sdk/src/lib/requests/transfer-solana.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,136 @@ | ||
import { assert, hexToU8a } from '@polkadot/util'; | ||
|
||
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'; | ||
|
||
import type { JsonRpcRequest } from '../util/types'; | ||
|
||
/** | ||
* Transfers SOL to another account on Solana. | ||
* | ||
* @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 transferSolana( | ||
/** 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; | ||
/** Solana address destination */ | ||
to: string; | ||
/** Amount to send in lamports */ | ||
amount: bigint; | ||
} | ||
): Promise<{ | ||
payloadToSign?: string; | ||
send: (args: { authentication: string }) => Promise<{ | ||
response: WorkerRpcReturnValue; | ||
blockHash: string; | ||
extrinsicHash: 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 { call } = await createTrustedCallType(api.registry, { | ||
method: 'request_intent', | ||
params: { | ||
who, | ||
intent: api.createType('Intent', { | ||
TransferSolana: api.createType('IntentTransferSolana', { | ||
to: data.to, | ||
value: data.amount, | ||
}), | ||
}), | ||
}, | ||
}); | ||
|
||
const nonce = await api.rpc.system.accountNextIndex(omniAccount.asSubstrate); | ||
|
||
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.