Skip to content

Only require 32 byte scripts on the out payment txo, not on every txo in#3

Open
kahuang wants to merge 2 commits into
dcposch:masterfrom
kahuang:master
Open

Only require 32 byte scripts on the out payment txo, not on every txo in#3
kahuang wants to merge 2 commits into
dcposch:masterfrom
kahuang:master

Conversation

@kahuang

@kahuang kahuang commented Aug 10, 2022

Copy link
Copy Markdown
Contributor

the transaction...

@netlify

netlify Bot commented Aug 10, 2022

Copy link
Copy Markdown

Deploy Preview for distracted-gates-f46892 canceled.

Name Link
🔨 Latest commit 1994519
🔍 Latest deploy log https://app.netlify.com/sites/distracted-gates-f46892/deploys/62f3c6c615b7d70008a23913

Comment thread submitter/index.ts
return;
}
const targetHeight = Math.min(btcLatestHeight, mirrorLatestHeight + 30);
const targetHeight = Math.min(btcLatestHeight, mirrorLatestHeight + 20);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with 30 I was running into rate limiting from the getblocks api

Comment thread src/BtcProofUtils.sol
(nOutScriptBytes, offset) = readVarInt(rawTx, offset);
require(nOutScriptBytes <= 32, "Scripts over 32 bytes unsupported");
txOut.scriptLen = uint32(nOutScriptBytes);
txOut.script = bytes32(rawTx[offset:offset + nOutScriptBytes]);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

won't this conversion to bytes32 fail if the slice is longer than 32 bytes?

we can still remove the restriction on input scripts, but we may have to do something like

if (nOutScriptBytes > 32) txOut.script = 0x0;
else txOu.script = bytes32(...);

... and just add a comment on the script field in the struct that it'll be 0 if scriptLen is over 32

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea wasn't sure how much surgery to do if tests were looking @ this

Is there a reason to parse in txes at all? We don't seem to verify anything from them

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the conversion truncates, so it doesn't break, its just not 100% correct in the struct. In any case, we validate for the only tx that matters in the function that calls it. Can clean this up before we go live

bytes constant b0 = hex"0000000000000000000000000000000000000000";

// validate a btc testnet tx
bytes32 constant testHash = hex"0000000082316dc75c041439bad0de12bd5d63d60072f6d998beab875b7d6bf5";

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment thread src/BtcProofUtils.sol
offset += 4;
uint256 nInScriptBytes;
(nInScriptBytes, offset) = readVarInt(rawTx, offset);
require(nInScriptBytes <= 32, "Scripts over 32 bytes unsupported");

@dcposch dcposch Aug 10, 2022

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one more thing we could do.

so a bit of digging shows that scriptSig (output script) should always be < 32 bytes for all standard bitcoin transaction types

but scriptPubKey (input script) can come from anywhere & can be longer

you could remove bytes32 script entirely from TxInput for some gas savings.

bitcoin mirror users want to prove transactions pay certain people (that's the main use cases) and maybe that a transaction spends a previous utxo (BitcoinTxIn.prevTxId) but i can't think of any reason a user would ever need to prove things about the scriptPubKey (and if they do, they can use offset+length information this parser gives them to read it themselves directly)

@dcposch dcposch left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, i just tested and apparently solidity lets you cast a longer byte array to bytes32 by simply truncating

this is unambiguous in our case--if scriptLen > 32, then script is truncated

LGTM

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants