From ff7697bf4bca5b786210a541fd7fcddba35f911c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chuck=20LeDuc=20D=C3=ADaz?= Date: Wed, 29 Apr 2020 20:24:07 +0200 Subject: [PATCH] use did-resolver for all blockchain stuff; v1.2.0 --- package.json | 8 ++- src/index.js | 115 ++++++++++++-------------------------------- test/lorena-test.js | 12 ----- 3 files changed, 33 insertions(+), 102 deletions(-) diff --git a/package.json b/package.json index 4483e42..016795a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@lorena-ssi/lorena-sdk", - "version": "1.1.14", + "version": "1.2.0", "description": "Lorena SDK", "author": "Alex Puig ", "license": "MIT", @@ -23,11 +23,9 @@ }, "homepage": "https://github.com/lorena-ssi/lorena-sdk#readme", "dependencies": { - "@lorena-ssi/credential-lib": "^1.1.0", - "@lorena-ssi/did-resolver": "^0.2.3", + "@lorena-ssi/credential-lib": "^1.1.1", + "@lorena-ssi/did-resolver": "^0.3.0", "@lorena-ssi/matrix-lib": "^1.0.13", - "@lorena-ssi/maxonrow-lib": "^1.2.0", - "@lorena-ssi/substrate-lib": "^3.0.0", "@lorena-ssi/wallet-lib": "^1.1.0", "@lorena-ssi/zenroom-lib": "^1.5.0", "debug": "^4.1.1", diff --git a/src/index.js b/src/index.js index fe3b9a0..2f76731 100644 --- a/src/index.js +++ b/src/index.js @@ -1,8 +1,6 @@ import Matrix from '@lorena-ssi/matrix-lib' import Zenroom from '@lorena-ssi/zenroom-lib' import Credential from '@lorena-ssi/credential-lib' -import SubstrateBlockchain from '@lorena-ssi/substrate-lib' -import MaxonrowBlockchain from '@lorena-ssi/maxonrow-lib' import LorenaDidResolver from '@lorena-ssi/did-resolver' import { Resolver } from 'did-resolver' import { EventEmitter } from 'events' @@ -10,10 +8,6 @@ import log from 'debug' const debug = log('did:debug:sdk') -const didValue = (did) => { - const didValue = did.split(':') - return didValue[3] -} /** * Lorena SDK - Class */ @@ -29,7 +23,6 @@ export default class Lorena extends EventEmitter { this.zenroom = new Zenroom(opts.silent || false) this.wallet = walletHandler this.matrix = false - this.blockchain = false this.recipeId = 0 this.queue = [] this.processing = false @@ -37,6 +30,7 @@ export default class Lorena extends EventEmitter { this.nextBatch = '' this.disconnecting = false this.threadId = 0 + this.resolver = false } /** @@ -116,15 +110,6 @@ export default class Lorena extends EventEmitter { this.wallet.info.person = person.subject } - /* this.zenroom.randomDID() - .then((did) => { - this.wallet.info.did = 'did:lor:' + network + ':' + did - return this.zenroom.newKeyPair(this.wallet.info.did) - }) - .then((keyPair) => { - this.wallet.info.keyPair = keyPair - return */ - async signCredential (subject) { return new Promise((resolve) => { // Sign the persona @@ -149,10 +134,9 @@ export default class Lorena extends EventEmitter { /** * Connect to Lorena IDspace. * - * @param {string=} network The network to which we will connect * @returns {boolean} success (or errors thrown) */ - async connect (network = undefined) { + async connect () { if (this.ready === true) return true else if (this.wallet.info.matrixUser) { try { @@ -160,59 +144,6 @@ export default class Lorena extends EventEmitter { this.matrix = new Matrix(this.wallet.info.matrixServer) await this.matrix.connect(this.wallet.info.matrixUser, this.wallet.info.matrixPass) - let info - // If we know the DID, get the info for the network of the did - if (this.wallet.info.did && this.wallet.info.did !== '') { - info = LorenaDidResolver.getInfoForDid(this.wallet.info.did) - // If we don't know the did, get it for the network already specified (or in arguments) - } else if (this.wallet.info.network || network) { - info = LorenaDidResolver.getInfoForNetwork(this.wallet.info.network || network) - // old wallets don't have a network, but they do have a blockchain - } else if (this.wallet.info.blockchainServer) { - // Take the network name from the first position in the hostname - const network = this.wallet.info.blockchainServer.split('//')[1].split('.')[0] - info = LorenaDidResolver.getInfoForNetwork(network) - this.wallet.info.network = network - // If we don't know the did or the network we're in trouble - } else { - debug('connect: unknown network type') - throw new Error('Connect: Unknown network type') - } - - const nodeProvider = { - connection: { - url: info.blockchainEndpoint, - timeout: 60000 - }, - trace: { - silent: true, - silentRpc: true - }, - chainId: 'maxonrow-chain', - name: 'mxw', - // Only necessary to create Tokens - nonFungibleToken: { - provider: 'unknown', - issuer: 'unknown', - middleware: 'unknown', - feeCollector: 'unknown' - } - } - - // Connect to Blockchain. - switch (info.type) { - case 'maxonrow': - this.blockchain = new MaxonrowBlockchain(info.symbol, nodeProvider) - break - case 'substrate': - this.blockchain = new SubstrateBlockchain(info.blockchainEndpoint) - break - default: - throw new Error(`Unsupported network type ${info.type}`) - } - - await this.blockchain.connect() - // Ready to use events. const events = await this.matrix.events('') this.nextBatch = events.nextBatch @@ -236,9 +167,6 @@ export default class Lorena extends EventEmitter { disconnect () { this.emit('disconnecting') this.disconnecting = true - if (this.blockchain) { - this.blockchain.disconnect() - } } /** @@ -391,19 +319,37 @@ export default class Lorena extends EventEmitter { return this.oneMsg(`message:${recipe}`) } + async getDiddoc (did) { + if (!this.resolver) { + const lorResolver = LorenaDidResolver.getResolver() + this.resolver = new Resolver(lorResolver, true) + } + const diddoc = await this.resolver.resolve(did) + return diddoc + } + + async getMatrixUserIDForDID (did) { + const diddoc = await this.getDiddoc(did) + const matrixUserID = diddoc.service[0].serviceEndpoint + return matrixUserID + } + + async getPublicKeyForDID (did) { + const diddoc = await this.getDiddoc(did) + const publicKey = diddoc.authentication[0].publicKey + return publicKey + } + /** * Open Connection with another user. * * @param {string} did DID - * @param {string} matrixUrl Matrix user ID/Url + * @param {string} matrixUserID Matrix user ID in format @username:home.server.xxx * @returns {Promise} Room ID created, or false */ - async createConnection (did, matrixUrl) { - if (matrixUrl === undefined) { - const lorResolver = LorenaDidResolver.getResolver() - const resolver = new Resolver(lorResolver) - const diddoc = await resolver.resolve(did) - matrixUrl = diddoc.service[0].serviceEndpoint + async createConnection (did, matrixUserID) { + if (matrixUserID === undefined) { + matrixUserID = await this.getMatrixUserIDForDID(did) } const link = { @@ -412,12 +358,12 @@ export default class Lorena extends EventEmitter { roomId: '', roomName: await this.zenroom.random(12), keyPair: false, - matrixUser: matrixUrl, + matrixUser: matrixUserID, status: 'invited', alias: '' } return new Promise((resolve, reject) => { - this.matrix.createConnection(link.roomName, matrixUrl) + this.matrix.createConnection(link.roomName, matrixUserID) .then((roomId) => { link.roomId = roomId this.wallet.add('links', link) @@ -460,8 +406,7 @@ export default class Lorena extends EventEmitter { }) .then(async (result) => { if (result === false) throw (new Error('Timeout')) - const key = await this.blockchain.getActualDidKey(didValue(link.linkDid)) - // debug(`memberOf: getActualDidKey result ${key}`) + const key = await this.getPublicKeyForDID(link.linkDid) if (key === '') { debug(`memberOf: Public key not found for ${link.did}`) throw new Error(`Public key not found for ${link.did}`) diff --git a/test/lorena-test.js b/test/lorena-test.js index 37d25ab..ce2802a 100644 --- a/test/lorena-test.js +++ b/test/lorena-test.js @@ -6,16 +6,6 @@ import { describe, it } from 'mocha' chai.use(require('chai-as-promised')) chai.use(require('chai-spies')) -const lorenaKeys = [ - 'matrix', - 'blockchain', - 'opts', - '_events', - '_eventsCount', - '_maxListeners', - 'wallet' -] - describe('Lorena API', function () { let lorena, wallet const password = 'test' @@ -30,12 +20,10 @@ describe('Lorena API', function () { it('should construct a Lorena class', async () => { lorena = new Lorena(wallet) - expect(lorena).to.include.all.keys(lorenaKeys) }) it('should construct a Lorena class with debug', async () => { lorena = new Lorena(wallet, { debug: true, silent: true }) - expect(lorena).to.include.all.keys(lorenaKeys) }) it('should not init wallet for an invalid network', async () => {