Skip to content

Commit

Permalink
switch entirely to @scure/base
Browse files Browse the repository at this point in the history
  • Loading branch information
TateB committed Jan 12, 2024
1 parent 9da9b1d commit 053f14f
Show file tree
Hide file tree
Showing 61 changed files with 350 additions and 595 deletions.
Binary file modified bun.lockb
Binary file not shown.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,14 @@
},
"devDependencies": {
"@types/fs-extra": "^11.0.2",
"bun-types": "latest",
"bun-types": "1.0.22",
"fs-extra": "^11.1.1",
"mitata": "^0.1.6",
"typescript": "^5.1.6"
},
"dependencies": {
"@noble/curves": "^1.2.0",
"@noble/hashes": "^1.3.2",
"@scure/base": "^1.1.3",
"bech32": "^2.0.0"
"@scure/base": "^1.1.5"
}
}
6 changes: 3 additions & 3 deletions src/coin/ae.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { concatBytes } from "@noble/hashes/utils";
import type { Coin } from "../types.js";
import { base58Decode, base58Encode } from "../utils/base58.js";
import { base58CheckDecode, base58CheckEncode } from "../utils/base58.js";

const name = "ae";
const coinType = 457;

export const encodeAeAddress = (source: Uint8Array): string => {
return `ak_${base58Encode(source.slice(2))}`;
return `ak_${base58CheckEncode(source.slice(2))}`;
};
export const decodeAeAddress = (source: string): Uint8Array => {
return concatBytes(
new Uint8Array([0x30, 0x78] /* 0x string */),
base58Decode(source.slice(3))
base58CheckDecode(source.slice(3))
);
};

Expand Down
8 changes: 4 additions & 4 deletions src/coin/aib.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Coin } from "../types.js";
import {
createBase58WithCheckDecoder,
createBase58WithCheckEncoder,
createBase58VersionedDecoder,
createBase58VersionedEncoder,
} from "../utils/base58.js";

const name = "aib";
Expand All @@ -10,11 +10,11 @@ const coinType = 55;
const p2pkhVersions = [new Uint8Array([0x17])];
const p2shVersions = [new Uint8Array([0x05])];

