carbon-js-sdk 0.11.66 → 0.11.67
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/CarbonSDK.js +4 -4
- package/lib/provider/metamask/MetaMask.d.ts +1 -1
- package/lib/provider/metamask/MetaMask.js +5 -5
- package/lib/provider/rainbowKit/RainbowKitAccount.d.ts +1 -1
- package/lib/provider/rainbowKit/RainbowKitAccount.js +5 -5
- package/lib/util/auth.d.ts +1 -1
- package/lib/util/auth.js +2 -3
- package/lib/wallet/CarbonWallet.d.ts +2 -1
- package/lib/wallet/CarbonWallet.js +10 -9
- package/package.json +1 -1
package/lib/CarbonSDK.js
CHANGED
|
@@ -375,7 +375,7 @@ class CarbonSDK {
|
|
|
375
375
|
let message;
|
|
376
376
|
let signature;
|
|
377
377
|
if (signMessageRequired) {
|
|
378
|
-
const result = yield MetaMask_1.MetaMask.signAndRecoverPubKey(metamask, opts === null || opts === void 0 ? void 0 : opts.enableJwtAuth);
|
|
378
|
+
const result = yield MetaMask_1.MetaMask.signAndRecoverPubKey(metamask, opts === null || opts === void 0 ? void 0 : opts.enableJwtAuth, opts === null || opts === void 0 ? void 0 : opts.authMessage);
|
|
379
379
|
pubKey = result.publicKey;
|
|
380
380
|
message = result.message;
|
|
381
381
|
signature = result.signature;
|
|
@@ -388,7 +388,7 @@ class CarbonSDK {
|
|
|
388
388
|
public_key: Buffer.from(pubKey, 'base64').toString('hex'),
|
|
389
389
|
signature: signature,
|
|
390
390
|
};
|
|
391
|
-
yield wallet.reloadJwtToken(authRequest);
|
|
391
|
+
yield wallet.reloadJwtToken(authRequest, opts === null || opts === void 0 ? void 0 : opts.authMessage);
|
|
392
392
|
}
|
|
393
393
|
return this.connect(wallet, opts);
|
|
394
394
|
});
|
|
@@ -406,7 +406,7 @@ class CarbonSDK {
|
|
|
406
406
|
let message;
|
|
407
407
|
let signature;
|
|
408
408
|
if (signMessageRequired) {
|
|
409
|
-
const result = yield RainbowKitAccount_1.default.signAndRecoverPubKey(rainbowKit, opts === null || opts === void 0 ? void 0 : opts.enableJwtAuth);
|
|
409
|
+
const result = yield RainbowKitAccount_1.default.signAndRecoverPubKey(rainbowKit, opts === null || opts === void 0 ? void 0 : opts.enableJwtAuth, opts === null || opts === void 0 ? void 0 : opts.authMessage);
|
|
410
410
|
pubKey = result.publicKey;
|
|
411
411
|
message = result.message;
|
|
412
412
|
signature = result.signature;
|
|
@@ -419,7 +419,7 @@ class CarbonSDK {
|
|
|
419
419
|
public_key: Buffer.from(pubKey, 'base64').toString('hex'),
|
|
420
420
|
signature: signature,
|
|
421
421
|
};
|
|
422
|
-
yield wallet.reloadJwtToken(authRequest);
|
|
422
|
+
yield wallet.reloadJwtToken(authRequest, opts === null || opts === void 0 ? void 0 : opts.authMessage);
|
|
423
423
|
}
|
|
424
424
|
return this.connect(wallet, opts);
|
|
425
425
|
});
|
|
@@ -39,7 +39,7 @@ export declare class MetaMask extends Eip6963Provider {
|
|
|
39
39
|
private blockchain;
|
|
40
40
|
private connectedAccount;
|
|
41
41
|
static createMetamaskSigner(metamask: MetaMask, evmChainId: string, pubKeyBase64: string, addressOptions: SWTHAddressOptions): CarbonSigner;
|
|
42
|
-
static signAndRecoverPubKey(provider: MetaMask, enableJwtAuth?: boolean): Promise<{
|
|
42
|
+
static signAndRecoverPubKey(provider: MetaMask, enableJwtAuth?: boolean, customMsg?: string): Promise<{
|
|
43
43
|
publicKey: string;
|
|
44
44
|
signature: string;
|
|
45
45
|
message: string;
|
|
@@ -269,16 +269,16 @@ class MetaMask extends eip6963Provider_1.Eip6963Provider {
|
|
|
269
269
|
signMessage,
|
|
270
270
|
};
|
|
271
271
|
}
|
|
272
|
-
static signAndRecoverPubKey(provider, enableJwtAuth) {
|
|
272
|
+
static signAndRecoverPubKey(provider, enableJwtAuth, customMsg = evm_1.DEFAULT_PUBLIC_KEY_MESSAGE) {
|
|
273
273
|
return __awaiter(this, void 0, void 0, function* () {
|
|
274
274
|
const address = yield provider.defaultAccount();
|
|
275
|
-
const
|
|
276
|
-
const signature = yield provider.personalSign(address,
|
|
277
|
-
const publicKeyHex = index_1.EvmUtils.recoverPublicKey(
|
|
275
|
+
const signMessage = enableJwtAuth ? index_1.AuthUtils.getAuthMessage(customMsg) : customMsg;
|
|
276
|
+
const signature = yield provider.personalSign(address, signMessage);
|
|
277
|
+
const publicKeyHex = index_1.EvmUtils.recoverPublicKey(signMessage, signature);
|
|
278
278
|
return {
|
|
279
279
|
publicKey: Buffer.from(publicKeyHex, 'hex').toString('base64'),
|
|
280
280
|
signature,
|
|
281
|
-
message,
|
|
281
|
+
message: signMessage,
|
|
282
282
|
};
|
|
283
283
|
});
|
|
284
284
|
}
|
|
@@ -25,7 +25,7 @@ declare class RainbowKitAccount extends Eip6963Provider {
|
|
|
25
25
|
verifyNetworkAndConnectedAccount(evmHexAddress: string, evmChainId: string): Promise<void>;
|
|
26
26
|
verifyConnectedAccount(address: string): Promise<void>;
|
|
27
27
|
verifyNetwork(evmChainId: string): Promise<void>;
|
|
28
|
-
static signAndRecoverPubKey(provider: RainbowKitAccount, enableJwtAuth?: boolean): Promise<{
|
|
28
|
+
static signAndRecoverPubKey(provider: RainbowKitAccount, enableJwtAuth?: boolean, customMsg?: string): Promise<{
|
|
29
29
|
publicKey: string;
|
|
30
30
|
signature: string;
|
|
31
31
|
message: string;
|
|
@@ -253,16 +253,16 @@ class RainbowKitAccount extends eip6963Provider_1.Eip6963Provider {
|
|
|
253
253
|
yield this.changeNetworkIfRequired("Carbon", network);
|
|
254
254
|
});
|
|
255
255
|
}
|
|
256
|
-
static signAndRecoverPubKey(provider, enableJwtAuth) {
|
|
256
|
+
static signAndRecoverPubKey(provider, enableJwtAuth, customMsg = evm_1.DEFAULT_PUBLIC_KEY_MESSAGE) {
|
|
257
257
|
return __awaiter(this, void 0, void 0, function* () {
|
|
258
258
|
const address = yield provider.defaultAccount();
|
|
259
|
-
const
|
|
260
|
-
const signature = yield provider.personalSign(address,
|
|
261
|
-
const publicKeyHex = index_1.EvmUtils.recoverPublicKey(
|
|
259
|
+
const signMessage = enableJwtAuth ? index_1.AuthUtils.getAuthMessage(customMsg) : customMsg;
|
|
260
|
+
const signature = yield provider.personalSign(address, signMessage);
|
|
261
|
+
const publicKeyHex = index_1.EvmUtils.recoverPublicKey(signMessage, signature);
|
|
262
262
|
return {
|
|
263
263
|
publicKey: Buffer.from(publicKeyHex, 'hex').toString('base64'),
|
|
264
264
|
signature,
|
|
265
|
-
message,
|
|
265
|
+
message: signMessage,
|
|
266
266
|
};
|
|
267
267
|
});
|
|
268
268
|
}
|
package/lib/util/auth.d.ts
CHANGED
|
@@ -25,7 +25,7 @@ export declare type GrantRequest = {
|
|
|
25
25
|
signature: string;
|
|
26
26
|
refresh_token?: string;
|
|
27
27
|
});
|
|
28
|
-
export declare const getAuthMessage: () => string;
|
|
28
|
+
export declare const getAuthMessage: (message: string) => string;
|
|
29
29
|
export declare const hasExpired: (exp?: number) => boolean;
|
|
30
30
|
export declare const hasRefreshTokenExpired: (refreshToken: string) => boolean;
|
|
31
31
|
export declare const isValidIssuer: (iss: string | undefined, network: Network) => boolean;
|
package/lib/util/auth.js
CHANGED
|
@@ -6,7 +6,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.isValidIssuer = exports.hasRefreshTokenExpired = exports.hasExpired = exports.getAuthMessage = exports.GrantType = exports.expirybufferSeconds = void 0;
|
|
7
7
|
const constant_1 = require("../constant");
|
|
8
8
|
const dayjs_1 = __importDefault(require("dayjs"));
|
|
9
|
-
const evm_1 = require("./evm");
|
|
10
9
|
exports.expirybufferSeconds = 30;
|
|
11
10
|
var GrantType;
|
|
12
11
|
(function (GrantType) {
|
|
@@ -14,9 +13,9 @@ var GrantType;
|
|
|
14
13
|
GrantType["SignatureEth"] = "signature_eth";
|
|
15
14
|
GrantType["RefreshToken"] = "refresh_token";
|
|
16
15
|
})(GrantType = exports.GrantType || (exports.GrantType = {}));
|
|
17
|
-
const getAuthMessage = () => {
|
|
16
|
+
const getAuthMessage = (message) => {
|
|
18
17
|
const timestamp = dayjs_1.default().format('YYYY/MM/DD HH:mm:ss Z');
|
|
19
|
-
return `${
|
|
18
|
+
return `${message}\n[${timestamp}]`;
|
|
20
19
|
};
|
|
21
20
|
exports.getAuthMessage = getAuthMessage;
|
|
22
21
|
const hasExpired = (exp = 0) => {
|
|
@@ -34,6 +34,7 @@ export interface CarbonWalletGenericOpts {
|
|
|
34
34
|
gasFee?: GasFee;
|
|
35
35
|
isRainbowKit?: boolean;
|
|
36
36
|
enableJwtAuth?: boolean;
|
|
37
|
+
authMessage?: string;
|
|
37
38
|
jwt?: AccessTokenResponse;
|
|
38
39
|
/**
|
|
39
40
|
* Optional callback that will be called before signing is requested/executed.
|
|
@@ -156,7 +157,7 @@ export declare class CarbonWallet {
|
|
|
156
157
|
static withRainbowKit(rainbowKit: RainbowKitAccount, evmChainId: string, compressedPubKeyBase64: string, addressOptions: SWTHAddressOptions, walletProvider: SupportedEip6963Provider, opts?: Omit<CarbonWalletInitOpts, "signer">): CarbonWallet;
|
|
157
158
|
static withAddress(bech32Address: string, opts?: Partial<CarbonWalletInitOpts>): CarbonWallet;
|
|
158
159
|
initialize(queryClient: CarbonQueryClient, gasFee: GasFee, fallbackConfig?: OverrideConfig | null, opts?: CarbonWalletGenericOpts): Promise<CarbonWallet>;
|
|
159
|
-
reloadJwtToken(request?: GrantRequest): Promise<void>;
|
|
160
|
+
reloadJwtToken(request?: GrantRequest, authMessage?: string): Promise<void>;
|
|
160
161
|
queryViewOnlyEvmHexAddress(): Promise<void>;
|
|
161
162
|
private refreshJwtToken;
|
|
162
163
|
private getNewJwtToken;
|
|
@@ -28,6 +28,7 @@ const address_1 = require("../util/address");
|
|
|
28
28
|
const auth_2 = require("../util/auth");
|
|
29
29
|
const blockchain_1 = require("../util/blockchain");
|
|
30
30
|
const ethermint_1 = require("../util/ethermint");
|
|
31
|
+
const evm_1 = require("../util/evm");
|
|
31
32
|
const fetch_1 = require("../util/fetch");
|
|
32
33
|
const generic_1 = require("../util/generic");
|
|
33
34
|
const number_1 = require("../util/number");
|
|
@@ -182,7 +183,7 @@ class CarbonWallet {
|
|
|
182
183
|
if (!this.tmClient)
|
|
183
184
|
promises.push(this.reconnectTmClient(fallbackConfig));
|
|
184
185
|
if ((opts === null || opts === void 0 ? void 0 : opts.enableJwtAuth) && !this.isViewOnlyWallet())
|
|
185
|
-
promises.push(this.reloadJwtToken());
|
|
186
|
+
promises.push(this.reloadJwtToken(undefined, opts === null || opts === void 0 ? void 0 : opts.authMessage));
|
|
186
187
|
if (this.isViewOnlyWallet())
|
|
187
188
|
promises.push(this.queryViewOnlyEvmHexAddress());
|
|
188
189
|
yield Promise.all(promises);
|
|
@@ -190,22 +191,22 @@ class CarbonWallet {
|
|
|
190
191
|
return this;
|
|
191
192
|
});
|
|
192
193
|
}
|
|
193
|
-
reloadJwtToken(request) {
|
|
194
|
+
reloadJwtToken(request, authMessage = evm_1.DEFAULT_PUBLIC_KEY_MESSAGE) {
|
|
194
195
|
return __awaiter(this, void 0, void 0, function* () {
|
|
195
196
|
const network = this.network;
|
|
196
197
|
if (this.jwt) {
|
|
197
198
|
const { iss, exp } = jwt_decode_1.jwtDecode(this.jwt.access_token);
|
|
198
199
|
if (!auth_2.isValidIssuer(iss, network))
|
|
199
|
-
return this.getNewJwtToken(request);
|
|
200
|
+
return this.getNewJwtToken(authMessage, request);
|
|
200
201
|
const accessTokenExpired = auth_2.hasExpired(exp);
|
|
201
202
|
if (accessTokenExpired) {
|
|
202
203
|
if (!auth_2.hasRefreshTokenExpired(this.jwt.refresh_token))
|
|
203
204
|
return this.refreshJwtToken(this.jwt.refresh_token);
|
|
204
|
-
return this.getNewJwtToken(request);
|
|
205
|
+
return this.getNewJwtToken(authMessage, request);
|
|
205
206
|
}
|
|
206
207
|
return;
|
|
207
208
|
}
|
|
208
|
-
return this.getNewJwtToken(request);
|
|
209
|
+
return this.getNewJwtToken(authMessage, request);
|
|
209
210
|
});
|
|
210
211
|
}
|
|
211
212
|
queryViewOnlyEvmHexAddress() {
|
|
@@ -233,19 +234,19 @@ class CarbonWallet {
|
|
|
233
234
|
this.jwt = response.data.result;
|
|
234
235
|
});
|
|
235
236
|
}
|
|
236
|
-
getNewJwtToken(request) {
|
|
237
|
+
getNewJwtToken(authMessage, request) {
|
|
237
238
|
return __awaiter(this, void 0, void 0, function* () {
|
|
238
|
-
const req = request !== null && request !== void 0 ? request : yield this.constructGrantRequest();
|
|
239
|
+
const req = request !== null && request !== void 0 ? request : yield this.constructGrantRequest(authMessage);
|
|
239
240
|
const response = yield axios_1.default.post(this.networkConfig.authUrl, req);
|
|
240
241
|
this.jwt = response.data.result;
|
|
241
242
|
});
|
|
242
243
|
}
|
|
243
|
-
constructGrantRequest() {
|
|
244
|
+
constructGrantRequest(authMessage) {
|
|
244
245
|
return __awaiter(this, void 0, void 0, function* () {
|
|
245
246
|
try {
|
|
246
247
|
yield util_1.GenericUtils.callIgnoreError(() => { var _a; return (_a = this.onRequestAuth) === null || _a === void 0 ? void 0 : _a.call(this); });
|
|
247
248
|
const address = this.isEvmWallet() ? this.evmHexAddress : this.bech32Address;
|
|
248
|
-
const message = util_1.AuthUtils.getAuthMessage();
|
|
249
|
+
const message = util_1.AuthUtils.getAuthMessage(authMessage);
|
|
249
250
|
const signature = yield this.signer.signMessage(address, message);
|
|
250
251
|
return {
|
|
251
252
|
grant_type: this.isEvmWallet() ? auth_2.GrantType.SignatureEth : auth_2.GrantType.SignatureCosmos,
|