Skip to content

Commit

Permalink
fix order of types, fix reverse_endian bug, add hex_string_to_array
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsuo committed Dec 15, 2014
1 parent 927faf8 commit e234985
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 26 deletions.
3 changes: 2 additions & 1 deletion src/Coin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export

# utils.jl
reverse_endian,
get_checksum
get_checksum,
hex_string_to_array

export magic_mainnet,
magic_testnet,
Expand Down
38 changes: 22 additions & 16 deletions src/messages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,15 @@ type Message
checksum = uint32(parseint(get_checksum(payload, is_hex=true)[1:8], 16))

# Turn payload hex string into array of bytes
payload = [uint8(parseint(payload[2i-1:2i], 16)) for i in length(payload) / 2]
payload = hex_string_to_array(payload)

new(magic, command.data, length(payload), checksum, payload)
end
end

type Tx
version::Uint32 # Transaction data format version
num_inputs::Int # Variable length integer: number of input tx
inputs::Array{Tx_Input} # Array of transaction inputs
num_outputs::Int # Variable length integer: number of output tx
outputs::Array{Tx_Output} # Array of transaction outputs
lock_time::Uint32 # Block num / time when tx is locked
type OutPoint
hash::Array{Uint8} # 32-byte hash of the referenced transaction
index::Uint32 # Index of specific output in tx. 1st output is 0
end

type Tx_Input
Expand All @@ -103,19 +99,29 @@ type Tx_Input
sequence::Uint32 # Transaction version as defined by the sender
end

type OutPoint
hash::Array{Uint8} # 32-byte hash of the referenced transaction
index::Uint32 # Index of specific output in tx. 1st output is 0
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{Int8} # Script to set up cond'ns to claim tx output
scriptPubKey::Array{Uint8} # Script to set up cond'ns to claim 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)
new(value, scriptPubKey_length, scriptPubKey)
end
end

## value: transaction value in Satoshi
# function Tx_Outputs(value)
type Tx
version::Uint32 # Transaction data format version
num_inputs::Int # Variable length integer: number of input tx
inputs::Array{Tx_Input} # Array of transaction inputs
num_outputs::Int # Variable length integer: number of output tx
outputs::Array{Tx_Output} # Array of transaction outputs
lock_time::Uint32 # Block num / time when tx is locked
end

# Define the known magic values
Expand Down
28 changes: 19 additions & 9 deletions src/utils.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
function reverse_endian(hex_string::String)
hex_length = length(hex_string)

# Left pad with 0 to make hex_string even length
if hex_length % 2 != 0
hex_string = string("0", hex_string)
hex_length += 1
end

return join(reverse([hex_string[2i-1:2i] for i in 1:hex_length/2]))
return join(reverse(hex_string_to_array(hex_string)))
end

function reverse_endian(hex_data::Number)
Expand All @@ -19,10 +11,28 @@ function reverse_endian(hex_data::Number)
hex_data >>>= 8
end

bytes = sizeof(data_type)

result <<= int(floor(bytes - log(2, result) / 8)) * 8

return result
end

function get_checksum(message::String; is_hex = false)
create_digest(x) = Crypto.digest("SHA256", x, is_hex = is_hex)
return create_digest(create_digest(message))[1:8]
end

function hex_string_to_array(hex_string::String)
hex_length = length(hex_string)

# Left pad with 0 to make hex_string even length
if hex_length % 2 != 0
hex_string = string("0", hex_string)
hex_length += 1
end

hex_length = div(hex_length, 2)

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

0 comments on commit e234985

Please sign in to comment.