Skip to content

Commit

Permalink
tweaks + test
Browse files Browse the repository at this point in the history
  • Loading branch information
TateB committed Jan 18, 2024
1 parent b37ec1c commit 55bec29
Show file tree
Hide file tree
Showing 130 changed files with 455 additions and 277 deletions.
52 changes: 52 additions & 0 deletions src/async.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { expect, test } from "bun:test";
import { getCoderByCoinNameAsync, getCoderByCoinTypeAsync } from "./async.js";
import {
evmCoinNameToTypeMap,
nonEvmCoinNameToTypeMap,
} from "./consts/coinNameToTypeMap.js";
import {
evmCoinTypeToNameMap,
nonEvmCoinTypeToNameMap,
} from "./consts/coinTypeToNameMap.js";
import { coinTypeToEvmChainId } from "./utils/evm.js";

test("coin name", async () => {
const coder = await getCoderByCoinNameAsync("btc");
Expand All @@ -26,3 +35,46 @@ test("evm coin type", async () => {
expect(coder.name).toBe("op");
expect(coder.evmChainId).toBe(10);
});

const nonEvmCoinNames = Object.keys(nonEvmCoinNameToTypeMap);
const evmCoinNames = Object.keys(evmCoinNameToTypeMap);

test.each(nonEvmCoinNames)(
'getCoderByCoinNameAsync("%s")',
async (coinName) => {
const coder = await getCoderByCoinNameAsync(coinName);
expect(coder.name).toBe(coinName);
expect(coder.coinType).toBe(nonEvmCoinNameToTypeMap[coinName]);
expect(coder.encode).toBeFunction();
expect(coder.decode).toBeFunction();
}
);

test.each(evmCoinNames)('getCoderByCoinNameAsync("%s")', async (coinName) => {
const coder = await getCoderByCoinNameAsync(coinName);
expect(coder.name).toBe(coinName);
expect(coder.coinType).toBe(evmCoinNameToTypeMap[coinName]);
expect(coder.evmChainId).toBe(coinTypeToEvmChainId(coder.coinType));
expect(coder.encode).toBeFunction();
expect(coder.decode).toBeFunction();
});

const nonEvmCoinTypes = Object.values(nonEvmCoinNameToTypeMap);
const evmCoinTypes = Object.values(evmCoinNameToTypeMap);

test.each(nonEvmCoinTypes)("getCoderByCoinTypeAsync(%d)", async (coinType) => {
const coder = await getCoderByCoinTypeAsync(coinType);
expect(coder.name).toBe(nonEvmCoinTypeToNameMap[coinType][0]);
expect(coder.coinType).toBe(coinType);
expect(coder.encode).toBeFunction();
expect(coder.decode).toBeFunction();
});

test.each(evmCoinTypes)("getCoderByCoinTypeAsync(%d)", async (coinType) => {
const coder = await getCoderByCoinTypeAsync(coinType);
expect(coder.name).toBe(evmCoinTypeToNameMap[coinType][0]);
expect(coder.coinType).toBe(coinType);
expect(coder.evmChainId).toBe(coinTypeToEvmChainId(coinType));
expect(coder.encode).toBeFunction();
expect(coder.decode).toBeFunction();
});
15 changes: 15 additions & 0 deletions src/coders.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { expect, test } from "bun:test";
import * as coders from "./coders.js";
import { nonEvmCoinNameToTypeMap } from "./consts/coinNameToTypeMap.js";

const coinNames = Object.keys(nonEvmCoinNameToTypeMap);

const capitalise = (s: string) => s[0].toUpperCase() + s.slice(1);

test.each(coinNames)("coders.ts exports - %s", (coinName) => {
const coderSuffix = `${capitalise(coinName)}Address`;
const encoder = coders[`encode${coderSuffix}`];
const decoder = coders[`decode${coderSuffix}`];
expect(encoder).toBeFunction();
expect(decoder).toBeFunction();
});
8 changes: 6 additions & 2 deletions src/coders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export { decodeFilAddress, encodeFilAddress } from "./coin/fil.js";
export { decodeFioAddress, encodeFioAddress } from "./coin/fio.js";
export { decodeFiroAddress, encodeFiroAddress } from "./coin/firo.js";
export { decodeFlowAddress, encodeFlowAddress } from "./coin/flow.js";
export { decodeFluxAddress, encodeFluxAddress } from "./coin/flux.js";
export {
decodeFtmLegacyAddress,
encodeFtmLegacyAddress,
Expand Down Expand Up @@ -107,8 +108,8 @@ export {
} from "./coin/poaLegacy.js";
export { decodePpcAddress, encodePpcAddress } from "./coin/ppc.js";
export { decodeQtumAddress, encodeQtumAddress } from "./coin/qtum.js";
export { decodeRbtcAddress, encodeRbtcAddress } from "./coin/rbtc.js";
export { decodeRddAddress, encodeRddAddress } from "./coin/rdd.js";
export { decodeRskAddress, encodeRskAddress } from "./coin/rsk.js";
export { decodeRuneAddress, encodeRuneAddress } from "./coin/rune.js";
export { decodeRvnAddress, encodeRvnAddress } from "./coin/rvn.js";
export { decodeScAddress, encodeScAddress } from "./coin/sc.js";
Expand Down Expand Up @@ -137,6 +138,10 @@ export {
export { decodeVetAddress, encodeVetAddress } from "./coin/vet.js";
export { decodeViaAddress, encodeViaAddress } from "./coin/via.js";
export { decodeVlxAddress, encodeVlxAddress } from "./coin/vlx.js";
export {
decodeVlxLegacyAddress,
encodeVlxLegacyAddress,
} from "./coin/vlxLegacy.js";
export { decodeVsysAddress, encodeVsysAddress } from "./coin/vsys.js";
export { decodeWanAddress, encodeWanAddress } from "./coin/wan.js";
export { decodeWavesAddress, encodeWavesAddress } from "./coin/waves.js";
Expand All @@ -150,6 +155,5 @@ export { decodeXrpAddress, encodeXrpAddress } from "./coin/xrp.js";
export { decodeXtzAddress, encodeXtzAddress } from "./coin/xtz.js";
export { decodeXvgAddress, encodeXvgAddress } from "./coin/xvg.js";
export { decodeZecAddress, encodeZecAddress } from "./coin/zec.js";
export { decodeZelAddress, encodeZelAddress } from "./coin/zel.js";
export { decodeZenAddress, encodeZenAddress } from "./coin/zen.js";
export { decodeZilAddress, encodeZilAddress } from "./coin/zil.js";
4 changes: 2 additions & 2 deletions src/coin/abbc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Coin } from "../types.js";
import type { CheckedCoin } from "../types.js";
import { createEosDecoder, createEosEncoder } from "../utils/eosio.js";

const name = "abbc";
Expand All @@ -14,4 +14,4 @@ export const abbc = {
coinType,
encode: encodeAbbcAddress,
decode: decodeAbbcAddress,
} as const satisfies Coin;
} as const satisfies CheckedCoin;
4 changes: 2 additions & 2 deletions src/coin/ada.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Coin } from "../types.js";
import type { CheckedCoin } from "../types.js";
import { createBech32Decoder, createBech32Encoder } from "../utils/bech32.js";
import { byronDecode, byronEncode } from "../utils/byron.js";

Expand Down Expand Up @@ -30,4 +30,4 @@ export const ada = {
coinType,
encode: encodeAdaAddress,
decode: decodeAdaAddress,
} as const satisfies Coin;
} as const satisfies CheckedCoin;
4 changes: 2 additions & 2 deletions src/coin/ae.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { concatBytes } from "@noble/hashes/utils";
import type { Coin } from "../types.js";
import type { CheckedCoin } from "../types.js";
import { base58CheckDecode, base58CheckEncode } from "../utils/base58.js";

