Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client: Support non-8-byte discriminators #3125

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- lang: Update `dispatch` function to support dynamic discriminators ([#3104](https://github.com/coral-xyz/anchor/pull/3104)).
- lang: Remove the fallback function shortcut in `try_entry` function ([#3109](https://github.com/coral-xyz/anchor/pull/3109)).
- ts: Get discriminator lengths dynamically ([#3120](https://github.com/coral-xyz/anchor/pull/3120)).
- client: Support non-8-byte discriminators ([#3125](https://github.com/coral-xyz/anchor/pull/3125)).

### Fixes

Expand Down
25 changes: 10 additions & 15 deletions client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,28 +373,23 @@ pub fn handle_program_log<T: anchor_lang::Event + anchor_lang::AnchorDeserialize
.strip_prefix(PROGRAM_LOG)
.or_else(|| l.strip_prefix(PROGRAM_DATA))
{
let borsh_bytes = match STANDARD.decode(log) {
Ok(borsh_bytes) => borsh_bytes,
let log_bytes = match STANDARD.decode(log) {
Ok(log_bytes) => log_bytes,
_ => {
#[cfg(feature = "debug")]
println!("Could not base64 decode log: {}", log);
return Ok((None, None, false));
}
};

let mut slice: &[u8] = &borsh_bytes[..];
let disc: [u8; 8] = {
let mut disc = [0; 8];
disc.copy_from_slice(&borsh_bytes[..8]);
slice = &slice[8..];
disc
};
let mut event = None;
if disc == T::discriminator() {
let e: T = anchor_lang::AnchorDeserialize::deserialize(&mut slice)
.map_err(|e| ClientError::LogParseError(e.to_string()))?;
event = Some(e);
}
let event = log_bytes
.starts_with(T::DISCRIMINATOR)
.then(|| {
let mut data = &log_bytes[T::DISCRIMINATOR.len()..];
T::deserialize(&mut data).map_err(|e| ClientError::LogParseError(e.to_string()))
})
.transpose()?;

Ok((event, None, false))
}
// System log.
Expand Down
Loading