示例#1
0
def create_receiver_HTLC_TXS(pubkeySender, pubkeyReceiver, HTLCValue, balanceSender,
                           balanceReceiver, hashR, addressFunding, fundingScript,asset_id):
    receiver_HCTX = create_receiver_HCTX(pubkeySender=pubkeySender, pubkeyReceiver=pubkeyReceiver,
                                     HTLCValue=HTLCValue, balanceSender=balanceSender,
                                     balanceReceiver=balanceReceiver, hashR=hashR,
                                     addressFunding=addressFunding, fundingScript=fundingScript,asset_id=asset_id)

    receiver_RDTX = create_receiver_RDTX(addressReceiver=pubkeyToAddress(pubkeyReceiver),
                                         balanceReceiver=balanceReceiver, receiver_HCTxId=receiver_HCTX["txId"],
                                         RSMCScript=receiver_HCTX["RSMCscript"],asset_id=asset_id)

    HTDTX = createHTDTX(pubkeySender=pubkeySender,HTLCValue=HTLCValue, HTLCScript=receiver_HCTX["HTLCscript"],
                        receiver_HCTxId=receiver_HCTX["txId"],asset_id=asset_id)

    HETX = createHETX(pubkeySender=pubkeySender,
                      pubkeyReceiver=pubkeyReceiver, HTLCValue=HTLCValue,
                      HTLCScript=receiver_HCTX["HTLCscript"],receiver_HCTxId=receiver_HCTX["txId"],asset_id=asset_id)

    HERDTX = createHERDTX(addressReceiver=pubkeyToAddress(pubkeyReceiver),
                          HTLCValue=HTLCValue, HETxId=HETX["txId"], RSMCScript=HETX["RSMCscript"],asset_id=asset_id)

    return {
        "HCTX": receiver_HCTX,
        "RDTX": receiver_RDTX,
        "HTDTX": HTDTX,
        "HETX": HETX,
        "HERDTX": HERDTX
    }
示例#2
0
def create_rsmc(pubkeySelf, pubkeyOther, addressFunding, scriptFunding,
                balanceSelf, balanceOther, foundingTxId, assetType):

    if assetType == "NEO":
        assertId = setting.NEO_ASSETID
    elif assetType == "GAS":
        assertId = setting.GAS_ASSETID
    else:
        assertId = setting.CONTRACTHASH

    C_tx = createCTX(addressFunding=addressFunding,
                     balanceSelf=balanceSelf,
                     balanceOther=balanceOther,
                     pubkeySelf=pubkeySelf,
                     pubkeyOther=pubkeyOther,
                     fundingScript=scriptFunding,
                     asset_id=assertId,
                     fundingTxId=foundingTxId)

    RD_tx = createRDTX(addressRSMC=C_tx["addressRSMC"],
                       addressSelf=pubkeyToAddress(pubkeySelf),
                       balanceSelf=balanceSelf,
                       CTxId=C_tx["txId"],
                       RSMCScript=C_tx["scriptRSMC"],
                       asset_id=assertId)

    BR_tx = createBRTX(C_tx["addressRSMC"], pubkeyToAddress(pubkeyOther),
                       balanceSelf, C_tx["scriptRSMC"], C_tx["txId"], assertId)

    return {"C_TX": C_tx, "R_TX": RD_tx, "BR_TX": BR_tx}
示例#3
0
def createRefundTX(addressFunding, balanceSelf, balanceOther, pubkeySelf,
                   pubkeyOther, fundingScript, asset_id):

    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_self = create_opdata(address_from=addressFunding,
                                    address_to=pubkeyToAddress(pubkeySelf),
                                    value=balanceSelf,
                                    contract_hash=asset_id)
    op_data_to_other = create_opdata(address_from=addressFunding,
                                     address_to=pubkeyToAddress(pubkeyOther),
                                     value=balanceOther,
                                     contract_hash=asset_id)

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

    return {
        "txData": tx.get_tx_data(),
        "txId": createTxid(tx.get_tx_data()),
        "witness": "018240{signSelf}40{signOther}da" + fundingScript
    }
