Skip to content

Commit

Permalink
Add 10000 bytes check when creating P2WSH script
Browse files Browse the repository at this point in the history
  • Loading branch information
azuchi committed Jun 30, 2024
1 parent a8f5e4c commit 85c77ee
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/bitcoin/script/script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ def self.to_multisig_script(m, pubkeys, sort: false)
end

# generate p2wsh script for +redeem_script+
# @param [Script] redeem_script target redeem script
# @param [Script] p2wsh script
# @param [Bitcoin::Script] redeem_script target redeem script
# @return [Bitcoin::Script] p2wsh script
# @raise [ArgumentError] If the script size exceeds 10,000 bytes
def self.to_p2wsh(redeem_script)
raise ArgumentError, 'P2WSH witness script must be 10,000 bytes or less.' if redeem_script.size > Bitcoin::MAX_SCRIPT_SIZE
new << WITNESS_VERSION_V0 << redeem_script.to_sha256
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 @@ -182,6 +182,16 @@
expect(subject.to_addr).to eq('tb1q8nsuwycru4jyxrsv2ushyaee9yqyvvp2je60r4n6yjw06t88607sajrpy8')
end
end

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

describe 'P2TR script' do
Expand Down

0 comments on commit 85c77ee

Please sign in to comment.