Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/supported-cryptocurrencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ The following coins are supported:
| 589 | tfuel | Theta Fuel | checksummed-hex |
| 592 | grin | Grin | base58check |
| 714 | bnb | BNB | bech32 |
| 784 | sui | Sui | hex |
| 818 | vet | VeChain | checksummed-hex |
| 825 | hive | Hive | base58 + ripemd160-checksum |
| 888 | neo | NEO | base58check |
Expand Down
1 change: 1 addition & 0 deletions src/coders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export { decodeSteemAddress, encodeSteemAddress } from "./coin/steem.js";
export { decodeStratAddress, encodeStratAddress } from "./coin/strat.js";
export { decodeStrkAddress, encodeStrkAddress } from "./coin/strk.js";
export { decodeStxAddress, encodeStxAddress } from "./coin/stx.js";
export { decodeSuiAddress, encodeSuiAddress } from "./coin/sui.js";
export { decodeSysAddress, encodeSysAddress } from "./coin/sys.js";
export { decodeTfuelAddress, encodeTfuelAddress } from "./coin/tfuel.js";
export {
Expand Down
60 changes: 60 additions & 0 deletions src/coin/sui.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { hexToBytes } from "@noble/hashes/utils";
import { describe, expect, test } from "bun:test";
import { decodeSuiAddress, encodeSuiAddress } from "./sui.js";

describe.each([
// Test vectors from Sui documentation and examples
{
text: "0x21dcef5bbc5ec6d1789e8b92d3cb2c4d6855da09bd8197f8b256ca15714a7c47",
hex: "21dcef5bbc5ec6d1789e8b92d3cb2c4d6855da09bd8197f8b256ca15714a7c47",
},
{
text: "0x0000000000000000000000000000000000000000000000000000000000000001",
hex: "0000000000000000000000000000000000000000000000000000000000000001",
},
// Test case without 0x prefix
{
text: "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
hex: "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
},
])("sui address", ({ text, hex }) => {
test(`encode: ${text}`, () => {
expect(encodeSuiAddress(hexToBytes(hex))).toEqual(text.toLowerCase());
});
test(`decode: ${text}`, () => {
expect(decodeSuiAddress(text)).toEqual(hexToBytes(hex));
});
});

test("SUI decoding - incorrect length", () => {
expect(() =>
decodeSuiAddress("0x21dcef5bbc5ec6d1789e8b92d3cb2c4d6855da09bd8197f8b256ca15714a7c")
).toThrow("Unrecognised address format");
});

test("SUI decoding - invalid hex", () => {
expect(() =>
decodeSuiAddress("0x21dcef5bbc5ec6d1789e8b92d3cb2c4d6855da09bd8197f8b256ca15714a7c4g")
).toThrow("Unrecognised address format");
});

test("SUI encoding - incorrect length", () => {
expect(() =>
encodeSuiAddress(
hexToBytes("21dcef5bbc5ec6d1789e8b92d3cb2c4d6855da09bd8197f8b256ca15714a7c")
)
).toThrow("Unrecognised address format");
});

test("SUI decoding - without 0x prefix", () => {
const address = "21dcef5bbc5ec6d1789e8b92d3cb2c4d6855da09bd8197f8b256ca15714a7c47";
expect(decodeSuiAddress(address)).toEqual(hexToBytes(address));
});

test("SUI encoding/decoding - case insensitive input", () => {
const upperCaseAddress = "0x21DCEF5BBC5EC6D1789E8B92D3CB2C4D6855DA09BD8197F8B256CA15714A7C47";
const expectedBytes = hexToBytes("21dcef5bbc5ec6d1789e8b92d3cb2c4d6855da09bd8197f8b256ca15714a7c47");

expect(decodeSuiAddress(upperCaseAddress)).toEqual(expectedBytes);
expect(encodeSuiAddress(expectedBytes)).toEqual(upperCaseAddress.toLowerCase());
});
37 changes: 37 additions & 0 deletions src/coin/sui.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { CheckedCoin } from "../types.js";
import {
bytesToHex,
hexToBytes,
} from "../utils/bytes.js";
import { stripHexPrefix } from "../utils/hex.js";

const name = "sui";
const coinType = 784;

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

const encoded = bytesToHex(source);
return encoded.toLowerCase();
};

export const decodeSuiAddress = (source: string) => {
const stripped = stripHexPrefix(source);

// Check if the address has the correct format (64 hex chars)
if (!/^0x[a-fA-F0-9]{64}$/.test(source) && !/^[a-fA-F0-9]{64}$/.test(stripped)) {
throw new Error("Unrecognised address format");
}

const decoded = hexToBytes(`0x${stripped}` as const);
if (decoded.length !== 32) throw new Error("Unrecognised address format");

return decoded;
};

export const sui = {
name,
coinType,
encode: encodeSuiAddress,
decode: decodeSuiAddress,
} as const satisfies CheckedCoin;
1 change: 1 addition & 0 deletions src/coins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export { steem } from "./coin/steem.js";
export { strat } from "./coin/strat.js";
export { strk } from "./coin/strk.js";
export { stx } from "./coin/stx.js";
export { sui } from "./coin/sui.js";
export { sys } from "./coin/sys.js";
export { tfuel } from "./coin/tfuel.js";
export { thetaLegacy } from "./coin/thetaLegacy.js";
Expand Down
1 change: 1 addition & 0 deletions src/consts/coinTypeToNameMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export const nonEvmCoinTypeToNameMap = Object.freeze({
"592": ["grin", "Grin"],
"700": ["gnoLegacy", "[LEGACY] Gnosis"],
"714": ["bnb", "BNB"],
"784": ["sui", "Sui"],
"818": ["vet", "VeChain"],
"820": ["cloLegacy", "[LEGACY] Callisto"],
"825": ["hive", "Hive"],
Expand Down