Skip to content

Commit

Permalink
SOL length validation
Browse files Browse the repository at this point in the history
  • Loading branch information
TateB committed Jan 21, 2024
1 parent fc9d733 commit 68792be
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
19 changes: 14 additions & 5 deletions src/coin/sol.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ describe.each([
text: "CNR8RPMxjY28VsPA6KFq3B8PUdZnrTSC5HSFwKPBR29Z",
hex: "a8ed08e3e8fe204de45e7295cc1ad53db096621b878f8c546e5c09f5e48f70b4",
},
// The old test case (same as TRON and NMC), only keep it for reference purpose.
{
text: "TUrMmF9Gd4rzrXsQ34ui3Wou94E7HFuJQh",
hex: "41cf1ecacaf90a04bb0297f9991ae1262d0a3399e13d6d96c2",
},
])("sol address", ({ text, hex }) => {
test(`encode: ${text}`, () => {
expect(encodeSolAddress(hexToBytes(hex))).toEqual(text);
Expand All @@ -31,3 +26,17 @@ describe.each([
expect(decodeSolAddress(text)).toEqual(hexToBytes(hex));
});
});

test("SOL decoding - incorrect length", () => {
expect(() =>
decodeSolAddress("CNR8RPMxjY28VsPA6KFq3B8PUdZnrTSC5HSFwKPBR2")
).toThrow("Unrecognised address format");
});

test("SOL encoding - incorrect length", () => {
expect(() =>
encodeSolAddress(
hexToBytes("a8ed08e3e8fe204de45e7295cc1ad53db096621b878f8c546e5c09f5e48f")
)
).toThrow("Unrecognised address format");
});
13 changes: 11 additions & 2 deletions src/coin/sol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,17 @@ import {
const name = "sol";
const coinType = 501;

export const encodeSolAddress = base58UncheckedEncode;
export const decodeSolAddress = base58UncheckedDecode;
export const encodeSolAddress = (source: Uint8Array) => {
if (source.length !== 32) throw new Error("Unrecognised address format");

return base58UncheckedEncode(source);
};
export const decodeSolAddress = (source: string) => {
const decoded = base58UncheckedDecode(source);
if (decoded.length !== 32) throw new Error("Unrecognised address format");

return decoded;
};

export const sol = {
name,
Expand Down

0 comments on commit 68792be

Please sign in to comment.