示例#4
0
def createRefundTX(addressFunding,balanceSelf,balanceOther,pubkeySelf,pubkeyOther,fundingScript,asset_id):

    time_stamp = TransactionAttribute(usage=TransactionAttributeUsage.Remark,
                                      data=bytearray.fromhex(hex(int(time.time()))[2:]))

    txAttributes = [time_stamp]
    tx = ContractTransaction()

    funding_vouts=get_vout_by_address(addressFunding,balanceSelf+balanceOther,asset_id)
    if not funding_vouts:
        return None



    funding_inputs=[tx.createInput(preHash=item[0], preIndex=item[2]) for item in funding_vouts ]


    output_to_self= tx.createOutput(assetId=asset_id, amount=balanceSelf,address=pubkeyToAddress(pubkeySelf))
    output_to_other= tx.createOutput(assetId=asset_id, amount=balanceOther,address=pubkeyToAddress(pubkeyOther))
    tx.inputs = funding_inputs

    tx.outputs = [output_to_self,output_to_other]
    tx.Attributes = txAttributes


    return {
        "txData":tx.get_tx_data(),
        "txId":createTxid(tx.get_tx_data()),
        "witness":"018240{signSelf}40{signOther}da"+fundingScript
    }
示例#5
0
def create_sender_HTLC_TXS(pubkeySender, pubkeyReceiver, HTLCValue,
                           balanceSender, balanceReceiver, hashR,
                           addressFunding, fundingScript, asset_id):
    sender_HCTX = create_sender_HCTX(pubkeySender=pubkeySender,
                                     pubkeyReceiver=pubkeyReceiver,
                                     HTLCValue=HTLCValue,
                                     balanceSender=balanceSender,
                                     balanceReceiver=balanceReceiver,
                                     hashR=hashR,
                                     addressFunding=addressFunding,
                                     fundingScript=fundingScript,
                                     asset_id=asset_id)

    sender_RDTX = create_sender_RDTX(
        addressRSMC=sender_HCTX["addressRSMC"],
        addressSender=pubkeyToAddress(pubkeySender),
        balanceSender=balanceSender,
        senderHCTxId=sender_HCTX["txId"],
        RSMCScript=sender_HCTX["RSMCscript"],
        asset_id=asset_id)

    HEDTX = createHEDTX(addressHTLC=sender_HCTX["addressHTLC"],
                        addressReceiver=pubkeyToAddress(pubkeyReceiver),
                        HTLCValue=HTLCValue,
                        HTLCScript=sender_HCTX["HTLCscript"],
                        asset_id=asset_id)

    HTTX = createHTTX(addressHTLC=sender_HCTX["addressHTLC"],
                      pubkeySender=pubkeySender,
                      pubkeyReceiver=pubkeyReceiver,
                      HTLCValue=HTLCValue,
                      HTLCScript=sender_HCTX["HTLCscript"],
                      asset_id=asset_id)

    HTRDTX = createHTRDTX(addressRSMC=HTTX["addressRSMC"],
                          addressSender=pubkeyToAddress(pubkeySender),
                          HTLCValue=HTLCValue,
                          HTTxId=HTTX["txId"],
                          RSMCScript=HTTX["RSMCscript"],
                          asset_id=asset_id)

    return {
        "HCTX": sender_HCTX,
        "RDTX": sender_RDTX,
        "HEDTX": HEDTX,
        "HTTX": HTTX,
        "HTRDTX": HTRDTX
    }
