Bitcoin Forum
December 08, 2016, 08:03:31 AM *
News: To be able to use the next phase of the beta forum software, please ensure that your email address is correct/functional.
 
   Home   Help Search Donate Login Register  
Pages: [1]
  Print  
Author Topic: FindAndDelete method in script.h  (Read 947 times)
MatthewLM
Legendary
*
Offline Offline

Activity: 1092



View Profile WWW
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.

Bitcoin Extra Wallet | Peercoin Android Wallet
BTC: 1D5A1q5d192j5gYuWiP3CSE5fcaaZxe6E9  PPC: PH7fVn1Xs7nkUFmdwCX2ZRYfLPCSwGxAq9
1481184211
Hero Member
*
Offline Offline

Posts: 1481184211

View Profile Personal Message (Offline)

Ignore
1481184211
Reply with quote  #2

1481184211
Report to moderator
1481184211
Hero Member
*
Offline Offline

Posts: 1481184211

View Profile Personal Message (Offline)

Ignore
1481184211
Reply with quote  #2

1481184211
Report to moderator
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
MatthewLM
Legendary
*
Offline Offline

Activity: 1092



View Profile WWW
June 23, 2012, 08:02:37 PM
 #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++;
}
}
}

Bitcoin Extra Wallet | Peercoin Android Wallet
BTC: 1D5A1q5d192j5gYuWiP3CSE5fcaaZxe6E9  PPC: PH7fVn1Xs7nkUFmdwCX2ZRYfLPCSwGxAq9
genjix
Legendary
*
expert
Offline Offline

Activity: 1232


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:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!