Skip to content

Instantly share code, notes, and snippets.

@Gozala
Last active March 29, 2019 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gozala/0d653ee628abd15ccfc117605b2c0d6d to your computer and use it in GitHub Desktop.
Save Gozala/0d653ee628abd15ccfc117605b2c0d6d to your computer and use it in GitHub Desktop.
interface Format<a> {
format:Format;
defaultHashAlgorithm: string;
encode(a):Promise<Bytes<a>>;
decode(Bytes<a>):Promise<a>;
}
opaque type Bytes<a>:Uint8Array = Uint8Array
type BlockData<a> = Concat<Format, Bytes<a>>
opaque type Concat<a:Uint8Array, b:Uint8Array>:Uint8Array = Uint8Array
class Codec<a> {
provider:Format<a>;
constructor(provider:Format<a>) {
this.provider = provider
}
async toBlock(data:a):Promise<BlockData<a>> {
const bytes = await this.provider.encode(data)
return multicodec.addPrefix(this.format, bytes)
}
fromBlock(block:BlockData<a>):Promise<a> {
const bytes = multicodec.rmPrefix(block)
return this.provider.decode(bytes)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment