js-amino icon indicating copy to clipboard operation
js-amino copied to clipboard

unmarshalbinary not work for Types created with type

Open hikingpig opened this issue 4 years ago • 0 comments

Hi, I found it when trying to decode the PubKey from the example. Here's a reduced version used for reproducing the bug:

const { Codec, TypeFactory, Types } = require('js-amino')

let PubKeySecp256k1 = TypeFactory.create(
  'PubKeySecp256k1',
  [
    {
      name: 's',
      type: Types.ByteSlice
    }
  ],
  Types.ByteSlice
)

let codec = new Codec()
codec.registerConcrete(new PubKeySecp256k1(), 'tendermint/PubKeySecp256k1', {})

let pubKey0 = new PubKeySecp256k1([])

let pubKey = new PubKeySecp256k1([
  2,
  27,
  24,
  0,
  255,
  96,
  147,
  21,
  64,
  29,
  132,
  192,
  108,
  219,
  59,
  134,
  206,
  201,
  126,
  224,
  63,
  160,
  24,
  236,
  170,
  124,
  164,
  95,
  43,
  180,
  6,
  246,
  250
])

let pubKeyBz = codec.marshalBinary(pubKey)

codec.unMarshalBinary(pubKeyBz, pubKey0)

console.log('pubKey0 After decoded: ', JSON.stringify(pubKey0))

So in this case pubKey0 doesn't change.

when I remove the type in TypeFactory.create for pubKey, the unmarshalbinary works normally

const { Codec, TypeFactory, Types } = require('js-amino')

let PubKeySecp256k1 = TypeFactory.create(
  'PubKeySecp256k1',
  [
    {
      name: 's',
      type: Types.ByteSlice
    }
  ],
  // Types.ByteSlice
)

let codec = new Codec()
codec.registerConcrete(new PubKeySecp256k1(), 'tendermint/PubKeySecp256k1', {})

let pubKey0 = new PubKeySecp256k1([])

let pubKey = new PubKeySecp256k1([
  2,
  27,
  24,
  0,
  255,
  96,
  147,
  21,
  64,
  29,
  132,
  192,
  108,
  219,
  59,
  134,
  206,
  201,
  126,
  224,
  63,
  160,
  24,
  236,
  170,
  124,
  164,
  95,
  43,
  180,
  6,
  246,
  250
])

let pubKeyBz = codec.marshalBinary(pubKey)

codec.unMarshalBinary(pubKeyBz, pubKey0)

console.log('pubKey0 After decoded: ', JSON.stringify(pubKey0))

The question is how to decode when you have 'type' in TypeFactory.create?

hikingpig avatar Dec 19 '19 06:12 hikingpig