Skip to content

Commit 93dd618

Browse files
author
Arjan Zijderveld
committed
Converted license to Apache 2.0
Update README.md with more examples
1 parent a4bd061 commit 93dd618

25 files changed

+662
-1086
lines changed

Diff for: LICENSE

+201-674
Large diffs are not rendered by default.

Diff for: README.md

+72-5
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,88 @@ pip install substrate-interface
2222

2323
## Examples
2424

25-
Simple example, initialize interface and get head block hash of Kusama chain:
26-
2725
### Initialization
2826

27+
The following examples show how to initialize for supported chains:
28+
29+
#### Polkadot
30+
31+
```python
32+
substrate = SubstrateInterface(
33+
url="wss://rpc.polkadot.io",
34+
address_type=0,
35+
type_registry_preset='polkadot'
36+
)
37+
```
38+
39+
Note on support for `wss`, this is still quite limited at the moment as connections are not reused yet. Until support is
40+
improved it is prefered to use `http` endpoints (e.g. http://127.0.0.1:9933)
41+
42+
#### Kusama
43+
2944
```python
3045
substrate = SubstrateInterface(
3146
url="wss://kusama-rpc.polkadot.io/",
3247
address_type=2,
3348
type_registry_preset='kusama'
3449
)
50+
```
51+
52+
#### Kulupu
3553

36-
substrate.get_chain_head()
54+
```python
55+
substrate = SubstrateInterface(
56+
url="wss://rpc.kulupu.corepaper.org/ws",
57+
address_type=16,
58+
type_registry_preset='kulupu'
59+
)
3760
```
38-
Note on support for wss, this is still quite limited at the moment as connections are not reused yet. Until support is
39-
improved it is prefered to use http endpoints (e.g. http://127.0.0.1:9933)
61+
62+
#### Substrate Node Template
63+
Compatible with https://github.com/substrate-developer-hub/substrate-node-template
64+
65+
```python
66+
substrate = SubstrateInterface(
67+
url="http://127.0.0.1:9933",
68+
address_type=42,
69+
type_registry_preset='substrate-node-template'
70+
)
71+
72+
```
73+
74+
If custom types are introduced in the Substrate chain, the following example will add compatibility by creating a custom type
75+
registry JSON file and including this during initialization:
76+
77+
```json
78+
{
79+
"runtime_id": 2,
80+
"types": {
81+
"MyCustomInt": "u32",
82+
"MyStruct": {
83+
"type": "struct",
84+
"type_mapping": [
85+
["account", "AccountId"],
86+
["message", "Vec<u8>"]
87+
]
88+
}
89+
},
90+
"versioning": [
91+
]
92+
}
93+
```
94+
95+
```python
96+
custom_type_registry = load_type_registry_file("my-custom-types.json")
97+
98+
substrate = SubstrateInterface(
99+
url="http://127.0.0.1:9933",
100+
address_type=42,
101+
type_registry_preset='substrate-node-template',
102+
type_registry=custom_type_registry
103+
)
104+
105+
```
106+
40107

41108
### Get extrinsics for a certain block
42109

Diff for: docs/constants.html

+17-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,23 @@ <h1 class="title">Module <code>substrateinterface.constants</code></h1>
2626
<summary>
2727
<span>Expand source code</span>
2828
</summary>
29-
<pre><code class="python">STORAGE_HASH_SYSTEM_EVENTS = &#34;0xcc956bdb7605e3547539f321ac2bc95c&#34;
29+
<pre><code class="python"># Python Substrate Interface Library
30+
#
31+
# Copyright 2018-2020 Stichting Polkascan (Polkascan Foundation).
32+
#
33+
# Licensed under the Apache License, Version 2.0 (the &#34;License&#34;);
34+
# you may not use this file except in compliance with the License.
35+
# You may obtain a copy of the License at
36+
#
37+
# http://www.apache.org/licenses/LICENSE-2.0
38+
#
39+
# Unless required by applicable law or agreed to in writing, software
40+
# distributed under the License is distributed on an &#34;AS IS&#34; BASIS,
41+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
42+
# See the License for the specific language governing permissions and
43+
# limitations under the License.
44+
45+
STORAGE_HASH_SYSTEM_EVENTS = &#34;0xcc956bdb7605e3547539f321ac2bc95c&#34;
3046
STORAGE_HASH_SYSTEM_EVENTS_V9 = &#34;0x26aa394eea5630e07c48ae0c9558cef780d41e5e16056765bc8461851072c9d7&#34;</code></pre>
3147
</details>
3248
</section>

Diff for: docs/exceptions.html

+11-13
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,21 @@ <h1 class="title">Module <code>substrateinterface.exceptions</code></h1>
2626
<summary>
2727
<span>Expand source code</span>
2828
</summary>
29-
<pre><code class="python"># Python Substrate Interface
29+
<pre><code class="python"># Python Substrate Interface Library
3030
#
31-
# Copyright 2018-2020 openAware BV (NL).
32-
# This file is part of Polkascan.
31+
# Copyright 2018-2020 Stichting Polkascan (Polkascan Foundation).
3332
#
34-
# Polkascan is free software: you can redistribute it and/or modify
35-
# it under the terms of the GNU General Public License as published by
36-
# the Free Software Foundation, either version 3 of the License, or
37-
# (at your option) any later version.
33+
# Licensed under the Apache License, Version 2.0 (the &#34;License&#34;);
34+
# you may not use this file except in compliance with the License.
35+
# You may obtain a copy of the License at
3836
#
39-
# Polkascan is distributed in the hope that it will be useful,
40-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
41-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42-
# GNU General Public License for more details.
37+
# http://www.apache.org/licenses/LICENSE-2.0
4338
#
44-
# You should have received a copy of the GNU General Public License
45-
# along with Polkascan. If not, see &lt;http://www.gnu.org/licenses/&gt;.
39+
# Unless required by applicable law or agreed to in writing, software
40+
# distributed under the License is distributed on an &#34;AS IS&#34; BASIS,
41+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
42+
# See the License for the specific language governing permissions and
43+
# limitations under the License.
4644

4745

4846
class SubstrateRequestException(Exception):

Diff for: docs/index.html

+33-102
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,27 @@ <h1 class="title">Package <code>substrateinterface</code></h1>
2626
<summary>
2727
<span>Expand source code</span>
2828
</summary>
29-
<pre><code class="python"># Python Substrate Interface
29+
<pre><code class="python"># Python Substrate Interface Library
3030
#
31-
# Copyright 2018-2020 openAware BV (NL).
32-
# This file is part of Polkascan.
31+
# Copyright 2018-2020 Stichting Polkascan (Polkascan Foundation).
3332
#
34-
# Polkascan is free software: you can redistribute it and/or modify
35-
# it under the terms of the GNU General Public License as published by
36-
# the Free Software Foundation, either version 3 of the License, or
37-
# (at your option) any later version.
33+
# Licensed under the Apache License, Version 2.0 (the &#34;License&#34;);
34+
# you may not use this file except in compliance with the License.
35+
# You may obtain a copy of the License at
3836
#
39-
# Polkascan is distributed in the hope that it will be useful,
40-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
41-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42-
# GNU General Public License for more details.
37+
# http://www.apache.org/licenses/LICENSE-2.0
4338
#
44-
# You should have received a copy of the GNU General Public License
45-
# along with Polkascan. If not, see &lt;http://www.gnu.org/licenses/&gt;.
39+
# Unless required by applicable law or agreed to in writing, software
40+
# distributed under the License is distributed on an &#34;AS IS&#34; BASIS,
41+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
42+
# See the License for the specific language governing permissions and
43+
# limitations under the License.
4644

4745
import asyncio
4846
import binascii
4947
import json
5048
import logging
5149
import re
52-
from hashlib import blake2b
5350

5451
import requests
5552
import websockets
@@ -74,8 +71,6 @@ <h1 class="title">Package <code>substrateinterface</code></h1>
7471

7572
class Keypair:
7673

77-
chain_code = b&#34;SchnorrRistrettoHDKD&#34;
78-
7974
def __init__(self, ss58_address=None, public_key=None, private_key=None, address_type=42):
8075

8176
if ss58_address and not public_key:
@@ -111,32 +106,14 @@ <h1 class="title">Package <code>substrateinterface</code></h1>
111106
return bip39_generate(words)
112107

113108
@classmethod
114-
def create_from_mnemonic(cls, mnemonic, hard_path=None, address_type=42):
109+
def create_from_mnemonic(cls, mnemonic, address_type=42):
115110
seed_array = bip39_to_mini_secret(mnemonic, &#34;&#34;)
116111

117-
if hard_path:
118-
# Get private and public key from seed
119-
public_key, private_key = sr25519.pair_from_seed(bytes(seed_array))
120-
121-
# Private derivation
122-
_, child_pubkey, child_privkey = sr25519.derive_keypair(
123-
(public_key, public_key, private_key),
124-
hard_path.encode()
125-
)
126-
127-
return cls.create_from_private_key(
128-
private_key=child_privkey.hex(),
129-
public_key=child_pubkey.hex(),
130-
address_type=address_type
131-
)
132-
133-
else:
134-
135-
keypair = cls.create_from_seed(
136-
seed_hex=binascii.hexlify(bytearray(seed_array)).decode(&#34;ascii&#34;),
137-
address_type=address_type
138-
)
139-
keypair.mnemonic = mnemonic
112+
keypair = cls.create_from_seed(
113+
seed_hex=binascii.hexlify(bytearray(seed_array)).decode(&#34;ascii&#34;),
114+
address_type=address_type
115+
)
116+
keypair.mnemonic = mnemonic
140117

141118
return keypair
142119

@@ -321,7 +298,7 @@ <h1 class="title">Package <code>substrateinterface</code></h1>
321298
else:
322299
ws_result.update(json.loads(await websocket.recv()))
323300

324-
asyncio.get_event_loop().run_until_complete(ws_request(payload))
301+
asyncio.run(ws_request(payload))
325302
json_body = ws_result
326303

327304
else:
@@ -1914,8 +1891,6 @@ <h2 class="section-title" id="header-classes">Classes</h2>
19141891
</summary>
19151892
<pre><code class="python">class Keypair:
19161893

1917-
chain_code = b&#34;SchnorrRistrettoHDKD&#34;
1918-
19191894
def __init__(self, ss58_address=None, public_key=None, private_key=None, address_type=42):
19201895

19211896
if ss58_address and not public_key:
@@ -1951,32 +1926,14 @@ <h2 class="section-title" id="header-classes">Classes</h2>
19511926
return bip39_generate(words)
19521927

19531928
@classmethod
1954-
def create_from_mnemonic(cls, mnemonic, hard_path=None, address_type=42):
1929+
def create_from_mnemonic(cls, mnemonic, address_type=42):
19551930
seed_array = bip39_to_mini_secret(mnemonic, &#34;&#34;)
19561931

1957-
if hard_path:
1958-
# Get private and public key from seed
1959-
public_key, private_key = sr25519.pair_from_seed(bytes(seed_array))
1960-
1961-
# Private derivation
1962-
_, child_pubkey, child_privkey = sr25519.derive_keypair(
1963-
(public_key, public_key, private_key),
1964-
hard_path.encode()
1965-
)
1966-
1967-
return cls.create_from_private_key(
1968-
private_key=child_privkey.hex(),
1969-
public_key=child_pubkey.hex(),
1970-
address_type=address_type
1971-
)
1972-
1973-
else:
1974-
1975-
keypair = cls.create_from_seed(
1976-
seed_hex=binascii.hexlify(bytearray(seed_array)).decode(&#34;ascii&#34;),
1977-
address_type=address_type
1978-
)
1979-
keypair.mnemonic = mnemonic
1932+
keypair = cls.create_from_seed(
1933+
seed_hex=binascii.hexlify(bytearray(seed_array)).decode(&#34;ascii&#34;),
1934+
address_type=address_type
1935+
)
1936+
keypair.mnemonic = mnemonic
19801937

19811938
return keypair
19821939

@@ -2038,17 +1995,10 @@ <h2 class="section-title" id="header-classes">Classes</h2>
20381995

20391996
return sr25519.verify(signature, data, bytes.fromhex(self.public_key[2:]))</code></pre>
20401997
</details>
2041-
<h3>Class variables</h3>
2042-
<dl>
2043-
<dt id="substrateinterface.Keypair.chain_code"><code class="name">var <span class="ident">chain_code</span></code></dt>
2044-
<dd>
2045-
<div class="desc"></div>
2046-
</dd>
2047-
</dl>
20481998
<h3>Static methods</h3>
20491999
<dl>
20502000
<dt id="substrateinterface.Keypair.create_from_mnemonic"><code class="name flex">
2051-
<span>def <span class="ident">create_from_mnemonic</span></span>(<span>mnemonic, hard_path=None, address_type=42)</span>
2001+
<span>def <span class="ident">create_from_mnemonic</span></span>(<span>mnemonic, address_type=42)</span>
20522002
</code></dt>
20532003
<dd>
20542004
<div class="desc"></div>
@@ -2057,32 +2007,14 @@ <h3>Static methods</h3>
20572007
<span>Expand source code</span>
20582008
</summary>
20592009
<pre><code class="python">@classmethod
2060-
def create_from_mnemonic(cls, mnemonic, hard_path=None, address_type=42):
2010+
def create_from_mnemonic(cls, mnemonic, address_type=42):
20612011
seed_array = bip39_to_mini_secret(mnemonic, &#34;&#34;)
20622012

2063-
if hard_path:
2064-
# Get private and public key from seed
2065-
public_key, private_key = sr25519.pair_from_seed(bytes(seed_array))
2066-
2067-
# Private derivation
2068-
_, child_pubkey, child_privkey = sr25519.derive_keypair(
2069-
(public_key, public_key, private_key),
2070-
hard_path.encode()
2071-
)
2072-
2073-
return cls.create_from_private_key(
2074-
private_key=child_privkey.hex(),
2075-
public_key=child_pubkey.hex(),
2076-
address_type=address_type
2077-
)
2078-
2079-
else:
2080-
2081-
keypair = cls.create_from_seed(
2082-
seed_hex=binascii.hexlify(bytearray(seed_array)).decode(&#34;ascii&#34;),
2083-
address_type=address_type
2084-
)
2085-
keypair.mnemonic = mnemonic
2013+
keypair = cls.create_from_seed(
2014+
seed_hex=binascii.hexlify(bytearray(seed_array)).decode(&#34;ascii&#34;),
2015+
address_type=address_type
2016+
)
2017+
keypair.mnemonic = mnemonic
20862018

20872019
return keypair</code></pre>
20882020
</details>
@@ -2359,7 +2291,7 @@ <h2 id="parameters">Parameters</h2>
23592291
else:
23602292
ws_result.update(json.loads(await websocket.recv()))
23612293

2362-
asyncio.get_event_loop().run_until_complete(ws_request(payload))
2294+
asyncio.run(ws_request(payload))
23632295
json_body = ws_result
23642296

23652297
else:
@@ -6010,7 +5942,7 @@ <h2 id="returns">Returns</h2>
60105942
else:
60115943
ws_result.update(json.loads(await websocket.recv()))
60125944

6013-
asyncio.get_event_loop().run_until_complete(ws_request(payload))
5945+
asyncio.run(ws_request(payload))
60145946
json_body = ws_result
60155947

60165948
else:
@@ -6420,7 +6352,6 @@ <h1>Index</h1>
64206352
<li>
64216353
<h4><code><a title="substrateinterface.Keypair" href="#substrateinterface.Keypair">Keypair</a></code></h4>
64226354
<ul class="">
6423-
<li><code><a title="substrateinterface.Keypair.chain_code" href="#substrateinterface.Keypair.chain_code">chain_code</a></code></li>
64246355
<li><code><a title="substrateinterface.Keypair.create_from_mnemonic" href="#substrateinterface.Keypair.create_from_mnemonic">create_from_mnemonic</a></code></li>
64256356
<li><code><a title="substrateinterface.Keypair.create_from_private_key" href="#substrateinterface.Keypair.create_from_private_key">create_from_private_key</a></code></li>
64266357
<li><code><a title="substrateinterface.Keypair.create_from_seed" href="#substrateinterface.Keypair.create_from_seed">create_from_seed</a></code></li>

0 commit comments

Comments
 (0)