Skip to content

Commit f492d68

Browse files
refactor(root): consume bech32 HRP from statics for Cosmos-family coins
Replace hardcoded bech32 HRP literals across 16 Cosmos-family coin modules with reads from `Networks.{main,test}.<coin>.addressPrefix` in @bitgo/statics. statics is now the single source of truth for each chain's HRP. For each module, constants.ts builds address/validator/contract regexes from the imported HRP via template-literal `new RegExp(...)`, and keyPair.ts reads the HRP from statics for bech32 encoding. The standalone `*_ADDRESS_PREFIX` exports are preserved (sourced from statics) and marked `@deprecated` via JSDoc, pointing consumers at `Networks.<main|test>.<coin>.addressPrefix`. This avoids a breaking API change for downstream packages that previously imported these constants. Modules touched: - atom, osmo, sei, tia, baby, bld, initia, zeta, asi, islm, injective, mantra: single HRP (mainnet=testnet) - coreum, hash, cronos, rune: distinct mainnet/testnet HRPs For Cronos POS, the validator HRP `crocncl` (not `valoper`) is kept as a string suffix on the account HRP, preserving the original regex behavior. Ticket: WCN-606
1 parent e481b36 commit f492d68

34 files changed

Lines changed: 180 additions & 104 deletions

File tree

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { CosmosUtils } from '@bitgo/abstract-cosmos';
2+
import { Networks } from '@bitgo/statics';
23

34
const cosmosUtils = new CosmosUtils();
5+
const HRP = Networks.main.asi.addressPrefix;
46
export const validDenoms = ['fet', 'tfet', 'afet', 'atestfet', ...cosmosUtils.getTokenDenomsUsingCoinFamily('asi')];
5-
export const accountAddressRegex = /^(fetch)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38})$/;
6-
export const validatorAddressRegex = /^(fetchvaloper)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38})$/;
7-
export const contractAddressRegex = /^(fetch)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]+)$/;
8-
export const ADDRESS_PREFIX = 'fetch';
7+
export const accountAddressRegex = new RegExp(`^(${HRP})1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38})$`);
8+
export const validatorAddressRegex = new RegExp(`^(${HRP}valoper)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38})$`);
9+
export const contractAddressRegex = new RegExp(`^(${HRP})1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]+)$`);
10+
/** @deprecated Use `Networks.main.asi.addressPrefix` from `@bitgo/statics` instead. */
11+
export const ADDRESS_PREFIX = HRP;
912
export const GAS_AMOUNT = '100000000000000';
1013
export const GAS_LIMIT = 100000;

modules/sdk-coin-asi/src/lib/keyPair.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { KeyPairOptions } from '@bitgo/sdk-core';
22
import { pubkeyToAddress } from '@cosmjs/amino';
33
import { CosmosKeyPair } from '@bitgo/abstract-cosmos';
4-
import { ADDRESS_PREFIX } from './constants';
4+
import { Networks } from '@bitgo/statics';
55

66
/**
77
* Fetch keys and address management.
@@ -19,7 +19,7 @@ export class KeyPair extends CosmosKeyPair {
1919
type: 'tendermint/PubKeySecp256k1',
2020
value: base64String,
2121
},
22-
ADDRESS_PREFIX
22+
Networks.main.asi.addressPrefix
2323
);
2424
}
2525
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { CosmosUtils } from '@bitgo/abstract-cosmos';
2+
import { Networks } from '@bitgo/statics';
23

34
const cosmosUtils = new CosmosUtils();
5+
const HRP = Networks.main.atom.addressPrefix;
46
export const validDenoms = ['natom', 'uatom', 'matom', 'atom', ...cosmosUtils.getTokenDenomsUsingCoinFamily('atom')];
5-
export const accountAddressRegex = /^(cosmos)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38})$/;
6-
export const validatorAddressRegex = /^(cosmosvaloper)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38})$/;
7-
export const contractAddressRegex = /^(cosmos)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]+)$/;
7+
export const accountAddressRegex = new RegExp(`^(${HRP})1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38})$`);
8+
export const validatorAddressRegex = new RegExp(`^(${HRP}valoper)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38})$`);
9+
export const contractAddressRegex = new RegExp(`^(${HRP})1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]+)$`);
810
export const GAS_AMOUNT = '100000';
911
export const GAS_LIMIT = 200000;

modules/sdk-coin-atom/src/lib/keyPair.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { KeyPairOptions } from '@bitgo/sdk-core';
22
import { pubkeyToAddress } from '@cosmjs/amino';
33

44
import { CosmosKeyPair, PubKeyType } from '@bitgo/abstract-cosmos';
5+
import { Networks } from '@bitgo/statics';
56
/**
67
* Cosmos keys and address management.
78
*/
@@ -18,7 +19,7 @@ export class KeyPair extends CosmosKeyPair {
1819
type: PubKeyType.secp256k1,
1920
value: base64String,
2021
},
21-
'cosmos'
22+
Networks.main.atom.addressPrefix
2223
);
2324
}
2425
}

