Skip to content

Commit

Permalink
refactor: use keplr wallet connect if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronCQL committed Dec 11, 2023
1 parent 97c2c07 commit 4e9f7c8
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 198 deletions.
4 changes: 3 additions & 1 deletion src/wallet/wallets/cosmostation/CosmostationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ export class CosmostationController extends WalletController {
wallets.set(
chainId,
new CosmostationWalletConnectV2(
this.id,
this.wc,
chainId,
key,
address,
rpc,
gasPrice
gasPrice,
true // TODO: use sign mode direct when supported
)
);
}
Expand Down
84 changes: 3 additions & 81 deletions src/wallet/wallets/cosmostation/CosmostationWalletConnectV2.ts
Original file line number Diff line number Diff line change
@@ -1,82 +1,4 @@
import { PlainMessage } from "@bufbuild/protobuf";
import { Secp256k1PubKey, Tx, broadcastTx } from "cosmes/client";
import { base64 } from "cosmes/codec";
import {
CosmosBaseV1beta1Coin as Coin,
CosmosTxV1beta1Fee as Fee,
CosmosTxSigningV1beta1SignMode as SignMode,
} from "cosmes/protobufs";
import { WalletName, WalletType } from "cosmes/wallet";
import { KeplrWalletConnectV2 } from "../keplr/KeplrWalletConnectV2";

import { WalletConnectV2 } from "../../walletconnect/WalletConnectV2";
import {
ConnectedWallet,
SignArbitraryResponse,
UnsignedTx,
} from "../ConnectedWallet";