示例#6
0
def createCTX(balanceSelf,balanceOther,pubkeySelf,pubkeyOther,fundingScript,asset_id,fundingTxId):
    RSMCContract=createRSMCContract(hashSelf=pubkeyToAddressHash(pubkeySelf.strip()),pubkeySelf=pubkeySelf.strip(),
                                    hashOther=pubkeyToAddressHash(pubkeyOther.strip()),
                                    pubkeyOther=pubkeyOther.strip(),magicTimestamp=time.time())
    time_stamp = TransactionAttribute(usage=TransactionAttributeUsage.Remark,
                                      data=bytearray.fromhex(hex(int(time.time()))[2:]))

    txAttributes = [time_stamp]
    tx = ContractTransaction()

    funding_inputs=[tx.createInput(preHash=fundingTxId, preIndex=0)]


    output_to_RSMC= tx.createOutput(assetId=asset_id, amount=balanceSelf,address=RSMCContract["address"])
    output_to_other= tx.createOutput(assetId=asset_id, amount=balanceOther,address=pubkeyToAddress(pubkeyOther))
    tx.inputs = funding_inputs

    tx.outputs = [output_to_RSMC,output_to_other]
    tx.Attributes = txAttributes



    return {
        "txData":tx.get_tx_data(),
        "addressRSMC":RSMCContract["address"],
        "scriptRSMC":RSMCContract["script"],
        "txId":createTxid(tx.get_tx_data()),
        "witness":"018240{signSelf}40{signOther}da"+fundingScript
    }
示例#7
0
def createHTDTX(addressHTLC, pubkeySender, HTLCValue, HTLCScript, asset_id):

    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_sender = create_opdata(address_from=addressHTLC,
                                      address_to=pubkeyToAddress(pubkeySender),
                                      value=HTLCValue,
                                      contract_hash=asset_id)

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

    return {
        "txData":
        tx.get_tx_data(),
        "txId":
        createTxid(tx.get_tx_data()),
        "witness":
        "01{R_script}40{signOther}40{signSelf}fd" +
        createVerifyScript(HTLCScript)
    }
示例#8
0
def create_sender_HCTX(pubkeySender, pubkeyReceiver, HTLCValue, balanceSender,
                       balanceReceiver, hashR, addressFunding, fundingScript,
                       asset_id):
    RSMCContract = createRSMCContract(
        hashSelf=pubkeyToAddressHash(pubkeySender),
        pubkeySelf=pubkeySender,
        hashOther=pubkeyToAddressHash(pubkeyReceiver),
        pubkeyOther=pubkeyReceiver,
        magicTimestamp=time.time())

    HTLCContract = createHTLCContract(pubkeySelf=pubkeySender,
                                      pubkeyOther=pubkeyReceiver,
                                      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=asset_id)
    op_data_to_RSMC = create_opdata(address_from=addressFunding,
                                    address_to=RSMCContract["address"],
                                    value=balanceSender,
                                    contract_hash=asset_id)
    op_data_to_receiver = create_opdata(
        address_from=addressFunding,
        address_to=pubkeyToAddress(pubkeyReceiver),
        value=balanceReceiver,
        contract_hash=asset_id)

    tx = InvocationTransaction()
    tx.Version = 1
    tx.Attributes = txAttributes
    tx.Script = binascii.unhexlify(op_data_to_RSMC + op_data_to_receiver +
                                   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}da" + fundingScript
    }
示例#9
0
def create_sender_HCTX(pubkeySender, pubkeyReceiver, HTLCValue, balanceSender, balanceReceiver, hashR,
                   addressFunding, fundingScript,asset_id):
    RSMCContract = createRSMCContract(hashSelf=pubkeyToAddressHash(pubkeySender), pubkeySelf=pubkeySender,
                                      hashOther=pubkeyToAddressHash(pubkeyReceiver), pubkeyOther=pubkeyReceiver,
                                      magicTimestamp=time.time())

    HTLCContract = createHTLCContract(pubkeySelf=pubkeySender, pubkeyOther=pubkeyReceiver,
                                      futureTimestamp=int(time.time()) + 600,
                                      hashR=hashR)
    time_stamp = TransactionAttribute(usage=TransactionAttributeUsage.Remark,
                                      data=bytearray.fromhex(hex(int(time.time()))[2:]))


    txAttributes = [time_stamp]
    tx = ContractTransaction()

    funding_vouts=get_vout_by_address(addressFunding,balanceSender+balanceReceiver+HTLCValue,asset_id)
    if not funding_vouts:
        return None

    funding_inputs=[tx.createInput(preHash=item[0], preIndex=item[2]) for item in funding_vouts ]

    output_to_RSMC= tx.createOutput(assetId=asset_id, amount=balanceSender,address=RSMCContract["address"])
    output_to_HTLC= tx.createOutput(assetId=asset_id, amount=HTLCValue,address=HTLCContract["address"])
    output_to_receiver= tx.createOutput(assetId=asset_id,
                                        amount=balanceReceiver,address=pubkeyToAddress(pubkeyReceiver))

    tx.inputs = funding_inputs
    tx.outputs = [output_to_RSMC,output_to_receiver,output_to_HTLC]
    tx.Attributes = txAttributes



    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}da" + fundingScript
    }
