Skip to content

Instantly share code, notes, and snippets.

@axic
Created July 15, 2016 16:24
Show Gist options
  • Save axic/fb2506c94d95e7101f528721025497b9 to your computer and use it in GitHub Desktop.
Save axic/fb2506c94d95e7101f528721025497b9 to your computer and use it in GitHub Desktop.
Web3 demo code for Swarm
//
// Needs web3.js from https://github.com/axic/web3.js/tree/swarm
//
var Web3 = require('web3')
var url = require('url')
var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'))
try {
web3.eth.defaultAccount = web3.eth.coinbase
} catch (e) {
}
var swarmPut = function (buf, enc, cb) {
web3.bzz.put(buf, enc, function (err, ret) {
if (err) {
return cb(err)
}
cb(null, ret)
})
}
var swarmGet = function (key, cb) {
web3.bzz.get('bzz://' + key, function (err, ret) {
if (err) {
return cb(err)
}
cb(null, ret.content)
})
}
swarmPut('Hello World', 'application/text', function (err, ret) {
if (err) {
console.log('Swarm put failed: ', err)
return
}
console.log('Added to swarm: ', ret)
swarmGet(ret, function (err, ret) {
if (err) {
console.log('Swarm get failed: ', err)
return
}
console.log('Retrieved from swarm: ', ret)
})
})
@axic
Copy link
Author

axic commented Jul 15, 2016

Should have an output similar to:

$ node sampleswarm.js 
Added to swarm:  f0934b1877f410e103d49256d02cf99227e8e62151b77e31cb82c7474696a8c1
Retrieved from swarm:  Hello World

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment