diff --git a/lib/bitcoin/script/script.rb b/lib/bitcoin/script/script.rb index ea53e92..b25d454 100644 --- a/lib/bitcoin/script/script.rb +++ b/lib/bitcoin/script/script.rb @@ -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 diff --git a/spec/bitcoin/script/script_spec.rb b/spec/bitcoin/script/script_spec.rb index 72ac2d5..c64419d 100644 --- a/spec/bitcoin/script/script_spec.rb +++ b/spec/bitcoin/script/script_spec.rb @@ -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