pwc-sdk-wallet 0.8.0 → 0.8.1

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.
package/README.md CHANGED
@@ -1503,6 +1503,90 @@ npm install pwc-wallet-sdk react-native-webview
1503
1503
 
1504
1504
  **Note:** This is a peer dependency. Mobile developers need to install it to use the DApp Browser features.
1505
1505
 
1506
+ ### React Native Polyfills (Required for TRON Support)
1507
+
1508
+ The SDK uses `tronweb` for TRON blockchain support, which requires Node.js polyfills in React Native. You **must** set up polyfills before using the SDK in a React Native app.
1509
+
1510
+ #### Option 1: Automatic Polyfills (Recommended)
1511
+
1512
+ The SDK automatically applies polyfills when using TRON features, but you still need to install the required packages:
1513
+
1514
+ ```bash
1515
+ npm install buffer process react-native-get-random-values
1516
+ ```
1517
+
1518
+ Then, at the **very top** of your app's entry file (e.g., `index.js` or `App.js`), import the polyfills:
1519
+
1520
+ ```typescript
1521
+ // At the VERY TOP of index.js or App.js - before any other imports
1522
+ import 'react-native-get-random-values';
1523
+ import { Buffer } from 'buffer';
1524
+ import process from 'process';
1525
+
1526
+ // Make them available globally
1527
+ global.Buffer = Buffer;
1528
+ global.process = process;
1529
+ ```
1530
+
1531
+ #### Option 2: Manual Setup
1532
+
1533
+ If you prefer more control, you can manually set up polyfills:
1534
+
1535
+ 1. Install dependencies:
1536
+ ```bash
1537
+ npm install buffer process react-native-get-random-values
1538
+ ```
1539
+
1540
+ 2. Create a polyfill file (e.g., `polyfills.js`):
1541
+ ```typescript
1542
+ import 'react-native-get-random-values';
1543
+ import { Buffer } from 'buffer';
1544
+ import process from 'process';
1545
+
1546
+ global.Buffer = Buffer;
1547
+ global.process = process;
1548
+ ```
1549
+
1550
+ 3. Import it at the top of your entry file:
1551
+ ```typescript
1552
+ import './polyfills';
1553
+ // ... rest of your imports
1554
+ ```
1555
+
1556
+ #### Troubleshooting
1557
+
1558
+ If you encounter errors like:
1559
+ - `RCTFatal` or `TronWebProto.Vote.serializeBinaryToWriter`
1560
+ - `Buffer is not defined`
1561
+ - `process is not defined`
1562
+ - `crypto.getRandomValues is not a function`
1563
+
1564
+ Make sure:
1565
+ 1. ✅ `react-native-get-random-values` is imported **first** (before any crypto operations)
1566
+ 2. ✅ `buffer` and `process` are installed and set as globals
1567
+ 3. ✅ Polyfills are imported **before** importing the SDK
1568
+ 4. ✅ You've restarted your Metro bundler after installing dependencies
1569
+
1570
+ #### Metro Config (Optional but Recommended)
1571
+
1572
+ For better compatibility, you can configure Metro to handle Node.js modules. Create or update `metro.config.js`:
1573
+
1574
+ ```javascript
1575
+ const { getDefaultConfig } = require('metro-config');
1576
+
1577
+ module.exports = (async () => {
1578
+ const {
1579
+ resolver: { sourceExts, assetExts },
1580
+ } = await getDefaultConfig();
1581
+
1582
+ return {
1583
+ resolver: {
1584
+ sourceExts: [...sourceExts, 'cjs'],
1585
+ },
1586
+ };
1587
+ })();
1588
+ ```
1589
+
1506
1590
  ## Quick Start
1507
1591
 
1508
1592
  ### 1. Basic Wallet Creation
