Skip to content

Commit

Permalink
Add 520 byte check when creating P2SH scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
azuchi committed Jun 30, 2024
1 parent d453a62 commit a8f5e4c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/bitcoin/script/script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ def self.to_p2sh(script_hash)

# generate p2sh script with this as a redeem script
# @return [Script] P2SH script
# @raise [RuntimeError] If the script size exceeds 520 bytes
def to_p2sh
raise RuntimeError, "P2SH redeem script must be 520 bytes or less." if size > Bitcoin::MAX_SCRIPT_ELEMENT_SIZE
Script.to_p2sh(to_hash160)
end

Expand Down
10 changes: 10 additions & 0 deletions spec/bitcoin/script/script_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@
expect(subject[1].to_addr).to be nil
end
end

context 'invalid script length' do
it 'should raise error' do
boundary = Bitcoin::Script.new
520.times { boundary << OP_0 }
expect{ boundary.to_p2sh }.not_to raise_error
boundary << OP_0
expect{ boundary.to_p2sh }.to raise_error(RuntimeError)
end
end
end

describe 'p2wsh script' do
Expand Down

0 comments on commit a8f5e4c

Please sign in to comment.