Skip to content

Commit

Permalink
Update createAddressId to match current behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanwhit authored and atodorov committed Nov 22, 2022
1 parent 2c95633 commit 537d1cd
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions creditcoin-js/src/extrinsics/register-address.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiPromise, SubmittableResult } from '@polkadot/api';
import { Address, AddressId, Blockchain, EventReturnJoinType } from '../model';
import { Address, AddressId, Blockchain, CHAINS, EventReturnJoinType } from '../model';
import { KeyringPair } from '@polkadot/keyring/types';
import { handleTransaction, processEvents } from './common';
import { TxCallback, TxFailureCallback } from '../types';
Expand All @@ -9,8 +9,29 @@ import { blake2AsHex } from '@polkadot/util-crypto';

export type AddressRegistered = EventReturnJoinType<AddressId, Address>;

const assertUnreachable = (_x: never): never => {
throw new Error("Didn't expect to get here");
};

const blockchainToString = (blockchain: Blockchain): string => {
switch (blockchain) {
case CHAINS.ethereum:
return 'ethereum';
case CHAINS.rinkeby:
return 'rinkeby';
case CHAINS.luniverse:
return 'luniverse';
}
switch (blockchain.platform) {
case 'Evm':
return `evm-${blockchain.chainId.toString()}`;
default:
return assertUnreachable(blockchain.platform);
}
};

export const createAddressId = (blockchain: Blockchain, externalAddress: string) => {
const blockchainBytes = Buffer.from(blockchain.toString().toLowerCase());
const blockchainBytes = Buffer.from(blockchainToString(blockchain));
const key = u8aConcat(blockchainBytes, u8aToU8a(externalAddress));
return blake2AsHex(key);
};
Expand Down

0 comments on commit 537d1cd

Please sign in to comment.