diff --git a/src/core/Coin.spec.ts b/src/core/Coin.spec.ts index a9caf8a..04cfb71 100644 --- a/src/core/Coin.spec.ts +++ b/src/core/Coin.spec.ts @@ -147,5 +147,19 @@ describe('Coin', () => { ) expect(coin3).toEqual(coin4) }) + + it('parses denoms containing "-", "." and "_" (e.g. tokenfactory subdenoms)', () => { + const coin1 = new Coin('factory/init1abc/my-token_v1.5', 1001) + const coin2 = Coin.fromString('1001factory/init1abc/my-token_v1.5') + expect(coin1).toEqual(coin2) + + // fromString(toString()) must round-trip for any valid Cosmos denom + expect(Coin.fromString(coin1.toString())).toEqual(coin1) + }) + + it('throws on a numeric-only string (a denom must start with a letter)', () => { + expect(() => Coin.fromString('5000')).toThrow() + expect(() => Coin.fromString('12')).toThrow() + }) }) }) diff --git a/src/core/Coin.ts b/src/core/Coin.ts index 6d02557..6a2ab29 100644 --- a/src/core/Coin.ts +++ b/src/core/Coin.ts @@ -69,7 +69,9 @@ export class Coin extends JSONSerializable { } public static fromString(str: string): Coin { - const m = str.match(/^(-?[0-9]+(\.[0-9]+)?)([0-9a-zA-Z/]+)$/) + const m = str.match( + /^(-?[0-9]+(\.[0-9]+)?)([a-zA-Z][0-9a-zA-Z/:._-]{2,127})$/ + ) if (m === null) { throw new Error(`failed to parse to Coin: ${str}`) }