-
Notifications
You must be signed in to change notification settings - Fork 5
test(core): add tests for getAddressType #183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
annazhlb
wants to merge
4
commits into
main
Choose a base branch
from
test/get-address-type
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bff4933
test: (getAddressType): added tests
annazhlb ac12fba
test(core): add tests for getAddressType
annazhlb ae547c5
Merge branch 'main' of github.com:midl-xyz/midl-js into test/get-addr…
opcode0x b493bb4
test(core): update tests for getAddressType
annazhlb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| import ecc from "@bitcoinerlab/secp256k1"; | ||
| import * as bitcoin from "bitcoinjs-lib"; | ||
| import ECPairFactory from "ecpair"; | ||
| import { describe, expect, it } from "vitest"; | ||
| import { getAddressType } from "./getAddressType"; | ||
|
|
||
| describe("core | utils | getAddressType", () => { | ||
| const ECPair = ECPairFactory(ecc); | ||
| bitcoin.initEccLib(ecc); | ||
|
|
||
| const mainnet = bitcoin.networks.bitcoin; | ||
| const regtest = bitcoin.networks.regtest; | ||
| const testnet = bitcoin.networks.testnet; | ||
|
|
||
| const keyPairMainnet = ECPair.makeRandom({ network: mainnet }); | ||
| const keyPairTestnet = ECPair.makeRandom({ network: testnet }); | ||
| const keyPairRegtest = ECPair.makeRandom({ network: regtest }); | ||
|
|
||
| it("returns P2WPKH type", () => { | ||
| const { address: p2wpkhMainnet } = bitcoin.payments.p2wpkh({ | ||
| pubkey: keyPairMainnet.publicKey, | ||
| network: mainnet, | ||
| }); | ||
|
|
||
| const { address: p2wpkhTestnet } = bitcoin.payments.p2wpkh({ | ||
| pubkey: keyPairTestnet.publicKey, | ||
| network: testnet, | ||
| }); | ||
|
|
||
| const { address: p2wpkhRegtest } = bitcoin.payments.p2wpkh({ | ||
| pubkey: keyPairRegtest.publicKey, | ||
| network: regtest, | ||
| }); | ||
| // biome-ignore lint/style/noNonNullAssertion: <explanation> | ||
| expect(getAddressType(p2wpkhMainnet!)).toBe("p2wpkh"); | ||
| // biome-ignore lint/style/noNonNullAssertion: <explanation> | ||
| expect(getAddressType(p2wpkhTestnet!)).toBe("p2wpkh"); | ||
| // biome-ignore lint/style/noNonNullAssertion: <explanation> | ||
| expect(getAddressType(p2wpkhRegtest!)).toBe("p2wpkh"); | ||
| }); | ||
|
|
||
| it("returns P2SH-P2WPKH type", () => { | ||
| const { address: p2wpkhMainnet } = bitcoin.payments.p2sh({ | ||
| redeem: bitcoin.payments.p2wpkh({ | ||
| pubkey: keyPairMainnet.publicKey, | ||
| network: mainnet, | ||
| }), | ||
| network: mainnet, | ||
| }); | ||
|
|
||
| const { address: p2wpkhTestnet } = bitcoin.payments.p2sh({ | ||
| redeem: bitcoin.payments.p2wpkh({ | ||
| pubkey: keyPairTestnet.publicKey, | ||
| network: testnet, | ||
| }), | ||
| network: testnet, | ||
| }); | ||
|
|
||
| const { address: p2wpkhRegtest } = bitcoin.payments.p2sh({ | ||
| redeem: bitcoin.payments.p2wpkh({ | ||
| pubkey: keyPairRegtest.publicKey, | ||
| network: regtest, | ||
| }), | ||
| network: regtest, | ||
| }); | ||
| // biome-ignore lint/style/noNonNullAssertion: <explanation> | ||
| expect(getAddressType(p2wpkhMainnet!)).toBe("p2sh_p2wpkh"); //3 | ||
| // biome-ignore lint/style/noNonNullAssertion: <explanation> | ||
| expect(getAddressType(p2wpkhTestnet!)).toBe("p2sh_p2wpkh"); //2 | ||
| // biome-ignore lint/style/noNonNullAssertion: <explanation> | ||
| expect(getAddressType(p2wpkhRegtest!)).toBe("p2sh_p2wpkh"); //2 | ||
|
|
||
| console.log(p2wpkhTestnet, p2wpkhMainnet, p2wpkhRegtest); | ||
| }); | ||
|
|
||
| it("returns P2TR type", () => { | ||
| const { address: p2trMainnet } = bitcoin.payments.p2tr({ | ||
| pubkey: keyPairMainnet.publicKey.slice(1, 33), | ||
| network: mainnet, | ||
| }); | ||
|
|
||
| const { address: p2trTestnet } = bitcoin.payments.p2tr({ | ||
| pubkey: keyPairTestnet.publicKey.slice(1, 33), | ||
| network: testnet, | ||
| }); | ||
|
|
||
| const { address: p2trRegtest } = bitcoin.payments.p2tr({ | ||
| pubkey: keyPairRegtest.publicKey.slice(1, 33), | ||
| network: regtest, | ||
| }); | ||
| // biome-ignore lint/style/noNonNullAssertion: <explanation> | ||
| expect(getAddressType(p2trMainnet!)).toBe("p2tr"); | ||
| // biome-ignore lint/style/noNonNullAssertion: <explanation> | ||
| expect(getAddressType(p2trTestnet!)).toBe("p2tr"); | ||
| // biome-ignore lint/style/noNonNullAssertion: <explanation> | ||
| expect(getAddressType(p2trRegtest!)).toBe("p2tr"); | ||
| }); | ||
|
|
||
| it("returns P2PKH type", () => { | ||
|
||
| const { address: p2pkhMainnet } = bitcoin.payments.p2pkh({ | ||
| pubkey: keyPairMainnet.publicKey, | ||
| network: mainnet, | ||
| }); | ||
|
|
||
| const { address: p2pkhTestnet } = bitcoin.payments.p2pkh({ | ||
| pubkey: keyPairTestnet.publicKey, | ||
| network: testnet, | ||
| }); | ||
|
|
||
| const { address: p2pkhRegtest } = bitcoin.payments.p2pkh({ | ||
| pubkey: keyPairRegtest.publicKey, | ||
| network: regtest, | ||
| }); | ||
| // biome-ignore lint/style/noNonNullAssertion: <explanation> | ||
| expect(getAddressType(p2pkhMainnet!)).toBe("p2pkh"); // 1 | ||
| // biome-ignore lint/style/noNonNullAssertion: <explanation> | ||
| expect(getAddressType(p2pkhTestnet!)).toBe("p2pkh"); // mx | ||
| // biome-ignore lint/style/noNonNullAssertion: <explanation> | ||
| expect(getAddressType(p2pkhRegtest!)).toBe("p2pkh"); // mpx | ||
| }); | ||
|
|
||
| it("throws error if enter unknown address type", () => { | ||
| expect(() => | ||
| getAddressType("0x1234567890abcdef1234567890abcdef12345678"), | ||
| ).toThrowError("Unknown address type"); | ||
|
|
||
| expect(() => | ||
| getAddressType("bc1sdfghjklkdxt67hgfdgchvj56rtygh"), | ||
| ).toThrowError("Unknown address type"); | ||
|
|
||
| expect(() => | ||
| getAddressType("b c1qsdfghjklkdxt67hgfdgchvj56rtygh"), | ||
| ).toThrowError("Unknown address type"); | ||
|
|
||
| expect(() => | ||
| getAddressType("bC1qsdfghjklkdxt67hgfdgchvj56rtygh"), | ||
| ).toThrowError("Unknown address type"); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's do like this an we can opt out biome ingores
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
biome ignores are no longer used.
an string type is now provided for the address when calling the function