Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix ddo encrypt for browsers #822

Merged
merged 3 commits into from
May 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/metadatacache/MetadataCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,12 @@ export class MetadataCache {
* @return {Promise<String>} Hex encoded encrypted DDO.
*/
public async encryptDDO(ddo: any): Promise<any> {
const fullUrl = `${this.url}/api/v1/aquarius/assets/ddo/encrypt`
const fullUrl = `${this.url}/api/v1/aquarius/assets/ddo/encryptashex `
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we we stick to one form of encryption with one encrypt endpoint in Aquarius instead of adding multiple endpoints for one use case. If we want to support multiple types of encryptions then we should stick to REST principles and use the same endpoint, but with e.g. a body param

But I guess it's yet another hack for one use case. At least we should type what this methods returns which seems to be now Promise<string>.

And what's the implications for users who have used this encryptDDO method before, is this a breaking change for them?

const result = await this.fetch
.postWithOctet(fullUrl, ddo)
.then((response: Response) => {
if (response.ok) {
return response.blob()
return response.text()
}
this.logger.error('encryptDDO failed:', response.status, response.statusText, ddo)
return null
Expand Down
22 changes: 9 additions & 13 deletions src/metadatacache/OnChainMetaData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ export class OnChainMetadata {
encrypt: boolean = false
): Promise<TransactionReceipt> {
const rawData = await this.prepareRawData(ddo, encrypt)
return this.publishRaw(didZeroX(did), rawData.flags, rawData.data, consumerAccount)
if (!rawData) {
throw new Error(`Could not prepare raw data for publish`)
} else
return this.publishRaw(didZeroX(did), rawData.flags, rawData.data, consumerAccount)
}

/**
Expand All @@ -92,7 +95,10 @@ export class OnChainMetadata {
encrypt: boolean = false
): Promise<TransactionReceipt> {
const rawData = await this.prepareRawData(ddo, encrypt)
return this.updateRaw(didZeroX(did), rawData.flags, rawData.data, consumerAccount)
if (!rawData) {
throw new Error(`Could not prepare raw data for udate`)
} else
return this.updateRaw(didZeroX(did), rawData.flags, rawData.data, consumerAccount)
}

/**
Expand All @@ -109,17 +115,7 @@ export class OnChainMetadata {
flags = flags | 1
data = this.getHex(data)
} else {
const blob = await this.metadataCache.encryptDDO(data)
try {
const rawBuffer = (await new Response(blob).arrayBuffer()) as any
data =
'0x' +
Array.prototype.map
.call(new Uint8Array(rawBuffer), (x) => ('00' + x.toString(16)).slice(-2))
.join('')
} catch (e) {
console.error(e)
}
data = await this.metadataCache.encryptDDO(data)
if (!data) return null
flags = flags | 2
}
Expand Down