示例#10
0
def createHTDTX(pubkeySender,HTLCValue, HTLCScript,receiver_HCTxId,asset_id):

    time_stamp = TransactionAttribute(usage=TransactionAttributeUsage.Remark,
                                      data=bytearray.fromhex(hex(int(time.time()))[2:]))

    txAttributes = [time_stamp]
    tx = ContractTransaction()

    HTLC_inputs = [tx.createInput(preHash=receiver_HCTxId, preIndex=2)]
    output_to_sender = tx.createOutput(assetId=asset_id, amount=HTLCValue, address=pubkeyToAddress(pubkeySender))

    tx.inputs = HTLC_inputs
    tx.outputs = [output_to_sender]
    tx.Attributes = txAttributes

    return {
        "txData": tx.get_tx_data(),
        "txId": createTxid(tx.get_tx_data()),
        "witness": "01830040{signOther}40{signSelf}fd"+createVerifyScript(HTLCScript) #R is empty
    }
示例#11
0
def create_funder(pubkeySelf, pubkeyOther, deposit, assetType):
    walletSelf = {"pubkey": pubkeySelf, "deposit": deposit}
    walletOther = {"pubkey": pubkeyOther, "deposit": deposit}

    if assetType == "NEO":
        assertId = setting.NEO_ASSETID
    elif assetType == "GAS":
        assertId = setting.GAS_ASSETID
    else:
        assertId = setting.CONTRACTHASH

    founder = createFundingTx(walletSelf, walletOther, assertId)
    commitment = createCTX(founder.get("addressFunding"),
                           float(walletSelf["deposit"]),
                           float(walletOther["deposit"]),
                           walletSelf["pubkey"], walletOther["pubkey"],
                           founder.get("scriptFunding"), assertId,
                           founder.get('txId'))
    address_self = pubkeyToAddress(walletSelf["pubkey"])
    revocabledelivery = createRDTX(commitment.get("addressRSMC"), address_self,
                                   float(walletSelf["deposit"]),
                                   commitment.get("txId"),
                                   commitment.get("scriptRSMC"), assertId)
    return {"Founder": founder, "C_TX": commitment, "R_TX": revocabledelivery}
