ethereumjs / organization

A repo for discussions and other non-code organizing stuff
https://ethereumjs.readthedocs.io
12 stars 17 forks source link

Tracking issue: Move from Buffer -> Uint8Array #69

Closed jochem-brouwer closed 2 days ago

jochem-brouwer commented 3 years ago

As mentioned by @ricmoo, the nodejs injected Buffer is insecure and also makes adds a browser dependency of a Buffer implementation. It seems that for (most of?) our use cases, we should use Uint8Array and not Buffer, ideally we remove this "dependency" from our packages.

Tracking issue as this is a long-term issue.

rumkin commented 3 years ago

Uint8Array could be used with custom types for Address with ReadOnly util to prevent buffer from modifications in TS:

type ReadOnly<T> = {
    readonly [i: number]: T
}

type Address = ReadOnly<Uint8Array>

Or there could be realized some primitive ancestors of Uint8Array:

class Address extends Uint8Array {
    readonly [n: number]: number;

    static fromString(source: string): Address {
      // ...
    }
}

The first option is type-only solution, what means it costs nothing at runtime. The second allows to implement some utility methods and do instanceof check, but it has some overhead in runtime.