@@ -61,9 +61,6 @@ class Udi {
61
61
}
62
62
63
63
static convertToHex ( input : string ) : string {
64
- // Attempt hex conversion first
65
- if ( / ^ [ a - f A - F 0 - 9 ] { 64 } $ / . test ( input ) ) return input ;
66
-
67
64
// Convert from Base32
68
65
try {
69
66
const paddedInput = Udi . addBase32Padding ( input . toUpperCase ( ) ) ;
@@ -73,13 +70,11 @@ class Udi {
73
70
console . warn ( "Base32 decoding failed, trying Base64 encoding..." ) ;
74
71
}
75
72
76
- // Convert from Base64
77
- try {
78
- const standardBase64 = Udi . addBase64Padding ( Udi . toStandardBase64 ( input ) ) ;
79
- const buffer = Buffer . from ( standardBase64 , "base64" ) ;
80
- return buffer . toString ( "hex" ) ;
81
- } catch ( e ) {
82
- throw new Error ( "Invalid input encoding. Must be 32-byte hex, Base32, or Base64 string." ) ;
73
+ // Attempt hex conversion first
74
+ if ( / ^ [ a - f A - F 0 - 9 ] { 64 } $ / . test ( input ) ) {
75
+ return input ;
76
+ } else {
77
+ throw new Error ( "Input must be a 64-character hex string." ) ;
83
78
}
84
79
}
85
80
@@ -88,21 +83,15 @@ class Udi {
88
83
return input + "=" . repeat ( paddingNeeded ) ;
89
84
}
90
85
91
- static toStandardBase64 ( base64UrlSafe : string ) : string {
92
- return base64UrlSafe . replace ( / - / g, "+" ) . replace ( / _ / g, "/" ) ;
93
- }
94
-
95
- static addBase64Padding ( base64 : string ) : string {
96
- const paddingNeeded = ( 4 - ( base64 . length % 4 ) ) % 4 ;
97
- return base64 + "=" . repeat ( paddingNeeded ) ;
98
- }
99
-
100
- toUrn ( encoding : "hex" | "base32" | "base64" = "hex" ) : string {
86
+ toUrn ( encoding : "hex" | "base32" = "hex" ) : string {
101
87
const storeIdStr = this . formatBufferAsEncoding ( this . _storeIdHex , encoding ) ;
102
88
let urn = `${ Udi . namespace } :${ this . chainName } :${ storeIdStr } ` ;
103
89
104
90
if ( this . _rootHashHex ) {
105
- const rootHashStr = this . formatBufferAsEncoding ( this . _rootHashHex , encoding ) ;
91
+ const rootHashStr = this . formatBufferAsEncoding (
92
+ this . _rootHashHex ,
93
+ encoding
94
+ ) ;
106
95
urn += `:${ rootHashStr } ` ;
107
96
}
108
97
@@ -113,22 +102,19 @@ class Udi {
113
102
return urn ;
114
103
}
115
104
116
- private formatBufferAsEncoding ( hexString : string , encoding : "hex" | "base32" | "base64" ) : string {
105
+ private formatBufferAsEncoding (
106
+ hexString : string ,
107
+ encoding : "hex" | "base32"
108
+ ) : string {
117
109
const buffer = Buffer . from ( hexString , "hex" ) ;
118
110
if ( encoding === "hex" ) {
119
111
return hexString ;
120
112
} else if ( encoding === "base32" ) {
121
113
return base32Encode ( buffer ) . replace ( / = + $ / , "" ) ; // Strip padding for Base32
122
- } else if ( encoding === "base64" ) {
123
- return Udi . toBase64UrlSafe ( buffer . toString ( "base64" ) ) ; // Convert to URL-safe Base64
124
114
}
125
115
throw new Error ( "Unsupported encoding type" ) ;
126
116
}
127
117
128
- static toBase64UrlSafe ( base64Standard : string ) : string {
129
- return base64Standard . replace ( / \+ / g, "-" ) . replace ( / \/ / g, "_" ) . replace ( / = + $ / , "" ) ;
130
- }
131
-
132
118
equals ( other : Udi ) : boolean {
133
119
return (
134
120
this . _storeIdHex === other . _storeIdHex &&
@@ -145,7 +131,12 @@ class Udi {
145
131
}
146
132
147
133
clone ( ) : Udi {
148
- return new Udi ( this . chainName , this . _storeIdHex , this . _rootHashHex , this . resourceKey ) ;
134
+ return new Udi (
135
+ this . chainName ,
136
+ this . _storeIdHex ,
137
+ this . _rootHashHex ,
138
+ this . resourceKey
139
+ ) ;
149
140
}
150
141
151
142
hashCode ( ) : string {
@@ -167,15 +158,9 @@ class Udi {
167
158
}
168
159
169
160
get rootHashBase32 ( ) : string | null {
170
- return this . _rootHashHex ? this . formatBufferAsEncoding ( this . _rootHashHex , "base32" ) : null ;
171
- }
172
-
173
- get storeIdBase64 ( ) : string {
174
- return this . formatBufferAsEncoding ( this . _storeIdHex , "base64" ) ;
175
- }
176
-
177
- get rootHashBase64 ( ) : string | null {
178
- return this . _rootHashHex ? this . formatBufferAsEncoding ( this . _rootHashHex , "base64" ) : null ;
161
+ return this . _rootHashHex
162
+ ? this . formatBufferAsEncoding ( this . _rootHashHex , "base32" )
163
+ : null ;
179
164
}
180
165
}
181
166
0 commit comments