export const encodeAibAddress = createBase58WithCheckEncoder(
export const encodeAibAddress = createBase58VersionedEncoder(
p2pkhVersions[0],
p2shVersions[0]
);
export const decodeAibAddress = createBase58WithCheckDecoder(
export const decodeAibAddress = createBase58VersionedDecoder(
p2pkhVersions,
p2shVersions
);
Expand Down
10 changes: 3 additions & 7 deletions src/coin/algo.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { sha512_256 } from "@noble/hashes/sha512";
import { utils } from "@scure/base";
import type { Coin } from "../types.js";
import {
base32Decode,
base32Encode,
unpaddedBase32Options,
} from "../utils/base32.js";
import { base32UnpaddedDecode, base32UnpaddedEncode } from "../utils/base32.js";

const name = "algo";
const coinType = 283;
Expand All @@ -14,10 +10,10 @@ const algoChecksum = utils.checksum(4, (data) => sha512_256(data).slice(-4));

export const encodeAlgoAddress = (source: Uint8Array): string => {
const checksum = algoChecksum.encode(source);
return base32Encode(checksum, unpaddedBase32Options);
return base32UnpaddedEncode(checksum);
};
export const decodeAlgoAddress = (source: string): Uint8Array => {
const decoded = base32Decode(source, unpaddedBase32Options);
const decoded = base32UnpaddedDecode(source);

if (decoded.length !== 36) throw new Error("Unrecognised address format");

Expand Down
22 changes: 3 additions & 19 deletions src/coin/ar.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
import { base64urlnopad } from "@scure/base";
import type { Coin } from "../types.js";
import { base64Decode, base64Encode } from "../utils/base64.js";

const name = "ar";
const coinType = 472;

const encodeReplaceRegex = /\+|\/|\=/g;
const decodeReplaceRegex = /\-|\_/g;

export const encodeArAddress = (source: Uint8Array): string => {
return base64Encode(source).replace(encodeReplaceRegex, (match) => {
if (match === "+") return "-";
if (match === "/") return "_";
return "";
});
};
export const decodeArAddress = (source: string): Uint8Array => {
const restoredBase64 =
source.replace(decodeReplaceRegex, (match) => {
if (match === "-") return "+";
return "/";
}) + "=".repeat((4 - (source.length % 4)) % 4);
return base64Decode(restoredBase64);
};
export const encodeArAddress = base64urlnopad.encode;
export const decodeArAddress = base64urlnopad.decode;

export const ar = {
name,
Expand Down
6 changes: 3 additions & 3 deletions src/coin/ark.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { Coin } from "../types.js";
import { base58Decode, base58Encode } from "../utils/base58.js";
import { base58CheckDecode, base58CheckEncode } from "../utils/base58.js";

const name = "ark";
const coinType = 111;

export const encodeArkAddress = base58Encode;
export const encodeArkAddress = base58CheckEncode;
export const decodeArkAddress = (source: string): Uint8Array => {
const decoded = base58Decode(source);
const decoded = base58CheckDecode(source);
if (decoded[0] !== 23) throw new Error("Invalid address");
return decoded;
};
Expand Down
4 changes: 2 additions & 2 deletions src/coin/bch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { concatBytes } from "@noble/hashes/utils";
import type { Coin } from "../types.js";
import { createBase58WithCheckDecoder } from "../utils/base58.js";
import { createBase58VersionedDecoder } from "../utils/base58.js";
import {
decodeBchAddressToTypeAndHash,
encodeBchAddressWithVersion,
Expand All @@ -12,7 +12,7 @@ const coinType = 145;
const p2pkhVersions = [new Uint8Array([0x00])];
const p2shVersions = [new Uint8Array([0x05])];

const bchBase58Decode = createBase58WithCheckDecoder(
const bchBase58Decode = createBase58VersionedDecoder(
p2pkhVersions,
p2shVersions
);
Expand Down
8 changes: 4 additions & 4 deletions src/coin/bps.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Coin } from "../types.js";
import {
createBase58WithCheckDecoder,
createBase58WithCheckEncoder,
createBase58VersionedDecoder,
createBase58VersionedEncoder,
} from "../utils/base58.js";

const name = "bps";
Expand All @@ -10,11 +10,11 @@ const coinType = 576;
const p2pkhVersions = [new Uint8Array([0x00])];
const p2shVersions = [new Uint8Array([0x05])];

export const encodeBpsAddress = createBase58WithCheckEncoder(
export const encodeBpsAddress = createBase58VersionedEncoder(
p2pkhVersions[0],
p2shVersions[0]
);
export const decodeBpsAddress = createBase58WithCheckDecoder(
export const decodeBpsAddress = createBase58VersionedDecoder(
p2pkhVersions,
p2shVersions
);
Expand Down
6 changes: 3 additions & 3 deletions src/coin/bsv.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { concatBytes } from "@noble/hashes/utils";
import type { Coin } from "../types.js";
import { base58Decode, base58Encode } from "../utils/base58.js";
import { base58CheckDecode, base58CheckEncode } from "../utils/base58.js";

const name = "bsv";
const coinType = 236;

export const encodeBsvAddress = (source: Uint8Array): string =>
base58Encode(concatBytes(new Uint8Array([0x00]), source));
base58CheckEncode(concatBytes(new Uint8Array([0x00]), source));
export const decodeBsvAddress = (source: string): Uint8Array => {
const decoded = base58Decode(source);
const decoded = base58CheckDecode(source);

if (decoded.length !== 21) throw new Error("Unrecognised address format");

Expand Down
8 changes: 4 additions & 4 deletions src/coin/cca.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Coin } from "../types.js";
import {
createBase58WithCheckDecoder,
createBase58WithCheckEncoder,
createBase58VersionedDecoder,
createBase58VersionedEncoder,
} from "../utils/base58.js";

const name = "cca";
Expand All @@ -10,11 +10,11 @@ const coinType = 489;
const p2pkhVersions = [new Uint8Array([0x0b])];
const p2shVersions = [new Uint8Array([0x05])];

export const encodeCcaAddress = createBase58WithCheckEncoder(
export const encodeCcaAddress = createBase58VersionedEncoder(
p2pkhVersions[0],
p2shVersions[0]
);
export const decodeCcaAddress = createBase58WithCheckDecoder(
export const decodeCcaAddress = createBase58VersionedDecoder(
p2pkhVersions,
p2shVersions
);
Expand Down
8 changes: 4 additions & 4 deletions src/coin/dash.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Coin } from "../types.js";
import {
createBase58WithCheckDecoder,
createBase58WithCheckEncoder,
createBase58VersionedDecoder,
createBase58VersionedEncoder,
} from "../utils/base58.js";

const name = "dash";
Expand All @@ -10,11 +10,11 @@ const coinType = 5;
const p2pkhVersions = [new Uint8Array([0x4c])];
const p2shVersions = [new Uint8Array([0x10])];

export const encodeDashAddress = createBase58WithCheckEncoder(
export const encodeDashAddress = createBase58VersionedEncoder(
p2pkhVersions[0],
p2shVersions[0]
);
export const decodeDashAddress = createBase58WithCheckDecoder(
export const decodeDashAddress = createBase58VersionedDecoder(
p2pkhVersions,
p2shVersions
);
Expand Down
9 changes: 6 additions & 3 deletions src/coin/dcr.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import type { Coin } from "../types.js";
import { base58DecodeNoCheck, base58EncodeNoCheck } from "../utils/base58.js";
import {
base58UncheckedDecode,
base58UncheckedEncode,
} from "../utils/base58.js";

const name = "dcr";
const coinType = 42;

export const encodeDcrAddress = base58EncodeNoCheck;
export const decodeDcrAddress = base58DecodeNoCheck;
export const encodeDcrAddress = base58UncheckedEncode;
export const decodeDcrAddress = base58UncheckedDecode;

export const dcr = {
name,
Expand Down
8 changes: 4 additions & 4 deletions src/coin/divi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Coin } from "../types.js";
import {
createBase58WithCheckDecoder,
createBase58WithCheckEncoder,
createBase58VersionedDecoder,
createBase58VersionedEncoder,
} from "../utils/base58.js";

const name = "divi";
Expand All @@ -10,11 +10,11 @@ const coinType = 301;
const p2pkhVersions = [new Uint8Array([0x1e])];
const p2shVersions = [new Uint8Array([0xd])];

export const encodeDiviAddress = createBase58WithCheckEncoder(
export const encodeDiviAddress = createBase58VersionedEncoder(
p2pkhVersions[0],
p2shVersions[0]
);
export const decodeDiviAddress = createBase58WithCheckDecoder(
export const decodeDiviAddress = createBase58VersionedDecoder(
p2pkhVersions,
p2shVersions
);
Expand Down
8 changes: 4 additions & 4 deletions src/coin/doge.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Coin } from "../types.js";
import {
createBase58WithCheckDecoder,
createBase58WithCheckEncoder,
createBase58VersionedDecoder,
createBase58VersionedEncoder,
} from "../utils/base58.js";

const name = "doge";
Expand All @@ -10,11 +10,11 @@ const coinType = 3;
const p2pkhVersions = [new Uint8Array([0x1e])];
const p2shVersions = [new Uint8Array([0x16])];

export const encodeDogeAddress = createBase58WithCheckEncoder(
export const encodeDogeAddress = createBase58VersionedEncoder(
p2pkhVersions[0],
p2shVersions[0]
);
export const decodeDogeAddress = createBase58WithCheckDecoder(
export const decodeDogeAddress = createBase58VersionedDecoder(
p2pkhVersions,
p2shVersions
);
Expand Down
9 changes: 6 additions & 3 deletions src/coin/ela.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import type { Coin } from "../types.js";
import { base58DecodeNoCheck, base58EncodeNoCheck } from "../utils/base58.js";
import {
base58UncheckedDecode,
base58UncheckedEncode,
} from "../utils/base58.js";

const name = "ela";
const coinType = 2305;

export const encodeElaAddress = base58EncodeNoCheck;
export const decodeElaAddress = base58DecodeNoCheck;
export const encodeElaAddress = base58UncheckedEncode;
export const decodeElaAddress = base58UncheckedDecode;

export const ela = {
name,
Expand Down
13 changes: 3 additions & 10 deletions src/coin/fil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import { equalBytes } from "@noble/curves/abstract/utils";
import { blake2b } from "@noble/hashes/blake2b";
import { concatBytes } from "@noble/hashes/utils";
import type { Coin } from "../types.js";
import {
base32Decode,
base32Encode,
unpaddedBase32Options,
} from "../utils/base32.js";
import { base32UnpaddedDecode, base32UnpaddedEncode } from "../utils/base32.js";
import { decodeLeb128, encodeLeb128 } from "../utils/leb128.js";

const name = "fil";
Expand Down Expand Up @@ -41,7 +37,7 @@ export const encodeFilAddress = (source: Uint8Array): string => {
}
const checksum = blake2b(source, { dkLen: 4 });
const bytes = concatBytes(payload, checksum);
const decoded = base32Encode(bytes, unpaddedBase32Options).toLowerCase();
const decoded = base32UnpaddedEncode(bytes).toLowerCase();
return `f${protocol}${decoded}`;
};
export const decodeFilAddress = (source: string): Uint8Array => {
Expand All @@ -56,10 +52,7 @@ export const decodeFilAddress = (source: string): Uint8Array => {
return concatBytes(protocolByte, encodeLeb128(BigInt(encoded)));
}

const payloadWithChecksum = base32Decode(
encoded.toUpperCase(),
unpaddedBase32Options
);
const payloadWithChecksum = base32UnpaddedDecode(encoded.toUpperCase());
const payload = payloadWithChecksum.slice(0, -4);
const checksum = payloadWithChecksum.slice(-4);
const decoded = concatBytes(protocolByte, payload);
Expand Down
8 changes: 4 additions & 4 deletions src/coin/firo.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Coin } from "../types.js";
import {
createBase58WithCheckDecoder,
createBase58WithCheckEncoder,
createBase58VersionedDecoder,
createBase58VersionedEncoder,
} from "../utils/base58.js";

const name = "firo";
Expand All @@ -10,11 +10,11 @@ const coinType = 136;
const p2pkhVersions = [new Uint8Array([0x52])];
const p2shVersions = [new Uint8Array([0x07])];

export const encodeFiroAddress = createBase58WithCheckEncoder(
export const encodeFiroAddress = createBase58VersionedEncoder(
p2pkhVersions[0],
p2shVersions[0]
);
export const decodeFiroAddress = createBase58WithCheckDecoder(
export const decodeFiroAddress = createBase58VersionedDecoder(
p2pkhVersions,
p2shVersions
);
Expand Down
Loading

0 comments on commit 053f14f

Please sign in to comment.