-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
119 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { bech32 } from 'bech32'; | ||
import { bech32IsValid } from './validate'; | ||
import { describe, expect, test } from 'vitest'; | ||
|
||
describe('validate Bech32 address', () => { | ||
test('should return true for a correct Bech32 address without prefix', () => { | ||
const address = bech32.encode('osmo', bech32.toWords(Buffer.from('test', 'utf8'))); | ||
expect(bech32IsValid(address)).toBeTruthy(); | ||
}); | ||
|
||
test('should return true for a correct Bech32 address with prefix', () => { | ||
const prefix = 'osmo'; | ||
const address = bech32.encode(prefix, bech32.toWords(Buffer.from('test', 'utf8'))); | ||
expect(bech32IsValid(address, prefix)).toBeTruthy(); | ||
}); | ||
|
||
test('should return false if the prefix does not match', () => { | ||
const address = bech32.encode('osmo', bech32.toWords(Buffer.from('test', 'utf8'))); | ||
const wrongPrefix = 'noble'; | ||
expect(bech32IsValid(address, wrongPrefix)).toBeFalsy(); | ||
}); | ||
|
||
test('should return false for invalid Bech32 address', () => { | ||
const address = 'invalidaddress'; | ||
expect(bech32IsValid(address)).toBeFalsy(); | ||
}); | ||
|
||
test('should return false for Bech32 address with unexpected prefix when prefix is provided', () => { | ||
const prefix = 'osmo'; | ||
const address = bech32.encode('noble', bech32.toWords(Buffer.from('test', 'utf8'))); | ||
expect(bech32IsValid(address, prefix)).toBeFalsy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { bech32 } from 'bech32'; | ||
|
||
/** | ||
* Validates a Bech32 encoded address. If a prefix is provided, it also checks that the address's | ||
* prefix matches the expected prefix. | ||
*/ | ||
export const bech32IsValid = (bech32Address: string, prefix?: string): boolean => { | ||
try { | ||
const { prefix: decodedPrefix } = bech32.decode(bech32Address); | ||
return prefix ? prefix === decodedPrefix : true; | ||
} catch (error) { | ||
// If there's an error in decoding, it means the address is not valid Bech32 | ||
return false; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,32 @@ | ||
// Canonical data source: https://github.com/cosmos/chain-registry/tree/master | ||
import { AssetId } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/asset/v1/asset_pb'; | ||
|
||
export interface Chain { | ||
displayName: string; | ||
chainId: string; | ||
ibcChannel: string; | ||
iconUrl: string; | ||
nativeAssets: AssetId[]; | ||
addressPrefix: string; | ||
} | ||
|
||
// Canonical data source: https://github.com/cosmos/chain-registry/tree/master | ||
export const testnetIbcChains: Chain[] = [ | ||
{ | ||
displayName: 'Osmosis', | ||
chainId: 'osmo-test-5', | ||
ibcChannel: 'channel-0', | ||
iconUrl: | ||
'https://raw.githubusercontent.com/cosmos/chain-registry/f1348793beb994c6cc0256ed7ebdb48c7aa70003/osmosis/images/osmo.svg', | ||
nativeAssets: [], | ||
addressPrefix: 'osmo', | ||
}, | ||
{ | ||
displayName: 'Noble', | ||
chainId: 'grand-1', | ||
ibcChannel: 'channel-2', | ||
iconUrl: | ||
'https://raw.githubusercontent.com/cosmos/chain-registry/2ca39d0e4eaf3431cca13991948e099801f02e46/noble/images/stake.svg', | ||
nativeAssets: [], | ||
addressPrefix: 'noble', | ||
}, | ||
]; |