Skip to content

Commit

Permalink
feat(wip): allow to encode LSP2 Array length only
Browse files Browse the repository at this point in the history
  • Loading branch information
CJ42 committed Oct 25, 2023
1 parent 9f1b5fd commit b2c62fa
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 172 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export class ERC725 {
}

/**
* To prevent weird behovior from the lib, we must make sure all the schemas are correct before loading them.
* To prevent weird behavior from the lib, we must make sure all the schemas are correct before loading them.
*
* @param schemas
* @returns
Expand Down
37 changes: 11 additions & 26 deletions src/lib/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,30 +324,6 @@ const returnTypesOfUintNCompactBytesArray = () => {
return types;
};

/**
* Encodes any set of strings to string[CompactBytesArray]
*
* @param values An array of non restricted strings
* @returns string[CompactBytesArray]
*/
const encodeStringCompactBytesArray = (values: string[]): string => {
const hexValues: string[] = values.map((element) => utf8ToHex(element));

return encodeCompactBytesArray(hexValues);
};

/**
* Decode a string[CompactBytesArray] to an array of strings
* @param compactBytesArray A string[CompactBytesArray]
* @returns An array of strings
*/
const decodeStringCompactBytesArray = (compactBytesArray: string): string[] => {
const hexValues: string[] = decodeCompactBytesArray(compactBytesArray);
const stringValues: string[] = hexValues.map((element) => hexToUtf8(element));

return stringValues;
};

const valueTypeEncodingMap = {
bool: {
encode: (value: boolean) => (value ? '0x01' : '0x00'),
Expand Down Expand Up @@ -486,8 +462,17 @@ const valueTypeEncodingMap = {
decode: (value: string) => decodeCompactBytesArray(value),
},
'string[CompactBytesArray]': {
encode: (value: string[]) => encodeStringCompactBytesArray(value),
decode: (value: string) => decodeStringCompactBytesArray(value),
encode: (values: string[]) => {
const hexValues: string[] = values.map((element) => utf8ToHex(element));
return encodeCompactBytesArray(hexValues);
},
decode: (value: string) => {
const hexValues: string[] = decodeCompactBytesArray(value);
const stringValues: string[] = hexValues.map((element) =>
hexToUtf8(element),
);
return stringValues;
},
},
...returnTypesOfBytesNCompactBytesArray(),
...returnTypesOfUintNCompactBytesArray(),
Expand Down
Loading

0 comments on commit b2c62fa

Please sign in to comment.