Ejemplo n.º 1
0
def submit(raw):
    try:
        click.echo(submit_transaction_raw(transaction_raw=raw)["transaction_id"])
    except UnicodeDecodeError:
        click.echo(click.style("Error: {}")
                   .format("invalid Bytom signed transaction raw"), err=True)
        sys.exit()
    except Exception as exception:
        click.echo(click.style("Error: {}")
                   .format(str(exception)), err=True)
        sys.exit()
Ejemplo n.º 2
0
def decode(raw):
    try:
        click.echo(json.dumps(decode_transaction_raw(tx_raw=raw), indent=4))
    except UnicodeDecodeError:
        click.echo(
            click.style("Error: {}").format("invalid bytom transaction raw"),
            err=True)
        sys.exit()
    except Exception as exception:
        click.echo(click.style("Error: {}").format(str(exception)), err=True)
        sys.exit()
Ejemplo n.º 3
0
def htlc(secret_hash, recipient_address, sender_address, sequence, network):
    try:
        click.echo(
            HTLC(network=network).init(secret_hash=secret_hash,
                                       recipient_address=recipient_address,
                                       sender_address=sender_address,
                                       sequence=sequence).bytecode())
    except Exception as exception:
        click.echo(click.style("Error: {}").format(str(exception)), err=True)
        sys.exit()
Ejemplo n.º 4
0
def claim(transaction, recipient_guid, amount, asset, network):
    try:
        click.echo(
            ClaimTransaction(network=network).build_transaction(
                transaction_id=transaction,
                wallet=Wallet(network=network).from_guid(guid=recipient_guid),
                amount=int(amount),
                asset=asset).unsigned_raw())
    except Exception as exception:
        click.echo(click.style("Error: {}").format(str(exception)), err=True)
        sys.exit()
Ejemplo n.º 5
0
def fund(sender_guid, amount, asset, bytecode, network):
    try:
        click.echo(
            FundTransaction(network=network).build_transaction(
                wallet=Wallet(network=network).from_guid(guid=sender_guid),
                htlc=HTLC(network=network).from_bytecode(bytecode=bytecode),
                amount=int(amount),
                asset=asset).unsigned_raw())
    except Exception as exception:
        click.echo(click.style("Error: {}").format(str(exception)), err=True)
        sys.exit()
Ejemplo n.º 6
0
def refund(transaction, sender_guid, sender_public, amount, asset, network):
    try:
        click.echo(
            RefundTransaction(network=network).build_transaction(
                transaction_id=transaction,
                wallet=Wallet(network=network).from_public_key(
                    public=sender_public).from_guid(guid=sender_guid),
                amount=int(amount),
                asset=asset).unsigned_raw())
    except Exception as exception:
        click.echo(click.style("Error: {}").format(str(exception)), err=True)
        sys.exit()
Ejemplo n.º 7
0
def refund(transaction, sender_address, amount, version, network):
    try:
        click.echo(
            RefundTransaction(version=version,
                              network=network).build_transaction(
                                  transaction_id=transaction,
                                  wallet=Wallet(network=network).from_address(
                                      address=sender_address),
                                  amount=int(amount)).unsigned_raw())
    except Exception as exception:
        click.echo(click.style("Error: {}").format(str(exception)), err=True)
        sys.exit()
Ejemplo n.º 8
0
def fund(sender_address, amount, bytecode, version, network):
    try:
        click.echo(
            FundTransaction(version=version,
                            network=network).build_transaction(
                                wallet=Wallet(network=network).from_address(
                                    address=sender_address),
                                htlc=HTLC(network=network).from_bytecode(
                                    bytecode=bytecode),
                                amount=int(amount)).unsigned_raw())
    except Exception as exception:
        click.echo(click.style("Error: {}").format(str(exception)), err=True)
        sys.exit()
