Skip to content

Commit

Permalink
feat: export function to get verification method
Browse files Browse the repository at this point in the history
  • Loading branch information
CJ42 committed May 13, 2024
1 parent 39b98bc commit b9799d5
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 25 deletions.
51 changes: 51 additions & 0 deletions docs/methods/external-data-source-utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,54 @@ url: 'ifps://QmYr1VJLwerg6pEoscdhVGugo39pa6rycEZLjtRPDfW84UAx'
```

</details>

## 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
78 changes: 55 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* @author Robert McLeod <@robertdavid010>
* @author Fabian Vogelsteller <fabian@lukso.network>
* @author Hugo Masclet <@Hugoo>
* @author Jean Cavallera <@CJ42>
* @date 2020
*/

Expand All @@ -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
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
11 changes: 9 additions & 2 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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,
);
Expand Down

0 comments on commit b9799d5

Please sign in to comment.