Hello!
Datacoin blockchain started about two months ago. Thank you to all of you supporting Datacoin. It exists because of your support.
Now we can start developing blockchain-based web applications. Note that this feature doesn't touch sources of Datacoin daemon or qt-wallet: if you are not interested in this it you can still use Datacoins and without understanding this post. Blockchain-based web applications require a http-server that redirects HTTP requests from browser to daemon. This is implemented now with Perl (dtc_http_server.pl). Yes, I know I would have to do it using C++. I'll rewrite it later.
Why web-applications inside blockchain are good?
1. they are protected from being deleted or censored
2. they work locally and don't require a live Internet connection
3. web interface is convenient to users
4. no new technologies are required for developement: HTTP, AJAX and JSON RPC are simply linked together
Datacoin HTTP server is very simple and it is intended to work locally on end-user's machine.
Types of requests supported by Datacoin local HTTP serverNB:
NET can be 'dtc' or 'dtc-testnet'
1. Get data from blockchainhttp://localhost:PORT/NET/get/TXHASH -> data from tx with TXHASH will be sent back (data is assumed to be in Envelope format - see envelope.proto file)
Example:
http://localhost:8080/dtc/get/40a3eb9624528008a87d9119dbed4041d45c9f6483ecd52a7b24f9c8b4fdf5482. Call daemon's methodhttp://localhost:PORT/NET/rpc/METHOD(/ARGS) -> json-rpc call to daemon's METHOD will be performed and resulting json will be sent back to browser
Several arguments are possible in form arg1/arg2/arg3/...
Example:
http://localhost:8080/dtc/rpc/getblockhash/1000{
"result" : "ce3ca3728ee050bf14d8f9a059f13c0062ca303cf2c982796c7dec2ce10f13c6",
"error" : null,
"id" : null
}
NB: due to security reasons only
'getinfo',
'getmininginfo',
'getblock',
'getblockhash' and
'getrawtransaction' are supported now. Later all reading methods will be added.
'sendtoaddress' and other money-spending methods probably will be added also but some security issues are to be considered before in order to protect users from theft.
3. Call http local server's methodhttp://localhost:PORT/NET/lsrpc/METHOD(/ARGS) -> http server will handle this method itself (or translate to several requests to daemon).
There is only one example of lsrpc methods now:
'getenvelope'. It parses
tx.data field of a given transaction as an Envelope and returns envelope in json format (without 'Data'). This method is needed to get metadata easily.
Example:
http://localhost:8080/dtc/lsrpc/getenvelope/40a3eb9624528008a87d9119dbed4041d45c9f6483ecd52a7b24f9c8b4fdf548{
"result":
{
"FileName":"blockexplorer.html",
"ContentType":"text/html",
"Compression":"1",
"PublicKey" : "MIGJA... a lot of base64 letters ...AE=",
}
}
DemoIn order to demonstrate this in action I've sent 'Block explorer' application in transaction
40a3eb9624528008a87d9119dbed4041d45c9f6483ecd52a7b24f9c8b4fdf548. Here is an instruction on how to access it from your own computer with local HTTP server (actually this will be too difficult for 99% of users, I need to rewrite server using C++ and deliver it as a precompiled binary):
Before you start: DON'T do this on the machine with many coins!!! Setup a different user account on Windows (or Linux) or a virtual machine. There is a lot of possible bugs in this code that can lead to loss of money.
Now you are warned. Let's continue.1. backup yout wallet.dat and protect it with password
2. git clone
https://github.com/foo1inge/datacoin-browser.git3. install all required CPAN modules (the way to do this depends from your Perl distribution, ask Perl guru if you don't know how to do this). Here is list of modules:
HTTP::Server::Simple::CGI
Google::ProtocolBuffers
MIME::Base64
IO::Compress::Bzip2
IO::Uncompress::Bunzip2
Crypt::OpenSSL::RSA
JSON::RPC::Client
File::HomeDir
Data::Dumper
4. start Datacoin daemon (
'getdata' RPC method is required. It was commited two weeks ago and probably you have to update your daemon).
5.
cd datacoin-browser/perl/6.
perl dtc_http_server.pl --http-port=8080This will start local HTTP server on port 8080. In order to see Datacoin testnet add
"--testnet=1" option - it will connect to daemon working in testnet.
7. go to
http://localhost:8080/dtc/get/40a3eb9624528008a87d9119dbed4041d45c9f6483ecd52a7b24f9c8b4fdf548 8. enjoy
You can deploy this on any machine. Here is this application deployed on Amazon EC2 (micro instance, very slow):
http://ec2-54-201-144-165.us-west-2.compute.amazonaws.com/dtc/get/40a3eb9624528008a87d9119dbed4041d45c9f6483ecd52a7b24f9c8b4fdf548Blockexplorer application consists of three transactions:
40a3eb9624528008a87d9119dbed4041d45c9f6483ecd52a7b24f9c8b4fdf548 - blockexplorer.html
465ca75b6dedf70bc85db24548ee99bab776028f9295116afba4fa1e4838ff5f - blockexplorer.js
73b7c7165a51f604040e17681705a39145392691a0447266b9074a2ad9140327 - datacoin-logo.pngAll these files are accessed by TXID (not by names!) the following way (snippets from
blockexplorer.html):
src="/dtc/get/465ca75b6dedf70bc85db24548ee99bab776028f9295116afba4fa1e4838ff5f"
type="text/javascript"
encoding="UTF-8">
src="73b7c7165a51f604040e17681705a39145392691a0447266b9074a2ad9140327" alt="Datacoin logo"/>
|
Blockexplorer does following steps while starting:
1.
getinfo RPC call (to get current height)
2.
getblockhash /
getblock for each block in cycle
3.
getrawtransaction for each transaction
4.
getenvelope for each transaction with data
All these calls are coded as ordinary AJAX in
blockexplorer.html.
Important issues for web developers on how to develop applications for Datacoin1. Start development in testnet
Testnet block explorer:
http://ec2-54-201-144-165.us-west-2.compute.amazonaws.com:81/dtc-testnet/get/7fead7473c448b2d69d0e62354dedd59895a1ab099c1835155d83f7327c5156a 2. Never use specific IP or PORT in URLs - only relative URLs are meaningful (user can start local HTTP server on any IP:PORT).
In your HTML code instead of doing
use relative URL:
3. Use
"--content-type" argument of
dtc_put_file.pl script to set a
"Content-type" HTTP attribute for your files.
Here is an example:
perl dtc_put_file.pl --content-type="text/html" blockexplorer.html
4. Use
"--add_key" argument of
dtc_put_file.pl script and save private key (it will be printed on screen) in a secure place in order to upate your application later.
Here is a full example:
perl dtc_put_file.pl --add_key=true --content-type="text/html" blockexplorer.html
NB: update feature isn't implemented yet
I'm working on it now.
Datacoin development plans regarding blockchain-based web-applications:(I don't mention here issues not related to the topic)
1. bugfixing
2. Update feature (see detailed description of update scenario in envelope.proto file)
3. Big files (more 128Kb) feature
4. C++ version of local HTTP server
5. "senddata" from local HTTP server
6. Access to P2P file sharing networks from local HTTP server (something like localhost:8080/kademlia/HASH). Needs some investigation.