@imtbl/sdk 0.3.0-alpha.3 → 0.3.1-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/dist/blockchain-data.js +1 -1
- package/dist/browser/checkout.js +2 -2
- package/dist/checkout_sdk.d.ts +2 -0
- package/dist/checkout_sdk.js +12 -12
- package/dist/checkout_widgets.js +1 -1
- package/dist/immutablex_client.d.ts +2 -2
- package/dist/immutablex_client.js +1 -1
- package/dist/index.browser.js +11 -11
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +251 -192
- package/dist/index.d.ts +6 -4
- package/dist/index.js +252 -193
- package/dist/passport.d.ts +4 -4
- package/dist/passport.js +238 -179
- package/package.json +2 -2
package/dist/passport.js
CHANGED
|
@@ -7,10 +7,10 @@ import { OpenIdExtension } from '@magic-ext/oidc';
|
|
|
7
7
|
import BN from 'bn.js';
|
|
8
8
|
import * as encUtils from 'enc-utils';
|
|
9
9
|
import { Web3Provider, JsonRpcProvider } from '@ethersproject/providers';
|
|
10
|
+
import { EventEmitter } from 'events';
|
|
10
11
|
import { ethers, BigNumber } from 'ethers';
|
|
11
12
|
import { walletContracts } from '@0xsequence/abi';
|
|
12
13
|
import { encodeSignature } from '@0xsequence/config';
|
|
13
|
-
import { EventEmitter } from 'events';
|
|
14
14
|
|
|
15
15
|
function bind$1(fn, thisArg) {
|
|
16
16
|
return function wrap() {
|
|
@@ -4607,7 +4607,7 @@ class MultiRollupApiClients {
|
|
|
4607
4607
|
}
|
|
4608
4608
|
|
|
4609
4609
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
4610
|
-
const defaultHeaders = { 'x-sdk-version': 'ts-immutable-sdk-0.3.
|
|
4610
|
+
const defaultHeaders = { 'x-sdk-version': 'ts-immutable-sdk-0.3.1-alpha' };
|
|
4611
4611
|
const createConfig = ({ basePath, headers, }) => {
|
|
4612
4612
|
if (!basePath.trim()) {
|
|
4613
4613
|
throw Error('basePath can not be empty');
|
|
@@ -4663,7 +4663,7 @@ const createImmutableXConfiguration = ({ basePath, chainID, coreContractAddress,
|
|
|
4663
4663
|
chainID,
|
|
4664
4664
|
coreContractAddress,
|
|
4665
4665
|
registrationContractAddress,
|
|
4666
|
-
sdkVersion: 'ts-immutable-sdk-0.3.
|
|
4666
|
+
sdkVersion: 'ts-immutable-sdk-0.3.1-alpha',
|
|
4667
4667
|
});
|
|
4668
4668
|
/**
|
|
4669
4669
|
* Sets `sdkVersion` at the time of build
|
|
@@ -8387,6 +8387,8 @@ var globalAxios = axios;
|
|
|
8387
8387
|
* https://openapi-generator.tech
|
|
8388
8388
|
* Do not edit the class manually.
|
|
8389
8389
|
*/
|
|
8390
|
+
// Some imports not used depending on template conditions
|
|
8391
|
+
// @ts-ignore
|
|
8390
8392
|
const BASE_PATH = "https://guardian.sandbox.imtbl.com".replace(/\/+$/, "");
|
|
8391
8393
|
/**
|
|
8392
8394
|
*
|
|
@@ -8414,10 +8416,10 @@ class BaseAPI {
|
|
|
8414
8416
|
*/
|
|
8415
8417
|
class RequiredError extends Error {
|
|
8416
8418
|
field;
|
|
8419
|
+
name = "RequiredError";
|
|
8417
8420
|
constructor(field, msg) {
|
|
8418
8421
|
super(msg);
|
|
8419
8422
|
this.field = field;
|
|
8420
|
-
this.name = "RequiredError";
|
|
8421
8423
|
}
|
|
8422
8424
|
}
|
|
8423
8425
|
|
|
@@ -8462,8 +8464,6 @@ const setBearerAuthToObject = async function (object, configuration) {
|
|
|
8462
8464
|
}
|
|
8463
8465
|
};
|
|
8464
8466
|
function setFlattenedQueryParams(urlSearchParams, parameter, key = "") {
|
|
8465
|
-
if (parameter == null)
|
|
8466
|
-
return;
|
|
8467
8467
|
if (typeof parameter === "object") {
|
|
8468
8468
|
if (Array.isArray(parameter)) {
|
|
8469
8469
|
parameter.forEach(item => setFlattenedQueryParams(urlSearchParams, item, key));
|
|
@@ -8619,7 +8619,7 @@ const TransactionsApiAxiosParamCreator = function (configuration) {
|
|
|
8619
8619
|
* @summary Info for a specific transaction
|
|
8620
8620
|
* @param {string} transactionID The id of the starkex transaction to retrieve
|
|
8621
8621
|
* @param {'starkex' | 'evm'} chainType roll up type
|
|
8622
|
-
* @param {
|
|
8622
|
+
* @param {string} [chainID] ID of evm chain
|
|
8623
8623
|
* @param {*} [options] Override http request option.
|
|
8624
8624
|
* @throws {RequiredError}
|
|
8625
8625
|
*/
|
|
@@ -8694,7 +8694,7 @@ const TransactionsApiFp = function (configuration) {
|
|
|
8694
8694
|
* @summary Info for a specific transaction
|
|
8695
8695
|
* @param {string} transactionID The id of the starkex transaction to retrieve
|
|
8696
8696
|
* @param {'starkex' | 'evm'} chainType roll up type
|
|
8697
|
-
* @param {
|
|
8697
|
+
* @param {string} [chainID] ID of evm chain
|
|
8698
8698
|
* @param {*} [options] Override http request option.
|
|
8699
8699
|
* @throws {RequiredError}
|
|
8700
8700
|
*/
|
|
@@ -9372,6 +9372,7 @@ async function createOrder({ starkSigner, user, request, ordersApi, guardianClie
|
|
|
9372
9372
|
amount_sell: amountSell,
|
|
9373
9373
|
token_sell: convertToSignableToken(request.sell),
|
|
9374
9374
|
fees: request.fees,
|
|
9375
|
+
split_fees: true,
|
|
9375
9376
|
expiration_timestamp: request.expiration_timestamp,
|
|
9376
9377
|
};
|
|
9377
9378
|
const getSignableOrderResponse = await ordersApi.getSignableOrder({
|
|
@@ -9885,7 +9886,7 @@ class PassportConfiguration {
|
|
|
9885
9886
|
this.imxPublicApiDomain = 'https://api.immutable.com';
|
|
9886
9887
|
this.zkEvmRpcUrl = ''; // TODO: ID-785 Update once mainnet has been deployed
|
|
9887
9888
|
this.zkEvmChainId = ''; // TODO: ID-785 Update once mainnet has been deployed
|
|
9888
|
-
this.relayerUrl = 'https://
|
|
9889
|
+
this.relayerUrl = 'https://api.immutable.com/relayer-mr';
|
|
9889
9890
|
this.multiRollupConfig = multiRollupConfig.getProduction();
|
|
9890
9891
|
break;
|
|
9891
9892
|
}
|
|
@@ -9899,7 +9900,7 @@ class PassportConfiguration {
|
|
|
9899
9900
|
this.imxPublicApiDomain = 'https://api.sandbox.immutable.com';
|
|
9900
9901
|
this.zkEvmRpcUrl = 'https://zkevm-rpc.sandbox.x.immutable.com';
|
|
9901
9902
|
this.zkEvmChainId = 'eip155:13392';
|
|
9902
|
-
this.relayerUrl = 'https://
|
|
9903
|
+
this.relayerUrl = 'https://api.sandbox.immutable.com/relayer-mr';
|
|
9903
9904
|
this.multiRollupConfig = multiRollupConfig.getSandbox();
|
|
9904
9905
|
break;
|
|
9905
9906
|
}
|
|
@@ -10061,6 +10062,169 @@ class ConfirmationScreen {
|
|
|
10061
10062
|
}
|
|
10062
10063
|
}
|
|
10063
10064
|
|
|
10065
|
+
var RelayerTransactionStatus;
|
|
10066
|
+
(function (RelayerTransactionStatus) {
|
|
10067
|
+
RelayerTransactionStatus["PENDING"] = "PENDING";
|
|
10068
|
+
RelayerTransactionStatus["SUBMITTED"] = "SUBMITTED";
|
|
10069
|
+
RelayerTransactionStatus["SUCCESSFUL"] = "SUCCESSFUL";
|
|
10070
|
+
RelayerTransactionStatus["REVERTED"] = "REVERTED";
|
|
10071
|
+
RelayerTransactionStatus["FAILED"] = "FAILED";
|
|
10072
|
+
})(RelayerTransactionStatus || (RelayerTransactionStatus = {}));
|
|
10073
|
+
var ProviderEvent;
|
|
10074
|
+
(function (ProviderEvent) {
|
|
10075
|
+
ProviderEvent["ACCOUNTS_CHANGED"] = "accountsChanged";
|
|
10076
|
+
})(ProviderEvent || (ProviderEvent = {}));
|
|
10077
|
+
|
|
10078
|
+
class TypedEventEmitter {
|
|
10079
|
+
emitter = new EventEmitter();
|
|
10080
|
+
emit(eventName, ...eventArg) {
|
|
10081
|
+
this.emitter.emit(eventName, ...eventArg);
|
|
10082
|
+
}
|
|
10083
|
+
on(eventName, handler) {
|
|
10084
|
+
this.emitter.on(eventName, handler);
|
|
10085
|
+
}
|
|
10086
|
+
removeListener(eventName, handler) {
|
|
10087
|
+
this.emitter.removeListener(eventName, handler);
|
|
10088
|
+
}
|
|
10089
|
+
}
|
|
10090
|
+
|
|
10091
|
+
class RelayerClient {
|
|
10092
|
+
config;
|
|
10093
|
+
constructor({ config }) {
|
|
10094
|
+
this.config = config;
|
|
10095
|
+
}
|
|
10096
|
+
async postToRelayer(request) {
|
|
10097
|
+
const body = {
|
|
10098
|
+
id: 1,
|
|
10099
|
+
jsonrpc: '2.0',
|
|
10100
|
+
...request,
|
|
10101
|
+
};
|
|
10102
|
+
const response = await fetch(`${this.config.relayerUrl}/v1/transactions`, {
|
|
10103
|
+
method: 'POST',
|
|
10104
|
+
headers: {
|
|
10105
|
+
'Content-Type': 'application/json',
|
|
10106
|
+
},
|
|
10107
|
+
body: JSON.stringify(body),
|
|
10108
|
+
});
|
|
10109
|
+
const jsonResponse = await response.json();
|
|
10110
|
+
if (jsonResponse.error) {
|
|
10111
|
+
throw jsonResponse.error;
|
|
10112
|
+
}
|
|
10113
|
+
return jsonResponse;
|
|
10114
|
+
}
|
|
10115
|
+
async ethSendTransaction(to, data) {
|
|
10116
|
+
const payload = {
|
|
10117
|
+
method: 'eth_sendTransaction',
|
|
10118
|
+
params: [{
|
|
10119
|
+
to,
|
|
10120
|
+
data,
|
|
10121
|
+
chainId: this.config.zkEvmChainId.toString(),
|
|
10122
|
+
}],
|
|
10123
|
+
};
|
|
10124
|
+
const { result } = await this.postToRelayer(payload);
|
|
10125
|
+
return result;
|
|
10126
|
+
}
|
|
10127
|
+
async imGetTransactionByHash(hash) {
|
|
10128
|
+
const payload = {
|
|
10129
|
+
method: 'im_getTransactionByHash',
|
|
10130
|
+
params: [hash],
|
|
10131
|
+
};
|
|
10132
|
+
const { result } = await this.postToRelayer(payload);
|
|
10133
|
+
return result;
|
|
10134
|
+
}
|
|
10135
|
+
async imGetFeeOptions(userAddress, data) {
|
|
10136
|
+
const payload = {
|
|
10137
|
+
method: 'im_getFeeOptions',
|
|
10138
|
+
params: [{
|
|
10139
|
+
userAddress,
|
|
10140
|
+
data,
|
|
10141
|
+
chainId: this.config.zkEvmChainId.toString(),
|
|
10142
|
+
}],
|
|
10143
|
+
};
|
|
10144
|
+
const { result } = await this.postToRelayer(payload);
|
|
10145
|
+
return result;
|
|
10146
|
+
}
|
|
10147
|
+
}
|
|
10148
|
+
|
|
10149
|
+
/**
|
|
10150
|
+
* ProviderErrors should take priority over RpcErrorCodes
|
|
10151
|
+
*/
|
|
10152
|
+
var ProviderErrorCode;
|
|
10153
|
+
(function (ProviderErrorCode) {
|
|
10154
|
+
ProviderErrorCode[ProviderErrorCode["USER_REJECTED_REQUEST"] = 4001] = "USER_REJECTED_REQUEST";
|
|
10155
|
+
ProviderErrorCode[ProviderErrorCode["UNAUTHORIZED"] = 4100] = "UNAUTHORIZED";
|
|
10156
|
+
ProviderErrorCode[ProviderErrorCode["UNSUPPORTED_METHOD"] = 4200] = "UNSUPPORTED_METHOD";
|
|
10157
|
+
ProviderErrorCode[ProviderErrorCode["DISCONNECTED"] = 4900] = "DISCONNECTED";
|
|
10158
|
+
})(ProviderErrorCode || (ProviderErrorCode = {}));
|
|
10159
|
+
var RpcErrorCode;
|
|
10160
|
+
(function (RpcErrorCode) {
|
|
10161
|
+
RpcErrorCode[RpcErrorCode["RPC_SERVER_ERROR"] = -32000] = "RPC_SERVER_ERROR";
|
|
10162
|
+
RpcErrorCode[RpcErrorCode["INVALID_REQUEST"] = -32600] = "INVALID_REQUEST";
|
|
10163
|
+
RpcErrorCode[RpcErrorCode["METHOD_NOT_FOUND"] = -32601] = "METHOD_NOT_FOUND";
|
|
10164
|
+
RpcErrorCode[RpcErrorCode["INVALID_PARAMS"] = -32602] = "INVALID_PARAMS";
|
|
10165
|
+
RpcErrorCode[RpcErrorCode["INTERNAL_ERROR"] = -32603] = "INTERNAL_ERROR";
|
|
10166
|
+
RpcErrorCode[RpcErrorCode["PARSE_ERROR"] = -32700] = "PARSE_ERROR";
|
|
10167
|
+
})(RpcErrorCode || (RpcErrorCode = {}));
|
|
10168
|
+
class JsonRpcError extends Error {
|
|
10169
|
+
message;
|
|
10170
|
+
code;
|
|
10171
|
+
constructor(code, message) {
|
|
10172
|
+
super(message);
|
|
10173
|
+
this.message = message;
|
|
10174
|
+
this.code = code;
|
|
10175
|
+
}
|
|
10176
|
+
}
|
|
10177
|
+
|
|
10178
|
+
const MESSAGE_TO_SIGN = 'Only sign this message from Immutable Passport';
|
|
10179
|
+
async function registerZkEvmUser({ authManager, magicProvider, multiRollupApiClients, accessToken, }) {
|
|
10180
|
+
const web3Provider = new Web3Provider(magicProvider);
|
|
10181
|
+
const ethSigner = web3Provider.getSigner();
|
|
10182
|
+
const ethereumAddress = await ethSigner.getAddress();
|
|
10183
|
+
const ethereumSignature = await signRaw(MESSAGE_TO_SIGN, ethSigner);
|
|
10184
|
+
const headers = { Authorization: `Bearer ${accessToken}` };
|
|
10185
|
+
try {
|
|
10186
|
+
await multiRollupApiClients.passportApi.createCounterfactualAddress({
|
|
10187
|
+
createCounterfactualAddressRequest: {
|
|
10188
|
+
ethereumAddress,
|
|
10189
|
+
ethereumSignature,
|
|
10190
|
+
},
|
|
10191
|
+
}, { headers });
|
|
10192
|
+
}
|
|
10193
|
+
catch (error) {
|
|
10194
|
+
throw new JsonRpcError(RpcErrorCode.INTERNAL_ERROR, `Failed to create counterfactual address: ${error}`);
|
|
10195
|
+
}
|
|
10196
|
+
const user = await authManager.loginSilent();
|
|
10197
|
+
if (!user?.zkEvm) {
|
|
10198
|
+
throw new JsonRpcError(RpcErrorCode.INTERNAL_ERROR, 'Failed to refresh user details');
|
|
10199
|
+
}
|
|
10200
|
+
return user;
|
|
10201
|
+
}
|
|
10202
|
+
|
|
10203
|
+
const loginZkEvmUser = async ({ authManager, config, magicAdapter, multiRollupApiClients, }) => {
|
|
10204
|
+
const user = await authManager.getUser() || await authManager.login();
|
|
10205
|
+
if (!user.idToken) {
|
|
10206
|
+
throw new Error('User is missing idToken');
|
|
10207
|
+
}
|
|
10208
|
+
const magicProvider = await magicAdapter.login(user.idToken, config.network);
|
|
10209
|
+
if (!user.zkEvm) {
|
|
10210
|
+
// Generate counterfactual address and retrieve updated Auth0 user
|
|
10211
|
+
const userZkevm = await registerZkEvmUser({
|
|
10212
|
+
authManager,
|
|
10213
|
+
magicProvider,
|
|
10214
|
+
multiRollupApiClients,
|
|
10215
|
+
accessToken: user.accessToken,
|
|
10216
|
+
});
|
|
10217
|
+
return {
|
|
10218
|
+
user: userZkevm,
|
|
10219
|
+
magicProvider,
|
|
10220
|
+
};
|
|
10221
|
+
}
|
|
10222
|
+
return {
|
|
10223
|
+
user: user,
|
|
10224
|
+
magicProvider,
|
|
10225
|
+
};
|
|
10226
|
+
};
|
|
10227
|
+
|
|
10064
10228
|
// These are ignored by the Relayer but for consistency we set them to the
|
|
10065
10229
|
// appropriate values for a 1/1 wallet.
|
|
10066
10230
|
//
|
|
@@ -10136,51 +10300,48 @@ const chainIdNumber = (chainId) => {
|
|
|
10136
10300
|
return BigNumber.from(caip2Components[1]);
|
|
10137
10301
|
};
|
|
10138
10302
|
|
|
10139
|
-
var RelayerTransactionStatus;
|
|
10140
|
-
(function (RelayerTransactionStatus) {
|
|
10141
|
-
RelayerTransactionStatus["PENDING"] = "PENDING";
|
|
10142
|
-
RelayerTransactionStatus["SUBMITTED"] = "SUBMITTED";
|
|
10143
|
-
RelayerTransactionStatus["SUCCESSFUL"] = "SUCCESSFUL";
|
|
10144
|
-
RelayerTransactionStatus["REVERTED"] = "REVERTED";
|
|
10145
|
-
RelayerTransactionStatus["FAILED"] = "FAILED";
|
|
10146
|
-
})(RelayerTransactionStatus || (RelayerTransactionStatus = {}));
|
|
10147
|
-
var ProviderEventNames;
|
|
10148
|
-
(function (ProviderEventNames) {
|
|
10149
|
-
ProviderEventNames["ACCOUNTS_CHANGED"] = "accountsChanged";
|
|
10150
|
-
})(ProviderEventNames || (ProviderEventNames = {}));
|
|
10151
|
-
|
|
10152
|
-
/**
|
|
10153
|
-
* ProviderErrors should take priority over RpcErrorCodes
|
|
10154
|
-
*/
|
|
10155
|
-
var ProviderErrorCode;
|
|
10156
|
-
(function (ProviderErrorCode) {
|
|
10157
|
-
ProviderErrorCode[ProviderErrorCode["USER_REJECTED_REQUEST"] = 4001] = "USER_REJECTED_REQUEST";
|
|
10158
|
-
ProviderErrorCode[ProviderErrorCode["UNAUTHORIZED"] = 4100] = "UNAUTHORIZED";
|
|
10159
|
-
ProviderErrorCode[ProviderErrorCode["UNSUPPORTED_METHOD"] = 4200] = "UNSUPPORTED_METHOD";
|
|
10160
|
-
ProviderErrorCode[ProviderErrorCode["DISCONNECTED"] = 4900] = "DISCONNECTED";
|
|
10161
|
-
})(ProviderErrorCode || (ProviderErrorCode = {}));
|
|
10162
|
-
var RpcErrorCode;
|
|
10163
|
-
(function (RpcErrorCode) {
|
|
10164
|
-
RpcErrorCode[RpcErrorCode["RPC_SERVER_ERROR"] = -32000] = "RPC_SERVER_ERROR";
|
|
10165
|
-
RpcErrorCode[RpcErrorCode["INVALID_REQUEST"] = -32600] = "INVALID_REQUEST";
|
|
10166
|
-
RpcErrorCode[RpcErrorCode["METHOD_NOT_FOUND"] = -32601] = "METHOD_NOT_FOUND";
|
|
10167
|
-
RpcErrorCode[RpcErrorCode["INVALID_PARAMS"] = -32602] = "INVALID_PARAMS";
|
|
10168
|
-
RpcErrorCode[RpcErrorCode["INTERNAL_ERROR"] = -32603] = "INTERNAL_ERROR";
|
|
10169
|
-
RpcErrorCode[RpcErrorCode["PARSE_ERROR"] = -32700] = "PARSE_ERROR";
|
|
10170
|
-
})(RpcErrorCode || (RpcErrorCode = {}));
|
|
10171
|
-
class JsonRpcError extends Error {
|
|
10172
|
-
message;
|
|
10173
|
-
code;
|
|
10174
|
-
constructor(code, message) {
|
|
10175
|
-
super(message);
|
|
10176
|
-
this.message = message;
|
|
10177
|
-
this.code = code;
|
|
10178
|
-
}
|
|
10179
|
-
}
|
|
10180
|
-
|
|
10181
10303
|
const MAX_TRANSACTION_HASH_RETRIEVAL_RETRIES = 30;
|
|
10182
10304
|
const TRANSACTION_HASH_RETRIEVAL_WAIT = 1000;
|
|
10183
|
-
const
|
|
10305
|
+
const converBigNumberishToNumber = (value) => BigNumber.from(value).toNumber();
|
|
10306
|
+
const transformGardianTransactions = (txs) => {
|
|
10307
|
+
try {
|
|
10308
|
+
return txs.map((t) => ({
|
|
10309
|
+
delegateCall: t.delegateCall === true,
|
|
10310
|
+
revertOnError: t.revertOnError === true,
|
|
10311
|
+
gasLimit: t.gasLimit ? converBigNumberishToNumber(t.gasLimit).toString() : '0',
|
|
10312
|
+
target: t.to ?? ethers.constants.AddressZero,
|
|
10313
|
+
value: t.value ? converBigNumberishToNumber(t.value).toString() : '0',
|
|
10314
|
+
data: t.data ? t.data.toString() : '0x00',
|
|
10315
|
+
}));
|
|
10316
|
+
}
|
|
10317
|
+
catch (error) {
|
|
10318
|
+
const errorMessage = (error instanceof Error) ? error.message : String(error);
|
|
10319
|
+
throw new JsonRpcError(RpcErrorCode.INVALID_PARAMS, `Transaction failed to parsing: ${errorMessage}`);
|
|
10320
|
+
}
|
|
10321
|
+
};
|
|
10322
|
+
const guardianValidation = async ({ transactionAPI, chainId, nonce, user, metaTransactions, }) => {
|
|
10323
|
+
const headers = { Authorization: `Bearer ${user.accessToken}` };
|
|
10324
|
+
const guardianTransactions = transformGardianTransactions(metaTransactions);
|
|
10325
|
+
try {
|
|
10326
|
+
await transactionAPI.evaluateTransaction({
|
|
10327
|
+
id: 'evm',
|
|
10328
|
+
transactionEvaluationRequest: {
|
|
10329
|
+
chainType: 'evm',
|
|
10330
|
+
chainId,
|
|
10331
|
+
transactionData: {
|
|
10332
|
+
nonce,
|
|
10333
|
+
userAddress: user.zkEvm.ethAddress,
|
|
10334
|
+
metaTransactions: guardianTransactions,
|
|
10335
|
+
},
|
|
10336
|
+
},
|
|
10337
|
+
}, { headers });
|
|
10338
|
+
}
|
|
10339
|
+
catch (error) {
|
|
10340
|
+
const errorMessage = (error instanceof Error) ? error.message : String(error);
|
|
10341
|
+
throw new JsonRpcError(RpcErrorCode.INTERNAL_ERROR, `Transaction failed to validate with error: ${errorMessage}`);
|
|
10342
|
+
}
|
|
10343
|
+
};
|
|
10344
|
+
const sendTransaction = async ({ params, magicProvider, jsonRpcProvider, relayerClient, transactionAPI, config, user, }) => {
|
|
10184
10345
|
const transactionRequest = params[0];
|
|
10185
10346
|
if (!transactionRequest.to) {
|
|
10186
10347
|
throw new JsonRpcError(RpcErrorCode.INVALID_PARAMS, 'eth_sendTransaction requires a "to" field');
|
|
@@ -10218,6 +10379,13 @@ const sendTransaction = async ({ params, magicProvider, jsonRpcProvider, relayer
|
|
|
10218
10379
|
value: imxFeeOption.tokenPrice,
|
|
10219
10380
|
revertOnError: true,
|
|
10220
10381
|
};
|
|
10382
|
+
await guardianValidation({
|
|
10383
|
+
transactionAPI,
|
|
10384
|
+
chainId: config.zkEvmChainId,
|
|
10385
|
+
nonce: converBigNumberishToNumber(nonce),
|
|
10386
|
+
user,
|
|
10387
|
+
metaTransactions: [metaTransaction, feeMetaTransaction],
|
|
10388
|
+
});
|
|
10221
10389
|
// NOTE: We sign again because we now are adding the fee transaction, so the
|
|
10222
10390
|
// whole payload is different and needs a new signature.
|
|
10223
10391
|
const signedTransactions = await getSignedMetaTransactions([metaTransaction, feeMetaTransaction], nonce, chainId, user.zkEvm.ethAddress, signer);
|
|
@@ -10245,146 +10413,30 @@ const sendTransaction = async ({ params, magicProvider, jsonRpcProvider, relayer
|
|
|
10245
10413
|
return relayerTransaction.hash;
|
|
10246
10414
|
};
|
|
10247
10415
|
|
|
10248
|
-
class
|
|
10249
|
-
emitter = new EventEmitter();
|
|
10250
|
-
emit(eventName, ...eventArg) {
|
|
10251
|
-
this.emitter.emit(eventName, ...eventArg);
|
|
10252
|
-
}
|
|
10253
|
-
on(eventName, handler) {
|
|
10254
|
-
this.emitter.on(eventName, handler);
|
|
10255
|
-
}
|
|
10256
|
-
removeListener(eventName, handler) {
|
|
10257
|
-
this.emitter.removeListener(eventName, handler);
|
|
10258
|
-
}
|
|
10259
|
-
}
|
|
10260
|
-
|
|
10261
|
-
class RelayerClient {
|
|
10262
|
-
config;
|
|
10263
|
-
constructor({ config }) {
|
|
10264
|
-
this.config = config;
|
|
10265
|
-
}
|
|
10266
|
-
async postToRelayer(request) {
|
|
10267
|
-
const body = {
|
|
10268
|
-
id: 1,
|
|
10269
|
-
jsonrpc: '2.0',
|
|
10270
|
-
...request,
|
|
10271
|
-
};
|
|
10272
|
-
const response = await fetch(`${this.config.relayerUrl}/v1/transactions`, {
|
|
10273
|
-
method: 'POST',
|
|
10274
|
-
headers: {
|
|
10275
|
-
'Content-Type': 'application/json',
|
|
10276
|
-
},
|
|
10277
|
-
body: JSON.stringify(body),
|
|
10278
|
-
});
|
|
10279
|
-
const jsonResponse = await response.json();
|
|
10280
|
-
if (jsonResponse.error) {
|
|
10281
|
-
throw jsonResponse.error;
|
|
10282
|
-
}
|
|
10283
|
-
return jsonResponse;
|
|
10284
|
-
}
|
|
10285
|
-
async ethSendTransaction(to, data) {
|
|
10286
|
-
const payload = {
|
|
10287
|
-
method: 'eth_sendTransaction',
|
|
10288
|
-
params: [{
|
|
10289
|
-
to,
|
|
10290
|
-
data,
|
|
10291
|
-
chainId: this.config.zkEvmChainId.toString(),
|
|
10292
|
-
}],
|
|
10293
|
-
};
|
|
10294
|
-
const { result } = await this.postToRelayer(payload);
|
|
10295
|
-
return result;
|
|
10296
|
-
}
|
|
10297
|
-
async imGetTransactionByHash(hash) {
|
|
10298
|
-
const payload = {
|
|
10299
|
-
method: 'im_getTransactionByHash',
|
|
10300
|
-
params: [hash],
|
|
10301
|
-
};
|
|
10302
|
-
const { result } = await this.postToRelayer(payload);
|
|
10303
|
-
return result;
|
|
10304
|
-
}
|
|
10305
|
-
async imGetFeeOptions(userAddress, data) {
|
|
10306
|
-
const payload = {
|
|
10307
|
-
method: 'im_getFeeOptions',
|
|
10308
|
-
params: [{
|
|
10309
|
-
userAddress,
|
|
10310
|
-
data,
|
|
10311
|
-
chainId: this.config.zkEvmChainId.toString(),
|
|
10312
|
-
}],
|
|
10313
|
-
};
|
|
10314
|
-
const { result } = await this.postToRelayer(payload);
|
|
10315
|
-
return result;
|
|
10316
|
-
}
|
|
10317
|
-
}
|
|
10318
|
-
|
|
10319
|
-
const MESSAGE_TO_SIGN = 'Only sign this message from Immutable Passport';
|
|
10320
|
-
async function registerZkEvmUser({ authManager, magicProvider, multiRollupApiClients, accessToken, }) {
|
|
10321
|
-
const web3Provider = new Web3Provider(magicProvider);
|
|
10322
|
-
const ethSigner = web3Provider.getSigner();
|
|
10323
|
-
const ethereumAddress = await ethSigner.getAddress();
|
|
10324
|
-
const ethereumSignature = await signRaw(MESSAGE_TO_SIGN, ethSigner);
|
|
10325
|
-
const headers = { Authorization: `Bearer ${accessToken}` };
|
|
10326
|
-
try {
|
|
10327
|
-
await multiRollupApiClients.passportApi.createCounterfactualAddress({
|
|
10328
|
-
createCounterfactualAddressRequest: {
|
|
10329
|
-
ethereumAddress,
|
|
10330
|
-
ethereumSignature,
|
|
10331
|
-
},
|
|
10332
|
-
}, { headers });
|
|
10333
|
-
}
|
|
10334
|
-
catch (error) {
|
|
10335
|
-
throw new JsonRpcError(RpcErrorCode.INTERNAL_ERROR, `Failed to create counterfactual address: ${error}`);
|
|
10336
|
-
}
|
|
10337
|
-
const user = await authManager.loginSilent();
|
|
10338
|
-
if (!user?.zkEvm) {
|
|
10339
|
-
throw new JsonRpcError(RpcErrorCode.INTERNAL_ERROR, 'Failed to refresh user details');
|
|
10340
|
-
}
|
|
10341
|
-
return user;
|
|
10342
|
-
}
|
|
10343
|
-
|
|
10344
|
-
const loginZkEvmUser = async ({ authManager, config, magicAdapter, multiRollupApiClients, }) => {
|
|
10345
|
-
const user = await authManager.getUser() || await authManager.login();
|
|
10346
|
-
if (!user.idToken) {
|
|
10347
|
-
throw new Error('User is missing idToken');
|
|
10348
|
-
}
|
|
10349
|
-
const magicProvider = await magicAdapter.login(user.idToken, config.network);
|
|
10350
|
-
if (!user.zkEvm) {
|
|
10351
|
-
// Generate counterfactual address and retrieve updated Auth0 user
|
|
10352
|
-
const userZkevm = await registerZkEvmUser({
|
|
10353
|
-
authManager,
|
|
10354
|
-
magicProvider,
|
|
10355
|
-
multiRollupApiClients,
|
|
10356
|
-
accessToken: user.accessToken,
|
|
10357
|
-
});
|
|
10358
|
-
return {
|
|
10359
|
-
user: userZkevm,
|
|
10360
|
-
magicProvider,
|
|
10361
|
-
};
|
|
10362
|
-
}
|
|
10363
|
-
return {
|
|
10364
|
-
user: user,
|
|
10365
|
-
magicProvider,
|
|
10366
|
-
};
|
|
10367
|
-
};
|
|
10368
|
-
|
|
10369
|
-
class ZkEvmProvider extends TypedEventEmitter {
|
|
10416
|
+
class ZkEvmProvider {
|
|
10370
10417
|
authManager;
|
|
10371
10418
|
config;
|
|
10372
10419
|
confirmationScreen;
|
|
10373
10420
|
magicAdapter;
|
|
10374
10421
|
relayerClient;
|
|
10422
|
+
transactionAPI;
|
|
10375
10423
|
multiRollupApiClients;
|
|
10376
|
-
jsonRpcProvider; // Used for read
|
|
10424
|
+
jsonRpcProvider; // Used for read
|
|
10425
|
+
eventEmitter;
|
|
10377
10426
|
magicProvider; // Used for signing
|
|
10378
10427
|
user;
|
|
10379
10428
|
constructor({ authManager, magicAdapter, config, confirmationScreen, multiRollupApiClients, }) {
|
|
10380
|
-
super();
|
|
10381
10429
|
this.authManager = authManager;
|
|
10382
10430
|
this.magicAdapter = magicAdapter;
|
|
10383
10431
|
this.config = config;
|
|
10384
10432
|
this.confirmationScreen = confirmationScreen;
|
|
10385
10433
|
this.relayerClient = new RelayerClient({ config });
|
|
10434
|
+
this.transactionAPI = new TransactionsApi(new Configuration({
|
|
10435
|
+
basePath: this.config.imxPublicApiDomain,
|
|
10436
|
+
}));
|
|
10386
10437
|
this.jsonRpcProvider = new JsonRpcProvider(this.config.zkEvmRpcUrl);
|
|
10387
10438
|
this.multiRollupApiClients = multiRollupApiClients;
|
|
10439
|
+
this.eventEmitter = new TypedEventEmitter();
|
|
10388
10440
|
}
|
|
10389
10441
|
isLoggedIn() {
|
|
10390
10442
|
return this.magicProvider !== undefined && this.user !== undefined;
|
|
@@ -10403,7 +10455,7 @@ class ZkEvmProvider extends TypedEventEmitter {
|
|
|
10403
10455
|
});
|
|
10404
10456
|
this.user = user;
|
|
10405
10457
|
this.magicProvider = magicProvider;
|
|
10406
|
-
this.emit(
|
|
10458
|
+
this.eventEmitter.emit(ProviderEvent.ACCOUNTS_CHANGED, [this.user.zkEvm.ethAddress]);
|
|
10407
10459
|
return [this.user.zkEvm.ethAddress];
|
|
10408
10460
|
}
|
|
10409
10461
|
case 'eth_sendTransaction': {
|
|
@@ -10413,6 +10465,7 @@ class ZkEvmProvider extends TypedEventEmitter {
|
|
|
10413
10465
|
return sendTransaction({
|
|
10414
10466
|
params: request.params || [],
|
|
10415
10467
|
magicProvider: this.magicProvider,
|
|
10468
|
+
transactionAPI: this.transactionAPI,
|
|
10416
10469
|
jsonRpcProvider: this.jsonRpcProvider,
|
|
10417
10470
|
config: this.config,
|
|
10418
10471
|
relayerClient: this.relayerClient,
|
|
@@ -10532,6 +10585,12 @@ class ZkEvmProvider extends TypedEventEmitter {
|
|
|
10532
10585
|
}
|
|
10533
10586
|
throw new JsonRpcError(RpcErrorCode.INVALID_REQUEST, 'Invalid request');
|
|
10534
10587
|
}
|
|
10588
|
+
on(event, listener) {
|
|
10589
|
+
this.eventEmitter.on(event, listener);
|
|
10590
|
+
}
|
|
10591
|
+
removeListener(event, listener) {
|
|
10592
|
+
this.eventEmitter.removeListener(event, listener);
|
|
10593
|
+
}
|
|
10535
10594
|
}
|
|
10536
10595
|
|
|
10537
10596
|
class Passport {
|
|
@@ -10625,4 +10684,4 @@ class Passport {
|
|
|
10625
10684
|
}
|
|
10626
10685
|
}
|
|
10627
10686
|
|
|
10628
|
-
export { JsonRpcError, Networks, Passport, PassportError, ProviderErrorCode,
|
|
10687
|
+
export { JsonRpcError, Networks, Passport, PassportError, ProviderErrorCode, ProviderEvent, RpcErrorCode };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@imtbl/sdk",
|
|
3
3
|
"description": "Immutable Unified SDK",
|
|
4
|
-
"version": "0.3.
|
|
4
|
+
"version": "0.3.1-alpha",
|
|
5
5
|
"author": "Immutable",
|
|
6
6
|
"browser": "./dist/index.browser.js",
|
|
7
7
|
"bugs": "https://github.com/immutable/ts-immutable-sdk/issues",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"@ethersproject/abstract-signer": "^5.7.0",
|
|
12
12
|
"@ethersproject/providers": "^5.7.2",
|
|
13
13
|
"@ethersproject/wallet": "^5.7.0",
|
|
14
|
-
"@imtbl/core-sdk": "^
|
|
14
|
+
"@imtbl/core-sdk": "^2.0.1",
|
|
15
15
|
"@jest/globals": "^29.5.0",
|
|
16
16
|
"@magic-ext/oidc": "^1.0.1",
|
|
17
17
|
"@metamask/detect-provider": "^2.0.0",
|