Bitcoin Forum

Bitcoin => Bitcoin Technical Support => Topic started by: trout on October 30, 2013, 07:02:32 PM



Title: howto nlocktime
Post by: trout on October 30, 2013, 07:02:32 PM
hi,

can anyone explain me how to make an Nlocktime transaction?

I just want to send all my funds to myself to be spendable in a year or so.
Since it's "all my funds" I want to make sure it's safe.

also, will the current version of multibit and bitcoin-qt handle correclty a
already spendable transacion which had an non-0 nlocktime?

thanks


PS Why do I want to do this - to block myself from gambling


Title: Re: howto nlocktime
Post by: dserrano5 on October 30, 2013, 07:39:56 PM
can anyone explain me how to make an Nlocktime transaction?

As far as I know you have to createrawtransaction, then edit the raw hexadecimal by hand :). When edited, issue a decoderawtransaction to check that everything's ok, then finally sendrawtransaction.


Title: Re: howto nlocktime
Post by: trout on October 30, 2013, 08:31:38 PM
can anyone explain me how to make an Nlocktime transaction?

As far as I know you have to createrawtransaction, then edit the raw hexadecimal by hand :). When edited, issue a decoderawtransaction to check that everything's ok, then finally sendrawtransaction.
hm. is there at least a tool to convert a raw transaction to a readable form _and back_?


Title: Re: howto nlocktime
Post by: dserrano5 on October 30, 2013, 08:54:15 PM
hm. is there at least a tool to convert a raw transaction to a readable form _and back_?

Sorry I know of none.


Title: Re: howto nlocktime
Post by: trout on November 01, 2013, 12:08:08 AM
alright, how do I find nlocktime in the hex?


Title: Re: howto nlocktime
Post by: TheButterZone on November 01, 2013, 04:41:49 AM
can anyone explain me how to make an Nlocktime transaction?

As far as I know you have to createrawtransaction, then edit the raw hexadecimal by hand :). When edited, issue a decoderawtransaction to check that everything's ok, then finally sendrawtransaction.
hm. is there at least a tool to convert a raw transaction to a readable form _and back_?

https://blockchain.info/decode-tx
then
https://blockchain.info/pushtx


Title: Re: howto nlocktime
Post by: dserrano5 on November 01, 2013, 09:48:24 AM
hm. is there at least a tool to convert a raw transaction to a readable form _and back_?

https://blockchain.info/decode-tx
then
https://blockchain.info/pushtx

The OP is specifically interested in encoding transactions, and those (otherwise useful) pages can't do that. 'createrawtransaction' does but it doesn't accept setting the nlocktime.

