Skip to content

Commit

Permalink
Merge pull request #34 from trinity-project/tdev
Browse files Browse the repository at this point in the history
Tdev
  • Loading branch information
pigxo authored Apr 18, 2018
2 parents f8171f1 + edc84e3 commit 8436ad5
Show file tree
Hide file tree
Showing 10 changed files with 467 additions and 41 deletions.
302 changes: 301 additions & 1 deletion TX/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,304 @@ def createRefundTX(addressFunding,balanceSelf,balanceOther,pubkeySelf,pubkeyOthe
"txData":tx.get_tx_data(),
"txId":createTxid(tx.get_tx_data()),
"witness":"018240{signSelf}40{signOther}47"+fundingScript
}
}


def createSelfHCTX(pubkeySelf, pubkeyOther, HTLCValue, balanceSelf, balanceOther, hashR, addressFunding, fundingScript):
RSMCContract = createRSMCContract(hashSelf=pubkeyToAddressHash(pubkeySelf), pubkeySelf=pubkeySelf,
hashOther=pubkeyToAddressHash(pubkeyOther), pubkeyOther=pubkeyOther,
magicTimestamp=time.time())

HTLCContract = createHTLCContract(pubkeySelf=pubkeySelf, pubkeyOther=pubkeyOther,
futureTimestamp=int(time.time()) + 600,
hashR=hashR)
time_stamp = TransactionAttribute(usage=TransactionAttributeUsage.Remark,
data=bytearray.fromhex(hex(int(time.time()))[2:]))
address_hash_funding = TransactionAttribute(usage=TransactionAttributeUsage.Script,
data=ToAddresstHash(addressFunding).Data)
txAttributes = [address_hash_funding, time_stamp]

op_data_to_HTLC = create_opdata(address_from=addressFunding, address_to=HTLCContract["address"], value=HTLCValue,
contract_hash=TNC)
op_data_to_RSMC = create_opdata(address_from=addressFunding, address_to=RSMCContract["address"],
value=balanceSelf - HTLCValue, contract_hash=TNC)
op_data_to_other = create_opdata(address_from=addressFunding, address_to=pubkeyToAddress(pubkeyOther),
value=balanceOther, contract_hash=TNC)

tx = InvocationTransaction()
tx.Version = 1
tx.Attributes = txAttributes
tx.Script = binascii.unhexlify(op_data_to_RSMC + op_data_to_other + op_data_to_HTLC)

return {
"txData": tx.get_tx_data(),
"addressRSMC": RSMCContract["address"],
"addressHTLC": HTLCContract["address"],
"RSMCscript": RSMCContract["script"],
"HTLCscript": HTLCContract["script"],
"txId": createTxid(tx.get_tx_data()),
"witness": "018240{signSelf}40{signOther}47" + fundingScript
}


def createSelfRDTX(addressRSMC, addressSelf, balanceSelf, selfHCTxId, RSMCScript):
time_stamp = TransactionAttribute(usage=TransactionAttributeUsage.Remark,
data=bytearray.fromhex(hex(int(time.time()))[2:]))
address_hash_RSMC = TransactionAttribute(usage=TransactionAttributeUsage.Script,
data=ToAddresstHash(addressRSMC).Data)
pre_txid = TransactionAttribute(usage=TransactionAttributeUsage.Remark1, data=bytearray.fromhex(
hex_reverse(selfHCTxId)))

outputTo = TransactionAttribute(usage=TransactionAttributeUsage.Remark2, data=ToAddresstHash(addressSelf).Data)

txAttributes = [address_hash_RSMC, time_stamp, pre_txid, outputTo]

op_data_to_self = create_opdata(address_from=addressRSMC, address_to=addressSelf, value=balanceSelf,
contract_hash=TNC)

tx = InvocationTransaction()
tx.Version = 1
tx.Attributes = txAttributes
tx.Script = binascii.unhexlify(op_data_to_self)

return {
"txData": tx.get_tx_data(),
"txId": createTxid(tx.get_tx_data()),
"witness_part1": "01{lengthAll}{lengthOfBlockheight}{blockheight}40{signSelf}40{signOther}",
"witness_part2": createVerifyScript(RSMCScript)
}


def createOtherHCTX(pubkeySelf, pubkeyOther, HTLCValue, balanceSelf, balanceOther, hashR, addressFunding,
fundingScript):
RSMCContract = createRSMCContract(hashSelf=pubkeyToAddressHash(pubkeySelf), pubkeySelf=pubkeySelf,
hashOther=pubkeyToAddressHash(pubkeyOther), pubkeyOther=pubkeyOther,
magicTimestamp=time.time())

HTLCContract = createHTLCContract(pubkeySelf=pubkeySelf, pubkeyOther=pubkeyOther,
futureTimestamp=int(time.time()) + 600,
hashR=hashR)
time_stamp = TransactionAttribute(usage=TransactionAttributeUsage.Remark,
data=bytearray.fromhex(hex(int(time.time()))[2:]))
address_hash_funding = TransactionAttribute(usage=TransactionAttributeUsage.Script,
data=ToAddresstHash(addressFunding).Data)
txAttributes = [address_hash_funding, time_stamp]

op_data_to_HTLC = create_opdata(address_from=addressFunding, address_to=HTLCContract["address"], value=HTLCValue,
contract_hash=TNC)
op_data_to_RSMC = create_opdata(address_from=addressFunding, address_to=RSMCContract["address"], value=balanceSelf,
contract_hash=TNC)
op_data_to_other = create_opdata(address_from=addressFunding, address_to=pubkeyToAddress(pubkeyOther),
value=balanceOther - HTLCValue, contract_hash=TNC)

tx = InvocationTransaction()
tx.Version = 1
tx.Attributes = txAttributes
tx.Script = binascii.unhexlify(op_data_to_RSMC + op_data_to_other + op_data_to_HTLC)

return {
"txData": tx.get_tx_data(),
"addressRSMC": RSMCContract["address"],
"addressHTLC": HTLCContract["address"],
"RSMCscript": RSMCContract["script"],
"HTLCscript": HTLCContract["script"],
"txId": createTxid(tx.get_tx_data()),
"witness": "018240{signSelf}40{signOther}47" + fundingScript
}


def createHTDTX(addressHTLC, pubkeySelf, pubkeyOther, HTLCValue, HTLCScript):
RSMCContract = createRSMCContract(hashSelf=pubkeyToAddressHash(pubkeySelf), pubkeySelf=pubkeySelf,
hashOther=pubkeyToAddressHash(pubkeyOther), pubkeyOther=pubkeyOther,
magicTimestamp=time.time())

time_stamp = TransactionAttribute(usage=TransactionAttributeUsage.Remark,
data=bytearray.fromhex(hex(int(time.time()))[2:]))
address_hash_HTLC = TransactionAttribute(usage=TransactionAttributeUsage.Script,
data=ToAddresstHash(addressHTLC).Data)
txAttributes = [address_hash_HTLC, time_stamp]

op_data_to_self = create_opdata(address_from=addressHTLC, address_to=RSMCContract["address"], value=HTLCValue,
contract_hash=TNC)

tx = InvocationTransaction()
tx.Version = 1
tx.Attributes = txAttributes
tx.Script = binascii.unhexlify(op_data_to_self)

return {
"txData": tx.get_tx_data(),
"txId": createTxid(tx.get_tx_data()),
"addressRSMC": RSMCContract["address"],
"RSMCscript": RSMCContract["script"],
"witness_part1": "01{lengthAll}{lengthOfR}{R}40{signOther}40{signSelf}",
"witness_part2": createVerifyScript(HTLCScript)
}


def createOtherRDTX(addressRSMC, addressSelf, balanceSelf, otherHCTxId, RSMCScript):
time_stamp = TransactionAttribute(usage=TransactionAttributeUsage.Remark,
data=bytearray.fromhex(hex(int(time.time()))[2:]))
address_hash_RSMC = TransactionAttribute(usage=TransactionAttributeUsage.Script,
data=ToAddresstHash(addressRSMC).Data)
pre_txid = TransactionAttribute(usage=TransactionAttributeUsage.Remark1, data=bytearray.fromhex(
hex_reverse(otherHCTxId)))

outputTo = TransactionAttribute(usage=TransactionAttributeUsage.Remark2, data=ToAddresstHash(addressSelf).Data)

txAttributes = [address_hash_RSMC, time_stamp, pre_txid, outputTo]

op_data_to_self = create_opdata(address_from=addressRSMC, address_to=addressSelf, value=balanceSelf,
contract_hash=TNC)

tx = InvocationTransaction()
tx.Version = 1
tx.Attributes = txAttributes
tx.Script = binascii.unhexlify(op_data_to_self)

return {
"txData": tx.get_tx_data(),
"txId": createTxid(tx.get_tx_data()),
"witness_part1": "01{lengthAll}{lengthOfBlockheight}{blockheight}40{signSelf}40{signOther}",
"witness_part2": createVerifyScript(RSMCScript)
}


def createHEDTX(addressHTLC, addressOther, HTLCValue, HTLCScript):
time_stamp = TransactionAttribute(usage=TransactionAttributeUsage.Remark,
data=bytearray.fromhex(hex(int(time.time()))[2:]))
address_hash_HTLC = TransactionAttribute(usage=TransactionAttributeUsage.Script,
data=ToAddresstHash(addressHTLC).Data)
txAttributes = [address_hash_HTLC, time_stamp]

op_data_to_other = create_opdata(address_from=addressHTLC, address_to=addressOther, value=HTLCValue,
contract_hash=TNC)

tx = InvocationTransaction()
tx.Version = 1
tx.Attributes = txAttributes
tx.Script = binascii.unhexlify(op_data_to_other)

return {
"txData": tx.get_tx_data(),
"txId": createTxid(tx.get_tx_data()),
"witness_part1": "01{lengthAll}{lengthOfR}{R}40{signOther}40{signSelf}",
"witness_part2": createVerifyScript(HTLCScript)
}


def createHTTX(addressHTLC, pubkeySelf, pubkeyOther, HTLCValue, HTLCScript):
RSMCContract = createRSMCContract(hashSelf=pubkeyToAddressHash(pubkeySelf), pubkeySelf=pubkeySelf,
hashOther=pubkeyToAddressHash(pubkeyOther), pubkeyOther=pubkeyOther,
magicTimestamp=time.time())

time_stamp = TransactionAttribute(usage=TransactionAttributeUsage.Remark,
data=bytearray.fromhex(hex(int(time.time()))[2:]))
address_hash_HTLC = TransactionAttribute(usage=TransactionAttributeUsage.Script,
data=ToAddresstHash(addressHTLC).Data)
txAttributes = [address_hash_HTLC, time_stamp]

op_data_to_self = create_opdata(address_from=addressHTLC, address_to=RSMCContract["address"], value=HTLCValue,
contract_hash=TNC)

tx = InvocationTransaction()
tx.Version = 1
tx.Attributes = txAttributes
tx.Script = binascii.unhexlify(op_data_to_self)

return {
"txData": tx.get_tx_data(),
"txId": createTxid(tx.get_tx_data()),
"addressRSMC": RSMCContract["address"],
"RSMCscript": RSMCContract["script"],
"witness_part1": "01{lengthAll}{lengthOfR}{R}40{signOther}40{signSelf}",
"witness_part2": createVerifyScript(HTLCScript)
}


