|
1 | 1 | import { describe, it } from 'mocha';
|
2 | 2 | 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); |
4 | 23 |
|
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; |
10 | 29 |
|
11 |
| -describe('Request Email Verification Code', () => { |
12 | 30 | it('should request email verification code successfully', async function () {
|
13 |
| - this.timeout(10000); |
14 |
| - |
15 | 31 | const result: RequestEmailVerificationCodeResponse = await omniApi.requestEmailVerificationCode({
|
16 | 32 | client_id: ClientId.Wildmeta,
|
17 | 33 | user_email: 'test@gmail.com',
|
18 | 34 | });
|
19 | 35 |
|
| 36 | + // expected result: |
| 37 | + // null |
| 38 | + |
20 | 39 | expect(result).to.be.null;
|
21 | 40 | });
|
| 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 | + }); |
22 | 215 | });
|
0 commit comments