From 93ba43bf940f2a3e6e5e1542041982766ef730d6 Mon Sep 17 00:00:00 2001 From: Harsh R <53080940+fullstackninja864@users.noreply.github.com> Date: Thu, 20 Jul 2023 14:50:21 +0530 Subject: [PATCH] feature(walletkit-core): added method to get address type from address and network as input (#153) * feature(walletkit-core): added method to get address type from address and network as input * added testcase for Address type * invalid address check --- packages/walletkit-core/src/api/address.ts | 24 +++++++++++- .../walletkit-core/src/api/address.unit.ts | 39 ++++++++++++++++++- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/packages/walletkit-core/src/api/address.ts b/packages/walletkit-core/src/api/address.ts index 7dd62ea..6196cc4 100644 --- a/packages/walletkit-core/src/api/address.ts +++ b/packages/walletkit-core/src/api/address.ts @@ -1,4 +1,4 @@ -import { fromScript } from "@defichain/jellyfish-address"; +import { fromAddress, fromScript } from "@defichain/jellyfish-address"; import { NetworkName } from "@defichain/jellyfish-network"; import { OP_PUSHDATA, Script } from "@defichain/jellyfish-transaction"; import { ethers } from "ethers"; @@ -80,3 +80,25 @@ export function getDecodedAddress( return undefined; } } + +export function getAddressType( + address: string, + network: NetworkName +): AddressType | undefined { + try { + // check if is dfc address first + const decodedAddress = fromAddress(address, network); + if (decodedAddress !== undefined) { + return decodedAddress.type; + } + + // check if the address is evm + const isEVMAddress = ethers.utils.isAddress(address); + if (isEVMAddress) { + return AddressType.ETH; + } + return undefined; + } catch (e) { + return undefined; + } +} diff --git a/packages/walletkit-core/src/api/address.unit.ts b/packages/walletkit-core/src/api/address.unit.ts index 0132916..e64ddb0 100644 --- a/packages/walletkit-core/src/api/address.unit.ts +++ b/packages/walletkit-core/src/api/address.unit.ts @@ -1,6 +1,11 @@ import { OP_CODES, Script } from "@defichain/jellyfish-transaction"; -import { AddressType, EthDecodedAddress, getDecodedAddress } from "./address"; +import { + AddressType, + EthDecodedAddress, + getAddressType, + getDecodedAddress, +} from "./address"; /* detailed `fromScript()` test cases are done in @defichain/jellyfish-address */ describe("Address Decoder", () => { @@ -20,7 +25,6 @@ describe("Address Decoder", () => { script, network: "testnet", }; - expect(getDecodedAddress(script, "testnet")).toStrictEqual(expected); }); @@ -76,3 +80,34 @@ describe("Address Decoder", () => { expect(getDecodedAddress(script, "testnet")).toBeUndefined(); }); }); + +describe("Get Address Type", () => { + const addresses = [ + { + address: "tf1qzvvn8rp2q93w5rf9afpjjm2w2wtuu2fnvl6j3p", + type: AddressType.P2WPKH, + }, + { + address: "tf1qncd7qa2cafwv3cpw68vqczg3qj904k2f4lard4wrj50rzkwmagvsemeex5", + type: AddressType.P2WSH, + }, + { address: "77nPza1LqwQzS9jMCnxVV3xKYWnLLZePwo", type: AddressType.P2PKH }, + { address: "tkwD8teFiwr9fGwwX2KfgrgYRbwEiWmMJw", type: AddressType.P2SH }, + { + address: "0xe36f18af5bFfDcB442E84970408F68570aB88F52", + type: AddressType.ETH, + }, + ]; + + addresses.forEach(({ address, type }) => { + it(`should be able to get valid ${type} address type`, () => { + expect(getAddressType(address, "testnet")).toStrictEqual(type); + }); + }); + + it(`should be able to get valid undefined with wrong address`, () => { + expect( + getAddressType("bcrt1q6cxskutl6jf0jjeqxc3ymfuqakhw4r247tht58", "testnet") + ).toStrictEqual(undefined); + }); +});