Skip to content

Commit

Permalink
Publish metadata and contract sources
Browse files Browse the repository at this point in the history
  • Loading branch information
axic committed Mar 15, 2017
1 parent 922c533 commit bd0a6ea
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* global alert, confirm, prompt, FileReader, Option, Worker, chrome */
'use strict'

var async = require('async')
var $ = require('jquery')
var base64 = require('js-base64').Base64
var swarmgw = require('swarmgw')
Expand Down Expand Up @@ -657,13 +658,53 @@ var run = function () {
})
}

function publishOnSwarm (contract, cb) {
// gather list of files to publish
var sources = []

sources.push({
content: contract.metadata,
hash: contract.metadataHash
})

var metadata
try {
metadata = JSON.parse(contract.metadata)
} catch (e) {
return cb(e)
}

if (metadata === undefined) {
return cb('No metadata')
}

Object.keys(metadata.sources).forEach(function (fileName) {
// find hash
var hash
try {
hash = metadata.sources[fileName].urls[0].match('bzzr://(.+)')[1]
} catch (e) {
return cb('Metadata inconsistency')
}

sources.push({
content: files.get(fileName),
hash: hash
})
})

// publish the list of sources in order, fail if any failed
async.eachSeries(sources, function (item, cb) {
swarmVerifiedPublish(item.content, item.hash, cb)
}, cb)
}

udapp.event.register('publishContract', this, function (contract) {
console.log('Publish contract...', contract)
swarmVerifiedPublish(contract.metadata, contract.metadataHash, function (err) {
publishOnSwarm(contract, function (err) {
if (err) {
alert('Failed to publish metadata: ' + err)
} else {
alert('Published metadata')
alert('Metadata published successfully')
}
})
})
Expand Down

0 comments on commit bd0a6ea

Please sign in to comment.