const name = "ae";
Expand All @@ -20,4 +20,4 @@ export const ae = {
coinType,
encode: encodeAeAddress,
decode: decodeAeAddress,
} as const satisfies Coin;
} as const satisfies CheckedCoin;
4 changes: 2 additions & 2 deletions src/coin/aib.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Coin } from "../types.js";
import type { CheckedCoin } from "../types.js";
import {
createBase58VersionedDecoder,
createBase58VersionedEncoder,
Expand All @@ -24,4 +24,4 @@ export const aib = {
coinType,
encode: encodeAibAddress,
decode: decodeAibAddress,
} as const satisfies Coin;
} as const satisfies CheckedCoin;
4 changes: 2 additions & 2 deletions src/coin/aion.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Coin } from "../types.js";
import type { CheckedCoin } from "../types.js";
import { bytesToHex, hexWithoutPrefixToBytes } from "../utils/bytes.js";

const name = "aion";
Expand All @@ -22,4 +22,4 @@ export const aion = {
coinType,
encode: encodeAionAddress,
decode: decodeAionAddress,
} as const satisfies Coin;
} as const satisfies CheckedCoin;
4 changes: 2 additions & 2 deletions src/coin/algo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { sha512_256 } from "@noble/hashes/sha512";
import { utils } from "@scure/base";
import type { Coin } from "../types.js";
import type { CheckedCoin } from "../types.js";
import { base32UnpaddedDecode, base32UnpaddedEncode } from "../utils/base32.js";

const name = "algo";
Expand All @@ -25,4 +25,4 @@ export const algo = {
coinType,
encode: encodeAlgoAddress,
decode: decodeAlgoAddress,
} as const satisfies Coin;
} as const satisfies CheckedCoin;
4 changes: 2 additions & 2 deletions src/coin/ar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { base64urlnopad } from "@scure/base";
import type { Coin } from "../types.js";
import type { CheckedCoin } from "../types.js";

const name = "ar";
const coinType = 472;
Expand All @@ -12,4 +12,4 @@ export const ar = {
coinType,
encode: encodeArAddress,
decode: decodeArAddress,
} as const satisfies Coin;
} as const satisfies CheckedCoin;
4 changes: 2 additions & 2 deletions src/coin/ardr.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Coin } from "../types.js";
import type { CheckedCoin } from "../types.js";

const name = "ardr";
const coinType = 16754;
Expand Down Expand Up @@ -87,4 +87,4 @@ export const ardr = {
coinType,
encode: encodeArdrAddress,
decode: decodeArdrAddress,
} as const satisfies Coin;
} as const satisfies CheckedCoin;
4 changes: 2 additions & 2 deletions src/coin/ark.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Coin } from "../types.js";
import type { CheckedCoin } from "../types.js";
import { base58CheckDecode, base58CheckEncode } from "../utils/base58.js";

const name = "ark";
Expand All @@ -16,4 +16,4 @@ export const ark = {
coinType,
encode: encodeArkAddress,
decode: decodeArkAddress,
} as const satisfies Coin;
} as const satisfies CheckedCoin;
4 changes: 2 additions & 2 deletions src/coin/atom.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Coin } from "../types.js";
import type { CheckedCoin } from "../types.js";
import { createBech32Decoder, createBech32Encoder } from "../utils/bech32.js";

const name = "atom";
Expand All @@ -12,4 +12,4 @@ export const atom = {
coinType,
encode: encodeAtomAddress,
decode: decodeAtomAddress,
} as const satisfies Coin;
} as const satisfies CheckedCoin;
4 changes: 2 additions & 2 deletions src/coin/avax.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Coin } from "../types.js";
import type { CheckedCoin } from "../types.js";
import { createBech32Decoder, createBech32Encoder } from "../utils/bech32.js";

const name = "avax";
Expand Down Expand Up @@ -26,4 +26,4 @@ export const avax = {
coinType,
encode: encodeAvaxAddress,
decode: decodeAvaxAddress,
} as const satisfies Coin;
} as const satisfies CheckedCoin;
4 changes: 2 additions & 2 deletions src/coin/bcd.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Coin } from "../types.js";
import type { CheckedCoin } from "../types.js";
import {
createBitcoinDecoder,
createBitcoinEncoder,
Expand Down Expand Up @@ -27,4 +27,4 @@ export const bcd = {
coinType,
encode: encodeBcdAddress,
decode: decodeBcdAddress,
} as const satisfies Coin;
} as const satisfies CheckedCoin;
4 changes: 2 additions & 2 deletions src/coin/bch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { concatBytes } from "@noble/hashes/utils";
import type { Coin } from "../types.js";
import type { CheckedCoin } from "../types.js";
import { createBase58VersionedDecoder } from "../utils/base58.js";
import {
decodeBchAddressToTypeAndHash,
Expand Down Expand Up @@ -63,4 +63,4 @@ export const bch = {
coinType,
encode: encodeBchAddress,
decode: decodeBchAddress,
} as const satisfies Coin;
} as const satisfies CheckedCoin;
4 changes: 2 additions & 2 deletions src/coin/bcn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { equalBytes } from "@noble/curves/abstract/utils";
import { keccak_256 } from "@noble/hashes/sha3";
import { concatBytes } from "@noble/hashes/utils";
import { utils } from "@scure/base";
import type { Coin } from "../types.js";
import type { CheckedCoin } from "../types.js";
import { decodeXmrAddress, encodeXmrAddress } from "./xmr.js";

const name = "bcn";
Expand Down Expand Up @@ -34,4 +34,4 @@ export const bcn = {
coinType,
encode: encodeBcnAddress,
decode: decodeBcnAddress,
} as const satisfies Coin;
} as const satisfies CheckedCoin;
4 changes: 2 additions & 2 deletions src/coin/bdx.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Coin } from "../types.js";
import type { CheckedCoin } from "../types.js";
import { decodeXmrAddress, encodeXmrAddress } from "./xmr.js";

const name = "bdx";
Expand All @@ -12,4 +12,4 @@ export const bdx = {
coinType,
encode: encodeBdxAddress,
decode: decodeBdxAddress,
} as const satisfies Coin;
} as const satisfies CheckedCoin;
4 changes: 2 additions & 2 deletions src/coin/bnb.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Coin } from "../types.js";
import type { CheckedCoin } from "../types.js";
import { createBech32Decoder, createBech32Encoder } from "../utils/bech32.js";

const name = "bnb";
Expand All @@ -14,4 +14,4 @@ export const bnb = {
coinType,
encode: encodeBnbAddress,
decode: decodeBnbAddress,
} as const satisfies Coin;
} as const satisfies CheckedCoin;
4 changes: 2 additions & 2 deletions src/coin/bps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Coin } from "../types.js";
import type { CheckedCoin } from "../types.js";
import {
createBase58VersionedDecoder,
createBase58VersionedEncoder,
Expand All @@ -24,4 +24,4 @@ export const bps = {
coinType,
encode: encodeBpsAddress,
decode: decodeBpsAddress,
} as const satisfies Coin;
} as const satisfies CheckedCoin;
4 changes: 2 additions & 2 deletions src/coin/bsv.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { concatBytes } from "@noble/hashes/utils";
import type { Coin } from "../types.js";
import type { CheckedCoin } from "../types.js";
import { base58CheckDecode, base58CheckEncode } from "../utils/base58.js";

const name = "bsv";
Expand All @@ -23,4 +23,4 @@ export const bsv = {
coinType,
encode: encodeBsvAddress,
decode: decodeBsvAddress,
} as const satisfies Coin;
} as const satisfies CheckedCoin;
4 changes: 2 additions & 2 deletions src/coin/btc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Coin } from "../types.js";
import type { CheckedCoin } from "../types.js";
import {
createBitcoinDecoder,
createBitcoinEncoder,
Expand Down Expand Up @@ -27,4 +27,4 @@ export const btc = {
coinType,
encode: encodeBtcAddress,
decode: decodeBtcAddress,
} as const satisfies Coin;
} as const satisfies CheckedCoin;
4 changes: 2 additions & 2 deletions src/coin/btg.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Coin } from "../types.js";
import type { CheckedCoin } from "../types.js";
import {
createBitcoinDecoder,
createBitcoinEncoder,
Expand Down Expand Up @@ -27,4 +27,4 @@ export const btg = {
coinType,
encode: encodeBtgAddress,
decode: decodeBtgAddress,
} as const satisfies Coin;
} as const satisfies CheckedCoin;
4 changes: 2 additions & 2 deletions src/coin/btm.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Coin } from "../types.js";
import type { CheckedCoin } from "../types.js";
import {
createBech32SegwitDecoder,
createBech32SegwitEncoder,
Expand All @@ -17,4 +17,4 @@ export const btm = {
coinType,
encode: encodeBtmAddress,
decode: decodeBtmAddress,
} as const satisfies Coin;
} as const satisfies CheckedCoin;
4 changes: 2 additions & 2 deletions src/coin/bts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Coin } from "../types.js";
import type { CheckedCoin } from "../types.js";
import { createEosDecoder, createEosEncoder } from "../utils/eosio.js";

const name = "bts";
Expand All @@ -14,4 +14,4 @@ export const bts = {
coinType,
encode: encodeBtsAddress,
decode: decodeBtsAddress,
} as const satisfies Coin;
} as const satisfies CheckedCoin;
4 changes: 2 additions & 2 deletions src/coin/cca.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Coin } from "../types.js";
import type { CheckedCoin } from "../types.js";
import {
createBase58VersionedDecoder,
createBase58VersionedEncoder,
Expand All @@ -24,4 +24,4 @@ export const cca = {
coinType,
encode: encodeCcaAddress,
decode: decodeCcaAddress,
} as const satisfies Coin;
} as const satisfies CheckedCoin;
Loading

0 comments on commit 55bec29

Please sign in to comment.