Skip to content

Commit

Permalink
create version and networkaddress objects
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsuo committed Dec 19, 2014
1 parent bc2ca6e commit 7eaf315
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 15 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ A (self-educational, incomplete, and likely incorrect) library for working with

# To Do
- https://en.bitcoin.it/wiki/Technical_background_of_Bitcoin_addresses
# TODO: multiple inputs
# TODO: convenience functions
# TODO: clean up and documentation
- TODO: multiple inputs
- TODO: convenience functions
- TODO: clean up and documentation
- TODO: update versioning info automatically (e.g., useragent, changelog, etc)

## Wallet
First, we're going to implement a thin-client wallet.
Expand Down
39 changes: 29 additions & 10 deletions src/Coin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,38 @@ export
decode58,
decode58_to_array,

# utils.jl
reverse_endian,
get_checksum,
to_varint,
to_varstring,
bytearray

export BITCOIN_PROTOCOL_VERSION,
SERVICES_NODE_NETWORK

##############################################################################
##
## Message exports
##
##############################################################################

export
# messages.jl,
create_header,
Message,
create_header,

# tx.jl
Tx,
Tx_Input,
OutPoint,
Tx_Output,

# tx.jl
create_tx,
get_tx,

# utils.jl
reverse_endian,
get_checksum,
to_varint,
bytearray
# version.jl
NetworkAddress,
Version

export magic_mainnet,
magic_testnet,
Expand All @@ -58,12 +73,16 @@ include("utils.jl")
include("base58.jl")
include("keys.jl")
include("addresses.jl")
include("messages.jl")
include("messages/messages.jl")
include("signatures.jl")
include("op.jl")
include("tx.jl")
include("server.jl")

Crypto.init()

# Version 0.9.3
const BITCOIN_PROTOCOL_VERSION = 93000

const SERVICES_NODE_NETWORK = 1

end # module Coin
3 changes: 3 additions & 0 deletions src/messages.jl → src/messages/messages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ import Base.convert
# "ping", "pong", "reject", "filterload", "filteradd",
# "filterclear", "merkleblock", "alert"]

include("tx.jl")
include("version.jl")

# Define the known magic values
const magic_mainnet = 0xd9b4bef9
const magic_testnet = 0xdab5bffa
Expand Down
File renamed without changes.
74 changes: 74 additions & 0 deletions src/messages/version.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
type NetworkAddress
time::Uint32
services::Uint64
IP::Array{Uint8}
port::Uint16

# Assume addresses come in the form IP:Port
function NetworkAddress(address::String; services = SERVICES_NODE_NETWORK

# TODO: more rigorous IP address checking
pieces = split(address, ":")
ip = pieces[1:end-1]
port = pieces[end]

# If we have an ipv6 address (e.g., xxxx:...{6}...:xxxx)
if length(ip) > 1
ip = reduce(vcat, [Crypto.int2oct(uint16(parseint(x, 16))) for x in ip])
else
ip = split(ip, ".")
ip = reduce(vcat, [Crypto.int2oct(uint8(x)) for x in ip])

pad = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff]
ip = [pad, ip]
end

new(uint64(time()), uint64(services), ip, uint16(port))
end
end

function convert(::Type{Array{Uint8}}, x::NetworkAddress)
result = Array(Uint8, 0)

append!(result, reverse(Crypto.int2oct(x.services)))
append!(result, x.IP)
append!(result, Crypto.int2oct(x.port))

return result
end

type Version
version::Uint32
services::Uint64
timestamp::Uint64
addr_recv::NetworkAddress
addr_from::NetworkAddress
nonce::Array{Uint8}
user_agent::Array{Uint8}
start_height::Uint32
relay::Bool

# Take address in the form
function Version(addr_recv::String, addr_from::String;
version = BITCOIN_PROTOCOL_VERSION,
services = SERVICES_NODE_NETWORK,
timestamp = int(time()),
nonce = Crypto.random(64),
user_agent = "/Coin.jl:0.0.1/",
start_height = 0,
relay = true)

addr_recv = NetworkAddress(addr_recv, services = services)
addr_from = NetworkAddress(addr_from, services = services)

new(version = uint32(version),
services = uint64(services),
timestamp = uint64(timestamp),
addr_recv = addr_recv,
addr_from = addr_from,
nonce = nonce,
user_agent = user_agent,
start_height = uint32(start_height),
relay = relay)
end
end
5 changes: 5 additions & 0 deletions src/server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

Crypto.init()

const LISTEN_PORT = 18333 # 8333 for mainnet
const RPC_PORT = 18332 # 8332 for mainnet

priv_key = "7e68e472a6b41f165c0d13b57b6ac88440367c25361b5d6d407bb5fe2cd05a12"
# moZRBjoq3ELstSp6CPeBXir5FSpjjHyQMp
pub_key = get_pub_key(priv_key, network_id = 0x6f)

connect(ip"107.170.32.58", 18333)
6 changes: 6 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ function to_varint(x::Integer)
return result
end

# Bitcoin's variable length string
# https://en.bitcoin.it/wiki/Protocol_specification#Variable_length_string
function to_varstring(x::String)
return [to_varint(length(x)), x.data]
end

function reverse_endian(hex_string::String)
return join([hex(x, 2) for x in reverse(hex2oct(hex_string))])
end
Expand Down
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
# - Add negative WIF tests (e.g., invalid compression bits)
# - Test different key types (e.g., compressed)
# - Add negative key tests
# - add test for leading 0
# - to_varstring

using Coin
using Base.Test
Expand All @@ -26,8 +28,6 @@ base58data = parseint(BigInt, "800c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471
# Base 58 decoding
@test decode58("5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ") == base58data

# TODO: add test for leading 0

##############################################################################
##
## Key generation tests
Expand Down

0 comments on commit 7eaf315

Please sign in to comment.