Skip to content

Commit

Permalink
add: pushCAIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Aman035 committed Sep 13, 2024
1 parent a77ee7f commit 42bcfda
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
30 changes: 30 additions & 0 deletions packages/core/src/lib/address/address.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { hexToBytes, bytesToHex } from '@noble/hashes/utils';
import { getAddress } from 'viem';
import { bech32m } from 'bech32';
import { ENV } from '../constants';
import { PUSH_NETWORK } from './address.types';

const PUSH_PREFIX = 'push';

Expand Down Expand Up @@ -39,4 +41,32 @@ export class Address {
throw new Error('Invalid Push address');
}
};

/**
* Converts an EVM address in Push CAIP format
* @param address
* @param env
*/
static toPushCAIP = (address: `0x${string}`, env: ENV = ENV.STAGING) => {
let network: PUSH_NETWORK;
switch (env) {
case ENV.LOCAL:
case ENV.DEV: {
network = PUSH_NETWORK.DEVNET;
break;
}
case ENV.STAGING: {
network = PUSH_NETWORK.TESTNET;
break;
}
case ENV.PROD: {
network = PUSH_NETWORK.MAINNET;
break;
}
default: {
throw Error('Invalid ENV');
}
}
return `push:${network}:${address}`;
};
}
5 changes: 5 additions & 0 deletions packages/core/src/lib/address/address.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum PUSH_NETWORK {
DEVNET = 'devnet',
TESTNET = 'testnet',
MAINNET = 'mainnet',
}
18 changes: 18 additions & 0 deletions packages/core/tests/address/address.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Address } from '../../src/lib/address/address'; // Adjust the import path accordingly
import { ENV } from '../../src/lib/constants';

describe('Address', () => {
const evmAddress = '0x35B84d6848D16415177c64D64504663b998A6ab4';
Expand Down Expand Up @@ -27,4 +28,21 @@ describe('Address', () => {
expect(() => Address.pushToEvm(invalidPushAddress)).toThrow();
});
});

describe('toPushCAIP', () => {
it('should convert evm address to Push Devnet CAIP', () => {
const result1 = Address.toPushCAIP(evmAddress, ENV.LOCAL);
const result2 = Address.toPushCAIP(evmAddress, ENV.DEV);
expect(result1).toEqual(result2);
expect(result1).toEqual(`push:devnet:${evmAddress}`);
});
it('should convert evm address to Push Testnet CAIP', () => {
const result = Address.toPushCAIP(evmAddress, ENV.STAGING);
expect(result).toEqual(`push:testnet:${evmAddress}`);
});
it('should convert evm address to Push Mainnet CAIP', () => {
const result = Address.toPushCAIP(evmAddress, ENV.PROD);
expect(result).toEqual(`push:mainnet:${evmAddress}`);
});
});
});

0 comments on commit 42bcfda

Please sign in to comment.