Skip to content

Refactor repetitive [u8; 32] conversion pattern in token info code #2493

@coderabbitai

Description

@coderabbitai

Issue Description

Currently, there is a repetitive pattern in the code where we convert various fields to [u8; 32] arrays, for example:

<[u8; 32]>::try_from(v0.identity_id).map_err(|_| Error::RequestError {
    error: "can't convert identity_id to [u8; 32]".to_string(),
})?;

and

let token_ids = v0
    .token_ids
    .into_iter()
    .map(<[u8; 32]>::try_from)
    .collect::<Result<Vec<_>, _>>()
    .map_err(|_| Error::RequestError {
        error: "can't convert token_id to [u8; 32]".to_string(),
    })?;

This pattern appears in multiple places throughout the codebase, particularly in token info and related modules.

Proposed Solution

Create a helper method such as:

fn get_bytes<T>(input: T) -> Result<[u8; 32], Error> 
where 
    T: TryInto<[u8; 32]> 
{
    input.try_into().map_err(|_| Error::RequestError {
        error: "can't convert to [u8; 32]".to_string(),
    })
}

This would allow us to simplify the conversion code to:

let identity_id = get_bytes(v0.identity_id)?;

Origin

This issue was created following a discussion in PR #2449: #2449 (comment)

Metadata

Metadata

Labels

No labels
No labels

Type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions