Last active
March 29, 2019 16:50
-
-
Save Gozala/0d653ee628abd15ccfc117605b2c0d6d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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