MetacoSA / NBitcoin

Comprehensive Bitcoin library for the .NET framework.
MIT License
1.89k stars 849 forks source link

Signing Blockcypher Transaction using NBitcoin #665

Open echapeta opened 5 years ago

echapeta commented 5 years ago

Hello,

We are trying to post a LTC send transaction using the “New Transaction Endpoint” and the “Send Transaction Endpoint”

We have problems with “Send Transaction Endpoint” call,

We get the following error: "error": "Error reading signatures and pubkeys: Invalid signature, wrong length:

This is the code:

Dim xnetwork As Network = NBitcoin.Altcoins.Litecoin.Instance.Mainnet

Dim PKHex As String = DecryptData("E2a4gJEc2MSelnRuYZT3av6vF9VMIwtScCbWyIoKK5vcraYyFPu/yU9bmwIaJjx2p4iGsSSItvD788blexMLZqcYOkisZnoYNN5lggoeCWM=", EncryptionKey)

NOTE:  PKHex is the “private” from Blockcypher’s “Generate Address Endpoint”

Dim key1 As Key = New Key(Encoders.Hex.DecodeData(PKHex))

Dim sourcePrivateKey As BitcoinSecret = New BitcoinSecret(key1, xnetwork)

 Dim JSON1 As String = "{""inputs"":[{""addresses"": [""LSaifm3Rs1waFnYiVRzE7fg8KiqpS5E7FS""]}],""outputs"":[{""addresses"": [""LWNjk74qaxTKXPQFDpt8nidnFeB84d92g5""], ""value"": 6166}]}"
Dim T1 As String = PostHTTP("https://api.blockcypher.com/v1/ltc/main/txs/new", JSON1)

Dim T1R As Dictionary(Of String, Object) = Json2Dic(T1)
Dim ToSign As String = T1R("tosign")(0)

Dim ToSignAscii = Encoders.Hex.DecodeData(ToSign)

Dim Tsigned As String = sourcePrivateKey.PrivateKey.SignMessage(ToSignAscii)
Dim HexTsigned As String = String2Hex(Tsigned)

Dim nPubKey As String = sourcePrivateKey.PubKey.ToString(xnetwork)
Dim hexpubkey As String = String2Hex(nPubKey)

Dim JSON0 As String = T1.Substring(0, T1.Length - 1)
Dim JSON2 As String = JSON0 & ",""signatures"": [ """ & HexTsigned & """ ], ""pubkeys"": [ """ & hexpubkey & """ ]}"

Dim TConf As String = PostHTTP("https://api.blockcypher.com/v1/ltc/main/txs/send?token=23e0dd9b4bca4b8bbbdf1eaf69645b22", JSON2)

This is the response from “Send Transaction Endpoint”

{ "errors": [ { "error": "Error reading signatures and pubkeys: Invalid signature, wrong length: 483051453674534C7A4275625A326F7070574A795050386A66357A4574767846482F495370374C595146644542474C62446F73487A614C43466D4A39654D48325645314179786842594E31473363747A426D7535644C6F3D." } ], "tx": { "block_height": -1, "block_index": -1, "hash": "d2271219e0740c95766ad40fb177b3846a347ecdd8118bcc2e776a7c396a0748", "addresses": [ "LSaifm3Rs1waFnYiVRzE7fg8KiqpS5E7FS", "LWNjk74qaxTKXPQFDpt8nidnFeB84d92g5" ], "total": 1020985, "fees": 4700, "size": 119, "preference": "high", "relayed_by": "181.165.207.243", "received": "2019-03-06T19:02:24.468037508Z", "ver": 1, "double_spend": false, "vin_sz": 1, "vout_sz": 2, "confirmations": 0, "inputs": [ { "prev_hash": "58c6cf93b069413878cd4ac55ad9b695a6375de29f32dc3110342edf2477dea0", "output_index": 1, "output_value": 1025685, "sequence": 4294967295, "addresses": [ "LSaifm3Rs1waFnYiVRzE7fg8KiqpS5E7FS" ], "script_type": "pay-to-pubkey-hash", "age": 1562185 } ], "outputs": [ { "value": 6166, "script": "76a9147a52ad46d474b67a5afa31a41569be58e4b5de7988ac", "addresses": [ "LWNjk74qaxTKXPQFDpt8nidnFeB84d92g5" ], "script_type": "pay-to-pubkey-hash" }, { "value": 1014819, "script": "76a91450b64499d00c6847023ed0be2009a8a27c51bd6188ac", "addresses": [ "LSaifm3Rs1waFnYiVRzE7fg8KiqpS5E7FS" ], "script_type": "pay-to-pubkey-hash" } ] }, "tosign": [ "" ] }

Can you help us ?, do you have any working examples using NBitcoin ?

NicolasDorier commented 5 years ago

I am not sure what you are trying to sign.

Read https://programmingblockchain.gitbooks.io/programmingblockchain/content/ to understand how bitcoin works.

echapeta commented 5 years ago

Hello Nicolas !

We have to use Blockcypher because give us access to LTC and other alttokens besides BTC. For Ethereum we are using Nethereum.

According to Blockcypher, we must sign:

b2c44f31aab5fc26ab0f4d2f088abeeedc91a70d7f4201d616b54f1a8ccfd181

The code above outputs this signature:

H0QE6tSLzBubZ2oppWJyPP8jf5zEtvxFH/ISp7LYQFdEBGLbDosHzaLCFmJ9eMH2VE1AyxhBYN1G3ctzBmu5dLo=

Converted to Hex as Blockcypher requests:

483051453674534C7A4275625A326F7070574A795050386A66357A4574767846482F495370374C595146644542474C62446F73487A614C43466D4A39654D48325645314179786842594E31473363747A426D7535644C6F3D

Giving that, Blockcypher says:

"error": "Error reading signatures and pubkeys: Invalid signature, wrong length: 483051453674534C7A4275625A326F7070574A795050386A66357A4574767846482F495370374C595146644542474C62446F73487A614C43466D4A39654D48325645314179786842594E31473363747A426D7535644C6F3D."

Can you help us?

Thank You !!!!!

NicolasDorier commented 5 years ago

I can't help you I don't know how Blockcypher works, I only know bitcoin. Please read the book to understand how bitcoin works.

echapeta commented 5 years ago

I will try with the Blockcypher people.

Thank You!

xarvis11 commented 2 years ago

Hi @echapeta were you able to resolve this issue? I'm facing the same problem. appreciate your help :)

NicolasDorier commented 2 years ago

I advise you against using BlockCypher and instead using https://github.com/dgarage/NBXplorer/ which relies on your own node and is light weight.

There is lot's of code sample on how to use it in C#