def make_etx_spec(self, our, their): our_color_def = self.resolve_color_spec(our['color_spec']) our_color_set = ColorSet.from_color_ids(self.model.get_color_map(), [our_color_def.get_color_id()]) their_color_def = self.resolve_color_spec(their['color_spec']) their_color_set = ColorSet.from_color_ids(self.model.get_color_map(), [their_color_def.get_color_id()]) extra_value = 0 if our_color_def == UNCOLORED_MARKER: # pay fee + padding for one colored outputs extra_value = 10000 + 8192 * 1 c_utxos, c_change = self.select_inputs( SimpleColorValue(colordef=our_color_def, value=our['value'] + extra_value)) inputs = {our['color_spec']: [utxo.get_outpoint() for utxo in c_utxos]} wam = self.model.get_address_manager() our_address = wam.get_change_address(their_color_set) targets = [(our_address.get_address(), their['color_spec'], their['value'])] if c_change > 0: our_change_address = wam.get_change_address(our_color_set) targets.append((our_change_address.get_address(), our['color_spec'], c_change.get_value())) return ETxSpec(inputs, targets, c_utxos)
def test_from_color_ids(self): self.assertTrue(self.colorset0.equals(ColorSet.from_color_ids(self.colormap, [0]))) self.assertTrue(self.colorset3.equals(ColorSet.from_color_ids(self.colormap, [1, 2]))) tmp = ColorSet.from_color_ids(self.colormap, [1, 2, 3]) self.assertTrue(tmp.has_color_id(1)) self.assertTrue(tmp.has_color_id(2)) self.assertTrue(tmp.has_color_id(3))
def test_from_color_ids(self): self.assertTrue( self.colorset0.equals(ColorSet.from_color_ids(self.colormap, [0]))) self.assertTrue( self.colorset3.equals( ColorSet.from_color_ids(self.colormap, [1, 2]))) tmp = ColorSet.from_color_ids(self.colormap, [1, 2, 3]) self.assertTrue(tmp.has_color_id(1)) self.assertTrue(tmp.has_color_id(2)) self.assertTrue(tmp.has_color_id(3))
def get_asset_value_for_colorvalue(self, colorvalue): colorset = ColorSet.from_color_ids(self.colormap, [colorvalue.get_color_id()]) asset = self.find_asset_by_color_set(colorset) if not asset: raise Exception('asset not found') return AdditiveAssetValue(asset=asset, value=colorvalue.get_value())
def make_etx_spec(self, our, their): fee = FEE if our.get_colordef() == UNCOLORED_MARKER else 0 c_utxos, c_change = self.select_inputs(our + fee) inputs = {our.get_colordef(): [utxo.get_outpoint() for utxo in c_utxos]} wam = self.model.get_address_manager() our_color_set = ColorSet.from_color_ids(self.model.get_color_map(), [our.get_color_id()]) their_color_set = ColorSet.from_color_ids(self.model.get_color_map(), [their.get_color_id()]) our_address = wam.get_change_address(their_color_set) targets = [ColorTarget(our_address.get_address(), their)] if c_change > 0: our_change_address = wam.get_change_address(our_color_set) targets.append(ColorTarget(our_change_address.get_address(), c_change)) return ETxSpec(inputs, targets, c_utxos)
def get_asset_value_for_colorvalue(self, colorvalue): colorset = ColorSet.from_color_ids(self.colormap, [colorvalue.get_color_id()]) asset = self.find_asset_by_color_set(colorset) if not asset: raise Exception('asset not found') return AdditiveAssetValue(asset=asset, value=colorvalue.get_value())
def make_etx_spec(self, our, their): fee = FEE if our.get_colordef() == UNCOLORED_MARKER else 0 c_utxos, c_change = self.select_inputs(our + fee) inputs = { our.get_colordef(): [utxo.get_outpoint() for utxo in c_utxos] } wam = self.model.get_address_manager() our_color_set = ColorSet.from_color_ids(self.model.get_color_map(), [our.get_color_id()]) their_color_set = ColorSet.from_color_ids(self.model.get_color_map(), [their.get_color_id()]) our_address = wam.get_change_address(their_color_set) targets = [ColorTarget(our_address.get_address(), their)] if c_change > 0: our_change_address = wam.get_change_address(our_color_set) targets.append( ColorTarget(our_change_address.get_address(), c_change)) return ETxSpec(inputs, targets, c_utxos)
def prepare_targets(self, etx_spec, their): self.targets = etx_spec.targets wam = self.model.get_address_manager() colormap = self.model.get_color_map() their_color_set = ColorSet.from_color_ids(self.model.get_color_map(), [their.get_color_id()]) ct = ColorTarget( wam.get_change_address(their_color_set).get_address(), their) self.targets.append(ct)
def prepare_targets(self, etx_spec, their): self.targets = etx_spec.targets wam = self.model.get_address_manager() colormap = self.model.get_color_map() their_color_set = ColorSet.from_color_ids(self.model.get_color_map(), [their.get_color_id()]) ct = ColorTarget(wam.get_change_address(their_color_set).get_address(), their) self.targets.append(ct)
def make_query(self, query): """Create a UTXOQuery from query <query>. Queries are dicts with: color_set - color associated with this query """ color_set = query.get('color_set') if not color_set: if 'color_id_set' in query: color_set = ColorSet.from_color_ids(self.model.get_color_map(), query['color_id_set']) elif 'asset' in query: color_set = query['asset'].get_color_set() else: raise Exception('color set is not specified') return UTXOQuery(self.model, color_set)
def make_query(self, query): """Create a UTXOQuery from query <query>. Queries are dicts with: color_set - color associated with this query """ color_set = query.get('color_set') if not color_set: if 'color_id_set' in query: color_set = ColorSet.from_color_ids( self.model.get_color_map(), query['color_id_set']) elif 'asset' in query: color_set = query['asset'].get_color_set() else: raise Exception('color set is not specified') return UTXOQuery(self.model, color_set)
def prepare_targets(self, etx_spec, their): self.targets = [] for address, color_spec, value in etx_spec.targets: colordef = self.ewctrl.resolve_color_spec(color_spec) self.targets.append(ColorTarget(address, SimpleColorValue(colordef=colordef, value=value))) wam = self.model.get_address_manager() colormap = self.model.get_color_map() their_colordef = self.ewctrl.resolve_color_spec(their['color_spec']) their_color_set = ColorSet.from_color_ids(self.model.get_color_map(), [their_colordef.get_color_id()]) ct = ColorTarget(wam.get_change_address(their_color_set).get_address(), SimpleColorValue(colordef=their_colordef, value=their['value'])) self.targets.append(ct)
def get_change_addr(self, color_def): """Get an address associated with color definition <color_def> that is in the current wallet for receiving change. """ color_id = color_def.color_id wam = self.model.get_address_manager() color_set = None if color_def == UNCOLORED_MARKER: color_set = ColorSet.from_color_ids(self.model.get_color_map(), [0]) elif self.asset.get_color_set().has_color_id(color_id): color_set = self.asset.get_color_set() if color_set is None: raise InvalidColorIdError('wrong color id') aw = wam.get_change_address(color_set) return aw.get_address()
def get_change_addr(self, color_def): """Get an address associated with color definition <color_def> that is in the current wallet for receiving change. """ color_id = color_def.color_id wam = self.model.get_address_manager() color_set = None if color_def == UNCOLORED_MARKER: color_set = ColorSet.from_color_ids(self.model.get_color_map(), [0]) elif self.asset.get_color_set().has_color_id(color_id): color_set = self.asset.get_color_set() if color_set is None: raise InvalidColorIdError('wrong color id') aw = wam.get_change_address(color_set) return aw.get_address()
def make_query(self, query): """Create a CoinQuery from query <query>. Queries are dicts with: color_set - color associated with this query """ query = query.copy() color_set = query.get('color_set') if not color_set: if 'color_id_set' in query: color_set = ColorSet.from_color_ids( self.model.get_color_map(), query['color_id_set']) elif 'asset' in query: color_set = query['asset'].get_color_set() else: raise Exception('Color set is not specified!') if 'spent' not in query: query['spent'] = False return CoinQuery(self.model, color_set, query)
def select_inputs(self, colorvalue): cs = ColorSet.from_color_ids(self.model.get_color_map(), [colorvalue.get_color_id()]) cq = self.model.make_coin_query({"color_set": cs}) utxo_list = cq.get_result() selection = [] csum = SimpleColorValue(colordef=colorvalue.get_colordef(), value=0) for utxo in utxo_list: csum += SimpleColorValue.sum(utxo.colorvalues) selection.append(utxo) if csum >= colorvalue: break if csum < colorvalue: raise InsufficientFundsError('not enough money') return selection, (csum - colorvalue)
def select_inputs(self, colorvalue): cs = ColorSet.from_color_ids(self.model.get_color_map(), [colorvalue.get_color_id()]) cq = self.model.make_coin_query({"color_set": cs}) utxo_list = cq.get_result() selection = [] csum = SimpleColorValue(colordef=colorvalue.get_colordef(), value=0) for utxo in utxo_list: csum += SimpleColorValue.sum(utxo.colorvalues) selection.append(utxo) if csum >= colorvalue: break if csum < colorvalue: raise InsufficientFundsError('not enough money') return selection, (csum - colorvalue)
def resolve_color_spec(self, color_spec): colormap = self.model.get_color_map() color_id = colormap.resolve_color_desc(color_spec, False) if color_id is None: raise Exception("color spec not recognized") return ColorSet.from_color_ids(self.model.get_color_map(), [color_id])
def get_change_addr(self, color_def): color_id = color_def.color_id cs = ColorSet.from_color_ids(self.model.get_color_map(), [color_id]) wam = self.model.get_address_manager() return wam.get_change_address(cs).get_address()
def get_change_addr(self, color_def): color_id = color_def.color_id cs = ColorSet.from_color_ids(self.model.get_color_map(), [color_id]) wam = self.model.get_address_manager() return wam.get_change_address(cs).get_address()
def resolve_color_spec(self, color_spec): colormap = self.model.get_color_map() color_id = colormap.resolve_color_desc(color_spec, False) if color_id is None: raise InvalidColorIdError("color spec not recognized") return ColorSet.from_color_ids(self.model.get_color_map(), [color_id])
def get_asset_by_color_id(self, colorid): colorset = ColorSet.from_color_ids(self.colormap, [colorid]) asset = self.find_asset_by_color_set(colorset) if not asset: raise Exception('Asset not found!') return asset
def get_asset_by_color_id(self, colorid): colorset = ColorSet.from_color_ids(self.colormap, [colorid]) asset = self.find_asset_by_color_set(colorset) if not asset: raise Exception('Asset not found!') return asset