Bitcoin Forum
May 23, 2024, 05:59:41 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: createrawtransaction: Invalid private key  (Read 1082 times)
redPanda (OP)
Member
**
Offline Offline

Activity: 65
Merit: 16


View Profile
November 19, 2015, 05:43:57 PM
 #1

I want to spend 0.01 tBTC from vout 0 of txid 5fbce786b0bd9f46e0511a5e8091d57900e2eb5d4132375aada3e964a75e6ca5
on testnet

from: n2eTmd37KTGhRZNJsf9tfVdCG1YejciETu (0.01 tBTC)

to:     0.005 tBTC -> 2NCo1Y7JJgLabJ5AQjBpgavyX1JLWJxGPYK
change: 0.004 tBTC -> n2eTmd37KTGhRZNJsf9tfVdCG1YejciETu
fees:   0.001 tBTC

Code:
createrawtransaction '[{"txid":"5fbce786b0bd9f46e0511a5e8091d57900e2eb5d4132375aada3e964a75e6ca5","vout":0}]'
 '{"2NCo1Y7JJgLabJ5AQjBpgavyX1JLWJxGPYK":0.005, "n2eTmd37KTGhRZNJsf9tfVdCG1YejciETu":0.004}'

output:
0100000001a56c5ea764e9a3ad5a3732415debe20079d591805e1a51e0469fbdb086e7bc5f0000
000000ffffffff0220a107000000000017a914d66d4e8e58f8e1457fd3c3dff2def8f7075fa778
87801a0600000000001976a914e7c6286efb8730ef8211964f5046e0a0e3568bf788ac00000000

the problem is when I try to sign that tx:

Code:
signrawtransaction "0100000001a56c5e...211964f5046e0a0e3568bf788ac00000000"
'[]' '["75e8fd535e581ba...12e23500a96ee675"]'

output:  Invalid private key (code -5)

I also tried with WIF format: 14xCi3eQ3AJvk...xyEQRooQNv1GGQSb
with the same output...

what's wrong ?
If I don't use the wallet of bitcoind, should I provide the "scriptPubKey"
and the redeemScript ? If yes, it's easy to find the scriptPubKey in the previous
tx but how do I create the redeemScript ?


to verify that my private Key is valid, I tested it with bitcore (node.js):

Code:
bitcore = require('bitcore');
bitcore.Networks.defaultNetwork = 'testnet';

var privateKey = new bitcore.PrivateKey('75e8fd535e581ba...12e23500a96ee675');
console.log('privateKey: ' + privateKey);          // 75e8fd535e581ba...12e23500a96ee675
var exported = privateKey.toWIF();         
console.log('exported privateKey: ' + exported);   // 14xCi3eQ3AJvk...xyEQRooQNv1GGQSb
var publicKey = privateKey.toPublicKey();
console.log('publicKey: ' + publicKey);            // 03f0435d4a6284...c14d33566929584990
var addressTest = publicKey.toAddress(bitcore.Networks.testnet);
console.log('address testnet: ' + addressTest);     // n2eTmd37KTGhRZNJsf9tfVdCG1YejciETu  <-- OK
achow101
Moderator
Legendary
*
Offline Offline

Activity: 3402
Merit: 6642


Just writing some code


View Profile WWW
November 19, 2015, 05:50:30 PM
 #2

Your command for signrawtransaction is wrong. The developer docs and this page https://en.bitcoin.it/wiki/Raw_Transactions#signrawtransaction_.3Chex_string.3E_.5B.7B.22txid.22:txid.2C.22vout.22:n.2C.22scriptPubKey.22:hex.7D.2C....5D_.5B.3Cprivatekey1.3E.2C....5D_.5Bsighash.3D.22ALL.22.5DD should help.

redPanda (OP)
Member
**
Offline Offline

Activity: 65
Merit: 16


View Profile
November 19, 2015, 07:37:11 PM
 #3

From the developer doc:
Quote
signrawtransaction <hex string> [{"txid":txid,"vout":n,"scriptPubKey":hex},...] [<privatekey1>,...] [sighash="ALL"]

Sign as many inputs as possible for raw transaction (serialized, hex-encoded).
1)
Quote
The first argument may be several variations of the same transaction concatenated together;
signatures from all of them will be combined together, along with signatures for keys in the local wallet.
I want to spend only 1 output which needs only 1 signature (P2PK):
My first argument is the result of:
Code:
createrawtransaction '[{"txid":"5fbce786b0bd9f46e0511a5e8091d57900e2eb5d4132375aada3e964a75e6ca5","vout":0}]'
 '{"2NCo1Y7JJgLabJ5AQjBpgavyX1JLWJxGPYK":0.005, "n2eTmd37KTGhRZNJsf9tfVdCG1YejciETu":0.004}'

