Skip to content

Commit

Permalink
fix: allow memo and fee to be set for mobile wallets
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronCQL committed Aug 24, 2023
1 parent 19bd5ba commit 1172c74
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 29 deletions.
35 changes: 21 additions & 14 deletions src/wallet/walletconnect/WalletConnectV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,21 @@ type GetAccountResponse = {
pubkey: string;
};

type SignAminoResponse =
| {
// Keplr
signature: {
signature: string;
};
}
| {
// Cosmostation
signature: string;
};
type SignAminoResponse = {
signature: string;
signed: StdSignDoc;
};

/**
* The data returned by the `cosmos_signAmino` for keplr-like wallets. `signed` is
* optional because some wallets (like cosmostation) do not return it.
*/
type PartialSignAminoResponse = {
signature: {
signature: string;
};
signed?: StdSignDoc | undefined;
};

const Method = {
GET_ACCOUNTS: "cosmos_getAccounts",
Expand Down Expand Up @@ -172,16 +176,19 @@ export class WalletConnectV2 {
chainId: string,
signerAddress: string,
signDoc: StdSignDoc
): Promise<string> {
const { signature } = await this.request<SignAminoResponse>(
): Promise<SignAminoResponse> {
const { signature, signed } = await this.request<PartialSignAminoResponse>(
chainId,
Method.SIGN_AMINO,
{
signerAddress,
signDoc,
}
);
return typeof signature === "string" ? signature : signature.signature;
return {
signature: signature.signature,
signed: signed ?? signDoc, // simply return the original sign doc if `signed` is not returned
};
}

/**
Expand Down
16 changes: 11 additions & 5 deletions src/wallet/wallets/cosmostation/CosmostationWalletConnectV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Adapter, broadcastTx } from "cosmes/client";
import { fromBase64ToUint8Array } from "cosmes/codec";
import {
CosmosBaseV1beta1Coin as Coin,
CosmosTxV1beta1Fee as Fee,
CosmosTxSigningV1beta1SignMode as SignMode,
} from "cosmes/protobufs";
import { WalletName, WalletType } from "cosmes/wallet";
Expand Down Expand Up @@ -52,7 +53,7 @@ export class CosmostationWalletConnectV2 extends ConnectedWallet {
opts
);
const { memo } = unsignedTx;
const signature = await this.wc.signAmino(
const { signature, signed } = await this.wc.signAmino(
this.chainId,
this.address,
tx.toStdSignDoc({
Expand All @@ -65,12 +66,17 @@ export class CosmostationWalletConnectV2 extends ConnectedWallet {
);
// Since `sendTx` on WC isn't implemented yet, we have to broadcast manually
return broadcastTx(this.rpc, {
sequence,
fee,
tx,
sequence: BigInt(signed.sequence),
fee: new Fee({
amount: signed.fee.amount as Coin[],
gasLimit: BigInt(signed.fee.gas),
payer: signed.fee.payer,
granter: signed.fee.granter,
}),
signMode: SignMode.LEGACY_AMINO_JSON,
signature: fromBase64ToUint8Array(signature),
tx,
memo,
memo: signed.memo,
});
}
}
16 changes: 11 additions & 5 deletions src/wallet/wallets/keplr/KeplrWalletConnectV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Adapter, broadcastTx } from "cosmes/client";
import { fromBase64ToUint8Array } from "cosmes/codec";
import {
CosmosBaseV1beta1Coin as Coin,
CosmosTxV1beta1Fee as Fee,
CosmosTxSigningV1beta1SignMode as SignMode,
} from "cosmes/protobufs";
import { WalletName, WalletType } from "cosmes/wallet";
Expand Down Expand Up @@ -53,7 +54,7 @@ export class KeplrWalletConnectV2 extends ConnectedWallet {
opts
);
const { memo } = unsignedTx;
const signature = await this.wc.signAmino(
const { signature, signed } = await this.wc.signAmino(
this.chainId,
this.address,
tx.toStdSignDoc({
Expand All @@ -66,12 +67,17 @@ export class KeplrWalletConnectV2 extends ConnectedWallet {
);
// Since `sendTx` on WC isn't implemented yet, we have to broadcast manually
return broadcastTx(this.rpc, {
sequence,
fee,
tx,
sequence: BigInt(signed.sequence),
fee: new Fee({
amount: signed.fee.amount as Coin[],
gasLimit: BigInt(signed.fee.gas),
payer: signed.fee.payer,
granter: signed.fee.granter,
}),
signMode: SignMode.LEGACY_AMINO_JSON,
signature: fromBase64ToUint8Array(signature),
tx,
memo,
memo: signed.memo,
});
}
}
16 changes: 11 additions & 5 deletions src/wallet/wallets/leap/LeapWalletConnectV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Adapter, broadcastTx } from "cosmes/client";
import { fromBase64ToUint8Array } from "cosmes/codec";
import {
CosmosBaseV1beta1Coin as Coin,
CosmosTxV1beta1Fee as Fee,
CosmosTxSigningV1beta1SignMode as SignMode,
} from "cosmes/protobufs";
import { WalletName, WalletType } from "cosmes/wallet";
Expand Down Expand Up @@ -52,7 +53,7 @@ export class LeapWalletConnectV2 extends ConnectedWallet {
opts
);
const { memo } = unsignedTx;
const signature = await this.wc.signAmino(
const { signature, signed } = await this.wc.signAmino(
this.chainId,
this.address,
tx.toStdSignDoc({
Expand All @@ -65,12 +66,17 @@ export class LeapWalletConnectV2 extends ConnectedWallet {
);
// Since `sendTx` on WC isn't implemented yet, we have to broadcast manually
return broadcastTx(this.rpc, {
sequence,
fee,
tx,
sequence: BigInt(signed.sequence),
fee: new Fee({
amount: signed.fee.amount as Coin[],
gasLimit: BigInt(signed.fee.gas),
payer: signed.fee.payer,
granter: signed.fee.granter,
}),
signMode: SignMode.LEGACY_AMINO_JSON,
signature: fromBase64ToUint8Array(signature),
tx,
memo,
memo: signed.memo,
});
}
}

0 comments on commit 1172c74

Please sign in to comment.