Skip to content

Commit

Permalink
HTLC implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
xeroc committed Feb 28, 2019
1 parent 436fb40 commit e4d743d
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 32 deletions.
46 changes: 46 additions & 0 deletions bitshares/bitshares.py
Original file line number Diff line number Diff line change
Expand Up @@ -1344,3 +1344,49 @@ def asset_settle(self, amount, account=None, **kwargs):
}
)
return self.finalizeOp(op, account, "active", **kwargs)

def htlc_create(
self,
amount,
to,
preimage,
hash_type=None,
account=None,
expiration=60 * 60,
**kwargs
):
from binascii import hexlify
from graphenebase.base58 import ripemd160

if not account:
if "default_account" in self.config:
account = self.config["default_account"]
if not account:
raise ValueError("You need to provide an account")
account = Account(account, blockchain_instance=self)
to = Account(to, blockchain_instance=self)

if not isinstance(amount, (Amount)):
raise ValueError("'amount' must be of type Amount")

if hash_type == "sha1":
raise NotImplementedError
else:
preimage_type = 0
preimage_hash = hexlify(
ripemd160(hexlify(bytes(preimage, "utf-8")))
).decode("ascii")

op = operations.Htlc_create(
**{
"fee": {"amount": 0, "asset_id": "1.3.0"},
"from": account["id"],
"to": to["id"],
"amount": amount.json(),
"preimage_hash": [preimage_type, preimage_hash],
"preimage_size": len(preimage),
"claim_period_seconds": expiration,
"extensions": [],
}
)
return self.finalizeOp(op, account, "active", **kwargs)
60 changes: 36 additions & 24 deletions bitsharesbase/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,12 +472,16 @@ def __init__(self, *args, **kwargs):
if len(args) == 1 and len(kwargs) == 0:
kwargs = args[0]

super().__init__(OrderedDict([
('fee', Asset(kwargs["fee"])),
('issuer', ObjectId(kwargs["issuer"], "account")),
('amount_to_claim', Asset(kwargs["amount_to_claim"])),
('extensions', Set([])),
]))
super().__init__(
OrderedDict(
[
("fee", Asset(kwargs["fee"])),
("issuer", ObjectId(kwargs["issuer"], "account")),
("amount_to_claim", Asset(kwargs["amount_to_claim"])),
("extensions", Set([])),
]
)
)


class Asset_claim_pool(GrapheneObject):
Expand All @@ -488,13 +492,17 @@ def __init__(self, *args, **kwargs):
if len(args) == 1 and len(kwargs) == 0:
kwargs = args[0]

super().__init__(OrderedDict([
('fee', Asset(kwargs["fee"])),
('issuer', ObjectId(kwargs["issuer"], "account")),
('asset_id', ObjectId(kwargs["asset_id"], "asset")),
('amount_to_claim', Asset(kwargs["amount_to_claim"])),
('extensions', Set([])),
]))
super().__init__(
OrderedDict(
[
("fee", Asset(kwargs["fee"])),
("issuer", ObjectId(kwargs["issuer"], "account")),
("asset_id", ObjectId(kwargs["asset_id"], "asset")),
("amount_to_claim", Asset(kwargs["amount_to_claim"])),
("extensions", Set([])),
]
)
)


class Override_transfer(GrapheneObject):
Expand Down Expand Up @@ -824,13 +832,20 @@ def __init__(self, *args, **kwargs):
if len(args) == 1 and len(kwargs) == 0:
kwargs = args[0]

super().__init__(OrderedDict([
('fee', Asset(kwargs["fee"])),
('issuer', ObjectId(kwargs["issuer"], "account")),
('asset_to_settle', ObjectId(kwargs["asset_to_settle"], "asset")),
('settle_price', Price(kwargs["settle_price"])),
('extensions', Set([])),
]))
super().__init__(
OrderedDict(
[
("fee", Asset(kwargs["fee"])),
("issuer", ObjectId(kwargs["issuer"], "account")),
(
"asset_to_settle",
ObjectId(kwargs["asset_to_settle"], "asset"),
),
("settle_price", Price(kwargs["settle_price"])),
("extensions", Set([])),
]
)
)


class Committee_member_create(GrapheneObject):
Expand Down Expand Up @@ -943,10 +958,7 @@ def detail(self, *args, **kwargs):
("fee", Asset(kwargs["fee"])),
("htlc_id", ObjectId(kwargs["htlc_id"], "htlc")),
("redeemer", ObjectId(kwargs["redeemer"], "account")),
(
"preimage", # Bytes(kwargs["preimage"])
Array([Uint8(o) for o in unhexlify(kwargs["preimage"])]),
),
("preimage", Bytes(kwargs["preimage"])),
("extensions", Set([])),
]
)
Expand Down
15 changes: 7 additions & 8 deletions tests/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,9 +794,9 @@ def test_asset_global_settle(self):
"issuer": "1.2.123",
"asset_to_settle": "1.3.0",
"settle_price": {
"base": {"amount": 1123456, "asset_id": "1.3.0"},
"quote": {"amount": 78901122, "asset_id": "1.3.0"},
},
"base": {"amount": 1123456, "asset_id": "1.3.0"},
"quote": {"amount": 78901122, "asset_id": "1.3.0"},
},
"extensions": [],
}
)
Expand Down Expand Up @@ -844,16 +844,15 @@ def test_asset_claim_pool(self):
)
self.doit()


"""
def test_htlc_create(self):
preimage_hash = hexlify(ripemd160(hexlify(b"foobar"))).decode("ascii")
self.op = operations.Htlc_create(
**{
"fee": {"amount": 0, "asset_id": "1.3.0"},
"from": "1.2.123",
"to": "1.2.124",
"amount": {"amount": 1123456, "asset_id": "1.3.0"},
"preimage_hash": [0, ripemd160("foobar")],
"preimage_hash": [0, preimage_hash],
"preimage_size": 200,
"claim_period_seconds": 120,
"extensions": [],
Expand All @@ -870,11 +869,12 @@ def test_htlc_create(self):
self.doit(False)

def test_htlc_redeem(self):
preimage = hexlify(b"foobar").decode("ascii")
self.op = operations.Htlc_redeem(
**{
"fee": {"amount": 0, "asset_id": "1.3.0"},
"redeemer": "1.2.124",
"preimage": hexlify(b"foobar").decode("ascii"),
"preimage": preimage,
"htlc_id": "1.16.132",
"extensions": [],
}
Expand Down Expand Up @@ -904,7 +904,6 @@ def test_htlc_extend(self):
"c6320840059f85da3fbebaf2a965bb5eca15179f30"
)
self.doit(0)
"""

def compareConstructedTX(self):
self.maxDiff = None
Expand Down

0 comments on commit e4d743d

Please sign in to comment.