@@ -4,6 +4,64 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.TronChainService = void 0;
7
+ const isReactNative = (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') ||
8
+ (typeof window !== 'undefined' && window?.ReactNativeWebView !== undefined) ||
9
+ (typeof global !== 'undefined' && global.__DEV__ !== undefined && typeof navigator !== 'undefined');
10
+ if (isReactNative || (typeof window === 'undefined' && typeof global !== 'undefined')) {
11
+ // Polyfill Buffer
12
+ if (typeof global.Buffer === 'undefined') {
13
+ try {
14
+ global.Buffer = require('buffer').Buffer;
15
+ }
16
+ catch (e) {
17
+ console.warn('[PWC SDK] Failed to load buffer polyfill:', e);
18
+ }
19
+ }
20
+ // Polyfill process
21
+ if (typeof global.process === 'undefined') {
22
+ try {
23
+ global.process = require('process');
24
+ }
25
+ catch (e) {
26
+ // Fallback minimal process object
27
+ global.process = {
28
+ env: {},
29
+ version: '',
30
+ versions: {},
31
+ platform: 'react-native',
32
+ nextTick: (fn) => setTimeout(() => fn(), 0),
33
+ browser: true,
34
+ };
35
+ }
36
+ }
37
+ // Ensure process.env exists
38
+ if (!global.process.env) {
39
+ global.process.env = {};
40
+ }
41
+ // Ensure crypto.getRandomValues is available
42
+ if (typeof global.crypto === 'undefined' || !global.crypto.getRandomValues) {
43
+ try {
44
+ require('react-native-get-random-values');
45
+ }
46
+ catch (e) {
47
+ // Fallback if react-native-get-random-values is not installed
48
+ if (typeof global.crypto === 'undefined') {
49
+ global.crypto = {};
50
+ }
51
+ if (!global.crypto.getRandomValues) {
52
+ global.crypto.getRandomValues = function (arr) {
53
+ if (arr) {
54
+ const view = new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength);
55
+ for (let i = 0; i < view.length; i++) {
56
+ view[i] = Math.floor(Math.random() * 256);
57
+ }
58
+ }
59
+ return arr;
60
+ };
61
+ }
62
+ }
63
+ }
64
+ }
7
65
  const tronweb_1 = __importDefault(require("tronweb"));
