Skip to content

Commit

Permalink
Tweaks for did:key:* and did:dock:* (#388)
Browse files Browse the repository at this point in the history
  • Loading branch information
olegnn authored Feb 7, 2024
1 parent ebb8e86 commit 2d56ba1
Show file tree
Hide file tree
Showing 29 changed files with 777 additions and 511 deletions.
1 change: 1 addition & 0 deletions .jsdoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"allowUnknownTags": true,
"dictionaries": ["jsdoc","closure"]
},
"plugins": ["node_modules/jsdoc-typeof-plugin"],
"templates": {
"cleverLinks": false,
"monospaceLinks": false
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@docknetwork/sdk",
"version": "7.2.1",
"version": "7.3.0",
"main": "index.js",
"license": "MIT",
"repository": {
Expand Down Expand Up @@ -45,6 +45,7 @@
"jest": "^24.5.0",
"jest-environment-node": "^24.5.0",
"jsdoc": "^3.6.3",
"jsdoc-typeof-plugin": "^1.0.0",
"parity-scale-codec": "^0.5.3",
"r1csfile": "0.0.41",
"ramda": "^0.27.2",
Expand Down
22 changes: 14 additions & 8 deletions scripts/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,13 +306,18 @@ export const formatDock = (value, config = {}) =>
* @param {function(dock: DockAPI, ...args: any[]): Promise<T>}
* @returns {function(...args: any[]): Promise<T>}
*/
export const withDockAPI = curry((params, fn) => async (...args) => {
export const withDockAPI = curry(({ senderAccountURI, ...params }, fn) => async (...args) => {
console.log("Connecting...");
let err, res;
const dockAPI = new DockAPI();

try {
await dockAPI.init(params);
if (senderAccountURI) {
const account = dockAPI.keyring.addFromUri(senderAccountURI);
dockAPI.setAccount(account);
}

res = await fn(dockAPI, ...args);
} catch (e) {
err = e;
Expand Down Expand Up @@ -380,14 +385,14 @@ export const addLoggerPrefix = curry((buildPrefix, logger) =>
);

/**
* Returns hash and number of the first block satisfying provided predicate.
* Throws an error in case such block can't be found.
* Returns hash and number of the first block satisfying the provided predicate.
* Throws an error in case such a block can't be found.
*
* @template T
* @param {*} api
* @param {{ start: number, end: number, targetValue: T, fetchValue: (number) => Promise<T>, checkBlock: (block: any) => boolean }}
* @param {{ maxBlocksPerUnit: number = null, debug = false }} targetEra
* @returns {{ number: number, hash: * }}
* @param {{ start: number, end: number, targetValue: T, fetchValue: (number) => Promise<T>, checkBlock: (block: any) => Promise<boolean> }}
* @param {{ maxBlocksPerUnit: number = null, debug = false }}
* @returns {{ number: number, hash: any }}
*/
export const binarySearchFirstSatisfyingBlock = curry(
async (
Expand All @@ -405,7 +410,7 @@ export const binarySearchFirstSatisfyingBlock = curry(
const midBlockHash = await api.rpc.chain.getBlockHash(midBlockNumber);
const midValue = await fetchValue(midBlockHash);

if (debug)
if (debug) {
timestampLogger.log(
"target value:",
targetValue,
Expand All @@ -418,6 +423,7 @@ export const binarySearchFirstSatisfyingBlock = curry(
"jumps:",
jumps
);
}

if (midValue < targetValue) {
startBlockNumber = midBlockNumber + 1;
Expand Down Expand Up @@ -445,7 +451,7 @@ export const binarySearchFirstSatisfyingBlock = curry(
}

const midBlock = await api.derive.chain.getBlock(midBlockHash);
const found = checkBlock(midBlock);
const found = await checkBlock(midBlock);

if (found) {
if (debug)
Expand Down
16 changes: 16 additions & 0 deletions scripts/set-storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { toPairs } from "ramda";
import { withDockAPI } from "./helpers";
import fs from 'fs';

const { FullNodeEndpoint, SudoSecretURI } = process.env;
const [_, __, filePath] = process.argv;

async function main(dock, filePath) {
const keyValuePairs = JSON.parse(fs.readFileSync(filePath));
console.log('Setting', JSON.stringify(keyValuePairs, null, 2));
const setStorageTx = dock.api.tx.system.setStorage(toPairs(keyValuePairs));
return dock.signAndSend(dock.api.tx.sudo.sudo(setStorageTx))
}

withDockAPI({ senderAccountURI: SudoSecretURI, address: FullNodeEndpoint })(main)(filePath).catch(console.error);

4 changes: 2 additions & 2 deletions src/modules/did/did.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
NoOnchainDIDError,
NoOffchainDIDError,
createDidSig,
DockDIDMethodKeyQualifier,
DidMethodKeyQualifier,
} from '../../utils/did';
import { getStateChange } from '../../utils/misc';

Expand Down Expand Up @@ -681,7 +681,7 @@ class DIDModule {
* @return {string} The DID identifier.
*/
getFullyQualifiedDIDMethodKey(didMethodKey) {
return `${DockDIDMethodKeyQualifier}${didMethodKey}`;
return `${DidMethodKeyQualifier}${didMethodKey}`;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/modules/schema.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { canonicalize } from 'json-canonicalize';
import { validate } from 'jsonschema';

import { hexDIDToQualified } from '../utils/did';
import { typedHexDIDFromSubstrate } from '../utils/did';

import {
createNewDockBlobId,
Expand Down Expand Up @@ -140,7 +140,7 @@ export default class Schema {
return {
...chainValue,
id,
author: hexDIDToQualified(chainBlob[0]),
author: typedHexDIDFromSubstrate(dockApi, chainBlob[0]).toQualifiedEncodedString(),
};
}
throw new Error('Incorrect schema format');
Expand Down
2 changes: 1 addition & 1 deletion src/resolver/generic/const.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export const METHOD_REG_EXP_PATTERN = '([a-zA-Z0-9_]+)';
export const HEX_ID_REG_EXP_PATTERN = '(0x[0-9a-fA-F]+)';

/**
* Use to specify `PREFIX`/`METHOD` that will be dispatched in case no direct matches found.
* Use to specify `PREFIX`/`METHOD` that will be dispatched in case no direct matches are found.
*/
export const WILDCARD = Symbol.for('@docknetwork/sdk/wildcard-resolver');
6 changes: 3 additions & 3 deletions src/resolver/generic/multi-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import { ensureString } from '../../utils/type-helpers';
*/
export default class MultiResolver extends Resolver {
/**
* Matching string prefix, array of string prefixes, or wildcard pattern.
* Matching string prefix, an array of string prefixes, or wildcard pattern.
* @type {Array<string> | string | symbol}
* @abstract
* @static
*/
static PREFIX;
/**
* Matching string method, array of string methods, or wildcard pattern.
* Matching string method, an array of string methods, or wildcard pattern.
* @type {Array<string> | string | symbol}
* @abstract
* @static
Expand Down Expand Up @@ -58,7 +58,7 @@ export default class MultiResolver extends Resolver {

if (!resolverList.length) {
throw new Error(
'No resolvers were provided. You need to either implement `resolve` or provide a list of resolvers.',
'No resolvers were provided. You need to either implement `resolve` or provide a list of resolvers',
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/resolver/generic/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class Resolver {
}

/**
* Resolves an entity if it's identifier is supported.
* Resolves an entity if its identifier is supported.
* Each resolver must have static `string` or `WILDCARD` symbol properties `PREFIX` and `METHOD`,
* then the `supports` method will return `true` if they match the identifier.
* In case the resolver must be used for any `PREFIX`/`METHOD` as default, use the `WILDCARD` symbol.
Expand Down
1 change: 1 addition & 0 deletions src/utils/codec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function isHexWithGivenByteSize(value, byteSize = undefined) {
*/
export function getHexIdentifier(id, qualifiers, byteSize) {
const strId = String(id);

for (const qualifier of [].concat(qualifiers)) {
if (strId.startsWith(qualifier)) {
// Fully qualified ID. Remove the qualifier
Expand Down
Loading

0 comments on commit 2d56ba1

Please sign in to comment.