-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathdns.py
136 lines (97 loc) · 3.95 KB
/
dns.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from typing import Any, List, Protocol
from Cryptodome.Hash import keccak
from multiversx_sdk import (
Address,
AddressComputer,
SmartContractQuery,
SmartContractQueryResponse,
)
from multiversx_sdk_cli import cli_shared, utils
from multiversx_sdk_cli.config import get_address_hrp
from multiversx_sdk_cli.constants import ADDRESS_ZERO_HEX
from multiversx_sdk_cli.transactions import do_prepare_transaction
MaxNumShards = 256
ShardIdentiferLen = 2
InitialDNSAddress = bytes([1] * 32)
# fmt: off
class INetworkProvider(Protocol):
def query_contract(self, query: SmartContractQuery) -> SmartContractQueryResponse:
...
# fmt: on
def resolve(name: str, proxy: INetworkProvider) -> Address:
dns_address = dns_address_for_name(name)
response = _query_contract(contract_address=dns_address, proxy=proxy, function="resolve", args=[name.encode()])
if len(response.return_data_parts) == 0:
return Address.new_from_hex(ADDRESS_ZERO_HEX, get_address_hrp())
result = response.return_data_parts[0]
return Address(result, get_address_hrp())
def validate_name(name: str, shard_id: int, proxy: INetworkProvider):
dns_address = compute_dns_address_for_shard_id(shard_id)
response = _query_contract(
contract_address=dns_address,
proxy=proxy,
function="validateName",
args=[name.encode()],
)
return_code: str = response.return_code
if return_code == "ok":
print(f"name [{name}] is valid")
else:
print(f"name [{name}] is invalid")
def register(args: Any):
args = utils.as_object(args)
cli_shared.check_guardian_args(args)
cli_shared.check_broadcast_args(args)
cli_shared.prepare_nonce_in_args(args)
cli_shared.prepare_chain_id_in_args(args)
args.receiver = dns_address_for_name(args.name).bech32()
args.data = dns_register_data(args.name)
tx = do_prepare_transaction(args)
cli_shared.send_or_simulate(tx, args)
def compute_all_dns_addresses() -> List[Address]:
addresses: List[Address] = []
for i in range(0, 256):
addresses.append(compute_dns_address_for_shard_id(i))
return addresses
def name_hash(name: str) -> bytes:
return keccak.new(digest_bits=256).update(str.encode(name)).digest()
def registration_cost(shard_id: int, proxy: INetworkProvider) -> int:
dns_address = compute_dns_address_for_shard_id(shard_id)
response = _query_contract(
contract_address=dns_address,
proxy=proxy,
function="getRegistrationCost",
args=[],
)
data = response.return_data_parts[0]
if not data:
return 0
else:
return int.from_bytes(data, byteorder="big", signed=False)
def version(shard_id: int, proxy: INetworkProvider) -> str:
dns_address = compute_dns_address_for_shard_id(shard_id)
response = _query_contract(contract_address=dns_address, proxy=proxy, function="version", args=[])
return response.return_data_parts[0].decode()
def dns_address_for_name(name: str) -> Address:
hash = name_hash(name)
shard_id = hash[31]
return compute_dns_address_for_shard_id(shard_id)
def compute_dns_address_for_shard_id(shard_id: int) -> Address:
deployer_pubkey_prefix = InitialDNSAddress[: len(InitialDNSAddress) - ShardIdentiferLen]
deployer_pubkey = deployer_pubkey_prefix + bytes([0, shard_id])
deployer = Address(deployer_pubkey, get_address_hrp())
nonce = 0
address_computer = AddressComputer(number_of_shards=3)
contract_address = address_computer.compute_contract_address(deployer, nonce)
return contract_address
def dns_register_data(name: str) -> str:
name_enc: bytes = str.encode(name)
return "register@{}".format(name_enc.hex())
def _query_contract(
contract_address: Address,
proxy: INetworkProvider,
function: str,
args: List[bytes],
) -> SmartContractQueryResponse:
query = SmartContractQuery(contract=contract_address, function=function, arguments=args)
return proxy.query_contract(query)