Skip to content

Commit

Permalink
Merge branch 'master' of gitlab.com:braneproject/Bitcoin.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
roshii committed Mar 2, 2019
2 parents a5009d8 + 015d97e commit df4fe8d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ license = "MIT"
authors = ["Simon Castano <[email protected]>"]
description = "Bitcoin Library"
repository = "https://gitlab.com/braneproject/Bitcoin.jl.git"
version = "0.1.2"
version = "0.1.3"

[deps]
Base58 = "a832ea1e-0ca7-55ca-a41a-6462721bb861"
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ https://braneproject.gitlab.io/Bitcoin.jl/

## Buy me a cup of coffee

[Donate Bitcoin](bitcoin:1786ytdyKz1TJgpVM34DKDB85eEQkvwgjo)
[Donate Litecoin](litecoin:LQKx7ZSspht4UZ5b5S7UVWeDW1tHZY5xnt)
[Donate Bitcoin](bitcoin:34nvxratCQcQgtbwxMJfkmmxwrxtShTn67)
42 changes: 28 additions & 14 deletions src/tx.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ mutable struct TxIn <: TxComponent
end

function show(io::IO, z::TxIn)
print(io, "\n", bytes2hex(z.prev_tx), ":", z.prev_index)
print(io, "\n", bytes2hex(z.prev_tx), ":", z.prev_index, "\n", z.script_sig)
end

"""
Expand Down Expand Up @@ -243,12 +243,11 @@ function txfee(tx::Tx)
end

"""
txsighash(tx::Tx, input_index::Integer) -> Integer
txsighash256(tx::Tx, input_index::Integer) -> Array{UInt8,1}
Returns the integer representation of the hash that needs to get
signed for index input_index
Returns the hash that needs to get signed for index input_index
"""
function txsighash(tx::Tx, input_index::Integer)
function txsighash256(tx::Tx, input_index::Integer)
alt_tx_ins = TxIn[]
for tx_in in tx.tx_ins
alt_tx_in = TxIn(tx_in.prev_tx, tx_in.prev_index, Script(nothing), tx_in.sequence)
Expand All @@ -265,8 +264,17 @@ function txsighash(tx::Tx, input_index::Integer)
result = UInt8[]
append!(result, txserialize(alt_tx))
append!(result, int2bytes(SIGHASH_ALL, 4, true))
h256 = hash256(result)
return bytes2int(h256)
return hash256(result)
end

"""
txsighash(tx::Tx, input_index::Integer) -> Integer
Returns the integer representation of the hash that needs to get
signed for index input_index
"""
function txsighash(tx::Tx, input_index::Integer)
return bytes2int(txsighash256(tx, input_index))
end

"""
Expand All @@ -275,8 +283,9 @@ Returns whether the input has a valid signature
function txinputverify(tx::Tx, input_index)
tx_in = tx.tx_ins[input_index + 1]
z = txsighash(tx, input_index)
combined_script = tx_in.script_sig
append!(combined_script.instructions, txin_scriptpubkey(tx_in, tx.testnet).instructions)
combined_script = Script(copy(tx_in.script_sig.instructions))
append!(combined_script.instructions,
txin_scriptpubkey(tx_in, tx.testnet).instructions)
return scriptevaluate(combined_script, z)
end

Expand All @@ -299,12 +308,17 @@ end
"""
Signs the input using the private key
"""
function txsigninput(tx::Tx, input_index, private_key)
function txsigninput(tx::Tx, input_index::Integer, private_key::PrivateKey)
z = txsighash(tx, input_index)
sig = sig2der(pksign(private_key, z))
append!(sig, int2bytes(SIGHASH_ALL))
sec = point2sec(private_key.𝑃)
script_sig = Script([sig, sec])
sig = pksign(private_key, z)
txpushsignature(tx, input_index, z, sig, private_key.𝑃)
end

function txpushsignature(tx::Tx, input_index::Integer, z::Integer, sig::Signature, pubkey::S256Point)
der = sig2der(sig)
append!(der, int2bytes(SIGHASH_ALL))
sec = point2sec(pubkey)
script_sig = Script([der, sec])
tx.tx_ins[input_index + 1].script_sig = script_sig
return txinputverify(tx, input_index)
end
6 changes: 3 additions & 3 deletions test/txtest.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Base58: base58decode
using Base58: base58checkdecode

@testset "Transaction" begin
raw_tx = hex2bytes("0100000001813f79011acb80925dfe69b3def355fe914bd1d96a3f5f71bf8303c6a989c7d1000000006b483045022100ed81ff192e75a3fd2304004dcadb746fa5e24c5031ccfcf21320b0277457c98f02207a986d955c6e0cb35d446a89d3f56100f4d7f67801c31967743a9c8e10615bed01210349fc4e631e3624a545de3f89f5d8684c7b8138bd94bdd531d2e213bf016b278afeffffff02a135ef01000000001976a914bc3b654dca7e56b04dca18f2566cdaf02e8d9ada88ac99c39800000000001976a9141c4bc762dd5423e332166702cb75f40df79fea1288ac19430600")
Expand Down Expand Up @@ -86,9 +86,9 @@ using Base58: base58decode
prev_tx = hex2bytes("0025bc3c0fa8b7eb55b9437fdbd016870d18e0df0ace7bc9864efc38414147c8")
push!(tx_ins, TxIn(prev_tx, 0))
tx_outs = TxOut[]
h160 = base58decode(b"mzx5YhAH9kNHtcN481u6WkjeHjYtVeKVh2")
h160 = base58checkdecode(b"mzx5YhAH9kNHtcN481u6WkjeHjYtVeKVh2")[2:end]
push!(tx_outs, TxOut(Int(0.99 * 100000000), Bitcoin.p2pkh_script(h160)))
h160 = base58decode(b"mnrVtF8DWjMu839VW3rBfgYaAfKk8983Xf")
h160 = base58checkdecode(b"mnrVtF8DWjMu839VW3rBfgYaAfKk8983Xf")[2:end]
push!(tx_outs, TxOut(Int(0.1 * 100000000), Bitcoin.p2pkh_script(h160)))
tx = Tx(1, tx_ins, tx_outs, 0, true)
@test txsigninput(tx, 0, private_key)
Expand Down

0 comments on commit df4fe8d

Please sign in to comment.