(parameters: InputParameter[])
| 39 | } |
| 40 | |
| 41 | export function encode(parameters: InputParameter[]): string { |
| 42 | const types = parameters.map((parameter) => parameter.type) as ParameterType[]; |
| 43 | |
| 44 | // Each parameter name is represented by a `bytes32` string. The value |
| 45 | // types are what the user provides |
| 46 | const nameTypePairs = flatMap(types, (type) => { |
| 47 | const transformedType = TYPE_TRANSFORMATIONS[type]; |
| 48 | return ['bytes32', transformedType ?? type]; |
| 49 | }); |
| 50 | |
| 51 | // The first type is always a bytes32 as it represents the schema header |
| 52 | const allTypes = ['bytes32', ...nameTypePairs]; |
| 53 | |
| 54 | // Build the schema which includes the version and the abbreviated list of parameters |
| 55 | const schemaHeader = buildSchemaHeader(types as ParameterType[]); |
| 56 | const encodedHeader = ethers.utils.formatBytes32String(schemaHeader); |
| 57 | |
| 58 | // Map and encode each name/value pair where necessary |
| 59 | const flatNameValues = buildNameValuePairs(parameters); |
| 60 | |
| 61 | // The schema header is always the first value to be encoded |
| 62 | const allValues = [encodedHeader, ...flatNameValues]; |
| 63 | |
| 64 | const encoder = new ethers.utils.AbiCoder(); |
| 65 | return encoder.encode(allTypes, allValues); |
| 66 | } |
no test coverage detected