Skip to content

Commit

Permalink
add: push Address conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Aman035 committed Sep 12, 2024
1 parent 723f733 commit a324dce
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 6 deletions.
18 changes: 13 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"@bufbuild/protobuf": "^2.0.0",
"@noble/hashes": "^1.5.0",
"axios": "^1.7.7",
"bech32": "^2.0.0",
"tslib": "^2.3.0",
"uuid": "^10.0.0",
"viem": "^2.21.1"
Expand Down
42 changes: 42 additions & 0 deletions packages/core/src/lib/address/address.ts
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');
}
};
}
3 changes: 2 additions & 1 deletion packages/core/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { PushNetwork } from './pushNetwork';
import { Block } from './block/block';
import { Tx } from './tx/tx';
import { Validator } from './validator/validator';
import { Address } from './address/address';
import { CONSTANTS } from './constants';

/**
* FOR ADVANCE USECASES
* serialization / deserialization capabilities for block & tx
* Validator calls
*/
export { CONSTANTS, Block, Tx, Validator };
export { CONSTANTS, Block, Tx, Validator, Address };

export default PushNetwork;
30 changes: 30 additions & 0 deletions packages/core/tests/address/address.test.ts
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();
});
});
});

0 comments on commit a324dce

Please sign in to comment.