8
66
  /**
9
67
  * Service for interacting with TRON blockchain.
@@ -156,5 +156,5 @@ class HDKeyring {
156
156
  }
157
157
  }
158
158
  exports.HDKeyring = HDKeyring;
159
- // Cache để tránh tạo lại seed
159
+ // Cache to avoid recreating seed
160
160
  HDKeyring.seedCache = new Map();
@@ -137,7 +137,7 @@ class SolanaKeyring {
137
137
  if (data.type !== 'Solana' || !data.mnemonic) {
138
138
  throw new Error('Invalid data for SolanaKeyring deserialization.');
139
139
  }
140
- // Chỉ khôi phục mnemonic, không khôi phục lại accounts
140
+ // Only restore mnemonic, do not restore accounts
141
141
  return new SolanaKeyring(data.mnemonic);
142
142
  }
143
143
  /**
@@ -41,6 +41,64 @@ const bip32_1 = require("bip32");
41
41
  const ecc = __importStar(require("@bitcoinerlab/secp256k1"));
42
42
  const bip39 = __importStar(require("bip39"));
43
43
  const chains_1 = require("../config/chains");
44
+ const isReactNative = (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') ||
45
+ (typeof window !== 'undefined' && window?.ReactNativeWebView !== undefined) ||
46
+ (typeof global !== 'undefined' && global.__DEV__ !== undefined && typeof navigator !== 'undefined');
47
+ if (isReactNative || (typeof window === 'undefined' && typeof global !== 'undefined')) {
48
+ // Polyfill Buffer
49
+ if (typeof global.Buffer === 'undefined') {
50
+ try {
51
+ global.Buffer = require('buffer').Buffer;
52
+ }
53
+ catch (e) {
54
+ console.warn('[PWC SDK] Failed to load buffer polyfill:', e);
55
+ }
56
+ }
57
+ // Polyfill process
58
+ if (typeof global.process === 'undefined') {
59
+ try {
60
+ global.process = require('process');
61
+ }
62
+ catch (e) {
63
+ // Fallback minimal process object
64
+ global.process = {
65
+ env: {},
66
+ version: '',
67
+ versions: {},
68
+ platform: 'react-native',
69
+ nextTick: (fn) => setTimeout(() => fn(), 0),
70
+ browser: true,
71
+ };
72
+ }
73
+ }
74
+ // Ensure process.env exists
75
+ if (!global.process.env) {
76
+ global.process.env = {};
77
+ }
78
+ // Ensure crypto.getRandomValues is available
79
+ if (typeof global.crypto === 'undefined' || !global.crypto.getRandomValues) {
80
+ try {
81
+ require('react-native-get-random-values');
82
+ }
83
+ catch (e) {
84
+ // Fallback if react-native-get-random-values is not installed
85
+ if (typeof global.crypto === 'undefined') {
86
+ global.crypto = {};
87
+ }
88
+ if (!global.crypto.getRandomValues) {
89
+ global.crypto.getRandomValues = function (arr) {
90
+ if (arr) {
91
+ const view = new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength);
92
+ for (let i = 0; i < view.length; i++) {
93
+ view[i] = Math.floor(Math.random() * 256);
94
+ }
95
+ }
96
+ return arr;
97
+ };
98
+ }
99
+ }
100
+ }
101
+ }
44
102
  const tronweb_1 = __importDefault(require("tronweb"));
45
103
  /**
46
104
  * TRON keyring for managing multiple TRON accounts
@@ -166,7 +224,7 @@ class TronKeyring {
166
224
  if (data.type !== 'Tron' || !data.mnemonic) {
167
225
  throw new Error('Invalid data for TronKeyring deserialization.');
168
226
  }
169
- // Chỉ khôi phục mnemonic, không khôi phục lại accounts
227
+ // Only restore mnemonic, do not restore accounts
170
228
  return new TronKeyring(data.mnemonic);
171
229
  }
172
230
  /**
@@ -0,0 +1,34 @@
1
+ /**
2
+ * React Native Polyfills
3
+ *
4
+ * This file provides necessary polyfills for Node.js APIs that are required
5
+ * by dependencies like tronweb (which uses Protocol Buffers).
6
+ *
7
+ * IMPORTANT: This file must be imported at the very beginning of your app entry point
8
+ * (e.g., index.js or App.js) BEFORE any other imports that use these polyfills.
9
+ *
10
+ * Usage in React Native app:
11
+ * ```typescript
12
+ * // At the very top of index.js or App.js
13
+ * import 'pwc-sdk-wallet/dist/polyfills/react-native';
14
+ *
15
+ * // Then import other modules
16
+ * import { Vault } from 'pwc-sdk-wallet';
17
+ * ```
18
+ *
19
+ * Alternatively, you can install the required polyfills in your app:
20
+ * ```bash
21
+ * npm install buffer process react-native-get-random-values
22
+ * ```
23
+ *
24
+ * And import them at the top of your entry file:
25
+ * ```typescript
26
+ * import 'react-native-get-random-values';
27
+ * import { Buffer } from 'buffer';
28
+ * import process from 'process';
29
+ *
30
+ * global.Buffer = Buffer;
31
+ * global.process = process;
32
+ * ```
33
+ */
34
+ export {};
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+ /**
3
+ * React Native Polyfills
4
+ *
5
+ * This file provides necessary polyfills for Node.js APIs that are required
6
+ * by dependencies like tronweb (which uses Protocol Buffers).
7
+ *
8
+ * IMPORTANT: This file must be imported at the very beginning of your app entry point
9
+ * (e.g., index.js or App.js) BEFORE any other imports that use these polyfills.
10
+ *
11
+ * Usage in React Native app:
12
+ * ```typescript
13
+ * // At the very top of index.js or App.js
14
+ * import 'pwc-sdk-wallet/dist/polyfills/react-native';
15
+ *
16
+ * // Then import other modules
17
+ * import { Vault } from 'pwc-sdk-wallet';
18
+ * ```
19
+ *
20
+ * Alternatively, you can install the required polyfills in your app:
21
+ * ```bash
22
+ * npm install buffer process react-native-get-random-values
23
+ * ```
24
+ *
25
+ * And import them at the top of your entry file:
26
+ * ```typescript
27
+ * import 'react-native-get-random-values';
28
+ * import { Buffer } from 'buffer';
29
+ * import process from 'process';
30
+ *
31
+ * global.Buffer = Buffer;
32
+ * global.process = process;
33
+ * ```
34
+ */
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ const isReactNative = typeof navigator !== 'undefined' && navigator.product === 'ReactNative' ||
37
+ (typeof window !== 'undefined' && window?.ReactNativeWebView !== undefined);
38
+ // Only apply polyfills in React Native environment
39
+ if (isReactNative || typeof window === 'undefined') {
40
+ // Polyfill Buffer
41
+ if (typeof global.Buffer === 'undefined') {
42
+ try {
43
+ global.Buffer = require('buffer').Buffer;
44
+ }
45
+ catch (e) {
46
+ console.warn('Failed to load buffer polyfill:', e);
47
+ }
48
+ }
49
+ // Polyfill process
50
+ if (typeof global.process === 'undefined') {
51
+ try {
52
+ global.process = require('process');
53
+ }
54
+ catch (e) {
55
+ // Fallback minimal process object
56
+ global.process = {
57
+ env: {},
58
+ version: '',
59
+ versions: {},
60
+ platform: 'react-native',
61
+ nextTick: (fn) => setTimeout(() => fn(), 0),
62
+ browser: true,
63
+ };
64
+ }
65
+ }
66
+ // Ensure process.env exists
67
+ if (!global.process.env) {
68
+ global.process.env = {};
69
+ }
70
+ // Polyfill crypto.getRandomValues (required by react-native-get-random-values)
71
+ // This should be imported by the app, but we ensure it's available
72
+ if (typeof global.crypto === 'undefined' || !global.crypto.getRandomValues) {
73
+ try {
74
+ // Try to use react-native-get-random-values if available
75
+ require('react-native-get-random-values');
76
+ }
77
+ catch (e) {
78
+ // Fallback implementation
79
+ if (typeof global.crypto === 'undefined') {
80
+ global.crypto = {};
81
+ }
82
+ if (!global.crypto.getRandomValues) {
83
+ global.crypto.getRandomValues = function (arr) {
84
+ if (arr) {
85
+ const view = new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength);
86
+ for (let i = 0; i < view.length; i++) {
87
+ view[i] = Math.floor(Math.random() * 256);
88
+ }
89
+ }
90
+ return arr;
91
+ };
92
+ }
93
+ }
94
+ }
95
+ // Polyfill TextEncoder/TextDecoder if not available
96
+ if (typeof global.TextEncoder === 'undefined') {
97
+ try {
98
+ const { TextEncoder, TextDecoder } = require('util');
99
+ global.TextEncoder = TextEncoder;
100
+ global.TextDecoder = TextDecoder;
101
+ }
102
+ catch (e) {
103
+ // Use polyfill if util is not available
104
+ try {
105
+ const { TextEncoder: TE, TextDecoder: TD } = require('text-encoding');
106
+ global.TextEncoder = TE;
107
+ global.TextDecoder = TD;
108
+ }
109
+ catch (e2) {
110
+ console.warn('TextEncoder/TextDecoder polyfills not available');
111
+ }
112
+ }
113
+ }
114
+ // Ensure global is available
115
+ if (typeof global === 'undefined' && typeof window !== 'undefined') {
116
+ window.global = window;
117
+ }
118
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pwc-sdk-wallet",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
4
4
  "description": "A comprehensive wallet SDK for React Native (pwc), supporting multi-chain and multi-account features.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -44,6 +44,7 @@
44
44
  "bs58": "^6.0.0",
45
45
  "buffer": "^6.0.3",
46
46
  "crypto-js": "^4.2.0",
47
+ "process": "^0.11.10",
47
48
  "ed25519-hd-key": "^1.3.0",
48
49
  "react-native-camera": "^4.2.1",
49
50
  "react-native-permissions": "^5.4.1",