2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-08-25 05:54:03 +00:00
|
|
|
|
2024-04-29 02:57:30 +00:00
|
|
|
import { assertEquals, assertThrows } from "@std/assert";
|
2023-09-21 09:29:13 +00:00
|
|
|
import { decodeBase58, encodeBase58 } from "./base58.ts";
|
2022-08-25 05:54:03 +00:00
|
|
|
|
|
|
|
const testSetString = [
|
|
|
|
["", ""],
|
|
|
|
["f", "2m"],
|
|
|
|
["ß", "FtS"],
|
|
|
|
["fo", "8o8"],
|
|
|
|
["foo", "bQbp"],
|
|
|
|
["foob", "3csAg9"],
|
|
|
|
["fooba", "CZJRhmz"],
|
|
|
|
["foobar", "t1Zv2yaZ"],
|
|
|
|
["Hello World!", "2NEpo7TZRRrLZSi2U"],
|
|
|
|
[new Uint8Array([0, 0, 0, 40, 127, 180, 205]), "111233QC4"],
|
2022-12-13 12:14:23 +00:00
|
|
|
[new Uint8Array([10, 0, 10]), "4MpV"],
|
2023-03-24 03:40:02 +00:00
|
|
|
[
|
|
|
|
// deno-fmt-ignore
|
|
|
|
new Uint8Array([
|
|
|
|
2, 212, 53, 147, 199, 21, 253, 211,
|
|
|
|
28, 97, 20, 26, 189, 4, 169, 159,
|
|
|
|
214, 130, 44, 133, 88, 133, 76, 205,
|
|
|
|
227, 154, 86, 132, 231, 165, 109, 162,
|
|
|
|
125, 137, 84
|
|
|
|
]),
|
|
|
|
"HNZata7iMYWmk5RvZRTiAsSDhV8366zq2YGb3tLH5Upf74F",
|
|
|
|
],
|
2024-02-24 20:24:08 +00:00
|
|
|
] as const;
|
2022-08-25 05:54:03 +00:00
|
|
|
|
|
|
|
const testSetBinary = testSetString.map(([data, b58]) => {
|
|
|
|
if (typeof data === "string") {
|
|
|
|
return [
|
|
|
|
new TextEncoder().encode(data),
|
|
|
|
b58,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [data, b58];
|
|
|
|
}) as Array<[Uint8Array, string]>;
|
|
|
|
|
2023-12-28 03:03:43 +00:00
|
|
|
Deno.test("encodeBase58() encodes string", () => {
|
2022-08-25 05:54:03 +00:00
|
|
|
for (const [input, output] of testSetString) {
|
2023-09-21 09:29:13 +00:00
|
|
|
assertEquals(encodeBase58(input), output);
|
2022-08-25 05:54:03 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-12-28 03:03:43 +00:00
|
|
|
Deno.test("encodeBase58() encodes binary", () => {
|
2022-08-25 05:54:03 +00:00
|
|
|
for (const [input, output] of testSetBinary) {
|
2023-09-21 09:29:13 +00:00
|
|
|
assertEquals(encodeBase58(input), output);
|
2022-08-25 05:54:03 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-12-28 03:03:43 +00:00
|
|
|
Deno.test("encodeBase58() encodes binary buffer", () => {
|
2022-08-25 05:54:03 +00:00
|
|
|
for (const [input, output] of testSetBinary) {
|
2023-09-21 09:29:13 +00:00
|
|
|
assertEquals(encodeBase58(input.buffer), output);
|
2022-08-25 05:54:03 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-12-28 03:03:43 +00:00
|
|
|
Deno.test("decodeBase58() decodes binary", () => {
|
2022-08-25 05:54:03 +00:00
|
|
|
for (const [input, output] of testSetBinary) {
|
2023-09-21 09:29:13 +00:00
|
|
|
const outputBinary = decodeBase58(output);
|
2022-08-25 05:54:03 +00:00
|
|
|
assertEquals(outputBinary, input);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-12-28 03:03:43 +00:00
|
|
|
Deno.test("decodeBase58() throws on invalid input", () => {
|
2022-08-25 05:54:03 +00:00
|
|
|
assertThrows(
|
2023-09-21 09:29:13 +00:00
|
|
|
() => decodeBase58("+2NEpo7TZRRrLZSi2U"),
|
2024-05-30 07:17:42 +00:00
|
|
|
TypeError,
|
2022-08-25 05:54:03 +00:00
|
|
|
`Invalid base58 char at index 0 with value +`,
|
|
|
|
);
|
|
|
|
});
|