import { MamReader, MAM_MODE } from 'mam.ts'; import * as tv4 from 'tv4'; import { DID, DIDTx, DID_COMMANDS } from './DID'; interface schema { "version" : { "type": "string" }; "command" : { "type": "string" }; "data" : {}; } export class DIDReader { private mamReader : MamReader; private state : DID; private Messages : string[]; constructor(provider : string, didMamStreamRoot : string) { //Create the mamReader this.mamReader = new MamReader(provider, didMamStreamRoot, MAM_MODE.PUBLIC); this.state = null; this.Messages = []; } public async QueryDID() : Promise { this.Messages = await this.mamReader.fetch(); //Loop through the transactions for (let i=0; i < this.Messages.length; i++) { //TODO: Check for versioning let Message : DIDTx = JSON.parse(this.Messages[i]); switch(Message.command) { case DID_COMMANDS.CREATE_DID: if(this.validateTxToSchema(Message.command, Message.data)){ this.ProcessCreateDID(Message.data, Message.proof); } else { console.log("The incoming data is not valid."); } break; case DID_COMMANDS.ADD_KEYPAIR: if(this.validateTxToSchema(Message.command, Message.data)){ this.ProcessAddKeypair(Message.data, Message.proof); } else { console.log("The incoming data is not valid."); } break; case DID_COMMANDS.ADD_SERVICEPOINT: if(this.validateTxToSchema(Message.command, Message.data)) { this.ProcessAddServicePoint(Message.data, Message.proof); } else { console.log("The incoming ServicePoint is not valid"); } break; default: console.log("Unsupported command"); break; } } return true; } private ProcessCreateDID(txData : {}, txProof : {}) { if(this.state == null){ this.state = new DID(txData["@context"],txData["did"],txData["authentication"],txData["publicKey"]); }else{ console.log("There already is a state, can not create a new state."); } } private ProcessAddKeypair(txData : {}, txProof : {}){ if(this.state != null){ this.state.addPublicKey(txData["id"],txData["type"],"",txData["publicKeyPem"]); }else{ console.log("There is no state yet."); } } private ProcessAddServicePoint(txData : {}, txProof : {}) { if(this.state != null) { this.state.AddServicePoint({id : txData["service"][0]["id"], type : txData["service"][0]["type"], serviceEndpoint : txData["service"][0]["serviceEndpoint"]}); } else { console.log("There is no state yet"); } } private validateTxToSchema(command:DID_COMMANDS, txData:{}) : Boolean{ let schema : schema = { "version": { "type": "string" }, "command" : { "type": "string" }, "data": {} }; switch(command){ case DID_COMMANDS.CREATE_DID: schema.data = { "@context": { "type": "string" }, "did": { "type": "string" }, "publicKey": [ { "id" : { "type": "string" }, "type" : { "type": "string" }, "publicKeyPem" : { "type": "string" } } ], "authentication" : [ { "type" : { "type": "string" }, "publicKey" : { "type": "string" } } ] }; break; case DID_COMMANDS.ADD_KEYPAIR: schema.data = { "publicKey" : [ { "id" : { "type": "string" }, "type" : { "type": "string" }, "publicKeyPem": { "type": "string" } } ] } break; case DID_COMMANDS.ADD_SERVICEPOINT: schema.data = { "service" : [ { "id" : { "type": "string" }, "type" : { "type": "string" }, "serviceEndpoint": { "type": "string" } } ] } break; default: console.log("Unexpected schema.") } return tv4.validate(txData, schema); } public GetState() : DID { return this.state; } }