Example #1
0
async def sign_tx(ctx, msg: RippleSignTx, keychain):
    validate(msg)

    await paths.validate_path(ctx, helpers.validate_full_path, keychain,
                              msg.address_n, CURVE)

    node = keychain.derive(msg.address_n)
    source_address = helpers.address_from_public_key(node.public_key())

    set_canonical_flag(msg)
    tx = serialize(msg, source_address, pubkey=node.public_key())
    to_sign = get_network_prefix() + tx

    check_fee(msg.fee)
    if msg.payment.destination_tag is not None:
        await layout.require_confirm_destination_tag(
            ctx, msg.payment.destination_tag)
    await layout.require_confirm_fee(ctx, msg.fee)
    await layout.require_confirm_tx(ctx, msg.payment.destination,
                                    msg.payment.amount)

    signature = ecdsa_sign(node.private_key(), first_half_of_sha512(to_sign))
    tx = serialize(msg,
                   source_address,
                   pubkey=node.public_key(),
                   signature=signature)
    return RippleSignedTx(signature, tx)
Example #2
0
    def test_transactions(self):
        # from https://github.com/miracle2k/ripple-python
        source_address = "r3P9vH81KBayazSTrQj6S25jW6kDb779Gi"
        payment = RipplePayment(200000000,
                                "r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV")
        common = RippleSignTx(None, 10, None, 1, None, payment)
        self.assertEqual(
            serialize(common, source_address),
            unhexlify(
                "120000240000000161400000000bebc20068400000000000000a811450f97a072f1c4357f1ad84566a609479d927c9428314550fc62003e785dc231a1058a05e56e3f09cf4e6"
            ))

        source_address = "r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV"
        payment = RipplePayment(1, "r3P9vH81KBayazSTrQj6S25jW6kDb779Gi")
        common = RippleSignTx(None, 99, None, 99, None, payment)
        self.assertEqual(
            serialize(common, source_address),
            unhexlify(
                "12000024000000636140000000000000016840000000000000638114550fc62003e785dc231a1058a05e56e3f09cf4e6831450f97a072f1c4357f1ad84566a609479d927c942"
            ))

        # https://github.com/ripple/ripple-binary-codec/blob/4581f1b41e712f545ba08be15e188a557c731ecf/test/fixtures/data-driven-tests.json#L1579
        source_address = "r9TeThyi5xiuUUrFjtPKZiHcDxs7K9H6Rb"
        payment = RipplePayment(25000000, "r4BPgS7DHebQiU31xWELvZawwSG2fSPJ7C")
        common = RippleSignTx(None, 10, 0, 2, None, payment)
        self.assertEqual(
            serialize(common, source_address),
            unhexlify(
                "120000220000000024000000026140000000017d784068400000000000000a81145ccb151f6e9d603f394ae778acf10d3bece874f68314e851bbbe79e328e43d68f43445368133df5fba5a"
            ))

        # https://github.com/ripple/ripple-binary-codec/blob/4581f1b41e712f545ba08be15e188a557c731ecf/test/fixtures/data-driven-tests.json#L1651
        source_address = "rGWTUVmm1fB5QUjMYn8KfnyrFNgDiD9H9e"
        payment = RipplePayment(200000, "rw71Qs1UYQrSQ9hSgRohqNNQcyjCCfffkQ")
        common = RippleSignTx(None, 15, 0, 144, None, payment)
        # 201b005ee9ba removed from the test vector because last ledger sequence is not supported
        self.assertEqual(
            serialize(common, source_address),
            unhexlify(
                "12000022000000002400000090614000000000030d4068400000000000000f8114aa1bd19d9e87be8069fdbf6843653c43837c03c6831467fe6ec28e0464dd24fb2d62a492aac697cfad02"
            ))

        # https://github.com/ripple/ripple-binary-codec/blob/4581f1b41e712f545ba08be15e188a557c731ecf/test/fixtures/data-driven-tests.json#L1732
        source_address = "r4BPgS7DHebQiU31xWELvZawwSG2fSPJ7C"
        payment = RipplePayment(25000000, "rBqSFEFg2B6GBMobtxnU1eLA1zbNC9NDGM",
                                4146942154)
        common = RippleSignTx(None, 12, 0, 1, None, payment)
        self.assertEqual(
            serialize(common, source_address),
            unhexlify(
                "120000220000000024000000012ef72d50ca6140000000017d784068400000000000000c8114e851bbbe79e328e43d68f43445368133df5fba5a831476dac5e814cd4aa74142c3ab45e69a900e637aa2"
            ))
    def test_transactions_for_signing(self):
        # https://github.com/ripple/ripple-binary-codec/blob/4581f1b41e712f545ba08be15e188a557c731ecf/test/signing-data-encoding-test.js
        source_address = "r9LqNeG6qHxjeUocjvVki2XR35weJ9mZgQ"
        payment = RipplePayment(1000, "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")
        common = RippleSignTx(None, 10, 2147483648, 1, None, payment)

        tx = serialize(
            common,
            source_address,
            pubkey=unhexlify(
                "ed5f5ac8b98974a3ca843326d9b88cebd0560177b973ee0b149f782cfaa06dc66a"
            ),
        )
        tx = get_network_prefix() + tx

        assert tx[0:4] == unhexlify("53545800")  # signing prefix
        assert tx[4:7] == unhexlify("120000")  # transaction type
        assert tx[7:12] == unhexlify("2280000000")  # flags
        assert tx[12:17] == unhexlify("2400000001")  # sequence
        assert tx[17:26] == unhexlify("6140000000000003e8")  # amount
        assert tx[26:35] == unhexlify("68400000000000000a")  # fee
        assert tx[35:70] == unhexlify(
            "7321ed5f5ac8b98974a3ca843326d9b88cebd0560177b973ee0b149f782cfaa06dc66a"
        )  # singing pub key
        assert tx[70:92] == unhexlify(
            "81145b812c9d57731e27a2da8b1830195f88ef32a3b6")  # account
        assert tx[92:114] == unhexlify(
            "8314b5f762798a53d543a014caf8b297cff8f2f937e8")  # destination
        assert len(tx[114:]) == 0  # that's it