aeternity / aepp-calldata-js

Aeternity data serialization library
ISC License
3 stars 4 forks source link

Export api to encode value to fate bytecode #216

Closed davidyuk closed 1 year ago

davidyuk commented 1 year ago

For example

  const recordAci = {
    record: [{
      name: 'operation',
      type: 'string',
    }, {
      name: 'parameter',
      type: 'int',
    }],
  } as const;
  const recordDataDecoded = {
    operation: 'test',
    parameter: 42,
  };

I want to get the corresponding fate-encoded bytecode ('cb_KxF0ZXN0VANAuWU=') for my implementation of EIP-712 🙄

Currently, I'm doing it this way

import ContractByteArrayEncoder from '@aeternity/aepp-calldata/src/ContractByteArrayEncoder.js';
import AciTypeResolver from '@aeternity/aepp-calldata/src/AciTypeResolver.js';

export function encodeFateValue(value, aci) {
  const contractByteArrayEncoder = new ContractByteArrayEncoder();
  const aciTypeResolver = new AciTypeResolver([]);
  aciTypeResolver.isCustomType = () => false;
  return contractByteArrayEncoder.encode(aciTypeResolver.resolveType(aci), value);
}
davidyuk commented 1 year ago

There is the same issue with decoding, I see only typeless decoding exported https://github.com/aeternity/aepp-calldata-js/blob/82b5a98f9b308482627da8d7484d213e9cf87151/src/api/ContractByteArrayEncoder.js#L27

by the way, shouldn't decodeWithType accept arguments in the same order as encode? 🙃 https://github.com/aeternity/aepp-calldata-js/blob/82b5a98f9b308482627da8d7484d213e9cf87151/src/ContractByteArrayEncoder.js#L80

dincho commented 1 year ago

Any proposal for the API? I prefer to stick to the ACI interface/protocol as much as possible.

davidyuk commented 1 year ago

I prefer to stick to the ACI interface/protocol as much as possible.

Sure, it should accept a subset of ACI defining a value to encode/decode.

Other than that a would prefer a functional approach 🙄

dincho commented 1 year ago

@davidyuk please take a look #223 if that suits your needs

This library follows basic OOP approach for it's public API.