Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
feature(walletkit-core): added method to get address type from addres…
Browse files Browse the repository at this point in the history
…s 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
  • Loading branch information
fullstackninja864 authored Jul 20, 2023
1 parent 3e0a6f8 commit 93ba43b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
24 changes: 23 additions & 1 deletion packages/walletkit-core/src/api/address.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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;
}
}
39 changes: 37 additions & 2 deletions packages/walletkit-core/src/api/address.unit.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand All @@ -20,7 +25,6 @@ describe("Address Decoder", () => {
script,
network: "testnet",
};

expect(getDecodedAddress(script, "testnet")).toStrictEqual(expected);
});

Expand Down Expand Up @@ -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);
});
});

0 comments on commit 93ba43b

Please sign in to comment.