def __get_peer_pool_map(self, peer_pubkey=None):
     contract_address = bytearray.fromhex(self.CONTRACT_ADDRESS)
     contract_address.reverse()
     view = self.__sdk.rpc.get_storage(contract_address.hex(),
                                       self.GOVERNANCE_VIEW.encode().hex())
     stream = StreamManager.GetStream(bytearray.fromhex(view))
     reader = BinaryReader(stream)
     governance_view = GovernanceView()
     governance_view.deserialize(reader)
     stream.close()
     stream2 = StreamManager.GetStream()
     writer = BinaryWriter(stream2)
     writer.write_int32(governance_view.view)
     view_bytes = stream2.ToArray()
     peer_pool_bytes = self.PEER_POOL.encode('utf-8')
     key_bytes = peer_pool_bytes + a2b_hex(view_bytes)
     value = self.__sdk.rpc.get_storage(contract_address.hex(),
                                        key_bytes.hex())
     stream3 = StreamManager.GetStream(bytearray.fromhex(value))
     reader2 = BinaryReader(stream3)
     length = reader2.read_int32()
     peer_pool_map = {}
     for i in range(length):
         item = PeerPoolItem()
         item.deserialize(reader2)
         peer_pool_map[item.peer_pubkey] = item.to_json()
     if peer_pubkey is not None:
         if peer_pubkey not in peer_pool_map:
             return None
         return peer_pool_map[peer_pubkey]
     return peer_pool_map
 def test_read_var_int(self):
     value = 123
     writer_stream = StreamManager.GetStream()
     writer = BinaryWriter(writer_stream)
     writer.write_var_int(value)
     reader_stream = StreamManager.GetStream(writer_stream.getbuffer())
     reader = BinaryReader(reader_stream)
     self.assertEqual(reader.read_var_int(), value)
 def push_bytes(data):
     ms = StreamManager.GetStream()
     writer = BinaryWriter(ms)
     if len(data) == 0:
         raise ValueError("push data error: data is null")
     if len(data) <= int.from_bytes(PUSHBYTES75,
                                    'little') + 1 - int.from_bytes(
                                        PUSHBYTES1, 'little'):
         num = len(data) + int.from_bytes(PUSHBYTES1, 'little') - 1
         writer.WriteByte(num)
     elif len(data) < 0x100:
         writer.WriteByte(PUSHDATA1)
         writer.WriteUInt8(len(data))
     elif len(data) < 0x10000:
         writer.WriteByte(PUSHDATA2)
         writer.WriteUInt16(len(data))
     else:
         writer.WriteByte(PUSHDATA4)
         writer.WriteUInt32(len(data))
     writer.WriteBytes(data)
     ms.flush()
     res = ms.ToArray()
     StreamManager.ReleaseStream(ms)
     res = bytes_reader(res)
     return res
示例#4
0
 def get_param_info(program: bytes):
     ms = StreamManager.GetStream(program)
     reader = BinaryReader(ms)
     list = []
     while True:
         try:
             res = ProgramBuilder.read_bytes(reader)
         except:
             break
         list.append(res)
     return list
    def serialize(self) -> bytes:
        ms = StreamManager.GetStream()
        writer = BinaryWriter(ms)
        writer.WriteBytes(self.serialize_unsigned())
        writer.WriteVarInt(len(self.sigs))

        for sig in self.sigs:
            writer.WriteBytes(sig.serialize())

        ms.flush()
        temp = ms.ToArray()
        StreamManager.ReleaseStream(ms)
        return a2b_hex(temp)
 def get_total_stake(self, address: str):
     contract_address = bytearray.fromhex(self.CONTRACT_ADDRESS)
     contract_address.reverse()
     total_bytes = self.TOTAL_STAKE.encode('utf-8')
     address_bytes = Address.b58decode(address).to_array()
     key = total_bytes + address_bytes
     res = self.__sdk.rpc.get_storage(contract_address.hex(), key.hex())
     stream = StreamManager.GetStream(bytearray.fromhex(res))
     reader = BinaryReader(stream)
     total_stake = TotalStake()
     total_stake.deserialize(reader)
     stream.close()
     return total_stake
 def serialize(self, is_hex: bool = False) -> bytes:
     ms = StreamManager.GetStream()
     writer = BinaryWriter(ms)
     writer.write_bytes(self.serialize_unsigned())
     writer.write_var_int(len(self.sigs))
     for sig in self.sigs:
         writer.write_bytes(sig.serialize())
     ms.flush()
     temp = ms.ToArray()
     StreamManager.ReleaseStream(ms)
     if is_hex:
         return temp
     else:
         return a2b_hex(temp)
 def serialize_unsigned(self) -> bytes:
     ms = StreamManager.GetStream()
     writer = BinaryWriter(ms)
     writer.WriteUInt8(self.version)
     writer.WriteUInt8(self.tx_type)
     writer.WriteUInt32(self.nonce)
     writer.WriteUInt64(self.gas_price)
     writer.WriteUInt64(self.gas_limit)
     writer.WriteBytes(bytes(self.payer))
     writer.WriteVarBytes(bytes(self.payload))
     writer.WriteVarInt(len(self.attributes))
     ms.flush()
     res = ms.ToArray()
     StreamManager.ReleaseStream(ms)
     return res
 def get_authorize_info(self, peer_pubkey: str, addr: Address):
     contract_address = bytearray.fromhex(self.CONTRACT_ADDRESS)
     contract_address.reverse()
     peer_pubkey_prefix = bytearray.fromhex(peer_pubkey)
     address_bytes = addr.to_array()
     authorize_info_pool = self.AUTHORIZE_INFO_POOL.encode()
     key = authorize_info_pool + peer_pubkey_prefix + address_bytes
     res = self.__sdk.rpc.get_storage(contract_address.hex(), key.hex())
     if res is None or res == '':
         return None
     stream = StreamManager.GetStream(bytearray.fromhex(res))
     reader = BinaryReader(stream)
     authorize_info = AuthorizeInfo()
     authorize_info.deserialize(reader)
     return authorize_info.to_json()
