From 034bd527ac155b81f3fc82f143728bb23f3d6e87 Mon Sep 17 00:00:00 2001 From: Daniel Suo Date: Mon, 15 Dec 2014 14:48:10 -0500 Subject: [PATCH] add tx_in to bytearray --- src/messages.jl | 30 +++++++++++++++++++----------- test/runtests.jl | 18 +++++++++++------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/messages.jl b/src/messages.jl index 49c75b1..bd0ab24 100644 --- a/src/messages.jl +++ b/src/messages.jl @@ -91,14 +91,14 @@ type Message end type OutPoint - index::Uint32 # Index of specific output in tx. 1st output is 0 hash::Array{Uint8} # 32-byte hash of the referenced transaction + index::Uint32 # Index of specific output in tx. 1st output is 0 - function OutPoint(index::Integer, hash::String) - OutPoint(uint32(index), hex_string_to_array(hash)) + function OutPoint(hash::String, index::Integer) + OutPoint(hex_string_to_array(hash), uint32(index)) end - function OutPoint(index::Integer, hash::Array{Uint8}) + function OutPoint(hash::Array{Uint8}, index::Integer) if index < 0 error("OutPoint cannot have negative index") end @@ -107,15 +107,15 @@ type OutPoint error("OutPoint requres 32-byte hash of referenced transaction") end - new(uint32(index), hash) + new(hash, uint32(index)) end end function convert(::Type{Array{Uint8}}, outpoint::OutPoint) result = Array(Uint8, 0) - append!(result, reverse(bytearray(outpoint.index))) append!(result, reverse(outpoint.hash)) + append!(result, reverse(bytearray(outpoint.index))) return result end @@ -126,16 +126,24 @@ type Tx_Input scriptSig::Array{Uint8} # Script to confirm tx authorization sequence::Uint32 # Tx version as defined by the sender - function Tx_Input(previous_output::OutPoint, scriptSig::String; sequence = 0) + function Tx_Input(previous_output::OutPoint, scriptSig::String; sequence = 0xffffffff) scriptSig = hex_string_to_array(scriptSig) - Tx_Input(previous_output, scriptSig, sequence) + Tx_Input(previous_output, scriptSig, sequence = sequence) end - function Tx_Input(previous_output::OutPoint, scriptSig::Array{Uint8}; sequence = 0) + + function Tx_Input(previous_output::OutPoint, scriptSig::Array{Uint8}; sequence = 0xffffffff) scriptSig_length = length(scriptSig) - sequence = uint32(sequence) - new(previous_output, scriptSig_length, scriptSig) + new(previous_output, scriptSig_length, scriptSig, uint32(sequence)) end +end + +function convert(::Type{Array{Uint8}}, tx_in::Tx_Input) + result = Array(Uint8, 0) + append!(result, bytearray(tx_in.previous_output)) + append!(result, reverse(to_varint(tx_in.scriptSig_length))) + append!(result, tx_in.scriptSig) + append!(result, reverse(bytearray(tx_in.sequence))) end type Tx_Output diff --git a/test/runtests.jl b/test/runtests.jl index 4426b05..a3f438d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -68,12 +68,17 @@ tx_out = Tx_Output(5000000, "76A9141AA0CD1CBEA6E7458A7ABAD512A9D9EA1AFB225E88AC" @test bytearray(tx_out) == [0x40, 0x4b, 0x4c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0x1a, 0xa0, 0xcd, 0x1c, 0xbe, 0xa6, 0xe7, 0x45, 0x8a, 0x7a, 0xba, 0xd5, 0x12, 0xa9, 0xd9, 0xea, 0x1a, 0xfb, 0x22, 0x5e, 0x88, 0xac] # Transaction outpoint (i.e., reference to previous transaction) -@test_throws ErrorException OutPoint(1, "abc") -outpoint = OutPoint(1, "abcaabcaabcaabcaabcaabcaabcaabcaabcaabcaabcaabcaabcaabcaabcaabca") -@test bytearray(outpoint) == [0x01, 0x00, 0x00, 0x00, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab] -outpoint = OutPoint(0, "81b4c832d70cb56ff957589752eb4125a4cab78a25a8fc52d6a09e5bd4404d48") -@test bytearray(outpoint) == [0x00, 0x00, 0x00, 0x00, 0x48, 0x4d, 0x40, 0xd4, 0x5b, 0x9e, 0xa0, 0xd6, 0x52, 0xfc, 0xa8, 0x25, 0x8a, 0xb7, 0xca, 0xa4, 0x25, 0x41, 0xeb, 0x52, 0x97, 0x58, 0x57, 0xf9, 0x6f, 0xb5, 0x0c, 0xd7, 0x32, 0xc8, 0xb4, 0x81] - +@test_throws ErrorException OutPoint("abc", 1) +outpoint = OutPoint("abcaabcaabcaabcaabcaabcaabcaabcaabcaabcaabcaabcaabcaabcaabcaabca", 1) +@test bytearray(outpoint) == [0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0x01, 0x00, 0x00, 0x00] +outpoint = OutPoint("81b4c832d70cb56ff957589752eb4125a4cab78a25a8fc52d6a09e5bd4404d48", 0) +@test bytearray(outpoint) == [0x48, 0x4d, 0x40, 0xd4, 0x5b, 0x9e, 0xa0, 0xd6, 0x52, 0xfc, 0xa8, 0x25, 0x8a, 0xb7, 0xca, 0xa4, 0x25, 0x41, 0xeb, 0x52, 0x97, 0x58, 0x57, 0xf9, 0x6f, 0xb5, 0x0c, 0xd7, 0x32, 0xc8, 0xb4, 0x81, 0x00, 0x00, 0x00, 0x00] + +# Transaction input +outpoint = OutPoint("81b4c832d70cb56ff957589752eb4125a4cab78a25a8fc52d6a09e5bd4404d48", 0) +scriptSig = "47304402202cb265bf10707bf49346c3515dd3d16fc454618c58ec0a0ff448a676c54ff71302206c6624d762a1fcef4618284ead8f08678ac05b13c84235f1654e6ad168233e8201410414e301b2328f17442c0b8310d787bf3d8a404cfbd0704f135b6ad4b2d3ee751310f981926e53a6e8c39bd7d3fefd576c543cce493cbac06388f2651d1aacbfcd" +tx_in = Tx_Input(outpoint, scriptSig) +@test bytearray(tx_in) == [0x48, 0x4d, 0x40, 0xd4, 0x5b, 0x9e, 0xa0, 0xd6, 0x52, 0xfc, 0xa8, 0x25, 0x8a, 0xb7, 0xca, 0xa4, 0x25, 0x41, 0xeb, 0x52, 0x97, 0x58, 0x57, 0xf9, 0x6f, 0xb5, 0x0c, 0xd7, 0x32, 0xc8, 0xb4, 0x81, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x47, 0x30, 0x44, 0x02, 0x20, 0x2c, 0xb2, 0x65, 0xbf, 0x10, 0x70, 0x7b, 0xf4, 0x93, 0x46, 0xc3, 0x51, 0x5d, 0xd3, 0xd1, 0x6f, 0xc4, 0x54, 0x61, 0x8c, 0x58, 0xec, 0x0a, 0x0f, 0xf4, 0x48, 0xa6, 0x76, 0xc5, 0x4f, 0xf7, 0x13, 0x02, 0x20, 0x6c, 0x66, 0x24, 0xd7, 0x62, 0xa1, 0xfc, 0xef, 0x46, 0x18, 0x28, 0x4e, 0xad, 0x8f, 0x08, 0x67, 0x8a, 0xc0, 0x5b, 0x13, 0xc8, 0x42, 0x35, 0xf1, 0x65, 0x4e, 0x6a, 0xd1, 0x68, 0x23, 0x3e, 0x82, 0x01, 0x41, 0x04, 0x14, 0xe3, 0x01, 0xb2, 0x32, 0x8f, 0x17, 0x44, 0x2c, 0x0b, 0x83, 0x10, 0xd7, 0x87, 0xbf, 0x3d, 0x8a, 0x40, 0x4c, 0xfb, 0xd0, 0x70, 0x4f, 0x13, 0x5b, 0x6a, 0xd4, 0xb2, 0xd3, 0xee, 0x75, 0x13, 0x10, 0xf9, 0x81, 0x92, 0x6e, 0x53, 0xa6, 0xe8, 0xc3, 0x9b, 0xd7, 0xd3, 0xfe, 0xfd, 0x57, 0x6c, 0x54, 0x3c, 0xce, 0x49, 0x3c, 0xba, 0xc0, 0x63, 0x88, 0xf2, 0x65, 0x1d, 0x1a, 0xac, 0xbf, 0xcd, 0xff, 0xff, 0xff, 0xff] ############################################################################## ## @@ -108,4 +113,3 @@ outpoint = OutPoint(0, "81b4c832d70cb56ff957589752eb4125a4cab78a25a8fc52d6a09e5b @test to_varint(0xffff) == [0xfd, 0xff, 0xff] @test to_varint(0x10000) == [0xfe, 0x00, 0x01, 0x00, 0x00] @test to_varint(0x100000000) == [0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00] -