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 fcee836
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 28 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
34 changes: 33 additions & 1 deletion src/lib/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ import { isDynamicKeyName } from './encodeKeyName';
import { decodeKey } from './decodeData';

describe('utils', () => {
describe('encodeKey/decodeKey', () => {
describe.only('encodeKey/decodeKey', () => {
const testCases = [
// test encoding an array of address
{
schema: {
name: 'LSP3IssuedAssets[]',
Expand Down Expand Up @@ -218,10 +219,25 @@ describe('utils', () => {

testCases.forEach((testCase) => {
it(`encodes/decodes keyType Array / tuples (valueContent: ${testCase.schema.valueContent}, valueType: ${testCase.schema.valueType}`, () => {
console.log(
'encodeKey(testCase.schema as ERC725JSONSchema, testCase.decodedValue)',
);
console.log(
encodeKey(testCase.schema as ERC725JSONSchema, testCase.decodedValue),
);

assert.deepStrictEqual(
encodeKey(testCase.schema as ERC725JSONSchema, testCase.decodedValue),
testCase.encodedValue,
);

console.log(
'decodeKey(testCase.schema as ERC725JSONSchema, testCase.encodedValue)',
);
console.log(
decodeKey(testCase.schema as ERC725JSONSchema, testCase.encodedValue),
);

assert.deepStrictEqual(
decodeKey(testCase.schema as ERC725JSONSchema, testCase.encodedValue),
testCase.decodedValue,
Expand Down Expand Up @@ -397,6 +413,22 @@ describe('utils', () => {
assert.strictEqual(encodeArrayKey(key, index), expectedValue);
});
});

it('should encode the array length only if passing a number', async () => {
// test encoding only the length
const schema: ERC725JSONSchema = {
name: 'LSP3IssuedAssets[]',
key: '0x3a47ab5bd3a594c3a8995f8fa58d0876c96819ca4516bd76100c92462f2f9dc0',
keyType: 'Array',
valueContent: 'Address',
valueType: 'address',
};

const decodedValue = 3;
const encodedValue = '0x00000000000000000000000000000003';

assert.equal(encodeKey(schema, decodedValue), encodedValue);
});
});

describe('encodeData', () => {
Expand Down
5 changes: 5 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ export function encodeKey(

switch (lowerCaseKeyType) {
case 'array': {
// if we are encoding only the Array length
if (typeof value === 'number') {
return encodeValueType('uint128', value);
}

if (!Array.isArray(value)) {
console.error("Can't encode a non array for key of type array");
return null;
Expand Down

0 comments on commit fcee836

Please sign in to comment.