Пример #1
0
    def serialize(self, so, val):
        i = 0
        l = len(val)
        while i < l:
            # Boundary
            if (i):
                STInt8.serialize(so, self.typeBoundary)

            j = 0
            l2 = len(val[i])
            while j < l2:
                entry = val[i][j]
                #if (entry.hasOwnProperty('_value')) {entry = entry._value}
                type = 0

                if (entry.account):
                    type |= self.typeAccount
                if (entry.currency):
                    type |= self.typeCurrency
                if (entry.issuer):
                    type |= self.typeIssuer

                STInt8.serialize(so, type)

                if (entry.account):
                    #so.append(UInt160.from_json(entry.account).to_bytes())
                    so.append(decodeAddress(0,entry.account))

                if (entry.currency):
                    currencyBytes = STCurrency.from_json_to_bytes(entry.currency, entry.non_native)
                    so.append(currencyBytes)

                if (entry.issuer):
                    #so.append(UInt160.from_json(entry.issuer).to_bytes())
                    so.append(decode_address(0,entry.issuer))
                j += 1
            i += 1

        STInt8.serialize(so, self.typeEnd)
Пример #2
0
 def serialize(so, val):
     byte_data = decode_address(0, val)
     SerializedType.serialize_varint(so, len(byte_data))
     so.append(byte_data)
Пример #3
0
    def serialize(so, val):
        """"""
        amount = Amount.from_json(val)

        if not amount.is_valid():
            raise Exception('Not a valid Amount object.')

        # Amount(64 - bit integer)
        valueBytes = array_set(8, 0)

        # For SWT, offset is 0
        # only convert the value
        if amount.is_native():
            bn = amount._value
            valueHex = hex(bn).replace('0x', '')
            # print('valueHex is ',valueHex)

            # Enforce correct length (64 bits)
            if len(valueHex) > 16:
                raise Exception('Amount Value out of bounds')

            while len(valueHex) < 16:
                valueHex = '0' + valueHex

            # Convert the HEX value to bytes array
            valueBytes = hex_to_bytes(
                valueHex)  # bytes.fromBits(hex.toBits(valueHex))

            # Clear most significant two bits - these bits should already be 0 if
            # Amount enforces the range correctly, but we'll clear them anyway just
            # so this code can make certain guarantees about the encoded value.
            valueBytes[0] &= 0x3f

            if not amount.is_negative():
                valueBytes[0] |= 0x40

            so.append(valueBytes)

        else:
            # For other non - native currency
            # 1.Serialize the currency value with offset
            # Put offset
            hi = 0
            lo = 0

            # First bit: non - native
            hi |= 1 << 31

            # print('amount._value is',amount._value)
            # print('amount._offset is', amount._offset)
            if not amount.is_zero():
                # Second bit: non - negative?
                if not amount.is_negative():
                    hi |= 1 << 30

                # print('amount._offset is',amount._offset)
                # print('amount._value is', amount._value)

                # Next eight bits: offset / exponent
                hi |= ((97 + amount._offset) & 0xff) << 22
                # Remaining 54 bits: mantissa
                hi |= amount._value >> 32 & 0x3fffff
                lo = amount._value & 0xffffffff

            # Convert from a bitArray to an array of bytes.
            arr = [hi, lo]
            l = len(arr)

            if l == 0:
                bl = 0
            else:
                x = arr[l - 1]
                bl = (l - 1) * 32 + (round(x / 0x10000000000) or 32)

            # Setup a new byte array and filled the byte data in
            # Results should not longer than 8 bytes as defined earlier
            tmparray = []

            i = 0
            while i < bl / 8:
                if (i & 3) == 0:
                    tmp = arr[round(i / 4)]
                #  tmparray.append(tmp >> 24)
                tmparray.append((tmp >> 24) % 256)
                tmp <<= 8
                i += 1
            # print('tmparray is', tmparray)
            if len(tmparray) > 8:
                raise Exception(
                    'Invalid byte array length in AMOUNT value representation')
            valueBytes = tmparray

            so.append(valueBytes)

            # 2. Serialize the currency info with currency code
            # and issuer
            # console.log("Serial non-native AMOUNT ......")
            # Currency(160 - bit hash)
            tum_bytes = amount.tum_to_bytes()
            so.append(tum_bytes)

            # Issuer(160 - bit hash)
            # so.append(amount.issuer().to_bytes())
            so.append(decode_address(0, amount.issuer()))