Skip to content

refactor: change Material.metadata from string to Uint8Array#178

Merged
lcovar merged 1 commit intomasterfrom
BTC-3085.metadata-uint8array
Feb 26, 2026
Merged

refactor: change Material.metadata from string to Uint8Array#178
lcovar merged 1 commit intomasterfrom
BTC-3085.metadata-uint8array

Conversation

@lcovar
Copy link
Copy Markdown
Contributor

@lcovar lcovar commented Feb 25, 2026

Why

Metadata is binary data (SCALE-encoded runtime metadata) but was stored as hex-encoded strings, requiring constant encoding/decoding at every boundary. Using Uint8Array eliminates this overhead:

  • Removes unnecessary hex encoding/decoding operations
  • Reduces memory usage (hex strings are 2x the size of raw bytes)
  • Improves type safety - makes it clear this is binary data, not text
  • Follows JavaScript/TypeScript conventions (Uint8Array is the standard for binary data)
  • Matches wasm-utxo package conventions (Uint8Array everywhere)
  • Improves performance by eliminating the encode/decode step

What Changed

TypeScript:

  • Material.metadata: changed from string to Uint8Array
  • Test fixtures: added hexToUint8Array() and getWestendMetadata() helpers

Rust:

  • Material.metadata: changed from String to Vec<u8>
  • MaterialJs::new(): accepts &[u8] instead of &str
  • Removed all decode_metadata() functions (no longer needed - bytes passed directly)
  • Added hex_or_bytes serde deserializer for backwards compatibility (handles hex strings from JSON tests and byte arrays from WASM)

Verification

  • ✅ All 30 TypeScript tests passing
  • ✅ All 28 Rust tests passing
  • ✅ cargo clippy clean
  • ✅ WASM module rebuilt and functional

Addresses Otto's PR #145 review feedback: "seriously consider Uint8Array"

Related to: BTC-3064

@lcovar lcovar force-pushed the BTC-3085.metadata-uint8array branch from c4e3f33 to facaa59 Compare February 25, 2026 22:12
Copy link
Copy Markdown
Contributor

@OttoAllmendinger OttoAllmendinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the rust code looks much better

the serde hex utility is more general than just metadata and probably deserves a more general name

Comment thread packages/wasm-dot/src/types.rs Outdated
pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
where
D: Deserializer<'de>,
{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can probably be more general "buffer from hex" or something

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the hex_or_bytes deserializer realized we don't actually need it. We're not using hex strings anywhere anymore

@lcovar lcovar force-pushed the BTC-3085.metadata-uint8array branch 2 times, most recently from e76ef1e to 3912ff0 Compare February 26, 2026 18:21
Base automatically changed from BTC-0.dot-wasm to master February 26, 2026 21:05
@lcovar lcovar marked this pull request as ready for review February 26, 2026 21:05
@lcovar lcovar requested a review from a team as a code owner February 26, 2026 21:05
metadata is binary data (SCALE-encoded runtime metadata) but was stored as hex strings, requiring constant encoding/decoding. using Uint8Array eliminates this overhead and improves type safety.

changes:
- TypeScript: Material.metadata is now Uint8Array instead of string
- Rust: Material.metadata is now Vec<u8> instead of String
- MaterialJs WASM wrapper: accepts &[u8] instead of &str
- removed all decode_metadata functions (no longer needed)
- test fixtures: added hexToUint8Array() and getWestendMetadata() helpers
- no custom serde deserializer needed - standard serde handles byte arrays and Uint8Array

benefits:
- eliminates hex encode/decode overhead at every use
- reduces memory usage (hex strings are 2x the size of raw bytes)
- improves type safety (Uint8Array clearly indicates binary data)
- matches wasm-utxo conventions (Uint8Array everywhere)
- simpler - clients forced to pass bytes, tests use bytes, code accepts bytes

BTC-3085

Signed-off-by: Luis Covarrubias <luiscovarrubias@bitgo.com>
@lcovar lcovar force-pushed the BTC-3085.metadata-uint8array branch from 3912ff0 to 4c92b2a Compare February 26, 2026 21:07
@lcovar lcovar merged commit fb3f392 into master Feb 26, 2026
12 checks passed
@lcovar lcovar deleted the BTC-3085.metadata-uint8array branch February 26, 2026 21:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants