@jolibox/implement 1.3.2 → 1.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,6 +10,6 @@ export declare const createAPI: <ParamsSchema extends import("@/common/api-facto
10
10
  }) => (...inputs: Inputs) => import("@jolibox/types").StandardResponse<Return>;
11
11
  export declare const registerCanIUse: (name: string, config: import("@jolibox/common").CanIUseInfo) => void;
12
12
  import { t } from '@/common/api-factory/validator';
13
+ export { canIUseNative } from '@/native/utils/can-iuse-native';
13
14
  export { t };
14
- export declare const canIUseNative: (schema: string) => boolean;
15
15
  export { convertVersionCodeToVString } from './utils';
@@ -11,3 +11,4 @@ import './runtime';
11
11
  import './is-native-support';
12
12
  import './call-host-method';
13
13
  import './payment';
14
+ import './rewards';
@@ -0,0 +1 @@
1
+ export {};
@@ -20,5 +20,6 @@ export declare class NativeTaskTracker extends TaskTracker {
20
20
  tracker(event: TrackEvent, info?: Record<string, unknown> | null): void;
21
21
  start(duration?: number): void;
22
22
  private tryReportOpenGamePlus;
23
+ private createTask;
23
24
  }
24
25
  export {};
@@ -0,0 +1 @@
1
+ export declare const canIUseNative: (schema: string) => boolean;
@@ -1,9 +1,9 @@
1
1
  Invoking: npm run clean && npm run build:esm && tsc
2
2
 
3
- > @jolibox/implement@1.3.2 clean
3
+ > @jolibox/implement@1.3.3 clean
4
4
  > rimraf ./dist
5
5
 
6
6
 
7
- > @jolibox/implement@1.3.2 build:esm
7
+ > @jolibox/implement@1.3.3 build:esm
8
8
  > BUILD_VERSION=$(node -p "require('./package.json').version") node esbuild.config.js --format=esm
9
9
 
