Skip to content

Commit 11bd747

Browse files
committed
- allow sign options for keplr mobile to support custom gas fees
1 parent 4633f94 commit 11bd747

File tree

6 files changed

+118
-17
lines changed

6 files changed

+118
-17
lines changed

src/wallet/walletconnect/WalletConnectV2.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ export class WalletConnectV2 {
183183
public async signAmino(
184184
chainId: string,
185185
signerAddress: string,
186-
stdSignDoc: StdSignDoc
186+
stdSignDoc: StdSignDoc,
187187
): Promise<SignAminoResponse> {
188188
const { signature, signed } = await this.request<WcSignAminoResponse>(
189189
chainId,
@@ -202,7 +202,7 @@ export class WalletConnectV2 {
202202
public async signDirect(
203203
chainId: string,
204204
signerAddress: string,
205-
signDoc: SignDoc
205+
signDoc: SignDoc,
206206
): Promise<SignDirectResponse> {
207207
const { signature, signed } = await this.request<WcSignDirectResponse>(
208208
chainId,
@@ -264,7 +264,7 @@ export class WalletConnectV2 {
264264
}
265265
}
266266

267-
private async request<T>(chainId: string, method: Method, params: unknown) {
267+
protected async request<T>(chainId: string, method: Method, params: unknown) {
268268
const session = localStorage.getItem(this.sessionStorageKey);
269269
if (!session || !this.signClient) {
270270
throw new Error("Session not found for " + chainId);
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { SignDoc, StdSignDoc } from "cosmes/registry";
2+
3+
import { MobileAppDetails } from "./QRCodeModal";
4+
import { WalletConnectV2 } from "./WalletConnectV2";
5+
6+
/**
7+
* The data returned by the `cosmos_signAmino` method. `signed` is optional
8+
* because some wallets (like Cosmostation) may not return it.
9+
*/
10+
type WcSignAminoResponse = {
11+
signature: {
12+
signature: string;
13+
};
14+
signed?: StdSignDoc | undefined;
15+
};
16+
type SignAminoResponse = Required<WcSignAminoResponse>;
17+
18+
/**
19+
* The data returned by the `cosmos_signDirect` method. `signed` is optional
20+
* because some wallets (like Cosmostation) may not return it.
21+
*/
22+
type WcSignDirectResponse = {
23+
signature: {
24+
signature: string;
25+
};
26+
signed?: SignDoc | undefined;
27+
};
28+
type SignDirectResponse = Required<WcSignDirectResponse>;
29+
30+
const Method = {
31+
GET_ACCOUNTS: "cosmos_getAccounts",
32+
SIGN_AMINO: "cosmos_signAmino",
33+
SIGN_DIRECT: "cosmos_signDirect",
34+
} as const;
35+
type Method = (typeof Method)[keyof typeof Method];
36+
37+
export interface SignOptions {
38+
readonly preferNoSetFee?: boolean;
39+
readonly preferNoSetMemo?: boolean;
40+
readonly disableBalanceCheck?: boolean;
41+
}
42+
43+
export class WalletConnectV2Keplr extends WalletConnectV2 {
44+
defaultOptions: {
45+
sign: { preferNoSetFee: boolean; preferNoSetMemo: boolean };
46+
};
47+
48+
constructor(projectId: string, mobileAppDetails: MobileAppDetails) {
49+
super(projectId, mobileAppDetails);
50+
this.defaultOptions = {
51+
sign: { preferNoSetFee: true, preferNoSetMemo: true },
52+
};
53+
}
54+
55+
public async signAminoKeplr(
56+
chainId: string,
57+
signerAddress: string,
58+
stdSignDoc: StdSignDoc,
59+
signOptions: SignOptions,
60+
): Promise<SignAminoResponse> {
61+
const { signature, signed } = await this.request<WcSignAminoResponse>(
62+
chainId,
63+
Method.SIGN_AMINO,
64+
{
65+
signerAddress,
66+
signDoc: stdSignDoc,
67+
signOptions: signOptions,
68+
}
69+
);
70+
return {
71+
signature: signature,
72+
signed: signed ?? stdSignDoc, // simply return the original sign doc if `signed` is not returned
73+
};
74+
}
75+
76+
public async signDirectKeplr(
77+
chainId: string,
78+
signerAddress: string,
79+
signDoc: SignDoc,
80+
signOptions: SignOptions,
81+
): Promise<SignDirectResponse> {
82+
const { signature, signed } = await this.request<WcSignDirectResponse>(
83+
chainId,
84+
Method.SIGN_DIRECT,
85+
{
86+
signerAddress,
87+
signDoc,
88+
signOptions:signOptions
89+
}
90+
);
91+
return {
92+
signature: signature,
93+
signed: signed ?? signDoc, // simply return the original sign doc if `signed` is not returned
94+
};
95+
}
96+
}

src/wallet/wallets/cosmostation/CosmostationController.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ import { base64 } from "cosmes/codec";
44
import { WalletName } from "../../constants/WalletName";
55
import { WalletType } from "../../constants/WalletType";
66
import { onWindowEvent } from "../../utils/window";
7-
import { WalletConnectV2 } from "../../walletconnect/WalletConnectV2";
87
import { ConnectedWallet } from "../ConnectedWallet";
98
import { ChainInfo, WalletController } from "../WalletController";
109
import { WalletError } from "../WalletError";
1110
import { CosmostationExtension } from "./CosmostationExtension";
1211
import { CosmostationWalletConnectV2 } from "./CosmostationWalletConnectV2";
12+
import { WalletConnectV2Keplr } from "../../walletconnect/WalletConnectV2Keplr";
1313

1414
export class CosmostationController extends WalletController {
15-
private readonly wc: WalletConnectV2;
15+
private readonly wc: WalletConnectV2Keplr;
1616

1717
constructor(wcProjectId: string) {
1818
super(WalletName.COSMOSTATION);
19-
this.wc = new WalletConnectV2(wcProjectId, {
19+
this.wc = new WalletConnectV2Keplr(wcProjectId, {
2020
// https://github.com/cosmostation/cosmostation-wc-modal/blob/main/src/modal.tsx#L22-L34
2121
name: "Cosmostation",
2222
android:

src/wallet/wallets/keplr/KeplrController.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ import { base64 } from "cosmes/codec";
44
import { WalletName } from "../../constants/WalletName";
55
import { WalletType } from "../../constants/WalletType";
66
import { onWindowEvent } from "../../utils/window";
7-
import { WalletConnectV2 } from "../../walletconnect/WalletConnectV2";
87
import { ConnectedWallet } from "../ConnectedWallet";
98
import { ChainInfo, WalletController } from "../WalletController";
109
import { WalletError } from "../WalletError";
1110
import { KeplrExtension } from "./KeplrExtension";
1211
import { KeplrWalletConnectV2 } from "./KeplrWalletConnectV2";
12+
import { WalletConnectV2Keplr } from "cosmes/wallet/walletconnect/WalletConnectV2Keplr";
1313

1414
export class KeplrController extends WalletController {
15-
private readonly wc: WalletConnectV2;
15+
private readonly wc: WalletConnectV2Keplr;
1616

1717
constructor(wcProjectId: string) {
1818
super(WalletName.KEPLR);
19-
this.wc = new WalletConnectV2(wcProjectId, {
19+
this.wc = new WalletConnectV2Keplr(wcProjectId, {
2020
// https://github.com/chainapsis/keplr-wallet/blob/master/packages/wc-qrcode-modal/src/modal.tsx#L61-L75
2121
name: "Keplr",
2222
android:

src/wallet/wallets/keplr/KeplrWalletConnectV2.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@ import {
1313
} from "cosmes/protobufs";
1414
import { WalletError, WalletName, WalletType } from "cosmes/wallet";
1515

16-
import { WalletConnectV2 } from "../../walletconnect/WalletConnectV2";
1716
import {
1817
ConnectedWallet,
1918
SignArbitraryResponse,
2019
UnsignedTx,
2120
} from "../ConnectedWallet";
21+
import { WalletConnectV2Keplr } from "../../walletconnect/WalletConnectV2Keplr";
2222

2323
export class KeplrWalletConnectV2 extends ConnectedWallet {
24-
private readonly wc: WalletConnectV2;
24+
private readonly wc: WalletConnectV2Keplr;
2525
private readonly useAmino: boolean;
2626

2727
constructor(
2828
walletName: WalletName,
29-
wc: WalletConnectV2,
29+
wc: WalletConnectV2Keplr,
3030
chainId: string,
3131
pubKey: Secp256k1PubKey,
3232
address: string,
@@ -72,15 +72,20 @@ export class KeplrWalletConnectV2 extends ConnectedWallet {
7272
memo,
7373
timeoutHeight,
7474
};
75+
const signOptions = {
76+
preferNoSetFee: true,
77+
preferNoSetMemo: true,
78+
};
79+
7580
let txRaw: TxRaw;
7681
if (this.useAmino) {
7782
const { signed, signature } = await WalletError.wrap(
78-
this.wc.signAmino(this.chainId, this.address, tx.toStdSignDoc(params))
83+
this.wc.signAminoKeplr(this.chainId, this.address, tx.toStdSignDoc(params), signOptions)
7984
);
8085
txRaw = tx.toSignedAmino(signed, signature.signature);
8186
} else {
8287
const { signed, signature } = await WalletError.wrap(
83-
this.wc.signDirect(this.chainId, this.address, tx.toSignDoc(params))
88+
this.wc.signDirectKeplr(this.chainId, this.address, tx.toSignDoc(params), signOptions)
8489
);
8590
txRaw = tx.toSignedDirect(signed, signature.signature);
8691
}

src/wallet/wallets/leap/LeapController.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ import { base64 } from "cosmes/codec";
44
import { WalletName } from "../../constants/WalletName";
55
import { WalletType } from "../../constants/WalletType";
66
import { onWindowEvent } from "../../utils/window";
7-
import { WalletConnectV2 } from "../../walletconnect/WalletConnectV2";
87
import { ConnectedWallet } from "../ConnectedWallet";
98
import { ChainInfo, WalletController } from "../WalletController";
109
import { WalletError } from "../WalletError";
1110
import { LeapExtension } from "./LeapExtension";
1211
import { LeapWalletConnectV2 } from "./LeapWalletConnectV2";
12+
import { WalletConnectV2Keplr } from "../../walletconnect/WalletConnectV2Keplr";
1313

1414
export class LeapController extends WalletController {
15-
private readonly wc: WalletConnectV2;
15+
private readonly wc: WalletConnectV2Keplr;
1616

1717
constructor(wcProjectId: string) {
1818
super(WalletName.LEAP);
19-
this.wc = new WalletConnectV2(wcProjectId, {
19+
this.wc = new WalletConnectV2Keplr(wcProjectId, {
2020
name: "Leap",
2121
android:
2222
"leapcosmos://wcV2#Intent;package=io.leapwallet.cosmos;scheme=leapwallet;end;",

0 commit comments

Comments
 (0)