Skip to content

Commit

Permalink
add tx_output type conversion to byte array
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsuo committed Dec 15, 2014
1 parent af362b3 commit cd006ed
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ First, we're going to implement a thin-client wallet.

- Should consider creating object types (e.g., addresses with metadata; wallets; etc)
- Clean up tests to make more consistent
- Different nets (e.g., https://github.com/haskoin/haskoin/blob/master/prodnet/Network/Haskoin/Constants.hs)

### Public key distribution
- See [ref](https://github.com/danielsuo/Crypto.jl)
Expand Down
3 changes: 2 additions & 1 deletion src/Coin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export
reverse_endian,
get_checksum,
hex_string_to_array,
to_varint
to_varint,
bytearray

export magic_mainnet,
magic_testnet,
Expand Down
22 changes: 18 additions & 4 deletions src/messages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,34 @@ end

type Tx_Output
# ERROR: does Uint64 exist on 32-bit OS?
value::Uint64 # Transaction value
scriptPubKey_length::Int # Variable length integer: length of scriptPubKey
scriptPubKey::Array{Uint8} # Script to set up cond'ns to claim tx output
value::Uint64 # Transaction value
scriptPubKey_length::Array{Uint8} # Variable length int: scriptPubKey len
scriptPubKey::Array{Uint8} # Script for claiming tx output

# value: transaction value in Satoshi
# scriptPubKey: script as hex string
function Tx_Output(value, scriptPubKey::String)
value = uint64(value)
scriptPubKey = hex_string_to_array(scriptPubKey)
scriptPubKey_length = length(scriptPubKey)
scriptPubKey_length = to_varint(length(scriptPubKey))
new(value, scriptPubKey_length, scriptPubKey)
end
end

function convert(::Type{Array{Uint8}}, tx_out::Tx_Output)
result = Array(Uint8, 0)

# TODO: This is a really terrible way to get little
# endian byte array
append!(result, reverse(convert(Array{Uint8}, tx_out.value)))

append!(result, reverse(tx_out.scriptPubKey_length))

append!(result, tx_out.scriptPubKey)

return result
end

type Tx
version::Uint32 # Transaction data format version
num_inputs::Int # Variable length integer: number of input tx
Expand Down
9 changes: 9 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import Base.convert
#
# Only providing convenience functions to and from Array{Uint8} because varint
# seems like its primarily a transport structure
#
# VarInt are created as big-endian here, but are sent little-endian as per
# https://bitcoin.org/en/developer-reference#compactsize-unsigned-integers
function to_varint(x::Integer)
result = Array(Uint8, 0)

Expand Down Expand Up @@ -73,4 +76,10 @@ function hex_string_to_array(hex_string::String)
hex_length = div(hex_length, 2)

return [uint8(parseint(hex_string[2i-1:2i], 16)) for i in 1:hex_length]
end

# Convenience function for converting to Array{Uint8}
# Must be defined for given types
function bytearray(x)
convert(Array{Uint8}, x)
end
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ public_key = Coin.get_public_key(secret_key)
payload = "0100000001484d40d45b9ea0d652fca8258ab7caa42541eb52975857f96fb50cd732c8b481000000008a47304402202cb265bf10707bf49346c3515dd3d16fc454618c58ec0a0ff448a676c54ff71302206c6624d762a1fcef4618284ead8f08678ac05b13c84235f1654e6ad168233e8201410414e301b2328f17442c0b8310d787bf3d8a404cfbd0704f135b6ad4b2d3ee751310f981926e53a6e8c39bd7d3fefd576c543cce493cbac06388f2651d1aacbfcdffffffff0162640100000000001976a914c8e90996c7c6080ee06284600c684ed904d14c5c88ac00000000"
# @test Coin.create_header(magic_mainnet, "tx", payload) == "f9beb4d9747800000000000000000000df000000ea0f5494"

# Transaction output
tx_out = Coin.Tx_Output(123, "abc")
@test bytearray(tx_out) == Uint8[123,0,0,0,0,0,0,0,2,10,188]
tx_out = Coin.Tx_Output(5000000, "76A9141AA0CD1CBEA6E7458A7ABAD512A9D9EA1AFB225E88AC")
@test bytearray(tx_out) == Uint8[64,75,76,0,0,0,0,0,25,118,169,20,26,160,205,28,190,166,231,69,138,122,186,213,18,169,217,234,26,251,34,94,136,172]

##############################################################################
##
## Utility tests
Expand Down

0 comments on commit cd006ed

Please sign in to comment.