pyckb 1.0.2__py2.py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
ckb/__init__.py ADDED
@@ -0,0 +1,9 @@
1
+ from . import bech32
2
+ from . import config
3
+ from . import core
4
+ from . import denomination
5
+ from . import ecdsa
6
+ from . import molecule
7
+ from . import rpc
8
+ from . import wallet
9
+ from . import secp256k1
ckb/bech32.py ADDED
@@ -0,0 +1,148 @@
1
+ # Copyright (c) 2017, 2020 Pieter Wuille
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+ #
21
+ # BIP-0173 https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki
22
+ # BIP-0350 https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki
23
+ # Derived from https://raw.githubusercontent.com/sipa/bech32/master/ref/python/segwit_addr.py
24
+ #
25
+ # Reference implementation for Bech32/bech32 and segwit addresses.
26
+
27
+ import typing
28
+
29
+ CHARSET = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l'
30
+ CONST_0 = 1
31
+ CONST_M = 0x2bc830a3
32
+
33
+
34
+ def bech32_polymod(data: bytearray) -> int:
35
+ # Internal function that computes the Bech32 checksum.
36
+ assert isinstance(data, bytearray)
37
+ gen = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3]
38
+ chk = 1
39
+ for val in data:
40
+ top = chk >> 25
41
+ chk = chk & 0x1ffffff
42
+ chk = chk << 5 ^ val
43
+ for i in range(5):
44
+ chk ^= gen[i] if ((top >> i) & 1) else 0
45
+ return chk
46
+
47
+
48
+ def bech32_hrpconv(hrp: str) -> bytearray:
49
+ # Expand the HRP into values for checksum computation.
50
+ r = bytearray()
51
+ r.extend([ord(x) >> 5 for x in hrp])
52
+ r.append(0)
53
+ r.extend([ord(x) & 31 for x in hrp])
54
+ return r
55
+
56
+
57
+ def bech32_re_arrange_5(data: bytearray) -> bytearray:
58
+ # Re-arrange those bits into groups of 5, and pad with zeroes at the end if needed.
59
+ assert isinstance(data, bytearray)
60
+ acc = 0
61
+ bit = 0
62
+ ret = bytearray()
63
+ max_val = 0x1f
64
+ max_acc = 0xfff
65
+ for b in data:
66
+ acc = ((acc << 8) | b) & max_acc
67
+ bit += 8
68
+ for _ in range(bit // 5):
69
+ bit -= 5
70
+ ret.append((acc >> bit) & max_val)
71
+ if bit:
72
+ pad_bit = 5 - bit
73
+ ret.append((acc << pad_bit) & max_val)
74
+ return ret
75
+
76
+
77
+ def bech32_create_checksum(hrp: str, ver: int, data: bytearray) -> bytearray:
78
+ hrpdata = bech32_hrpconv(hrp) + data
79
+ polymod = bech32_polymod(hrpdata + bytearray(6))
80
+ if ver == 0:
81
+ polymod = polymod ^ CONST_0
82
+ if ver >= 1:
83
+ polymod = polymod ^ CONST_M
84
+ return bytearray([(polymod >> 5 * (5 - i)) & 31 for i in range(6)])
85
+
86
+
87
+ def bech32_verify_checksum(hrp: str, ver: int, data: bytearray) -> bool:
88
+ if ver == 0:
89
+ return bech32_polymod(bech32_hrpconv(hrp) + bytearray(data)) == CONST_0
90
+ if ver >= 1:
91
+ return bech32_polymod(bech32_hrpconv(hrp) + bytearray(data)) == CONST_M
92
+
93
+
94
+ def bech32_decode(ver: int, bech: str) -> typing.Tuple[str, bytearray]:
95
+ # Validate a string, and determine HRP and data.
96
+ for c in bech:
97
+ assert ord(c) >= ord('!')
98
+ assert ord(c) <= ord('~')
99
+ bech = bech.lower()
100
+ pos = bech.rfind('1')
101
+ assert pos > 0
102
+ assert pos + 6 < len(bech)
103
+ for c in bech[pos+1:]:
104
+ assert c in CHARSET
105
+ hrp = bech[:pos]
106
+ data = bytearray([CHARSET.find(x) for x in bech[pos+1:]])
107
+ assert bech32_verify_checksum(hrp, ver, data)
108
+ return hrp, data[:-6]
109
+
110
+
111
+ def bech32_encode(hrp: str, ver: int, data: bytearray) -> str:
112
+ datasum = data + bech32_create_checksum(hrp, ver, data)
113
+ return hrp + '1' + ''.join([CHARSET[d] for d in datasum])
114
+
115
+
116
+ def bech32_re_arrange_8(data: bytearray) -> bytearray:
117
+ # Re-arrange those bits into groups of 8 bits. Any incomplete group at the end MUST be 4 bits or less, MUST be all
118
+ # zeroes, and is discarded.
119
+ assert isinstance(data, bytearray)
120
+ acc = 0
121
+ bit = 0
122
+ ret = bytearray()
123
+ max_val = 0xff
124
+ max_acc = 0xfff
125
+ for b in data:
126
+ assert b <= 0x1f
127
+ acc = ((acc << 5) | b) & max_acc
128
+ bit += 5
129
+ for _ in range(bit // 8):
130
+ bit -= 8
131
+ ret.append((acc >> bit) & max_val)
132
+ assert bit < 5
133
+ return ret
134
+
135
+
136
+ def decode(hrp: str, addr: str) -> bytearray:
137
+ # Decode a segwit address.
138
+ pre, data = bech32_decode(1, addr)
139
+ assert pre == hrp
140
+ return bech32_re_arrange_8(data)
141
+
142
+
143
+ def encode(hrp: str, prog: bytearray) -> str:
144
+ # Encode a segwit address.
145
+ assert isinstance(prog, bytearray)
146
+ r = bech32_encode(hrp, 1, bech32_re_arrange_5(prog))
147
+ assert prog == decode(hrp, r)
148
+ return r
ckb/config.py ADDED
@@ -0,0 +1,120 @@
1
+ import random
2
+ import requests
3
+ import typing
4
+
5
+
6
+ class ObjectDict(dict):
7
+ def __getattr__(self, name: str) -> typing.Any:
8
+ try:
9
+ return self[name]
10
+ except KeyError:
11
+ raise AttributeError(name)
12
+
13
+ def __setattr__(self, name: str, value: typing.Any):
14
+ self[name] = value
15
+
16
+
17
+ develop = ObjectDict({
18
+ 'hrp': 'ckt',
19
+ 'url': 'http://127.0.0.1:8114',
20
+ 'script': ObjectDict({
21
+ 'dao': ObjectDict({
22
+ 'code_hash': bytearray.fromhex('82d76d1b75fe2fd9a27dfbaa65a039221a380d76c926f378d3f81cf3e7e13f2e'),
23
+ 'hash_type': 1,
24
+ 'cell_dep': ObjectDict({
25
+ 'out_point': ObjectDict({
26
+ 'tx_hash': bytearray.fromhex('0000000000000000000000000000000000000000000000000000000000000000'),
27
+ 'index': 2,
28
+ }),
29
+ 'dep_type': 0,
30
+ })
31
+ }),
32
+ 'secp256k1_blake160': ObjectDict({
33
+ 'code_hash': bytearray.fromhex('9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8'),
34
+ 'hash_type': 1,
35
+ 'cell_dep': ObjectDict({
36
+ 'out_point': ObjectDict({
37
+ 'tx_hash': bytearray.fromhex('0000000000000000000000000000000000000000000000000000000000000000'),
38
+ 'index': 0,
39
+ }),
40
+ 'dep_type': 1,
41
+ })
42
+ }),
43
+ })
44
+ })
45
+
46
+ mainnet = ObjectDict({
47
+ 'hrp': 'ckb',
48
+ # https://github.com/nervosnetwork/ckb/wiki/Public-JSON-RPC-nodes
49
+ 'url': 'https://mainnet.ckbapp.dev',
50
+ 'script': ObjectDict({
51
+ 'dao': ObjectDict({
52
+ 'code_hash': bytearray.fromhex('82d76d1b75fe2fd9a27dfbaa65a039221a380d76c926f378d3f81cf3e7e13f2e'),
53
+ 'hash_type': 1,
54
+ 'cell_dep': ObjectDict({
55
+ 'out_point': ObjectDict({
56
+ 'tx_hash': bytearray.fromhex('e2fb199810d49a4d8beec56718ba2593b665db9d52299a0f9e6e75416d73ff5c'),
57
+ 'index': 2,
58
+ }),
59
+ 'dep_type': 0,
60
+ })
61
+ }),
62
+ 'secp256k1_blake160': ObjectDict({
63
+ 'code_hash': bytearray.fromhex('9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8'),
64
+ 'hash_type': 1,
65
+ 'cell_dep': ObjectDict({
66
+ 'out_point': ObjectDict({
67
+ 'tx_hash': bytearray.fromhex('71a7ba8fc96349fea0ed3a5c47992e3b4084b031a42264a018e0072e8172e46c'),
68
+ 'index': 0,
69
+ }),
70
+ 'dep_type': 1,
71
+ })
72
+ }),
73
+ })
74
+ })
75
+
76
+ testnet = ObjectDict({
77
+ 'hrp': 'ckt',
78
+ # https://github.com/nervosnetwork/ckb/wiki/Public-JSON-RPC-nodes
79
+ 'url': 'https://testnet.ckbapp.dev',
80
+ 'script': ObjectDict({
81
+ 'dao': ObjectDict({
82
+ 'code_hash': bytearray.fromhex('82d76d1b75fe2fd9a27dfbaa65a039221a380d76c926f378d3f81cf3e7e13f2e'),
83
+ 'hash_type': 1,
84
+ 'cell_dep': ObjectDict({
85
+ 'out_point': ObjectDict({
86
+ 'tx_hash': bytearray.fromhex('8f8c79eb6671709633fe6a46de93c0fedc9c1b8a6527a18d3983879542635c9f'),
87
+ 'index': 2,
88
+ }),
89
+ 'dep_type': 0,
90
+ })
91
+ }),
92
+ 'secp256k1_blake160': ObjectDict({
93
+ 'code_hash': bytearray.fromhex('9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8'),
94
+ 'hash_type': 1,
95
+ 'cell_dep': ObjectDict({
96
+ 'out_point': ObjectDict({
97
+ 'tx_hash': bytearray.fromhex('f8de3bb47d055cdf460d93a2a6e1b05f7432f9777c8c474abf4eec1d4aee5d37'),
98
+ 'index': 0,
99
+ }),
100
+ 'dep_type': 1,
101
+ })
102
+ }),
103
+ })
104
+ })
105
+
106
+
107
+ def upgrade(url: str):
108
+ r = requests.post(url, json={
109
+ 'id': random.randint(0x00000000, 0xffffffff),
110
+ 'jsonrpc': '2.0',
111
+ 'method': 'get_block_by_number',
112
+ 'params': ['0x0']
113
+ })
114
+ t = r.json()['result']['transactions']
115
+ develop.url = url
116
+ develop.script.dao.cell_dep.out_point.tx_hash = bytearray.fromhex(t[0]['hash'][2:])
117
+ develop.script.secp256k1_blake160.cell_dep.out_point.tx_hash = bytearray.fromhex(t[1]['hash'][2:])
118
+
119
+
120
+ current = develop