Skip to content

Commit

Permalink
fix linking to custom scripts
Browse files Browse the repository at this point in the history
use routes like /script/<script_sig>/<pk_script>/<sig_hash> to avoid
caching issues (caching doesn't take ?parameters into account)
  • Loading branch information
mhanne committed Nov 26, 2015
1 parent 0ed28ec commit 7c98d55
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -23,6 +23,7 @@ public/stats.json
public/node_*.png
public/node_*.html
public/graphs
public/script/*

coverage/
doc/
Expand Down
21 changes: 13 additions & 8 deletions app/controllers/blocks_controller.rb
Expand Up @@ -3,6 +3,8 @@

class BlocksController < ApplicationController

include Bitcoin

respond_to :html, :json, :bin, :hex

around_filter :timeout
Expand Down Expand Up @@ -99,18 +101,21 @@ def script
@script_sig = @txin.script_sig
@pk_script = @txout.pk_script
@sig_hash = nil
else # when no tx is given, try to get script directly from parameters
@script_sig = Bitcoin::Script.from_string(params[:script_sig]).raw if params[:script_sig]
@pk_script = Bitcoin::Script.from_string(params[:pk_script]).raw if params[:script_sig]
elsif params[:script_sig] && params[:pk_script] # when no tx is given, try to get script directly from parameters
@script_sig = Script.from_string(params[:script_sig]).raw
@pk_script = Script.from_string(params[:pk_script]).raw
@sig_hash = params[:sig_hash].htb if params[:sig_hash] && !params[:sig_hash].blank?
else
@script_sig = Script.from_string("1 2").raw
@pk_script = Script.from_string("OP_ADD 3 OP_EQUAL").raw
end

# if there is a script, execute it
if @script_sig && @pk_script
@script = Bitcoin::Script.new(@script_sig, @pk_script)
@script = Script.new(@script_sig, @pk_script)

if @script.is_script_hash?
@inner_script = Bitcoin::Script.new(@script.inner_p2sh_script)
@inner_script = Script.new(@script.inner_p2sh_script)
end

if @options[:verify_sigpushonly] && !@script.is_push_only?(@script_sig)
Expand Down Expand Up @@ -169,7 +174,7 @@ def search
elsif @id.to_i.to_s == @id
block = STORE.block_at_height(@id.to_i)
return redirect_to(block_path(block.hash)) if block
elsif STORE.is_a?(Bitcoin::Blockchain::Backends::SequelBase)
elsif STORE.is_a?(Blockchain::Backends::SequelBase)
return if search_block(@id)
return if search_tx(@id)
return if search_name(@id)
Expand Down Expand Up @@ -199,9 +204,9 @@ def relay_tx
if request.post? && @input = params[:tx]
begin
if @input =~ /^[0-9a-f]+$/i
@tx = Bitcoin::P::Tx.new(@input.htb)
@tx = P::Tx.new(@input.htb)
elsif !!JSON.parse(@input)
@tx = Bitcoin::P::Tx.from_json(@input)
@tx = P::Tx.from_json(@input)
end
rescue
@error = "Error decoding transaction."
Expand Down
2 changes: 1 addition & 1 deletion app/views/blocks/script.html.haml
Expand Up @@ -11,7 +11,7 @@
= link_to "#{@tx.hash}:#{@txin.tx_idx}", tx_path(@tx.hash)
- else
Not found
(#{link_to "Link to this script", "/script?script_sig=#{@script_sig_str.gsub("\n", " ")}&pk_script=#{@pk_script_str.gsub("\n", " ")}&signature_hash=#{@sig_hash_str}"})
(#{link_to "Link to this script", "/script/#{@script_sig_str.gsub("\n", " ")}/#{@pk_script_str.gsub("\n", " ")}/#{@sig_hash_str}"})
%tr
%th{style: "width: 10em"} Input Script
%td
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Expand Up @@ -5,6 +5,8 @@
match 'block/:id' => 'blocks#block', :as => :block
match 'tx/:id' => 'blocks#tx', :as => :tx
match 'script/:id' => 'blocks#script', :as => :script
match 'script/:script_sig/:pk_script' => 'blocks#script'
match 'script/:script_sig/:pk_script/:sig_hash' => 'blocks#script'
match 'script' => 'blocks#script'
match 'address/:id' => 'blocks#address', :as => :address
match 'search/:search' => 'blocks#search', :as => :search
Expand Down

0 comments on commit 7c98d55

Please sign in to comment.