1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00

add ability to add a public key and autorisation, break out the interfaces to seperate modules

This commit is contained in:
Bill 2018-11-23 12:09:56 +08:00
parent 0f692632b1
commit 6ba9046d64
9 changed files with 1457 additions and 1367 deletions

2699
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -6,10 +6,7 @@
import PublicKey from "./PublicKey"
interface IAuthentication {
publicKey?: any
type?: string
}
import IAuthentication from "./IAuthentication"
export default class Authentication {
@ -27,7 +24,7 @@ export default class Authentication {
} else {
this.publicKey = new PublicKey(data.publicKey)
}
this.type = data.type
this.type = data.type ? data.type : Authentication.TYPE_RSA
}
/*

View File

@ -5,6 +5,9 @@
*/
import Authentication from "./Authentication"
import IAuthentication from "./IAuthentication"
import IPublicKey from "./IPublicKey"
import IService from "./IService"
import Proof from "./Proof"
import PublicKey from "./PublicKey"
import Service from "./Service"
@ -245,7 +248,7 @@ export default class DDO {
*
* :return The Service object
*/
public addService(data): Service {
public addService(data: IService): Service {
const service = new Service(data)
if (service.id == null ) {
service.id = this.did
@ -254,6 +257,36 @@ export default class DDO {
return service
}
/*
* Add a public key to the DDO
*
* :param data: data with the type IPublicKey
*
* :return PublicKey object created and added to the list
*/
public addPublicKey(data: IPublicKey): PublicKey {
const publicKey = new PublicKey(data)
if ( publicKey.id == null) {
const nextIndex = this.publicKeys.length + 1
publicKey.id = this.did + "#keys-" + nextIndex
}
this.publicKeys.push(publicKey)
return publicKey
}
/*
* Add a authentication data to the DDO
*
* :param data: data with the type IAuthentication
*
* :return Authentication object created and added to the list
*/
public addAuthentication(data: IAuthentication): Authentication {
const authentication = new Authentication(data)
this.authentications.push(authentication)
return authentication
}
/*
* Add proof to the DDO
*

View File

@ -0,0 +1,5 @@
export default interface IAuthentication {
publicKey?: any
type?: string
}

8
src/libDDO/IPublicKey.ts Normal file
View File

@ -0,0 +1,8 @@
export default interface IPublicKey {
id?: string
owner?: string
type?: string
value?: string
storeType?: string
}

6
src/libDDO/IService.ts Normal file
View File

@ -0,0 +1,6 @@
export default interface IService {
id?: string
serviceEndpoint?: string
type?: string
}

View File

@ -2,11 +2,7 @@
* Class to provide access to the DDO public key
*
*/
interface IPublicKey {
id?: string
owner?: string
type?: string
}
import IPublicKey from "./IPublicKey"
export default class PublicKey {
@ -28,7 +24,7 @@ export default class PublicKey {
public constructor(data?: any) {
this.id = data.id
this.owner = data.owner
this.type = data.type
this.type = data.type ? data.type : PublicKey.TYPE_RSA
if ( data.hasOwnProperty("storeType") ) {
this.storeType = data.storeType
}

View File

@ -4,11 +4,8 @@
*
*
*/
interface IService {
id?: string
serviceEndpoint?: string
type?: string
}
import IService from "./IService"
export default class Service {

View File

@ -1,11 +1,25 @@
import {assert} from "chai"
import ConfigProvider from "../../src/ConfigProvider"
import DDO from "../../src/libDDO/DDO"
import Account from "../../src/ocean/Account"
import IdGenerator from "../../src/ocean/IdGenerator"
import Ocean from "../../src/ocean/Ocean"
import config from "../config"
import TestContractHandler from "../keeper/TestContractHandler"
import PublicKey from "../../src/libDDO/PublicKey"
import * as jsonDDO from "../testdata/ddoSample1.json"
let ocean: Ocean
describe("libDDO", () => {
before(async () => {
ConfigProvider.setConfig(config)
await TestContractHandler.prepareContracts()
ocean = await Ocean.getInstance(config)
})
describe("#constructor()", () => {
it("should create an empty ddo", async () => {
@ -147,9 +161,16 @@ describe("libDDO", () => {
const did = "did:op:" + IdGenerator.generateId()
const ddo = new DDO(did)
assert(ddo)
const service = ddo.addService({type: "metatrippy", serviceEndpoint: "http://localhost:5000"})
const testServiceType = "metatrippy"
const testServiceURL = "http://localhost:5555"
const service = ddo.addService({
type: testServiceType,
serviceEndpoint: testServiceURL,
})
assert(service)
assert(service.id === did)
assert(service.type === testServiceType )
assert(service.endpoint === testServiceURL )
})
it("should add a static proof and validate", async () => {
const did = "did:op:" + IdGenerator.generateId()
@ -172,7 +193,33 @@ describe("libDDO", () => {
// console.log(ddo.toJSON())
assert(ddo.validateProof())
})
})
describe("DDO signing", () => {
it("should add an Ethereum account public key", async () => {
const ownerAccount: Account = (await ocean.getAccounts())[0]
assert(ownerAccount)
const did = "did:op:" + IdGenerator.generateId()
const ddo = new DDO(did)
assert(ddo)
// add the public key
const publicKey = ddo.addPublicKey({
value: await ownerAccount.getPublicKey(),
storeType: "hex",
owner: ownerAccount.getId(),
})
// add the authentication record
const authentication = ddo.addAuthentication({ publicKey: publicKey.id })
assert(publicKey.id === authentication.publicKeyId)
assert(publicKey.owner === ownerAccount.getId())
assert(publicKey.type === PublicKey.TYPE_RSA)
const data: any = publicKey.toData()
assert(data.hex === publicKey.value)
})
})
})