-->  "0100000001a56c5ea764e9a3ad5a3732415debe20079d591805e1a51e0469fbdb086e7bc5f00000 00000ffffffff0260e316000000000017a91
4d66d4e8e58f8e1457fd3c3dff2def8f7075fa77887801a0600000000001976a914e7c6286efb87 30ef8211964f5046e0a0e3568bf788ac00000000"

2)
Quote
The optional second argument is an array of parent transaction outputs,
so you can create a chain of raw transactions that depend on each other before sending them to the network
This argument is optionnal: I only have 1 tx with 1 input, not a chain
(the parent tx is confirmed, I don't have a chain of tx to include in the same block)
the second argument is empty:

--> '[]'

Just to make sure: I tried with the previous tx output with no difference:
'[{"txid":"5fbce786b0bd9f46e0511a5e8091d57900e2eb5d4132375aada3e964a75e6ca5","vout":"0","scriptPubKey":"76a914e7c6286efb8730ef8211964f5046e0a0e3568bf788ac"}]'

3)
Quote
Third optional argument is an array of base58-encoded private keys that,
if given, will be the only keys used to sign the transaction.
the previous output is a simple array of one private Key (P2PK) corresponding to n2eTmd37KTGhRZNJsf9tfVdCG1YejciETu
since I don't use the bitcoind wallet, I MUST give this private Key (it's not in the wallet)
this private key should be in base-58

--> '["14xCi3eQ3AJvk...xyEQRooQNv1GGQSb"]'

4)
Quote
The fourth optional argument is a string that specifies how the signature hash
is computed, and can be "ALL", "NONE", "SINGLE", "ALL|ANYONECANPAY", "NONE|ANYONECANPAY", or "SINGLE|ANYONECANPAY".
I'm not sure about this one, If i try anything ("ALL", "NONE", "SINGLE"),
I still have the "Invalid private key" error
The default value is sighash="ALL" so this last argument is empty

The final command is the same as in my first post but with the base-58 private key: 14xCi3eQ3AJvk...xyEQRooQNv1GGQSb
with the same error: "Invalid private key (code -5)"
Code:
signrawtransaction "0100000001a56c5ea764e9a3ad5a3732415debe20079d591805e1a51e0469fbdb086e7bc5f0000000000ffffffff0260e316000000000017a914d66d4e8e58f8e1457fd3c3dff2def8f7075fa77887801a0600000000001976a914e7c6286efb8730ef8211964f5046e0a0e3568bf788ac00000000"
'[]' '["14xCi3eQ3AJvk...xyEQRooQNv1GGQSb"]'
I don't see what my problem is.
achow101
Moderator
Legendary
*
Offline Offline

Activity: 3402
Merit: 6642


Just writing some code


View Profile WWW
November 19, 2015, 07:45:52 PM
 #4

Your private key is incorrect. It should be start with a '9' or a 'c' and be 51-52 characters long. Where did you get the private keys?

redPanda (OP)
Member
**
Offline Offline

Activity: 65
Merit: 16


View Profile
November 19, 2015, 07:56:12 PM
Last edit: November 19, 2015, 08:07:02 PM by redPanda
 #5

Quote
Where did you get the private keys?
It has been generated by bitcore (node.js), for testnet.
Code:
var privateKey = new bitcore.PrivateKey();
the key has 64 char
the WIF format is 14xCi3eQ3AJvk...xyEQRooQNv1GGQSb
and has 52 char
achow101
Moderator
Legendary
*
Offline Offline

Activity: 3402
Merit: 6642


Just writing some code


View Profile WWW
November 19, 2015, 08:11:07 PM
 #6

Quote
Where did you get the private keys?
It has been generated by bitcore (node.js), for testnet.
Code:
var privateKey = new bitcore.PrivateKey();
the key has 64 char
the WIF format is 14xCi3eQ3AJvk...xyEQRooQNv1GGQSb
and has 52 char
No, WIF format is 51-52 chars and should start with '9' or 'c'. The private key you have is the raw hex code. According to the docs at https://bitcore.io/api/lib/private-key you will need to use the toWIF() function in order to get the WIF key which you need. You cannot use the raw hex key.

