1
0
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:
Bill Barman 2018-11-20 13:01:52 +08:00
parent 250f086389
commit 752787528e
8 changed files with 1322 additions and 3327 deletions

4294
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -69,6 +69,8 @@
"@types/mocha": "^5.2.5", "@types/mocha": "^5.2.5",
"@types/node": "^10.12.6", "@types/node": "^10.12.6",
"chai": "^4.2.0", "chai": "^4.2.0",
"eslint": "^5.9.0",
"eslint-config-oceanprotocol": "^1.3.0",
"mocha": "^5.2.0", "mocha": "^5.2.0",
"nyc": "^13.1.0", "nyc": "^13.1.0",
"source-map-support": "^0.5.9", "source-map-support": "^0.5.9",

View File

@ -1,29 +1,29 @@
// import PublicKey from "./PublicKey"
interface IAuthentication {
publicKey?: string
type?: string
}
export default class Authentication { export default class Authentication {
// private publicKey?: PublicKey
public publicKeyId: string public publicKeyId: string
public type: string public type: string
public value: string public value: string
public constructor(data?: any) { public constructor(data?: IAuthentication) {
this.publicKeyId = data['publicKey'] this.publicKeyId = data.publicKey
this.type = data['type'] this.type = data.type
this.value = '' this.value = ""
} }
public toData(): object { public toData(): IAuthentication {
return { return {
'publicKey': this.publicKeyId, publicKey: this.publicKeyId,
'type': this.type type: this.type,
} } as IAuthentication
} }
public isValid(): boolean { public isValid(): boolean {
return this.publicKeyId != '' && this.type != '' return this.publicKeyId && this.publicKeyId.length > 0 && this.type.length && this.type.length > 0
} }
} }

View File

