mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
lint cleanup
This commit is contained in:
parent
250f086389
commit
752787528e
4296
package-lock.json
generated
4296
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -69,6 +69,8 @@
|
||||
"@types/mocha": "^5.2.5",
|
||||
"@types/node": "^10.12.6",
|
||||
"chai": "^4.2.0",
|
||||
"eslint": "^5.9.0",
|
||||
"eslint-config-oceanprotocol": "^1.3.0",
|
||||
"mocha": "^5.2.0",
|
||||
"nyc": "^13.1.0",
|
||||
"source-map-support": "^0.5.9",
|
||||
|
@ -1,29 +1,29 @@
|
||||
// import PublicKey from "./PublicKey"
|
||||
|
||||
interface IAuthentication {
|
||||
publicKey?: string
|
||||
type?: string
|
||||
}
|
||||
|
||||
export default class Authentication {
|
||||
|
||||
|
||||
// private publicKey?: PublicKey
|
||||
public publicKeyId: string
|
||||
public type: string
|
||||
public value: string
|
||||
|
||||
public constructor(data?: any) {
|
||||
this.publicKeyId = data['publicKey']
|
||||
this.type = data['type']
|
||||
this.value = ''
|
||||
}
|
||||
|
||||
public toData(): object {
|
||||
return {
|
||||
'publicKey': this.publicKeyId,
|
||||
'type': this.type
|
||||
}
|
||||
}
|
||||
public isValid(): boolean {
|
||||
return this.publicKeyId != '' && this.type != ''
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public constructor(data?: IAuthentication) {
|
||||
this.publicKeyId = data.publicKey
|
||||
this.type = data.type
|
||||
this.value = ""
|
||||
}
|
||||
|
||||
public toData(): IAuthentication {
|
||||
return {
|
||||
publicKey: this.publicKeyId,
|
||||
type: this.type,
|
||||
} as IAuthentication
|
||||
}
|
||||
|
||||
public isValid(): boolean {
|
||||
return this.publicKeyId && this.publicKeyId.length > 0 && this.type.length && this.type.length > 0
|
||||
}
|
||||
}
|
||||
|
@ -1,28 +1,24 @@
|
||||
|
||||
import Authentication from "./Authentication"
|
||||
import Proof from "./Proof"
|
||||
import PublicKey from "./PublicKey"
|
||||
import Service from "./Service"
|
||||
import Proof from "./Proof"
|
||||
|
||||
|
||||
import * as Web3 from "web3"
|
||||
|
||||
interface IDDO {
|
||||
id: string
|
||||
created?: string
|
||||
["@context"]: string
|
||||
publicKey?: []
|
||||
authentication?: []
|
||||
service?: []
|
||||
proof?: {}
|
||||
}
|
||||
|
||||
export default class DDO {
|
||||
|
||||
public static CONTEXT: string = "https://w3id.org/future-method/v1"
|
||||
|
||||
/*
|
||||
public static serialize(ddo: DDO): string {
|
||||
return JSON.stringify(ddo, null, 2)
|
||||
}
|
||||
|
||||
public static deserialize(ddoString: string): DDO {
|
||||
const ddo = JSON.parse(ddoString)
|
||||
|
||||
return ddo as DDO
|
||||
}
|
||||
*/
|
||||
|
||||
public context: string = DDO.CONTEXT
|
||||
public did: string
|
||||
public created: string
|
||||
@ -32,111 +28,113 @@ export default class DDO {
|
||||
public proof: Proof
|
||||
|
||||
public constructor(did?: any) {
|
||||
if (typeof did == 'string') {
|
||||
if (typeof did === "string") {
|
||||
this.did = did
|
||||
}
|
||||
else if (typeof did == 'object') {
|
||||
if (typeof did === "object") {
|
||||
this.readFromData(did)
|
||||
}
|
||||
}
|
||||
|
||||
public readFromData(data: object) {
|
||||
this.did = data['id']
|
||||
var date = new Date()
|
||||
|
||||
public readFromData(data: IDDO) {
|
||||
if (data.hasOwnProperty("id") ) {
|
||||
this.did = data.id
|
||||
}
|
||||
const date = new Date()
|
||||
this.created = date.toISOString()
|
||||
if (data.hasOwnProperty('created')) {
|
||||
this.created = data['created']
|
||||
if (data.hasOwnProperty("created")) {
|
||||
this.created = data.created
|
||||
}
|
||||
|
||||
|
||||
this.context = DDO.CONTEXT
|
||||
if ( data.hasOwnProperty('@context') ) {
|
||||
this.context = data['@context']
|
||||
if ( data.hasOwnProperty("@context") ) {
|
||||
this.context = data["@context"]
|
||||
}
|
||||
|
||||
|
||||
this.publicKeys = []
|
||||
|
||||
if ( data.hasOwnProperty('publicKey') ) {
|
||||
data['publicKey'].forEach(function(value) {
|
||||
|
||||
if ( data.hasOwnProperty("publicKey") ) {
|
||||
data.publicKey.forEach(function(value) {
|
||||
this.publicKeys.push(new PublicKey(value))
|
||||
}, this)
|
||||
}
|
||||
|
||||
this.authentications = []
|
||||
if ( data.hasOwnProperty('authentication') ) {
|
||||
data['authentication'].forEach(function(value) {
|
||||
if ( data.hasOwnProperty("authentication") ) {
|
||||
data.authentication.forEach(function(value) {
|
||||
this.authentications.push(new Authentication(value))
|
||||
}, this)
|
||||
}
|
||||
|
||||
this.services = []
|
||||
if ( data.hasOwnProperty('service') ) {
|
||||
data['service'].forEach(function(value) {
|
||||
if ( data.hasOwnProperty("service") ) {
|
||||
data.service.forEach(function(value) {
|
||||
this.services.push(new Service(value))
|
||||
}, this)
|
||||
}
|
||||
|
||||
if ( data.hasOwnProperty('proof') ) {
|
||||
this.proof = new Proof(data['proof'])
|
||||
|
||||
if ( data.hasOwnProperty("proof") ) {
|
||||
this.proof = new Proof(data.proof)
|
||||
}
|
||||
}
|
||||
|
||||
public toData(): object {
|
||||
var data = {
|
||||
'@context': this.context,
|
||||
'id': this.did,
|
||||
'created': this.created
|
||||
|
||||
public toData(): IDDO {
|
||||
const data: IDDO = {
|
||||
"@context": this.context,
|
||||
"id": this.did,
|
||||
"created": this.created,
|
||||
}
|
||||
if ( this.publicKeys.length > 0 ) {
|
||||
data['publicKey'] = []
|
||||
data.publicKey = []
|
||||
this.publicKeys.forEach(function(publicKey) {
|
||||
this.push(publicKey.toData())
|
||||
}, data['publicKey'])
|
||||
}, data.publicKey)
|
||||
}
|
||||
|
||||
if ( this.authentications.length > 0 ) {
|
||||
data['authentication'] = []
|
||||
data.authentication = []
|
||||
this.authentications.forEach(function(authentication) {
|
||||
this.push(authentication.toData())
|
||||
}, data['authentication'] )
|
||||
}, data.authentication )
|
||||
}
|
||||
|
||||
|
||||
if ( this.services.length > 0 ) {
|
||||
data['service'] = []
|
||||
data.service = []
|
||||
this.services.forEach(function(service) {
|
||||
this.push(service.toData())
|
||||
}, data['service'])
|
||||
}, data.service)
|
||||
}
|
||||
|
||||
|
||||
if ( this.isProofDefined() ) {
|
||||
data['proof'] = this.proof.toData()
|
||||
data.proof = this.proof.toData()
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
|
||||
public toJSON(): string {
|
||||
return JSON.stringify(this.toData(), null, 2)
|
||||
}
|
||||
|
||||
|
||||
public isProofDefined(): boolean {
|
||||
return this.proof != null
|
||||
}
|
||||
public validate(): boolean {
|
||||
|
||||
if (this.context.length == 0 || this.did.length == 0 || this.created.length == 0) {
|
||||
|
||||
if (this.context.length === 0 || this.did.length === 0 || this.created.length === 0) {
|
||||
return false
|
||||
}
|
||||
|
||||
if ( this.publicKeys.length == 0 ) {
|
||||
if ( this.publicKeys.length === 0 ) {
|
||||
return false
|
||||
}
|
||||
if ( this.authentications.length == 0 ) {
|
||||
if ( this.authentications.length === 0 ) {
|
||||
return false
|
||||
}
|
||||
if ( this.services.length == 0 ) {
|
||||
if ( this.services.length === 0 ) {
|
||||
return false
|
||||
}
|
||||
|
||||
var result = { 'isValid': true }
|
||||
|
||||
const result = { isValid: true }
|
||||
this.publicKeys.forEach(function(publicKey) {
|
||||
if ( !publicKey.isValid() ) {
|
||||
this.isValid = false
|
||||
@ -173,33 +171,33 @@ export default class DDO {
|
||||
}
|
||||
// return a service based on the service type value
|
||||
public getService(serviceType: string): Service {
|
||||
var result = { 'service': null }
|
||||
const result = { service: null }
|
||||
this.services.forEach(function(service) {
|
||||
if (service.type == serviceType ) {
|
||||
if (service.type === serviceType ) {
|
||||
this.service = service
|
||||
}
|
||||
}, result)
|
||||
return result.service
|
||||
}
|
||||
|
||||
|
||||
public findServiceKeyValue(key: string, value: string): Service {
|
||||
var result = { 'service': null }
|
||||
const result = { service: null }
|
||||
this.services.forEach(function(service) {
|
||||
if (service.values[key] == value) {
|
||||
if (service.values[key] === value) {
|
||||
this.service = service
|
||||
}
|
||||
}, result)
|
||||
return result.service
|
||||
}
|
||||
|
||||
|
||||
// return a string list of fields used for hashing
|
||||
public hashTextList(): string[] {
|
||||
var values = []
|
||||
|
||||
const values = []
|
||||
|
||||
if (this.created) {
|
||||
values.push(this.created)
|
||||
}
|
||||
|
||||
|
||||
this.publicKeys.forEach(function(publicKey) {
|
||||
this.push(publicKey.type)
|
||||
this.push(publicKey.value)
|
||||
@ -216,19 +214,19 @@ export default class DDO {
|
||||
}, values)
|
||||
return values
|
||||
}
|
||||
|
||||
|
||||
public calculateHash(): string {
|
||||
var values = this.hashTextList()
|
||||
const values = this.hashTextList()
|
||||
return Web3.utils.sha3(values.join())
|
||||
}
|
||||
|
||||
|
||||
public isEmpty(): boolean {
|
||||
return this.did && this.did.length == 0
|
||||
&& this.publicKeys.length == 0
|
||||
&& this.authentications.length == 0
|
||||
&& this.services.length == 0
|
||||
return this.did && this.did.length === 0
|
||||
&& this.publicKeys.length === 0
|
||||
&& this.authentications.length === 0
|
||||
&& this.services.length === 0
|
||||
}
|
||||
|
||||
|
||||
public isDIDAssigned(): boolean {
|
||||
return this.did && this.did.length > 0
|
||||
}
|
||||
|
@ -1,29 +1,37 @@
|
||||
interface IProof {
|
||||
created?: string
|
||||
creator?: string
|
||||
type?: string
|
||||
signatureValue?: string
|
||||
}
|
||||
|
||||
export default class Proof {
|
||||
|
||||
|
||||
|
||||
public created: string
|
||||
public creator: string
|
||||
public type: string
|
||||
public signatureValue: string
|
||||
|
||||
public constructor(data?: any) {
|
||||
this.created = data['created']
|
||||
this.creator = data['creator']
|
||||
this.type = data['type']
|
||||
this.signatureValue = data['signatureValue']
|
||||
public constructor(data?: IProof) {
|
||||
this.created = data.created
|
||||
this.creator = data.creator
|
||||
this.type = data.type
|
||||
this.signatureValue = data.signatureValue
|
||||
}
|
||||
|
||||
public toData(): object {
|
||||
return {
|
||||
'created': this.created,
|
||||
'creator': this.creator,
|
||||
'type': this.type,
|
||||
'signatureValue': this.signatureValue
|
||||
}
|
||||
}
|
||||
public isValid(): boolean {
|
||||
return this.created != '' && this.creator != '' && this.type != ''
|
||||
&& this.signatureValue != ''
|
||||
}
|
||||
}
|
||||
|
||||
public toData(): IProof {
|
||||
return {
|
||||
created: this.created,
|
||||
creator: this.creator,
|
||||
type: this.type,
|
||||
signatureValue: this.signatureValue,
|
||||
} as IProof
|
||||
}
|
||||
|
||||
public isValid(): boolean {
|
||||
return this.created && this.created.length > 0
|
||||
&& this.creator && this.creator.length > 0
|
||||
&& this.type && this.type.length > 0
|
||||
&& this.signatureValue && this.signatureValue.length > 0
|
||||
}
|
||||
}
|
||||
|
@ -1,36 +1,45 @@
|
||||
|
||||
interface IPublicKey {
|
||||
id?: string
|
||||
owner?: string
|
||||
type?: string
|
||||
}
|
||||
|
||||
export default class PublicKey {
|
||||
|
||||
public static TYPE_RSA: string = 'RsaSignatureAuthentication2018'
|
||||
public static PEM: string = 'publicKeyPem'
|
||||
public static JWK: string = 'publicKeyJwk'
|
||||
public static HEX: string = 'publicKeyHex'
|
||||
public static BASE64: string = 'publicKeyBase64'
|
||||
public static BASE85: string = 'publicKeyBase85'
|
||||
|
||||
|
||||
public static TYPE_RSA: string = "RsaSignatureAuthentication2018"
|
||||
public static PEM: string = "publicKeyPem"
|
||||
public static JWK: string = "publicKeyJwk"
|
||||
public static HEX: string = "publicKeyHex"
|
||||
public static BASE64: string = "publicKeyBase64"
|
||||
public static BASE85: string = "publicKeyBase85"
|
||||
|
||||
public did: string
|
||||
public owner: string
|
||||
public type: string
|
||||
public value: string
|
||||
|
||||
public constructor(data?: any) {
|
||||
this.did = data['id']
|
||||
this.owner = data['owner']
|
||||
this.type = data['type']
|
||||
public constructor(data?: IPublicKey) {
|
||||
this.did = data.id
|
||||
this.owner = data.owner
|
||||
this.type = data.type
|
||||
this.value = data[PublicKey.PEM]
|
||||
}
|
||||
|
||||
public toData(): object {
|
||||
|
||||
public toData(): IPublicKey {
|
||||
return {
|
||||
'id': this.did,
|
||||
'owner': this.owner,
|
||||
'type': this.type,
|
||||
[PublicKey.PEM]: this.value
|
||||
}
|
||||
id: this.did,
|
||||
owner: this.owner,
|
||||
type: this.type,
|
||||
[PublicKey.PEM]: this.value,
|
||||
} as IPublicKey
|
||||
}
|
||||
|
||||
|
||||
public isValid(): boolean {
|
||||
return this.did != '' && this.owner != '' && this.type != '' && this.value != ''
|
||||
return this.did && this.did.length > 0
|
||||
&& this.owner && this.owner.length > 0
|
||||
&& this.type && this.type.length > 0
|
||||
&& this.value && this.value.length > 0
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -1,38 +1,46 @@
|
||||
|
||||
interface IService {
|
||||
id?: string
|
||||
serviceEndpoint?: string
|
||||
type?: string
|
||||
}
|
||||
|
||||
export default class Service {
|
||||
|
||||
|
||||
|
||||
public did: string
|
||||
public endpoint: string
|
||||
public type: string
|
||||
public values: object
|
||||
|
||||
public constructor(data?: any) {
|
||||
this.did = data['id']
|
||||
this.endpoint = data['serviceEndpoint']
|
||||
this.type = data['type']
|
||||
public constructor(data?: IService) {
|
||||
this.did = data.id
|
||||
this.endpoint = data.serviceEndpoint
|
||||
this.type = data.type
|
||||
this.values = Object.assign({}, data)
|
||||
delete this.values['id']
|
||||
delete this.values['serviceEndpoint']
|
||||
delete this.values['type']
|
||||
|
||||
// remove any valid keys from the 'values'
|
||||
const dataRef: IService = this.values
|
||||
delete dataRef.id
|
||||
delete dataRef.serviceEndpoint
|
||||
delete dataRef.type
|
||||
}
|
||||
|
||||
public toData(): object {
|
||||
var data = {
|
||||
'id': this.did,
|
||||
'serviceEndpoint': this.endpoint,
|
||||
'type': this.type
|
||||
|
||||
public toData(): IService {
|
||||
let data: IService = {
|
||||
id: this.did,
|
||||
serviceEndpoint: this.endpoint,
|
||||
type: this.type,
|
||||
}
|
||||
if (Object.keys(this.values).length > 0) {
|
||||
data = Object.assign(data, this.values)
|
||||
}
|
||||
return data
|
||||
return data as IService
|
||||
}
|
||||
|
||||
|
||||
public isValid(): boolean {
|
||||
return this.did && this.did.length > 0
|
||||
&& this.endpoint && this.endpoint.length > 0
|
||||
return this.did && this.did.length > 0
|
||||
&& this.endpoint && this.endpoint.length > 0
|
||||
&& this.type && this.type.length > 0
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ import DDO from "../../src/libDDO/DDO"
|
||||
import * as jsonDDO from "../testdata/ddoSample1.json"
|
||||
|
||||
|
||||
describe("DDO", () => {
|
||||
describe("libDDO", () => {
|
||||
|
||||
describe("#constructor()", () => {
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user