1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00
This commit is contained in:
alexcos20 2020-02-11 22:15:42 +02:00
parent dc53af095c
commit b29ac25de1
3 changed files with 41 additions and 19 deletions

View File

@ -3,7 +3,6 @@ import { DDO } from '../ddo/DDO'
import DID from '../ocean/DID' import DID from '../ocean/DID'
import { Logger } from '../utils' import { Logger } from '../utils'
import { WebServiceConnector } from '../ocean/utils/WebServiceConnector' import { WebServiceConnector } from '../ocean/utils/WebServiceConnector'
import { Account } from '../squid'
const apiPath = '/api/v1/aquarius/assets/ddo' const apiPath = '/api/v1/aquarius/assets/ddo'
@ -250,7 +249,6 @@ export class Aquarius {
return result return result
} }
/** /**
* Transfer ownership of a DDO * Transfer ownership of a DDO
* @param {DID | string} did DID of the asset to update. * @param {DID | string} did DID of the asset to update.
@ -259,11 +257,23 @@ export class Aquarius {
* @param {String} signature Signature using updated field to verify that the consumer has rights * @param {String} signature Signature using updated field to verify that the consumer has rights
* @return {Promise<String>} Result. * @return {Promise<String>} Result.
*/ */
public async transferOwnership(did: DID | string, newOwner: string, updated: string, signature: string): Promise<String> { public async transferOwnership(
did: DID | string,
newOwner: string,
updated: string,
signature: string
): Promise<string> {
did = did && DID.parse(did) did = did && DID.parse(did)
const fullUrl = `${this.url}${apiPath}/transferownership/${did.getDid()}` const fullUrl = `${this.url}${apiPath}/transferownership/${did.getDid()}`
const result = await this.fetch const result = await this.fetch
.put(fullUrl, JSON.stringify({signature: signature, updated: updated, newowner: newOwner})) .put(
fullUrl,
JSON.stringify({
signature: signature,
updated: updated,
newowner: newOwner
})
)
.then((response: any) => { .then((response: any) => {
if (response.ok) { if (response.ok) {
return response.text return response.text
@ -273,9 +283,9 @@ export class Aquarius {
response.status, response.status,
response.statusText response.statusText
) )
return null return null
}) })
.catch(error => { .catch(error => {
this.logger.error('Error updating metadata: ', error) this.logger.error('Error updating metadata: ', error)
return null return null

View File

@ -429,10 +429,18 @@ export class OceanAssets extends Instantiable {
newOwner, newOwner,
oldOwner oldOwner
) )
//get a signature // get a signature
const signature=await this.ocean.utils.signature.signForAquarius(oldDdo.updated, account) const signature = await this.ocean.utils.signature.signForAquarius(
if(signature!=null) oldDdo.updated,
await this.ocean.aquarius.transferOwnership(did,newOwner,oldDdo.updated,signature) account
)
if (signature != null)
await this.ocean.aquarius.transferOwnership(
did,
newOwner,
oldDdo.updated,
signature
)
return txReceipt return txReceipt
} }

View File

@ -42,22 +42,27 @@ export class SignatureUtils {
return this.web3.eth.personal.ecRecover(text, signature) return this.web3.eth.personal.ecRecover(text, signature)
} }
public async getHash(message:string): Promise<string> { public async getHash(message: string): Promise<string> {
var hex = '' let hex = ''
for(var i=0;i<message.length;i++) { for (let i = 0; i < message.length; i++) {
hex += ''+message.charCodeAt(i).toString(16) hex += '' + message.charCodeAt(i).toString(16)
} }
var hexMessage = "0x" + hex const hexMessage = '0x' + hex
return hexMessage as string return hexMessage as string
} }
public async signForAquarius(message:string, account:Account): Promise<string> {
const hash=await this.getHash(message) public async signForAquarius(message: string, account: Account): Promise<string> {
const hash = await this.getHash(message)
const isMetaMask = const isMetaMask =
this.web3 && this.web3 &&
this.web3.currentProvider && this.web3.currentProvider &&
(this.web3.currentProvider as any).isMetaMask (this.web3.currentProvider as any).isMetaMask
try { try {
return this.web3.eth.personal.sign(hash, account.getId(),account.getPassword()) return this.web3.eth.personal.sign(
hash,
account.getId(),
account.getPassword()
)
} catch (e) { } catch (e) {
if (isMetaMask) { if (isMetaMask) {
throw e throw e
@ -66,6 +71,5 @@ export class SignatureUtils {
this.logger.warn(e) this.logger.warn(e)
return null return null
} }
} }
} }