@spacesops/wdk-react-native-provider 1.0.0-beta.10

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.
Files changed (89) hide show
  1. package/LICENSE +176 -0
  2. package/README.md +841 -0
  3. package/lib/module/contexts/constants.js +20 -0
  4. package/lib/module/contexts/constants.js.map +1 -0
  5. package/lib/module/contexts/reducer.js +87 -0
  6. package/lib/module/contexts/reducer.js.map +1 -0
  7. package/lib/module/contexts/types.js +4 -0
  8. package/lib/module/contexts/types.js.map +1 -0
  9. package/lib/module/contexts/wallet-context.js +409 -0
  10. package/lib/module/contexts/wallet-context.js.map +1 -0
  11. package/lib/module/index.js +15 -0
  12. package/lib/module/index.js.map +1 -0
  13. package/lib/module/package.json +1 -0
  14. package/lib/module/polyfills.js +34 -0
  15. package/lib/module/polyfills.js.map +1 -0
  16. package/lib/module/services/wdk-service/bare-api.js +69 -0
  17. package/lib/module/services/wdk-service/bare-api.js.map +1 -0
  18. package/lib/module/services/wdk-service/index.js +506 -0
  19. package/lib/module/services/wdk-service/index.js.map +1 -0
  20. package/lib/module/services/wdk-service/types.js +48 -0
  21. package/lib/module/services/wdk-service/types.js.map +1 -0
  22. package/lib/module/services/wdk-service/wdk-encryption-salt.js +29 -0
  23. package/lib/module/services/wdk-service/wdk-encryption-salt.js.map +1 -0
  24. package/lib/module/services/wdk-service/wdk-secret-manager-storage.js +64 -0
  25. package/lib/module/services/wdk-service/wdk-secret-manager-storage.js.map +1 -0
  26. package/lib/module/services/wdk-service/wdk-secret-manager-worklet.bundle.js +1 -0
  27. package/lib/module/services/wdk-service/wdk-worklet.mobile.bundle.js +1 -0
  28. package/lib/module/spec/hrpc/hrpc.json +66 -0
  29. package/lib/module/spec/hrpc/index.js +121 -0
  30. package/lib/module/spec/hrpc/index.js.map +1 -0
  31. package/lib/module/spec/hrpc/messages.js +291 -0
  32. package/lib/module/spec/hrpc/messages.js.map +1 -0
  33. package/lib/module/spec/schema/index.js +291 -0
  34. package/lib/module/spec/schema/index.js.map +1 -0
  35. package/lib/module/spec/schema/schema.json +186 -0
  36. package/lib/module/utils/get-balances-from-balance-map.js +18 -0
  37. package/lib/module/utils/get-balances-from-balance-map.js.map +1 -0
  38. package/lib/module/utils/get-transactions-from-transaction-map.js +10 -0
  39. package/lib/module/utils/get-transactions-from-transaction-map.js.map +1 -0
  40. package/lib/module/worklet/wdk-secret-manager-worklet.js +106 -0
  41. package/lib/module/worklet/wdk-secret-manager-worklet.js.map +1 -0
  42. package/lib/typescript/package.json +1 -0
  43. package/lib/typescript/src/contexts/constants.d.ts +3 -0
  44. package/lib/typescript/src/contexts/constants.d.ts.map +1 -0
  45. package/lib/typescript/src/contexts/reducer.d.ts +38 -0
  46. package/lib/typescript/src/contexts/reducer.d.ts.map +1 -0
  47. package/lib/typescript/src/contexts/types.d.ts +40 -0
  48. package/lib/typescript/src/contexts/types.d.ts.map +1 -0
  49. package/lib/typescript/src/contexts/wallet-context.d.ts +10 -0
  50. package/lib/typescript/src/contexts/wallet-context.d.ts.map +1 -0
  51. package/lib/typescript/src/index.d.ts +7 -0
  52. package/lib/typescript/src/index.d.ts.map +1 -0
  53. package/lib/typescript/src/polyfills.d.ts +7 -0
  54. package/lib/typescript/src/polyfills.d.ts.map +1 -0
  55. package/lib/typescript/src/services/wdk-service/bare-api.d.ts +32 -0
  56. package/lib/typescript/src/services/wdk-service/bare-api.d.ts.map +1 -0
  57. package/lib/typescript/src/services/wdk-service/index.d.ts +68 -0
  58. package/lib/typescript/src/services/wdk-service/index.d.ts.map +1 -0
  59. package/lib/typescript/src/services/wdk-service/types.d.ts +125 -0
  60. package/lib/typescript/src/services/wdk-service/types.d.ts.map +1 -0
  61. package/lib/typescript/src/services/wdk-service/wdk-encryption-salt.d.ts +6 -0
  62. package/lib/typescript/src/services/wdk-service/wdk-encryption-salt.d.ts.map +1 -0
  63. package/lib/typescript/src/services/wdk-service/wdk-secret-manager-storage.d.ts +13 -0
  64. package/lib/typescript/src/services/wdk-service/wdk-secret-manager-storage.d.ts.map +1 -0
  65. package/lib/typescript/src/utils/get-balances-from-balance-map.d.ts +4 -0
  66. package/lib/typescript/src/utils/get-balances-from-balance-map.d.ts.map +1 -0
  67. package/lib/typescript/src/utils/get-transactions-from-transaction-map.d.ts +4 -0
  68. package/lib/typescript/src/utils/get-transactions-from-transaction-map.d.ts.map +1 -0
  69. package/metro-polyfills.js +89 -0
  70. package/package.json +152 -0
  71. package/src/contexts/constants.ts +19 -0
  72. package/src/contexts/reducer.ts +103 -0
  73. package/src/contexts/types.ts +51 -0
  74. package/src/contexts/wallet-context.tsx +411 -0
  75. package/src/index.tsx +28 -0
  76. package/src/polyfills.ts +31 -0
  77. package/src/services/wdk-service/bare-api.ts +88 -0
  78. package/src/services/wdk-service/index.ts +765 -0
  79. package/src/services/wdk-service/types.ts +137 -0
  80. package/src/services/wdk-service/wdk-encryption-salt.ts +30 -0
  81. package/src/services/wdk-service/wdk-secret-manager-storage.ts +102 -0
  82. package/src/spec/hrpc/hrpc.json +66 -0
  83. package/src/spec/hrpc/index.js +228 -0
  84. package/src/spec/hrpc/messages.js +328 -0
  85. package/src/spec/schema/index.js +328 -0
  86. package/src/spec/schema/schema.json +186 -0
  87. package/src/utils/get-balances-from-balance-map.ts +22 -0
  88. package/src/utils/get-transactions-from-transaction-map.ts +18 -0
  89. package/src/worklet/wdk-secret-manager-worklet.js +118 -0
