Skip to content

Commit

Permalink
refactor(multichain): use KnownCaipNamespace + getMultichainTypeFromA…
Browse files Browse the repository at this point in the history
…ddress -> getCaipNamespaceFromAddress
  • Loading branch information
ccharly committed Oct 9, 2024
1 parent 4622980 commit a266559
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@
"@metamask/snaps-utils": "^8.1.1",
"@metamask/transaction-controller": "^37.2.0",
"@metamask/user-operation-controller": "^13.0.0",
"@metamask/utils": "^9.1.0",
"@metamask/utils": "^9.3.0",
"@ngraveio/bc-ur": "^1.1.12",
"@noble/hashes": "^1.3.3",
"@popperjs/core": "^2.4.0",
Expand Down
8 changes: 4 additions & 4 deletions shared/lib/multichain.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
MultichainType,
getMultichainTypeFromAddress,
getCaipNamespaceFromAddress,
isBtcMainnetAddress,
isBtcTestnetAddress,
} from './multichain';
Expand Down Expand Up @@ -66,7 +66,7 @@ describe('multichain', () => {
it.each([...BTC_MAINNET_ADDRESSES, ...BTC_TESTNET_ADDRESSES])(
'returns ChainType.Bitcoin for bitcoin address: %s',
(address: string) => {
expect(getMultichainTypeFromAddress(address)).toBe(
expect(getCaipNamespaceFromAddress(address)).toBe(
MultichainType.Bip122,
);
},
Expand All @@ -76,7 +76,7 @@ describe('multichain', () => {
it.each(ETH_ADDRESSES)(
'returns ChainType.Ethereum for ethereum address: %s',
(address: string) => {
expect(getMultichainTypeFromAddress(address)).toBe(
expect(getCaipNamespaceFromAddress(address)).toBe(
MultichainType.Eip155,
);
},
Expand All @@ -86,7 +86,7 @@ describe('multichain', () => {
it.each(SOL_ADDRESSES)(
'returns ChainType.Ethereum for non-supported address: %s',
(address: string) => {
expect(getMultichainTypeFromAddress(address)).toBe(
expect(getCaipNamespaceFromAddress(address)).toBe(
MultichainType.Eip155,
);
},
Expand Down
17 changes: 4 additions & 13 deletions shared/lib/multichain.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import { CaipNamespace, KnownCaipNamespace } from '@metamask/utils';
import { validate, Network } from 'bitcoin-address-validation';

/**
* Multi-chain family type.
*/
export enum MultichainType {
// Bitcoin-like:
Bip122 = 'bip122',
// Ethereum:
Eip155 = 'eip155',
}

/**
* Returns whether an address is on the Bitcoin mainnet.
*
Expand Down Expand Up @@ -43,10 +34,10 @@ export function isBtcTestnetAddress(address: string): boolean {
* @param address - The address to check.
* @returns The chain's type for that address.
*/
export function getMultichainTypeFromAddress(address: string): MultichainType {
export function getCaipNamespaceFromAddress(address: string): CaipNamespace {
if (isBtcMainnetAddress(address) || isBtcTestnetAddress(address)) {
return MultichainType.Bip122;
return KnownCaipNamespace.Bip122;
}
// Defaults to "Ethereum" for all other cases for now.
return MultichainType.Eip155;
return KnownCaipNamespace.Eip155;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import type { PolymorphicRef } from '../box';

import { AvatarBase, AvatarBaseProps } from '../avatar-base';
import {
MultichainType,
getMultichainTypeFromAddress,
getCaipNamespaceFromAddress,
} from '../../../../shared/lib/multichain';
import {
AvatarAccountDiameter,
Expand All @@ -17,12 +16,15 @@ import {
AvatarAccountComponent,
AvatarAccountProps,
} from './avatar-account.types';
import { KnownCaipNamespace } from '@metamask/utils';

function getJazziconNamespace(address: string): string | undefined {
switch (getMultichainTypeFromAddress(address)) {
case MultichainType.Bip122:
return 'bip122';
case MultichainType.Eip155:
const namespace = getCaipNamespaceFromAddress(address);

switch (namespace) {
case KnownCaipNamespace.Bip122:
return namespace;
case KnownCaipNamespace.Eip155:
return undefined; // Falls back to default Jazzicon behavior
default:
return undefined;
Expand Down
19 changes: 18 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6559,6 +6559,23 @@ __metadata:
languageName: node
linkType: hard

"@metamask/utils@npm:^9.3.0":
version: 9.3.0
resolution: "@metamask/utils@npm:9.3.0"
dependencies:
"@ethereumjs/tx": "npm:^4.2.0"
"@metamask/superstruct": "npm:^3.1.0"
"@noble/hashes": "npm:^1.3.1"
"@scure/base": "npm:^1.1.3"
"@types/debug": "npm:^4.1.7"
debug: "npm:^4.3.4"
pony-cause: "npm:^2.1.10"
semver: "npm:^7.5.4"
uuid: "npm:^9.0.1"
checksum: 10/ed6648cd973bbf3b4eb0e862903b795a99d27784c820e19f62f0bc0ddf353e98c2858d7e9aaebc0249a586391b344e35b9249d13c08e3ea0c74b23dc1c6b1558
languageName: node
linkType: hard

"@mobily/ts-belt@npm:^3.13.1":
version: 3.13.1
resolution: "@mobily/ts-belt@npm:3.13.1"
Expand Down Expand Up @@ -26130,7 +26147,7 @@ __metadata:
"@metamask/test-dapp": "npm:^8.4.0"
"@metamask/transaction-controller": "npm:^37.2.0"
"@metamask/user-operation-controller": "npm:^13.0.0"
"@metamask/utils": "npm:^9.1.0"
"@metamask/utils": "npm:^9.3.0"
"@ngraveio/bc-ur": "npm:^1.1.12"
"@noble/hashes": "npm:^1.3.3"
"@octokit/core": "npm:^3.6.0"
Expand Down

0 comments on commit a266559

Please sign in to comment.