Bitcoin Forum
May 12, 2024, 06:34:44 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: How to use OP_CHECKSEQUENCEVERIFY  (Read 1411 times)
sonicskye (OP)
Jr. Member
*
Offline Offline

Activity: 41
Merit: 3


View Profile WWW
June 06, 2017, 07:44:48 AM
 #1

Dear all,

I am currently studying the OP_CHECKSEQUENCEVERIFY (OP_CSV) opcode and trying to create transactions manually. I have this construction

OP_IF
   2 [PUBKEY A] [PUBKEY B] 2 OP_CHECKMULTISIG
OP_ELSE
   [CSV VALUE] OP_NOP3 OP_DROP
   OP_DUP OP_HASH160 [PUBKEYHASH B] OP_EQUALVERIFY OP_CHECKSIG
OP_ENDIF

So far I have succeeded redeeming the P2SH tx above through the multisignature (the OP_IF.. block). But I have problems when redeeming the transaction through the OP_ELSE.. OP_ENDIF block which is when the OP_CSV is evaluated. I have set the [CSV VALUE] to 0x0a in hexadecimal (or 10 in decimal).

My current effort was to set the nSequence and the nLockTime variables to be more than the CSV value. I have created a raw tx to redeem the OP_CSV as follows

01000000010c3e18ff26e98ba39381c84d2fb9e8e198e63d0b3697f9bd57f63577c96da23f00000 000d5483045022100fc4f7bfa3c536e743b02af8b7de5d4052f43db54f59692478b25c585b9df21 1a02203cfcfc0ed618fae6aa49b11e803ec4e5654551fb52fe2d026929f4a307fe2ef0012103d7c 6052544bc42eb2bc0d27c884016adb933f15576a1a2d21cd4dd0f2de0c37d004c67635221025e37 e03703f001de34123b513beaf0e4044a2dd39a1dd92ec1706f184920031a2103d7c6052544bc42e b2bc0d27c884016adb933f15576a1a2d21cd4dd0f2de0c37d52ae67010ab27576a914937fe2ee82 229d282edec2606c70e755875334c088ac680f0000000130750000000000001976a914937fe2ee8 2229d282edec2606c70e755875334c088ac0f000000

