Created
July 15, 2016 16:24
-
-
Save axic/fb2506c94d95e7101f528721025497b9 to your computer and use it in GitHub Desktop.
Web3 demo code for Swarm
This file contains hidden or 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
// | |
// 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) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Should have an output similar to: