def from_xprv(cls, xprv: str) -> 'Xprv':
        ...
        from witnet.util.transformations.bech32 import bech32_decode_address

        bech = bech32_decode_address(xprv)
        idx = 0
        # byte lengths of the serialized byte data
        DEPTH = 1
        INDEX = 4 * bech[idx:idx + DEPTH][0]
        CHAIN = 32
        secret_prefix = b'\x00'
        KEYLN = 32

        depth = bech[idx:idx + DEPTH]
        idx += DEPTH
        if INDEX == 0:
            print('MasterKey')
        index = None
        chain_code = bytes(bech[idx:idx + CHAIN])

        idx += CHAIN
        idx += len(secret_prefix)
        key_data = bytes(bech[idx:idx + KEYLN])
        private_key = WitPrivateKey(key_data)
        public_key = private_key.to_public()

        chain_code = bytes(bech[1:33])
        tmp_xprv = Xprv(key=private_key, code=chain_code, depth=depth[0])
        return bech
def from_slip32(master_key: str, root_path=None) -> Xprv:
    bech = bech32_decode_address(master_key)
    idx = 0
    # byte lengths of the serialized byte data
    DEPTH = 1
    INDEX = 4 * bech[idx:idx + DEPTH][0]
    CHAIN = 32
    secret_prefix = b'\x00'
    KEYLN = 32

    depth = bech[idx:idx + DEPTH]
    idx += DEPTH
    index = None
    chain_code = bytes(bech[idx:idx + CHAIN])

    idx += CHAIN
    idx += len(secret_prefix)
    key_data = bytes(bech[idx:idx + KEYLN])
    private_key = WitPrivateKey(key_data)
    public_key = private_key.to_public()

    chain_code = bytes(bech[1:33])
    private_key = WitPrivateKey(bytes(bech[34:66]))

    return Xprv(key=private_key,
                code=chain_code,
                depth=depth[0],
                path=root_path)
예제 #3
0
    def create_vtt(self, to, private_key: WitPrivateKey, change_address=None, utxo_selection_strategy=None,
                   fee: int = 0,
                   fee_type='absolute'):

        node = NodeClient.manager()
        now = datetime.timestamp(datetime.now())
        to_sum = 0
        print(self.balance)
        print(to_sum + fee)
        for receiver in to:
            to_sum += receiver['value']
        to_sum += fee
        if self.balance < nano_wit_to_wit(to_sum):
            return '0', {"error": "Insufficient Funds"}

        available_utxos, selected_utxos = {}, []
        for x, utxo in enumerate(self.utxos):
            if utxo['timelock'] < now:
                available_utxos[utxo['output_pointer']] = utxo['value']

        sorted_x = sorted(available_utxos.items(), key=lambda kv: kv[1])
        value_owed = to_sum
        selected_utxo_total_value = 0

        for i, x in enumerate(sorted_x):
            if value_owed > 0:
                selected_utxos.append(x)
                selected_utxo_total_value += x[1]
                value_owed -= x[1]

        change = int(abs(value_owed))
        if change > 0:
            to.append({'address': self.address, 'time_lock': 0, 'value': change})
            change = 0

        inputs, outputs = [], []

        # Inputs
        for utxo in selected_utxos:
            output_pointer, value = utxo
            # print(output_pointer, value)
            _input = Input.from_json({'output_pointer': output_pointer})
            inputs.append(_input)

        # Outputs
        for receiver in to:
            pkh = receiver['address']
            value = receiver['value']

            if 'time_lock' in receiver:
                time_lock = receiver['time_lock']
            else:
                time_lock = 0

            vto_dict = {
                'pkh': pkh,
                'time_lock': time_lock,
                'value': value
            }

            output: ValueTransferOutput = ValueTransferOutput.from_json(vto_dict)
            outputs.append(output)
        vtt_transaction_body = VTTransactionBody(inputs=inputs, outputs=outputs)

        vtt_hash = vtt_transaction_body.hash()
        der_bytes = private_key.sign_hash(vtt_hash).encode(compact=False)

        signatures = []
        signature = Signature(secp256k1=Secp256k1Signature(der=der_bytes))
        pubkey = private_key.to_public().pub_key()
        sig = KeyedSignature(signature=signature, public_key=pubkey)

        for _input in inputs:
            signatures.append(sig)

        vtt_transaction_body = VTTransactionBody(inputs=inputs, outputs=outputs)
        transaction = VTTransaction(body=vtt_transaction_body, signatures=signatures)
        return vtt_hash, transaction