import { Hasher } from 'multiformats/hashes/hasher'; import CID from 'multiformats/cid' import { keccak256 } from './hashers' export interface EncodedDatum { cid: CID; content: Uint8Array; } // EncodeWith is the public interface for encoding data, it's templated to take data in a format and function to encode it and hash it, // it returns the contentId and encodedData in an array export interface Encoder { (data: any): Promise; } // DataEncoder specifies the definition for a data encoding function export interface DataEncoder { (data: T): Promise; } // EncodeWith is a type for defining the composition of encoding functions export interface EncodeWith { (data: T, dataEncoder: DataEncoder, hasher: Hasher): Promise } /* getEncoder composes an encoding function that takes data and returns a function single parameter function that can be used to encode data when writing to storage example usage export const binMemoryEncoder = getEncoder(inMemoryDataEncoder, binaryDataEncoder) storage.put("mydata", binMemoryEncoder) */ export function getEncoder(encoder: EncodeWith, dataEncoder: DataEncoder): Encoder { return async (data) => { return encoder(data, dataEncoder, keccak256) } }