569 lines
43 KiB
HTML
569 lines
43 KiB
HTML
|
|
|
|
<!DOCTYPE html>
|
|
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
|
|
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
|
|
<head>
|
|
<meta charset="utf-8">
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
<title>Wallets — ethers.js 2.0.0 documentation</title>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
|
|
|
|
|
|
|
|
<link rel="stylesheet" href="_static/css/ethers.css" type="text/css" />
|
|
|
|
<link rel="stylesheet" href="_static/css/ethers.css" type="text/css" />
|
|
|
|
|
|
|
|
<link rel="top" title="ethers.js 2.0.0 documentation" href="index.html"/>
|
|
<link rel="up" title="Application Programming Interface (API)" href="api.html"/>
|
|
<link rel="next" title="Providers API" href="api-providers.html"/>
|
|
<link rel="prev" title="Application Programming Interface (API)" href="api.html"/>
|
|
|
|
|
|
<script src="_static/js/modernizr.min.js"></script>
|
|
|
|
</head>
|
|
|
|
<body class="wy-body-for-nav" role="document">
|
|
|
|
<div class="wy-grid-for-nav">
|
|
|
|
|
|
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
|
|
<div class="wy-side-scroll">
|
|
<div class="wy-side-nav-search">
|
|
|
|
|
|
|
|
<a href="index.html" class="icon icon-home"> ethers.js
|
|
|
|
|
|
|
|
</a>
|
|
|
|
|
|
|
|
|
|
<div class="version">
|
|
2.0.0
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div role="search">
|
|
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
|
|
<input type="text" name="q" placeholder="Search docs" />
|
|
<input type="hidden" name="check_keywords" value="yes" />
|
|
<input type="hidden" name="area" value="default" />
|
|
</form>
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
|
|
|
|
|
|
|
|
<p class="caption"><span class="caption-text">Developer Documentation</span></p>
|
|
<ul class="current">
|
|
<li class="toctree-l1"><a class="reference internal" href="getting-started.html">Getting Started</a></li>
|
|
<li class="toctree-l1 current"><a class="reference internal" href="api.html">Application Programming Interface (API)</a><ul class="current">
|
|
<li class="toctree-l2 current"><a class="current reference internal" href="#">Wallets</a><ul>
|
|
<li class="toctree-l3"><a class="reference internal" href="#creating-instances">Creating Instances</a><ul>
|
|
<li class="toctree-l4"><a class="reference internal" href="#examples"><em>Examples</em></a></li>
|
|
</ul>
|
|
</li>
|
|
<li class="toctree-l3"><a class="reference internal" href="#prototype">Prototype</a><ul>
|
|
<li class="toctree-l4"><a class="reference internal" href="#id1"><em>Examples</em></a></li>
|
|
</ul>
|
|
</li>
|
|
<li class="toctree-l3"><a class="reference internal" href="#blockchain-operations">Blockchain Operations</a><ul>
|
|
<li class="toctree-l4"><a class="reference internal" href="#id2"><em>Examples</em></a></li>
|
|
</ul>
|
|
</li>
|
|
<li class="toctree-l3"><a class="reference internal" href="#parsing-transactions">Parsing Transactions</a><ul>
|
|
<li class="toctree-l4"><a class="reference internal" href="#id3"><em>Examples</em></a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
</li>
|
|
<li class="toctree-l2"><a class="reference internal" href="api-providers.html">Providers API</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="api-contract.html">Contracts</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="api-utils.html">Utilities</a></li>
|
|
</ul>
|
|
</li>
|
|
<li class="toctree-l1"><a class="reference internal" href="api-advanced.html">Low-Level API</a></li>
|
|
<li class="toctree-l1"><a class="reference internal" href="cookbook.html">Cookbook</a></li>
|
|
<li class="toctree-l1"><a class="reference internal" href="notes.html">Notes</a></li>
|
|
<li class="toctree-l1"><a class="reference internal" href="testing.html">Testing</a></li>
|
|
</ul>
|
|
|
|
|
|
|
|
</div>
|
|
</div>
|
|
</nav>
|
|
|
|
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
|
|
|
|
|
|
<nav class="wy-nav-top" role="navigation" aria-label="top navigation">
|
|
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
|
|
<a href="index.html">ethers.js</a>
|
|
</nav>
|
|
|
|
|
|
|
|
<div class="wy-nav-content">
|
|
<div class="rst-content">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<div role="navigation" aria-label="breadcrumbs navigation">
|
|
<ul class="wy-breadcrumbs">
|
|
<li><a href="index.html">Docs</a> »</li>
|
|
|
|
<li><a href="api.html">Application Programming Interface (API)</a> »</li>
|
|
|
|
<li>Wallets</li>
|
|
<li class="wy-breadcrumbs-aside">
|
|
|
|
|
|
<a href="_sources/api-wallet.txt" rel="nofollow"> View page source</a>
|
|
|
|
|
|
</li>
|
|
</ul>
|
|
<hr/>
|
|
</div>
|
|
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
|
|
<div itemprop="articleBody">
|
|
|
|
<div class="section" id="wallets">
|
|
<span id="api-wallet"></span><h1>Wallets<a class="headerlink" href="#wallets" title="Permalink to this headline">¶</a></h1>
|
|
<p>A <strong>wallet</strong> manages a private/public key pair which is used to cryptographically sign
|
|
transactions and prove ownership on the Ethereum network.</p>
|
|
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">var</span> <span class="n">Wallet</span> <span class="o">=</span> <span class="n">ethers</span><span class="o">.</span><span class="n">Wallet</span><span class="p">;</span>
|
|
</pre></div>
|
|
</div>
|
|
<hr class="docutils" />
|
|
<div class="section" id="creating-instances">
|
|
<h2>Creating Instances<a class="headerlink" href="#creating-instances" title="Permalink to this headline">¶</a></h2>
|
|
<dl class="docutils">
|
|
<dt>new <sup>ethers</sup> . Wallet( privateKey [ , provider ] )</dt>
|
|
<dd>Creates a new instance from <em>privateKey</em> and optionally connect a provider</dd>
|
|
<dt><sup>Wallet</sup> . createRandom ( [ options ] )</dt>
|
|
<dd>Creates a new random wallet; <em>options</em> may specify <code class="docutils literal"><span class="pre">extraEntropy</span></code> to stir into
|
|
the random source (make sure this wallet is stored somewhere safe; if lost there
|
|
is no way to recover it)</dd>
|
|
<dt><sup>Wallet</sup> . fromEncryptedWallet ( json, password [ , progressCallback ] )</dt>
|
|
<dd>Decrypt an encrypted Secret Storage JSON Wallet (from Geth, or that was
|
|
created using <em>prototype.encrypt</em> )</dd>
|
|
<dt><sup>Wallet</sup> . fromMnemonic ( mnemonic [ , path ] )</dt>
|
|
<dd><p class="first">Generate a BIP39 + BIP32 wallet from a <em>mnemonic</em> deriving path</p>
|
|
<p class="last"><strong>default:</strong> <em>path</em>=”m/44’/60’/0’/0/0”</p>
|
|
</dd>
|
|
<dt><sup>Wallet</sup> . fromBrainWallet ( username, password [ , progressCallback ] )</dt>
|
|
<dd>Generate a wallet from a username and password</dd>
|
|
</dl>
|
|
<div class="section" id="examples">
|
|
<h3><em>Examples</em><a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h3>
|
|
<p><strong>Private Key</strong></p>
|
|
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">var</span> <span class="n">privateKey</span> <span class="o">=</span> <span class="s2">"0x0123456789012345678901234567890123456789012345678901234567890123"</span><span class="p">;</span>
|
|
<span class="n">var</span> <span class="n">wallet</span> <span class="o">=</span> <span class="n">new</span> <span class="n">Wallet</span><span class="p">(</span><span class="n">privateKey</span><span class="p">);</span>
|
|
|
|
<span class="n">console</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="s2">"Address: "</span> <span class="o">+</span> <span class="n">wallet</span><span class="o">.</span><span class="n">address</span><span class="p">);</span>
|
|
<span class="o">//</span> <span class="s2">"Address: 0x14791697260E4c9A71f18484C9f997B308e59325"</span>
|
|
</pre></div>
|
|
</div>
|
|
<p><strong>Random Wallet</strong></p>
|
|
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">var</span> <span class="n">wallet</span> <span class="o">=</span> <span class="n">Wallet</span><span class="o">.</span><span class="n">createRandom</span><span class="p">();</span>
|
|
<span class="n">console</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="s2">"Address: "</span> <span class="o">+</span> <span class="n">wallet</span><span class="o">.</span><span class="n">address</span><span class="p">);</span>
|
|
<span class="o">//</span> <span class="s2">"Address: ... this will be different every time ..."</span>
|
|
</pre></div>
|
|
</div>
|
|
<p><strong>Secret Storage Wallet</strong> (e.g. Geth)</p>
|
|
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">var</span> <span class="n">data</span> <span class="o">=</span> <span class="p">{</span>
|
|
<span class="nb">id</span><span class="p">:</span> <span class="s2">"fb1280c0-d646-4e40-9550-7026b1be504a"</span><span class="p">,</span>
|
|
<span class="n">address</span><span class="p">:</span> <span class="s2">"88a5c2d9919e46f883eb62f7b8dd9d0cc45bc290"</span><span class="p">,</span>
|
|
<span class="n">Crypto</span><span class="p">:</span> <span class="p">{</span>
|
|
<span class="n">kdfparams</span><span class="p">:</span> <span class="p">{</span>
|
|
<span class="n">dklen</span><span class="p">:</span> <span class="mi">32</span><span class="p">,</span>
|
|
<span class="n">p</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
|
|
<span class="n">salt</span><span class="p">:</span> <span class="s2">"bbfa53547e3e3bfcc9786a2cbef8504a5031d82734ecef02153e29daeed658fd"</span><span class="p">,</span>
|
|
<span class="n">r</span><span class="p">:</span> <span class="mi">8</span><span class="p">,</span>
|
|
<span class="n">n</span><span class="p">:</span> <span class="mi">262144</span>
|
|
<span class="p">},</span>
|
|
<span class="n">kdf</span><span class="p">:</span> <span class="s2">"scrypt"</span><span class="p">,</span>
|
|
<span class="n">ciphertext</span><span class="p">:</span> <span class="s2">"10adcc8bcaf49474c6710460e0dc974331f71ee4c7baa7314b4a23d25fd6c406"</span><span class="p">,</span>
|
|
<span class="n">mac</span><span class="p">:</span> <span class="s2">"1cf53b5ae8d75f8c037b453e7c3c61b010225d916768a6b145adf5cf9cb3a703"</span><span class="p">,</span>
|
|
<span class="n">cipher</span><span class="p">:</span> <span class="s2">"aes-128-ctr"</span><span class="p">,</span>
|
|
<span class="n">cipherparams</span><span class="p">:</span> <span class="p">{</span>
|
|
<span class="n">iv</span><span class="p">:</span> <span class="s2">"1dcdf13e49cea706994ed38804f6d171"</span>
|
|
<span class="p">}</span>
|
|
<span class="p">},</span>
|
|
<span class="s2">"version"</span> <span class="p">:</span> <span class="mi">3</span>
|
|
<span class="p">};</span>
|
|
|
|
<span class="n">var</span> <span class="n">json</span> <span class="o">=</span> <span class="n">JSON</span><span class="o">.</span><span class="n">stringify</span><span class="p">(</span><span class="n">data</span><span class="p">);</span>
|
|
<span class="n">var</span> <span class="n">password</span> <span class="o">=</span> <span class="s2">"foo"</span><span class="p">;</span>
|
|
<span class="n">Wallet</span><span class="o">.</span><span class="n">fromEncryptedWallet</span><span class="p">(</span><span class="n">json</span><span class="p">,</span> <span class="n">password</span><span class="p">)</span><span class="o">.</span><span class="n">then</span><span class="p">(</span><span class="n">function</span><span class="p">(</span><span class="n">wallet</span><span class="p">)</span> <span class="p">{</span>
|
|
<span class="n">console</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="s2">"Address: "</span> <span class="o">+</span> <span class="n">wallet</span><span class="o">.</span><span class="n">address</span><span class="p">);</span>
|
|
<span class="o">//</span> <span class="s2">"Address: 0x88a5C2d9919e46F883EB62F7b8Dd9d0CC45bc290"</span>
|
|
<span class="p">});</span>
|
|
</pre></div>
|
|
</div>
|
|
<p><strong>Mnemonic Phrase</strong></p>
|
|
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">var</span> <span class="n">mnemonic</span> <span class="o">=</span> <span class="s2">"radar blur cabbage chef fix engine embark joy scheme fiction master release"</span><span class="p">;</span>
|
|
<span class="n">var</span> <span class="n">wallet</span> <span class="o">=</span> <span class="n">Wallet</span><span class="o">.</span><span class="n">fromMnemonic</span><span class="p">(</span><span class="n">mnemonic</span><span class="p">);</span>
|
|
|
|
<span class="n">console</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="s2">"Address: "</span> <span class="o">+</span> <span class="n">wallet</span><span class="o">.</span><span class="n">address</span><span class="p">);</span>
|
|
<span class="o">//</span> <span class="s2">"Address: 0xaC39b311DCEb2A4b2f5d8461c1cdaF756F4F7Ae9"</span>
|
|
</pre></div>
|
|
</div>
|
|
<p><strong>Brain Wallet</strong></p>
|
|
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">var</span> <span class="n">username</span> <span class="o">=</span> <span class="s2">"support@ethers.io"</span><span class="p">;</span>
|
|
<span class="n">var</span> <span class="n">password</span> <span class="o">=</span> <span class="s2">"password123"</span><span class="p">;</span>
|
|
<span class="n">Wallet</span><span class="o">.</span><span class="n">fromBrainWallet</span><span class="p">(</span><span class="n">username</span><span class="p">,</span> <span class="n">password</span><span class="p">)</span><span class="o">.</span><span class="n">then</span><span class="p">(</span><span class="n">function</span><span class="p">(</span><span class="n">wallet</span><span class="p">)</span> <span class="p">{</span>
|
|
<span class="n">console</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="s2">"Address: "</span> <span class="o">+</span> <span class="n">wallet</span><span class="o">.</span><span class="n">address</span><span class="p">);</span>
|
|
<span class="o">//</span> <span class="s2">"Address: 0x7Ee9AE2a2eAF3F0df8D323d555479be562ac4905"</span>
|
|
<span class="p">});</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<hr class="docutils" />
|
|
<div class="section" id="prototype">
|
|
<h2>Prototype<a class="headerlink" href="#prototype" title="Permalink to this headline">¶</a></h2>
|
|
<dl class="docutils" id="address">
|
|
<dt><sup>prototype</sup> . address</dt>
|
|
<dd>The public address of a wallet</dd>
|
|
<dt><sup>prototype</sup> . privateKey</dt>
|
|
<dd>The private key of a wallet; keep this secret</dd>
|
|
<dt><sup>prototype</sup> . provider</dt>
|
|
<dd>Optional; a connected <a class="reference internal" href="api-providers.html#provider"><span class="std std-ref">Provider</span></a> which allows the wallet to connect to
|
|
the Ethereum network to query its state and send transactions</dd>
|
|
<dt><sup>prototype</sup> . getAddress ( )</dt>
|
|
<dd>A function which returns the address; for Wallet, this simple returns the
|
|
<a class="reference internal" href="#address">address</a> property</dd>
|
|
<dt><sup>prototype</sup> . sign ( transaction )</dt>
|
|
<dd>Signs <em>transaction</em> and returns the signed transaction as a <a class="reference internal" href="api-utils.html#hexstring"><span class="std std-ref">hex string</span></a>.
|
|
See <a class="reference internal" href="api-providers.html#transactionrequest"><span class="std std-ref">Transaction Requests</span></a>.</dd>
|
|
<dt><sup>prototype</sup> . encrypt ( password [ , options ] [ , progressCallback ] )</dt>
|
|
<dd>Returns a Promise with the wallet encrypted as a Secret Storage JSON Wallet;
|
|
<em>options</em> may include overrides for the scypt parameters.</dd>
|
|
</dl>
|
|
<div class="section" id="id1">
|
|
<h3><em>Examples</em><a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h3>
|
|
<p><strong>Signing Transactions</strong></p>
|
|
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">var</span> <span class="n">privateKey</span> <span class="o">=</span> <span class="s2">"0x0123456789012345678901234567890123456789012345678901234567890123"</span><span class="p">;</span>
|
|
<span class="n">var</span> <span class="n">wallet</span> <span class="o">=</span> <span class="n">new</span> <span class="n">Wallet</span><span class="p">(</span><span class="n">privateKey</span><span class="p">);</span>
|
|
|
|
<span class="n">console</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="s1">'Address: '</span> <span class="o">+</span> <span class="n">wallet</span><span class="o">.</span><span class="n">address</span><span class="p">);</span>
|
|
<span class="o">//</span> <span class="s2">"Address: 0x14791697260E4c9A71f18484C9f997B308e59325"</span><span class="o">.</span>
|
|
|
|
<span class="n">var</span> <span class="n">transaction</span> <span class="o">=</span> <span class="p">{</span>
|
|
<span class="n">nonce</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
|
|
<span class="n">gasLimit</span><span class="p">:</span> <span class="mi">21000</span><span class="p">,</span>
|
|
<span class="n">gasPrice</span><span class="p">:</span> <span class="n">utils</span><span class="o">.</span><span class="n">bigNumberify</span><span class="p">(</span><span class="s2">"20000000000"</span><span class="p">),</span>
|
|
|
|
<span class="n">to</span><span class="p">:</span> <span class="s2">"0x88a5C2d9919e46F883EB62F7b8Dd9d0CC45bc290"</span><span class="p">,</span>
|
|
|
|
<span class="n">value</span><span class="p">:</span> <span class="n">utils</span><span class="o">.</span><span class="n">parseEther</span><span class="p">(</span><span class="s2">"1.0"</span><span class="p">),</span>
|
|
<span class="n">data</span><span class="p">:</span> <span class="s2">"0x"</span><span class="p">,</span>
|
|
|
|
<span class="o">//</span> <span class="n">This</span> <span class="n">ensures</span> <span class="n">the</span> <span class="n">transaction</span> <span class="n">cannot</span> <span class="n">be</span> <span class="n">replayed</span> <span class="n">on</span> <span class="n">different</span> <span class="n">networks</span>
|
|
<span class="n">chainId</span><span class="p">:</span> <span class="n">providers</span><span class="o">.</span><span class="n">Provider</span><span class="o">.</span><span class="n">chainId</span><span class="o">.</span><span class="n">homestead</span>
|
|
<span class="p">};</span>
|
|
|
|
<span class="n">var</span> <span class="n">signedTransaction</span> <span class="o">=</span> <span class="n">wallet</span><span class="o">.</span><span class="n">sign</span><span class="p">(</span><span class="n">transaction</span><span class="p">);</span>
|
|
|
|
<span class="n">console</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">signedTransaction</span><span class="p">);</span>
|
|
<span class="o">//</span> <span class="s2">"0xf86c808504a817c8008252089488a5c2d9919e46f883eb62f7b8dd9d0cc45bc2"</span> <span class="o">+</span>
|
|
<span class="o">//</span> <span class="s2">"90880de0b6b3a7640000801ca0d7b10eee694f7fd9acaa0baf51e91da5c3d324"</span> <span class="o">+</span>
|
|
<span class="o">//</span> <span class="s2">"f67ad827fbe4410a32967cbc32a06ffb0b4ac0855f146ff82bef010f6f2729b4"</span> <span class="o">+</span>
|
|
<span class="o">//</span> <span class="s2">"24c57b3be967e2074220fca13e79"</span>
|
|
|
|
<span class="o">//</span> <span class="n">This</span> <span class="n">can</span> <span class="n">now</span> <span class="n">be</span> <span class="n">sent</span> <span class="n">to</span> <span class="n">the</span> <span class="n">Ethereum</span> <span class="n">network</span>
|
|
<span class="n">provider</span><span class="o">.</span><span class="n">sendTransaction</span><span class="p">(</span><span class="n">signedTransaction</span><span class="p">)</span><span class="o">.</span><span class="n">then</span><span class="p">(</span><span class="n">function</span><span class="p">(</span><span class="nb">hash</span><span class="p">)</span> <span class="p">{</span>
|
|
<span class="n">console</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="s1">'Hash: '</span> <span class="o">+</span> <span class="nb">hash</span><span class="p">);</span>
|
|
<span class="o">//</span> <span class="n">Hash</span><span class="p">:</span>
|
|
<span class="p">});</span>
|
|
</pre></div>
|
|
</div>
|
|
<p><strong>Encrypting</strong></p>
|
|
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">var</span> <span class="n">password</span> <span class="o">=</span> <span class="s2">"password123"</span><span class="p">;</span>
|
|
|
|
<span class="n">function</span> <span class="n">callback</span><span class="p">(</span><span class="n">percent</span><span class="p">)</span> <span class="p">{</span>
|
|
<span class="n">console</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="s2">"Encrypting: "</span> <span class="o">+</span> <span class="n">parseInt</span><span class="p">(</span><span class="n">percent</span> <span class="o">*</span> <span class="mi">100</span><span class="p">)</span> <span class="o">+</span> <span class="s2">"</span><span class="si">% c</span><span class="s2">omplete"</span><span class="p">);</span>
|
|
<span class="p">}</span>
|
|
|
|
<span class="n">var</span> <span class="n">encryptPromise</span> <span class="o">=</span> <span class="n">wallet</span><span class="o">.</span><span class="n">encrypt</span><span class="p">(</span><span class="n">password</span><span class="p">,</span> <span class="n">callback</span><span class="p">);</span>
|
|
|
|
<span class="n">encryptPromise</span><span class="o">.</span><span class="n">then</span><span class="p">(</span><span class="n">function</span><span class="p">(</span><span class="n">json</span><span class="p">)</span> <span class="p">{</span>
|
|
<span class="n">console</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">json</span><span class="p">);</span>
|
|
<span class="p">});</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<hr class="docutils" />
|
|
<div class="section" id="blockchain-operations">
|
|
<h2>Blockchain Operations<a class="headerlink" href="#blockchain-operations" title="Permalink to this headline">¶</a></h2>
|
|
<p>These operations require the wallet have a provider attached to it.</p>
|
|
<dl class="docutils">
|
|
<dt><sup>prototype</sup> . getBalance ( [ blockTag ] )</dt>
|
|
<dd><p class="first">Returns a Promise with the balance of the wallet (as a <a class="reference internal" href="api-utils.html#bignumber"><span class="std std-ref">BigNumber</span></a>,
|
|
in <strong>wei</strong>) at the <a class="reference internal" href="api-providers.html#blocktag"><span class="std std-ref">blockTag</span></a>.</p>
|
|
<p class="last"><strong>default:</strong> <em>blockTag</em>=”latest”</p>
|
|
</dd>
|
|
<dt><sup>prototype</sup> . getTransactionCount ( [ blockTag ] )</dt>
|
|
<dd><p class="first">Returns a Promise with the number of transactions this account has ever sent
|
|
(also called the <em>nonce</em>) at the <a class="reference internal" href="api-providers.html#blocktag"><span class="std std-ref">blockTag</span></a>.</p>
|
|
<p class="last"><strong>default:</strong> <em>blockTag</em>=”latest”</p>
|
|
</dd>
|
|
<dt><sup>prototype</sup> . estimateGas ( transaction )</dt>
|
|
<dd>Returns a Promise with the estimated cost for <em>transaction</em> (in <strong>gas</strong>, as a
|
|
<a class="reference internal" href="api-utils.html#bignumber"><span class="std std-ref">BigNumber</span></a>)</dd>
|
|
<dt><sup>prototype</sup> . sendTransaction ( transaction )</dt>
|
|
<dd>Sends the <em>transaction</em> to the network and returns a Promise with the transaction
|
|
details. It is highly recommended to omit <em>transaction.chainId</em>, it will be
|
|
filled in by <em>provider</em>.</dd>
|
|
<dt><sup>prototype</sup> . send ( address, amountWei [ , options ] )</dt>
|
|
<dd>Sends <em>amountWei</em> to <em>address</em> on the network and returns a Promise with the
|
|
transaction details.</dd>
|
|
</dl>
|
|
<div class="section" id="id2">
|
|
<h3><em>Examples</em><a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h3>
|
|
<p><strong>Query the Network</strong></p>
|
|
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">var</span> <span class="n">privateKey</span> <span class="o">=</span> <span class="s1">'0x0123456789012345678901234567890123456789012345678901234567890123'</span><span class="p">;</span>
|
|
<span class="n">var</span> <span class="n">wallet</span> <span class="o">=</span> <span class="n">new</span> <span class="n">ethers</span><span class="o">.</span><span class="n">Wallet</span><span class="p">(</span><span class="n">privateKey</span><span class="p">);</span>
|
|
<span class="n">wallet</span><span class="o">.</span><span class="n">provider</span> <span class="o">=</span> <span class="n">ethers</span><span class="o">.</span><span class="n">providers</span><span class="o">.</span><span class="n">getDefaultProvider</span><span class="p">();</span>
|
|
|
|
<span class="n">var</span> <span class="n">balancePromise</span> <span class="o">=</span> <span class="n">wallet</span><span class="o">.</span><span class="n">getBalance</span><span class="p">(</span><span class="n">address</span><span class="p">);</span>
|
|
|
|
<span class="n">balancePromise</span><span class="o">.</span><span class="n">then</span><span class="p">(</span><span class="n">function</span><span class="p">(</span><span class="n">balance</span><span class="p">)</span> <span class="p">{</span>
|
|
<span class="n">console</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">balance</span><span class="p">);</span>
|
|
<span class="p">});</span>
|
|
|
|
<span class="n">var</span> <span class="n">transactionCountPromise</span> <span class="o">=</span> <span class="n">wallet</span><span class="o">.</span><span class="n">getTransactionCount</span><span class="p">(</span><span class="n">address</span><span class="p">);</span>
|
|
|
|
<span class="n">transactionCountPromise</span><span class="o">.</span><span class="n">then</span><span class="p">(</span><span class="n">function</span><span class="p">(</span><span class="n">transactionCount</span><span class="p">)</span> <span class="p">{</span>
|
|
<span class="n">console</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">transactionCount</span><span class="p">);</span>
|
|
<span class="p">});</span>
|
|
</pre></div>
|
|
</div>
|
|
<p><strong>Transfer Ether</strong></p>
|
|
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">var</span> <span class="n">privateKey</span> <span class="o">=</span> <span class="s1">'0x0123456789012345678901234567890123456789012345678901234567890123'</span><span class="p">;</span>
|
|
<span class="n">var</span> <span class="n">wallet</span> <span class="o">=</span> <span class="n">new</span> <span class="n">ethers</span><span class="o">.</span><span class="n">Wallet</span><span class="p">(</span><span class="n">privateKey</span><span class="p">);</span>
|
|
<span class="n">wallet</span><span class="o">.</span><span class="n">provider</span> <span class="o">=</span> <span class="n">ethers</span><span class="o">.</span><span class="n">providers</span><span class="o">.</span><span class="n">getDefaultProvider</span><span class="p">();</span>
|
|
|
|
<span class="o">//</span> <span class="n">We</span> <span class="n">must</span> <span class="k">pass</span> <span class="ow">in</span> <span class="n">the</span> <span class="n">amount</span> <span class="k">as</span> <span class="n">wei</span> <span class="p">(</span><span class="mi">1</span> <span class="n">ether</span> <span class="o">=</span> <span class="mi">1</span><span class="n">e18</span> <span class="n">wei</span><span class="p">),</span> <span class="n">so</span> <span class="n">we</span> <span class="n">use</span>
|
|
<span class="o">//</span> <span class="n">this</span> <span class="n">convenience</span> <span class="n">function</span> <span class="n">to</span> <span class="n">convert</span> <span class="n">ether</span> <span class="n">to</span> <span class="n">wei</span><span class="o">.</span>
|
|
<span class="n">var</span> <span class="n">amount</span> <span class="o">=</span> <span class="n">ethers</span><span class="o">.</span><span class="n">parseEther</span><span class="p">(</span><span class="s1">'1.0'</span><span class="p">);</span>
|
|
|
|
<span class="n">var</span> <span class="n">sendPromise</span> <span class="o">=</span> <span class="n">wallet</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">address</span><span class="p">,</span> <span class="n">amount</span><span class="p">);</span>
|
|
|
|
<span class="n">sendPromise</span><span class="o">.</span><span class="n">then</span><span class="p">(</span><span class="n">function</span><span class="p">(</span><span class="n">transactionHash</span><span class="p">)</span> <span class="p">{</span>
|
|
<span class="n">console</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">transactionHash</span><span class="p">);</span>
|
|
<span class="p">});</span>
|
|
|
|
|
|
<span class="o">//</span> <span class="n">These</span> <span class="n">will</span> <span class="n">query</span> <span class="n">the</span> <span class="n">network</span> <span class="k">for</span> <span class="n">appropriate</span> <span class="n">values</span>
|
|
<span class="n">var</span> <span class="n">options</span> <span class="o">=</span> <span class="p">{</span>
|
|
<span class="o">//</span><span class="n">gasLimit</span><span class="p">:</span> <span class="mi">21000</span>
|
|
<span class="o">//</span><span class="n">gasPrice</span><span class="p">:</span> <span class="n">utils</span><span class="o">.</span><span class="n">bigNumberify</span><span class="p">(</span><span class="s2">"20000000000"</span><span class="p">)</span>
|
|
<span class="p">};</span>
|
|
|
|
<span class="n">var</span> <span class="n">promiseSend</span> <span class="o">=</span> <span class="n">wallet</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">address</span><span class="p">,</span> <span class="n">amount</span><span class="p">,</span> <span class="n">options</span><span class="p">);</span>
|
|
|
|
<span class="n">promiseSend</span><span class="o">.</span><span class="n">then</span><span class="p">(</span><span class="n">function</span><span class="p">(</span><span class="n">transaction</span><span class="p">)</span> <span class="p">{</span>
|
|
<span class="n">console</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">transaction</span><span class="p">);</span>
|
|
<span class="p">});</span>
|
|
</pre></div>
|
|
</div>
|
|
<p><strong>Sending (Complex) Transactions</strong></p>
|
|
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">var</span> <span class="n">privateKey</span> <span class="o">=</span> <span class="s1">'0x0123456789012345678901234567890123456789012345678901234567890123'</span><span class="p">;</span>
|
|
<span class="n">var</span> <span class="n">wallet</span> <span class="o">=</span> <span class="n">new</span> <span class="n">ethers</span><span class="o">.</span><span class="n">Wallet</span><span class="p">(</span><span class="n">privateKey</span><span class="p">);</span>
|
|
<span class="n">wallet</span><span class="o">.</span><span class="n">provider</span> <span class="o">=</span> <span class="n">ethers</span><span class="o">.</span><span class="n">providers</span><span class="o">.</span><span class="n">getDefaultProvider</span><span class="p">();</span>
|
|
|
|
<span class="n">var</span> <span class="n">transaction</span> <span class="o">=</span> <span class="p">{</span>
|
|
<span class="o">//</span> <span class="n">Recommendation</span><span class="p">:</span> <span class="n">omit</span> <span class="n">nonce</span><span class="p">;</span> <span class="n">the</span> <span class="n">provider</span> <span class="n">will</span> <span class="n">query</span> <span class="n">the</span> <span class="n">network</span>
|
|
<span class="o">//</span> <span class="n">nonce</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
|
|
|
|
<span class="o">//</span> <span class="n">Gas</span> <span class="n">Limit</span><span class="p">;</span> <span class="mi">21000</span> <span class="n">will</span> <span class="n">send</span> <span class="n">ether</span> <span class="n">to</span> <span class="n">another</span> <span class="n">use</span><span class="p">,</span> <span class="n">but</span> <span class="n">to</span> <span class="n">execute</span> <span class="n">contracts</span>
|
|
<span class="o">//</span> <span class="n">larger</span> <span class="n">limits</span> <span class="n">are</span> <span class="n">required</span><span class="o">.</span> <span class="n">The</span> <span class="n">provider</span><span class="o">.</span><span class="n">estimateGas</span> <span class="n">can</span> <span class="n">be</span> <span class="n">used</span> <span class="k">for</span> <span class="n">this</span><span class="o">.</span>
|
|
<span class="n">gasLimit</span><span class="p">:</span> <span class="mi">1000000</span>
|
|
|
|
<span class="o">//</span> <span class="n">Recommendations</span><span class="p">:</span> <span class="n">omit</span> <span class="n">gasPrice</span><span class="p">;</span> <span class="n">the</span> <span class="n">provider</span> <span class="n">will</span> <span class="n">query</span> <span class="n">the</span> <span class="n">network</span>
|
|
<span class="o">//</span><span class="n">gasPrice</span><span class="p">:</span> <span class="n">utils</span><span class="o">.</span><span class="n">bigNumberify</span><span class="p">(</span><span class="s2">"20000000000"</span><span class="p">),</span>
|
|
|
|
<span class="o">//</span> <span class="n">Required</span><span class="p">;</span> <span class="n">unless</span> <span class="n">deploying</span> <span class="n">a</span> <span class="n">contract</span> <span class="p">(</span><span class="ow">in</span> <span class="n">which</span> <span class="n">case</span> <span class="n">omit</span><span class="p">)</span>
|
|
<span class="n">to</span><span class="p">:</span> <span class="s2">"0x88a5C2d9919e46F883EB62F7b8Dd9d0CC45bc290"</span><span class="p">,</span>
|
|
|
|
<span class="o">//</span> <span class="n">Optional</span>
|
|
<span class="n">data</span><span class="p">:</span> <span class="s2">"0x"</span><span class="p">,</span>
|
|
|
|
<span class="o">//</span> <span class="n">Optional</span>
|
|
<span class="n">value</span><span class="p">:</span> <span class="n">ethers</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">parseEther</span><span class="p">(</span><span class="s2">"1.0"</span><span class="p">),</span>
|
|
|
|
<span class="o">//</span> <span class="n">Recommendation</span><span class="p">:</span> <span class="n">omit</span> <span class="n">chainId</span><span class="p">;</span> <span class="n">the</span> <span class="n">provider</span> <span class="n">will</span> <span class="n">populate</span> <span class="n">this</span>
|
|
<span class="o">//</span> <span class="n">chaindId</span><span class="p">:</span> <span class="n">providers</span><span class="o">.</span><span class="n">Provider</span><span class="o">.</span><span class="n">chainId</span><span class="o">.</span><span class="n">homestead</span>
|
|
<span class="p">};</span>
|
|
|
|
<span class="o">//</span> <span class="n">Estimate</span> <span class="n">the</span> <span class="n">gas</span> <span class="n">cost</span> <span class="k">for</span> <span class="n">the</span> <span class="n">transaction</span>
|
|
<span class="o">//</span><span class="n">var</span> <span class="n">estimateGasPromise</span> <span class="o">=</span> <span class="n">wallet</span><span class="o">.</span><span class="n">estimateGas</span><span class="p">(</span><span class="n">transaction</span><span class="p">);</span>
|
|
|
|
<span class="o">//</span><span class="n">estimateGasPromise</span><span class="o">.</span><span class="n">then</span><span class="p">(</span><span class="n">function</span><span class="p">(</span><span class="n">gasEstimate</span><span class="p">)</span> <span class="p">{</span>
|
|
<span class="o">//</span> <span class="n">console</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">gasEstimate</span><span class="p">);</span>
|
|
<span class="o">//</span><span class="p">});</span>
|
|
|
|
<span class="o">//</span> <span class="n">Send</span> <span class="n">the</span> <span class="n">transaction</span>
|
|
<span class="n">var</span> <span class="n">sendTransactionPromise</span> <span class="o">=</span> <span class="n">wallet</span><span class="o">.</span><span class="n">sendTransaction</span><span class="p">(</span><span class="n">transaction</span><span class="p">);</span>
|
|
|
|
<span class="n">sendTransactionPromise</span><span class="o">.</span><span class="n">then</span><span class="p">(</span><span class="n">function</span><span class="p">(</span><span class="n">transactionHash</span><span class="p">)</span> <span class="p">{</span>
|
|
<span class="n">console</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">transactionHash</span><span class="p">);</span>
|
|
<span class="p">});</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<hr class="docutils" />
|
|
<div class="section" id="parsing-transactions">
|
|
<h2>Parsing Transactions<a class="headerlink" href="#parsing-transactions" title="Permalink to this headline">¶</a></h2>
|
|
<dl class="docutils">
|
|
<dt><sup>Wallet</sup> . parseTransaction ( hexStringOrArrayish )</dt>
|
|
<dd>Parses a raw <em>hexStringOrArrayish</em> into a Transaction.</dd>
|
|
</dl>
|
|
<div class="section" id="id3">
|
|
<h3><em>Examples</em><a class="headerlink" href="#id3" title="Permalink to this headline">¶</a></h3>
|
|
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="o">//</span> <span class="n">Mainnet</span><span class="p">:</span>
|
|
<span class="n">var</span> <span class="n">raw</span> <span class="o">=</span> <span class="s2">"0xf87083154262850500cf6e0083015f9094c149be1bcdfa69a94384b46a1f913"</span> <span class="o">+</span>
|
|
<span class="s2">"50e5f81c1ab880de6c75de74c236c8025a05b13ef45ce3faf69d1f40f9d15b007"</span> <span class="o">+</span>
|
|
<span class="s2">"0cc9e2c92f"</span>
|
|
<span class="n">var</span> <span class="n">transaction</span> <span class="o">=</span> <span class="p">{</span>
|
|
<span class="n">to</span><span class="p">:</span> <span class="mf">0123.</span><span class="o">..</span>
|
|
<span class="n">value</span><span class="p">:</span> <span class="mf">0123.</span><span class="o">..</span>
|
|
<span class="p">};</span>
|
|
<span class="n">var</span> <span class="n">signedTransaction</span> <span class="o">=</span> <span class="n">wallet</span><span class="o">.</span><span class="n">sign</span><span class="p">(</span><span class="n">transaction</span><span class="p">);</span>
|
|
<span class="n">var</span> <span class="n">transaction</span> <span class="o">=</span> <span class="n">Wallet</span><span class="o">.</span><span class="n">parseTransaction</span><span class="p">(</span><span class="n">signedTransaction</span><span class="p">);</span>
|
|
|
|
<span class="n">console</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">transaction</span><span class="p">);</span>
|
|
<span class="o">//</span> <span class="p">{</span>
|
|
<span class="o">//</span> <span class="n">to</span><span class="p">:</span> <span class="s2">"0xc149Be1bcDFa69a94384b46A1F91350E5f81c1AB"</span><span class="p">,</span>
|
|
<span class="o">//</span> <span class="n">from</span><span class="p">:</span> <span class="s2">"0xEA674fdDe714fd979de3EdF0F56AA9716B898ec8"</span><span class="p">,</span>
|
|
<span class="o">//</span>
|
|
<span class="o">//</span> <span class="n">chainId</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
|
|
<span class="o">//</span>
|
|
<span class="o">//</span> <span class="n">gasLimit</span><span class="p">:</span> <span class="n">utils</span><span class="o">.</span><span class="n">bigNumberify</span><span class="p">(</span><span class="s2">"90000"</span><span class="p">),</span>
|
|
<span class="o">//</span> <span class="n">gasPrice</span><span class="p">:</span> <span class="n">utils</span><span class="o">.</span><span class="n">bigNumberify</span><span class="p">(</span><span class="s2">"21488430592"</span><span class="p">),</span>
|
|
<span class="o">//</span>
|
|
<span class="o">//</span> <span class="n">nonce</span><span class="p">:</span> <span class="mi">1393250</span>
|
|
<span class="o">//</span> <span class="n">data</span><span class="p">:</span> <span class="s2">"0x"</span><span class="p">,</span>
|
|
<span class="o">//</span> <span class="n">value</span><span class="p">:</span> <span class="n">utils</span><span class="o">.</span><span class="n">parseEther</span><span class="p">(</span><span class="s2">"1.0017071732629267"</span><span class="p">),</span>
|
|
<span class="o">//</span>
|
|
<span class="o">//</span> <span class="n">r</span><span class="p">:</span> <span class="s2">"0x5b13ef45ce3faf69d1f40f9d15b0070cc9e2c92f3df79ad46d5b3226d7f3d1e8"</span><span class="p">,</span>
|
|
<span class="o">//</span> <span class="n">s</span><span class="p">:</span> <span class="s2">"0x535236e497c59e3fba93b78e124305c7c9b20db0f8531b015066725e4bb31de6"</span><span class="p">,</span>
|
|
<span class="o">//</span> <span class="n">v</span><span class="p">:</span> <span class="mi">37</span><span class="p">,</span>
|
|
<span class="o">//</span> <span class="p">}</span>
|
|
</pre></div>
|
|
</div>
|
|
<hr class="docutils" />
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
</div>
|
|
</div>
|
|
<footer>
|
|
|
|
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
|
|
|
|
<a href="api-providers.html" class="btn btn-neutral float-right" title="Providers API" accesskey="n">Next <span class="fa fa-arrow-circle-right"></span></a>
|
|
|
|
|
|
<a href="api.html" class="btn btn-neutral" title="Application Programming Interface (API)" accesskey="p"><span class="fa fa-arrow-circle-left"></span> Previous</a>
|
|
|
|
</div>
|
|
|
|
|
|
<hr/>
|
|
|
|
<div role="contentinfo">
|
|
<p>
|
|
© Copyright 2016, Richard Moore <me@ricmoo.com>.
|
|
|
|
</p>
|
|
</div>
|
|
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
|
|
|
|
</footer>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
</section>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<script type="text/javascript">
|
|
var DOCUMENTATION_OPTIONS = {
|
|
URL_ROOT:'./',
|
|
VERSION:'2.0.0',
|
|
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>
|
|
|
|
|
|
|
|
|
|
|
|
<script type="text/javascript" src="_static/js/theme.js"></script>
|
|
|
|
|
|
|
|
|
|
<script type="text/javascript">
|
|
jQuery(function () {
|
|
SphinxRtdTheme.StickyNav.enable();
|
|
});
|
|
</script>
|
|
|
|
|
|
</body>
|
|
</html> |