diff --git a/src/async.test.ts b/src/async.test.ts index 395a054e..af315cb8 100644 --- a/src/async.test.ts +++ b/src/async.test.ts @@ -27,6 +27,7 @@ test("evm coin name", async () => { expect(coder.coinType).toBe(2147483658); expect(coder.name).toBe("op"); expect(coder.evmChainId).toBe(10); + expect("isUnknownChain" in coder).toBeFalse(); }); test("evm coin type", async () => { @@ -34,6 +35,15 @@ test("evm coin type", async () => { expect(coder.coinType).toBe(2147483658); expect(coder.name).toBe("op"); expect(coder.evmChainId).toBe(10); + expect(coder.isUnknownChain).toBeFalse(); +}); + +test("unknown evm coin type", async () => { + const coder = await getCoderByCoinTypeAsync(2147483659); + expect(coder.coinType).toBe(2147483659); + expect(coder.name).toBe("Unknown Chain (11)"); + expect(coder.evmChainId).toBe(11); + expect(coder.isUnknownChain).toBeTrue(); }); const nonEvmCoinNames = Object.keys(nonEvmCoinNameToTypeMap); diff --git a/src/async.ts b/src/async.ts index 36825468..b1000d57 100644 --- a/src/async.ts +++ b/src/async.ts @@ -46,24 +46,24 @@ export const getCoderByCoinTypeAsync = async < const names = coinTypeToNameMap[String(coinType) as keyof typeof coinTypeToNameMap]; - if (!names) throw new Error(`Unsupported coin type: ${coinType}`); - - const [name] = names; - if (coinType >= SLIP44_MSB) { // EVM coin const evmChainId = coinTypeToEvmChainId(coinType); + const isUnknownChain = !names; + const name = isUnknownChain ? `Unknown Chain (${evmChainId})` : names[0]; return { name, coinType: coinType as EvmCoinType, evmChainId, + isUnknownChain, encode: eth.encode, decode: eth.decode, } as GetCoderByCoinType; } - const mod = await import(`./coin/${name}`); + if (!names) throw new Error(`Unsupported coin type: ${coinType}`); + const [name] = names; + const mod = await import(`./coin/${name}`); if (!mod) throw new Error(`Failed to load coin: ${name}`); - return mod[name]; }; diff --git a/src/index.test.ts b/src/index.test.ts index af086f3a..33eb731a 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -31,6 +31,7 @@ test("evm coin name", () => { expect(coder.coinType).toBe(2147483658); expect(coder.name).toBe("op"); expect(coder.evmChainId).toBe(10); + expect("isUnknownChain" in coder).toBeFalse(); expect(coder.encode).toBeFunction(); expect(coder.decode).toBeFunction(); }); @@ -40,6 +41,7 @@ test("evm coin type", () => { expect(coder.coinType).toBe(2147483658); expect(coder.name).toBe("op"); expect(coder.evmChainId).toBe(10); + expect(coder.isUnknownChain).toBeFalse(); expect(coder.encode).toBeFunction(); expect(coder.decode).toBeFunction(); }); diff --git a/src/index.ts b/src/index.ts index 1f2bb8ab..07b51a65 100644 --- a/src/index.ts +++ b/src/index.ts @@ -73,6 +73,7 @@ export const getCoderByCoinType = < const names = coinTypeToNameMap[String(coinType) as keyof typeof coinTypeToNameMap]; // https://docs.ens.domains/ens-improvement-proposals/ensip-11-evmchain-address-resolution + if (coinType >= SLIP44_MSB) { // EVM coin const evmChainId = coinTypeToEvmChainId(coinType); @@ -87,12 +88,12 @@ export const getCoderByCoinType = < encode: ethFormat.encode, decode: ethFormat.decode, } as GetCoderByCoinType; - } else { - if (!names) { - throw new Error(`Unsupported coin type: ${coinType}`); - } - const [name] = names; - const format = formats[name as keyof typeof formats]; - return format as GetCoderByCoinType; } + + if (!names) { + throw new Error(`Unsupported coin type: ${coinType}`); + } + const [name] = names; + const format = formats[name as keyof typeof formats]; + return format as GetCoderByCoinType; }; diff --git a/src/types.ts b/src/types.ts index 3f68f198..0b36d7c8 100644 --- a/src/types.ts +++ b/src/types.ts @@ -26,7 +26,9 @@ export type CoinTypeToFormatMap = { : never; }; export type CoinNameToFormatMap = { - [key in CoinName]: CoinTypeToFormatMap[CoinNameToTypeMap[key]]; + [key in CoinName]: Prettify< + Omit + >; }; export type EvmCoinMap = typeof evmCoinNameToTypeMap;