@portal-hq/web 3.4.1 → 3.5.0-alpha
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/lib/commonjs/index.js +2 -0
- package/lib/commonjs/integrations/yield/index.js +16 -0
- package/lib/commonjs/integrations/yield/yieldxyz.js +115 -0
- package/lib/commonjs/integrations/yield/yieldxyz.test.js +154 -0
- package/lib/commonjs/mpc/index.js +122 -1
- package/lib/commonjs/mpc/index.test.js +539 -0
- package/lib/esm/index.js +2 -0
- package/lib/esm/integrations/yield/index.js +10 -0
- package/lib/esm/integrations/yield/yieldxyz.js +112 -0
- package/lib/esm/integrations/yield/yieldxyz.test.js +149 -0
- package/lib/esm/mpc/index.js +122 -1
- package/lib/esm/mpc/index.test.js +540 -1
- package/package.json +3 -2
- package/src/__mocks/constants.ts +319 -0
- package/src/index.ts +25 -2
- package/src/integrations/yield/index.ts +14 -0
- package/src/integrations/yield/yieldxyz.test.ts +220 -0
- package/src/integrations/yield/yieldxyz.ts +122 -0
- package/src/mpc/index.test.ts +645 -0
- package/src/mpc/index.ts +170 -5
- package/tsconfig.json +1 -1
- package/types.d.ts +1 -1
package/src/mpc/index.ts
CHANGED
|
@@ -33,15 +33,32 @@ import type {
|
|
|
33
33
|
EvaluateTransactionOperationType,
|
|
34
34
|
EvaluatedTransaction,
|
|
35
35
|
NFTAsset,
|
|
36
|
-
|
|
36
|
+
GetAssetsResponse,
|
|
37
37
|
BuiltTransaction,
|
|
38
38
|
EjectPrivateKeysArgs,
|
|
39
39
|
EjectPrivateKeysResult,
|
|
40
40
|
FundParams,
|
|
41
41
|
FundResponse,
|
|
42
42
|
} from '../../types'
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
import {
|
|
44
|
+
YieldXyzEnterRequest,
|
|
45
|
+
YieldXyzEnterYieldResponse,
|
|
46
|
+
YieldXyzExitRequest,
|
|
47
|
+
YieldXyzExitResponse,
|
|
48
|
+
YieldXyzGetBalancesRequest,
|
|
49
|
+
YieldXyzGetBalancesResponse,
|
|
50
|
+
YieldXyzGetHistoricalActionsRequest,
|
|
51
|
+
YieldXyzGetHistoricalActionsResponse,
|
|
52
|
+
YieldXyzGetTransactionResponse,
|
|
53
|
+
YieldXyzGetYieldsRequest,
|
|
54
|
+
YieldXyzGetYieldsResponse,
|
|
55
|
+
YieldXyzManageYieldRequest,
|
|
56
|
+
YieldXyzManageYieldResponse,
|
|
57
|
+
YieldXyzTrackTransactionRequest,
|
|
58
|
+
YieldXyzTrackTransactionResponse,
|
|
59
|
+
} from '../../yieldxyz-types'
|
|
60
|
+
|
|
61
|
+
const WEB_SDK_VERSION = '3.5.0-alpha'
|
|
45
62
|
|
|
46
63
|
class Mpc {
|
|
47
64
|
public iframe?: HTMLIFrameElement
|
|
@@ -730,7 +747,7 @@ class Mpc {
|
|
|
730
747
|
public async getAssets(
|
|
731
748
|
chainId: string,
|
|
732
749
|
includeNfts = false,
|
|
733
|
-
): Promise<
|
|
750
|
+
): Promise<GetAssetsResponse> {
|
|
734
751
|
return new Promise((resolve, reject) => {
|
|
735
752
|
const handleGetAssets = (event: MessageEvent<WorkerResult>) => {
|
|
736
753
|
const { type, data } = event.data
|
|
@@ -752,7 +769,7 @@ class Mpc {
|
|
|
752
769
|
window.removeEventListener('message', handleGetAssets)
|
|
753
770
|
|
|
754
771
|
// Resolve the promise with the result
|
|
755
|
-
resolve(data as
|
|
772
|
+
resolve(data as GetAssetsResponse)
|
|
756
773
|
}
|
|
757
774
|
}
|
|
758
775
|
|
|
@@ -1191,10 +1208,148 @@ class Mpc {
|
|
|
1191
1208
|
})
|
|
1192
1209
|
}
|
|
1193
1210
|
|
|
1211
|
+
public async getYieldXyzYields(
|
|
1212
|
+
data: YieldXyzGetYieldsRequest,
|
|
1213
|
+
): Promise<YieldXyzGetYieldsResponse> {
|
|
1214
|
+
return this.handleRequestToIframeAndPost({
|
|
1215
|
+
methodMessage: 'portal:yieldxyz:discover',
|
|
1216
|
+
errorMessage: 'portal:yieldxyz:discoverError',
|
|
1217
|
+
resultMessage: 'portal:yieldxyz:discoverResult',
|
|
1218
|
+
data,
|
|
1219
|
+
})
|
|
1220
|
+
}
|
|
1221
|
+
|
|
1222
|
+
public async enterYieldXyzYield(
|
|
1223
|
+
data: YieldXyzEnterRequest,
|
|
1224
|
+
): Promise<YieldXyzEnterYieldResponse> {
|
|
1225
|
+
return this.handleRequestToIframeAndPost({
|
|
1226
|
+
methodMessage: 'portal:yieldxyz:enter',
|
|
1227
|
+
errorMessage: 'portal:yieldxyz:enterError',
|
|
1228
|
+
resultMessage: 'portal:yieldxyz:enterResult',
|
|
1229
|
+
data,
|
|
1230
|
+
})
|
|
1231
|
+
}
|
|
1232
|
+
|
|
1233
|
+
public async exitYieldXyzYield(
|
|
1234
|
+
data: YieldXyzExitRequest,
|
|
1235
|
+
): Promise<YieldXyzExitResponse> {
|
|
1236
|
+
return this.handleRequestToIframeAndPost({
|
|
1237
|
+
methodMessage: 'portal:yieldxyz:exit',
|
|
1238
|
+
errorMessage: 'portal:yieldxyz:exitError',
|
|
1239
|
+
resultMessage: 'portal:yieldxyz:exitResult',
|
|
1240
|
+
data,
|
|
1241
|
+
})
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1244
|
+
public async getYieldXyzBalances(
|
|
1245
|
+
data: YieldXyzGetBalancesRequest,
|
|
1246
|
+
): Promise<YieldXyzGetBalancesResponse> {
|
|
1247
|
+
return this.handleRequestToIframeAndPost({
|
|
1248
|
+
methodMessage: 'portal:yieldxyz:getBalances',
|
|
1249
|
+
errorMessage: 'portal:yieldxyz:getBalancesError',
|
|
1250
|
+
resultMessage: 'portal:yieldxyz:getBalancesResult',
|
|
1251
|
+
data,
|
|
1252
|
+
})
|
|
1253
|
+
}
|
|
1254
|
+
|
|
1255
|
+
public async getYieldXyzHistoricalActions(
|
|
1256
|
+
data: YieldXyzGetHistoricalActionsRequest,
|
|
1257
|
+
): Promise<YieldXyzGetHistoricalActionsResponse> {
|
|
1258
|
+
return this.handleRequestToIframeAndPost({
|
|
1259
|
+
methodMessage: 'portal:yieldxyz:getHistoricalActions',
|
|
1260
|
+
errorMessage: 'portal:yieldxyz:getHistoricalActionsError',
|
|
1261
|
+
resultMessage: 'portal:yieldxyz:getHistoricalActionsResult',
|
|
1262
|
+
data,
|
|
1263
|
+
})
|
|
1264
|
+
}
|
|
1265
|
+
|
|
1266
|
+
public async manageYieldXyzYield(
|
|
1267
|
+
data: YieldXyzManageYieldRequest,
|
|
1268
|
+
): Promise<YieldXyzManageYieldResponse> {
|
|
1269
|
+
return this.handleRequestToIframeAndPost({
|
|
1270
|
+
methodMessage: 'portal:yieldxyz:manage',
|
|
1271
|
+
errorMessage: 'portal:yieldxyz:manageYieldError',
|
|
1272
|
+
resultMessage: 'portal:yieldxyz:manageYieldResult',
|
|
1273
|
+
data,
|
|
1274
|
+
})
|
|
1275
|
+
}
|
|
1276
|
+
|
|
1277
|
+
public async trackYieldXyzTransaction(
|
|
1278
|
+
data: YieldXyzTrackTransactionRequest,
|
|
1279
|
+
): Promise<YieldXyzTrackTransactionResponse> {
|
|
1280
|
+
return this.handleRequestToIframeAndPost({
|
|
1281
|
+
methodMessage: 'portal:yieldxyz:track',
|
|
1282
|
+
errorMessage: 'portal:yieldxyz:trackError',
|
|
1283
|
+
resultMessage: 'portal:yieldxyz:trackResult',
|
|
1284
|
+
data,
|
|
1285
|
+
})
|
|
1286
|
+
}
|
|
1287
|
+
|
|
1288
|
+
public async getYieldXyzTransaction(
|
|
1289
|
+
data: string,
|
|
1290
|
+
): Promise<YieldXyzGetTransactionResponse> {
|
|
1291
|
+
return this.handleRequestToIframeAndPost({
|
|
1292
|
+
methodMessage: 'portal:yieldxyz:getTransaction',
|
|
1293
|
+
errorMessage: 'portal:yieldxyz:getTransactionError',
|
|
1294
|
+
resultMessage: 'portal:yieldxyz:getTransactionResult',
|
|
1295
|
+
data,
|
|
1296
|
+
})
|
|
1297
|
+
}
|
|
1298
|
+
|
|
1194
1299
|
/***************************
|
|
1195
1300
|
* Private Methods
|
|
1196
1301
|
***************************/
|
|
1197
1302
|
|
|
1303
|
+
/**
|
|
1304
|
+
* Util to handle requests to the iframe and post the result to the parent
|
|
1305
|
+
*/
|
|
1306
|
+
private async handleRequestToIframeAndPost<T>({
|
|
1307
|
+
methodMessage,
|
|
1308
|
+
errorMessage,
|
|
1309
|
+
resultMessage,
|
|
1310
|
+
data,
|
|
1311
|
+
}: {
|
|
1312
|
+
methodMessage: string
|
|
1313
|
+
errorMessage: string
|
|
1314
|
+
resultMessage: string
|
|
1315
|
+
data: any
|
|
1316
|
+
}): Promise<T> {
|
|
1317
|
+
return new Promise((resolve, reject) => {
|
|
1318
|
+
const handleRequest = (event: MessageEvent<WorkerResult>) => {
|
|
1319
|
+
const { type, data: result } = event.data
|
|
1320
|
+
const { origin } = event
|
|
1321
|
+
|
|
1322
|
+
// ignore any broadcast postMessages
|
|
1323
|
+
if (origin !== this.getOrigin()) {
|
|
1324
|
+
return
|
|
1325
|
+
}
|
|
1326
|
+
|
|
1327
|
+
if (type === errorMessage) {
|
|
1328
|
+
// Remove the event listener
|
|
1329
|
+
window.removeEventListener('message', handleRequest)
|
|
1330
|
+
|
|
1331
|
+
// Reject the promise with the error
|
|
1332
|
+
return reject(new PortalMpcError(result as PortalError))
|
|
1333
|
+
} else if (type === resultMessage) {
|
|
1334
|
+
// Remove the event listener
|
|
1335
|
+
window.removeEventListener('message', handleRequest)
|
|
1336
|
+
|
|
1337
|
+
// Resolve the promise with the result
|
|
1338
|
+
resolve(result as T)
|
|
1339
|
+
}
|
|
1340
|
+
}
|
|
1341
|
+
|
|
1342
|
+
// Bind the function to the message event
|
|
1343
|
+
window.addEventListener('message', handleRequest)
|
|
1344
|
+
|
|
1345
|
+
// Send the request to the iframe
|
|
1346
|
+
this.postMessage({
|
|
1347
|
+
type: methodMessage,
|
|
1348
|
+
data,
|
|
1349
|
+
})
|
|
1350
|
+
})
|
|
1351
|
+
}
|
|
1352
|
+
|
|
1198
1353
|
/**
|
|
1199
1354
|
* Appends the iframe to the document body
|
|
1200
1355
|
*/
|
|
@@ -1303,6 +1458,16 @@ class Mpc {
|
|
|
1303
1458
|
}
|
|
1304
1459
|
|
|
1305
1460
|
private validateBackupConfig(data: BackupArgs) {
|
|
1461
|
+
// Validate that backupMethod is one of the valid BackupMethods
|
|
1462
|
+
const validBackupMethods = Object.values(BackupMethods)
|
|
1463
|
+
if (!validBackupMethods.includes(data.backupMethod)) {
|
|
1464
|
+
throw new Error(
|
|
1465
|
+
`Invalid backup method: ${
|
|
1466
|
+
data.backupMethod
|
|
1467
|
+
}. Valid methods are: ${validBackupMethods.join(', ')}`,
|
|
1468
|
+
)
|
|
1469
|
+
}
|
|
1470
|
+
|
|
1306
1471
|
if (data.backupMethod === BackupMethods.password) {
|
|
1307
1472
|
if (!data.backupConfigs.passwordStorage) {
|
|
1308
1473
|
throw new Error('Password storage config is required')
|
package/tsconfig.json
CHANGED