-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bundle related functions moved from EntityBuilder to BundleBuilder; E…
…ntityDownloader changed name to BundleDownloader; Bundle repository extracted out of entity repository; Removed some unused methods and updated documentation; proofBlock renamed to bundleProofBlock; (#312)
- Loading branch information
1 parent
361efac
commit 06a1da0
Showing
17 changed files
with
724 additions
and
666 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
Copyright: Ambrosus Technologies GmbH | ||
Email: tech@ambrosus.com | ||
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
This Source Code Form is “Incompatible With Secondary Licenses”, as defined by the Mozilla Public License, v. 2.0. | ||
*/ | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export const up = async (db, config, logger) => { | ||
await db.collection('bundle_metadata').updateMany({}, {$rename: {proofBlock: 'bundleProofBlock'}}); | ||
|
||
logger.info(`Renamed proofBlock to bundleProofBlock in bundle metadata`); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
Copyright: Ambrosus Technologies GmbH | ||
Email: tech@ambrosus.com | ||
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
This Source Code Form is “Incompatible With Secondary Licenses”, as defined by the Mozilla Public License, v. 2.0. | ||
*/ | ||
|
||
import validateAndCast from '../utils/validations'; | ||
|
||
export default class BundleBuilder { | ||
constructor(identityManager, entityBuilder) { | ||
this.identityManager = identityManager; | ||
this.entityBuilder = entityBuilder; | ||
} | ||
|
||
assembleBundle(assets, events, timestamp, secret) { | ||
const createdBy = this.identityManager.addressFromSecret(secret); | ||
const preparedEvents = events.map((event) => this.entityBuilder.prepareEventForBundlePublication(event)); | ||
const entries = [ | ||
...assets, | ||
...preparedEvents | ||
].map((entry) => this.entityBuilder.removeBundle(entry)); | ||
const entriesHash = this.identityManager.calculateHash(entries); | ||
const idData = { | ||
createdBy, | ||
entriesHash, | ||
timestamp | ||
}; | ||
const signature = this.identityManager.sign(secret, idData); | ||
const content = { | ||
signature, | ||
idData, | ||
entries | ||
}; | ||
const bundleId = this.identityManager.calculateHash(content); | ||
|
||
return { | ||
bundleId, | ||
content | ||
}; | ||
} | ||
|
||
validateBundle(bundle) { | ||
validateAndCast(bundle) | ||
.required([ | ||
'bundleId', | ||
'content.signature', | ||
'content.idData', | ||
'content.idData.createdBy', | ||
'content.idData.timestamp', | ||
'content.idData.entriesHash', | ||
'content.entries' | ||
]) | ||
.fieldsConstrainedToSet(['content', 'bundleId', 'metadata']) | ||
.fieldsConstrainedToSet(['idData', 'entries', 'signature'], 'content') | ||
.isNonNegativeInteger(['content.idData.timestamp']) | ||
.validate( | ||
['bundleId'], | ||
(hash) => this.identityManager.checkHashMatches(hash, bundle.content), | ||
`bundleId value doesn't match the content hash` | ||
) | ||
.validate( | ||
['content.idData.entriesHash'], | ||
(hash) => this.identityManager.checkHashMatches(hash, bundle.content.entries), | ||
`entriesHash value doesn't match the entries hash` | ||
); | ||
|
||
this.identityManager.validateSignature( | ||
bundle.content.idData.createdBy, | ||
bundle.content.signature, | ||
bundle.content.idData | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
Copyright: Ambrosus Technologies GmbH | ||
Email: tech@ambrosus.com | ||
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
This Source Code Form is “Incompatible With Secondary Licenses”, as defined by the Mozilla Public License, v. 2.0. | ||
*/ | ||
|
||
export default class BundleRepository { | ||
constructor(db) { | ||
this.db = db; | ||
this.blacklistedFields = { | ||
_id: 0, | ||
repository: 0 | ||
}; | ||
} | ||
|
||
async storeBundle(bundle, storagePeriods) { | ||
if (await this.db.collection('bundles').findOne({bundleId: bundle.bundleId}) === null) { | ||
await this.db.collection('bundles').insertOne({...bundle}); | ||
} | ||
if (await this.db.collection('bundle_metadata').findOne({bundleId: bundle.bundleId}) === null) { | ||
await this.db.collection('bundle_metadata').insertOne({bundleId: bundle.bundleId, storagePeriods}); | ||
} | ||
} | ||
|
||
async storeBundleProofMetadata(bundleId, proofBlock, timestamp, txHash) { | ||
await this.db.collection('bundle_metadata').updateOne({bundleId}, { | ||
$set: { | ||
bundleTransactionHash: txHash, | ||
bundleProofBlock: proofBlock, | ||
bundleUploadTimestamp: timestamp | ||
} | ||
}); | ||
} | ||
|
||
async findBundlesWaitingForUpload() { | ||
return await this.db.collection('bundle_metadata') | ||
.find({bundleProofBlock: {$exists: false}}) | ||
.toArray(); | ||
} | ||
|
||
async storeBundleShelteringExpirationDate(bundleId, expirationDate) { | ||
await this.db.collection('bundles').updateOne({bundleId}, { | ||
$set: { | ||
'repository.holdUntil': expirationDate | ||
} | ||
}); | ||
} | ||
|
||
async getBundle(bundleId) { | ||
return await this.db.collection('bundles').findOne({bundleId}, {projection: this.blacklistedFields}); | ||
} | ||
|
||
async getBundleMetadata(bundleId) { | ||
return await this.db.collection('bundle_metadata').findOne({bundleId}, {projection: this.blacklistedFields}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.