In the raw tx, I have set the nSequence to 15 and nLockTime to 15 (I know this is not a good idea, but I'm desperate).

The raw tx itself was rejected in many pushtx services, except the blockcypher, although it has not been confirmed to the blockchain.
https://live.blockcypher.com/btc/tx/bcfbb172403ba73643ae02d40cac1332b75173d9726917dd8263a15ce365c012/

Please let me know if you have any suggestions. Thank you.
1715538884
Hero Member
*
Offline Offline

Posts: 1715538884

View Profile Personal Message (Offline)

Ignore
1715538884
Reply with quote  #2

1715538884
Report to moderator
1715538884
Hero Member
*
Offline Offline

Posts: 1715538884

View Profile Personal Message (Offline)

Ignore
1715538884
Reply with quote  #2

1715538884
Report to moderator
"You Asked For Change, We Gave You Coins" -- casascius
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715538884
Hero Member
*
Offline Offline

Posts: 1715538884

View Profile Personal Message (Offline)

Ignore
1715538884
Reply with quote  #2

1715538884
Report to moderator
achow101
Moderator
Legendary
*
expert
Offline Offline

Activity: 3388
Merit: 6635


Just writing some code


View Profile WWW
June 06, 2017, 04:26:22 PM
 #2

Read the BIP: https://github.com/bitcoin/bips/blob/master/bip-0112.mediawiki

Your transaction needs to have a version number of 2, not 1. There may also be some other issue, but read the BIP carefully and you will be able to find it.

sonicskye (OP)
Jr. Member
*
Offline Offline

Activity: 41
Merit: 3


View Profile WWW
June 07, 2017, 02:19:03 AM
 #3

Read the BIP: https://github.com/bitcoin/bips/blob/master/bip-0112.mediawiki

Your transaction needs to have a version number of 2, not 1. There may also be some other issue, but read the BIP carefully and you will be able to find it.

Thank you for your reply.
I have tried to use version 2. I also have read the BIP and I assume that one only need to set the nSequence variable to be higher than the OP_CSV value, it means it has nothing to do with the nLockTime. All Failed.

Someone told me that this OP_CSV is not currently active in mainnet.
piotr_n
Legendary
*
Offline Offline

Activity: 2053
Merit: 1354


aka tonikt


View Profile WWW
June 07, 2017, 08:24:55 AM
Last edit: June 07, 2017, 10:32:09 AM by piotr_n
Merited by ABCbits (1)
 #4

@sonicskye, I think your tx is failing the "minimal push" check.
it's some crazy stuff that (I think) is supposed to prevent tx malleability - don't ask me about it Smiley


but what you have to change is the place where you push value 10 onto the stack
instead of using {0x01, 0x0a}, you should use a single 0x5a (OP_10)

plus, as it was said before, you need version 2 or more - then it will work.

Check out gocoin - my original project of full bitcoin node & cold wallet written in Go.
PGP fingerprint: AB9E A551 E262 A87A 13BB  9059 1BE7 B545 CDF3 FD0E
sonicskye (OP)
Jr. Member
*
Offline Offline

Activity: 41
Merit: 3


View Profile WWW
June 07, 2017, 09:03:08 PM
 #5

@sonicskye, I think your tx is failing the "minimal push" check.
it's some crazy stuff that (I think) is supposed to prevent tx malleability - don't ask me about it Smiley


but what you have to change is the place where you push value 10 onto the stack
instead of using {0x01, 0x0a}, you should use a single 0x5a (OP_10)

plus, as it was said before, you need version 2 or more - then it will work.


Thank you for your reply. I have rechecked my script and it uses 0x01 0x0a instead of 0x5a.
Just to make it clear, according to Bitcoin Wiki (https://en.bitcoin.it/wiki/Script), does it mean if we need to push numbers from 1-16, we shall use OP_1 to OP_16?
piotr_n
Legendary
*
Offline Offline

Activity: 2053
Merit: 1354


aka tonikt


View Profile WWW
June 08, 2017, 06:44:09 AM
Last edit: June 08, 2017, 07:14:10 AM by piotr_n
 #6

does it mean if we need to push numbers from 1-16, we shall use OP_1 to OP_16?

Yes, but also a few other things that you have to mind:

https://github.com/bitcoin/bitcoin/blob/master/src/script/interpreter.cpp#L223

https://github.com/bitcoin/bitcoin/blob/master/src/script/script.h#L226

Check out gocoin - my original project of full bitcoin node & cold wallet written in Go.
PGP fingerprint: AB9E A551 E262 A87A 13BB  9059 1BE7 B545 CDF3 FD0E
amaclin
Legendary
*
Offline Offline

Activity: 1260
Merit: 1019


View Profile
June 08, 2017, 09:57:54 PM
 #7

does it mean if we need to push numbers from 1-16, we shall use OP_1 to OP_16?
It is not nessesary in p2sh redeem script to use opcodes OP_0, OP_1...OP_16 instead of pushes
but is saves one byte per input on each full bitcoin node Smiley
sonicskye (OP)
Jr. Member
*
Offline Offline

Activity: 41
Merit: 3


View Profile WWW
June 09, 2017, 11:09:55 AM
 #8

does it mean if we need to push numbers from 1-16, we shall use OP_1 to OP_16?
It is not nessesary in p2sh redeem script to use opcodes OP_0, OP_1...OP_16 instead of pushes
but is saves one byte per input on each full bitcoin node Smiley

hello, it's been a while  Grin

anyway, do you have any suggestions about this OP_CSV problem? if the push does not matter in OP_CSV as you said it only saves a byte per input. I already have set the version to 2, then what else, did I miss something?
amaclin
Legendary
*
Offline Offline

Activity: 1260
Merit: 1019


View Profile
June 09, 2017, 11:19:53 AM
 #9

anyway, do you have any suggestions about this OP_CSV problem? if the push does not matter in OP_CSV
as you said it only saves a byte per input. I already have set the version to 2, then what else, did I miss something?
I haven't dived deep in OP_CSV/OP_CLTV.
PM me about your problem - it is hard for me to read carefully everything in this topic
May be I will be able to check it next week.
piotr_n
Legendary
*
Offline Offline

Activity: 2053
Merit: 1354


aka tonikt


View Profile WWW
June 09, 2017, 02:48:57 PM
Last edit: June 09, 2017, 04:45:57 PM by piotr_n
Merited by ABCbits (1), Husna QA (1)
 #10

anyway, do you have any suggestions about this OP_CSV problem? if the push does not matter in OP_CSV as you said it only saves a byte per input. I already have set the version to 2, then what else, did I miss something?
I just told you what was the problem.
the push does matter!
why don't you just try it?

change tx version to 2 and {0x01,0x0a} to 0x5a inside the spending script, and it will work.
trust me.

obviously, you won't be able to spend the existing output through the else condition.
because it contains "illegal" pushes.
you need to make another P2SH address, for the script with 5a (instead of 010a)

Check out gocoin - my original project of full bitcoin node & cold wallet written in Go.
PGP fingerprint: AB9E A551 E262 A87A 13BB  9059 1BE7 B545 CDF3 FD0E
sonicskye (OP)
Jr. Member
*
Offline Offline

Activity: 41
Merit: 3


View Profile WWW
June 11, 2017, 05:39:30 AM
Merited by Husna QA (1)
 #11

I just told you what was the problem.
the push does matter!
why don't you just try it?

change tx version to 2 and {0x01,0x0a} to 0x5a inside the spending script, and it will work.
trust me.

obviously, you won't be able to spend the existing output through the else condition.
because it contains "illegal" pushes.
you need to make another P2SH address, for the script with 5a (instead of 010a)

your suggestion was awesome!

I managed to redeem the transaction (I created a new P2SH script without the push opcode).

https://blockchain.info/tx/4ba75dfe11988663cfcbbc8911eedee1f867d99fa744a6137e885315b3a073e1

the OP_CSV is simpler compared to OP_CLTV in my opinion, since we do not need to set the nLockTime and calculate a specific time in the future. All we need is just the nSequence with nSequence >= OP_CSV value and set the tx version to 2. Awesome! Thank you @piotr_n.
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!