export class CosmostationWalletConnectV2 extends ConnectedWallet {
private readonly wc: WalletConnectV2;

constructor(
wc: WalletConnectV2,
chainId: string,
pubKey: Secp256k1PubKey,
address: string,
rpc: string,
gasPrice: PlainMessage<Coin>
) {
super(
WalletName.COSMOSTATION,
WalletType.WALLETCONNECT,
chainId,
pubKey,
address,
rpc,
gasPrice
);
this.wc = wc;
}

public async signArbitrary(_data: string): Promise<SignArbitraryResponse> {
// ! Not implemented by Cosmostation
throw new Error("Method not implemented.");
}

public async signAndBroadcastTx(
{ msgs, memo }: UnsignedTx,
fee: Fee,
accountNumber: bigint,
sequence: bigint
): Promise<string> {
const tx = new Tx({
chainId: this.chainId,
pubKey: this.pubKey,
msgs: msgs,
});
const { signature, signed } = await this.wc.signAmino(
this.chainId,
this.address,
tx.toStdSignDoc({
accountNumber,
sequence,
fee,
memo,
})
);
// Since `sendTx` on WC isn't implemented yet, we have to broadcast manually
return broadcastTx(this.rpc, {
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: base64.decode(signature),
memo: signed.memo,
});
}
}
// Cosmostation's API is similar to Keplr.
export const CosmostationWalletConnectV2 = KeplrWalletConnectV2;
11 changes: 10 additions & 1 deletion src/wallet/wallets/keplr/KeplrController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,16 @@ export class KeplrController extends WalletController {
});
wallets.set(
chainId,
new KeplrWalletConnectV2(this.wc, chainId, key, address, rpc, gasPrice)
new KeplrWalletConnectV2(
this.id,
this.wc,
chainId,
key,
address,
rpc,
gasPrice,
true // TODO: use sign mode direct when supported
)
);
}
return { wallets, wc: this.wc };
Expand Down
8 changes: 4 additions & 4 deletions src/wallet/wallets/keplr/KeplrExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {

export class KeplrExtension extends ConnectedWallet {
private readonly ext: Keplr;
private readonly isLedger: boolean;
private readonly useAmino: boolean;

constructor(
walletName: WalletName,
Expand All @@ -33,7 +33,7 @@ export class KeplrExtension extends ConnectedWallet {
address: string,
rpc: string,
gasPrice: PlainMessage<Coin>,
isLedger: boolean
useAmino: boolean
) {
super(
walletName,
Expand All @@ -51,7 +51,7 @@ export class KeplrExtension extends ConnectedWallet {
preferNoSetMemo: true,
},
};
this.isLedger = isLedger;
this.useAmino = useAmino;
}

public async signArbitrary(data: string): Promise<SignArbitraryResponse> {
Expand Down Expand Up @@ -83,7 +83,7 @@ export class KeplrExtension extends ConnectedWallet {
timeoutHeight,
};
let txRaw: TxRaw;
if (this.isLedger) {
if (this.useAmino) {
const { signed, signature } = await this.ext.signAmino(
this.chainId,
this.address,
Expand Down
70 changes: 41 additions & 29 deletions src/wallet/wallets/keplr/KeplrWalletConnectV2.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { PlainMessage } from "@bufbuild/protobuf";
import { Secp256k1PubKey, Tx, broadcastTx } from "cosmes/client";
import { base64 } from "cosmes/codec";
import {
RpcClient,
Secp256k1PubKey,
ToSignDocParams,
ToStdSignDocParams,
Tx,
} from "cosmes/client";
import {
CosmosBaseV1beta1Coin as Coin,
CosmosTxV1beta1Fee as Fee,
CosmosTxSigningV1beta1SignMode as SignMode,
CosmosTxV1beta1TxRaw as TxRaw,
} from "cosmes/protobufs";
import { WalletName, WalletType } from "cosmes/wallet";

Expand All @@ -17,17 +22,20 @@ import {

export class KeplrWalletConnectV2 extends ConnectedWallet {
private readonly wc: WalletConnectV2;
private readonly useAmino: boolean;

constructor(
walletName: WalletName,
wc: WalletConnectV2,
chainId: string,
pubKey: Secp256k1PubKey,
address: string,
rpc: string,
gasPrice: PlainMessage<Coin>
gasPrice: PlainMessage<Coin>,
useAmino: boolean
) {
super(
WalletName.KEPLR,
walletName,
WalletType.WALLETCONNECT,
chainId,
pubKey,
Expand All @@ -36,6 +44,7 @@ export class KeplrWalletConnectV2 extends ConnectedWallet {
gasPrice
);
this.wc = wc;
this.useAmino = useAmino;
}

public async signArbitrary(_data: string): Promise<SignArbitraryResponse> {
Expand All @@ -45,7 +54,7 @@ export class KeplrWalletConnectV2 extends ConnectedWallet {
}

public async signAndBroadcastTx(
{ msgs, memo }: UnsignedTx,
{ msgs, memo, timeoutHeight }: UnsignedTx,
fee: Fee,
accountNumber: bigint,
sequence: bigint
Expand All @@ -55,29 +64,32 @@ export class KeplrWalletConnectV2 extends ConnectedWallet {
pubKey: this.pubKey,
msgs: msgs,
});
const { signature, signed } = await this.wc.signAmino(
this.chainId,
this.address,
tx.toStdSignDoc({
accountNumber,
sequence,
fee,
memo,
})
);

const params: ToStdSignDocParams | ToSignDocParams = {
accountNumber,
sequence,
fee,
memo,
timeoutHeight,
};
let txRaw: TxRaw;
if (this.useAmino) {
const { signed, signature } = await this.wc.signAmino(
this.chainId,
this.address,
tx.toStdSignDoc(params)
);
txRaw = tx.toSignedAmino(signed, signature.signature);
} else {
const { signed, signature } = await this.wc.signDirect(
this.chainId,
this.address,
tx.toSignDoc(params)
);
txRaw = tx.toSignedDirect(signed, signature.signature);
}

// Since `sendTx` on WC isn't implemented yet, we have to broadcast manually
return broadcastTx(this.rpc, {
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: base64.decode(signature),
memo: signed.memo,
});
return RpcClient.broadcastTx(this.rpc, txRaw);
}
}
11 changes: 10 additions & 1 deletion src/wallet/wallets/leap/LeapController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,16 @@ export class LeapController extends WalletController {
});
wallets.set(
chainId,
new LeapWalletConnectV2(this.wc, chainId, key, address, rpc, gasPrice)
new LeapWalletConnectV2(
this.id,
this.wc,
chainId,
key,
address,
rpc,
gasPrice,
true // TODO: use sign mode direct when supported
)
);
}
return { wallets, wc: this.wc };
Expand Down
84 changes: 3 additions & 81 deletions src/wallet/wallets/leap/LeapWalletConnectV2.ts
Original file line number Diff line number Diff line change
@@ -1,82 +1,4 @@
import { PlainMessage } from "@bufbuild/protobuf";
import { Secp256k1PubKey, Tx, broadcastTx } from "cosmes/client";
import { base64 } from "cosmes/codec";
import {
CosmosBaseV1beta1Coin as Coin,
CosmosTxV1beta1Fee as Fee,
CosmosTxSigningV1beta1SignMode as SignMode,
} from "cosmes/protobufs";
import { WalletName, WalletType } from "cosmes/wallet";
import { KeplrWalletConnectV2 } from "../keplr/KeplrWalletConnectV2";

import { WalletConnectV2 } from "../../walletconnect/WalletConnectV2";
import {
ConnectedWallet,
SignArbitraryResponse,
UnsignedTx,
} from "../ConnectedWallet";

export class LeapWalletConnectV2 extends ConnectedWallet {
private readonly wc: WalletConnectV2;

constructor(
wc: WalletConnectV2,
chainId: string,
pubKey: Secp256k1PubKey,
address: string,
rpc: string,
gasPrice: PlainMessage<Coin>
) {
super(
WalletName.LEAP,
WalletType.WALLETCONNECT,
chainId,
pubKey,
address,
rpc,
gasPrice
);
this.wc = wc;
}

public async signArbitrary(_data: string): Promise<SignArbitraryResponse> {
// ! Not implemented by Leap
throw new Error("Method not implemented.");
}

public async signAndBroadcastTx(
{ msgs, memo }: UnsignedTx,
fee: Fee,
accountNumber: bigint,
sequence: bigint
): Promise<string> {
const tx = new Tx({
chainId: this.chainId,
pubKey: this.pubKey,
msgs: msgs,
});
const { signature, signed } = await this.wc.signAmino(
this.chainId,
this.address,
tx.toStdSignDoc({
accountNumber,
sequence,
fee,
memo,
})
);
// Since `sendTx` on WC isn't implemented yet, we have to broadcast manually
return broadcastTx(this.rpc, {
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: base64.decode(signature),
memo: signed.memo,
});
}
}
// Leap's API is similar to Keplr.
export const LeapWalletConnectV2 = KeplrWalletConnectV2;

0 comments on commit 4e9f7c8

Please sign in to comment.