示例#10
0
 def get_split_fee_address(self, address: str):
     contract_address = bytearray.fromhex(self.CONTRACT_ADDRESS)
     contract_address.reverse()
     split_fee_address_bytes = self.SPLIT_FEE_ADDRESS.encode()
     address_bytes = Address.b58decode(address).to_array()
     key = split_fee_address_bytes + address_bytes
     res = self.__sdk.rpc.get_storage(contract_address.hex(), key.hex())
     if res is None or res == '':
         return None
     split_fee_address = SplitFeeAddress()
     stream = StreamManager.GetStream(bytearray.fromhex(res))
     reader = BinaryReader(stream)
     split_fee_address.deserialize(reader)
     stream.close()
     return split_fee_address.to_json()
示例#11
0
 def get_peer_attributes(self, peer_pubkey: str):
     contract_address = bytearray.fromhex(self.CONTRACT_ADDRESS)
     contract_address.reverse()
     peer_attributes = self.PEER_ATTRIBUTES.encode('utf-8')
     peer_pubkey_bytes = a2b_hex(peer_pubkey.encode())
     key = peer_attributes + peer_pubkey_bytes
     res = self.__sdk.rpc.get_storage(contract_address.hex(), key.hex())
     if res is None or res == '':
         return None
     peer_attributes = PeerAttributes()
     stream = StreamManager.GetStream(bytearray.fromhex(res))
     reader = BinaryReader(stream)
     peer_attributes.deserialize(reader)
     stream.close()
     return peer_attributes.to_json()
 def serialize_unsigned(self) -> bytes:
     ms = StreamManager.GetStream()
     writer = BinaryWriter(ms)
     writer.write_uint8(self.version)
     writer.write_uint8(self.tx_type)
     writer.write_uint32(self.nonce)
     writer.write_uint64(self.gas_price)
     writer.write_uint64(self.gas_limit)
     writer.write_bytes(bytes(self.payer))
     self.serialize_exclusive_data(writer)
     if hasattr(self, "payload"):
         writer.write_var_bytes(bytes(self.payload))
     writer.write_var_int(len(self.attributes))
     ms.flush()
     res = ms.ToArray()
     StreamManager.ReleaseStream(ms)
     return res
 def deserialize_from(txbytes: bytes):
     ms = StreamManager.GetStream(txbytes)
     reader = BinaryReader(ms)
     tx = Transaction()
     tx.version = reader.read_uint8()
     tx.tx_type = reader.read_uint8()
     tx.nonce = reader.read_uint32()
     tx.gas_price = reader.read_uint64()
     tx.gas_limit = reader.read_uint64()
     tx.payer = reader.read_bytes(20)
     tx.payload = reader.read_var_bytes()
     attri_len = reader.read_var_int()
     if attri_len is 0:
         tx.attributes = bytearray()
     sigs_len = reader.read_var_int()
     tx.sigs = []
     for i in range(sigs_len):
         tx.sigs.append(Sig.deserialize(reader))
     return tx