modules/sdk-coin-baby/src/lib/constants.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { CosmosUtils } from '@bitgo/abstract-cosmos';
2+
import { Networks } from '@bitgo/statics';
23

34
const cosmosUtils = new CosmosUtils();
5+
const HRP = Networks.main.baby.addressPrefix;
46
export const validDenoms = ['baby', 'tbaby', 'ubbn', ...cosmosUtils.getTokenDenomsUsingCoinFamily('baby')];
5-
export const accountAddressRegex = /^(bbn)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38})$/;
6-
export const validatorAddressRegex = /^(bbnvaloper)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38})$/;
7-
export const contractAddressRegex = /^(bbn)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]+)$/;
8-
export const ADDRESS_PREFIX = 'bbn';
7+
export const accountAddressRegex = new RegExp(`^(${HRP})1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38})$`);
8+
export const validatorAddressRegex = new RegExp(`^(${HRP}valoper)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38})$`);
9+
export const contractAddressRegex = new RegExp(`^(${HRP})1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]+)$`);
10+
/** @deprecated Use `Networks.main.baby.addressPrefix` from `@bitgo/statics` instead. */
11+
export const ADDRESS_PREFIX = HRP;
912
export const GAS_AMOUNT = '7000';
1013
export const GAS_LIMIT = 200000;
1114
export const UNAVAILABLE_TEXT = 'UNAVAILABLE';

modules/sdk-coin-baby/src/lib/keyPair.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { KeyPairOptions } from '@bitgo/sdk-core';
22
import { pubkeyToAddress } from '@cosmjs/amino';
33

44
import { CosmosKeyPair } from '@bitgo/abstract-cosmos';
5-
import { ADDRESS_PREFIX } from './constants';
5+
import { Networks } from '@bitgo/statics';
66

