Skip to content

Commit

Permalink
Merge pull request #55 from leapwallet/main
Browse files Browse the repository at this point in the history
Release provider 0.1.21
  • Loading branch information
leapsamvel authored Sep 14, 2023
2 parents e2c502a + 7108c0c commit e67540f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 29 deletions.
2 changes: 1 addition & 1 deletion packages/cosmos-snap-provider/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@leapwallet/cosmos-snap-provider",
"version": "0.1.20",
"version": "0.1.21",
"repository": {
"url": "[email protected]:leapwallet/cosmos-metamask-snap.git"
},
Expand Down
27 changes: 2 additions & 25 deletions packages/cosmos-snap-provider/src/cosmjs-offline-signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
import { SignDoc } from 'cosmjs-types/cosmos/tx/v1beta1/tx';
import { AccountData, AminoSignResponse } from '@cosmjs/amino';
import { DirectSignResponse, OfflineDirectSigner } from '@cosmjs/proto-signing';
import BigNumber from 'bignumber.js';
import { getKey, requestSignAmino, requestSignature } from './snap';
import { getGasPriceForChainName } from './helper/gas';
import { SignAminoOptions, StdSignDoc } from './types';
import Chains from './constants/chainInfo';

export class CosmjsOfflineSigner implements OfflineDirectSigner {
readonly chainId: string;
Expand Down Expand Up @@ -63,32 +60,11 @@ export class CosmjsOfflineSigner implements OfflineDirectSigner {
throw new Error('Signer address does not match wallet address');
}

const chain = Chains[this.chainId as keyof typeof Chains];
// Override gasPrice
if (!options?.preferNoSetFee && chain && chain.denom) {
const gasPriceFromRegistry = await getGasPriceForChainName(
chain.chainName,
);
const gas: any =
'gasLimit' in signDoc.fee ? signDoc.fee.gasLimit : signDoc.fee.gas;
if (gasPriceFromRegistry) {
const amount = [
{
amount: new BigNumber(gasPriceFromRegistry)
.multipliedBy(new BigNumber(gas))
.decimalPlaces(0, 1)
.toString(),
denom: chain.denom,
},
];
signDoc.fee.amount = amount;
}
}

return requestSignAmino(
this.chainId,
signerAddress,
signDoc,
options
) as unknown as Promise<AminoSignResponse>;
}
}
Expand Down Expand Up @@ -119,6 +95,7 @@ export async function signArbitrary(
const { signDoc } = getADR36SignDoc(signer, data);
const result = await requestSignAmino(chainId, signer, signDoc, {
isADR36: true,
preferNoSetFee: true
});
return result.signature;
}
Expand Down
40 changes: 37 additions & 3 deletions packages/cosmos-snap-provider/src/snap.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/* eslint @typescript-eslint/prefer-optional-chain: 0 */ // --> OFF
/* eslint jsdoc/match-description: 0 */ // --> OFF
import { AccountData, StdSignDoc, AminoSignResponse } from '@cosmjs/amino';
import { AccountData, AminoSignResponse } from '@cosmjs/amino';
import BigNumber from 'bignumber.js';
import Long from 'long';
import { defaultSnapOrigin } from './config';
import { ChainInfo, GetSnapsResponse, Snap } from './types';
import Chains from './constants/chainInfo';
import { getGasPriceForChainName } from './helper/gas';
import { ChainInfo, GetSnapsResponse, SignAminoOptions, Snap, StdSignDoc } from './types';

/**
* The fool proof version of getting the ethereum provider suggested by
Expand Down Expand Up @@ -160,8 +163,39 @@ export const requestSignAmino = async (
chainId: string,
signerAddress: string,
signDoc: StdSignDoc,
{ isADR36 = false } = {},
options?: SignAminoOptions,
) => {

const {
isADR36 = false
} = options || {};

if (chainId !== signDoc.chain_id) {
throw new Error('Chain ID does not match signer chain ID');
}

const chain = Chains[chainId as keyof typeof Chains];
// Override gasPrice
if (!options?.preferNoSetFee && chain && chain.denom) {
const gasPriceFromRegistry = await getGasPriceForChainName(
chain.chainName,
);
const gas: any =
'gasLimit' in signDoc.fee ? signDoc.fee.gasLimit : signDoc.fee.gas;
if (gasPriceFromRegistry) {
const amount = [
{
amount: new BigNumber(gasPriceFromRegistry)
.multipliedBy(new BigNumber(gas))
.decimalPlaces(0, 1)
.toString(),
denom: chain.denom,
},
];
signDoc.fee.amount = amount;
}
}

const signResponse = (await sendReqToSnap('signAmino', {
chainId,
signerAddress,
Expand Down
1 change: 1 addition & 0 deletions packages/cosmos-snap-provider/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export type ChainInfo = {

export type SignAminoOptions = {
preferNoSetFee?: boolean;
isADR36?: boolean;
};

export type StdFee = {
Expand Down

0 comments on commit e67540f

Please sign in to comment.