EOSIO / eosjs2

Library to talk to the eos api
MIT License
29 stars 21 forks source link

Expose implementations for use with node #27

Closed jeffreyssmith2nd closed 6 years ago

jeffreyssmith2nd commented 6 years ago

This is necessary to substitute the TextEncoder/TextDecoder for use with nodejs. The Api must be extended and serializeTransaction and deserializeTransaction overwritten.

import { Api, SerialBuffer } from "eosjs2"
import { TextDecoder, TextEncoder } from "text-encoding"

export class MyEosjsApi extends Api {
  public serializeTransaction(transaction: any): Uint8Array {
    const textEncoder = new TextEncoder()
    const textDecoder = new TextDecoder()
    const buffer = new SerialBuffer({ textEncoder, textDecoder })
    this.serialize(buffer, "transaction", {
      max_net_usage_words: 0,
      max_cpu_usage_ms: 0,
      delay_sec: 0,
      context_free_actions: [],
      actions: [],
      transaction_extensions: [],
      ...transaction,
    })
    return buffer.asUint8Array()
  }

  public deserializeTransaction(transaction: Uint8Array): any {
    const textEncoder = new TextEncoder()
    const textDecoder = new TextDecoder()
    const buffer = new SerialBuffer({ textEncoder, textDecoder })
    buffer.pushArray(transaction)
    return this.deserialize(buffer, "transaction")
  }
}
tbfleming commented 6 years ago

It's ok to add optional textEncoder and textDecoder arguments to Api's constructor.

jeffreyssmith2nd commented 6 years ago

Added optional TextEncoder/TextDecoder to api. If the solution is acceptable, I'll remove the original commit

tbfleming commented 6 years ago

It is.