ecadlabs / taquito

A library for building dApps on the Tezos Blockchain - JavaScript / TypeScript
https://taquito.io
Apache License 2.0
298 stars 116 forks source link

Accept hex prefixed with 0x as well #1624

Closed jchenche closed 2 years ago

jchenche commented 2 years ago

Description Right now, Taquito only accepts hex bytes without the 0x prefix. But we want to support both formats, with or without 0x.

Steps To Reproduce Steps to reproduce the behavior:

  1. Deploy the following contract:

    parameter %abyte bytes;
    storage bytes;
    code { CAR ;
       PUSH nat 3;
       PUSH nat 1;
       SLICE ;
       IF_NONE { FAIL } {};
       NIL operation ;
       PAIR }
  2. Call an entrypoint with the following code (omitting the setups): Note that contract KT1MwH7SqQgfod9Lmsu9dqFmY3SBASEiEjku is already deployed to Ithaca.

    Tezos.contract
    .at('KT1MwH7SqQgfod9Lmsu9dqFmY3SBASEiEjku')
    .then((contract) => {
    // console.log(util.inspect(contract.script, false, null, true))
    return contract.methods.default("0x12a47ef2").send()
    })
    .then((op) => {
    console.log(`Waiting for ${op.hash} to be confirmed...`);
    return op.confirmation(3).then(() => op.hash);
    })
    .then((hash) => console.log(`Operation injected: https://ithaca.tzstats.com/${hash}`))
    .catch((error) => console.log(`Error: ${error}`));

Current behaviour It will give an "Invalid bytes" error.

Expected behavior Calling an entrypoint should succeed and after calling the entrypoint, if you go and inspect the storage using TzKT, it should be a47ef2.

Extra Code responsible for this:

  private isValid(val: any): BytesValidationError | null {
    if (typeof val === 'string' && /^[0-9a-fA-F]*$/.test(val) && val.length % 2 === 0) {
      return null;
    } else {
      return new BytesValidationError(val, this, `Invalid bytes: ${val}`);
    }
  }

from file taquito/packages/taquito-michelson-encoder/src/tokens/comparable/bytes.ts

roxaneletourneau commented 2 years ago

Hey team! Please add your planning poker estimate with ZenHub @dsawali @zainen @hui-an-yang