示例#12
0
def createFundingTx(walletSelf,walletOther,asset_id):
    '''

    :param walletSelf: dict {
            "pubkey":"",
            "deposit":0,
            "assetId":"0x00000"
    }
    :param walletOhter: dict {
            "pubkey":"",
            "deposit":0,
            "assetId":"0x00000"
    :return:
    '''

    multi_contract = createMultiSigContract(walletSelf["pubkey"],walletOther["pubkey"])
    contractAddress = multi_contract["address"]
    time_stamp = TransactionAttribute(usage=TransactionAttributeUsage.Remark,
                                      data=bytearray.fromhex(hex(int(time.time()))[2:]))

    txAttributes = [time_stamp]

    tx = ContractTransaction()

    walletSelf_vouts=get_vout_by_address(pubkeyToAddress(walletSelf["pubkey"]),walletSelf["deposit"],asset_id)
    if not walletSelf_vouts:
        return None

    walletOther_vouts=get_vout_by_address(pubkeyToAddress(walletOther["pubkey"]),walletOther["deposit"],asset_id)
    if not walletOther_vouts:
        return None

    self_inputs=[tx.createInput(preHash=item[0], preIndex=item[2]) for item in walletSelf_vouts ]
    other_inputs=[tx.createInput(preHash=item[0], preIndex=item[2]) for item in walletOther_vouts ]

    output_to_fundingaddress = tx.createOutput(assetId=asset_id, amount=walletSelf["deposit"]+walletOther["deposit"],
                              address=contractAddress)
    self_inputs_total=0
    other_inputs_total=0
    for item in walletSelf_vouts:
        self_inputs_total+=item[1]
    for item in walletOther_vouts:
        other_inputs_total+=item[1]

    output_to_self= tx.createOutput(assetId=asset_id, amount=self_inputs_total-walletSelf["deposit"],
                              address=pubkeyToAddress(walletSelf["pubkey"])) if self_inputs_total>walletSelf["deposit"] \
                                else None
    output_to_other= tx.createOutput(assetId=asset_id, amount=other_inputs_total-walletOther["deposit"],
                              address=pubkeyToAddress(walletOther["pubkey"])) if other_inputs_total>walletOther["deposit"] \
                                else None
    tx.inputs = self_inputs+other_inputs

    tx.outputs = [item for item in (output_to_fundingaddress,output_to_self,output_to_other) if item]
    tx.Attributes = txAttributes

    hash_self=ToAddresstHash(pubkeyToAddress(walletSelf["pubkey"]))
    hash_other=ToAddresstHash(pubkeyToAddress(walletOther["pubkey"]))
    if hash_self > hash_other:
        witness = "024140{signOther}2321"+walletOther["pubkey"]+"ac"+"4140{signSelf}2321"+walletSelf["pubkey"]+"ac"
    else:
        witness = "024140{signSelf}2321"+walletSelf["pubkey"]+"ac"+"4140{signOther}2321"+walletOther["pubkey"]+"ac"

    return {
        "txData":tx.get_tx_data(),
        "addressFunding":contractAddress,
        "txId": createTxid(tx.get_tx_data()),
        "scriptFunding":multi_contract["script"],
        "witness":witness
    }
示例#13
0
def createFundingTx(walletSelf, walletOther, asset_id):
    '''

    :param walletSelf: dict {git
            "pubkey":"",
            "deposit":0
    }
    :param walletOhter: dict {
            "pubkey":"",
            "deposit":0
    :return:
    '''

    multi_contract = createMultiSigContract(walletSelf["pubkey"],
                                            walletOther["pubkey"])
    contractAddress = multi_contract["address"]
    time_stamp = TransactionAttribute(usage=TransactionAttributeUsage.Remark,
                                      data=bytearray.fromhex(
                                          hex(int(time.time()))[2:]))
    address_hash_self = TransactionAttribute(
        usage=TransactionAttributeUsage.Script,
        data=ToAddresstHash(pubkeyToAddress(walletSelf["pubkey"])).Data)
    address_hash_other = TransactionAttribute(
        usage=TransactionAttributeUsage.Script,
        data=ToAddresstHash(pubkeyToAddress(walletOther["pubkey"])).Data)
    txAttributes = [address_hash_self, address_hash_other, time_stamp]

    op_dataSelf = create_opdata(address_from=pubkeyToAddress(
        walletSelf["pubkey"]),
                                address_to=contractAddress,
                                value=walletSelf["deposit"],
                                contract_hash=asset_id)
    op_dataOther = create_opdata(address_from=pubkeyToAddress(
        walletOther["pubkey"]),
                                 address_to=contractAddress,
                                 value=walletOther["deposit"],
                                 contract_hash=asset_id)

    tx = InvocationTransaction()
    tx.Version = 1
    tx.Attributes = txAttributes
    tx.Script = binascii.unhexlify(op_dataSelf + op_dataOther)

    hash_self = ToAddresstHash(pubkeyToAddress(walletSelf["pubkey"]))
    hash_other = ToAddresstHash(pubkeyToAddress(walletOther["pubkey"]))
    if hash_self > hash_other:
        witness = "024140{signOther}2321" + walletOther[
            "pubkey"] + "ac" + "4140{signSelf}2321" + walletSelf[
                "pubkey"] + "ac"
    else:
        witness = "024140{signSelf}2321" + walletSelf[
            "pubkey"] + "ac" + "4140{signOther}2321" + walletOther[
                "pubkey"] + "ac"

    return {
        "txData": tx.get_tx_data(),
        "addressFunding": contractAddress,
        "txId": createTxid(tx.get_tx_data()),
        "scriptFunding": multi_contract["script"],
        "witness": witness
    }