@OP: anyway, according to the wiki (https://en.bitcoin.it/wiki/Protocol_specification#tx) nlocktime is always the last item in the transaction, ie the last four bytes. You'll see that your raw transaction ends with 4 zero bytes. Play with them until you achieve your desired nlocktime. Beware, it's a little endian integer:

Code:
01000000 =>          1: block 1
0a000000 =>         10: block 10
ff000000 =>        255: block 255
00010000 =>        256: block 256
00020000 =>        512: block 512
00100000 =>       4096: block 4096
00000001 =>   16777216: block 16777216
ff64cd1d =>  499999999: block 499999999
0065cd1d =>  500000000: Nov 5 1985 00:53:20 UTC, note it isn't a block number anymore!
805ac352 => 1388534400: Jan 1 2014 00:00:00 UTC

So if you have this transaction:

Code:
01000000010fff030dfce93c3e4457af8ad0cd47c184a9671db28cb3cb1188b4366ce5906800000000473045022100a4b143ff81dd6ab899aef5234a02054354de8a309eff811d654cd21d6e0b536102207100585568dafba3663720a710aa0cafed27e2379292c246cf0b3cb5b21b3579ffffffff01905f0100000000001976a91428d4cb88f005376046ab1fca64ae9c8a4bcfc73a88ac00000000

which decodes to:

Code:
{
   "lock_time":0,
   "inputs":[ blah blah ],
   "vout_sz":1,
   "hash":"72d9c8951e543ce7a370aec3c3007e8f4e6e3d244a770631ae55a23b00457be4",
   "vin_sz":1,
   "out":[ blah blah ],
   "size":156,
   "version":1
}

and change the last 4 bytes:

Code:
01000000010fff030dfce93c3e4457af8ad0cd47c184a9671db28cb3cb1188b4366ce5906800000000473045022100a4b143ff81dd6ab899aef5234a02054354de8a309eff811d654cd21d6e0b536102207100585568dafba3663720a710aa0cafed27e2379292c246cf0b3cb5b21b3579ffffffff01905f0100000000001976a91428d4cb88f005376046ab1fca64ae9c8a4bcfc73a88ac1b787352

you obtain an identical transaction except for the nlocktime (and the hash of course)

Code:
{
   "lock_time":1383299099,
   "inputs":[ blah blah ],
   "vout_sz":1,
   "hash":"ace99d8d6301982e7978997a7c47f8807314eaaca5c3f7b03576b2d036faed2c",
   "vin_sz":1,
   "out":[ blah blah ],
   "size":156,
   "version":1
}

That value corresponds to Nov 1 2013 09:44:59 UTC (ie right now).


Title: Re: howto nlocktime
Post by: trout on November 01, 2013, 11:18:59 AM
hm. is there at least a tool to convert a raw transaction to a readable form _and back_?

https://blockchain.info/decode-tx
then
https://blockchain.info/pushtx

The OP is specifically interested in encoding transactions, and those (otherwise useful) pages can't do that. 'createrawtransaction' does but it doesn't accept setting the nlocktime.

@OP: anyway, according to the wiki (https://en.bitcoin.it/wiki/Protocol_specification#tx) nlocktime is always the last item in the transaction, ie the last four bytes. You'll see that your raw transaction ends with 4 zero bytes. Play with them until you achieve your desired nlocktime. Beware, it's a little endian integer:

Code:
01000000 =>          1: block 1
0a000000 =>         10: block 10
ff000000 =>        255: block 255
00010000 =>        256: block 256
00020000 =>        512: block 512
00100000 =>       4096: block 4096
00000001 =>   16777216: block 16777216
ff64cd1d =>  499999999: block 499999999
0065cd1d =>  500000000: Nov 5 1985 00:53:20 UTC, note it isn't a block number anymore!
805ac352 => 1388534400: Jan 1 2014 00:00:00 UTC

So if you have this transaction:

Code:
01000000010fff030dfce93c3e4457af8ad0cd47c184a9671db28cb3cb1188b4366ce5906800000000473045022100a4b143ff81dd6ab899aef5234a02054354de8a309eff811d654cd21d6e0b536102207100585568dafba3663720a710aa0cafed27e2379292c246cf0b3cb5b21b3579ffffffff01905f0100000000001976a91428d4cb88f005376046ab1fca64ae9c8a4bcfc73a88ac00000000

which decodes to:

Code:
{
   "lock_time":0,
   "inputs":[ blah blah ],
   "vout_sz":1,
   "hash":"72d9c8951e543ce7a370aec3c3007e8f4e6e3d244a770631ae55a23b00457be4",
   "vin_sz":1,
   "out":[ blah blah ],
   "size":156,
   "version":1
}

and change the last 4 bytes:

Code:
01000000010fff030dfce93c3e4457af8ad0cd47c184a9671db28cb3cb1188b4366ce5906800000000473045022100a4b143ff81dd6ab899aef5234a02054354de8a309eff811d654cd21d6e0b536102207100585568dafba3663720a710aa0cafed27e2379292c246cf0b3cb5b21b3579ffffffff01905f0100000000001976a91428d4cb88f005376046ab1fca64ae9c8a4bcfc73a88ac1b787352

you obtain an identical transaction except for the nlocktime (and the hash of course)

Code:
{
   "lock_time":1383299099,
   "inputs":[ blah blah ],
   "vout_sz":1,
   "hash":"ace99d8d6301982e7978997a7c47f8807314eaaca5c3f7b03576b2d036faed2c",
   "vin_sz":1,
   "out":[ blah blah ],
   "size":156,
   "version":1
}

That value corresponds to Nov 1 2013 09:44:59 UTC (ie right now).

great, thanks for the detailed answer! I think should be able to do this now.



Title: Re: howto nlocktime
Post by: b!z on November 01, 2013, 01:01:49 PM
can anyone explain me how to make an Nlocktime transaction?

As far as I know you have to createrawtransaction, then edit the raw hexadecimal by hand :). When edited, issue a decoderawtransaction to check that everything's ok, then finally sendrawtransaction.

Really? I thought you would do it manually, but on a computer. It would be hard to do it by hand, with a pen and a piece of paper.


Title: Re: howto nlocktime
Post by: dserrano5 on November 01, 2013, 01:31:26 PM
Really? I thought you would do it manually, but on a computer. It would be hard to do it by hand, with a pen and a piece of paper.

You could say that, yes… but you'll agree that having to edit raw hexadecimal output in 2013 qualifies as "by hand" or "manually", as opposed to "automatically". Turns out that "manually" comes from latin "manus" meaning "hand".


Title: Re: howto nlocktime
Post by: Abdussamad on November 02, 2013, 11:52:01 AM
Is nlocktime supported by bitcoin nodes? I remember reading on this forum that most nodes don't support it.


Title: Re: howto nlocktime
Post by: flatfly on November 02, 2013, 02:04:35 PM
Nlocktime is not properly supported at this time, so you can't do what you want with it, as far as I know, unfortunately.

However a friend of mine has developed a generic self-discipline time-lock type of app that could, among other things, help you secure those coins. He made it specifically for dealing with gambling addiction issues.

If you're interested, I will check with him what's the status on that.


Title: Re: howto nlocktime
Post by: trout on November 02, 2013, 03:05:35 PM
Nlocktime is not properly supported at this time, so you can't do what you want with it, as far as I know, unfortunately.

that's a bad news... and just as I was about to spend half a day creating and editing those raw transactions.

However a friend of mine has developed a generic self-discipline time-lock type of app that could, among other things, help you secure those coins. He made it specifically for dealing with gambling addiction issues.

If you're interested, I will check with him what's the status on that.

yes, I'm interested. Provided it's  secure and trust-free


Title: Re: howto nlocktime
Post by: flatfly on November 02, 2013, 10:21:36 PM
Nlocktime is not properly supported at this time, so you can't do what you want with it, as far as I know, unfortunately.

that's a bad news... and just as I was about to spend half a day creating and editing those raw transactions.

However a friend of mine has developed a generic self-discipline time-lock type of app that could, among other things, help you secure those coins. He made it specifically for dealing with gambling addiction issues.

If you're interested, I will check with him what's the status on that.

yes, I'm interested. Provided it's  secure and trust-free

Sure, I will check and let you know.


Title: Re: howto nlocktime
Post by: gmaxwell on November 02, 2013, 11:18:40 PM
Nlocktime is supported fine, replacement is not. But the OP's request doesn't need replacement.

He needs to create a transaction moving his coins to a key he keeps safe,  preferably signed with single|anyone_can_pay  so fees could be added later, with an nlocktime for the future... and then he needs to destroy the private key the coins are currently assigned to.

I do not recommend this— at least not for long spans of time, no one knows what the future holds. Future discovered security issues may make a transaction authored today no longer good in the future— unlikely, perhaps, but no one will promise otherwise.


Title: Re: howto nlocktime
Post by: jabetizo on November 02, 2013, 11:30:16 PM
I just did a test with small amounts using this tool: http://brainwallet.org/#tx (all outgoing transactions from 1FBirjB4q1MLH4gZXTKXNUfgMzuQDx49rh have a lock time: example1 (http://blockexplorer.com/rawtx/8d1e62608be2e7042a3dc091235ebf95b2eca353d1557ffbb3d569c0864267b7), example2 (http://blockexplorer.com/rawtx/c1d55e6be0f94034c4728e33f4a54507e87f1c637f1c24df1c11bca849ff5e65))

As well as setting the "lock_time" to the future block number or unix time, you have to change the "sequence" to 0, otherwise the lock_time is disregarded.

I tried to broadcast the tx with https://blockchain.info/pushtx but it's not supported ("invalid sequence").

Broadcasting it with BitcoinQT works, but only when the lock_time has been reached, otherwise it won't broadcast ("transaction rejected").


Title: Re: howto nlocktime
Post by: dooglus on November 07, 2014, 05:03:22 AM
As well as setting the "lock_time" to the future block number or unix time, you have to change the "sequence" to 0, otherwise the lock_time is disregarded.

Changing the sequence other than 4294967295 (the default) is enough.

From the code (with details omitted):

"a transaction is final if the locktime is less than the blockheight or blocknumber (depending on whether the locktime is low or high, resp.)
otherwise a transaction isn't final if any of its inputs aren't final
otherwise it's final

an input is final if its sequence is the biggest it can be (4294967295)"

Code:
bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
{
    if ((int64_t)tx.nLockTime < ((int64_t)tx.nLockTime < LOCKTIME_THRESHOLD ? (int64_t)nBlockHeight : nBlockTime))
        return true;
    BOOST_FOREACH(const CTxIn& txin, tx.vin)
        if (!txin.IsFinal())
            return false;
    return true;
}

class CTxIn
{
public:
    bool IsFinal() const
    {
        return (nSequence == std::numeric_limits<unsigned int>::max());
    }
}


Title: Re: howto nlocktime
Post by: hhanh00 on November 07, 2014, 12:01:00 PM
People won't hold your transaction in their memory pool for a year...


Title: Re: howto nlocktime
Post by: dooglus on November 07, 2014, 04:12:38 PM
People won't hold your transaction in their memory pool for a year...

I think you misunderstand.

You can't send a transaction to the p2p network until the timelock has expired.

Until then you keep the transaction somewhere safe, and wait.

I tested this with a transaction I made with an nLockTime 10 seconds in the future.

The first time I tried to broadcast it, it was still timelocked:

Quote
$ bitcoin-cli sendrawtransaction $tx
error: {"code":-26,"message":"64: non-final"}

The second time, it worked:

Quote
$ bitcoin-cli sendrawtransaction $tx
8b8486d428cd5aade180437b09ce95f65ca2e9f3e3b5ea69f6ca9c47f1bd249c

Edit: there's a free 0.1 BTC for the first person to discover why I was giggling like a schoolboy when I made that transaction.


Title: Re: howto nlocktime
Post by: hhanh00 on November 07, 2014, 05:45:04 PM
People won't hold your transaction in their memory pool for a year...

I think you misunderstand.

You can't send a transaction to the p2p network until the timelock has expired.

Until then you keep the transaction somewhere safe, and wait.

I tested this with a transaction I made with an nLockTime 10 seconds in the future.

The first time I tried to broadcast it, it was still timelocked:

Quote
$ bitcoin-cli sendrawtransaction $tx
error: {"code":-26,"message":"64: non-final"}

The second time, it worked:

Quote
$ bitcoin-cli sendrawtransaction $tx
8b8486d428cd5aade180437b09ce95f65ca2e9f3e3b5ea69f6ca9c47f1bd249c

Yes but that's just what bitcoind does. It won't relay non-standard tx and using nlocktime makes the tx non standard. The protocol doesn't say that. It should accept it and wait until the locktime expires before allowing mining. However, this feature could be easily abused.

From main.cpp
Code:
    // Treat non-final transactions as non-standard to prevent a specific type
    // of double-spend attack, as well as DoS attacks. (if the transaction
    // can't be mined, the attacker isn't expending resources broadcasting it)
    // Basically we don't want to propagate transactions that can't be included in
    // the next block.

Quote
Edit: there's a free 0.1 BTC for the first person to discover why I was giggling like a schoolboy when I made that transaction.
Because the address sent to is 1LASS?


Title: Re: howto nlocktime
Post by: dooglus on November 07, 2014, 05:56:34 PM
Edit: there's a free 0.1 BTC for the first person to discover why I was giggling like a schoolboy when I made that transaction.

Because the address sent to is 1LASS?

Nope, it's slightly ruder than that.


Title: Re: howto nlocktime
Post by: SpanishSoldier on November 07, 2014, 06:42:59 PM
Edit: there's a free 0.1 BTC for the first person to discover why I was giggling like a schoolboy when I made that transaction.

Because the address sent to is 1LASS?

Nope, it's slightly ruder than that.

Well it must be something to do with this...

Code:
{"ver":1,"size":225,"inputs":[{"sequence":142620501,"prev_out":{"spent":true,"tx_index":68671943,"type":0,"addr":"1JDBngCAx8Vc4MXKo5Ye54rg4eEsgovfUT","value":8990000,"n":1,"script":"76a914bcc8228412c4780c304847487816226b55a4088a88ac"},"script":"4730440220273d23cb2bdc6f6b4a9c9e112c06a6399cc8ba7ffe5f32fda777e5b595482fed022016f4f0148d86b571b2d7ad29953c7d6de4c9d15706d1bbb4b017baf6aa0916bf0121022e9d2a251a345c77ce8ded0ceae6906bcdb83d15cf2ce4890600bbda105cb2d0"}],"double_spend":false,"time":1415337539,"block_height":328916,"tx_index":68673323,"vin_sz":1,"hash":"8b8486d428cd5aade180437b09ce95f65ca2e9f3e3b5ea69f6ca9c47f1bd249c","vout_sz":2,"relayed_by":"209.190.122.2","out":[{"spent":false,"tx_index":68673323,"type":0,"addr":"1LASSfaBQbSAxyA4feP82fLDDEUwNbC5S3","value":1000000,"n":0,"script":"76a914d2335f3362c0b82f624b6c32d3829831f8e5b4b388ac"},{"spent":false,"tx_index":68673323,"type":0,"addr":"1JDBngCAx8Vc4MXKo5Ye54rg4eEsgovfUT","value":7980000,"n":1,"script":"76a914bcc8228412c4780c304847487816226b55a4088a88ac"}]}

Might be because it is sent from 1JD.


Title: Re: howto nlocktime
Post by: hhanh00 on November 08, 2014, 01:47:05 AM
Edit: there's a free 0.1 BTC for the first person to discover why I was giggling like a schoolboy when I made that transaction.

Because the address sent to is 1LASS?

Nope, it's slightly ruder than that.
ASS?


Title: Re: howto nlocktime
Post by: bit3000 on August 07, 2015, 10:09:49 PM
hi, I've done some successful transactions using nlocktime and changing sequence numbers.
I used brainwallet service to do the raw transaction, but now that site is dead.
What method can I use to do a nlocktime transaction?


Title: Re: howto nlocktime
Post by: Muhammed Zakir on August 08, 2015, 03:18:59 AM
hi, I've done some successful transactions using nlocktime and changing sequence numbers.
I used brainwallet service to do the raw transaction, but now that site is dead.
What method can I use to do a nlocktime transaction?

https://coinb.in/#newTransaction


Title: Re: howto nlocktime
Post by: bit3000 on August 08, 2015, 12:25:07 PM
https://coinb.in/#newTransaction
thanks, but what is the "script" field in the Inputs box?


Title: Re: howto nlocktime
Post by: Muhammed Zakir on August 08, 2015, 03:27:39 PM
https://coinb.in/#newTransaction
thanks, but what is the "script" field in the Inputs box?

All inputs will be loaded manually when you enter an address or private key*.

Script determines how the person who receive that Bitcoins can access it. Read more at https://en.bitcoin.it/wiki/Script.

You can use http://btc.blockr.io/api/v1/tx/info/txid api to get raw transaction data. It displays script.

* I recommend entering an address instead of private key and sign unsigned raw transaction using Bitcoin Core or any other client which supports signing raw transaction.


Title: Re: howto nlocktime
Post by: TheButterZone on August 28, 2015, 07:17:58 PM
Edit: there's a free 0.1 BTC for the first person to discover why I was giggling like a schoolboy when I made that transaction.

Because the address sent to is 1LASS?

Nope, it's slightly ruder than that.

Well it must be something to do with this...

Code:
{"ver":1,"size":225,"inputs":[{"sequence":142620501,"prev_out":{"spent":true,"tx_index":68671943,"type":0,"addr":"1JDBngCAx8Vc4MXKo5Ye54rg4eEsgovfUT","value":8990000,"n":1,"script":"76a914bcc8228412c4780c304847487816226b55a4088a88ac"},"script":"4730440220273d23cb2bdc6f6b4a9c9e112c06a6399cc8ba7ffe5f32fda777e5b595482fed022016f4f0148d86b571b2d7ad29953c7d6de4c9d15706d1bbb4b017baf6aa0916bf0121022e9d2a251a345c77ce8ded0ceae6906bcdb83d15cf2ce4890600bbda105cb2d0"}],"double_spend":false,"time":1415337539,"block_height":328916,"tx_index":68673323,"vin_sz":1,"hash":"8b8486d428cd5aade180437b09ce95f65ca2e9f3e3b5ea69f6ca9c47f1bd249c","vout_sz":2,"relayed_by":"209.190.122.2","out":[{"spent":false,"tx_index":68673323,"type":0,"addr":"1LASSfaBQbSAxyA4feP82fLDDEUwNbC5S3","value":1000000,"n":0,"script":"76a914d2335f3362c0b82f624b6c32d3829831f8e5b4b388ac"},{"spent":false,"tx_index":68673323,"type":0,"addr":"1JDBngCAx8Vc4MXKo5Ye54rg4eEsgovfUT","value":7980000,"n":1,"script":"76a914bcc8228412c4780c304847487816226b55a4088a88ac"}]}

Might be because it is sent from 1JD.

Did anyone win the BTC0.1, dooglus? I see "SAxy" in there.