77
/**
88
* Babylon keys and address management.
@@ -20,7 +20,7 @@ export class KeyPair extends CosmosKeyPair {
2020
type: 'tendermint/PubKeySecp256k1',
2121
value: base64String,
2222
},
23-
ADDRESS_PREFIX
23+
Networks.main.baby.addressPrefix
2424
);
2525
}
2626
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { CosmosUtils } from '@bitgo/abstract-cosmos';
2+
import { Networks } from '@bitgo/statics';
23

34
const cosmosUtils = new CosmosUtils();
5+
const HRP = Networks.main.bld.addressPrefix;
46
export const validDenoms = ['nbld', 'ubld', 'mbld', 'bld', ...cosmosUtils.getTokenDenomsUsingCoinFamily('bld')];
5-
export const accountAddressRegex = /^(agoric)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38})$/;
6-
export const validatorAddressRegex = /^(agoricvaloper)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38})$/;
7-
export const contractAddressRegex = /^(agoric)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]+)$/;
7+
export const accountAddressRegex = new RegExp(`^(${HRP})1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38})$`);
8+
export const validatorAddressRegex = new RegExp(`^(${HRP}valoper)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38})$`);
9+
export const contractAddressRegex = new RegExp(`^(${HRP})1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]+)$`);

modules/sdk-coin-bld/src/lib/keyPair.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { KeyPairOptions } from '@bitgo/sdk-core';
22
import { pubkeyToAddress } from '@cosmjs/amino';
33

44
import { CosmosKeyPair } from '@bitgo/abstract-cosmos';
5+
import { Networks } from '@bitgo/statics';
56

67
/**
78
* Agoric keys and address management.
@@ -19,7 +20,7 @@ export class KeyPair extends CosmosKeyPair {
1920
type: 'tendermint/PubKeySecp256k1',
2021
value: base64String,
2122
},
22-
'agoric'
23+
Networks.main.bld.addressPrefix
2324
);
2425
}
2526
}
Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
import { CosmosUtils } from '@bitgo/abstract-cosmos';
2+
import { Networks } from '@bitgo/statics';
23

34
const cosmosUtils = new CosmosUtils();
5+
const MAINNET_HRP = Networks.main.coreum.addressPrefix;
6+
const TESTNET_HRP = Networks.test.coreum.addressPrefix;
47
export const validDenoms = ['ucore', 'utestcore', ...cosmosUtils.getTokenDenomsUsingCoinFamily('coreum')];
5-
export const mainnetAccountAddressRegex =
6-
/^(core)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38}|['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{58})$/;
7-
export const mainnetValidatorAddressRegex = /^(corevaloper)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38})$/;
8-
export const MAINNET_ADDRESS_PREFIX = 'core';
9-
export const testnetAccountAddressRegex =
10-
/^(testcore)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38}|['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{58})$/;
11-
export const testnetValidatorAddressRegex = /^(testcorevaloper)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38})$/;
12-
export const TESTNET_ADDRESS_PREFIX = 'testcore';
8+
export const mainnetAccountAddressRegex = new RegExp(
9+
`^(${MAINNET_HRP})1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38}|['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{58})$`
10+
);
11+
export const mainnetValidatorAddressRegex = new RegExp(
12+
`^(${MAINNET_HRP}valoper)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38})$`
13+
);
14+
export const testnetAccountAddressRegex = new RegExp(
15+
`^(${TESTNET_HRP})1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38}|['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{58})$`
16+
);
17+
export const testnetValidatorAddressRegex = new RegExp(
18+
`^(${TESTNET_HRP}valoper)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38})$`
19+
);
20+
/** @deprecated Use `Networks.main.coreum.addressPrefix` from `@bitgo/statics` instead. */
21+
export const MAINNET_ADDRESS_PREFIX = MAINNET_HRP;
22+
/** @deprecated Use `Networks.test.coreum.addressPrefix` from `@bitgo/statics` instead. */
23+
export const TESTNET_ADDRESS_PREFIX = TESTNET_HRP;
1324

1425
export const GAS_LIMIT = 200000;
1526
export const GAS_AMOUNT = '6250';

modules/sdk-coin-coreum/src/lib/keyPair.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { KeyPairOptions, AddressFormat } from '@bitgo/sdk-core';
22
import { pubkeyToAddress } from '@cosmjs/amino';
33

44
import { CosmosKeyPair } from '@bitgo/abstract-cosmos';
5-
import { MAINNET_ADDRESS_PREFIX, TESTNET_ADDRESS_PREFIX } from './constants';
5+
import { Networks } from '@bitgo/statics';
66

77
/**
88
* Coreum keys and address management.
@@ -15,7 +15,8 @@ export class KeyPair extends CosmosKeyPair {
1515
/** @inheritdoc */
1616
getAddress(format: AddressFormat = AddressFormat.mainnet): string {
1717
const base64String = Buffer.from(this.getKeys().pub.slice(0, 66), 'hex').toString('base64');
18-
const address_prefix = format === AddressFormat.testnet ? TESTNET_ADDRESS_PREFIX : MAINNET_ADDRESS_PREFIX;
18+
const address_prefix =
19+
format === AddressFormat.testnet ? Networks.test.coreum.addressPrefix : Networks.main.coreum.addressPrefix;
1920
return pubkeyToAddress(
2021
{
2122
type: 'tendermint/PubKeySecp256k1',

0 commit comments

Comments
 (0)