bigchaindb/docs/build/html/Serialization.html
2016-02-09 19:16:18 +01:00

168 lines
10 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Example &mdash; Bigchain 0.0.1 documentation</title>
<link rel="stylesheet" href="_static/alabaster.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '0.0.1',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="top" title="Bigchain 0.0.1 documentation" href="index.html" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
</head>
<body role="document">
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<p>We need to clearly define how to serialize a json object to calculate the hash.</p>
<p>The serialization should produce the same byte output independently of the architecture running the software. If
there are diferences in the serialization hash validations will fail altough the transaction is correct</p>
<div class="section" id="example">
<span id="example"></span><h1>Example<a class="headerlink" href="#example" title="Permalink to this headline"></a></h1>
<div class="highlight-python"><div class="highlight"><pre><span class="n">a</span> <span class="o">=</span> <span class="n">r</span><span class="o">.</span><span class="n">expr</span><span class="p">({</span><span class="s1">&#39;a&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span><span class="o">.</span><span class="n">to_json</span><span class="p">()</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">b</span><span class="o">.</span><span class="n">connection</span><span class="p">)</span>
<span class="s1">u&#39;{&quot;a&quot;:1}&#39;</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">({</span><span class="s1">&#39;a&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span>
<span class="s1">&#39;{&quot;a&quot;: 1}&#39;</span>
<span class="n">a</span> <span class="o">==</span> <span class="n">b</span>
<span class="bp">False</span>
</pre></div>
</div>
<p>We should provide the serialization and deserialization so that the following is always true.</p>
</div>
<div class="section" id="example">
<span id="id1"></span><h1>Example<a class="headerlink" href="#example" title="Permalink to this headline"></a></h1>
<div class="highlight-python"><div class="highlight"><pre><span class="n">deserialize</span><span class="p">(</span><span class="n">serialize</span><span class="p">(</span><span class="n">data</span><span class="p">))</span> <span class="o">==</span> <span class="n">data</span>
<span class="bp">True</span>
</pre></div>
</div>
</div>
<div class="section" id="standard-serialization-for-the-bigchain">
<span id="standard-serialization-for-the-bigchain"></span><h1>Standard serialization for the bigchain<a class="headerlink" href="#standard-serialization-for-the-bigchain" title="Permalink to this headline"></a></h1>
<p>After looking at this further I think that the python json module is still the best bet because it
complies with the RFC. We can specify the encoding, separators used and enforce it to order by the keys to
make sure that we obtain maximum interopelability.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">json</span>
<span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">skipkeys</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">ensure_ascii</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="s2">&quot;utf-8&quot;</span><span class="p">,</span>
<span class="n">separators</span><span class="o">=</span><span class="p">(</span><span class="s1">&#39;,&#39;</span><span class="p">,</span> <span class="s1">&#39;:&#39;</span><span class="p">),</span> <span class="n">sort_keys</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
<ul class="simple">
<li><code class="docutils literal"><span class="pre">skipkeys</span></code>: With skipkeys <code class="docutils literal"><span class="pre">False</span></code> if the provided keys are not a string the serialization will fail. This way we
enforce all keys to be strings</li>
<li><code class="docutils literal"><span class="pre">ensure_ascii</span></code>: The RFC recommends <code class="docutils literal"><span class="pre">utf-8</span></code> for maximum interoperability. By setting ensure_ascii to <code class="docutils literal"><span class="pre">False</span></code> we
allow unicode characters and force the encoding to <code class="docutils literal"><span class="pre">utf-8</span></code>.</li>
<li><code class="docutils literal"><span class="pre">separators</span></code>: We need to define a standard separator to use in the serialization. We did not do this different
implementations could use different separators for serialization resulting in a still valid transaction but with
a different hash e. g. an extra whitespace introduced in the serialization would not still create a valid json object
but the hash would be different</li>
</ul>
<div class="section" id="example">
<span id="id2"></span><h2>Example<a class="headerlink" href="#example" title="Permalink to this headline"></a></h2>
<p>Everytime we need to perform some operation on the data like calculating the hash or signing/verifying the transaction
we need to use the previous criteria to serialize the data and then use the <code class="docutils literal"><span class="pre">byte</span></code> representation of the serialized
data (if we threat the data as bytes we eliminate possible enconding errors e.g. unicode characters)</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c1"># calculate the hash of a transaction</span>
<span class="c1"># the transaction is a dictionary</span>
<span class="n">tx_serialized</span> <span class="o">=</span> <span class="nb">bytes</span><span class="p">(</span><span class="n">serialize</span><span class="p">(</span><span class="n">tx</span><span class="p">))</span>
<span class="n">tx_hash</span> <span class="o">=</span> <span class="n">hashlib</span><span class="o">.</span><span class="n">sha3_256</span><span class="p">(</span><span class="n">tx_serialized</span><span class="p">)</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>
<span class="c1"># signing a transaction</span>
<span class="n">tx_serialized</span> <span class="o">=</span> <span class="nb">bytes</span><span class="p">(</span><span class="n">serialize</span><span class="p">(</span><span class="n">tx</span><span class="p">))</span>
<span class="n">signature</span> <span class="o">=</span> <span class="n">sk</span><span class="o">.</span><span class="n">sign</span><span class="p">(</span><span class="n">tx_serialized</span><span class="p">)</span>
<span class="c1"># verify signature</span>
<span class="n">tx_serialized</span> <span class="o">=</span> <span class="nb">bytes</span><span class="p">(</span><span class="n">serialize</span><span class="p">(</span><span class="n">tx</span><span class="p">))</span>
<span class="n">vk</span><span class="o">.</span><span class="n">verify</span><span class="p">(</span><span class="n">signature</span><span class="p">,</span> <span class="n">tx_serialized</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Example</a></li>
<li><a class="reference internal" href="#example">Example</a></li>
<li><a class="reference internal" href="#standard-serialization-for-the-bigchain">Standard serialization for the bigchain</a><ul>
<li><a class="reference internal" href="#example">Example</a></li>
</ul>
</li>
</ul>
<div class="relations">
<h3>Related Topics</h3>
<ul>
<li><a href="index.html">Documentation overview</a><ul>
</ul></li>
</ul>
</div>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/Serialization.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
&copy;2016, ascribe GmbH.
|
Powered by <a href="http://sphinx-doc.org/">Sphinx 1.3.5</a>
&amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.7</a>
|
<a href="_sources/Serialization.txt"
rel="nofollow">Page source</a>
</div>
</body>
</html>