From a8f5e4cde399f263463a5d01fdf8962869dbd82a Mon Sep 17 00:00:00 2001 From: azuchi Date: Sun, 30 Jun 2024 15:02:17 +0900 Subject: [PATCH] Add 520 byte check when creating P2SH scripts --- lib/bitcoin/script/script.rb | 2 ++ spec/bitcoin/script/script_spec.rb | 10 ++++++++++ 2 files changed, 12 insertions(+) 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