Skip to content

Commit 7d3feac

Browse files
authored
Add some non-trading JSON-RPC test examples. (#3591)
* add post_heima_login && add_wallet * add some test case * Add a general method * remove one-time command * fmt * fix ci * fix add wallet response type * rm unused comment
1 parent 22017ba commit 7d3feac

File tree

12 files changed

+3762
-2768
lines changed

12 files changed

+3762
-2768
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ jobs:
173173
fi
174174
if [ "${{ steps.filter.outputs.omni_executor_test }}" = "true" ] || [ "$rebuild_parachain" = "true" ] || [ "$rebuild_omni_executor" = "true" ]; then
175175
run_omni_executor_test=true
176-
rebuild_omni_executor=true
177176
fi
178177
179178
echo "rebuild_parachain=$rebuild_parachain" | tee -a $GITHUB_OUTPUT

tee-worker/omni-executor/mock-server/src/pumpx.rs

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
#![allow(opaque_hidden_inferred_bound)]
22

3+
use serde_json::json;
34
use warp::{http::Response, Filter};
45

56
const BASE_PATH: &str = "pumpx";
67

8+
fn build_success_response(response: serde_json::Value) -> Response<String> {
9+
Response::builder()
10+
.status(200)
11+
.header("content-type", "application/json")
12+
.body(response.to_string())
13+
.unwrap()
14+
}
15+
716
pub(crate) fn handle() -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone
817
{
918
let user_connect = warp::post()
@@ -23,14 +32,38 @@ pub(crate) fn handle() -> impl Filter<Extract = (impl warp::Reply,), Error = war
2332
});
2433

