Bitcoin Forum
November 08, 2024, 02:11:53 AM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: FindAndDelete method in script.h  (Read 1207 times)
MatthewLM (OP)
Legendary
*
Offline Offline

Activity: 1190
Merit: 1004


View Profile
June 23, 2012, 05:10:13 PM
 #1

Hello I'm looking here: https://github.com/bitcoin/bitcoin/blob/master/src/script.h#L482

This is used to remove OP_CODE_SEPARATOR codes and signatures. So from what I can tell the OP_CODE_SERPARATOR codes are just removed if they match the next operator in the loop. For signatures, how do they match? Do the signatures have to match the entire push operation? It seems like it may also match signatures if they come after each other in a push operation like:

OP_PUSHDATA1 <num_of_bytes_to_push_for_3_sigs> <sig_bytes> <sig_bytes> <sig_bytes>

So it would match if the signature repeats like that? Or would it only match the first one? Also it seems to me that data in front of the signature would cause the signature not to be removed. Any data after the signature is ignored. Is this right?

Signature not removed?:

OP_PUSHDATA1 <num_of_bytes_to_push> <arbitrary_data> <sig_bytes>

Signature removed but not arbitrary data?:

OP_PUSHDATA1 <num_of_bytes_to_push> <sig_bytes> <arbitrary_data>

Obviously this is important to make OP_CHECKSIG etc. to work.
MatthewLM (OP)
Legendary
*
Offline Offline

Activity: 1190
Merit: 1004


View Profile
June 23, 2012, 08:02:37 PM
Last edit: June 23, 2012, 08:38:41 PM by MatthewLM
 #2

I have this for the signatures. Is it correct? It searches for push operations and then goes to the beginning of the data to check for a signature. If a signature is found then it is removed:

Code:
void CBSubScriptRemoveSignature(u_int8_t * subScript,u_int32_t * subScriptLen,CBScriptStackItem signature){
if (signature.data == NULL) return; // Signature zero
u_int8_t * ptr = subScript;
u_int8_t * end = subScript + *subScriptLen;
for (;ptr < end;) {
if (*ptr && *ptr < 78) { // Push
// Move to data for push and record movement to next operation. No checking of push data bounds since it should have already been done.
u_int32_t move;
if (*ptr < 75) {
move = *ptr;
ptr++;
}else if (*ptr == CB_SCRIPT_OP_PUSHDATA1){
move = ptr[1];
ptr += 2;
}else if (*ptr == CB_SCRIPT_OP_PUSHDATA2){
move = ptr[1];
move += ptr[2] << 8;
ptr += 3;
}else{ // PUSHDATA4
move = ptr[1];
move += ptr[2] << 8;
move += ptr[3] << 16;
move += ptr[4] << 24;
ptr += 5;
}
// Check within bounds
if (ptr + signature.length >= end)
break;
// Check signature
if (memcmp(ptr, signature.data, signature.length)) {
// Remove signature
memmove(ptr, ptr + signature.length, end - (ptr + signature.length));
end -= signature.length;
*subScriptLen -= signature.length; // Length adjustment.
}
// Move to next operation
ptr += move;
}else{
ptr++;
}
}
}
genjix
Legendary
*
expert
Offline Offline

Activity: 1232
Merit: 1076


View Profile
June 26, 2012, 12:10:33 PM
 #3

I'm not sure what you're asking. Yes it deletes code separators.

Check my code here (I think it is easier to understand):

https://gitorious.org/libbitcoin/libbitcoin/blobs/master/src/script.cpp

Search for bool script::op_checksigverify(...)

You see the look where I create the script_code object? It loops from the last codehash position until the end of all the operations, skipping the signature or any codeseparators. Everything else is pushed to the new script object which is then used for the check_signature function.

I would suggest not copying Bitcoin's representation of scripts and instead store the parse tree for scripts. It makes it much easier for the user from an API perspective. Just remember that coinbases are unparsable so you'll need either a special script type or opcode for a raw_script (which is uninterpretable).
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!