Commit 9336bdb
fix(sdk): reject unpaired surrogates before signing, and correct the length-prefix docs
Review findings 2 and 5 on #4259.
The Kotlin and Swift docs described the digest's length prefix in each
language's own string units — `message.length` (UTF-16 code units) and
`message.count` (grapheme clusters). The format prefixes the UTF-8 BYTE
count; the three agree only for ASCII. Corrected to
`message.toByteArray(Charsets.UTF_8).size` and `message.utf8.count`.
That doc bug has a real counterpart: a Kotlin `String` is an unvalidated
UTF-16 sequence and may hold an unpaired surrogate, which has no UTF-8
encoding. Every conversion below is lenient and they do not even agree —
the JNI bridge's string read substitutes U+FFFD, while
`toByteArray(Charsets.UTF_8)` substitutes '?' (verified on JDK 17). So
the wallet would sign bytes the caller never wrote and return a signature
that verifies for a different message, silently, and "verbatim" in the
docs would be false.
Rejected at the Kotlin entry point, the last layer that still holds the
exact UTF-16 and can explain why. Marshalling a UTF-8 ByteArray across
the JNI instead — the other option raised in review — would NOT fix this:
Kotlin's own encoder is equally lossy, so it would relocate the silent
substitution and change it from U+FFFD to '?' while widening the FFI
surface. Well-formed strings are unaffected. The lossy `get_string`
read is the shared `read_cstring_required` behaviour on every JNI string
parameter in the crate, so it is a codebase-wide convention rather than
something this path introduced; flagged rather than changed here.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>1 parent bf3a588 commit 9336bdb
2 files changed
Lines changed: 52 additions & 6 deletions
File tree
- packages
- kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/wallet
- swift-sdk/Sources/SwiftDashSDK/PlatformWallet/CoreWallet
packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/wallet/ManagedPlatformWallet.kt
Lines changed: 47 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
180 | 180 | | |
181 | 181 | | |
182 | 182 | | |
183 | | - | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
184 | 187 | | |
185 | 188 | | |
186 | 189 | | |
| |||
223 | 226 | | |
224 | 227 | | |
225 | 228 | | |
226 | | - | |
227 | | - | |
228 | | - | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
229 | 236 | | |
230 | 237 | | |
231 | 238 | | |
| |||
237 | 244 | | |
238 | 245 | | |
239 | 246 | | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
240 | 259 | | |
241 | 260 | | |
242 | 261 | | |
| |||
245 | 264 | | |
246 | 265 | | |
247 | 266 | | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
248 | 291 | | |
249 | 292 | | |
250 | 293 | | |
| |||
Lines changed: 5 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
142 | 142 | | |
143 | 143 | | |
144 | 144 | | |
145 | | - | |
146 | | - | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
147 | 150 | | |
148 | 151 | | |
149 | 152 | | |
| |||
0 commit comments