2534
let post_heima_login = warp::post()
26-
.and(warp::path(BASE_PATH))
2735
.and(warp::path!("v3" / "account" / "post_heima_login"))
28-
.map(|| {
29-
// TODO implements your response logic
30-
Response::builder().status(200).body("").unwrap()
36+
.and(warp::body::json())
37+
.map(|body: serde_json::Value| {
38+
log::info!("Received post_heima_login request: {:?}", body);
39+
40+
let response = json!({
41+
"code": 10000,
42+
"message": "OK",
43+
"data": {
44+
"user_id": null
45+
}
46+
});
47+
48+
build_success_response(response)
49+
});
50+
51+
let add_wallet = warp::post()
52+
.and(warp::path!("v3" / "account" / "add_wallet"))
53+
.and(warp::header::optional::<String>("authorization"))
54+
.map(|auth: Option<String>| {
55+
log::info!("Received add_wallet request with auth: {:?}", auth);
56+
57+
let response = json!({
58+
"code": 10000,
59+
"message": "OK",
60+
"data": {}
61+
});
62+
63+
build_success_response(response)
3164
});
3265

3366
// mock other APIs you need here
3467

35-
user_connect.or(verify_google_code).or(post_heima_login)
68+
user_connect.or(verify_google_code).or(post_heima_login).or(add_wallet)
3669
}
Lines changed: 202 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,215 @@
11
import { describe, it } from 'mocha';
22
import { expect } from 'chai';
3-
import { omniApi, RequestEmailVerificationCodeResponse } from './utils';
3+
import {
4+
ClientId,
5+
GetShieldingKeyResponse,
6+
omniApi,
7+
RequestEmailVerificationCodeResponse,
8+
UserLoginResponse,
9+
calculateOmniAccount,
10+
GetWeb3SignInMessageResponse,
11+
randomEvmWallet,
12+
GetNextIntentIdResponse,
13+
ExportWalletResponse,
14+
encrypt,
15+
decryptWithAes,
16+
AddWalletResponse,
17+
} from './utils';
18+
import { signMessage } from 'viem/accounts';
19+
import { u8aToHex } from '@polkadot/util';
20+
import { AesOutput } from '@heima-network/api-augment/identity';
21+
describe('Omni JsonRpc Mock Tests', function () {
22+
this.timeout(100000);
423

5-
enum ClientId {
6-
Wildmeta = 'wildmeta',
7-
Heima = 'heima',
8-
Pumpx = 'pumpx',
9-
}
24+
const evmWallet = randomEvmWallet();
25+
const omniAccount = calculateOmniAccount(evmWallet.address);
26+
const aesRandomKey = crypto.getRandomValues(new Uint8Array(32));
27+
let id_token: string;
28+
let shieldingKey: GetShieldingKeyResponse;
1029

11-
describe('Request Email Verification Code', () => {
1230
it('should request email verification code successfully', async function () {
13-
this.timeout(10000);
14-
1531
const result: RequestEmailVerificationCodeResponse = await omniApi.requestEmailVerificationCode({
1632
client_id: ClientId.Wildmeta,
1733
user_email: 'test@gmail.com',
1834
});
1935

36+
// expected result:
37+
// null
38+
2039
expect(result).to.be.null;
2140
});
41+
42+
it('should get shielding key successfully', async function () {
43+
const result: GetShieldingKeyResponse = await omniApi.getShieldingKey();
44+
45+
// expected result:
46+
// {
47+
// "n": "0x123...",
48+
// "e": "0x123..."
49+
// }
50+
51+
expect(result.n).to.be.not.undefined;
52+
expect(result.e).to.be.not.undefined;
53+
54+
// store shielding key for later use
55+
shieldingKey = result;
56+
});
57+
58+
it('should get next intent id successfully', async function () {
59+
const result: GetNextIntentIdResponse = await omniApi.getNextIntentId({
60+
omni_account: omniAccount,
61+
});
62+
63+
// expected result:
64+
// 1
65+
66+
expect(result).to.be.equal(1);
67+
});
68+
69+
it('should get web3 sign in message successfully', async function () {
70+
const result: GetWeb3SignInMessageResponse = await omniApi.getWeb3SignInMessage({
71+
client_id: ClientId.Wildmeta,
72+
omni_account: omniAccount,
73+
});
74+
75+
// expected result:
76+
// {
77+
// "message_code": "123456",
78+
// "client_id": "wildmeta",
79+
// "omni_account": "0x1234567890123456789012345678901234567890"
80+
// }
81+
82+
expect(result.message_code).to.be.not.undefined;
83+
expect(result.client_id).to.be.equal(ClientId.Wildmeta);
84+
expect(result.omni_account).to.be.equal(omniAccount);
85+
});
86+
87+
it('should fail to login with web2(email) due to missing verification code', async function () {
88+
try {
89+
const result: UserLoginResponse = await omniApi.userLogin({
90+
user_id: {
91+
type: 'email',
92+
value: 'test@gmail.com',
93+
},
94+
user_auth: {
95+
type: 'email',
96+
value: '654321', // email code
97+
},
98+
client_id: ClientId.Wildmeta,
99+
client_auth: {
100+
type: 'wildmeta',
101+
value: {
102+
google_code: '',
103+
invite_code: '',
104+
},
105+
},
106+
});
107+
} catch (error) {
108+
// Expected failure: verify_email_authentication fails when code not found in VerificationCodeStorage
109+
// This happens because we need to call requestEmailVerificationCode first and use the actual code
110+
console.log('Expected email login failure:', error);
111+
}
112+
});
113+
114+
it('should login user successfully with web3(evm)', async function () {
115+
const omniAccount = calculateOmniAccount(evmWallet.address);
116+
const message: GetWeb3SignInMessageResponse = await omniApi.getWeb3SignInMessage({
117+
client_id: ClientId.Wildmeta,
118+
omni_account: omniAccount,
119+
});
120+
121+
const messageString = JSON.stringify(message);
122+
const signature = await signMessage({ message: messageString, privateKey: evmWallet.privateKey });
123+
124+
const result: UserLoginResponse = await omniApi.userLogin({
125+
user_id: {
126+
type: 'evm',
127+
value: evmWallet.address,
128+
},
129+
user_auth: {
130+
type: 'evm',
131+
value: signature,
132+
},
133+
client_id: ClientId.Wildmeta,
134+
client_auth: {
135+
type: 'wildmeta',
136+
value: {
137+
google_code: '',
138+
invite_code: '',
139+
},
140+
},
141+
});
142+
143+
// expected result:
144+
// {
145+
// "access_token": "0x123...",
146+
// "id_token": "0x123...",
147+
// "backend_response": {
148+
// "code": 10000,
149+
// "message": "OK",
150+
// "data": {
151+
// "user_id": null
152+
// }
153+
// }
154+
// }
155+
156+
expect(result.access_token).to.be.not.undefined;
157+
expect(result.id_token).to.be.not.undefined;
158+
expect(result.backend_response.code).to.be.equal(10000);
159+
expect(result.backend_response.message).to.be.equal('OK');
160+
161+
// Store the access token for use in subsequent tests
162+
id_token = result.id_token;
163+
});
164+
165+
it('should add wallet successfully', async function () {
166+
const result: AddWalletResponse = await omniApi.addWallet(id_token);
167+
168+
// expected result:
169+
// "backend_response": {
170+
// "code": 10000,
171+
// "message": "OK",
172+
// "data": {}
173+
// }
174+
175+
expect(result.backend_response.code).to.be.equal(10000);
176+
expect(result.backend_response.message).to.be.equal('OK');
177+
});
178+
179+
it('should export wallet successfully', async function () {
180+
const encryptedKey = await encrypt({ cleartext: aesRandomKey, shieldingKey });
181+
try {
182+
const result: ExportWalletResponse = await omniApi.exportWallet(
183+
{
184+
key: u8aToHex(encryptedKey.ciphertext),
185+
google_code: '',
186+
chain_id: 1, // get from backend
187+
wallet_index: 0, // get from backend
188+
wallet_address: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', // We do not currently know the logic for generating the backend address, so we can only use a hardcode here temporarily.
189+
},
190+
id_token
191+
);
192+
193+
// expected result:
194+
// {
195+
// ciphertext: "0xb0b6126dd7acae085cd4da94185d0707bb19257f6f95cac84c2c4b0ffc0e23995ac656c96f08abd007b32bafae5477f7",
196+
// aad: "0x",
197+
// nonce: "0xaad559c090dc5195ace3e3eb",
198+
// };
199+
} catch (error) {
200+
console.log('Expected export wallet failure:', error);
201+
}
202+
203+
const mockResult = {
204+
ciphertext:
205+
'0x1f7dde5143ccf34ac1756dcbd1cb7563408e5750c6758dba58ca383e723f47a1b2d774170b690229c01d7d9d9a1b2780',
206+
aad: '0x',
207+
nonce: '0xcc6e4b8b9b50bc990dcb016e',
208+
};
209+
210+
const mockAesKey = '0x3aefe1ec780dc389ed4e048eacaf25679a7f2e567d11f65071b51029dd63e79d';
211+
212+
const decryptedKey = await decryptWithAes(mockAesKey, mockResult as unknown as AesOutput, 'hex');
213+
console.log('decryptedKey', decryptedKey);
214+
});
22215
});

tee-worker/omni-executor/ts-tests/jsonrpc-mock-tests/package.json

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,25 @@
1010
"check-types": "tsc --noEmit"
1111
},
1212
"dependencies": {
13-
"mocha": "^11.7.1",
13+
"@heima-network/api-augment": "^0.0.3",
14+
"@polkadot/util": "^13.4.3",
15+
"@polkadot/util-crypto": "^13.4.3",
16+
"chai": "^4.3.6",
17+
"js-sha256": "^0.11.1",
18+
"mocha": "^10.6.0",
1419
"mocha-steps": "^1.3.0",
15-
"chai": "^5.2.1"
20+
"viem": "^2.31.7"
1621
},
1722
"devDependencies": {
1823
"@types/chai": "^5.2.2",
1924
"@types/mocha": "^10.0.10",
2025
"@types/node": "^20.4.4",
21-
"@typescript-eslint/eslint-plugin": "^8.36.0",
22-
"@typescript-eslint/parser": "^8.36.0",
26+
"@typescript-eslint/eslint-plugin": "^5.60.0",
27+
"@typescript-eslint/parser": "^5.60.0",
28+
"eslint": "^8.46.0",
2329
"prettier": "^2.8.3",
2430
"ts-node": "^10.9.1",
25-
"typescript": "^5.1.0",
26-
"eslint": "^9.30.1"
31+
"typescript": "^5.1.0"
2732
},
2833
"packageManager": "pnpm@9.15.2",
2934
"engines": {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export enum ClientId {
2+
Wildmeta = 'wildmeta',
3+
Heima = 'heima',
4+
Pumpx = 'pumpx',
5+
}

tee-worker/omni-executor/ts-tests/jsonrpc-mock-tests/utils/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ export * from './omni-api';
33
export * from './client';
44
export * from './request-types';
55
export * from './response-types';
6+
export * from './constants';
7+
export * from './utils';
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
enum JsonRpcMethods {
22
OmniRequestEmailVerificationCode = 'omni_requestEmailVerificationCode',
3-
// todo: add other methods
3+
OmniGetShieldingKey = 'omni_getShieldingKey',
4+
OmniGetWeb3SignInMessage = 'omni_getWeb3SignInMessage',
5+
OmniUserLogin = 'omni_userLogin',
6+
OmniGetNextIntentId = 'omni_getNextIntentId',
7+
OmniAddWallet = 'omni_addWallet',
8+
OmniExportWallet = 'omni_exportWallet',
49
}
510

611
export { JsonRpcMethods };

0 commit comments

Comments
 (0)