From b9799d532fc8d66f235404ea3222484c6b8fc4c9 Mon Sep 17 00:00:00 2001 From: CJ42 Date: Mon, 13 May 2024 11:11:56 +0100 Subject: [PATCH] feat: export function to get verification method --- .../methods/external-data-source-utilities.md | 51 ++++++++++++ src/index.ts | 78 +++++++++++++------ src/lib/utils.ts | 11 ++- 3 files changed, 115 insertions(+), 25 deletions(-) diff --git a/docs/methods/external-data-source-utilities.md b/docs/methods/external-data-source-utilities.md index 3d944bb2..ef1c824b 100644 --- a/docs/methods/external-data-source-utilities.md +++ b/docs/methods/external-data-source-utilities.md @@ -156,3 +156,54 @@ url: 'ifps://QmYr1VJLwerg6pEoscdhVGugo39pa6rycEZLjtRPDfW84UAx' ``` + +## getVerificationMethod + +```js +const myErc725 = new ERC725(); +myErc725.getVerificationMethod(nameOrSig); +``` + +```js +ERC725.getVerificationMethod(nameOrSig); +``` + +```js +import { getVerificationMethod } from '@erc725/erc725.js'; +getVerificationMethod(nameOrSig); +``` + +Get the verification method definition, including the name, signature and related method. + +method: (data: string | object | Uint8Array | null) => string; +name: SUPPORTED_VERIFICATION_METHOD_STRINGS; +sig: SUPPORTED_VERIFICATION_METHODS; + +#### Parameters + +| Name | Type | Description | +| :---------- | :----- | :------------------------------------------ | +| `nameOrSig` | string | The 4 bytes hex of the verification method. | + +#### Returns + +| Name | Type | Description | +| :--- | :----- | :-------------------------------------------------------------------------------------- | +| | object | An object containing the name, signature and method related to the verification method. | + +### Example + +```javascript title="Example of the method" +getVerificationMethod('0x6f357c6a'); +/* +{ + method: [Function: keccak256Method], + name: 'keccak256(utf8)', + sig: '0x6f357c6a' +} +*/ +``` + +## hashData + +## isDataAuthentic diff --git a/src/index.ts b/src/index.ts index d571f779..4d3b990b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,6 +17,7 @@ * @author Robert McLeod <@robertdavid010> * @author Fabian Vogelsteller * @author Hugo Masclet <@Hugoo> + * @author Jean Cavallera <@CJ42> * @date 2020 */ @@ -30,12 +31,17 @@ import { convertIPFSGatewayUrl, generateSchemasFromDynamicKeys, duplicateMultiTypeERC725SchemaEntry, + getVerificationMethod, } from './lib/utils'; import { getSchema } from './lib/schemaParser'; import { isValidSignature } from './lib/isValidSignature'; -import { DEFAULT_GAS_VALUE } from './constants/constants'; +import { + DEFAULT_GAS_VALUE, + SUPPORTED_VERIFICATION_METHODS, + SUPPORTED_VERIFICATION_METHOD_STRINGS, +} from './constants/constants'; import { encodeKeyName, isDynamicKeyName } from './lib/encodeKeyName'; // Types @@ -82,7 +88,7 @@ export { }; export { ERC725Config, KeyValuePair, ProviderTypes } from './types'; -export { encodeData, encodeArrayKey } from './lib/utils'; +export { encodeData, encodeArrayKey, getVerificationMethod } from './lib/utils'; export { decodeData } from './lib/decodeData'; export { encodeKeyName, isDynamicKeyName } from './lib/encodeKeyName'; export { decodeMappingKey } from './lib/decodeMappingKey'; @@ -664,27 +670,8 @@ 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); - } + // Encoding methods + // ---------------- /** * @param type The valueType to encode the value as @@ -745,6 +732,51 @@ export class ERC725 { ): string | URLDataWithHash | number | boolean | null { return decodeValueContent(valueContent, value); } + + // External Data Source utilities (`VerifiableURI` and `JSONURI`) + // ---------------------------------------------------------------- + + 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); + } + + static getVerificationMethod(nameOrSig: string): + | { + method: (data: string | object | Uint8Array | null) => string; + name: SUPPORTED_VERIFICATION_METHOD_STRINGS; + sig: SUPPORTED_VERIFICATION_METHODS; + } + | undefined { + return getVerificationMethod(nameOrSig); + } + + getVerificationMethod(nameOrSig: string): + | { + method: (data: string | object | Uint8Array | null) => string; + name: SUPPORTED_VERIFICATION_METHOD_STRINGS; + sig: SUPPORTED_VERIFICATION_METHODS; + } + | undefined { + return getVerificationMethod(nameOrSig); + } } export default ERC725; diff --git a/src/lib/utils.ts b/src/lib/utils.ts index e8e49baf..4c4b6386 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -47,6 +47,7 @@ import { SUPPORTED_VERIFICATION_METHODS, SUPPORTED_VERIFICATION_METHODS_LIST, COMPACT_BYTES_ARRAY_STRING, + SUPPORTED_VERIFICATION_METHOD_STRINGS, } from '../constants/constants'; import { decodeValueContent, @@ -157,7 +158,7 @@ export function encodeKeyValue( /** * - * @param key The schema key of a schema with keyType = 'Array' + * @param key A data key either as a 32 bytes long value, or a data key name of keyType = 'Array' * @param index An integer representing the intended array index * @return The raw bytes key for the array element */ @@ -524,7 +525,13 @@ export function encodeData( ); } -export function getVerificationMethod(nameOrSig: string) { +export function getVerificationMethod(nameOrSig: string): + | { + method: (data: string | object | Uint8Array | null) => string; + name: SUPPORTED_VERIFICATION_METHOD_STRINGS; + sig: SUPPORTED_VERIFICATION_METHODS; + } + | undefined { const verificationMethod = Object.values(HASH_METHODS).find( ({ name, sig }) => name === nameOrSig || sig === nameOrSig, );