package/package.json CHANGED
@@ -1,17 +1,17 @@
1
1
  {
2
2
  "name": "@jolibox/implement",
3
3
  "description": "This project is Jolibox JS-SDk implement for Native && H5",
4
- "version": "1.3.2",
4
+ "version": "1.3.3",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
7
7
  "license": "MIT",
8
8
  "dependencies": {
9
- "@jolibox/common": "1.3.2",
10
- "@jolibox/types": "1.3.2",
11
- "@jolibox/native-bridge": "1.3.2",
12
- "@jolibox/ads": "1.3.2",
9
+ "@jolibox/common": "1.3.3",
10
+ "@jolibox/types": "1.3.3",
11
+ "@jolibox/native-bridge": "1.3.3",
12
+ "@jolibox/ads": "1.3.3",
13
13
  "localforage": "1.10.0",
14
- "@jolibox/ui": "1.0.0",
14
+ "@jolibox/ui": "1.3.3",
15
15
  "web-vitals": "4.2.4"
16
16
  },
17
17
  "devDependencies": {
@@ -20,7 +20,7 @@
20
20
  "@types/node": "18.0.0",
21
21
  "rimraf": "6.0.1",
22
22
  "esbuild": "0.24.2",
23
- "@jolibox/eslint-config": "1.0.0"
23
+ "@jolibox/eslint-config": "1.0.1-beta.16"
24
24
  },
25
25
  "scripts": {
26
26
  "clean": "rimraf ./dist",
@@ -0,0 +1,102 @@
1
+ import { PUBLIC_KEY_PEM } from './public';
2
+
3
+ function arrayBufferToBase64(buffer: ArrayBuffer) {
4
+ let binary = '';
5
+ const bytes = new Uint8Array(buffer);
6
+ const len = bytes.byteLength;
7
+ for (let i = 0; i < len; i++) {
8
+ binary += String.fromCharCode(bytes[i]);
9
+ }
10
+ return window.btoa(binary);
11
+ }
12
+
13
+ function importPublicKeyFromString(pem: string) {
14
+ const pemHeader = '-----BEGIN PUBLIC KEY-----';
15
+ const pemFooter = '-----END PUBLIC KEY-----';
16
+ const pemContents = pem
17
+ .substring(pem.indexOf(pemHeader) + pemHeader.length, pem.indexOf(pemFooter))
18
+ .replace(/\s+/g, '');
19
+
20
+ const binaryDerString = window.atob(pemContents);
21
+ const binaryDer = new Uint8Array(binaryDerString.length);
22
+ for (let i = 0; i < binaryDerString.length; i++) {
23
+ binaryDer[i] = binaryDerString.charCodeAt(i);
24
+ }
25
+
26
+ return binaryDer.buffer;
27
+ }
28
+
29
+ interface ReportBody {
30
+ eventType: string;
31
+ gameInfo: {
32
+ gameId: string;
33
+ sessionId: string;
34
+ };
35
+ }
36
+
37
+ export async function generateEncryptedPayload(payload: ReportBody) {
38
+ const crypto = window.crypto;
39
+ const subtle = crypto.subtle;
40
+
41
+ const keyBuffer = importPublicKeyFromString(PUBLIC_KEY_PEM);
42
+
43
+ const publicKey = await subtle.importKey(
44
+ 'spki',
45
+ keyBuffer,
46
+ {
47
+ name: 'RSA-OAEP',
48
+ hash: 'SHA-1'
49
+ },
50
+ false,
51
+ ['encrypt']
52
+ );
53
+
54
+ const aesKeyRaw = crypto.getRandomValues(new Uint8Array(32));
55
+ const aesKeyObj = await subtle.importKey('raw', aesKeyRaw, 'AES-GCM', false, ['encrypt']);
56
+
57
+ const encryptedAesKeyBuffer = await subtle.encrypt(
58
+ {
59
+ name: 'RSA-OAEP'
60
+ },
61
+ publicKey,
62
+ aesKeyRaw
63
+ );
64
+
65
+ const k = arrayBufferToBase64(encryptedAesKeyBuffer);
66
+
67
+ const timestamp = Date.now();
68
+ const payloadJson = {
69
+ timestamp,
70
+ data: payload
71
+ };
72
+
73
+ const encoder = new TextEncoder();
74
+ const payloadBytes = encoder.encode(JSON.stringify(payloadJson));
75
+
76
+ const iv = crypto.getRandomValues(new Uint8Array(12));
77
+
78
+ const ciphertextWithTagBuffer = await subtle.encrypt(
79
+ {
80
+ name: 'AES-GCM',
81
+ iv: iv,
82
+ tagLength: 128
83
+ },
84
+ aesKeyObj,
85
+ payloadBytes
86
+ );
87
+
88
+ const ciphertextWithTag = new Uint8Array(ciphertextWithTagBuffer);
89
+ const finalPayloadBuffer = new Uint8Array(iv.length + ciphertextWithTag.length);
90
+
91
+ finalPayloadBuffer.set(iv, 0); // 先放 IV
92
+ finalPayloadBuffer.set(ciphertextWithTag, iv.length); // 再放 Ciphertext + Tag
93
+
94
+ const encryptedPayload = arrayBufferToBase64(finalPayloadBuffer.buffer);
95
+ const data = {
96
+ k: k,
97
+ v: '1.0',
98
+ payload: encryptedPayload
99
+ };
100
+
101
+ return data;
102
+ }
@@ -0,0 +1,9 @@
1
+ export const PUBLIC_KEY_PEM = `-----BEGIN PUBLIC KEY-----
2
+ MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAs29BUZPZdQwNDEYtXufp
3
+ IGF40RBoddFzoD2ixl6PjyKWFXF8kxGS++HNtLspxhLvWoE12IZvrP7+whSpgHcy
4
+ 4ryuW86mOdHnP9iyeK69GosHz3lVhQjF9UdhJDXB7P/zdj3VlVzZIj0yL+YvZjO0
5
+ F9vHrK8GMr1/gf3HKAroVJUVmJlWjqhBGoQzb58mqbTSWSN0HkDdYwXHNh/Xc1le
6
+ DJDbBmfKOQdlBjABR9gb1wGz2LPFA40kzROV9mMiVePXpCOKNMy+b0UOW4RoF7pi
7
+ Rq0jHJ4fRrMWvf/GugT0mv8YyuU9DaXLqTGP+jOEVvzvgtkO84XtLW1vh5ggINPW
8
+ 2wIDAQAB
9
+ -----END PUBLIC KEY-----`;
@@ -8,6 +8,7 @@ import { EventEmitter, logger } from '@jolibox/common';
8
8
  import { httpClientManager } from '../http';
9
9
  import type { Track } from '.';
10
10
  import type { TrackEvent } from '@jolibox/types';
11
+ import { generateEncryptedPayload } from './encrypt';
11
12
 
12
13
  export class H5TaskTracker extends TaskTracker {
13
14
  private gameId: string;
@@ -35,9 +36,17 @@ export class H5TaskTracker extends TaskTracker {
35
36
  if (extraParams) {
36
37
  Object.assign(reportBody, extraParams);
37
38
  }
38
- await this.httpClient.post(`/api/base/app-event`, {
39
- data: reportBody
40
- });
39
+
40
+ try {
41
+ const encryptedBody = await generateEncryptedPayload(reportBody);
42
+ await this.httpClient.post(`/api/base/app-event/v2`, {
43
+ data: encryptedBody
44
+ });
45
+ } catch (e) {
46
+ await this.httpClient.post(`/api/base/app-event`, {
47
+ data: reportBody
48
+ });
49
+ }
41
50
  }
42
51
  tracker(event: TrackEvent, info: Record<string, unknown> | null = null) {
43
52
  this.track(event, info);
@@ -5,12 +5,7 @@ export const { createAPI, createSyncAPI } = createAPIFactory(track);
5
5
 
6
6
  export const registerCanIUse = registerCanIUseFactory('native');
7
7
  import { t } from '@/common/api-factory/validator';
8
- import { registerCanIUseContext } from '@jolibox/native-bridge';
9
- import { context } from '@/common/context';
8
+ export { canIUseNative } from '@/native/utils/can-iuse-native';
10
9
  export { t };
11
10
 
12
- export const { canIUseNative } = registerCanIUseContext(
13
- () => context.sdkInfo.nativeSDKVersionCode,
14
- () => context.platform
15
- );
16
11
  export { convertVersionCodeToVString } from './utils';
@@ -11,3 +11,4 @@ import './runtime';
11
11
  import './is-native-support';
12
12
  import './call-host-method';
13
13
  import './payment';
14
+ import './rewards';
@@ -0,0 +1,85 @@
1
+ import { BaseError, wrapUserFunction, createCommands } from '@jolibox/common';
2
+ import { canIUseNative, createAPI, registerCanIUse, t } from './base';
3
+ import { StandardResponse, ICoinDetailsData } from '@jolibox/types';
4
+ import { context } from '@/common/context';
5
+ import { createAPIError } from '@/common/report/errors';
6
+ import { invokeNative } from '@jolibox/native-bridge';
7
+
8
+ const commands = createCommands();
9
+
10
+ const safeCallbackWrapper = wrapUserFunction(reportError as (err: Error | BaseError) => void);
11
+
12
+ const getCoinDetails = createAPI('getCoinDetails', {
13
+ paramsSchema: t.tuple(t.function().optional()),
14
+ implement: async (
15
+ onUpdate?: (data: ICoinDetailsData) => void | Promise<void>
16
+ ): Promise<StandardResponse<ICoinDetailsData>> => {
17
+ try {
18
+ const currentType = context.mpType;
19
+ // check mp type
20
+ if (currentType !== 'miniApp') {
21
+ reportError(
22
+ createAPIError({
23
+ code: -1,
24
+ msg: '[JoliboxSDK]: rewards info not supported in games. Only supported in mini apps.'
25
+ })
26
+ );
27
+ return {
28
+ code: 'FAILURE',
29
+ message: '[JoliboxSDK]: rewards info not supported in games. Only supported in mini apps.'
30
+ };
31
+ }
32
+
33
+ // check native version
34
+ if (!canIUseNative('requestCoinDetailsSync')) {
35
+ reportError(
36
+ createAPIError({
37
+ code: -1,
38
+ msg: '[JoliboxSDK]: rewards info not supported in this platform.'
39
+ })
40
+ );
41
+ return {
42
+ code: 'FAILURE',
43
+ message: '[JoliboxSDK]: rewards info not supported in this platform. lower than 1.7.1'
44
+ };
45
+ }
46
+
47
+ const customOnUpdate = onUpdate
48
+ ? safeCallbackWrapper(onUpdate)
49
+ : (() => {
50
+ console.log('default update function called');
51
+ }).bind(this);
52
+ const { errNo, errMsg, data } = invokeNative('requestCoinDetailsSync');
53
+ if (errNo !== 0) {
54
+ throw createAPIError({
55
+ code: errNo ?? -1,
56
+ msg: errMsg
57
+ });
58
+ }
59
+ console.log('=====> sync data', data, errNo);
60
+
61
+ invokeNative('requestCoinDetailsAsync').then((res) => {
62
+ if (res.errNo == 0 && res.data) {
63
+ customOnUpdate(res.data);
64
+ console.log('=====> res.data', res.data);
65
+ }
66
+ });
67
+ return {
68
+ code: 'SUCCESS',
69
+ message: 'Successfully retrieved coin details',
70
+ data
71
+ };
72
+ } catch (error) {
73
+ return {
74
+ code: 'FAILURE',
75
+ message: '[JoliboxSDK]: Failed to get coin details: ' + (error as Error).message
76
+ };
77
+ }
78
+ }
79
+ });
80
+
81
+ commands.registerCommand('RewardsSDK.getCoinDetails', getCoinDetails);
82
+
83
+ registerCanIUse('rewards.getCoinDetails', {
84
+ version: '1.3.2'
85
+ });
@@ -9,6 +9,7 @@ import { applyNative } from '@jolibox/native-bridge';
9
9
  import type { Track } from '.';
10
10
  import type { TrackEvent } from '@jolibox/types';
11
11
  import { getGlobalStorage, setGlobalStorage } from '../api/storage';
12
+ import { canIUseNative } from '@/native/utils/can-iuse-native';
12
13
 
13
14
  const REPORT_FIRST_OPEN_GAME = 'REPORT_FIRST_OPEN_GAME';
14
15
 
@@ -55,16 +56,7 @@ export class NativeTaskTracker extends TaskTracker {
55
56
  Object.assign(reportBody, extraParams);
56
57
  }
57
58
 
58
- const reportTask =
59
- context.platform === 'android'
60
- ? fetch(`/api/base/app-event`, {
61
- method: 'POST',
62
- data: reportBody
63
- })
64
- : applyNative('trackAppEventAsync', {
65
- url: this.taskUrl,
66
- body: JSON.stringify(reportBody)
67
- });
59
+ const reportTask = this.createTask(reportBody);
68
60
  await reportTask;
69
61
  }
70
62
 
@@ -96,4 +88,27 @@ export class NativeTaskTracker extends TaskTracker {
96
88
  setGlobalStorage(REPORT_FIRST_OPEN_GAME, true);
97
89
  });
98
90
  }
91
+
92
+ private createTask(reportBody: {
93
+ eventType: string;
94
+ gameInfo: {
95
+ gameId: string;
96
+ sessionId: string;
97
+ };
98
+ }) {
99
+ if (canIUseNative('sendServerEventAsync')) {
100
+ return applyNative('sendServerEventAsync', {
101
+ payload: reportBody
102
+ });
103
+ }
104
+ return context.platform === 'android'
105
+ ? fetch(`/api/base/app-event`, {
106
+ method: 'POST',
107
+ data: reportBody
108
+ })
109
+ : applyNative('trackAppEventAsync', {
110
+ url: this.taskUrl,
111
+ body: JSON.stringify(reportBody)
112
+ });
113
+ }
99
114
  }
@@ -0,0 +1,7 @@
1
+ import { context } from '@/common/context';
2
+ import { registerCanIUseContext } from '@jolibox/native-bridge';
3
+
4
+ export const { canIUseNative } = registerCanIUseContext(
5
+ () => context.sdkInfo.nativeSDKVersionCode,
6
+ () => context.platform
7
+ );