@@ -0,0 +1,186 @@
1
+ {
2
+ "version": 1,
3
+ "schema": [
4
+ {
5
+ "name": "command-workletStart-request",
6
+ "namespace": "wdk-secret-manager",
7
+ "compact": false,
8
+ "flagsPosition": 0,
9
+ "fields": [
10
+ {
11
+ "name": "enableDebugLogs",
12
+ "required": false,
13
+ "type": "uint",
14
+ "version": 1
15
+ }
16
+ ]
17
+ },
18
+ {
19
+ "name": "command-workletStart-response",
20
+ "namespace": "wdk-secret-manager",
21
+ "compact": false,
22
+ "flagsPosition": 0,
23
+ "fields": [
24
+ {
25
+ "name": "status",
26
+ "type": "string",
27
+ "version": 1
28
+ }
29
+ ]
30
+ },
31
+ {
32
+ "name": "command-workletStop-request",
33
+ "namespace": "wdk-secret-manager",
34
+ "compact": false,
35
+ "flagsPosition": 0,
36
+ "fields": [
37
+ {
38
+ "name": "payload",
39
+ "required": false,
40
+ "type": "string",
41
+ "version": 1
42
+ }
43
+ ]
44
+ },
45
+ {
46
+ "name": "command-workletStop-response",
47
+ "namespace": "wdk-secret-manager",
48
+ "compact": false,
49
+ "flagsPosition": 0,
50
+ "fields": [
51
+ {
52
+ "name": "status",
53
+ "type": "string",
54
+ "version": 1
55
+ }
56
+ ]
57
+ },
58
+ {
59
+ "name": "command-generateAndEncrypt-request",
60
+ "namespace": "wdk-secret-manager",
61
+ "compact": false,
62
+ "flagsPosition": 0,
63
+ "fields": [
64
+ {
65
+ "name": "passkey",
66
+ "type": "string",
67
+ "version": 1
68
+ },
69
+ {
70
+ "name": "salt",
71
+ "type": "string",
72
+ "version": 1
73
+ },
74
+ {
75
+ "name": "seedPhrase",
76
+ "required": false,
77
+ "type": "string",
78
+ "version": 1
79
+ },
80
+ {
81
+ "name": "derivedKey",
82
+ "required": false,
83
+ "type": "string",
84
+ "version": 1
85
+ }
86
+ ]
87
+ },
88
+ {
89
+ "name": "command-generateAndEncrypt-response",
90
+ "namespace": "wdk-secret-manager",
91
+ "compact": false,
92
+ "flagsPosition": 0,
93
+ "fields": [
94
+ {
95
+ "name": "encryptedEntropy",
96
+ "type": "string",
97
+ "version": 1
98
+ },
99
+ {
100
+ "name": "encryptedSeed",
101
+ "type": "string",
102
+ "version": 1
103
+ }
104
+ ]
105
+ },
106
+ {
107
+ "name": "command-decrypt-request",
108
+ "namespace": "wdk-secret-manager",
109
+ "compact": false,
110
+ "flagsPosition": 0,
111
+ "fields": [
112
+ {
113
+ "name": "passkey",
114
+ "type": "string",
115
+ "version": 1
116
+ },
117
+ {
118
+ "name": "salt",
119
+ "type": "string",
120
+ "version": 1
121
+ },
122
+ {
123
+ "name": "encryptedData",
124
+ "type": "string",
125
+ "version": 1
126
+ },
127
+ {
128
+ "name": "derivedKey",
129
+ "required": false,
130
+ "type": "string",
131
+ "version": 1
132
+ }
133
+ ]
134
+ },
135
+ {
136
+ "name": "command-decrypt-response",
137
+ "namespace": "wdk-secret-manager",
138
+ "compact": false,
139
+ "flagsPosition": 0,
140
+ "fields": [
141
+ {
142
+ "name": "result",
143
+ "type": "string",
144
+ "version": 1
145
+ }
146
+ ]
147
+ },
148
+ {
149
+ "name": "log-type-enum",
150
+ "namespace": "wdk-secret-manager",
151
+ "offset": 1,
152
+ "enum": [
153
+ {
154
+ "key": "info",
155
+ "version": 1
156
+ },
157
+ {
158
+ "key": "error",
159
+ "version": 1
160
+ },
161
+ {
162
+ "key": "debug",
163
+ "version": 1
164
+ }
165
+ ]
166
+ },
167
+ {
168
+ "name": "command-log-request",
169
+ "namespace": "wdk-secret-manager",
170
+ "compact": false,
171
+ "flagsPosition": 0,
172
+ "fields": [
173
+ {
174
+ "name": "type",
175
+ "type": "@tetherto/wdk-secret-manager/log-type-enum",
176
+ "version": 1
177
+ },
178
+ {
179
+ "name": "data",
180
+ "type": "string",
181
+ "version": 1
182
+ }
183
+ ]
184
+ }
185
+ ]
186
+ }
@@ -0,0 +1,22 @@
1
+ import type {
2
+ Amount,
3
+ BalanceMap,
4
+ NetworkType,
5
+ } from '../services/wdk-service/types';
6
+
7
+ const getBalancesFromBalanceMap = (balanceMap: BalanceMap): Amount[] => {
8
+ const balances = Object.entries(balanceMap).map(
9
+ ([key, { balance, asset }]) => {
10
+ const [networkType] = key.split('_') as [NetworkType];
11
+ return {
12
+ networkType,
13
+ denomination: asset,
14
+ value: balance.toString(),
15
+ };
16
+ }
17
+ );
18
+
19
+ return balances;
20
+ };
21
+
22
+ export default getBalancesFromBalanceMap;
@@ -0,0 +1,18 @@
1
+ import type {
2
+ Transaction,
3
+ TransactionMap,
4
+ } from '../services/wdk-service/types';
5
+
6
+ const getTransactionsFromTransactionMap = (
7
+ transactionMap: TransactionMap
8
+ ): Transaction[] => {
9
+ const transactions: Transaction[] = Object.entries(transactionMap).reduce(
10
+ (allTransactions, [_, txArray]) => {
11
+ return allTransactions.concat(txArray);
12
+ },
13
+ [] as Transaction[]
14
+ );
15
+ return transactions;
16
+ };
17
+
18
+ export default getTransactionsFromTransactionMap;
@@ -0,0 +1,118 @@
1
+ const HRPC = require('../spec/hrpc');
2
+
3
+ const b4a = require('b4a');
4
+ const { getEnum } = require('../spec/schema');
5
+ const { WdkSecretManager } = require('@tetherto/wdk-secret-manager');
6
+ // eslint-disable-next-line no-undef
7
+ const { IPC } = BareKit;
8
+
9
+ const rpc = new HRPC(IPC);
10
+
11
+ const logEnums = getEnum('@tetherto/wdk-secret-manager/log-type-enum');
12
+
13
+ /**
14
+ *
15
+ * @param {string} enable = 0|1
16
+ * @param {logEnums} type
17
+ * @param {string} content
18
+ */
19
+ function sendLog(type, content) {
20
+ rpc.commandLog({
21
+ type: type,
22
+ data: content,
23
+ });
24
+ }
25
+
26
+ let enableDebugLogs = false;
27
+
28
+ function wrapConsole() {
29
+ const logPrefix = '[WDK-SECRET-MANAGER-WORKLET]';
30
+ const format = (...args) =>
31
+ args
32
+ .map((arg) => {
33
+ if (typeof arg === 'object') {
34
+ try {
35
+ return JSON.stringify(arg);
36
+ } catch {
37
+ return '[Circular]';
38
+ }
39
+ }
40
+ return arg;
41
+ })
42
+ .join(' ');
43
+
44
+ console.error = (...args) => {
45
+ const content = `${logPrefix} ${format(...args)}`;
46
+ sendLog(logEnums.error, content);
47
+ };
48
+
49
+ console.warn = (...args) => {
50
+ const content = `${logPrefix} ${format(...args)}`;
51
+ sendLog(logEnums.info, content);
52
+ };
53
+
54
+ console.info = (...args) => {
55
+ const content = `${logPrefix} ${format(...args)}`;
56
+ sendLog(logEnums.info, content);
57
+ };
58
+ console.debug = (...args) => {
59
+ if (!enableDebugLogs) return;
60
+ const content = `${logPrefix} ${format(...args)}`;
61
+ sendLog(logEnums.debug, content);
62
+ };
63
+ }
64
+
65
+ wrapConsole();
66
+
67
+ rpc.onCommandWorkletStart(async (init) => {
68
+ enableDebugLogs = !!init.enableDebugLogs;
69
+ console.debug('Worklet started ->', init);
70
+ return { status: 'started' };
71
+ });
72
+
73
+ rpc.onCommandGenerateAndEncrypt(async (payload) => {
74
+ try {
75
+ console.debug('encryption payload', payload);
76
+ if (payload.derivedKey)
77
+ payload.derivedKey = b4a.from(payload.derivedKey, 'hex');
78
+ payload.salt = b4a.from(payload.salt, 'hex');
79
+ const manager = new WdkSecretManager(payload.passkey, payload.salt);
80
+ const entropy = payload.seedPhrase
81
+ ? manager.mnemonicToEntropy(payload.seedPhrase)
82
+ : null;
83
+ const { encryptedSeed, encryptedEntropy } =
84
+ await manager.generateAndEncrypt(entropy, payload.derivedKey);
85
+ manager.dispose();
86
+ return {
87
+ encryptedSeed: b4a.toString(encryptedSeed, 'hex'),
88
+ encryptedEntropy: b4a.toString(encryptedEntropy, 'hex'),
89
+ };
90
+ } catch (e) {
91
+ throw new Error(`${e.message}: ${e.stack}`);
92
+ }
93
+ });
94
+
95
+ rpc.onCommandDecrypt(async (payload) => {
96
+ try {
97
+ if (payload.derivedKey)
98
+ payload.derivedKey = b4a.from(payload.derivedKey, 'hex');
99
+ payload.salt = b4a.from(payload.salt, 'hex');
100
+ const manager = new WdkSecretManager(
101
+ payload.passkey,
102
+ b4a.from(payload.salt, 'hex')
103
+ );
104
+ const decryptedData = manager.decrypt(
105
+ b4a.from(payload.encryptedData, 'hex'),
106
+ payload.derivedKey
107
+ );
108
+ manager.dispose();
109
+ return {
110
+ result: b4a.toString(decryptedData, 'hex'),
111
+ };
112
+ } catch (e) {
113
+ console.error(e.message);
114
+ return {
115
+ result: null,
116
+ };
117
+ }
118
+ });