def createHTRDTX(addressRSMC, addressSelf, HTLCValue, HTTxId, RSMCScript):
time_stamp = TransactionAttribute(usage=TransactionAttributeUsage.Remark,
data=bytearray.fromhex(hex(int(time.time()))[2:]))
address_hash_RSMC = TransactionAttribute(usage=TransactionAttributeUsage.Script,
data=ToAddresstHash(addressRSMC).Data)
pre_txid = TransactionAttribute(usage=TransactionAttributeUsage.Remark1, data=bytearray.fromhex(
hex_reverse(HTTxId)))

outputTo = TransactionAttribute(usage=TransactionAttributeUsage.Remark2, data=ToAddresstHash(addressSelf).Data)

txAttributes = [address_hash_RSMC, time_stamp, pre_txid, outputTo]

op_data_to_self = create_opdata(address_from=addressRSMC, address_to=addressSelf, value=HTLCValue,
contract_hash=TNC)

tx = InvocationTransaction()
tx.Version = 1
tx.Attributes = txAttributes
tx.Script = binascii.unhexlify(op_data_to_self)

return {
"txData": tx.get_tx_data(),
"txId": createTxid(tx.get_tx_data()),
"witness_part1": "01{lengthAll}{lengthOfBlockheight}{blockheight}40{signSelf}40{signOther}",
"witness_part2": createVerifyScript(RSMCScript)
}


def createChannel(walletSelf, walletOther):
funding_tx = createFundingTx(walletSelf=walletSelf, walletOther=walletOther)

self_C_tx = createCTX(addressFunding=funding_tx["addressFunding"], balanceSelf=walletSelf["deposit"],
balanceOther=walletOther["deposit"], pubkeySelf=walletSelf["pubkey"],
pubkeyOther=walletOther["pubkey"], fundingScript=funding_tx["scriptFunding"])

self_RD_tx = createRDTX(addressRSMC=self_C_tx["addressRSMC"], addressSelf=pubkeyToAddress(walletSelf["pubkey"]),
balanceSelf=walletSelf["deposit"], CTxId=self_C_tx["txId"],
RSMCScript=self_C_tx["scriptRSMC"])

other_BR_tx = createBRTX(addressRSMC=self_C_tx["addressRSMC"], addressOther=pubkeyToAddress(walletOther["pubkey"]),
balanceSelf=walletSelf["deposit"], RSMCScript=self_C_tx["scriptRSMC"])

other_C_tx = createCTX(addressFunding=funding_tx["addressFunding"], balanceSelf=walletOther["deposit"],
balanceOther=walletSelf["deposit"], pubkeySelf=walletOther["pubkey"],
pubkeyOther=walletSelf["pubkey"], fundingScript=funding_tx["scriptFunding"])

other_RD_tx = createRDTX(addressRSMC=other_C_tx["addressRSMC"], addressSelf=pubkeyToAddress(walletOther["pubkey"]),
balanceSelf=walletOther["deposit"], CTxId=other_C_tx["txId"],
RSMCScript=other_C_tx["scriptRSMC"])

self_BR_tx = createBRTX(addressRSMC=other_C_tx["addressRSMC"], addressOther=pubkeyToAddress(walletSelf["pubkey"]),
balanceSelf=walletSelf["deposit"], RSMCScript=other_C_tx["scriptRSMC"])

return {
"fundingTx": funding_tx,

"selfTxs": {
"C_TX": self_C_tx,
"RD_TX": self_RD_tx,
"BR_TX": self_BR_tx,
},

"otherTxs": {
"C_TX": other_C_tx,
"RD_TX": other_RD_tx,
"BR_TX": other_BR_tx,
},
}


def createHTLC_TXS(pubkeySelf, pubkeyOther, HTLCValue, balanceSelf, balanceOther, hashR, addressFunding, fundingScript):
self_HC_tx = createHCTX(pubkeySelf=pubkeySelf, pubkeyOther=pubkeyOther, HTLCValue=HTLCValue,
balanceSelf=balanceSelf,
balanceOther=balanceOther, hashR=hashR, addressFunding=addressFunding,
fundingScript=fundingScript)

