Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ WORKDIR /usr/src/app
# Install Linux dependencies
RUN apt-get update && apt-get install -y libssl-dev

COPY client_sdk_python ./web3/
COPY client_sdk_python ./client_sdk_python/
COPY tests ./tests/
COPY ens ./ens/

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ Read more in the [documentation on ReadTheDocs](http://web3py.readthedocs.io/).
```python


from web3 import Web3, HTTPProvider
from web3.eth import PlatON
from client_sdk_python import Web3, HTTPProvider
from client_sdk_python.eth import PlatON
from hexbytes import HexBytes

# get blockNumber
Expand Down
2 changes: 1 addition & 1 deletion client_sdk_python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
if sys.version_info < (3, 5):
raise EnvironmentError("Python 3.5 or above is required")

from eth_account import Account # noqa: E402
from platon_account import Account # noqa: E402
from client_sdk_python.main import Web3 # noqa: E402
from client_sdk_python.providers.rpc import ( # noqa: E402
HTTPProvider,
Expand Down
25 changes: 20 additions & 5 deletions client_sdk_python/debug.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
from client_sdk_python.module import (
Module,
)
import json
import rlp
from hexbytes import HexBytes
from client_sdk_python.utils.transactions import send_obj_transaction


class Debug(Module):

need_analyze = True

def economicConfig(self):
return self.web3.manager.request_blocking("debug_economicConfig", [])
return json.loads(self.web3.manager.request_blocking("debug_economicConfig", []))

def setValidatorList(self, node_list, pri_key, transaction_cfg={"gas": 210000}):
data_list = []
for node_id in node_list:
data_list.append(bytes.fromhex(node_id))
data = HexBytes(rlp.encode([rlp.encode(int(1900)), rlp.encode(data_list)])).hex()
return send_obj_transaction(self, data, self.web3.stakingAddress, pri_key, transaction_cfg)

def getBuildMsg(self):
return self.web3.manager.request_blocking("debug_getBuildMsg", [])
def getWaitSlashingNodeList(self):
result = self.web3.manager.request_blocking("debug_getWaitSlashingNodeList", [])
if not result:
return []
return json.loads(result)

def getReceiveMsg(self):
return self.web3.manager.request_blocking("debug_getReceiveMsg", [])
37 changes: 34 additions & 3 deletions client_sdk_python/eth.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import json
from eth_account import (
import sha3
import rlp
from eth_utils.hexadecimal import remove_0x_prefix
from platon_account import (
Account,
)
from platon_account.internal.transactions import bech32_address_bytes
from eth_utils import (
apply_to_return_value,
is_checksum_address,
Expand Down Expand Up @@ -51,6 +55,9 @@
wait_for_transaction_receipt,
)

from platon_account.internal.signing import (
to_standard_signature_bytes,
)

class Eth(Module):
account = Account()
Expand Down Expand Up @@ -109,8 +116,7 @@ def evidences(self):

@property
def consensusStatus(self):
data = self.web3.manager.request_blocking("platon_consensusStatus", [])
return json.loads(data)
return self.web3.manager.request_blocking("platon_consensusStatus", [])

def getPrepareQC(self, block_number):
return self.web3.manager.request_blocking("platon_getPrepareQC", [block_number])
Expand Down Expand Up @@ -417,6 +423,31 @@ def analyzeReceiptByHash(self, tx_hash):
def analyzeReceipt(self, transaction_receipt):
return self.web3.analyzeReceipt(transaction_receipt)

def ecrecover(self, block_identifier):
block = self.getBlock(block_identifier)
extra = block.proofOfAuthorityData[0:32]
sign = block.proofOfAuthorityData[32:]
miner = bech32_address_bytes(remove_0x_prefix(block.miner))
raw_data = [bytes.fromhex(remove_0x_prefix(block.parentHash.hex())),
miner,
bytes.fromhex(remove_0x_prefix(block.stateRoot.hex())),
bytes.fromhex(remove_0x_prefix(block.transactionsRoot.hex())),
bytes.fromhex(remove_0x_prefix(block.receiptsRoot.hex())),
bytes.fromhex(remove_0x_prefix(block.logsBloom.hex())),
block.number,
block.gasLimit,
block.gasUsed,
block.timestamp,
extra,
bytes.fromhex(remove_0x_prefix(block.nonce))
]
message_hash = sha3.keccak_256(rlp.encode(raw_data)).digest()
hash_bytes = HexBytes(message_hash)
signature_bytes = HexBytes(sign)
signature_bytes_standard = to_standard_signature_bytes(signature_bytes)
signature_obj = self.account._keys.Signature(signature_bytes=signature_bytes_standard)
return remove_0x_prefix(signature_obj.recover_public_key_from_msg_hash(hash_bytes).to_hex())


class PlatON(Eth):
pass
Expand Down
53 changes: 41 additions & 12 deletions client_sdk_python/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
add_0x_prefix,
from_wei,
is_address,
is_checksum_address,
# is_checksum_address,
keccak,
remove_0x_prefix,
to_checksum_address,
# to_checksum_address,
to_wei,
)
from platon_keys.utils.address import MIANNETHRP, TESTNETHRP

from ens import ENS

Expand Down Expand Up @@ -87,6 +88,23 @@ def get_default_modules():
}


def default_address(mainnet, testnet):
return {MIANNETHRP: mainnet, TESTNETHRP: testnet}


restricting = default_address("lat1zqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqp7pn3ep","lax1zqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqp3yp7hw")
staking = default_address("lat1zqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzsjx8h7", "lax1zqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzlh5ge3")
penalty = default_address("lat1zqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqyva9ztf", "lax1zqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqyrchd9x")
pipAddr = default_address("lat1zqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq93t3hkm", "lax1zqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq97wrcc5")
delegateReward = default_address("lat1zqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqxlcypcy", "lax1zqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqxsakwkt")


def to_checksum_address(val):
return val

def is_checksum_address(val):
return True

class Web3:
# Providers
HTTPProvider = HTTPProvider
Expand Down Expand Up @@ -118,13 +136,7 @@ class Web3:
isChecksumAddress = staticmethod(is_checksum_address)
toChecksumAddress = staticmethod(to_checksum_address)

# platon contract address
restrictingAddress = "0x1000000000000000000000000000000000000001"
stakingAddress = "0x1000000000000000000000000000000000000002"
penaltyAddress = "0x1000000000000000000000000000000000000004"
pipAddress = "0x1000000000000000000000000000000000000005"

def __init__(self, providers=empty, middlewares=None, modules=None, ens=empty, chain_id=101):
def __init__(self, providers=empty, middlewares=None, modules=None, ens=empty, chain_id=100):
self.manager = RequestManager(self, providers, middlewares)

if modules is None:
Expand All @@ -133,17 +145,29 @@ def __init__(self, providers=empty, middlewares=None, modules=None, ens=empty, c
for module_name, module_class in modules.items():
module_class.attach(self, module_name)

self.ens = ens
if chain_id == 100:
self.net_type = MIANNETHRP
else:
self.net_type = TESTNETHRP
# platon contract address
self.restrictingAddress = restricting[self.net_type]
self.stakingAddress = staking[self.net_type]
self.penaltyAddress = penalty[self.net_type]
self.pipAddress = pipAddr[self.net_type]
self.delegateRewardAddress = delegateReward[self.net_type]

self.chain_id = chain_id
self.ens = ens

def setChainId(self, chain_id):
self.chain_id = chain_id

@property
def chainId(self):
return self.chain_id

@chainId.setter
def chainId(self, chain_id):
self.chain_id = chain_id

@property
def middleware_stack(self):
return self.manager.middleware_stack
Expand Down Expand Up @@ -215,3 +239,8 @@ def ens(self):
@ens.setter
def ens(self, new_ens):
self._ens = new_ens

def pubkey_to_address(self, pubkey):
addr_dict = {MIANNETHRP: pubkey.to_bech32_address(),
TESTNETHRP: pubkey.to_bech32_test_address()}
return addr_dict[self.net_type]
2 changes: 1 addition & 1 deletion client_sdk_python/middleware/names.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ def name_to_address_middleware(w3):
abi_ens_resolver(w3),
]
return construct_formatting_middleware(
request_formatters=abi_request_formatters(normalizers, RPC_ABIS)
# request_formatters=abi_request_formatters(normalizers, RPC_ABIS)
)
6 changes: 5 additions & 1 deletion client_sdk_python/middleware/pythonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
is_string,
remove_0x_prefix,
text_if_str,
to_checksum_address,
# to_checksum_address,
)
from hexbytes import (
HexBytes,
Expand Down Expand Up @@ -63,6 +63,10 @@ def bytes_to_ascii(value):
is_not_null = complement(is_null)


def to_checksum_address(val):
return val


@curry
def to_hexbytes(num_bytes, val, variable_length=False):
if isinstance(val, (str, int, bytes)):
Expand Down
12 changes: 6 additions & 6 deletions client_sdk_python/middleware/signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
)
import operator

from eth_account import (
from platon_account import (
Account,
)
from eth_account.local import (
from platon_account.local import (
LocalAccount,
)
from eth_keys.datatypes import (
from platon_keys.datatypes import (
PrivateKey,
)
from eth_utils import (
Expand Down Expand Up @@ -63,7 +63,7 @@ def gen_normalized_accounts(val):
def to_account(val):
raise TypeError(
"key must be one of the types: "
"eth_keys.datatype.PrivateKey, eth_account.local.LocalAccount, "
"platon_keys.datatype.PrivateKey, platon_account.local.LocalAccount, "
"or raw private key as a hex string or byte string. "
"Was of type {0}".format(type(val)))

Expand Down Expand Up @@ -99,8 +99,8 @@ def construct_sign_and_send_raw_middleware(private_key_or_account):
Keyword arguments:
private_key_or_account -- A single private key or a tuple,
list or set of private keys. Keys can be any of the following formats:
- An eth_account.LocalAccount object
- An eth_keys.PrivateKey object
- An platon_account.LocalAccount object
- An platon_keys.PrivateKey object
- A raw private key as a hex string or byte string
"""

Expand Down
42 changes: 25 additions & 17 deletions client_sdk_python/pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,14 @@ def submitVersion(self, verifier, pip_id, new_version, end_voting_rounds, pri_ke
rlp.encode(int(new_version)), rlp.encode(int(end_voting_rounds))])
return send_obj_transaction(self, data, self.web3.pipAddress, pri_key, transaction_cfg)

def submitParam(self, verifier, url, end_voting_block, param_name, current_value, new_value,
pri_key, transaction_cfg=None):
def submitParam(self, verifier, pip_id, module, name, new_value, pri_key, transaction_cfg=None):
"""
todo fill
:param verifier:
:param url:
:param end_voting_block:
:param param_name:
:param current_value:
:param new_value:
Submit an param proposal
:param verifier: The certified submitting the proposal
:param pip_id: PIPID
:param module: parameter module
:param name: parameter name
:param new_value: New parameter value
:param pri_key: Private key for transaction
:param transaction_cfg: Transaction basic configuration
type: dict
Expand All @@ -87,8 +85,8 @@ def submitParam(self, verifier, url, end_voting_block, param_name, current_value
:return: if is need analyze return transaction result dict
if is not need analyze return transaction hash
"""
data = rlp.encode([rlp.encode(int(2002)), rlp.encode(bytes.fromhex(verifier)), rlp.encode(url), rlp.encode(int(end_voting_block)),
rlp.encode(param_name), rlp.encode(str(current_value)), rlp.encode(str(new_value))])
data = rlp.encode([rlp.encode(int(2002)), rlp.encode(bytes.fromhex(verifier)), rlp.encode(pip_id), rlp.encode(module),
rlp.encode(name), rlp.encode(new_value)])
return send_obj_transaction(self, data, self.web3.pipAddress, pri_key, transaction_cfg)

def submitCancel(self, verifier, pip_id, end_voting_rounds, tobe_canceled_proposal_id, pri_key, transaction_cfg=None):
Expand Down Expand Up @@ -232,16 +230,26 @@ def getActiveVersion(self, from_address=None):
data = rlp.encode([rlp.encode(int(2103))])
return parse_data(call_obj(self, from_address, self.web3.pipAddress, data))

# def getProgramVersion(self, from_address=None):
# data = rlp.encode([rlp.encode(int(2104))])
# return parse_data(call_obj(self, from_address, self.web3.pipAddress, data))
def getGovernParamValue(self, module, name, from_address=None):
"""
Query the current block height governance parameter value
:param module: Parameter module
:param name: parameter name
:param from_address:
:return:
"""
data = rlp.encode([rlp.encode(int(2104)), rlp.encode(module), rlp.encode(name)])
return parse_data(call_obj(self, from_address, self.web3.pipAddress, data))

def listParam(self, from_address=None):
def listGovernParam(self, module=None, from_address=None):
"""
todo fill
Query governance parameter list
:param module
:param from_address: Used to call the rpc call method
:return:
todo fill
"""
data = rlp.encode([rlp.encode(int(2105))])
if module is None:
module = ""
data = rlp.encode([rlp.encode(int(2106)), rlp.encode(module)])
return parse_data(call_obj(self, from_address, self.web3.pipAddress, data))
Loading