示例#14
0
    def serialize(self) -> bytes:
        invoke_script = ProgramBuilder.program_from_params(self.sig_data)
        if len(self.public_keys) == 0:
            raise ValueError("no public key in sig")

        if len(self.public_keys) == 1:
            verification_script = ProgramBuilder.program_from_pubkey(
                self.public_keys[0])
        else:
            verification_script = ProgramBuilder.program_from_multi_pubkey(
                self.M, self.public_keys)
        ms = StreamManager.GetStream()
        writer = BinaryWriter(ms)
        writer.WriteVarBytes(invoke_script)
        writer.WriteVarBytes(verification_script)
        ms.flush()
        res = ms.ToArray()
        res = bytes_reader(res)
        StreamManager.ReleaseStream(ms)
        return res
示例#15
0
 def get_program_info(program: bytes) -> ProgramInfo:
     length = len(program)
     end = program[length - 1]
     temp = program[:length - 1]
     ms = StreamManager.GetStream(temp)
     reader = BinaryReader(ms)
     info = ProgramInfo()
     if end == int.from_bytes(CHECKSIG, 'little'):
         pubkeys = ProgramBuilder.read_bytes(reader)
         info.set_pubkey([pubkeys])
         info.set_m(1)
     elif end == int.from_bytes(CHECKMULTISIG, 'little'):
         length = program[len(program) - 2] - int.from_bytes(
             PUSH1, 'little')
         m = reader.read_byte() - int.from_bytes(PUSH1, 'little') + 1
         pub = []
         for i in range(length):
             pub.append(reader.read_var_bytes())
         info.set_pubkey(pub)
         info.set_m(m)
     return info
示例#16
0
 def deserialize_from(sigbytes: bytes):
     ms = StreamManager.GetStream(sigbytes)
     reader = BinaryReader(ms)
     return Sig.deserialize(reader)
 def to_dict(item_serialize: str):
     stream = StreamManager.GetStream(bytearray.fromhex(item_serialize))
     reader = BinaryReader(stream)
     return ContractDataParser.__deserialize_stack_item(reader)
示例#18
0
 def __init__(self, bys: bytearray):
     self.ms = StreamManager.GetStream(bys)
     self.reader = BinaryReader(self.ms)
     self.code = bys
示例#19
0
    def parse_ddo(ont_id: str, ddo: str) -> dict:
        if ddo == "":
            return dict()
        ms = StreamManager.GetStream(a2b_hex(ddo))
        reader = BinaryReader(ms)
        try:
            publickey_bytes = reader.read_var_bytes()
        except Exception as e:
            raise e
        try:
            attribute_bytes = reader.read_var_bytes()
        except Exception as e:
            attribute_bytes = bytearray()
        try:
            recovery_bytes = reader.read_var_bytes()
        except Exception as e:
            recovery_bytes = bytearray()
        pubKey_list = []
        if len(publickey_bytes) != 0:
            ms = StreamManager.GetStream(publickey_bytes)
            reader2 = BinaryReader(ms)
            while True:
                try:
                    index = reader2.read_int32()
                    d = {}
                    d['PubKeyId'] = ont_id + "#keys-" + str(index)
                    pubkey = reader2.read_var_bytes()
                    if len(pubkey) == 33:
                        d["Type"] = KeyType.ECDSA.name
                        d["Curve"] = Curve.P256.name
                        d["Value"] = pubkey.hex()
                    else:
                        d["Type"] = KeyType.from_label(pubkey[0])
                        d["Curve"] = Curve.from_label(pubkey[1])
                        d["Value"] = pubkey.hex()
                    pubKey_list.append(d)
                except Exception as e:
                    break
        attribute_list = []
        if len(attribute_bytes) != 0:
            ms = StreamManager.GetStream(attribute_bytes)
            reader2 = BinaryReader(ms)

            while True:
                try:
                    d = {}
                    key = reader2.read_var_bytes()
                    if len(key) == 0:
                        break
                    d["Key"] = str(key, 'utf-8')
                    d["Type"] = str(reader2.read_var_bytes(), 'utf-8')
                    d["Value"] = str(reader2.read_var_bytes(), 'utf-8')
                    attribute_list.append(d)
                except Exception as e:
                    break
        d2 = {}
        d2["Owners"] = pubKey_list
        d2["Attributes"] = attribute_list
        if len(recovery_bytes) != 0:
            addr = Address(recovery_bytes)
            d2["Recovery"] = addr.b58encode()
        d2["OntId"] = ont_id
        return d2
示例#20
0
 def runtime_serialize(self, config: Config, engine: ExecutionEngine):
     t = PushData.pop_stack_item(engine)
     stream = StreamManager.GetStream()
     writer = BinaryWriter(stream)
     self.__serialize_stack_item(t, writer)
     PushData.push_data(engine, stream.ToArray())