other_HED_tx = createHEDTX(addressHTLC=self_HC_tx["addressHTLC"], addressOther=pubkeyToAddress(pubkeyOther),
HTLCValue=HTLCValue, HTLCScript=self_HC_tx["HTLCscript"])

self_HTT_tx = createHTTX(addressHTLC=self_HC_tx["addressHTLC"], addressSelf=pubkeyToAddress(pubkeySelf),
HTLCValue=HTLCValue, HTLCScript=self_HC_tx["HTLCscript"])

other_HC_tx = createHCTX(pubkeySelf=pubkeyOther, pubkeyOther=pubkeySelf, HTLCValue=HTLCValue,
balanceSelf=balanceSelf,
balanceOther=balanceOther, hashR=hashR, addressFunding=addressFunding,
fundingScript=fundingScript)
3 changes: 1 addition & 2 deletions TX/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ def pubkeyToAddress(pubkey):
return address




def ToAddresstHash(address):

data = b58decode(address)
Expand Down Expand Up @@ -177,5 +175,6 @@ def blockheight_to_script(input):
return output

if __name__=="__main__":
print(b58decode("b58decode"))
print (blockheight_to_script(1319028))
print (createTxid("d101a00400ca9a3b140069ec6703aa90a51280ab74eb92cb09cca0549514dfee2d95daf8b67b960aaf997900ab94abc3fd1b53c1087472616e7366657267f1dfcf0051ec48ec95c8d0569e0b95075d099d84f10400ca9a3b140069ec6703aa90a51280ab74eb92cb09cca05495142099925aaeee225009fc51b599c71fee77bd30ca53c1087472616e7366657267f1dfcf0051ec48ec95c8d0569e0b95075d099d84f100000000000000000320dfee2d95daf8b67b960aaf997900ab94abc3fd1b202099925aaeee225009fc51b599c71fee77bd30caf0045ac46e4b0000"))
2 changes: 1 addition & 1 deletion gateway/msg.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
"MessageType":"SyncWallet",
"MesssgeBody":{
"Publickey":publickey,
"CommitMinDeposit":Configure["Channel"]["CommitMinDeposit"]
"CommitMinDeposit":Configure["Channel"]["CommitMinDeposit"],
"Fee":Configure["Fee"]
}
}
Expand Down
2 changes: 1 addition & 1 deletion gateway/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def generate_sync_graph_msg(sync_type, sender, **kwargs):
"""
message = {
"MessageType": "SyncChannelState",
"SyncType": sync_type
"SyncType": sync_type,
"Sender": sender,
"Broadcast": True,
"Source": kwargs["source"],
Expand Down
11 changes: 6 additions & 5 deletions wallet/BlockChain/monior.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ def update_block_height(cls, blockheight):
if cls.Wallet:
cls.Wallet.SaveStoredData("BlockHeight", blockheight)
else:
LOG.debug("Wallet not opened")
#LOG.debug("Wallet not opened")
return None

@classmethod
def get_wallet_block_height(cls):
if cls.Wallet:
block_height = cls.Wallet.LoadStoredData("BlockHeight")
block_height = cls.Wallet.LoadStoredData("BlockHeight")
cls.Wallet_Change = False
return block_height
else:
LOG.debug("Wallet not opened")
#LOG.debug("Wallet not opened")
return None


Expand All @@ -82,9 +82,10 @@ def monitorblock():
except Exception as e:
pass
else:
LOG.debug("Not get the blockheight")

#LOG.debug("Not get the blockheight")
pass
blockheight_onchain = get_block_count()
blockheight = blockheight if blockheight else 0
if blockheight < blockheight_onchain:
time.sleep(1)
else:
Expand Down
Loading

0 comments on commit 8436ad5

Please sign in to comment.