near-api-js icon indicating copy to clipboard operation
near-api-js copied to clipboard

Array deserialization

Open mohitxskull opened this issue 2 years ago • 5 comments
trafficstars

Prerequisites

  • [X] I'm using the latest version of near-api-js.
  • [X] I have tried to start with a fresh project and reproduce the defect with minimal code changes.
  • [X] I have read the console error messages carefully (if applicable).

Description

i have an function on a smart contract which accepts an argument Vec<TokenMetadata> from my node.js backend i am sending an array, and i am getting an errorsaying

ExecutionError: Smart contract panicked: panicked at 'Failed to deserialize input from JSON.: Error("Invalid last symbol 51, offset 29.", line: 1, column: 185)', nft/src/lib.rs:91:1

Reproducible demo

https://github.com/servedbyskull/near-api-js-error

Steps to reproduce

clone the repo yarn yarn build yarn start

Expected behavior

Should run perfectly

Actual behavior

Throwed an error

Your environment

  • NEAR JavaScript API version used: 1.1.0
  • Frontend framework (if applicable):
  • Relevant dependencies (if applicable):

Self-service

  • [ ] I'd be willing to fix this bug myself.

mohitxskull avatar Jan 26 '23 12:01 mohitxskull

Thank you for the detailed description and examples @servedbyskull !

The transaction data appears to be serialized properly in near-api-js since I can deserialize the transaction before it's sent to get the original input. I think the next step would be to try this in the JS and Rust CLIs to see if the behavior is any different there - I'd expect the JS CLI to throw the same error but the Rust CLI uses a different serialization wrapper AFAIK.

andy-haynes avatar Jan 26 '23 22:01 andy-haynes

Thank you for the detailed description and examples @servedbyskull !

The transaction data appears to be serialized properly in near-api-js since I can deserialize the transaction before it's sent to get the original input. I think the next step would be to try this in the JS and Rust CLIs to see if the behavior is any different there - I'd expect the JS CLI to throw the same error but the Rust CLI uses a different serialization wrapper AFAIK.

err

Tried with JS Cli, same error

err

Tried with Rust Cli, same error

mohitxskull avatar Jan 27 '23 08:01 mohitxskull

Looking up that Invalid last symbol text I see various Rust libraries with the same code around this error message (disclaimer: I don't speak Rust or know whether transaction serialization relies on this same logic):

    /// The last non-padding input symbol's encoded 6 bits have nonzero bits that will be discarded.
    /// This is indicative of corrupted or truncated Base64.
    /// Unlike InvalidByte, which reports symbols that aren't in the alphabet, this error is for
    /// symbols that are in the alphabet but represent nonsensical encodings.
    InvalidLastSymbol(usize, u8),

...

            DecodeError::InvalidLastSymbol(index, byte) => {
                write!(f, "Invalid last symbol {}, offset {}.", byte, index)
            }

It's interesting that the "last symbol" byte mentioned in the error in your example code is 51 but in the CLI examples it's 117. I can't seem to get a value other than 51 by changing the parameters in the example repo so I'm not sure what could account for the disparity there.

This isn't an issue specific to near-api-js but I'm not sure where it would be best transferred. I can keep the issue open until I have a better place to put it but in the meantime I would try DevRel Office Hours or Discord - they should know who to escalate your issue to.

andy-haynes avatar Jan 27 '23 19:01 andy-haynes

Any update?

mohitxskull avatar Feb 28 '23 08:02 mohitxskull

Did you follow up with DevRel or on Discord?

andy-haynes avatar Feb 28 '23 19:02 andy-haynes