-
Notifications
You must be signed in to change notification settings - Fork 0
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
5 changed files
with
88 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,42 @@ | ||
import { hexToBytes, bytesToHex } from '@noble/hashes/utils'; | ||
import { getAddress } from 'viem'; | ||
import { bech32m } from 'bech32'; | ||
|
||
const PUSH_PREFIX = 'push'; | ||
|
||
export class Address { | ||
/** | ||
** NOTE | ||
* - bech32m prefix is always in lowercase | ||
* - bech32m address is always in lowercase | ||
*/ | ||
|
||
/** | ||
* Converts an EVM address to a Push (bech32m) address | ||
* @param address EVM address | ||
* @returns Push address | ||
*/ | ||
static evmToPush = (address: `0x${string}`): string => { | ||
try { | ||
const words = bech32m.toWords(hexToBytes(getAddress(address).slice(2))); | ||
return bech32m.encode(PUSH_PREFIX, words); | ||
} catch (e) { | ||
throw new Error('Invalid EVM address'); | ||
} | ||
}; | ||
|
||
/** | ||
* Converts a Push (bech32m) address to an EVM address | ||
* @param address Push address | ||
* @returns EVM address in checksum format | ||
*/ | ||
static pushToEvm = (address: `push${string}`): string => { | ||
try { | ||
const decoded = bech32m.decode(address); | ||
const bytes = new Uint8Array(bech32m.fromWords(decoded.words)); | ||
return getAddress(`0x${bytesToHex(bytes)}`); | ||
} catch (e) { | ||
throw new Error('Invalid Push address'); | ||
} | ||
}; | ||
} |
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,30 @@ | ||
import { Address } from '../../src/lib/address/address'; // Adjust the import path accordingly | ||
|
||
describe('Address', () => { | ||
const evmAddress = '0x35B84d6848D16415177c64D64504663b998A6ab4'; | ||
const pushAddress = 'push1xkuy66zg69jp29muvnty2prx8wvc5645f9y5ux'; | ||
|
||
describe('evmToPush', () => { | ||
it('should convert a valid EVM address to a Push address', () => { | ||
const result = Address.evmToPush(evmAddress); | ||
expect(result).toEqual(pushAddress); | ||
}); | ||
|
||
it('should throw an error for invalid EVM addresses', () => { | ||
const invalidAddress = '0xinvalidaddress'; | ||
expect(() => Address.evmToPush(invalidAddress)).toThrow(); | ||
}); | ||
}); | ||
|
||
describe('pushToEvm', () => { | ||
it('should convert a valid Push address back to an EVM address', () => { | ||
const result = Address.pushToEvm(pushAddress); | ||
expect(result).toEqual(evmAddress); | ||
}); | ||
|
||
it('should throw an error for invalid Push addresses', () => { | ||
const invalidPushAddress = 'pushinvalidaddress'; | ||
expect(() => Address.pushToEvm(invalidPushAddress)).toThrow(); | ||
}); | ||
}); | ||
}); |