Transaction.transfer add single input and outputs

This commit is contained in:
tim 2016-08-26 15:43:06 +02:00 committed by Sylvain Bellemare
parent 66f7ab6ea4
commit 46f0b2c62e

View File

@ -5,7 +5,6 @@ from uuid import uuid4
from cryptoconditions import ( from cryptoconditions import (
Fulfillment as CCFulfillment, Fulfillment as CCFulfillment,
Condition as CCCondition,
ThresholdSha256Fulfillment, ThresholdSha256Fulfillment,
Ed25519Fulfillment, Ed25519Fulfillment,
PreimageSha256Fulfillment, PreimageSha256Fulfillment,
@ -267,7 +266,8 @@ class Transaction(object):
self.data = data self.data = data
@classmethod @classmethod
def create(cls, owners_before, owners_after, payload, secret=None): def create(cls, owners_before, owners_after, payload, secret=None,
time_expire=None):
if not isinstance(owners_before, list): if not isinstance(owners_before, list):
raise TypeError('`owners_before` must be a list instance') raise TypeError('`owners_before` must be a list instance')
if not isinstance(owners_after, list): if not isinstance(owners_after, list):
@ -314,14 +314,42 @@ class Transaction(object):
ffill_tx = Fulfillment(ffill, owners_before) ffill_tx = Fulfillment(ffill, owners_before)
return cls(cls.CREATE, [ffill_tx], [cond_tx], data) return cls(cls.CREATE, [ffill_tx], [cond_tx], data)
elif len(owners_before) == 1 and len(owners_after) == 0 and time_expire is not None:
raise NotImplementedError('Timeout conditions will be implemented later')
elif len(owners_before) == 1 and len(owners_after) == 0 and secret is None: elif len(owners_before) == 1 and len(owners_after) == 0 and secret is None:
raise ValueError('Define a secret to create a hashlock condition') raise ValueError('Define a secret to create a hashlock condition')
else: else:
raise ValueError("This is not the case you're looking for ;)") raise ValueError("This is not the case you're looking for ;)")
@classmethod
def transfer(cls, inputs, owners_after, payload, secret=None, time_expiry=None):
if not isinstance(inputs, list):
raise TypeError('`inputs` must be a list instance')
if not isinstance(owners_after, list):
raise TypeError('`owners_after` must be a list instance')
data = Data(payload)
if len(inputs) == len(owners_after) and len(owners_after) == 1:
ffill_for_cond = Ed25519Fulfillment(public_key=owners_after[0])
cond_tx = Condition(ffill_for_cond, owners_after)
return cls(cls.TRANSFER, inputs, [cond_tx], data)
def __eq__(self, other): def __eq__(self, other):
return self.to_dict() == other.to_dict() return self.to_dict() == other.to_dict()
# TODO: There might be a better name
def to_spendable_fulfillments(self, condition_indices):
spendables = []
for cid in condition_indices:
input_cond = self.conditions[cid]
ffill = Fulfillment(input_cond.fulfillment,
input_cond.owners_after,
TransactionLink(self.id, cid))
spendables.append(ffill)
return spendables
def add_fulfillment(self, fulfillment): def add_fulfillment(self, fulfillment):
if fulfillment is not None and not isinstance(fulfillment, Fulfillment): if fulfillment is not None and not isinstance(fulfillment, Fulfillment):
raise TypeError('`fulfillment` must be a Fulfillment instance or None') raise TypeError('`fulfillment` must be a Fulfillment instance or None')