redPanda (OP)
Member
**
Offline Offline

Activity: 65
Merit: 16


View Profile
November 19, 2015, 08:38:52 PM
 #7

OK but here is a simple example:

Code:
bitcore = require('bitcore');
bitcore.Networks.defaultNetwork = 'testnet';
var privateKey = new bitcore.PrivateKey();
console.log('privateKey: ' + privateKey);
var exported = privateKey.toWIF();
console.log('privateKey toWIF: ' + exported);
and the output is:
Code:
privateKey:       13421db757900dae2ecd471b2cca0d950f8ca47fd867e3c44dadd64f57194252
privateKey toWIF: 1eSGCW9EJ8pUZ6QEpa3385at83n3hdXrJ57VpmmFQRSv3JcMVHW
achow101
Moderator
Legendary
*
Offline Offline

Activity: 3402
Merit: 6642


Just writing some code


View Profile WWW
November 19, 2015, 08:48:45 PM
 #8

OK but here is a simple example:

Code:
bitcore = require('bitcore');
bitcore.Networks.defaultNetwork = 'testnet';
var privateKey = new bitcore.PrivateKey();
console.log('privateKey: ' + privateKey);
var exported = privateKey.toWIF();
console.log('privateKey toWIF: ' + exported);
and the output is:
Code:
privateKey:       13421db757900dae2ecd471b2cca0d950f8ca47fd867e3c44dadd64f57194252
privateKey toWIF: 1eSGCW9EJ8pUZ6QEpa3385at83n3hdXrJ57VpmmFQRSv3JcMVHW
Something is definitely wrong with your install of Bitcore. It is not producing the proper WIF key. Again, it should start with a 'c' or '9'. In most cases, it should be a 'c'

redPanda (OP)
Member
**
Offline Offline

Activity: 65
Merit: 16


View Profile
November 19, 2015, 09:20:45 PM
 #9

I made 2 tests with bitaddress.org (livenet)

<privkey> WIF = 5JoVxXdyoVFPaVL4xHQUvdb8vVRDFCdeh8A16fodXMiYKmrgtCu
<pubkey>        = 1KLgFamwn7cm1qQSnuFVuYw99dmWUVZC9b

<privkey> WIF = 5K1xnQXX5d1xVaRab75HbLT6oPejddN1eUdesL8epMdjRgHVuqG
<pubkey>       = 1Npcxoh99nGGf829uJhvMWan4vUbTzfi9y

If I understand well, the private key is the same in livenet and tesnet:
the private key is just a ramdom number and the WIF format will
also be the same.

Then we calculate the <pubkey> by adding points on a elliptic curve,
so the pubkey is a pair of coordinates (x,y). Then we write the pubkey
like "04 x y" OR "02 x" (if y is positive) OR "03 x" (if y is negative).
It is only when we create the base-58 pubkey that we use a different
format so the pubkeys looks different in testnet and livenet.

Is it correct ? 
achow101
Moderator
Legendary
*
Offline Offline

Activity: 3402
Merit: 6642


Just writing some code


View Profile WWW
November 19, 2015, 09:25:56 PM
 #10

If I understand well, the private key is the same in livenet and tesnet:
the private key is just a ramdom number and the WIF format will
also be the same.
No. The WIF format will be different to indicate which network that private key is meant for. For testnet it will start with 'c' or '9'. On the mainnet it will start with '5', 'K' or 'L'

Then we calculate the <pubkey> by adding points on a elliptic curve,
so the pubkey is a pair of coordinates (x,y). Then we write the pubkey
like "04 x y" OR "02 x" (if y is positive) OR "03 x" (if y is negative).
It is only when we create the base-58 pubkey that we use a different
format so the pubkeys looks different in testnet and livenet.
Yes. Only after doing base58CheckEncoding to create the WIF format will the private key (encoded as WIF) and public key (encoded as an address) appear different.

redPanda (OP)
Member
**
Offline Offline

Activity: 65
Merit: 16


View Profile
November 19, 2015, 09:31:56 PM
 #11

OK, Thanks a lot

I have to figure what's wrong with my bitcore module...
redPanda (OP)
Member
**
Offline Offline

Activity: 65
Merit: 16


View Profile
November 23, 2015, 07:09:38 PM
 #12

UPDATE:

instead of:
Code:
bitcore.Networks.defaultNetwork = 'testnet'; WRONG
use:
Code:
bitcore.Networks.defaultNetwork = bitcore.Networks.testnet;
The private now is OK (starts with "c").
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!