Ejemplo n.º 9
0
def sign(xprivate, raw, account, change, address, bytecode, secret, path,
         indexes):
    if len(xprivate) != 128:
        click.echo(
            click.style("Error: {}").format("invalid Bytom xprivate key"),
            err=True)
        sys.exit()

    # Cleaning unsigned raw
    unsigned_raw = str(raw + "=" * (-len(raw) % 4))
    try:
        transaction = json.loads(b64decode(unsigned_raw.encode()).decode())
    except (binascii.Error, json.decoder.JSONDecodeError) as exception:
        click.echo(click.style("Error: {}").format(
            "invalid Bytom unsigned transaction raw"),
                   err=True)
        sys.exit()
    if "type" not in transaction or "network" not in transaction:
        click.echo(click.style("Warning: {}", fg="yellow").format(
            "there is no type & network provided in Bytom unsigned transaction raw"
        ),
                   err=True)
        click.echo(click.style("Error: {}").format(
            "invalid Bytom unsigned transaction raw"),
                   err=True)
        sys.exit()

    try:
        if transaction["type"] == "bytom_fund_unsigned":
            # Fund HTLC solver
            fund_solver = FundSolver(xprivate_key=xprivate,
                                     account=account,
                                     change=change,
                                     address=address,
                                     path=path,
                                     indexes=indexes)
            # Fund signature
            fund_signature = FundSignature(network=transaction["network"])
            fund_signature.sign(unsigned_raw=unsigned_raw, solver=fund_solver)
            click.echo(fund_signature.signed_raw())

        elif transaction["type"] == "bytom_claim_unsigned":
            if secret is None:
                click.echo(click.style("Error: {}").format(
                    "secret key is required for claim, use -s or --secret \"Hello Meheret!\""
                ),
                           err=True)
                sys.exit()
            if bytecode is None:
                click.echo(click.style("Error: {}").format(
                    "witness bytecode is required for claim, use -b or --bytecode \"016...\""
                ),
                           err=True)
                sys.exit()
            # Claim HTLC solver
            claim_solver = ClaimSolver(xprivate_key=xprivate,
                                       account=account,
                                       change=change,
                                       address=address,
                                       path=path,
                                       indexes=indexes,
                                       secret=secret,
                                       bytecode=bytecode)
            # Claim signature
            claim_signature = ClaimSignature(network=transaction["network"])
            claim_signature.sign(unsigned_raw=unsigned_raw,
                                 solver=claim_solver)
            click.echo(claim_signature.signed_raw())

        elif transaction["type"] == "bytom_refund_unsigned":
            if bytecode is None:
                click.echo(click.style("Error: {}").format(
                    "witness bytecode is required for refund, use -b or --bytecode \"016...\""
                ),
                           err=True)
                sys.exit()
            # Refunding HTLC solver
            refund_solver = RefundSolver(xprivate_key=xprivate,
                                         account=account,
                                         change=change,
                                         address=address,
                                         path=path,
                                         indexes=indexes,
                                         bytecode=bytecode)
            # Refund signature
            refund_signature = RefundSignature(network=transaction["network"])
            refund_signature.sign(unsigned_raw=unsigned_raw,
                                  solver=refund_solver)
            click.echo(refund_signature.signed_raw())
        else:
            click.echo(click.style("Error: {}").format(
                "unknown Bytom unsigned transaction raw type"),
                       err=True)
            sys.exit()
    except Exception as exception:
        click.echo(click.style("Error: {}").format(str(exception)), err=True)
        sys.exit()
Ejemplo n.º 10
0
def sign(private, raw, secret, version):
    if secret is None:
        secret = str()
    if len(private) != 64:
        click.echo(click.style("Error: {}")
                   .format("invalid bitcoin private key"), err=True)
        sys.exit()

    unsigned_raw = str(raw + "=" * (-len(raw) % 4))
    try:
        transaction = json.loads(b64decode(unsigned_raw.encode()).decode())
    except (binascii.Error, json.decoder.JSONDecodeError) as exception:
        click.echo(click.style("Error: {}")
                   .format("invalid bitcoin unsigned transaction raw"), err=True)
        sys.exit()
    if "type" not in transaction or "network" not in transaction:
        click.echo(click.style("Warning: {}", fg="yellow")
                   .format("there is no type & network provided in bitcoin unsigned transaction raw"), err=True)
        click.echo(click.style("Error: {}")
                   .format("invalid bitcoin unsigned transaction raw"), err=True)
        sys.exit()

    if transaction["type"] == "bitcoin_fund_unsigned":
        # Fund HTLC solver
        fund_solver = FundSolver(private_key=private)
        try:
            # Fund signature
            fund_signature = FundSignature(network=transaction["network"], version=version)
            fund_signature.sign(unsigned_raw=unsigned_raw, solver=fund_solver)
            click.echo(fund_signature.signed_raw())
        except Exception as exception:
            click.echo(click.style("Error: {}").format(str(exception)), err=True)
            sys.exit()

    elif transaction["type"] == "bitcoin_claim_unsigned":
        if secret != str():
            _secret = secret
        elif "secret" not in transaction or transaction["secret"] is None:
            click.echo(click.style("Warning: {}")
                       .format("secret key is empty, use -s or --secret \"Hello Meheret!\""), err=False)
            _secret = str()
        else:
            _secret = transaction["secret"]
        # Claim HTLC solver
        claim_solver = ClaimSolver(
            secret=_secret,
            private_key=private
        )
        try:
            # Claim signature
            claim_signature = ClaimSignature(network=transaction["network"], version=version)
            claim_signature.sign(unsigned_raw=unsigned_raw, solver=claim_solver)
            click.echo(claim_signature.signed_raw())
        except Exception as exception:
            click.echo(click.style("Error: {}").format(str(exception)), err=True)
            sys.exit()

    elif transaction["type"] == "bitcoin_refund_unsigned":
        if secret != str():
            _secret = secret
        elif "secret" not in transaction or transaction["secret"] is None:
            click.echo(click.style("Warning: {}")
                       .format("secret key is empty, use -s or --secret \"Hello Meheret!\""), err=False)
            _secret = str()
        else:
            _secret = transaction["secret"]
        # Refunding HTLC solver
        refund_solver = RefundSolver(
            secret=_secret,
            private_key=private
        )
        try:
            # Refund signature
            refund_signature = RefundSignature(network=transaction["network"], version=version)
            refund_signature.sign(unsigned_raw=unsigned_raw, solver=refund_solver)
            click.echo(refund_signature.signed_raw())
        except Exception as exception:
            click.echo(click.style("Error: {}").format(str(exception)), err=True)
            sys.exit()