@ -1,28 +1,24 @@
import Authentication from "./Authentication" import Authentication from "./Authentication"
import Proof from "./Proof"
import PublicKey from "./PublicKey" import PublicKey from "./PublicKey"
import Service from "./Service" import Service from "./Service"
import Proof from "./Proof"
import * as Web3 from "web3" import * as Web3 from "web3"
interface IDDO {
id: string
created?: string
["@context"]: string
publicKey?: []
authentication?: []
service?: []
proof?: {}
}
export default class DDO { export default class DDO {
public static CONTEXT: string = "https://w3id.org/future-method/v1" 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 context: string = DDO.CONTEXT
public did: string public did: string
public created: string public created: string
@ -32,83 +28,85 @@ export default class DDO {
public proof: Proof public proof: Proof
public constructor(did?: any) { public constructor(did?: any) {
if (typeof did == 'string') { if (typeof did === "string") {
this.did = did this.did = did
} }
else if (typeof did == 'object') { if (typeof did === "object") {
this.readFromData(did) this.readFromData(did)
} }
} }
public readFromData(data: object) { public readFromData(data: IDDO) {
this.did = data['id'] if (data.hasOwnProperty("id") ) {
var date = new Date() this.did = data.id
}
const date = new Date()
this.created = date.toISOString() this.created = date.toISOString()
if (data.hasOwnProperty('created')) { if (data.hasOwnProperty("created")) {
this.created = data['created'] this.created = data.created
} }
this.context = DDO.CONTEXT this.context = DDO.CONTEXT
if ( data.hasOwnProperty('@context') ) { if ( data.hasOwnProperty("@context") ) {
this.context = data['@context'] this.context = data["@context"]
} }
this.publicKeys = [] this.publicKeys = []
if ( data.hasOwnProperty('publicKey') ) { if ( data.hasOwnProperty("publicKey") ) {
data['publicKey'].forEach(function(value) { data.publicKey.forEach(function(value) {
this.publicKeys.push(new PublicKey(value)) this.publicKeys.push(new PublicKey(value))
}, this) }, this)
} }
this.authentications = [] this.authentications = []
if ( data.hasOwnProperty('authentication') ) { if ( data.hasOwnProperty("authentication") ) {
data['authentication'].forEach(function(value) { data.authentication.forEach(function(value) {
this.authentications.push(new Authentication(value)) this.authentications.push(new Authentication(value))
}, this) }, this)
} }
this.services = [] this.services = []
if ( data.hasOwnProperty('service') ) { if ( data.hasOwnProperty("service") ) {
data['service'].forEach(function(value) { data.service.forEach(function(value) {
this.services.push(new Service(value)) this.services.push(new Service(value))
}, this) }, this)
} }
if ( data.hasOwnProperty('proof') ) { if ( data.hasOwnProperty("proof") ) {
this.proof = new Proof(data['proof']) this.proof = new Proof(data.proof)
} }
} }
public toData(): object { public toData(): IDDO {
var data = { const data: IDDO = {
'@context': this.context, "@context": this.context,
'id': this.did, "id": this.did,
'created': this.created "created": this.created,
} }
if ( this.publicKeys.length > 0 ) { if ( this.publicKeys.length > 0 ) {
data['publicKey'] = [] data.publicKey = []
this.publicKeys.forEach(function(publicKey) { this.publicKeys.forEach(function(publicKey) {
this.push(publicKey.toData()) this.push(publicKey.toData())
}, data['publicKey']) }, data.publicKey)
} }
if ( this.authentications.length > 0 ) { if ( this.authentications.length > 0 ) {
data['authentication'] = [] data.authentication = []
this.authentications.forEach(function(authentication) { this.authentications.forEach(function(authentication) {
this.push(authentication.toData()) this.push(authentication.toData())
}, data['authentication'] ) }, data.authentication )
} }
if ( this.services.length > 0 ) { if ( this.services.length > 0 ) {
data['service'] = [] data.service = []
this.services.forEach(function(service) { this.services.forEach(function(service) {
this.push(service.toData()) this.push(service.toData())
}, data['service']) }, data.service)
} }
if ( this.isProofDefined() ) { if ( this.isProofDefined() ) {
data['proof'] = this.proof.toData() data.proof = this.proof.toData()
} }
return data return data
} }
@ -122,21 +120,21 @@ export default class DDO {
} }
public validate(): boolean { 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 return false
} }
if ( this.publicKeys.length == 0 ) { if ( this.publicKeys.length === 0 ) {
return false return false
} }
if ( this.authentications.length == 0 ) { if ( this.authentications.length === 0 ) {
return false return false
} }
if ( this.services.length == 0 ) { if ( this.services.length === 0 ) {
return false return false
} }
var result = { 'isValid': true } const result = { isValid: true }
this.publicKeys.forEach(function(publicKey) { this.publicKeys.forEach(function(publicKey) {
if ( !publicKey.isValid() ) { if ( !publicKey.isValid() ) {
this.isValid = false this.isValid = false
@ -173,9 +171,9 @@ export default class DDO {
} }
// return a service based on the service type value // return a service based on the service type value
public getService(serviceType: string): Service { public getService(serviceType: string): Service {
var result = { 'service': null } const result = { service: null }
this.services.forEach(function(service) { this.services.forEach(function(service) {
if (service.type == serviceType ) { if (service.type === serviceType ) {
this.service = service this.service = service
} }
}, result) }, result)
@ -183,9 +181,9 @@ export default class DDO {
} }
public findServiceKeyValue(key: string, value: string): Service { public findServiceKeyValue(key: string, value: string): Service {
var result = { 'service': null } const result = { service: null }
this.services.forEach(function(service) { this.services.forEach(function(service) {
if (service.values[key] == value) { if (service.values[key] === value) {
this.service = service this.service = service
} }
}, result) }, result)
@ -194,7 +192,7 @@ export default class DDO {
// return a string list of fields used for hashing // return a string list of fields used for hashing
public hashTextList(): string[] { public hashTextList(): string[] {
var values = [] const values = []
if (this.created) { if (this.created) {
values.push(this.created) values.push(this.created)
@ -218,15 +216,15 @@ export default class DDO {
} }
public calculateHash(): string { public calculateHash(): string {
var values = this.hashTextList() const values = this.hashTextList()
return Web3.utils.sha3(values.join()) return Web3.utils.sha3(values.join())
} }
public isEmpty(): boolean { public isEmpty(): boolean {
return this.did && this.did.length == 0 return this.did && this.did.length === 0
&& this.publicKeys.length == 0 && this.publicKeys.length === 0
&& this.authentications.length == 0 && this.authentications.length === 0
&& this.services.length == 0 && this.services.length === 0
} }
public isDIDAssigned(): boolean { public isDIDAssigned(): boolean {

View File

@ -1,29 +1,37 @@
export default class Proof { interface IProof {
created?: string
creator?: string
type?: string
signatureValue?: string
}
export default class Proof {
public created: string public created: string
public creator: string public creator: string
public type: string public type: string
public signatureValue: string public signatureValue: string
public constructor(data?: any) { public constructor(data?: IProof) {
this.created = data['created'] this.created = data.created
this.creator = data['creator'] this.creator = data.creator
this.type = data['type'] this.type = data.type
this.signatureValue = data['signatureValue'] this.signatureValue = data.signatureValue
} }
public toData(): object { public toData(): IProof {
return { return {
'created': this.created, created: this.created,
'creator': this.creator, creator: this.creator,
'type': this.type, type: this.type,
'signatureValue': this.signatureValue signatureValue: this.signatureValue,
} } as IProof
} }
public isValid(): boolean { public isValid(): boolean {
return this.created != '' && this.creator != '' && this.type != '' return this.created && this.created.length > 0
&& this.signatureValue != '' && this.creator && this.creator.length > 0
&& this.type && this.type.length > 0
&& this.signatureValue && this.signatureValue.length > 0
} }
} }

View File

@ -1,36 +1,45 @@
interface IPublicKey {
id?: string
owner?: string
type?: string
}
export default class PublicKey { export default class PublicKey {
public static TYPE_RSA: string = 'RsaSignatureAuthentication2018' public static TYPE_RSA: string = "RsaSignatureAuthentication2018"
public static PEM: string = 'publicKeyPem' public static PEM: string = "publicKeyPem"
public static JWK: string = 'publicKeyJwk' public static JWK: string = "publicKeyJwk"
public static HEX: string = 'publicKeyHex' public static HEX: string = "publicKeyHex"
public static BASE64: string = 'publicKeyBase64' public static BASE64: string = "publicKeyBase64"
public static BASE85: string = 'publicKeyBase85' public static BASE85: string = "publicKeyBase85"
public did: string public did: string
public owner: string public owner: string
public type: string public type: string
public value: string public value: string
public constructor(data?: any) { public constructor(data?: IPublicKey) {
this.did = data['id'] this.did = data.id
this.owner = data['owner'] this.owner = data.owner
this.type = data['type'] this.type = data.type
this.value = data[PublicKey.PEM] this.value = data[PublicKey.PEM]
} }
public toData(): object { public toData(): IPublicKey {
return { return {
'id': this.did, id: this.did,
'owner': this.owner, owner: this.owner,
'type': this.type, type: this.type,
[PublicKey.PEM]: this.value [PublicKey.PEM]: this.value,
} } as IPublicKey
} }
public isValid(): boolean { 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
} }
} }

View File

@ -1,31 +1,40 @@
export default class Service {
interface IService {
id?: string
serviceEndpoint?: string
type?: string
}
export default class Service {
public did: string public did: string
public endpoint: string public endpoint: string
public type: string public type: string
public values: object public values: object
public constructor(data?: any) { public constructor(data?: IService) {
this.did = data['id'] this.did = data.id
this.endpoint = data['serviceEndpoint'] this.endpoint = data.serviceEndpoint
this.type = data['type'] this.type = data.type
this.values = Object.assign({}, data) this.values = Object.assign({}, data)
delete this.values['id']
delete this.values['serviceEndpoint'] // remove any valid keys from the 'values'
delete this.values['type'] const dataRef: IService = this.values
delete dataRef.id
delete dataRef.serviceEndpoint
delete dataRef.type
} }
public toData(): object { public toData(): IService {
var data = { let data: IService = {
'id': this.did, id: this.did,
'serviceEndpoint': this.endpoint, serviceEndpoint: this.endpoint,
'type': this.type type: this.type,
} }
if (Object.keys(this.values).length > 0) { if (Object.keys(this.values).length > 0) {
data = Object.assign(data, this.values) data = Object.assign(data, this.values)
} }
return data return data as IService
} }
public isValid(): boolean { public isValid(): boolean {
@ -35,4 +44,3 @@ export default class Service {
} }
} }

View File

@ -4,7 +4,7 @@ import DDO from "../../src/libDDO/DDO"
import * as jsonDDO from "../testdata/ddoSample1.json" import * as jsonDDO from "../testdata/ddoSample1.json"
describe("DDO", () => { describe("libDDO", () => {
describe("#constructor()", () => { describe("#constructor()", () => {