mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
docs and cleanup
This commit is contained in:
parent
6776088be3
commit
f51e631536
@ -13,15 +13,6 @@ import ValueType from "../models/ValueType"
|
|||||||
import * as Web3 from "web3"
|
import * as Web3 from "web3"
|
||||||
import * as DIDTools from "../utils/DIDTools"
|
import * as DIDTools from "../utils/DIDTools"
|
||||||
|
|
||||||
/*
|
|
||||||
*
|
|
||||||
* Resolve a DID to an URL/DDO or later an internal/extrenal DID
|
|
||||||
* :param did: 32 byte value or DID string to resolver, this is part of the ocean DID did:op:<32 byte value>
|
|
||||||
* :param max_hop_count: max number of hops allowed to find the destination URL/DDO
|
|
||||||
* :return DIDResolved object: URL or DDO of the resolved DID
|
|
||||||
* :return null: if the DID cannot be resolved
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
export default class DIDResolver {
|
export default class DIDResolver {
|
||||||
|
|
||||||
public didRegistry
|
public didRegistry
|
||||||
@ -29,6 +20,17 @@ export default class DIDResolver {
|
|||||||
this.didRegistry = didRegistry
|
this.didRegistry = didRegistry
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* Resolve a DID to an URL/DDO or later an internal/extrenal DID
|
||||||
|
*
|
||||||
|
* :param did: 32 byte value or DID string to resolver, this is part of the ocean DID did:op:<32 byte value>
|
||||||
|
* :param max_hop_count: max number of hops allowed to find the destination URL/DDO
|
||||||
|
*
|
||||||
|
* :return DIDResolved object: URL or DDO of the resolved DID
|
||||||
|
* :return null: if the DID cannot be resolved
|
||||||
|
*
|
||||||
|
*/
|
||||||
public async resolve(did: string, maxHopCount?: number): Promise<DIDResolved> {
|
public async resolve(did: string, maxHopCount?: number): Promise<DIDResolved> {
|
||||||
|
|
||||||
maxHopCount = maxHopCount ? maxHopCount : 0
|
maxHopCount = maxHopCount ? maxHopCount : 0
|
||||||
@ -72,7 +74,14 @@ export default class DIDResolver {
|
|||||||
return resolved
|
return resolved
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getDID(didId): Promise<DIDRecord> {
|
/*
|
||||||
|
* Internal method to get the actual DID record
|
||||||
|
*
|
||||||
|
* :param didId: 32 byte id to find
|
||||||
|
*
|
||||||
|
* :return DIDRecord of the found DID, if not then return null
|
||||||
|
*/
|
||||||
|
private async getDID(didId): Promise<DIDRecord> {
|
||||||
|
|
||||||
let record: DIDRecord = null
|
let record: DIDRecord = null
|
||||||
|
|
||||||
@ -80,7 +89,7 @@ export default class DIDResolver {
|
|||||||
const filterOwner: string = await this.didRegistry.getOwner(didId)
|
const filterOwner: string = await this.didRegistry.getOwner(didId)
|
||||||
assert(blockNumber > 0 )
|
assert(blockNumber > 0 )
|
||||||
|
|
||||||
// filter on the blockNumber only
|
// filter on the blockNumber, owner and didId
|
||||||
const filterOptions = {
|
const filterOptions = {
|
||||||
fromBlock: blockNumber,
|
fromBlock: blockNumber,
|
||||||
toBlock: blockNumber,
|
toBlock: blockNumber,
|
||||||
@ -91,6 +100,7 @@ export default class DIDResolver {
|
|||||||
}
|
}
|
||||||
const events = await this.didRegistry.getEventData("DIDAttributeRegistered", filterOptions)
|
const events = await this.didRegistry.getEventData("DIDAttributeRegistered", filterOptions)
|
||||||
if ( events && events.length > 0 ) {
|
if ( events && events.length > 0 ) {
|
||||||
|
// only get the last event, since this should be the latest
|
||||||
const event = events[events.length - 1]
|
const event = events[events.length - 1]
|
||||||
record = {
|
record = {
|
||||||
didId: event.returnValues.did,
|
didId: event.returnValues.did,
|
||||||
@ -99,7 +109,6 @@ export default class DIDResolver {
|
|||||||
valueType: ValueType[event.returnValues.valueType],
|
valueType: ValueType[event.returnValues.valueType],
|
||||||
key: event.returnValues.key,
|
key: event.returnValues.key,
|
||||||
value: event.returnValues.value,
|
value: event.returnValues.value,
|
||||||
|
|
||||||
} as DIDRecord
|
} as DIDRecord
|
||||||
}
|
}
|
||||||
return record
|
return record
|
||||||
|
@ -13,18 +13,18 @@ const OCEAN_DID_METHOD = "op"
|
|||||||
* This function generates all types of DID's including ocean DID's
|
* This function generates all types of DID's including ocean DID's
|
||||||
*
|
*
|
||||||
* :param didId: string to of the 'id' part of the DID
|
* :param didId: string to of the 'id' part of the DID
|
||||||
* :param path: option path part of the DID
|
* :param path: optional path part of the DID
|
||||||
* :param fragment: option fragment of the DID
|
* :param fragment: optional fragment of the DID
|
||||||
* :param method: option method of the DID, defaults to 'op'
|
* :param method: optional method of the DID, defaults to 'op'
|
||||||
*
|
*
|
||||||
* :return string generated DID, in the format did:<method>:<didId>[/<path>][#<fragment>]
|
* :return string generated DID, in the format did:<method>:<didId>[/<path>][#<fragment>]
|
||||||
*/
|
*/
|
||||||
export function didGenerate(didId: string, path?: string, fragment?: string, method?: string) {
|
export function didGenerate(didId: string, path?: string, fragment?: string, method?: string) {
|
||||||
method = method === undefined ? OCEAN_DID_METHOD : method
|
method = method ? method : OCEAN_DID_METHOD
|
||||||
method = method.toLowerCase().replace(/[^a-z0-9]/g, "")
|
method = method.toLowerCase().replace(/[^a-z0-9]/g, "")
|
||||||
didId = didId.replace(/[^a-zA-Z0-9-.]/g, "")
|
didId = didId.replace(/[^a-zA-Z0-9-.]/g, "")
|
||||||
const did = ["did:", method, ":", didId]
|
const did = ["did:", method, ":", didId]
|
||||||
if (path) {
|
if ( path ) {
|
||||||
did.push("/")
|
did.push("/")
|
||||||
did.push(path)
|
did.push(path)
|
||||||
}
|
}
|
||||||
@ -47,7 +47,9 @@ interface IDIDParse {
|
|||||||
* The function didParse will parse all types of DID's including Ocean DID
|
* The function didParse will parse all types of DID's including Ocean DID
|
||||||
* If this is a Ocean DID the function will return record with the value
|
* If this is a Ocean DID the function will return record with the value
|
||||||
* `idHex` set to a hex string ( without the leading 0x ).
|
* `idHex` set to a hex string ( without the leading 0x ).
|
||||||
|
*
|
||||||
* :param did: did string to parse
|
* :param did: did string to parse
|
||||||
|
*
|
||||||
* :return ParseRecord
|
* :return ParseRecord
|
||||||
* method - method of DID
|
* method - method of DID
|
||||||
* id - id of the DID
|
* id - id of the DID
|
||||||
@ -58,9 +60,6 @@ interface IDIDParse {
|
|||||||
export function didParse(did: string): IDIDParse {
|
export function didParse(did: string): IDIDParse {
|
||||||
|
|
||||||
let result: IDIDParse = null
|
let result: IDIDParse = null
|
||||||
if ( typeof did !== "string" ) {
|
|
||||||
throw TypeError("DID must be a string")
|
|
||||||
}
|
|
||||||
const match = did.match(/^did:([a-z0-9]+):([a-zA-Z0-9-.]+)(.*)/)
|
const match = did.match(/^did:([a-z0-9]+):([a-zA-Z0-9-.]+)(.*)/)
|
||||||
if ( match ) {
|
if ( match ) {
|
||||||
|
|
||||||
@ -84,6 +83,7 @@ export function didParse(did: string): IDIDParse {
|
|||||||
* Validate a Ocean DID, return true if valid, else false
|
* Validate a Ocean DID, return true if valid, else false
|
||||||
*
|
*
|
||||||
* :param did: string to validate as an Ocean DID
|
* :param did: string to validate as an Ocean DID
|
||||||
|
*
|
||||||
* :return true if the DID is valid
|
* :return true if the DID is valid
|
||||||
*/
|
*/
|
||||||
export function isDIDValid(did: string): boolean {
|
export function isDIDValid(did: string): boolean {
|
||||||
@ -96,11 +96,12 @@ export function isDIDValid(did: string): boolean {
|
|||||||
*
|
*
|
||||||
* :param id: can be a hex string with or without the leading '0x'
|
* :param id: can be a hex string with or without the leading '0x'
|
||||||
* :param method: if empty, default to 'op'
|
* :param method: if empty, default to 'op'
|
||||||
|
*
|
||||||
* :return a valid DID
|
* :return a valid DID
|
||||||
* :return '0' for a 0 DID
|
* :return '0' for a 0 DID
|
||||||
*/
|
*/
|
||||||
export function idToDID(id: string, method?: string): string {
|
export function idToDID(id: string, method?: string): string {
|
||||||
method = method === undefined ? OCEAN_DID_METHOD : method
|
method = method ? method : OCEAN_DID_METHOD
|
||||||
method = method.toLowerCase().replace(/[^a-z0-9]/g, "")
|
method = method.toLowerCase().replace(/[^a-z0-9]/g, "")
|
||||||
|
|
||||||
// remove any leading 0x
|
// remove any leading 0x
|
||||||
@ -123,15 +124,23 @@ export function idToDID(id: string, method?: string): string {
|
|||||||
*/
|
*/
|
||||||
export function didToId(did: string): string {
|
export function didToId(did: string): string {
|
||||||
const result = didParse(did)
|
const result = didParse(did)
|
||||||
if (result && result.idHex ) {
|
if ( result && result.idHex ) {
|
||||||
return result.idHex
|
return result.idHex
|
||||||
}
|
}
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Convert an Ocean DID string to 32 bytes
|
||||||
|
*
|
||||||
|
* :param did: string starting with "did:op:"
|
||||||
|
*
|
||||||
|
* :return 32 byte array of the Id in the DID
|
||||||
|
* :return null if the DID is a invalid Ocean DID
|
||||||
|
*/
|
||||||
export function didToIdBytes(did: string): Uint8Array {
|
export function didToIdBytes(did: string): Uint8Array {
|
||||||
const result = didParse(did)
|
const result = didParse(did)
|
||||||
if (result && result.idHex) {
|
if ( result && result.idHex ) {
|
||||||
return Web3.utils.hexToBytes("0x" + result.idHex)
|
return Web3.utils.hexToBytes("0x" + result.idHex)
|
||||||
}
|
}
|
||||||
return null
|
return null
|
||||||
|
Loading…
x
Reference in New Issue
Block a user