Skip to content

Commit

Permalink
fix: bring back the denom length validation out of regex
Browse files Browse the repository at this point in the history
  • Loading branch information
madrezaz committed Dec 18, 2023
1 parent 12bc9e7 commit db1cf5f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
10 changes: 5 additions & 5 deletions packages/stargate/src/fee.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ describe("GasPrice", () => {
expect(() => GasPrice.fromString("234")).toThrowError(/Invalid gas price string/i);
expect(() => GasPrice.fromString("-234tkn")).toThrowError(/Invalid gas price string/i);
// Checks details of <denom>
expect(() => GasPrice.fromString("234t")).toThrowError(/Invalid gas price string/i);
expect(() => GasPrice.fromString("234tt")).toThrowError(/Invalid gas price string/i);
expect(() => GasPrice.fromString("234t")).toThrowError(/denom must be between 3 and 128 characters/i);
expect(() => GasPrice.fromString("234tt")).toThrowError(/denom must be between 3 and 128 characters/i);
expect(() =>
GasPrice.fromString(
"234ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt",
),
).toThrowError(/Invalid gas price string/i);
).toThrowError(/denom must be between 3 and 128 characters/i);
// Checks details of <amount>
expect(() => GasPrice.fromString("3.utkn")).toThrowError(/Invalid gas price string/i);
expect(() => GasPrice.fromString("..utkn")).toThrowError(/Invalid gas price string/i);
expect(() => GasPrice.fromString("3.utkn")).toThrowError(/Fractional part missing/i);
expect(() => GasPrice.fromString("..utkn")).toThrowError(/More than one separator found/i);
});
});

Expand Down
15 changes: 14 additions & 1 deletion packages/stargate/src/fee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ import { StdFee } from "@cosmjs/amino";
import { Decimal, Uint53 } from "@cosmjs/math";
import { coins } from "@cosmjs/proto-signing";

/**
* Denom checker for the Cosmos SDK 0.42 denom pattern
* (https://github.com/cosmos/cosmos-sdk/blob/v0.42.4/types/coin.go#L599-L601).
*
* This is like a regexp but with helpful error messages.
*/
function checkDenom(denom: string): void {
if (denom.length < 3 || denom.length > 128) {
throw new Error("Denom must be between 3 and 128 characters");
}
}

/**
* A gas price, i.e. the price of a single unit of gas. This is typically a fraction of
* the smallest fee token unit, such as 0.012utoken.
Expand All @@ -25,11 +37,12 @@ export class GasPrice {
*/
public static fromString(gasPrice: string): GasPrice {
// Use Decimal.fromUserInput and checkDenom for detailed checks and helpful error messages
const matchResult = gasPrice.match(/^(\d+(?:\.\d+)?|\.\d+)([a-zA-Z][a-zA-Z0-9/:._-]{2,127})$/i);
const matchResult = gasPrice.match(/^([0-9.]+)([a-zA-Z][a-zA-Z0-9/:._-]*)$/i);
if (!matchResult) {
throw new Error("Invalid gas price string");
}
const [_, amount, denom] = matchResult;
checkDenom(denom);
const fractionalDigits = 18;
const decimalAmount = Decimal.fromUserInput(amount, fractionalDigits);
return new GasPrice(decimalAmount, denom);
Expand Down

0 comments on commit db1cf5f

Please sign in to comment.