1+ import { hexToBytes } from "@noble/hashes/utils" ;
2+ import { describe , expect , test } from "bun:test" ;
3+ import { decodeSuiAddress , encodeSuiAddress } from "./sui.js" ;
4+
5+ describe . each ( [
6+ // Test vectors from Sui documentation and examples
7+ {
8+ text : "0x21dcef5bbc5ec6d1789e8b92d3cb2c4d6855da09bd8197f8b256ca15714a7c47" ,
9+ hex : "21dcef5bbc5ec6d1789e8b92d3cb2c4d6855da09bd8197f8b256ca15714a7c47" ,
10+ } ,
11+ {
12+ text : "0x0000000000000000000000000000000000000000000000000000000000000001" ,
13+ hex : "0000000000000000000000000000000000000000000000000000000000000001" ,
14+ } ,
15+ // Test case without 0x prefix
16+ {
17+ text : "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef" ,
18+ hex : "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef" ,
19+ } ,
20+ ] ) ( "sui address" , ( { text, hex } ) => {
21+ test ( `encode: ${ text } ` , ( ) => {
22+ expect ( encodeSuiAddress ( hexToBytes ( hex ) ) ) . toEqual ( text . toLowerCase ( ) ) ;
23+ } ) ;
24+ test ( `decode: ${ text } ` , ( ) => {
25+ expect ( decodeSuiAddress ( text ) ) . toEqual ( hexToBytes ( hex ) ) ;
26+ } ) ;
27+ } ) ;
28+
29+ test ( "SUI decoding - incorrect length" , ( ) => {
30+ expect ( ( ) =>
31+ decodeSuiAddress ( "0x21dcef5bbc5ec6d1789e8b92d3cb2c4d6855da09bd8197f8b256ca15714a7c" )
32+ ) . toThrow ( "Unrecognised address format" ) ;
33+ } ) ;
34+
35+ test ( "SUI decoding - invalid hex" , ( ) => {
36+ expect ( ( ) =>
37+ decodeSuiAddress ( "0x21dcef5bbc5ec6d1789e8b92d3cb2c4d6855da09bd8197f8b256ca15714a7c4g" )
38+ ) . toThrow ( "Unrecognised address format" ) ;
39+ } ) ;
40+
41+ test ( "SUI encoding - incorrect length" , ( ) => {
42+ expect ( ( ) =>
43+ encodeSuiAddress (
44+ hexToBytes ( "21dcef5bbc5ec6d1789e8b92d3cb2c4d6855da09bd8197f8b256ca15714a7c" )
45+ )
46+ ) . toThrow ( "Unrecognised address format" ) ;
47+ } ) ;
48+
49+ test ( "SUI decoding - without 0x prefix" , ( ) => {
50+ const address = "21dcef5bbc5ec6d1789e8b92d3cb2c4d6855da09bd8197f8b256ca15714a7c47" ;
51+ expect ( decodeSuiAddress ( address ) ) . toEqual ( hexToBytes ( address ) ) ;
52+ } ) ;
53+
54+ test ( "SUI encoding/decoding - case insensitive input" , ( ) => {
55+ const upperCaseAddress = "0x21DCEF5BBC5EC6D1789E8B92D3CB2C4D6855DA09BD8197F8B256CA15714A7C47" ;
56+ const expectedBytes = hexToBytes ( "21dcef5bbc5ec6d1789e8b92d3cb2c4d6855da09bd8197f8b256ca15714a7c47" ) ;
57+
58+ expect ( decodeSuiAddress ( upperCaseAddress ) ) . toEqual ( expectedBytes ) ;
59+ expect ( encodeSuiAddress ( expectedBytes ) ) . toEqual ( upperCaseAddress . toLowerCase ( ) ) ;
60+ } ) ;
0 commit comments