From b1e0ed98718acce90f1db6917728394ab8aeb4fa Mon Sep 17 00:00:00 2001 From: CJ42 Date: Wed, 17 Apr 2024 20:15:24 +0100 Subject: [PATCH] feat: export methods for encoding/decoding dataSourceWithHash and valueContent --- src/index.ts | 66 ++++++++++++++++++++++++++++++++++++++++++++-- src/lib/encoder.ts | 4 +-- 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 948db4ff..fb8cab52 100644 --- a/src/index.ts +++ b/src/index.ts @@ -58,10 +58,19 @@ import { decodeData } from './lib/decodeData'; import { getDataFromExternalSources } from './lib/getDataFromExternalSources'; import { DynamicKeyPart, DynamicKeyParts } from './types/dynamicKeys'; import { getData } from './lib/getData'; -import { decodeValueType, encodeValueType } from './lib/encoder'; +import { + encodeDataSourceWithHash, + decodeDataSourceWithHash, + encodeValueType, + decodeValueType, + encodeValueContent, + decodeValueContent, +} from './lib/encoder'; import { internalSupportsInterface, checkPermissions } from './lib/detector'; import { decodeMappingKey } from './lib/decodeMappingKey'; import { encodePermissions, decodePermissions } from './lib/permissions'; +import { AssetURLEncode } from './types/encodeData'; +import { URLDataToEncode, URLDataWithHash, Verification } from './types'; export { ERC725JSONSchema, @@ -77,9 +86,12 @@ export { decodeData } from './lib/decodeData'; export { encodeKeyName, isDynamicKeyName } from './lib/encodeKeyName'; export { decodeMappingKey } from './lib/decodeMappingKey'; export { + encodeDataSourceWithHash, + decodeDataSourceWithHash, + encodeValueType, decodeValueType, + encodeValueContent, decodeValueContent, - encodeValueType, } from './lib/encoder'; export { getDataFromExternalSources } from './lib/getDataFromExternalSources'; export { encodePermissions, decodePermissions } from './lib/permissions'; @@ -646,6 +658,28 @@ export class ERC725 { return checkPermissions(requiredPermissions, grantedPermissions); } + encodeDataSourceWithHash( + verification: undefined | Verification, + dataSource: string, + ): string { + return encodeDataSourceWithHash(verification, dataSource); + } + + static encodeDataSourceWithHash( + verification: undefined | Verification, + dataSource: string, + ): string { + return encodeDataSourceWithHash(verification, dataSource); + } + + decodeDataSourceWithHash(value: string): URLDataWithHash { + return decodeDataSourceWithHash(value); + } + + static decodeDataSourceWithHash(value: string): URLDataWithHash { + return decodeDataSourceWithHash(value); + } + /** * @param type The valueType to encode the value as * @param value The value to encode @@ -677,6 +711,34 @@ export class ERC725 { decodeValueType(type: string, data: string) { return decodeValueType(type, data); } + + static encodeValueContent( + valueContent: string, + value: string | number | AssetURLEncode | URLDataToEncode | boolean, + ): string | false { + return encodeValueContent(valueContent, value); + } + + encodeValueContent( + valueContent: string, + value: string | number | AssetURLEncode | URLDataToEncode | boolean, + ): string | false { + return encodeValueContent(valueContent, value); + } + + static decodeValueContent( + valueContent: string, + value: string, + ): string | URLDataWithHash | number | boolean | null { + return decodeValueContent(valueContent, value); + } + + decodeValueContent( + valueContent: string, + value: string, + ): string | URLDataWithHash | number | boolean | null { + return decodeValueContent(valueContent, value); + } } export default ERC725; diff --git a/src/lib/encoder.ts b/src/lib/encoder.ts index 300852c4..c5d12d11 100644 --- a/src/lib/encoder.ts +++ b/src/lib/encoder.ts @@ -68,7 +68,7 @@ const BytesNValueContentRegex = /Bytes(\d+)/; const ALLOWED_BYTES_SIZES = [2, 4, 8, 16, 32, 64, 128, 256]; -const encodeDataSourceWithHash = ( +export const encodeDataSourceWithHash = ( verification: undefined | Verification, dataSource: string, ): string => { @@ -94,7 +94,7 @@ const encodeDataSourceWithHash = ( ].join(''); }; -const decodeDataSourceWithHash = (value: string): URLDataWithHash => { +export const decodeDataSourceWithHash = (value: string): URLDataWithHash => { if (value.slice(0, 6) === '0x0000') { // DEAL with VerifiableURI // NOTE: A JSONURL with a 0x00000000 verification method is invalid.